import numpy as np
from PIL import Image
import datetime

def voronoi(point_families, shape):
    depthmap = np.ones(shape, np.float) * 1e308
    colormap = np.zeros(shape, np.int)

    def hypot(X, Y):
        min_length = np.ones(shape, np.float) * 1e308
        for [x, y] in family:
            length = (X - x) ** 2 + (Y - y) ** 2
            min_length = np.where(length < min_length, length, min_length)
        return min_length
    for i, family in enumerate(point_families):
        paraboloid = np.fromfunction(hypot, shape)
        colormap = np.where(paraboloid < depthmap, i + 1, colormap)
        depthmap = np.where(paraboloid < depthmap, paraboloid, depthmap)
    return colormap

#
# def voronoi_default(points, shape):
#     depthmap = np.ones(shape, np.float) * 1e308
#     colormap = np.zeros(shape, np.int)
#
#     def hypot(X, Y):
#         return (X - x) ** 2 + (Y - y) ** 2
#     for i, (x, y) in enumerate(points):
#         paraboloid = np.fromfunction(hypot, shape)
#         colormap = np.where(paraboloid < depthmap, i + 1, colormap)
#         depthmap = np.where(paraboloid < depthmap, paraboloid, depthmap)
#
#     # for (x, y) in points:
#     #     colormap[x - 1:x + 2, y - 1:y + 2] = 0
#
#     return colormap

def draw_map(colormap):
    shape = colormap.shape

    palette = np.array([
        0x000000FF,
        0xFF0000FF,
        0x00FF00FF,
        0xFFFF00FF,
        0x0000FFFF,
        0xFF00FFFF,
        0x00FFFFFF,
        0xFFFFFFFF,
    ])

    colormap = np.transpose(colormap)
    pixels = np.empty(colormap.shape + (4,), np.int8)

    pixels[:, :, 3] = palette[colormap] & 0xFF
    pixels[:, :, 2] = (palette[colormap] >> 8) & 0xFF
    pixels[:, :, 1] = (palette[colormap] >> 16) & 0xFF
    pixels[:, :, 0] = (palette[colormap] >> 24) & 0xFF

    image = Image.frombytes("RGBA", shape, pixels)
    image.save('voronoi.png')

def border(colormap):
    max_value = np.max(colormap)
    boundary_grid = np.ones((colormap.shape[0], colormap.shape[1]), np.int)
    for i in range(1, max_value+1):
        x = 0
        for rows in colormap:
            y = 0
            for _ in rows:
                if x != 0 and y != 0 and x != colormap.shape[0] - 1 and y != colormap.shape[1] - 1:
                    if colormap[x - 1, y] == i and colormap[x - 1, y - 1] == i and colormap[x, y - 1] == i and colormap[
                        x + 1, y - 1] == i and colormap[x + 1, y] == i and colormap[x + 1, y + 1] == i and colormap[x, y + 1] == i and \
                            colormap[x - 1, y + 1] == i:
                        boundary_grid[x, y] = 0
                y = y + 1
            x = x + 1
    boundary_grid[0, :] = 0
    boundary_grid[:, 0] = 0
    boundary_grid[colormap.shape[0] - 1, :] = 0
    boundary_grid[:, colormap.shape[1] - 1] = 0
    return boundary_grid

# def get_points(grid):
#     points = list()
#     x = 0
#     for rows in grid:
#         y = 0
#         for column in rows:
#             if column == 1:
#                 points.append([x, y])
#             y = y + 1
#         x = x + 1
#     return points

def start(point_families, shape):
    print("start voronoi algorithm")
    a = datetime.datetime.now()
    voronoi_array = voronoi(point_families, (shape[1], shape[0]))
    # print(points)
    # points = ([10,30],[30,20],[40,40])
    # voronoi_array = voronoi(points, (100,100))
    voronoi_array = np.transpose(voronoi_array)
    border_array = border(voronoi_array)
    b = datetime.datetime.now()
    print('time taken')
    print(b - a)
    return border_array

# def partition_obstacles(grid):
#     obstacles = []
#     rest_point = (0, 0)
#     x = 0
#     for rows in grid:
#         y = 0
#         for column in rows:
#             if column == 0:
#                 rest_point = (x, y)
#                 obstacles.append(find_family(x,y))
#             y = y + 1
#         x = x + 1
#     return obstacles
#
#
# grid = []
# def find_family(x, y):

# if __name__ == '__main__':
#     points = get_points(1)
#     voronoi_array = voronoi(points)
#     border_array = border(voronoi_array)
#     draw_map(border_array)
#     # print(get_points(1))


import math
from scipy import ndimage
import tkinter
from tkinter import Frame, Tk, BOTH, Text, Menu, END,Button,Label,Scale,DoubleVar,IntVar
from tkinter.filedialog import Open, SaveAs
import cv2
import numpy as np
class Main(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.d = IntVar(value=12)
        self.sigma = IntVar(value=16)
        self.initUI()

    def initUI(self):
        self.parent.title("Digital Image Processing")
        self.pack(fill=BOTH, expand=1)

        menubar = Menu(self.parent)
        self.parent.config(menu=menubar)

        fileMenu = Menu(menubar)
        fileMenu.add_command(label="Open Image1", command=self.onOpen)
        fileMenu.add_command(label="Save", command=self.onSave)
        fileMenu.add_separator()
        fileMenu.add_command(label="Exit", command=self.quit)
        menubar.add_cascade(label="File", menu=fileMenu)

        # label for the slider
        slider_label = Label(
            self.parent,
            text='D:'
        )

        slider_label.place(x=10,y=50)

        #  slider
        slider =Scale(
            self.parent,
            from_=4,
            to=30,
            resolution = 2,
            orient='horizontal',  # vertical
            variable=self.d,
            command=self.onChangeValue
        )

        slider.place(x=100,y=50)

         # label for the slider
        slider_label2 = Label(
            self.parent,
            text='Sigma Color/Space:'
        )

        slider_label2.place(x=10,y=100)

        #  slider
        slider2 =Scale(
            self.parent,
            from_=2,
            to=100,
            resolution = 4,
            orient='horizontal',  # vertical
            variable=self.sigma,
            command=self.onChangeValue
        )

        slider2.place(x=100,y=100)

# Set the position of button on the top of window.

        self.txt = Text(self)
        self.txt.pack(fill=BOTH, expand=1)

    def onOpen(self):
        ftypes = [('Images', '*.jpg *.tif *.bmp *.gif *.png')]
        dlg = Open(self, filetypes = ftypes)
        fl = dlg.show()

        if fl != '':
            global imgin
            imgin = cv2.imread(fl)
            # imgin = cv2.imread(fl,);
            cv2.namedWindow("ImageIn 1", cv2.WINDOW_AUTOSIZE)
            cv2.imshow("ImageIn 1", imgin)
            self.onChangeValue(None)

    def onSave(self):
        ftypes = [('Images', '*.jpg *.tif *.bmp *.gif *.png')]
        dlg = SaveAs(self,filetypes = ftypes);
        fl = dlg.show()
        if fl != '':
            cv2.imwrite(fl,imgout)

    def distance(self,i, j):
        return np.absolute(i-j)

    def bilateral_filter(self,i,j,d,I,sigma_d,sigma_r):
        arr=[]
        sum_num=0
        sum_den=0
        for k in range(i-math.floor(d/2),i+math.ceil(d/2)):
            for l in range(j-math.floor(d/2),j+math.ceil(d/2)):
                if k >= I.shape[0]:
                    k = k- I.shape[0]
                if l >= I.shape[1]:
                    l = l - I.shape[1]
                term1=(((i-k)**2)+(j-l)**2)/(sigma_d**2*2)
                term2=(self.distance(I[i,j],I[k,l]))/(sigma_r**2*2)
                term=term1+term2
                w=math.exp(-term)
                arr.append(w)
                sum_num=sum_num+(I[k,l]*w)
                sum_den=sum_den+w
        return sum_num/sum_den

    def onChangeValue(self,event):
        global imgout
        imgout = np.copy(imgin)
        imgout = cv2.bilateralFilter(imgin,self.d.get(),self.sigma.get(),self.sigma.get())
        cv2.imshow("Bilateral Filter",imgout)

np.seterr(over='ignore')
root = Tk()
Main(root)
root.geometry("640x480+100+100")
root.mainloop()


import json

person1={
        'id':1,
        'name':'azita',
        'family':'shuz',
        'age':42,
        'avg':17.86,
        'marital':True,
        'children':[{'name':'shanix','age':9},{'name':'pasash','age':4}]
        }

print(person1)
print(type(person1))
jstr=json.dumps(person1)
print(jstr)
print(type[jstr])
print(len(jstr))
print(jstr[2])

with open('file2.json','w')as file2:
    file2.write(jstr)


from flask import Flask, render_template, request
import os
import sys
from werkzeug.utils import secure_filename
import Model_Prediction
from utility import read_params

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'prediction_file'

@app.route('/' , methods = ['GET'])
def index():
    return render_template("index.html")

@app.route('/hello' , methods = ['GET','POST'])
def hello():
    if request.method == 'POST':
        file = request.files['file']
        filename = secure_filename(file.filename)
        if filename.endswith(".csv") :
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            config1 = read_params(config_path="config/params.yaml")
            modelpred = Model_Prediction.ModelPrediction(config=config1)
            modelpred.predict()
            return render_template("download.html")
        else :
            return ("Invalid File")

if __name__ == '__main__':
    app.run(debug=True)


from . import dht
from pyA20.gpio import gpio
from pyA20.gpio import port
import datetime

class DHTInterface(object):
    '''
    classdocs
    '''

    def __init__(self, pa_number=0, sensor=22, debug=False):
        '''
        Constructor
        '''
        # initialize GPIO
        if 0 == pa_number:
            self._pin = port.PA0
        elif 1 == pa_number:
            self._pin = port.PA1

        self._sensor = sensor

        self._debug = debug
        gpio.init()

        # read data using pin
        self._instance = dht.DHT(pin=self._pin, sensor=sensor)

    def get_valeurs(self):
        counter = 0
        result = self._instance.read()
        while not result.is_valid() and counter < 20:
            result = self._instance.read()
            counter += 1

        if self._debug:
            print("Last valid input: " + str(datetime.datetime.now()))
            print("Temperature: {0:0.1f} C".format(result.temperature))
            print("Humidity: {0:0.1f} %%".format(result.humidity))

        return result.temperature, result.humidity

    def get_sensor(self):
        return self._sensor


from logging import getLogger

logger = getLogger(__name__)

import sqlite3

from .util import match as util_match
from .const import Const as const

class Sqlite3Helper:

    def __init__(self, conf):

        self.table_name = const.DB_TABLE_NAME
        self.column_names = const.DB_COLUMN_NAMES
        self.column_types = const.DB_COLUMN_TYPES

        self.conn = sqlite3.connect(conf['db']['path'], timeout=600)
        self.conn.row_factory = sqlite3.Row
        self.cur = self.conn.cursor()

    def create_table(self):
        create = []

        for ii, name in enumerate(self.column_names):
            create.append('%s %s' % (name, self.column_types[ii]))

        create = ','.join(create)

        create = 'create table if not exists %s (%s)' % (self.table_name,
                                                         create)
        self.cur.execute(create)
        self.conn.commit()

    def insert_many(self, data):
        questions = ','.join(['?' for ii in self.column_names])
        insert = '%s' % ','.join(self.column_names)
        insert = 'insert or ignore into %s (%s) values (%s)' % (self.table_name,
                                                      insert,
                                                      questions)
        self.cur.executemany(insert, data)
        self.conn.commit()

    def select(self, where='', base_match=''):
        select = ','.join(self.column_names)
        select = 'select %s from %s ' % (select, self.table_name)

        if where:
            select += 'where %s' % where

        select += ' order by tag'

        self.cur.execute(select)
        results = self.cur.fetchall()

        if base_match:
            results_match = []

            for result in results:
                if util_match.base_match(base_match,
                                         result['cluster'],
                                         result['host'],
                                         result['date']):
                    results_match.append(result)
            return results_match

        else:
            return results

    def update(self, update, where):
        update = 'update %s set %s where %s' % (self.table_name,
                                                update,
                                                where)
        self.cur.execute(update)
        self.conn.commit()

    def update_many(self, update, where, data):
        update = 'update %s set %s where %s' % (self.table_name,
                                                update,
                                                where)
        self.cur.executemany(update, data)
        self.conn.commit()

    def close(self):
        self.conn.commit()


# pylint:disable=E1101
""" Simple flappy bird game implementation """
import sys
import random
import pygame
import argparse
from agent import Agent

# load images and their sizes
BACKGROUND = pygame.image.load("assets/img/background.png")
BACKGROUND_WIDTH, BACKGROUND_HEIGHT = BACKGROUND.get_size()
BASE_IMAGE = pygame.image.load("assets/img/base.png")
BASE_IMAGE_WIDTH, BASE_IMAGE_HEIGHT = BASE_IMAGE.get_size()
BIRD_IMAGES_PATH = (
    "assets/img/bird_upflap.png",
    "assets/img/bird_midflap.png",
    "assets/img/bird_downflap.png",
)
BIRD_IMAGES = (
    pygame.image.load(BIRD_IMAGES_PATH[0]),
    pygame.image.load(BIRD_IMAGES_PATH[1]),
    pygame.image.load(BIRD_IMAGES_PATH[2]),
)
PIPE_IMAGE_PATH = "assets/img/pipe.png"
RESTART_IMAGE_PATH = "assets/img/restart.png"
RESTART_IMAGE = pygame.image.load(RESTART_IMAGE_PATH)
PIPE_IMAGE = pygame.image.load(PIPE_IMAGE_PATH)
PIPE_IMAGE_UPSIDE_DOWN = pygame.transform.flip(PIPE_IMAGE, False, True)

# set screen size based on background and base
SCREEN_WIDTH = BACKGROUND_WIDTH
SCREEN_HEIGHT = BACKGROUND_HEIGHT + BASE_IMAGE_HEIGHT

def main():
    global FRAMERATE, CLOCK, SCREEN, ITER, DEBUG, agent
    pygame.init()

    # parse command line arguments
    parser = argparse.ArgumentParser("flappy_bird.py")
    parser.add_argument(
        "--iter", type=int, default=1000, help="number of game iterations to run"
    )
    parser.add_argument(
        "--debug", action="store_true", help="output debug logs to stdout"
    )
    parser.add_argument(
        "--fps",
        type=int,
        choices=[30, 60, 120],
        default=120,
        help="Frames per second; 30 = normal, 60 = fast, 120 = very fast",
    )
    arguments = parser.parse_args()

    # define framerate for the game so that events are synchronized
    CLOCK = pygame.time.Clock()
    FRAMERATE = arguments.fps
    DEBUG = arguments.debug
    ITER = arguments.iter

    # initialize the agent
    agent = Agent(DEBUG)

    # create game window
    SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("Flappy Bird")

    mainGame()

def draw_text(text, font, text_color, x, y):
    """Function used to draw text on top of the screen"""
    img = font.render(text, True, text_color)
    SCREEN.blit(img, (x, y))

def reset_game(flappy, bottom_pipe_group, top_pipe_group):
    """Restarting game variables, flappy bird position and re-generating pipes"""
    game_over = False
    # clear all the pipes
    bottom_pipe_group.empty()
    top_pipe_group.empty()
    # repostion the flappy bird to the starting position
    flappy.rect.x = 100
    flappy.rect.y = int(SCREEN_HEIGHT / 2)
    flappy.velocity = 0

    score = 0
    return game_over, score

def append_scores(scores, scores_avarages, score):
    scores.append(score)
    scores_avarages.append(sum(scores) / len(scores))

def end_game(scores, scores_avarages):
    """Dumping agent's qvalues, creating plotted graphs and ending the game"""
    agent.dump_qvalues(force=True)
    save_data_to_text_file(scores, scores_avarages)
    pygame.quit()
    sys.exit()

def save_data_to_text_file(scores, scores_avarages):
    """Saves (game -> score) array to text file"""
    max_score = max(scores)
    max_game_index = scores.index(max_score) + 1
    if DEBUG:
        print(f"Max score {max_score} at game {max_game_index}")
        print(f"Avarage at the end: {scores_avarages[-1]}")
    with open("data/scores.txt", "w", encoding="utf-8") as f:
        f.write(f"Max score: {max_score} at game: {max_game_index}\n")
        f.write(f"Avarage at the end: {scores_avarages[-1]}\n")
        f.write("Scores:\n")
        f.write(f"{scores}\n")
        f.write("Avarages:\n")
        f.write(f"{scores_avarages}")

def generate_pipes(bottom_pipe_group, top_pipe_group, PIPE_GAP):
    """Generates two new pipes"""
    pipe_height = random.randint(-100, 100)
    bottom_pipe = Pipe(SCREEN_WIDTH, int(SCREEN_HEIGHT / 2) + pipe_height, -1, PIPE_GAP)
    top_pipe = Pipe(SCREEN_WIDTH, int(SCREEN_HEIGHT / 2) + pipe_height, 1, PIPE_GAP)
    bottom_pipe_group.add(bottom_pipe)
    top_pipe_group.add(top_pipe)

class Bird(pygame.sprite.Sprite):
    """Bird Sprite that is controlled by the player"""

    def __init__(self, x, y):
        # call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self)
        self.images = []
        self.index = 0
        self.counter = 0
        for bird_image in BIRD_IMAGES:
            self.images.append(bird_image)
        self.image = self.images[self.index]
        # create a rectangle of boundaries based on the image
        self.rect = self.image.get_rect()
        self.rect.center = [x, y]
        # set starting velocity to zero
        self.velocity = 0
        self.lmb_pressed = False

    def update(self, game_over, flying, agent_clicked):  # overwritten sprite function
        """Handles Bird animation and movement"""

        # movement handling
        # gravity
        if flying is True:
            self.velocity += 1
            # set a velocity limit
            self.velocity = min(self.velocity, 8)
            if self.rect.bottom < BACKGROUND_HEIGHT:
                self.rect.y += int(self.velocity)

        if game_over is False and flying is True:
            # flying up
            # lmb clicked
            # if pygame.mouse.get_pressed()[0] == 1 and self.lmb_pressed is False:
            #    self.lmb_pressed = True
            #    self.velocity = -10

            # if pygame.mouse.get_pressed()[0] == 0:
            #    self.lmb_pressed = False

            # if agent_clicked is True:
            #    agent_clicked = False
            #    self.velocity = -10

            # animation handling
            self.counter += 1
            flap_cooldown = 2

            if self.counter > flap_cooldown:
                self.counter = 0
                self.index += 1
                self.index %= len(self.images)
            self.image = self.images[self.index]

            # rotate the bird
            self.image = pygame.transform.rotate(
                self.images[self.index], self.velocity * -2
            )
        # "dead" bird effect when game is over
        elif game_over is True:
            self.image = pygame.transform.rotate(self.images[self.index], -90)
        # correct rotation when game has been restarted
        elif game_over is False and flying is False:
            self.image = pygame.transform.rotate(self.images[self.index], 0)

class Pipe(pygame.sprite.Sprite):
    """Class representing Pipes"""

    def __init__(self, x, y, position, PIPE_GAP):
        pygame.sprite.Sprite.__init__(self)
        self.image = PIPE_IMAGE
        self.rect = self.image.get_rect()
        # position 1 is from the top, -1 is from the bottom
        if position == 1:
            # flip the pipe
            self.image = PIPE_IMAGE_UPSIDE_DOWN
            self.rect.bottomleft = [x, y - int(PIPE_GAP / 2)]
        if position == -1:
            self.rect.topleft = [x, y + int(PIPE_GAP / 2)]

    def update(self, MOVE_SPEED):
        """Overload of Sprite class Update method used to move pipe to the left
        and delete it if it goes out of screen"""
        self.rect.x -= MOVE_SPEED
        if self.rect.right < 0:
            self.kill()

class Button:
    """Class representing a button used only by restart button in our case"""

    def __init__(self, x_coord, y_coord, image):
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.topleft = (x_coord, y_coord)
        self.image_width, self.image_height = self.image.get_size()
        self.image_width /= 2
        self.image_height /= 2
        self.lmb_pressed = False

    def draw(self):
        """Function to draw button on the screen roughly in the middle"""
        SCREEN.blit(
            self.image,
            (self.rect.x - self.image_width, self.rect.y - self.image_height),
        )

    def is_button_clicked(self):
        """Predicate determining whether the button has been clicked"""
        action = False
        if pygame.mouse.get_pressed()[0] == 1 and self.lmb_pressed is False:
            action = True
            self.lmp_pressed = True
        elif pygame.mouse.get_pressed()[0] == 0:
            self.lmb_pressed = False
        return action

def mainGame():
    # define game variables
    BASE_MOVE = 0
    MOVE_SPEED = 7  # by how much pixels to move a base in a single frame
    BASE_COLUMN_WIDTH = 24  # width of a single "column" of a base in pixels
    PIPE_GAP = 150  # the size of gap between top and bottom pipes
    PIPE_FREQUENCY = 900 * (30 / FRAMERATE)  # 900ms between generating new pipes
    score = 0
    scores = []
    scores_avarages = []
    last_pipe = pygame.time.get_ticks() - PIPE_FREQUENCY
    game_over = False
    agent_clicked = False
    flying = True  # bool for checking flying animation
    WAS_PIPE_PASSED = (
        False  # variable for tracking score, tracks wheter bird passed a pipe
    )
    # variables for tracking saving graphs
    last_game_plots_were_saved = 0
    were_plots_saved = False
    # define font for displaying score
    font = pygame.font.SysFont("Fira Mono", 60)
    # define colors
    white = (255, 255, 255)

    bird_group = pygame.sprite.Group()
    top_pipe_group = pygame.sprite.Group()
    bottom_pipe_group = pygame.sprite.Group()
    last_pipe = pygame.time.get_ticks() - PIPE_FREQUENCY
    closest_pipe = None

    flappy = Bird(100, int(SCREEN_HEIGHT / 2))

    bird_group.add(flappy)

    restart_button = Button(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, RESTART_IMAGE)

    # main game loop
    while True:

        CLOCK.tick(FRAMERATE)

        # draw background on screen
        SCREEN.blit(BACKGROUND, (0, 0))

        # draw pipes
        top_pipe_group.draw(SCREEN)
        bottom_pipe_group.draw(SCREEN)

        # generate new pipes
        if flying is True:
            time_now = pygame.time.get_ticks()
            # enough time has passed to generate a new pipe
            if time_now - last_pipe > PIPE_FREQUENCY:
                generate_pipes(bottom_pipe_group, top_pipe_group, PIPE_GAP)
                last_pipe = time_now

        # check what pipe to the right is the closes to flappy bird
        # it can be either 1st pipe on the list or the 2nd
        if len(bottom_pipe_group) > 0:
            if -flappy.rect.centerx + bottom_pipe_group.sprites()[0].rect.centerx > -30:
                closest_pipe = bottom_pipe_group.sprites()[0]
            else:
                closest_pipe = bottom_pipe_group.sprites()[1]

        # agent action check
        if agent.act(
            -flappy.rect.centerx + closest_pipe.rect.centerx,
            -flappy.rect.centery + closest_pipe.rect.centery,
            flappy.velocity,
        ):
            agent_clicked = True
            flappy.velocity = -10
            if DEBUG:
                print("Agent clicked")

        # draw and update bird
        bird_group.draw(SCREEN)
        bird_group.update(game_over, flying, agent_clicked)

        # draw the ground
        SCREEN.blit(BASE_IMAGE, (BASE_MOVE, BACKGROUND_HEIGHT))

        # check score
        if len(bottom_pipe_group) > 0:  # some pipes have been created
            if (
                bird_group.sprites()[0].rect.left
                > bottom_pipe_group.sprites()[0].rect.left
                and bird_group.sprites()[0].rect.right
                < bottom_pipe_group.sprites()[0].rect.right
                and WAS_PIPE_PASSED is False
            ):  # bird is between top and bottom pipe
                WAS_PIPE_PASSED = True
            if WAS_PIPE_PASSED is True:
                # and if bird has left the area between top and bottom pipe
                if (
                    bird_group.sprites()[0].rect.left
                    > bottom_pipe_group.sprites()[0].rect.right
                ):
                    score += 1
                    WAS_PIPE_PASSED = False
        draw_text(str(score), font, white, int(SCREEN_WIDTH / 2), 30)

        # check if any collision occured
        if (
            pygame.sprite.groupcollide(bird_group, bottom_pipe_group, False, False)
            or pygame.sprite.groupcollide(bird_group, top_pipe_group, False, False)
            or flappy.rect.top < 0
        ):
            game_over = True

        # check if the bird hit the ground
        if flappy.rect.bottom >= BACKGROUND_HEIGHT:
            game_over = True
            # flying = False

        if game_over is False and flying is True:
            # draw and move the base
            BASE_MOVE -= MOVE_SPEED
            if abs(BASE_MOVE) > BASE_COLUMN_WIDTH:
                BASE_MOVE = 0
            # update pipes
            bottom_pipe_group.update(MOVE_SPEED)
            top_pipe_group.update(MOVE_SPEED)

        if game_over is True:
            if DEBUG:
                print("Game over, updating scores")
            agent.update_scores()
            append_scores(scores, scores_avarages, score)
            game_over, score = reset_game(flappy, bottom_pipe_group, top_pipe_group)
            # restart_button.draw()
            # if restart_button.is_button_clicked() is True:
            #    game_over, score = reset_game(flappy, bottom_pipe_group, top_pipe_group)

        # event handling
        for event in pygame.event.get():
            # stop running the game if ESC is pressed
            if event.type == pygame.QUIT or (
                event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
            ):
                append_scores(scores, scores_avarages, score)
                end_game(scores, scores_avarages)
            # start game
            # if (
            #    event.type == pygame.MOUSEBUTTONDOWN
            #    and flying is False
            #    and game_over is False
            # ):
            #    flying = True

        # end game if we have reached game iterations
        if agent.game_count == ITER:
            end_game(scores, scores_avarages)
        # save current data
        if (
            agent.game_count % 250 == 0
            and agent.game_count != ITER
            and agent.game_count > 1
            and were_plots_saved == False
        ):
            print(f"SAVING DATA ON GAME {agent.game_count}")
            save_data_to_text_file(scores, scores_avarages)
            were_plots_saved = True
            last_game_plots_were_saved = agent.game_count
        if last_game_plots_were_saved != agent.game_count:
            were_plots_saved = False

        # update everything that has happened to the game
        pygame.display.update()

if __name__ == "__main__":
    main()


from datetime import datetime

from flask import (
    Blueprint, flash, g, redirect, render_template, request, url_for
)

from artplace.auth import login_required
from artplace.db import get_db

bp = Blueprint('explore', __name__, url_prefix='/explore')

def get_post(id, check_author=True):
    post = get_db().execute(
        'SELECT p.id, title, body, created, author_id, username'
        ' FROM post p JOIN user u ON p.author_id = u.id'
        ' WHERE p.id = ?',
        (id,)
    ).fetchone()

    if post is None:
        abort(404, "Post id {0} doesn't exist.".format(id))

    if check_author and post['author_id'] != g.user['id']:
        abort(403)

    return post

@bp.route('/')
@bp.route('/index')
@login_required
def index():
    db = get_db()
    posts = db.execute(
        'SELECT p.id, title, body, created, author_id, username, contract_id, enddate, price, lender_id, artpiece_id, image, imagetype, artpiecename, value'
        ' FROM post p JOIN user u ON p.author_id = u.id'
        ' LEFT JOIN contract c ON p.contract_id = c.id'
        ' LEFT JOIN artpiece a ON c.artpiece_id = a.id'
        ' ORDER BY created DESC'
    ).fetchall()
    wallet = db.execute(
        'SELECT w.id, owner_id, balance'
        ' FROM wallet w'
        ' WHERE owner_id = ?',
        (g.user['id'],)
    ).fetchone()

    return render_template('explore/explore.html', posts=posts, wallet=wallet)

@bp.route('/create', methods=('GET', 'POST'))
@login_required
def create():
    title = request.form['title_new']
    body = request.form['body_new']
    error = None

    if not title:
        error = 'Title is required.'

    if error is not None:
        flash(error)
    else:
        db = get_db()
        db.execute(
            'INSERT INTO post (title, body, author_id)'
            ' VALUES (?, ?, ?)',
            (title, body, g.user['id'])
        )
        db.commit()
        return redirect(url_for('explore.index'))

@bp.route('/edit', methods=('GET', 'POST'))
@login_required
def edit():
    id = request.form['post_id']
    post = get_post(id)
    title = request.form['title_edit']
    body = request.form['body_edit']
    error = None

    if not title:
        error = 'Title is required.'

    if error is not None:
        flash(error)
    else:
        db = get_db()
        db.execute(
            'UPDATE post SET title = ?, body = ?'
            ' WHERE id = ?',
            (title, body, id)
        )
        db.commit()
        return redirect(url_for('explore.index'))

@bp.route('/<int:id>/delete', methods=('GET', 'POST'))
@login_required
def delete(id):
    get_post(id)
    db = get_db()
    db.execute('DELETE FROM post WHERE id = ?', (id,))
    db.commit()
    return redirect(url_for('explore.index'))

@bp.route('/rent', methods=('GET', 'POST'))
@login_required
def rent():
    db = get_db()
    contract_id = request.form['contract_id']
    wallet_id = request.form['wallet_id']
    price = float(request.form['price'])
    post_id = request.form['post_id']
    artpiece_id = request.form['artpiece_id']
    owner_id = request.form['owner']
    # heerlijk gemaakt dit
    db.execute(
        'UPDATE contract SET borrower_id = ?'
        ' WHERE id = ?',
        (g.user['id'], contract_id)
    )
    wallet = db.execute(
        'SELECT w.id, balance'
        ' FROM wallet w'
        ' WHERE id = ?',
        (wallet_id,)
    ).fetchone()
    new_balance = wallet['balance'] - price
    db.execute(
        'UPDATE wallet SET balance = ?'
        ' WHERE id = ?',
        (new_balance, wallet_id)
    )
    db.execute(
        'UPDATE artpiece SET renter_id = ?'
        ' WHERE id = ?',
        (g.user['id'], artpiece_id)
    )
    db.execute('DELETE FROM post WHERE id = ?', (post_id,))
    owner_wallet = db.execute(
        'SELECT w.id, balance, owner_id'
        ' FROM wallet w'
        ' WHERE owner_id = ?',
        (owner_id,)
    ).fetchone()
    owner_balance = owner_wallet['balance'] + price
    db.execute(
        'UPDATE wallet SET balance = ?'
        ' WHERE id = ?',
        (owner_balance, owner_wallet['id'])
    )
    db.commit()
    return redirect(url_for('explore.index'))


import numpy as np
import re
import os
import mne

"""
This FileUtils.py module contains:

load_data - load data into a dictionary [X, y]

find_number - find the number after a specific string in text

create_input file - create the X and y files for NN input

split - splits data intro train and test with a rate of train_nr

split_kfold - splits the data set X into k folds

"""

def find_number(text, c):
	""" This function find the number after a specific string in text

		Input data:
			text - the text in which the number is searched
			c - the specific string

		Output data:
			The searched number
	"""
	return re.findall(r'%s(\d+)' % c, text)

def create_input_file(data_path, drop_ch):
	""" This function take all the signals from da DataBase and put them into an X amtrix with corresponding y tag

		Input data:
			data_path - the path of the DataBase
			drop_ch - the channel that wants to be droped

		Output data:
			The X and y array will be saved

	"""
	text_file = [f for f in os.listdir(data_path) if f.endswith('.raw.fif')]

	n_rec = len(text_file)
	X = np.zeros((n_rec,62,4000))
	y = np.zeros((n_rec,1))

	for file,rec in zip(text_file,range(len(text_file))):
		data_file = data_path + '\\' + file
		EEG_data = mne.io.read_raw_fif(data_file, preload = True)
		EEG_data.drop_channels(drop_ch)
		tag = find_number(file,'tag')

		sgn = EEG_data[:][0]
		sgn = sgn[:-2]

		X[rec,:,:] = sgn[:,500:4500]
		y[rec] = tag

	np.save(r'KaraOne_EEGSpeech_X',X)
	np.save(r'KaraOne_EEGSpeech_y',y)

def load_data(xname, yname, path = None):
	"""
	This function load the x and y data with name xname and yname from path

	Input data:
		xname - the name of x file
		yname - the name of y file
		path - the path of x nd y file. DEFAULT = None

	Output data:
		x - loaded x file
		y - loaded y file

	"""
	if(path):
		path_x = path + '/'+ xname
		path_y = path + '/' + y_name
		x = np.load(path_x)
		y = np.load(path_y)
	else:
		x = np.load(xname)
		y = np.load(yname)

	return [x, y]

def split(X, y, test_nr = 0.2, flag = 0, nr_cls = None, indexes = 0):
	"""
	This function splits data intro train and test with a rate of train_nr.

	Input data:
		X - EEG signals with dimension [nr. observations x nr. features] or [nr. observations x nr. channels x nr. features]
		y - target data. Dimension: [nr. observations]
			IMPORTANT: len(y) MUST be equal to len(X)!!!
		test_nr - the percentage of test data divided by 100. DEFAULT = 0.2 (20%)
		flag - is 0 or 1, if flag is 0 the split doesn't regard the class distribution, if flag is 1 the class will be split
			equally regarding the class target. DEFAULT = 0
		nr_cls - number of classes. If None, the classes targets will be computed using len(np.unique()). DEFAULT = None
		indexes - 0 or 1. If indexes is 0, the split indexes won't be returning. If indexes in 1, the function will return
			the indexes too. DEFAULT = 0

	Output data:
		xtrain - the training vectors
		ytrain - the training vectors target
		xtest - the test vectors
		ytest - the test vectors target

		optional:
			indxtrain - the indexes of the train vectors
			idxtest - the indexes of the test vectors

	"""

	if(len(X)!=len(y)):
		raise ValueError("X and y have different lengths")

	if(flag!= 0 and flag!=1):
		raise ValueError("It's not a valid flag number")

	if nr_cls is None:
		nr_cls = len(np.unique(y))

	dim = X.shape

	if flag==1:
		rnd_x = np.random.permutation(len(X))
		X = X[rnd_x]
		y = y[rnd_x]

		if len(dim)==3:
			xtrain = np.empty((0,dim[1],dim[2]))
			xtest = np.empty((0,dim[1],dim[2]))

		else:
			if len(dim)==2:
				xtrain = np.empty((0,dim[1]))
				xtest = np.empty((0,dim[1]))

			else:
				raise AttributeError("It's not a valid X matrix (X.shape is different from 2 or 3)")

		ytrain = np.empty((0))
		ytest = np.empty((0))

		if indexes==1:
			idxtrain = np.empty((0),dtype=int)
			idxtest = np.empty((0),dtype=int)

		for i in range(nr_cls):
			X_interm = X[np.ravel(y==i)]
			tr_word = len(X_interm) - int(len(X_interm)*test_nr)

			if indexes==1:
				idx = rnd_x[np.ravel(y==i)]
				idxtrain = np.concatenate((idxtrain,idx[:tr_word]))
				idxtest = np.concatenate((idxtest,idx[tr_word:]))

			xtrain = np.concatenate((xtrain,X_interm[:tr_word]))
			xtest = np.concatenate((xtest,X_interm[tr_word:]))
			ytrain = np.concatenate((ytrain,np.ones(len(X_interm[:tr_word]))*i))
			ytest = np.concatenate((ytest,np.ones(len(X_interm[tr_word:]))*i))

	elif flag==0:
		rnd_x = np.random.permutation(len(X))
		X = X[rnd_x]
		y = y[rnd_x]

		nr_train = len(X) - int(len(X)*test_nr)

		if indexes==1:
			idxtrain = rnd_x[:tr_word]
			idxtest = rnd_x[tr_word:]

		xtrain = X[:nr_train]
		xtest = X[nr_train:]
		ytrain = y[:nr_train]
		ytest = y[nr_train:]

	if indexes==1:
		return xtrain, ytrain, xtest, ytest, idxtrain, idxtest
	else:
		return xtrain, ytrain, xtest, ytest

def split_kfold(X, y, k=5, flag = 0, nr_cls = None, indexes = 0):

	""" This function splits the data set X into k folds

	Input Data:
		X - is the input data with dimension NxM[xL] where N is the number of observations, M [and L] is the number of features
		y - is the target values of data X
		k - the number of folds to split the data, DEFAULT = 5
		flag - can have values of 0 or 1. When flag is 1, the algorithm splits the data conserving the class distribution, when flag is 0,
		the data splits randomly without considering the number of observations in each class. DEFAULT = 0
		nr_cls - number of classes. If None, the classes targets will be computed using len(np.unique()) . DEFAULT = None
		indexes - 0 or 1. If indexes is 0, the split indexes won't be returning. If indexes in 1, the function will return
			the indexes too. DEFAULT = 0

	Output data:
		xtrain - the training vectors
		ytrain - the training vectors target
		xtest - the test vectors
		ytest - the test vectors target

		optional:
			indxtrain - the indexes of the train vectors
			idxtest - the indexes of the test vectors
	"""
	if(len(X)!=len(y)):
		raise ValueError("X and y have different lengths")

	if(flag!= 0 and flag!=1):
		raise ValueError("It's not a valid flag number")

	if nr_cls is None:
		nr_cls = len(np.unique(y))

	if flag==1:

		X_train = []
		y_train = []
		X_test = []
		y_test = []

		if indexes==1:
			idxtrain = []
			idxtest = []

		for kval in range(k): # looping over the fold

			xtr = []
			ytr = []
			xtst = []
			ytst = []

			if indexes==1:
				idtr = []
				idtst = []

			for clas in range(nr_cls): # looping over the classes
				X_interm = X[np.ravel(y==clas)] # save all observations from class that equals clas in X_interm in order to split them
												# equally further

				rnd = np.random.permutation(len(X_interm)) # make a random permutation of values, in case the observations are secvential
				X_interm = X_interm[rnd]

				nr_fold_cls = int(np.floor(len(X_interm)/k)) # computing the number of values in a folds for this class

				idx = kval*nr_fold_cls

				index_test = np.ravel(np.arange(idx,idx+nr_fold_cls)) # establish the the indexes from the X_interm which wil be for test
				index_train = np.arange(len(X_interm))
				index_train = np.ravel(np.delete(index_train, index_test)) # establish the the indexes from the X_interm which wil be for train

				xtr.extend(X_interm[index_train])					# concatenate the values every loop
				ytr.extend(np.ones((len(index_train),1))*clas)
				xtst.extend(X_interm[index_test])
				ytst.extend(np.ones((len(index_test),1))*clas)

				if indexes==1:
					idtr.extend(rnd[index_train])
					idtst.extend(rnd[index_test])

			X_train.append(xtr) # save every fold vectors
			y_train.append(ytr)
			X_test.append(xtst)
			y_test.append(ytst)

			if indexes==1:
				idxtrain.append(idtr)
				idxtest.append(idtst)

	if flag == 0:

		X_train = []
		y_train = []
		X_test = []
		y_test = []

		if indexes==1:
			idxtrain = []
			idxtest = []

		for kval in range(k):

			nr_fold_cls = int(np.floor(len(X)/k)) # computing number of observations per fold
			idx = kval*nr_fold_cls # computing the index of fold

			rnd = np.random.permutation(len(X)) # do a random permutation of the vectors in case there are secvential
			X = X[rnd]
			y = y[rnd]

			index_test = np.ravel(np.arange(idx,idx+nr_fold_cls)) # determine da indexes for the test
			index_train = np.arange(len(X))
			index_train = np.ravel(np.delete(index_train, index_test)) # determine the indexes for the train

			X_train.append(X[index_train]) # save observatons for every fold
			y_train.append(y[index_train])
			X_test.append(X[index_test])
			y_test.append(y[index_test])

			if indexes==1:
				idxtrain.append(rnd[index_train])
				idxtest.append(rnd[index_test])

	X_train = np.asarray(X_train) # transpose list into array
	X_test = np.asarray(X_test)
	y_train = np.ravel(np.asarray(y_train))
	y_test = np.ravel(np.asarray(y_test))

	if indexes==1:
		idxtrain = np.asarray(idxtrain)
		idxtest = np. asarray(idxtest)

	if indexes==1:
		return X_train, y_train, X_test, y_test, idxtrain, index_test
	else:
		return X_train, y_train, X_test, y_test

def split_leaveOneOut(x, y, idxtrain, idxtest):
	xtrain = np.concatenate((x[idxtrain[0]:idxtrain[1]],x[idxtrain[2]:idxtrain[3]]), axis=0)
	ytrain = np.concatenate((y[idxtrain[0]:idxtrain[1]],y[idxtrain[2]:idxtrain[3]]), axis=0)
	xtest = x[idxtest[0]:idxtest[1]]
	ytest = y[idxtest[0]:idxtest[1]]

	return xtrain, ytrain, xtest, ytest


import requests,json,time,random,csv
def skipCovid(id):
    ## 获取跳转地址
    url = "http://xinguan.pft.sh-weiyi.com/v2/open/service/perf/app/info?appKey=cov-qc&src=lab&orgId="+id
    headers={"Content-Type":"application/json;charset=UTF-8"}
    con = requests.get(url,headers)
    newUrl = con.text
    # 跳转新冠质管
    connection = requests.get(newUrl,headers,allow_redirects=False)
    cookie = requests.utils.dict_from_cookiejar(connection.cookies)
    return cookie
 # 查询检测体系
def queryDetectionSystem(cookie):
    queryUrl = "http://covid19-qc-lab.pft.sh-weiyi.com/service/iqc/data/item-tree"
    r = requests.get(queryUrl,cookies=cookie)
    response = json.loads(r.text)
    # try:
    #     qcId1 = response['data'][0]['children'][0]['children'][0]['id']
    #     qcId2 = response['data'][1]['children'][0]['children'][0]['id']
    #     return qcId1,qcId2
    # except Exception as e:
    #     pass
    return response
def reportData(labId,qcId1,bathNo1,qcId2,bathNo2,cookie):
    #质控数据上报
    reportUrl = "http://covid19-qc-lab.pft.sh-weiyi.com/service/iqc/data"
    data = {"labId":labId,"qcItemId":qcId1,
            "levDatas":[{"batchNo":bathNo1,"levDataId":"","result":random.randint(1,100),"qcDate":int(round(time.time() * 1000))},
             {"batchNo":bathNo1,"levDataId":"","result":random.randint(1,100),"qcDate":int(round(time.time() * 1000))}]}

    data2 = {"labId":labId,"qcItemId":qcId2,
            "levDatas":[{"batchNo":bathNo2,"levDataId":"","result":random.randint(1,100),"qcDate":int(round(time.time() * 1000))},
             {"batchNo":bathNo2,"levDataId":"","result":random.randint(1,100),"qcDate":int(round(time.time() * 1000))}]}
    response1 = requests.post(reportUrl,json=data,cookies = cookie)
    response2 = requests.post(reportUrl,json=data2,cookies = cookie)
    # print(response1.status_code)
    print(response1.text)
    print(response2.text)

## 读取csv文件数据
def readCSV():
    orgId = []
    path = "E:\PrivateFile\master\Python\covid19\查询实验室 - 5.csv"
    ## 读取某列的数据
    with open(path, 'r') as f:
        reader = csv.reader(f)
        rows = [row for row in reader]
        for i in range(1, len(rows)):
            orgId.append(rows[i][0])
    return orgId

#查询质控组合
def queryBacthGroup(cookie):
    url = 'http://covid19-qc-lab.pft.sh-weiyi.com/service/testing/batch-no-group?enabled=true'
    r1 = requests.get(url, cookies=cookie)
    response1 = json.loads(r1.text)
    batchGroup = response1['data'][0]['groupName']
    bacthNo1 = batchGroup[0:(batchGroup.index('|'))]
    bacthNo2 = batchGroup[(batchGroup.index('|') + 1):]
    return bacthNo1,bacthNo2

def writeLabId(labId):
    path = "E:\PrivateFile\master\Python\covid19\LabId.txt"
    with open (path,'a') as f:
        f.write(labId+"\n")

def queryData(qcId,cookie):
    url = "http://covid19-qc-lab.pft.sh-weiyi.com/service/iqc/data/page?qcItemId="+qcId+"&current=1&size=100"
    r = requests.get(url,cookies = cookie)
    response = json.loads(r.text)
    return response


import datetime
import logging
from django.conf import settings
from django.utils import timezone
from django.db import IntegrityError
from django.apps import apps
from ghorgs.managers import GithubOrganizationManager
from accounts.exceptions import OrganizationConfigurationError
from ..models import KippoTask, KippoTaskStatus
from ..functions import get_github_issue_category_label, get_github_issue_estimate_label

if settings.TEST:
    from octocat.mocks import GithubOrganizationManagerMock as GithubOrganizationManager

# load models from other apps
KippoProject = apps.get_model(app_label='projects', model_name='KippoProject')
ActiveKippoProject = apps.get_model(app_label='projects', model_name='ActiveKippoProject')
GithubRepository = apps.get_model(app_label='octocat', model_name='GithubRepository')
PersonalHoliday = apps.get_model(app_label='accounts', model_name='PersonalHoliday')

KippoOrganization = apps.get_model(app_label='accounts', model_name='KippoOrganization')
KippoUser = apps.get_model(app_label='accounts', model_name='KippoUser')
GITHUB_MANAGER_USER = KippoUser.objects.get(username=settings.GITHUB_MANAGER_USERNAME)

logger = logging.getLogger(__name__)

class KippoConfigurationError(Exception):
    pass

def collect_github_project_issues(kippo_organization: KippoOrganization, status_effort_date: datetime.date=None) -> tuple:
    """
    1. Collect issues from attached github projects
    2. If related KippoTask does not exist, create one
    3. If KippoTask exists create KippoTaskStatus

    :param kippo_organization: KippoOrganization
    :param status_effort_date: Date to get tasks from
    :return: processed_projects_count, created_task_count, created_taskstatus_count
    """
    # TODO: support non-update of done tasks
    # get done tasks for active projects and last week task status
    # if *still* in 'done' state do not create a new KippoTaskStatus entry

    if not status_effort_date:
        status_effort_date = timezone.now().date()

    if not kippo_organization.githubaccesstoken or not kippo_organization.githubaccesstoken.token:
        raise OrganizationConfigurationError(f'Token Not configured for: {kippo_organization.name}')

    manager = GithubOrganizationManager(organization=kippo_organization.github_organization_name,
                                        token=kippo_organization.githubaccesstoken.token)
    existing_tasks_by_html_url = {t.github_issue_html_url: t for t in KippoTask.objects.filter(is_closed=False) if t.github_issue_html_url}
    existing_open_projects = list(ActiveKippoProject.objects.filter(github_project_url__isnull=False))
    github_users = {u.github_login: u for u in KippoUser.objects.filter(github_login__isnull=False)}
    if settings.UNASSIGNED_USER_GITHUB_LOGIN not in github_users:
        raise KippoConfigurationError(f'"{settings.UNASSIGNED_USER_GITHUB_LOGIN}" must be created as a User to manage unassigned tasks')

    # collect project issues
    processed_projects = 0
    new_task_count = 0
    new_taskstatus_objects = []
    for github_project in manager.projects():
        logger.info('Processing github project ({})...'.format(github_project.name))

        # get the related KippoProject
        # --- For some reason standard filtering was not working as expected, so this method is being used...
        # --- The following was only returning a single project, 'Project(SB Mujin)'.
        # --- Project.objects.filter(is_closed=False, github_project_url__isnull=False)
        kippo_project = None
        for candiate_kippo_project in existing_open_projects:
            if candiate_kippo_project.github_project_url == github_project.html_url:
                kippo_project = candiate_kippo_project
                break

        if not kippo_project:
            logger.info('X -- Kippo Project Not found!')
        else:
            logger.info('-- KippoProject: {}'.format(kippo_project.name))
            processed_projects += 1
            logger.info('-- Processing Related Github Issues...')
            count = 0
            for count, issue in enumerate(github_project.issues(), 1):
                # check if issue is open
                # refer to github API for available fields
                # https://developer.github.com/v3/issues/
                if issue.state == 'open':
                    # add related repository as GithubRepository
                    repo_api_url = issue.repository_url
                    repo_html_url = issue.html_url.split('issues')[0]
                    name_index = -2
                    issue_repo_name = repo_html_url.rsplit('/', 2)[name_index]
                    kippo_github_repository, created = GithubRepository.objects.get_or_create(created_by=GITHUB_MANAGER_USER,
                                                                                updated_by=GITHUB_MANAGER_USER,
                                                                                project=kippo_project,
                                                                                name=issue_repo_name,
                                                                                api_url=repo_api_url,
                                                                                html_url=repo_html_url)
                    if created:
                        logger.info(f'>>> Created GithubRepository({kippo_project} {issue_repo_name})!')

                    default_task_category = kippo_github_repository.project.organization.default_task_category

                    # check if issue exists
                    existing_task = existing_tasks_by_html_url.get(issue.html_url, None)
                    assignees = [issue_assignee.login for issue_assignee in issue.assignees if issue_assignee.login in github_users]

                    if not assignees:
                        # assign task to special 'unassigned' user if task is not assigned to anyone
                        assignees = [settings.UNASSIGNED_USER_GITHUB_LOGIN]

                    estimate_denominator = len(assignees)
                    for issue_assignee in assignees:
                        issue_assigned_user = github_users.get(issue_assignee, None)
                        if not issue_assigned_user:
                            logger.warning(f'Not assigned ({issue_assignee}): {issue.html_url}')
                        else:
                            # only add task if issue is assigned to someone in the system!
                            if not existing_task:
                                category = get_github_issue_category_label(issue)
                                if not category:
                                    category = default_task_category
                                existing_task = KippoTask(
                                    created_by=GITHUB_MANAGER_USER,
                                    updated_by=GITHUB_MANAGER_USER,
                                    title=issue.title,
                                    category=category,
                                    project=kippo_project,
                                    assignee=issue_assigned_user,
                                    github_issue_api_url=issue.url,
                                    github_issue_html_url=issue.html_url,
                                    description=issue.body,
                                )
                                existing_task.save()
                                new_task_count += 1
                                logger.info(f'--> Created KippoTask: {issue.title} ({issue_assigned_user.username})')

                            # only update status if active or done (want to pick up
                            # -- this condition is only met when the task is open, closed tasks will not be updated.
                            active_task_column_names = kippo_project.columnset.get_active_column_names()
                            done_task_column_names = kippo_project.columnset.get_done_column_names()
                            task_status_updates_states = active_task_column_names + done_task_column_names
                            if issue.project_column in task_status_updates_states:
                                latest_comment = ''
                                if issue.latest_comment_body:
                                    latest_comment = f'{issue.latest_comment_created_by} [ {issue.latest_comment_created_at} ] {issue.latest_comment_body}'

                                unadjusted_issue_estimate = get_github_issue_estimate_label(issue)
                                adjusted_issue_estimate = None
                                if unadjusted_issue_estimate:
                                    # adjusting to take into account the number of assignees working on it
                                    # -- divides task load by the number of assignees
                                    adjusted_issue_estimate = unadjusted_issue_estimate/estimate_denominator

                                # create KippoTaskStatus with updated estimate
                                status = KippoTaskStatus(
                                    task=existing_task,
                                    created_by=GITHUB_MANAGER_USER,
                                    updated_by=GITHUB_MANAGER_USER,
                                    state=issue.project_column,
                                    estimate_days=adjusted_issue_estimate,
                                    effort_date=status_effort_date,
                                    comment=latest_comment
                                )
                                try:
                                    status.save()
                                    new_taskstatus_objects.append(status)
                                    logger.info(f'--> KippoTaskStatus Added: {issue.title} ({status_effort_date})')
                                except IntegrityError as e:
                                    logger.warning(f'--> KippoTaskStatus Already Exists: {issue.title} ({status_effort_date})')
                                    logger.warning(str(e))
            logger.info(f'>>> {kippo_project.name} - processed issues: {count}')

    return processed_projects, new_task_count, len(new_taskstatus_objects)

def run_collect_github_project_issues(event, context):
    for organization in KippoOrganization.objects.filter(github_organization_name__isnull=False):
        collect_github_project_issues(organization)


from typing import Tuple, Union, Deque
from core import expemu
from core import keys
from core.gdbserver import GDBServer
from collections import deque
from threading import RLock
import sys
import os
import threading
import argparse
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "1"
import pygame

CWD = os.path.abspath(os.getcwd())
ROOTFS = os.path.join(CWD, "rootfs")

BLACK = pygame.Color(0, 0, 0)

EMU_PYG_KEY_MAP = {
    keys.KEY_ON:       pygame.K_ESCAPE,
    keys.KEY_ENTER:    pygame.K_RETURN,
    keys.KEY_UP:       pygame.K_UP,
    keys.KEY_DOWN:     pygame.K_DOWN,
    keys.KEY_LEFT:     pygame.K_LEFT,
    keys.KEY_RIGHT:    pygame.K_RIGHT,
    keys.KEY_F1:       pygame.K_F1,
    keys.KEY_F2:       pygame.K_F2,
    keys.KEY_F3:       pygame.K_F3,
    keys.KEY_F4:       pygame.K_F4,
    keys.KEY_F5:       pygame.K_F5,
    keys.KEY_F6:       pygame.K_F6,
    keys.KEY_SYMB:     pygame.K_1,
    keys.KEY_PLOT:     pygame.K_2,
    keys.KEY_NUM:      pygame.K_3,
    keys.KEY_HOME:     pygame.K_q,
    keys.KEY_APPS:     pygame.K_w,
    keys.KEY_VIEWS:    pygame.K_e,
    keys.KEY_0:        pygame.K_KP_0,
    keys.KEY_1:        pygame.K_KP_1,
    keys.KEY_2:        pygame.K_KP_2,
    keys.KEY_3:        pygame.K_KP_3,
    keys.KEY_4:        pygame.K_KP_4,
    keys.KEY_5:        pygame.K_KP_5,
    keys.KEY_6:        pygame.K_KP_6,
    keys.KEY_7:        pygame.K_KP_7,
    keys.KEY_8:        pygame.K_KP_8,
    keys.KEY_9:        pygame.K_KP_9,
    keys.KEY_PLUS:     pygame.K_KP_PLUS,
    keys.KEY_SUBTRACTION: pygame.K_KP_MINUS,
    keys.KEY_MULTIPLICATION: pygame.K_KP_MULTIPLY,
    keys.KEY_DIVISION: pygame.K_KP_DIVIDE,
    keys.KEY_DOT:      pygame.K_KP_PERIOD,
    keys.KEY_LEFTBRACKET: pygame.K_LEFTBRACKET,
    keys.KEY_RIGHTBRACET: pygame.K_RIGHTBRACKET,
    keys.KEY_BACKSPACE: pygame.K_BACKSPACE,
    keys.KEY_SHIFT:    pygame.K_LSHIFT,
    keys.KEY_ALPHA:    pygame.K_CAPSLOCK,

    keys.KEY_COMMA:     pygame.K_COMMA,
    keys.KEY_VARS:      pygame.K_v,
    keys.KEY_MATH:      pygame.K_m,
    keys.KEY_ABC:       pygame.K_a,
    keys.KEY_SIN:       pygame.K_s,
    keys.KEY_COS:       pygame.K_c,
    keys.KEY_TAN:       pygame.K_t,
    keys.KEY_LOG:       pygame.K_l,
    keys.KEY_LN:        pygame.K_n,
    keys.KEY_XTPHIN:    pygame.K_x,
}

PYG_EMU_KEY_MAP = {v:k for k, v in EMU_PYG_KEY_MAP.items()}

class PYGameUIInterface(expemu.UIInterface):
    def __init__(self, screen: pygame.Surface):
        self.screen = screen
        # in memory surface, to speed up drawing
        self.surface = pygame.Surface((expemu.EMU_SCREEN_WIDTH, expemu.EMU_SCREEN_HEIGHT), depth=8)
        self.color_map = []
        for color in range(256):
            self.color_map.append(pygame.Color(color, color, color))
        self.key_event_queue: Deque[Tuple[bool, int]] = deque()
        self.gui_lock = RLock()
        self.font = pygame.font.SysFont("System", 20)

    def fill_rect(self, x, y, w, h, c):
        self.surface.fill(
            self.color_map[c & 0xFF],
            (x, y, w, h)
        )

    def get_pixel(self, x, y):
        p = self.surface.get_at((x, y))
        return sum(p.r + p.g + p.b) // (0xFF * 3)

    def draw_text(self, text, x, y, bg, fg):
        #print(f"Drawing '{text}' at {x}, {y} with color bg {bg}, fg {fg}.")
        t = self.font.render(text, True, self.color_map[fg & 0xFF], self.color_map[bg & 0xFF])
        with self.gui_lock:
            self.surface.blit(t, (x,y))

    def is_key_down(self, key_id) -> bool:
        if key_id in EMU_PYG_KEY_MAP:
            return pygame.key.get_pressed()[EMU_PYG_KEY_MAP[key_id]]
        return False

    def query_key_event(self) -> Union[None, Tuple[bool, int]]:
        if len(self.key_event_queue) <= 0:
            return None
        else:
            return self.key_event_queue.popleft()

    def app_quit(self):
        with self.gui_lock:
            pygame.quit()
            sys.exit()

    def _push_key_event(self, pressed: bool, key_id: int):
        self.key_event_queue.append((pressed, key_id))

    def _render_on_screen(self):
        with self.gui_lock:
            self.screen.blit(
                pygame.transform.scale(
                    self.surface,
                    (self.screen.get_width(), self.screen.get_height())
                ),
                (0, 0, self.screen.get_width(), self.screen.get_height())
            )
            pygame.display.flip()

def mainloop():
    # parse args from cli
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "exp",
        metavar="<ExistOS Program Path>",
        help="ExistOS program file path (*.exp)",
    )
    parser.add_argument(
        "-s", "--scale",
        default=2,
        type=int,
        metavar="Scale",
        help="Screen scale factor",
    )

    parser.add_argument(
        "-g", "--gdb",
        default=-1,
        type=int,
        metavar="PORT",
        help="Enable and set GDB Server port",
    )

    args = parser.parse_args()
    if not args.exp.lower().endswith(".exp"):
        print("Error: file should be *.exp")
        return
    # init pygame
    pygame.init()
    pygame.display.set_caption("EXP Emulator")
    # init screen
    screen = pygame.display.set_mode(
        (expemu.EMU_SCREEN_WIDTH * args.scale, expemu.EMU_SCREEN_HEIGHT * args.scale),
        vsync=True
    )
    screen.fill(BLACK)
    pygame.display.flip()
    # init GUI interface
    gui = PYGameUIInterface(screen)
    # init rootfs
    os.makedirs(ROOTFS, exist_ok=True)
    # init emu
    emu = expemu.Emulator(gui, ROOTFS, args.exp, args.gdb)
    # init pygame clock
    clock = pygame.time.Clock()
    # running
    emu_thead = threading.Thread(target=emu.emu_thread, daemon=True)
    emu_thead.start()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()
                return
            elif event.type == pygame.KEYDOWN:
                if event.key in PYG_EMU_KEY_MAP:
                    gui._push_key_event(True, PYG_EMU_KEY_MAP[event.key])
            elif event.type == pygame.KEYUP:
                if event.key in PYG_EMU_KEY_MAP:
                    gui._push_key_event(False, PYG_EMU_KEY_MAP[event.key])
        # render screen
        try:
            gui._render_on_screen()
        except pygame.error:
            running = False
        clock.tick(60.0)

if __name__ == "__main__":
    try:
        mainloop()
    except KeyboardInterrupt:
        pass


from __future__ import absolute_import, division, print_function

import gzip
import re
import zlib
from collections import Counter
from datetime import datetime
from itertools import chain

import click
from celery import group, shared_task
from celery.utils.log import get_task_logger
from elasticsearch.helpers import bulk as es_bulk
from elasticsearch.helpers import scan as es_scan
from flask import current_app, url_for
from flask_sqlalchemy import models_committed
from jsonschema import ValidationError
from redis import StrictRedis
from redis_lock import Lock
from six import text_type

from invenio_db import db
from invenio_indexer.api import RecordIndexer, current_record_to_index
from invenio_pidstore.errors import PIDDoesNotExistError
from invenio_pidstore.models import PersistentIdentifier
from invenio_search import current_search_client as es
from invenio_search.utils import schema_to_index

from inspire_dojson import marcxml2record
from inspire_dojson.utils import get_recid_from_ref
from inspire_utils.dedupers import dedupe_list
from inspire_utils.helpers import force_list
from inspire_utils.record import get_value
from inspirehep.modules.pidstore.minters import inspire_recid_minter
from inspirehep.modules.pidstore.utils import get_pid_type_from_schema
from inspirehep.modules.records.api import InspireRecord
from inspirehep.modules.records.receivers import index_after_commit

from .models import InspireProdRecords

logger = get_task_logger(__name__)

CHUNK_SIZE = 100
LARGE_CHUNK_SIZE = 2000

split_marc = re.compile('<record.*?>.*?</record>', re.DOTALL)

def chunker(iterable, chunksize=CHUNK_SIZE):
    buf = []
    for elem in iterable:
        buf.append(elem)
        if len(buf) == chunksize:
            yield buf
            buf = []
    if buf:
        yield buf

def split_blob(blob):
    """Split the blob using <record.*?>.*?</record> as pattern."""
    for match in split_marc.finditer(blob):
        yield match.group()

def split_stream(stream):
    """Split the stream using <record.*?>.*?</record> as pattern."""
    buf = []
    for row in stream:
        row = text_type(row, 'utf8')
        index = row.rfind('</record>')
        if index >= 0:
            buf.append(row[:index + 9])
            for blob in split_blob(''.join(buf)):
                yield blob.encode('utf8')
            buf = [row[index + 9:]]
        else:
            buf.append(row)

@shared_task(ignore_result=True)
def remigrate_records(only_broken=True, skip_files=None):
    """Remigrate records.

    Directly migrates the records (declared as broken), e.g. if the dojson
    conversion script have been corrected.
    """
    if skip_files is None:
        skip_files = current_app.config.get(
             'RECORDS_MIGRATION_SKIP_FILES',
             False,
        )

    query = db.session.query(InspireProdRecords)
    if only_broken:
        query = query.filter_by(valid=False)

    for i, chunk in enumerate(chunker(query.yield_per(CHUNK_SIZE))):
        records = [record.marcxml for record in chunk]
        logger.info("Processed {} records".format(i * CHUNK_SIZE))
        migrate_chunk.delay(records, skip_files=skip_files)

@shared_task(ignore_result=True)
def migrate(source, wait_for_results=False, skip_files=None):
    """Main migration function."""
    if skip_files is None:
        skip_files = current_app.config.get(
             'RECORDS_MIGRATION_SKIP_FILES',
             False,
        )

    if source.endswith('.gz'):
        fd = gzip.open(source)
    else:
        fd = open(source)

    if wait_for_results:
        # if the wait_for_results is true we enable returning results from migrate_chunk task
        # so that we could use them to synchronize migrate task (which in that case waits for
        # the migrate_chunk tasks to complete before it finishes).
        tasks = []
        migrate_chunk.ignore_result = False

    for i, chunk in enumerate(chunker(split_stream(fd), CHUNK_SIZE)):
        print("Processed {} records".format(i * CHUNK_SIZE))
        if wait_for_results:
            tasks.append(migrate_chunk.s(chunk, skip_files=skip_files))
        else:
            migrate_chunk.delay(chunk, skip_files=skip_files)

    if wait_for_results:
        job = group(tasks)
        result = job.apply_async()
        result.join()
        migrate_chunk.ignore_result = True
        print('All migration tasks have been completed.')

@shared_task(ignore_result=True)
def continuous_migration(skip_files=None):
    """Task to continuously migrate what is pushed up by Legacy."""
    if skip_files is None:
        skip_files = current_app.config.get(
             'RECORDS_MIGRATION_SKIP_FILES',
             False,
        )
    redis_url = current_app.config.get('CACHE_REDIS_URL')
    r = StrictRedis.from_url(redis_url)
    lock = Lock(r, 'continuous_migration', expire=120, auto_renewal=True)
    if lock.acquire(blocking=False):
        try:
            while r.llen('legacy_records'):
                raw_record = r.lrange('legacy_records', 0, 0)
                if raw_record:
                    migrate_and_insert_record(
                        zlib.decompress(raw_record[0]),
                        skip_files=skip_files,
                    )
                    db.session.commit()
                r.lpop('legacy_records')
        finally:
            lock.release()
    else:
        logger.info("Continuous_migration already executed. Skipping.")

def create_index_op(record):
    index, doc_type = current_record_to_index(record)

    return {
        '_op_type': 'index',
        '_index': index,
        '_type': doc_type,
        '_id': str(record.id),
        '_version': record.revision_id,
        '_version_type': 'external_gte',
        '_source': RecordIndexer._prepare_record(record, index, doc_type),
    }

@shared_task(ignore_result=False, compress='zlib', acks_late=True)
def migrate_chunk(chunk, skip_files=False):
    models_committed.disconnect(index_after_commit)

    index_queue = []

    try:
        for raw_record in chunk:
            with db.session.begin_nested():
                record = migrate_and_insert_record(
                    raw_record,
                    skip_files=skip_files,
                )
                if record:
                    index_queue.append(create_index_op(record))
        db.session.commit()
    finally:
        db.session.close()

    req_timeout = current_app.config['INDEXER_BULK_REQUEST_TIMEOUT']
    es_bulk(
        es,
        index_queue,
        stats_only=True,
        request_timeout=req_timeout,
    )

    models_committed.connect(index_after_commit)

@shared_task()
def add_citation_counts(chunk_size=500, request_timeout=120):
    def _build_recid_to_uuid_map(citations_lookup):
        pids = PersistentIdentifier.query.filter(
            PersistentIdentifier.object_type == 'rec').yield_per(1000)

        with click.progressbar(pids) as bar:
            return {
                pid.object_uuid: citations_lookup[int(pid.pid_value)]
                for pid in bar if int(pid.pid_value) in citations_lookup
            }

    def _get_records_to_update_generator(citations_lookup):
        with click.progressbar(citations_lookup.iteritems()) as bar:
            for uuid, citation_count in bar:
                yield {
                    '_op_type': 'update',
                    '_index': index,
                    '_type': doc_type,
                    '_id': str(uuid),
                    'doc': {'citation_count': citation_count}
                }

    index, doc_type = schema_to_index('records/hep.json')
    citations_lookup = Counter()

    click.echo('Extracting all citations...')
    with click.progressbar(es_scan(
            es,
            query={
                '_source': 'references.recid',
                'filter': {
                    'exists': {
                        'field': 'references.recid'
                    }
                },
                'size': LARGE_CHUNK_SIZE
            },
            scroll=u'2m',
            index=index,
            doc_type=doc_type)) as records:
        for record in records:
            unique_refs_ids = dedupe_list(list(chain.from_iterable(map(
                force_list, get_value(record, '_source.references.recid')))))

            for unique_refs_id in unique_refs_ids:
                citations_lookup[unique_refs_id] += 1
    click.echo('... DONE.')

    click.echo('Mapping recids to UUIDs...')
    citations_lookup = _build_recid_to_uuid_map(citations_lookup)
    click.echo('... DONE.')

    click.echo('Adding citation numbers...')
    success, failed = es_bulk(
        es,
        _get_records_to_update_generator(citations_lookup),
        chunk_size=chunk_size,
        raise_on_exception=False,
        raise_on_error=False,
        request_timeout=request_timeout,
        stats_only=True,
    )
    click.echo('... DONE: {} records updated with success. {} failures.'.format(
        success, failed))

def record_insert_or_replace(json, skip_files=False):
    """Insert or replace a record."""
    pid_type = get_pid_type_from_schema(json['$schema'])
    control_number = json['control_number']

    try:
        pid = PersistentIdentifier.get(pid_type, control_number)
        record = InspireRecord.get_record(pid.object_uuid)
        record.clear()
        record.update(json, skip_files=skip_files)
        if json.get('legacy_creation_date'):
            record.model.created = datetime.strptime(json['legacy_creation_date'], '%Y-%m-%d')
        record.commit()
    except PIDDoesNotExistError:
        record = InspireRecord.create(json, id_=None, skip_files=skip_files)
        if json.get('legacy_creation_date'):
            record.model.created = datetime.strptime(json['legacy_creation_date'], '%Y-%m-%d')
        inspire_recid_minter(str(record.id), json)

    if json.get('deleted'):
        new_recid = get_recid_from_ref(json.get('new_record'))
        if not new_recid:
            record.delete()

    return record

def migrate_and_insert_record(raw_record, skip_files=False):
    """Convert a marc21 record to JSON and insert it into the DB."""
    error = None

    try:
        json_record = marcxml2record(raw_record)
        if '$schema' in json_record:
            json_record['$schema'] = url_for(
                'invenio_jsonschemas.get_schema',
                schema_path='records/{0}'.format(json_record['$schema']),
            )
    except Exception as e:
        logger.exception('Migrator DoJSON Error')
        error = e

    recid = json_record['control_number']
    prod_record = InspireProdRecords(recid=recid)
    prod_record.marcxml = raw_record

    try:
        if not error:
            record = record_insert_or_replace(json_record, skip_files=skip_files)
    except ValidationError as e:
        # Aggregate logs by part of schema being validated.
        pattern = u'Migrator Validator Error: {}, Value: %r, Record: %r'
        logger.error(pattern.format('.'.join(e.schema_path)), e.instance, recid)
        error = e
    except Exception as e:
        # Receivers can always cause exceptions and we could dump the entire
        # chunk because of a single broken record.
        logger.exception('Migrator Record Insert Error')
        error = e

    if error:
        # Invalid record, will not get indexed.
        error_str = u'{0}: Record {1}: {2}'.format(type(error), recid, e)
        prod_record.valid = False
        prod_record.errors = error_str
        db.session.merge(prod_record)
        return None
    else:
        prod_record.valid = True
        db.session.merge(prod_record)
        return record


import requests
import base64
from config import API_KEY,APP_ID,SECRET_KEY
import pprint
import json

class image_identify:

	def __init__(self):

		self.image=''
		self.token=None
	#获取token
	def get_token(self):

		token_url='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+ API_KEY +'&client_secret='+SECRET_KEY
		response=requests.get(token_url)
		self.token=json.loads(response.text)['access_token']
		print(self.token)

		#进行识别，并返回words 列表
	def identify(self,image,detect_direction=False,):

		if not self.token:
			self.get_token()
		identify_url='https://aip.baidubce.com/rest/2.0/ocr/v1/accurate?access_token='+self.token
		with open(image,'rb') as f:
			data_f=base64.b64encode(f.read()).decode('utf-8')  #需要base64编码
		data={	'image':data_f,
				'detect_direction':detect_direction
				}
		response=requests.post(identify_url,data=data,headers={'Content-Type':'application/x-www-form-urlencoded'})
		pprint.pprint(response.json())
		identify_text=[word.get('words','') for word in response.json()['words_result']]
		return(identify_text)

if __name__ == '__main__':
	im_id=image_identify()
	# im_id.get_token()
	result=im_id.identify('1.jpg')
	print(result)


from typing import Any, Dict, List
from numbers import Number
import torch
import torch.distributed as dist

__all__ = [
    "all_reduce_scalar",
    "all_reduce_tensor",
    "all_reduce_dict",
    "all_gather_tensor",
]

def all_reduce_scalar(value: Number, op: str = "sum") -> Number:
    """All-reduce single scalar value. NOT torch tensor."""
    # https://github.com/NVIDIA/DeepLearningExamples/blob/master/PyTorch/LanguageModeling/Transformer-XL/pytorch/utils/distributed.py
    if dist.is_initialized() and dist.is_available():
        op = op.lower()
        if (op == "sum") or (op == "mean"):
            dist_op = dist.ReduceOp.SUM
        elif op == "min":
            dist_op = dist.ReduceOp.MIN
        elif op == "max":
            dist_op = dist.ReduceOp.MAX
        elif op == "product":
            dist_op = dist.ReduceOp.PRODUCT
        else:
            raise RuntimeError(f"Invalid all_reduce op: {op}")

        backend = dist.get_backend()
        if backend == torch.distributed.Backend.NCCL:
            device = torch.device("cuda")
        elif backend == torch.distributed.Backend.GLOO:
            device = torch.device("cpu")
        else:
            raise RuntimeError(f"Unsupported distributed backend: {backend}")

        tensor = torch.tensor(value, device=device, requires_grad=False)
        dist.all_reduce(tensor, op=dist_op)
        if op == "mean":
            tensor /= dist.get_world_size()
        ret = tensor.item()
    else:
        ret = value
    return ret

def all_reduce_tensor(tensor: torch.Tensor, op="sum", detach: bool = True) -> torch.Tensor:
    if dist.is_initialized() and dist.is_available():
        ret = tensor.clone()
        if detach:
            ret = ret.detach()
        if (op == "sum") or (op == "mean"):
            dist_op = dist.ReduceOp.SUM
        else:
            raise RuntimeError(f"Invalid all_reduce op: {op}")

        dist.all_reduce(ret, op=dist_op)
        if op == "mean":
            ret /= dist.get_world_size()
    else:
        ret = tensor
    return ret

def all_reduce_dict(result: Dict[str, Any], op="sum") -> Dict[str, Any]:
    new_result = {}
    for k, v in result.items():
        if isinstance(v, torch.Tensor):
            new_result[k] = all_reduce_tensor(v, op)
        elif isinstance(v, Number):
            new_result[k] = all_reduce_scalar(v, op)
        else:
            raise RuntimeError(f"Dictionary all_reduce should only have either tensor or scalar, got: {type(v)}")
    return new_result

def all_gather_tensor(tensor: torch.Tensor) -> List[torch.Tensor]:
    if dist.is_initialized() and dist.is_available():
        world_size = dist.get_world_size()
        local_rank = dist.get_rank()
        output = [
            tensor if (i == local_rank) else torch.empty_like(tensor) for i in range(world_size)
        ]
        dist.all_gather(output, tensor, async_op=False)
        return output
    else:
        return [tensor]


from .spliter import split
from .grouper import group
from .tokenizer import tokenize as _tokenize
from .formatting_grouper import group as space_group
from .future import has_print_function, replace_print_by_name
from .grammator import generate_parse
from .indentation_marker import mark_indentation
from .inner_formatting_grouper import group as inner_group
from .parser import ParsingError

parse_tokens = generate_parse(False)
parse_tokens_print_function = generate_parse(True)

def _parse(tokens, print_function):
    parser = parse_tokens if not print_function else parse_tokens_print_function
    try:
        try:
            return parser(tokens)
        except ParsingError:
            # swap parsers for print_function situation where I failed to find it
            parser = parse_tokens if print_function else parse_tokens_print_function
            return parser(tokens)
    except ParsingError as e:
        raise
    except Exception as e:
        import sys
        import traceback
        traceback.print_exc(file=sys.stderr)
        sys.stderr.write("%s\n" % e)
        sys.stderr.write("\nBaron has failed to parse this input. If this is valid python code (and by that I mean that the python binary successfully parse this code without any syntax error) (also consider that python does not yet parse python 3 code integrally) it would be kind if you can extract a snippet of your code that make Baron fails and open a bug here: https://github.com/PyCQA/baron/issues\n\nSorry for the inconvenience.")

def parse(source_code, print_function=None):
    # Python syntax requires source code to end with an ENDL token
    # the endl token is removed afterward if and only if it's the last token of the root level
    # It is possible that this token end up in a 'suite' grammar rule
    # which means that it is 'traped' in an indented block of code
    # I don't want to recursively cross the tree to hope to find it
    # This solution behave in the expected way for 90% of the case
    newline_appended = False
    linesep = "\r\n" if source_code.endswith("\r\n") else "\n"
    if source_code and not source_code.endswith(linesep):
        source_code += linesep
        newline_appended = True

    if print_function is None:
        tokens = tokenize(source_code, False)
        print_function = has_print_function(tokens)
        if print_function:
            replace_print_by_name(tokens)
    else:
        tokens = tokenize(source_code, print_function)

    if newline_appended:
        to_return = _parse(tokens, print_function)

        if to_return[-1]["type"] == "endl" and not to_return[-1]["formatting"]:
            return to_return[:-1]
        elif to_return[-1]["type"] == "endl" and to_return[-1]["formatting"]:
            return to_return[:-1] + to_return[-1]["formatting"]
        else:
            return to_return

    return _parse(tokens, print_function)

def tokenize(pouet, print_function=False):
    splitted = split(pouet)
    grouped = group(splitted)
    print_tokenized = _tokenize(grouped, print_function)
    space_grouped = space_group(print_tokenized)
    inner_grouped = inner_group(space_grouped)
    indentation_marked = mark_indentation(inner_grouped)
    return indentation_marked


import shutil
from pathlib import Path

import pytest
import yaml

from craft_store import StoreClient, endpoints

@pytest.fixture
def charm_client():
    """A common StoreClient for charms"""
    return StoreClient(
        application_name="integration-test",
        base_url="https://api.staging.charmhub.io",
        storage_base_url="https://storage.staging.snapcraftcontent.com",
        endpoints=endpoints.CHARMHUB,
        user_agent="integration-tests",
        environment_auth="CRAFT_STORE_CHARMCRAFT_CREDENTIALS",
    )

@pytest.fixture
def fake_charm_file(tmpdir):
    """Provide a fake charm to upload to charmhub."""
    # Make tmpdir Path instead of Path-like.
    prime_dir = Path(tmpdir) / "prime"
    prime_dir.mkdir()

    medadata_path = prime_dir / "metadata.yaml"
    with medadata_path.open("w") as metadata_file:
        yaml.safe_dump(
            data={
                "name": "craft-store-test-charm",
                "display-name": "display",
                "description": "description",
                "summary": "summary",
            },
            stream=metadata_file,
        )

    manifest_path = prime_dir / "manifest.yaml"
    with manifest_path.open("w") as manifest_file:
        yaml.safe_dump(
            data={
                "analysis": {
                    "attributes": [
                        {
                            "name": "language",
                            "result": "python",
                        },
                        {
                            "name": "framework",
                            "result": "operator",
                        },
                    ]
                },
                "bases": [
                    {
                        "architectures": ["amd64"],
                        "channel": "22.04",
                        "name": "ubuntu",
                    }
                ],
                "charmcraft-started-at": "2022-04-03T22:27:43.044456Z",
                "charmcraft-version": "1.5.0+12.g04477df.dirty",
            },
            stream=manifest_file,
        )

        charm_file = Path(tmpdir) / "test.charm"
        Path(shutil.make_archive(str(charm_file), "zip", str(prime_dir))).rename(
            charm_file
        )

        return charm_file


import numpy as np
import pandas as pd
from scipy import stats

stat_categories = ['FGM', 'FGA', 'FG_PCT', 'FG3M','FG3A', \
'FG3_PCT', 'FTM', 'FTA', 'FT_PCT', 'OREB', 'DREB', 'REB', \
'AST','STL', 'BLK', 'TOV', 'PTS']

def _get_scores(list_data_games, game):
    response = {}
    game_stat = pd.Series(game)[stat_categories]
    df = pd.DataFrame(list_data_games)
    df = df[stat_categories]
    mean = df.mean()
    std = df.std()
    diff = np.round(((game_stat - mean) / std).astype(np.double), 2)
    one_big_thing = {}

    diff["TOV"] = diff["TOV"]*-1
    pos_idx = np.array(diff).argmax()
    neg_idx = np.array(diff).argmin()
    abs_idx = np.array(np.abs(diff)).argmax()
    #diff["TOV"] = diff["TOV"]*-1

    one_big_thing["absolute"] = {"stat": diff.index[abs_idx], "score": diff.iloc[abs_idx]}
    one_big_thing["positive"] = {"stat": diff.index[pos_idx], "score": diff.iloc[pos_idx]}
    one_big_thing["negative"] = {"stat": diff.index[neg_idx], "score": diff.iloc[neg_idx]}
    one_big_thing["default"] = one_big_thing["positive"] if game["WL"] == "W" else one_big_thing["negative"]

    game = {k:game[k] for k in game.keys() if k not in stat_categories}
    #response["data"]=data
    #response["portion"]=portion
    response["scores"]=diff.to_dict()
    response["stats"]=game_stat.to_dict()
    response["game"]=game
    response["obt"]=one_big_thing
    return response

# each obt object in the list should have the "WL" set
def _get_team_season_summary(obt_list, stat_list):
    obt_df = pd.DataFrame(obt_list)
    wins = obt_df[obt_df["WL"] == "W"]["absolute"].apply(pd.Series)
    loss = obt_df[obt_df["WL"] == "L"]["absolute"].apply(pd.Series)
    wins_mode = stats.mode(wins["stat"]).mode[0]
    loss_mode = stats.mode(loss["stat"]).mode[0]

    stats_df = pd.DataFrame(stat_list)
    wins_mode_stat_list = list(stats_df[stats_df["WL"] == "W"][wins_mode])
    wins_mode_stat_avg = np.mean(stats_df[wins_mode])
    wins_mode_stat_win_avg = np.mean(stats_df[stats_df["WL"] == "W"][wins_mode])
    wins_mode_stat_loss_avg = np.mean(stats_df[stats_df["WL"] == "L"][wins_mode])
    loss_mode_stat_list = list(stats_df[stats_df["WL"] == "L"][loss_mode])
    loss_mode_stat_avg = np.mean(stats_df[loss_mode])
    loss_mode_stat_win_avg = np.mean(stats_df[stats_df["WL"] == "W"][loss_mode])
    loss_mode_stat_loss_avg = np.mean(stats_df[stats_df["WL"] == "L"][loss_mode])

    summary = {}
    wins_response = {}
    wins_response["stat"] = wins_mode
    wins_response["stat_list"] = wins_mode_stat_list
    wins_response["stat_avg"] = wins_mode_stat_avg
    wins_response["stat_win_avg"] = wins_mode_stat_win_avg
    wins_response["stat_loss_avg"] = wins_mode_stat_loss_avg
    loss_response = {}
    loss_response["stat"] = loss_mode
    loss_response["stat_list"] = loss_mode_stat_list
    loss_response["stat_avg"] = loss_mode_stat_avg
    loss_response["stat_win_avg"] = loss_mode_stat_win_avg
    loss_response["stat_loss_avg"] = loss_mode_stat_loss_avg
    summary["wins_stats"] = wins_response
    summary["loss_stats"] = loss_response
    summary["record"] = str(len(wins))+"-"+str(len(loss))
    return summary

def _get_default_data(season_id_int, game):
    portion = None
    # if playoffs, use the regular season
    if (season_id_int > 40000):
        season_id_int -= 20000
        data = str(season_id_int)

    # if at "beginning" of season, use last season as data
    #elif (game["GAME_NUM"] < 10):
    #    data = str(season_id_int-1)

    # else, take that season's games.
    else:
        data = str(season_id_int)
        #portion = "before"
    return data, portion


from datetime import datetime, timedelta
from time import time
from typing import AsyncIterable

import discord
from redbot.core.bot import Red
from redbot.core.config import Config
from tzlocal import get_localzone
from swift_i18n import Humanize

class Reminder:
    cfg: Config
    bot: Red

    def __init__(self, user: discord.User, data: dict):
        self.user = user
        self._data = data

    @classmethod
    async def all_reminders(cls, user: discord.User = None) -> AsyncIterable["Reminder"]:
        if user is None:
            users = await cls.cfg.all_users()
        else:
            users = {user.id: await cls.cfg.user(user).all()}

        for uid, data in users.items():
            for reminder in data.get("reminders", []):
                u = cls.bot.get_user(int(uid))
                if not u:
                    continue
                yield cls(user=u, data=reminder)

    @classmethod
    async def create(cls, user: discord.User, message: str, due_in: float) -> "Reminder":
        now = time()
        reminder = cls(user, {"message": message, "set_on": now, "remind_on": now + due_in})
        await reminder.save()
        return reminder

    async def save(self):
        async with self.cfg.user(self.user).reminders() as reminders:
            if self._data not in reminders:
                reminders.append(self._data)

    async def remove(self):
        async with self.cfg.user(self.user).reminders() as reminders:
            if self._data in reminders:
                reminders.remove(self._data)

    @property
    def reminder_embed(self):
        from .remindme import translate

        return discord.Embed(
            title=translate("reminder_header"),
            timestamp=self.set_on,
            description=self.message,
            colour=self.bot.color,
        ).set_footer(text=translate("reminder_set_on"))

    @property
    def reminder_text(self):
        from .remindme import translate

        return translate(
            "reminder_message",
            set_on=Humanize(self.set_on, format="long", tzinfo=get_localzone()),
            message=self.message,
        )

    async def send_reminder(self):
        if self.user.dm_channel is None:
            await self.user.create_dm()

        if await self.bot.embed_requested(self.user.dm_channel, self.user):
            await self.user.send(embed=self.reminder_embed)
        else:
            await self.user.send(content=self.reminder_text)

    @property
    def message(self):
        return self._data["message"]

    @property
    def set_on(self) -> datetime:
        return datetime.utcfromtimestamp(self._data["set_on"])

    @property
    def due_on(self) -> datetime:
        return datetime.utcfromtimestamp(self._data["remind_on"])

    def is_due(self, lenient: bool = False):
        if datetime.utcnow() > self.due_on:
            return True
        if lenient:
            return datetime.utcnow() + timedelta(minutes=1) > self.due_on
        return False


import os
import math
import random
import datetime
from pathlib import Path
import sys

#train_size = 80
#test_size = 100

def create_set(size, prefix, groupCount=None, groupSize=None, fixedParam=''):
    if groupCount == 'None':
        group_count = int(math.ceil(math.sqrt(int(size))))
    else:
        group_count = int(groupCount)
    if groupSize == 'None':
        group_size = int(math.ceil(int(size) / int(group_count)))
    else:
        group_size = int(groupSize)
        group_count = int(math.ceil(float(size) / int(group_size)))
    size=int(size)
    print("Anzahl Gruppen: {}".format(group_count))
    print("Gruppengröße: {}".format(group_size))

    groups = []
    graph_group = {}
    for i in range(group_count):
        rand_num = random.randint(0, 9)
        label = "smoke" if rand_num < 3 else "not_smoke"
        groups.append(label)
        graph_group[i] = []

    friends = []
    graph_friends = []
    for a in range(size):
        for b in range(a, size):
            if a == b:
                continue
            rand_num = random.randint(0, 9)
            rand_num2 = random.randint(0,9)
            same_group = get_group_for_person(a, group_size) == get_group_for_person(b, group_size)
            if same_group and rand_num < 8:
                friends.append("friends({},{})".format(get_person_name(a), get_person_name(b)))
                friends.append("friends({},{})".format(get_person_name(b), get_person_name(a)))

                graph_friends.append("{} -- {}".format(get_person_name(a).replace("Person_",""), get_person_name(b).replace("Person_", "")))

            if not same_group and rand_num2 < 0:
                friends.append("friends({},{})".format(get_person_name(a), get_person_name(b)))
                friends.append("friends({},{})".format(get_person_name(b), get_person_name(a)))
                graph_friends.append("{} -- {}".format(get_person_name(a).replace("Person_",""), get_person_name(b).replace("Person_", "")))

    smoking = []
    not_smoking = []
    cancer = []
    not_cancer = []
    s_smokes = []
    not_s_smkoes = []
    # not_s_smokes_vbl = []
    for i in range(size):
        rand_num1 = random.randint(0, 9)
        rand_num2 = random.randint(0, 9)
        group_number = get_group_for_person(i, group_size)
        if (groups[group_number] == "smoke" and rand_num1 < 7) or rand_num1 < 1:
            smoking.append("smokes({})".format(get_person_name(i)))
            graph_group[group_number].append("{}[ fillcolor=crimson]".format(i))
            s_smokes.append("s_smokes({})".format(get_person_name(i)))
            #if rand_num2 < 5:
            #    cancer.append("cancer({})".format(get_person_name(i)))
            #else:
            #    not_cancer.append("cancer({})".format(get_person_name(i)))
        else:
            not_smoking.append("not_smokes({}).".format(get_person_name(i)))
            graph_group[group_number].append("{}[ fillcolor=aquamarine3]".format(i))
            not_s_smkoes.append("not_s_smokes({}).".format(get_person_name(i)))
            if rand_num2 < 1:
                cancer.append("cancer({})".format(get_person_name(i)))
            else:
                not_cancer.append("cancer({})".format(get_person_name(i)))
        #if i % group_size - 1 == 0 or i % group_size - 2 == 0:
        #    person = get_person_name(i)
        #    if any(str(person) in s for s in smoking):
        #        s_smokes.append("s_smokes({})".format(person))
        #        smoking.remove("smokes({})".format(person))
        #        group = get_group_for_person(int(person.replace("Person_", "")), group_size)
        #        for i in graph_group[group]:
        #            if any(str(person) in j for j in i):
        #                graph_group[group].pop("{}[ fillcolor=aquamarine3]".format(person))
        #                graph_group[group].append("{}[ fillcolor=darkgrey]".format(i))
         #   else:
         #       not_s_smkoes.append("s_smokes({})".format(person))
         #       # not_s_smokes_vbl.append("not_ s_smokes({})".format(person))

    curr_dir = os.getcwd()
    Path(curr_dir + "\\result\\graphs").mkdir(parents=True, exist_ok=True)
    graph_file_name = curr_dir + "/result/graphs/{}_{}_graph_{}_{}.dot".format(prefix,fixedParam,size,datetime.datetime.now().strftime('%Y-%m-%d_%H%M%S'))
    print(graph_file_name)
    log_file_name = curr_dir + "/result/{}_log.txt".format(prefix)
    os.makedirs(os.path.dirname(log_file_name), exist_ok=True)
    with open(log_file_name, "w+") as f:
        f.write("Prefix: {}\n".format(prefix))
        f.write("Size: {}\n".format(size))
        f.write("Group count: {}\n".format(group_count))
        f.write("Group size: {}\n".format(group_size))
        f.write("\n")
        for i in range(len(groups)):
            index_from = 0 + i * group_size
            index_to = (i + 1) * group_size - 1
            f.write("Group #{} ({}-{}): {}\n".format(i, index_from, index_to, groups[i]))
        f.close()

    facts_file_name = curr_dir + "/result/{}/{}_facts.txt".format(prefix, prefix)
    os.makedirs(os.path.dirname(facts_file_name), exist_ok=True)
    with open(facts_file_name, "w+") as f:
        f.write("useStdLogicVariables: true.\n")
        for i in range(len(friends)):
            f.write(str(friends[i]) + ".\n")
        for i in range(len(smoking)):
            f.write(str(smoking[i]) + ".\n")
        for i in range(len(s_smokes)):
            f.write(str(s_smokes[i]) + ".\n")
        for j in range(len(not_s_smkoes)):
            f.write(str(not_s_smkoes[j]) + "\n")
        f.close()

    #pos_file_name = curr_dir + "/result/{}/{}_pos.txt".format(prefix, prefix)
    #os.makedirs(os.path.dirname(pos_file_name), exist_ok=True)
    #with open(pos_file_name, "w+") as f:
    #     for i in range(len(cancer)):
    #         f.write(str(cancer[i]) + ".\n")
    #    for i in range(len(smoking)):
    #        f.write(str(smoking[i]) + ".\n")
    #     for j in range(len(not_s_smokes_vbl)):
    #        f.write(str(not_s_smokes_vbl[j]) + "\n")
    #    f.close()

    #neg_file_name = curr_dir + "/result/{}/{}_neg.txt".format(prefix, prefix)
    #os.makedirs(os.path.dirname(neg_file_name), exist_ok=True)
    #with open(neg_file_name, "w+") as f:
        # for i in range(len(not_cancer)):
        #    f.write(str(not_cancer[i]) + ".\n")
    #    for i in range(len(not_smoking)):
    #        f.write(str(not_smoking[i]) + ".\n")
    #    f.close()

    with open(graph_file_name, "w+") as f:
        groupString = []
        for i in graph_group:
            if groups[i] == "smoke":
                color = "darksalmon"
            else:
                color = "darkseagreen1"
            body = """label = "Group_{}"
            		style=filled
            		color={}
            		node[style=filled]""".format(i,color)
            groupsBody = '''\nsubgraph cluster_{}'''.format(i) +"{" + body
            for j in range(len(graph_group[i])):
                groupsBody = groupsBody + graph_group[i].pop(0) + "\n"
            groupsBody = groupsBody + "}\n"
            groupString.append(groupsBody)
        graph_friends = ' \n'.join(graph_friends)
        groupString = ' \n'.join(groupString)
        wholeGraph = "graph graphSmokers {" + groupString + graph_friends + "}"
        #print(wholeGraph)
        f.write(wholeGraph)

    print("Dataset {} ({}) created!".format(prefix, size))
    return

def get_group_for_person(person_index, group_size):
    return person_index // group_size

def get_person_name(pers_index):
    return "Person_{}".format(pers_index)

# datenset größe, prefix, anzahl gruppen, gruppengröße
#create_set(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
create_set(70, "train", 5, 'None')
create_set(100, "test", 5, 'None')