import numpy as np
import sys
from ..maths import convert_to_int_or_float, rint
from ..array import FloatRange
from ..combinations import (
    conditions_to_combinations_on_the_fly as
    cp_conditions_to_combinations_on_the_fly)

def import_pygame():
    import pygame
    return pygame

pygame = import_pygame()

class Point:

    def __init__(self, point):

        if isinstance(point, Point):
            point_tmp = point.point
        elif isinstance(point, (np.ndarray, list, tuple)):
            point_tmp = point
        else:
            raise TypeError('point')

        # if isinstance(point_tmp, (np.ndarray, list, tuple)):
        x, y = point_tmp
        self.x = convert_to_int_or_float(x)
        self.y = convert_to_int_or_float(y)
        self.point = np.asarray([self.x, self.y])

    def convert_to_pygame_coordinates(self, size_frame):

        if isinstance(size_frame, (list, tuple)):
            half_size_frame = (np.asarray(size_frame) / 2)
        else:
            half_size_frame = (size_frame / 2)

        point_with_inverted_y = np.empty(2, dtype=self.point.dtype)
        point_with_inverted_y[0] = self.point[0]
        point_with_inverted_y[1] = -self.point[1]

        point_pg = PointPG((half_size_frame - .5) + point_with_inverted_y)

        return point_pg

class PointPG:

    def __init__(self, point, size_frame=None):

        if isinstance(point, PointPG):
            point_tmp = point.point
        elif isinstance(point, Point):
            point_tmp = point.convert_to_pygame_coordinates(size_frame).point
        elif isinstance(point, (np.ndarray, list, tuple)):
            point_tmp = point
        else:
            raise TypeError('point')

        # if isinstance(point_tmp, (np.ndarray, list, tuple)):
        x, y = point_tmp
        self.x = convert_to_int_or_float(x)
        self.y = convert_to_int_or_float(y)
        self.point = np.asarray([self.x, self.y])

class Line:

    def __init__(self, point_start, point_end):
        if isinstance(point_start, Point):
            self.point_start = point_start.point
        else:
            self.point_start = Point(point_start).point

        if isinstance(point_end, Point):
            self.point_end = point_end.point
        else:
            self.point_end = Point(point_end).point

    def convert_to_pygame_coordinates(self, size_frame):
        point_start_pg = Point(self.point_start).convert_to_pygame_coordinates(size_frame)
        point_end_pg = Point(self.point_end).convert_to_pygame_coordinates(size_frame)
        line_pg = LinePG(point_start_pg, point_end_pg)
        return line_pg

class LinePG:

    def __init__(self, point_start, point_end, size_frame=None):

        if isinstance(point_start, PointPG):
            self.point_start = point_start.point
        if isinstance(point_start, Point):
            self.point_start = point_start.convert_to_pygame_coordinates(size_frame).point
        else:
            self.point_start = PointPG(point_start).point

        if isinstance(point_end, PointPG):
            self.point_end = point_end.point
        if isinstance(point_end, Point):
            self.point_end = point_end.convert_to_pygame_coordinates(size_frame).point
        else:
            self.point_end = PointPG(point_end).point

class Rectangle:
    # (point, size, alignment_x=alignment_x, alignment_y=alignment_y)
    def __init__(self, point, size, alignment_x='c', alignment_y='c'):

        if isinstance(point, (list, tuple, np.ndarray)):
            point_tmp = Point(point)
        elif isinstance(point, Point):
            point_tmp = point
        elif isinstance(point, PointPG):
            raise TypeError('point')
        else:
            raise TypeError('point')

        self.width = self.w = convert_to_int_or_float(size[0])
        self.half_width = convert_to_int_or_float(self.w / 2)
        self.height = self.h = convert_to_int_or_float(size[1])
        self.half_height = convert_to_int_or_float(self.h / 2)

        alignment_x_low = alignment_x.lower()
        if alignment_x_low in ['c', 'center', 'centre']:
            self.centerx = point_tmp.x
            self.left = convert_to_int_or_float(self.centerx - (self.half_width - .5))
            self.right = convert_to_int_or_float(self.left + self.w)

        elif alignment_x_low in ['l', 'left']:
            self.left = point_tmp.x
            self.right = convert_to_int_or_float(self.left + self.w)
            self.centerx = convert_to_int_or_float(self.left + (self.half_width - .5))
        elif alignment_x_low in ['r', 'right']:
            self.right = point_tmp.x
            self.left = convert_to_int_or_float(self.right - self.w)
            self.centerx = convert_to_int_or_float(self.left + (self.half_width - .5))
        else:
            raise ValueError('alignment_x')

        alignment_y_low = alignment_y.lower()
        if alignment_y_low in ['c', 'center', 'centre']:
            self.centery = point_tmp.y
            self.bottom = convert_to_int_or_float(self.centery - (self.half_height - .5))
            self.top = convert_to_int_or_float(self.bottom + self.h)
        elif alignment_y_low in ['b', 'bottom']:
            self.bottom = point_tmp.y
            self.top = convert_to_int_or_float(self.bottom + self.h)
            self.centery = convert_to_int_or_float(self.bottom + (self.half_height - .5))
        elif alignment_y_low in ['t', 'top']:
            self.top = point_tmp.y
            self.bottom = convert_to_int_or_float(self.top - self.h)
            self.centery = convert_to_int_or_float(self.bottom + (self.half_height - .5))
        else:
            raise ValueError('alignment_y')

        self.bottomleft = np.asarray([self.left, self.bottom])
        self.bottomright = np.asarray([self.right, self.bottom])
        self.topleft = np.asarray([self.left, self.top])
        self.topright = np.asarray([self.right, self.top])
        self.center = np.asarray([self.centerx, self.centery])
        self.midleft = np.asarray([self.left, self.centery])
        self.midright = np.asarray([self.right, self.centery])
        self.midbottom = np.asarray([self.centerx, self.bottom])
        self.midtop = np.asarray([self.centerx, self.top])
        self.size = np.asarray([self.width, self.height])

    def convert_to_pygame_coordinates(self, size_frame):

        topleft_pg = Point(self.topleft).convert_to_pygame_coordinates(size_frame).point
        left_pg, top_pg = topleft_pg
        left_pg = rint(left_pg)
        top_pg = rint(top_pg)
        width = rint(self.width)
        height = rint(self.height)
        rect_pg = pygame.Rect(left_pg, top_pg, width, height)
        return rect_pg

class Cross:

    def __init__(self, center, size):

        if isinstance(center, Point):
            self.center = center.point
        else:
            self.center = Point(center).point

        if isinstance(size, (list, tuple)):
            len_size = len(size)
            if len_size == 1:
                self.size = np.asarray(size * 2)
            elif len_size == 2:
                self.size = np.asarray(size)
        elif isinstance(size, np.ndarray):
            len_shape = len(size.shape)
            if len_shape == 0:
                self.size = np.empty(2, dtype=size.dtype)
                self.size[:] = size
            elif len_shape == 1:
                if size.shape[0] == 1:
                    self.size = np.empty(2, dtype=size.dtype)
                    self.size[:] = size[0]
                elif size.shape[0] == 2:
                    self.size = size
        else:
            self.size = np.empty(2, dtype=type(size))
            self.size[:] = size

        self.half_size = self.size / 2

        # left, top = self.center - self.half_size
        # right, bottom = self.center + self.half_size
        left, top = self.center - (self.half_size - .5)
        right, bottom = self.center + (self.half_size - .5)

        self.left = convert_to_int_or_float(left)
        self.right = convert_to_int_or_float(right)
        self.top = convert_to_int_or_float(top)
        self.bottom = convert_to_int_or_float(bottom)

        self.point_start_horizontal_line = PointPG([self.left, self.center[1]])
        self.point_end_horizontal_line = PointPG([self.right, self.center[1]])

        self.point_start_vertical_line = Point([self.center[0], self.bottom])
        self.point_end_vertical_line = Point([self.center[0], self.top])

        self.horizontal_line = Line(self.point_start_horizontal_line, self.point_end_horizontal_line)
        self.vertical_line = Line(self.point_start_vertical_line, self.point_end_vertical_line)

class CrossPG:

    def __init__(self, center, size, size_frame):

        if isinstance(center, PointPG):
            self.center = center.point
        else:
            self.center = PointPG(center, size_frame=size_frame).point

        if isinstance(size, (list, tuple)):
            len_size = len(size)
            if len_size == 1:
                self.size = np.asarray(size * 2)
            elif len_size == 2:
                self.size = np.asarray(size)
        elif isinstance(size, np.ndarray):
            len_shape = len(size.shape)
            if len_shape == 0:
                self.size = np.empty(2, dtype=size.dtype)
                self.size[:] = size
            elif len_shape == 1:
                if size.shape[0] == 1:
                    self.size = np.empty(2, dtype=size.dtype)
                    self.size[:] = size[0]
                elif size.shape[0] == 2:
                    self.size = size
        else:
            self.size = np.empty(2, dtype=type(size))
            self.size[:] = size

        self.half_size = self.size / 2
        # left, top = self.center - self.half_size
        # right, bottom = self.center + self.half_size
        left, top = self.center - (self.half_size - .5)
        right, bottom = self.center + (self.half_size - .5)

        self.left = convert_to_int_or_float(left)
        self.right = convert_to_int_or_float(right)
        self.top = convert_to_int_or_float(top)
        self.bottom = convert_to_int_or_float(bottom)

        self.point_start_horizontal_line = PointPG([self.left, self.center[1]])
        self.point_end_horizontal_line = PointPG([self.right, self.center[1]])

        self.point_end_vertical_line = PointPG([self.center[0], self.top])
        self.point_start_vertical_line = PointPG([self.center[0], self.bottom])

        self.horizontal_line = LinePG(self.point_start_horizontal_line, self.point_end_horizontal_line)
        self.vertical_line = LinePG(self.point_start_vertical_line, self.point_end_vertical_line)

class Frame:

    def __init__(self, surface):
        self.surface = surface
        self.width, self.height = self.surface.get_size()
        self.w = self.width
        self.h = self.height
        self.half_width = self.half_w = convert_to_int_or_float(self.width / 2)
        self.half_height = self.half_h = convert_to_int_or_float(self.height / 2)
        self.size = np.asarray([self.w, self.h], dtype='i')
        self.half_size = np.asarray([self.half_w, self.half_h])

    def blit(self, source, point, alignment_x='c', alignment_y='c', area_sources=None, special_flags=0):

        # if isinstance(point, (list, tuple, np.ndarray)):
        #     point_pg = Point(point).convert_to_pygame_coordinates(self.size)
        # elif isinstance(point, Point):
        #     point_pg = point.convert_to_pygame_coordinates(self.size)
        # elif isinstance(point, PointPG):
        #     # point_pg = point
        #     raise TypeError('point')
        # else:
        #     raise TypeError('point')

        if isinstance(source, pygame.Surface):
            size_source = source.get_size()
        elif isinstance(source, Frame):
            size_source = source.size
        elif isinstance(source, StimText):
            size_source = source.frame.size
        else:
            raise TypeError('source')

        rect = Rectangle(point, size_source, alignment_x=alignment_x, alignment_y=alignment_y)
        rect_destination_pg = rect.convert_to_pygame_coordinates(self.size)

        # if isinstance(source, pygame.Surface):
        #     rect_destination_pg = source.get_rect()
        # elif isinstance(source, Frame):
        #     rect_destination_pg = source.surface.get_rect()
        # elif isinstance(source, StimText):
        #     rect_destination_pg = source.frame.surface.get_rect()
        # else:
        #     raise TypeError('source')

        # alignment_x_low = alignment_x.lower()
        # if alignment_x_low in ['c', 'center', 'centre']:
        #     rect_destination_pg.centerx = point_pg.x
        #     # rect_destination_pg.centerx = rint(point_pg.x + .5)
        # elif alignment_x_low in ['l', 'left']:
        #     rect_destination_pg.left = point_pg.x
        # elif alignment_x_low in ['r', 'right']:
        #     rect_destination_pg.right = point_pg.x
        # else:
        #     raise ValueError('alignment_x')
        #
        # alignment_y_low = alignment_y.lower()
        # if alignment_y_low in ['c', 'center', 'centre']:
        #     rect_destination_pg.centery = point_pg.y
        #     # rect_destination_pg.centery = rint(point_pg.y + .5)
        # elif alignment_y_low in ['b', 'bottom']:
        #     rect_destination_pg.bottom = point_pg.y
        # elif alignment_y_low in ['t', 'top']:
        #     rect_destination_pg.top = point_pg.y
        #     # rect_destination_pg.top = point_pg.y + 1
        # else:
        #     raise ValueError('alignment_y')

        if isinstance(source, pygame.Surface):
            self.surface.blit(
                source, rect_destination_pg,
                area=area_sources, special_flags=special_flags)
        elif isinstance(source, Frame):
            self.surface.blit(
                source.surface, rect_destination_pg,
                area=area_sources, special_flags=special_flags)
        elif isinstance(source, StimText):
            self.surface.blit(
                source.frame.surface, rect_destination_pg,
                area=area_sources, special_flags=special_flags)

    def blits(
            self, sources, point, alignment_x='c', alignment_y='c',
            area_sources=None, special_flags=0):

        n_sources = len(sources)
        for i in range(n_sources):
            self.blit(
                sources[i], point[i], alignment_x=alignment_x, alignment_y=alignment_y,
                area_sources=area_sources, special_flags=special_flags)

    def blits_in_different_columns_in_range_x(
            self, sources, range_x, point_y, alignment_x='c', alignment_y='c',
            area_sources=None, special_flags=0):

        n_sources = len(sources)
        myrange = FloatRange(range_x)
        alignment_x_low = alignment_x.lower()
        if alignment_x_low in ['c', 'center', 'centre']:
            points_x = myrange.centers_of_n_within_equal_ranges(n_sources)
        elif alignment_x_low in ['l', 'left']:
            points_x = myrange.starts_of_n_within_equal_ranges(n_sources)
        elif alignment_x_low in ['r', 'right']:
            points_x = myrange.stops_of_n_within_equal_ranges(n_sources)
        else:
            raise ValueError('alignment_x')

        for i in range(n_sources):
            self.blit(
                sources[i], [points_x[i], point_y], alignment_x=alignment_x, alignment_y=alignment_y,
                area_sources=area_sources, special_flags=special_flags)

    def blits_in_different_columns(
            self, sources, points_x, point_y, alignment_x='c', alignment_y='c',
            area_sources=None, special_flags=0):

        n_sources = len(sources)
        for i in range(n_sources):
            self.blit(
                sources[i], [points_x[i], point_y], alignment_x=alignment_x, alignment_y=alignment_y,
                area_sources=area_sources, special_flags=special_flags)

    def blits_in_different_rows(
            self, sources, point_x, points_y, alignment_x='c', alignment_y='c',
            area_sources=None, special_flags=0):

        n_sources = len(sources)
        for i in range(n_sources):
            self.blit(
                sources[i], [point_x, points_y[i]], alignment_x=alignment_x, alignment_y=alignment_y,
                area_sources=area_sources, special_flags=special_flags)

    def blits_in_different_columns_and_rows(
            self, sources, points_x, points_y, alignment_x='c', alignment_y='c',
            area_sources=None, special_flags=0):

        if not isinstance(points_x, (list, tuple, np.ndarray)):
            points_x = [points_x]
        if not isinstance(points_y, (list, tuple, np.ndarray)):
            points_y = [points_y]
        for (j, i), point_ji in cp_conditions_to_combinations_on_the_fly([points_x, points_y], order_outputs='iv'):
            self.blit(
                sources[j][i], point_ji, alignment_x=alignment_x, alignment_y=alignment_y,
                area_sources=area_sources, special_flags=special_flags)

    def fill(self, color, rect=None):
        if rect is None:
            rect_pg = None
        else:
            rect_pg = rect.convert_to_pygame_coordinates(self.size)

        self.surface.fill(color, rect_pg)
        return None

    def draw_line_from_points(self, point_start, point_end, color, width=1):

        if isinstance(point_start, Point):
            point_start_pg = point_start.convert_to_pygame_coordinates(self.size)
        elif isinstance(point_start, PointPG):
            point_start_pg = point_start
        else:
            point_start_pg = Point(point_start).convert_to_pygame_coordinates(self.size)

        if isinstance(point_end, Point):
            point_end_pg = point_end.convert_to_pygame_coordinates(self.size)
        elif isinstance(point_end, PointPG):
            point_end_pg = point_end
        else:
            point_end_pg = Point(point_end).convert_to_pygame_coordinates(self.size)

        line_pg = LinePG(point_start_pg, point_end_pg)
        pygame.draw.line(self.surface, color, line_pg.point_start, line_pg.point_end, width)

    def draw_line_from_line(self, line, color, width=1):
        if isinstance(line, Line):
            line_pg = line.convert_to_pygame_coordinates(self.size)
        elif isinstance(line, LinePG):
            line_pg = line
        else:
            raise TypeError('line')

        pygame.draw.line(self.surface, color, line_pg.point_start, line_pg.point_end, width)

    def draw_cross_from_point(self, point, size, color, width=1, alignment_x='c', alignment_y='c'):

        if isinstance(point, Point):
            point_pg = point.convert_to_pygame_coordinates(self.size)
        elif isinstance(point, PointPG):
            point_pg = point
        else:
            point_pg = Point(point).convert_to_pygame_coordinates(self.size)
        # TODO: cross alignment_x='c', alignment_y='c'
        cross_pg = CrossPG(point_pg, size, self.size)
        # draw horizontal line
        self.draw_line_from_line(cross_pg.horizontal_line, color, width=width)
        # draw horizontal line
        self.draw_line_from_line(cross_pg.vertical_line, color, width=width)

    def subsurface(self, rect):
        rect_pg = rect.convert_to_pygame_coordinates(self.size)
        my_subsurface = Frame(self.surface.subsurface(rect_pg))
        return my_subsurface

    def get_rect(self):
        left = -self.half_width
        bottom = -self.half_height
        rect = Rectangle([left, bottom], [self.width, self.height], alignment_x='l', alignment_y='b')
        return rect

def initiate_font():
    pygame.font.init()

class StimText:

    def __init__(
            self, text, size, color,
            alignment_rows='c', name=None, bold=False, italic=False, distance_rows=1,
            antialias=False, background=None):

        if isinstance(text, str):
            self.text = text
        elif isinstance(text, (list, tuple, np.ndarray)):
            self.text = '\n'.join(text)
        else:
            raise TypeError('text')

        self.rows = self.text.splitlines()
        self.n_rows = self.R = len(self.rows)

        self.size = size
        self.color = color
        self.alignment_rows = alignment_rows
        self.name = name
        self.bold = bold
        self.italic = italic
        self.distance_rows = distance_rows
        self.antialias = antialias
        self.background = background

        font = pygame.font.SysFont(name, size, bold=bold, italic=italic)
        if self.R == 1:
            self.frame = Frame(font.render(self.rows[0], self.antialias, self.color, self.background))
        else:
            frames: list = [None] * self.R
            widths_lines = np.empty(self.R, dtype='i')
            heights_lines = np.empty(self.R, dtype='i')
            for r in range(self.R):
                frames[r] = Frame(font.render(self.rows[r], self.antialias, self.color, self.background))
                widths_lines[r], heights_lines[r] = frames[r].size

            max_width_lines = int(widths_lines.max(initial=0))
            max_height_lines = int(heights_lines.max(initial=0))
            height = ((max_height_lines * self.distance_rows) * (self.R - 1)) + max_height_lines
            self.frame = Frame(pygame.Surface([max_width_lines, height]))

            if self.background is None:
                diff_color = 50
                threshould = 255 - diff_color
                background_tmp = [0, 0, 0]
                for c in range(3):
                    if self.color[c] <= threshould:
                        background_tmp[c] = self.color[c] + diff_color
                    elif self.color[c] > threshould:
                        background_tmp[c] = self.color[c] - diff_color

                # background_tmp = [self.color[c] for c in range(3)]
                # if 0 <= background_tmp[0] < 255:
                #     background_tmp[0] += 1
                # elif background_tmp[0] == 255:
                #     background_tmp[0] -= 1
                # else:
                #     raise ValueError('color[0]')
                # background_tmp = [50, 200, 50]
                # if 255 in self.color:
                #     background_tmp = [0, 0, 0]
                # else:
                #     background_tmp = [255, 255, 255]

                self.frame.fill(background_tmp)
                # self.frame.fill(self.color)
                self.frame.surface.set_colorkey(background_tmp)
            else:
                self.frame.surface.fill(self.background)

            left_first_line = -self.frame.half_width
            bottom_first_line = self.frame.half_height - max_height_lines
            rect_first_line = Rectangle(
                [left_first_line, bottom_first_line],
                [self.frame.width, max_height_lines],
                alignment_x='l', alignment_y='b')
            surface_first_line = self.frame.subsurface(rect_first_line)

            bottom_other_lines = -self.frame.half_height
            height_other_lines = self.frame.height - max_height_lines
            rect_other_lines = Rectangle(
                [left_first_line, bottom_other_lines],
                [self.frame.width, height_other_lines],
                alignment_x='l', alignment_y='b')
            surface_other_lines = self.frame.subsurface(rect_other_lines)

            alignment_rows_lower = alignment_rows.lower()
            if alignment_rows_lower in ['c', 'center', 'centre']:
                x_lines = 0
            elif alignment_rows_lower in ['l', 'left']:
                x_lines = -max_width_lines / 2
            elif alignment_rows_lower in ['r', 'right']:
                x_lines = max_width_lines / 2
            else:
                raise ValueError('alignment_x')

            surface_first_line.blit(
                frames[0], [x_lines, surface_first_line.half_height], alignment_x=alignment_rows, alignment_y='t')

            num_sources = np.arange(self.R - 1)
            y_lines = ((num_sources[::-1] - num_sources.mean() - 0.5) * (max_height_lines * self.distance_rows))

            surface_other_lines.blits_in_different_rows(
                frames[1:], x_lines, y_lines, alignment_x=alignment_rows, alignment_y='b')

    # def draw(self, destination, point, area_sources=None, special_flags=0):
    #
    #     if isinstance(point, PointPG):
    #         raise TypeError('point')
    #
    #     num_sources = np.arange(self.R)
    #     points_y = point[1] + ((num_sources[::-1] - num_sources.mean()) * self.size)
    #     point_x = point[0]
    #
    #     if isinstance(destination, pygame.Surface):
    #         Frame(destination).blits_in_different_rows(
    #             self.frames, point_x, points_y,
    #             area_sources=area_sources, special_flags=special_flags)
    #     elif isinstance(destination, Frame):
    #         destination.blits_in_different_rows(
    #             self.frames, point_x, points_y,
    #             area_sources=area_sources, special_flags=special_flags)

def list_of_text_to_list_of_stimtext(
        text, size, same_color=True, color=None,
        alignment_rows='c', name=None, bold=False, italic=False, distance_rows=1,
        antialias=False, background=None):

    if color is None:
        color = [255, 255, 255]
        same_color = True

    n_texts = len(text)
    frames: list = [None] * n_texts

    if same_color:
        for i in range(n_texts):
            frames[i] = StimText(
                text[i], size, color, alignment_rows=alignment_rows,
                name=name, bold=bold, italic=italic,
                distance_rows=distance_rows, antialias=antialias, background=background)
    else:
        for i in range(n_texts):
            frames[i] = StimText(
                text[i], size, color[i], alignment_rows=alignment_rows,
                name=name, bold=bold, italic=italic,
                distance_rows=distance_rows, antialias=antialias, background=background)

    return frames

def surfaces_to_sizes(surfaces):
    n_surfaces = len(surfaces)
    sizes: list = [None] * n_surfaces
    for i in range(n_surfaces):
        sizes[i] = surfaces[i].get_size()
    return sizes

def print_names_fonts():
    names = pygame.font.get_fonts()
    print('There are {n} fonts:'.format(n=len(names)))
    for name_i in names:
        print(name_i)

def font_names():
    names = pygame.font.get_fonts()
    return names

def update(rect=None):
    if rect is None:
        pygame.display.flip()
    else:
        info = pygame.display.Info()
        size_screen = [info.current_w, info.current_h]
        rect_pg = rect.convert_to_pygame_coordinates(size_screen)
        pygame.display.update(rect_pg)
    return None

def flip_until_key_is_pressed(key):
    wait = True
    while wait:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == pygame.KEYDOWN:

                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()
                elif event.key == key:
                    wait = False

        pygame.display.flip()


# import close_task
import close_task_new_version
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
import pandas as pd
import time

scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name("yedaya-volta-7cabc6952bb0.json",scope)

client = gspread.authorize(creds)

sheet = client.open("CloseTaskSheet").sheet1

data = sheet.get_all_records()
# print(data)
# pprint(data)

# row = sheet.row_values(2)
# col = sheet.col_values(2)
# cell = sheet.cell(2,2).value

df = pd.DataFrame(sheet.get_all_records())
# print(df)

taskStatusList = df['Status']
# df2[df2["E"].isin(["two", "four"])]
selectedData = df[df['Status'].isin([0])]

print(selectedData['ProjectID'])
print(df['Status'])
print(len(selectedData))
# driver = close_task.initiateGoogleBrowser()
# driver = close_task.initiateFFbrowser()
def updateSheets(action_result,index,row):
    if action_result:
        print("This is index",index)
        # row['Status']=action_result
        # edit_row = sheet.(index)['Status']
        sheet.update_cell(index+2,6,1)
        print('Task Closed, Cell Status Updated')
    else:
        print("Task was not closed...")

#set url parameters:
for index, row in selectedData.iterrows():
    driver = close_task_new_version.initiateFFbrowser()
    # print(row)
    task_identifer = row['Identifier']
    projectid = row['ProjectID']
    tasklistid = row['TaskListID']
    taskid = row['TaskID']
    task_url = row['TaskURL']
    print("This is Index",index)
    print("This is row",row)
    # print(row)
    time.sleep(10)
    # closed = close_task.Crawl(driver,projectid,tasklistid,taskid,task_url)
    closed = close_task_new_version.Crawl(driver,projectid,tasklistid,taskid,task_identifer,task_url)

    # closed = 1
    updateSheets(closed,index,row)
    driver.quit()
    time.sleep(15)
# print("row",row)
# print("col",col)
# print("cell",cell)
# pprint(row)
# pprint(col)
# pprint(cell)

# projectid = sheet.row_values(sheet.col_values(5)==1)

# print(projectid)
# #sheet.insert_row(insertRow,4)
# num = sheet.row_count
# print(len(sheet.get_all_records()))
# print(sheet.get_all_records())
# driver = close_task.initiateBrowser()
# close_task.Crawl(driver,)


# http://python-pkcs11.readthedocs.io/en/latest/applied.html#importing-exporting-keys

# http://python-pkcs11.readthedocs.io/en/latest/api.html
# https://pypi.python.org/pypi/pyjks

import os
import pkcs11
from pkcs11 import *
import getpass
import OpenSSL
from OpenSSL import crypto
from log import *
import logging
#import jks
import platform

from pkcs11.util.x509 import decode_x509_certificate
from pkcs11.util import rsa
from pkcs11.util import dsa

class citizen_card:
	PKCS11_LIB_LINUX = "/usr/local/lib/libpteidpkcs11.so"
	PKCS11_LIB_MAC =  "/usr/local/lib/libpteidpkcs11.dylib"
	PKCS11_LIB_WINDOWS = "c:\\Windows\\System32\\pteidpkcs11.dll"
	PKCS11_LIB = ""

	def __init__(self):

		# detects the operating system
		if os.uname()[0] == "Darwin": # MAC
			if os.path.isfile(self.PKCS11_LIB_MAC):
				self.PKCS11_LIB = self.PKCS11_LIB_MAC
			else:
				print("PKCS11 library doesn't exist on OSX!")

		elif platform.uname()[0] == "Windows":
			if os.path.isfile(self.PKCS11_LIB_WINDOWS):
				self.PKCS11_LIB = self.PKCS11_LIB_WINDOWS
			else:
				print("PKCS11 library doesn't exist on Windows!")

		else:
			if os.path.isfile(self.PKCS11_LIB_LINUX):
				self.PKCS11_LIB = self.PKCS11_LIB_LINUX
			else:
				print("PKCS11 library doesn't exist on Linux!")

		self.lib = pkcs11.lib(self.PKCS11_LIB) # lib to use
		self.token = None

		# select the desired token
		tokens = self.lib.get_tokens()

		for t in tokens:
			self.token = t
			break

	# gets the authentication public key and save on a file
	def save_auth_pub_key(self):

		with self.token.open() as session:

			objs = session.get_objects({Attribute.CLASS: ObjectClass.PUBLIC_KEY})

			pubKey = None

			for obj in objs:
				if "AUTH" in str(obj).upper():
					pubKey = obj
					break

			objs = None

			if pubKey is None:
				print("No pubKey")

			# To export an RSA public key in PKCS #1 DER-encoded format
			public_key = pkcs11.util.rsa.encode_rsa_public_key(pubKey)

			# writes the pub_key to a file
			pub = open("auth_publicKey_cc.txt", 'wb')
			pub.write(public_key)
			pub.close()

			return pubKey

	# loads the authentication public key from the file
	def load_auth_pub_key(self):

		pub = open("auth_publicKey_cc.txt", 'rb')
		public = pub.read()
		pub.close()

		public_key = pkcs11.util.rsa.decode_rsa_public_key(public)

		return public_key

	# signs a data string with the authentication private key
	def sign_auth_private_key(self, data):

		with self.token.open() as session:

			objs = session.get_objects({Attribute.CLASS: ObjectClass.PRIVATE_KEY})
			private = None

			for obj in objs:
				if("AUTH" in str(obj).upper()):
					private = obj
					break

			objs = None

			if(private is None):
				print("No private Authentication Key Found.")

			# http://python-pkcs11.readthedocs.io/en/latest/api.html#object-capabilities
			signature = private.sign(data, mechanism=pkcs11.Mechanism.SHA256_RSA_PKCS)
			return signature

	# verifies if the signature is correct
	def verify_auth_signature(self, data, signature):

		with self.token.open() as session:

			objs = session.get_objects({Attribute.CLASS: ObjectClass.PUBLIC_KEY})
			pub_key = None

			for obj in objs:
				if("AUTH" in str(obj).upper()):
					pub_key = obj
					break

			objs = None

			if(pub_key is None):
				print("No public Authentication Key Found.")

			# <class '__main__.PublicKey'>
			return pub_key.verify(data, signature, mechanism=pkcs11.Mechanism.SHA256_RSA_PKCS)

	# saves the certificates from the cc in DER format
	def save_certificates(self):

		# http://python-pkcs11.readthedocs.io/en/latest/applied.html#exporting-certificates

		path = "certificates"

		with self.token.open() as session:

			objs = session.get_objects({Attribute.CLASS: ObjectClass.CERTIFICATE})

			for cert in objs:

				# Convert from DER-encoded value to OpenSSL object
				c = crypto.load_certificate(crypto.FILETYPE_ASN1, cert[Attribute.VALUE])

				subject = c.get_subject().CN
				issuer = c.get_issuer().CN

				# Convert to PEM format
   	 			#c = crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)

				# save the certificates
				open(path+"/"+str(subject)+"_"+str(issuer)+".cer", "wb").write(crypto.dump_certificate (crypto.FILETYPE_ASN1, c))

	# loads the saved cc certificates in DER format
	def load_certificates(self):

		path = "certificates"

		t_a = [] # trusted anchors
		i_c = [] # intermidiate certificates

		# gets the total path to the directory
		current_directory = os.getcwd()
		j = os.path.join(current_directory, path)
		directory = os.listdir(j)

		os.remove("certificates/.DS_Store")

		for cert in directory:
			cert_path = os.path.join(os.path.join(os.getcwd(), path), cert)
			open_cert = open(cert_path, "rb").read()

			c = crypto.load_certificate(crypto.FILETYPE_ASN1, open_cert)

			subject = c.get_subject().CN
			issuer = c.get_issuer().CN

			if subject == issuer:
				t_a += [c]
			else:
				i_c += [c]

		return t_a, i_c

	def get_cert_keystore(self):
		t_a = [] # trusted anchors
		i_c = [] # intermidiate certificates

		keystore = jks.KeyStore.load('KeyStore/CC_KS', 'password')

		for alias, certificate in keystore.certs.items():

			c = crypto.load_certificate(crypto.FILETYPE_ASN1, certificate.cert)

			subject = c.get_subject().CN
			issuer = c.get_issuer().CN

			if(subject==issuer):
				t_a += [c]
			else:
				i_c += [c]

		return t_a, i_c

	# http://aviadas.com/blog/2015/06/18/verifying-x509-certificate-chain-of-trust-in-python/
	def verify_certificate_chain(self, certificate, trusted_certificates):
		try:
			store = crypto.X509Store()

			# Adds a trusted certificate to this store
			for cert in trusted_certificates:
				store.add_cert(cert)

			# An X.509 store context is used to carry out the actual verification process of a certificate in a described context.
			store_context = crypto.X509StoreContext(store, certificate)

			# verifies the certificate
			store_context.verify_certificate()

			return True

		except Exception as e:
			print(e)
			return False

	def get_cert_keystore(self):
		trusted_anchors = []
		intermediate_certificates = []

		keystore = jks.KeyStore.load('Keystore/CC_KS', 'password')

		for alias, certificate in keystore.certs.items():

			t = crypto.load_certificate(crypto.FILETYPE_ASN1, certificate.cert)

			subject = t.get_subject().CN
			issuer = t.get_issuer().CN

			if(subject==issuer):
				trusted_anchors += [t]
			else:
				intermediate_certificates += [t]

		return trusted_anchors, intermediate_certificates

	def get_cert_path(self, certificate, trusted_anchors, intermediate_certificates, path = {}):

		issuer = certificate.get_issuer().CN
		subject = certificate.get_subject().CN

		# Check if it has expired
		if(certificate.has_expired()):
			return None

		if path == {}:
			path[subject] = issuer

		for c in intermediate_certificates:
			# Check if the issuer of the certificate is the subject of the parent
			parent_issuer = c.get_issuer().CN
			parent_subject = c.get_subject().CN
			if(subject != parent_subject and issuer == parent_subject):

				# print(certificate.verify(c.get_pubkey()))
				path[issuer] = parent_issuer
				path = self.get_cert_path(c, trusted_anchors, intermediate_certificates, path)
				return path

		# With the old middleware there are no trusted anchors...
		for trusted in trusted_anchors:
			parent_issuer = trusted.get_issuer().CN
			parent_subject = trusted.get_subject().CN

			if(subject == parent_subject):
				return path

		# With the old middleware we need this return...
		return path

if __name__ == "__main__":

	cc = citizen_card()

	#pub_save = cc.save_auth_pub_key()
	#pub_load = cc.load_auth_pub_key()

	data = "Francisco Cunha"
	signature_auth = cc.sign_auth_private_key(data)
	print(cc.verify_auth_signature(data, signature_auth))

	#cc.save_certificates()

	"""
	t_a1, i_c1 = cc.load_certificates()
	t_a2, i_c2 = cc.get_cert_keystore()

	intermediate_certificates = []
	trusted_anchors = []

	for t in t_a1+t_a2:
		if t.get_subject().CN not in [ce.get_subject().CN for ce in trusted_anchors]:
			trusted_anchors += [t]

	for i in i_c1+i_c2:
		if i.get_subject().CN not in [ce.get_subject().CN for ce in intermediate_certificates]:
			intermediate_certificates += [i]

	print(cc.get_cert_path(intermediate_certificates[0], trusted_anchors, intermediate_certificates))
	print("\n")
	print(cc.verify_certificate_chain(intermediate_certificates[0], intermediate_certificates[1:]))
	"""

	#cert = open("certificates/ECRaizEstado.crt", "rb").read()
	#print(cc.verify_certificate_chain("cert"))


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

flat_chess = cv2.imread("../data/flat_chessboard.png")
gray_flat_chess = cv2.cvtColor(flat_chess, cv2.COLOR_BGR2GRAY)
flat_chess = cv2.cvtColor(flat_chess, cv2.COLOR_BGR2RGB)
# gray = np.float32(gray_flat_chess)

real_chess = cv2.imread("../data/real_chessboard.jpg")
gray_real_chess = cv2.cvtColor(real_chess, cv2.COLOR_BGR2GRAY)
real_chess = cv2.cvtColor(real_chess, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(real_chess, cv2.COLOR_RGB2GRAY)
dst = cv2.cornerHarris(gray, 2, 3, 0.04)

dst = cv2.dilate(dst, None)
plt.interactive(True)

# plt.imshow(gray_flat_chess, cmap="gray")
real_chess[dst > dst.max() * 0.01] = [255, 0, 0]
plt.imshow(real_chess, cmap="gray")


from flask import Flask, make_response, jsonify, request
from flask_cors import CORS
from bd import Inventario

app = Flask(__name__)
CORS(app)

@app.route('/produtos', methods=["GET"])
def get_db():
    return make_response(
        jsonify(Inventario)
    )

@app.route('/produto-add', methods=["POST"])
def create_produto():
    produto = request.json
    indice = len(Inventario) + 1
    produto['id'] = indice
    Inventario.append(produto)
    return produto

app.run()


import pytorch_quik as pq
import torch.utils.data as td

def test_make_tds(final_data, args):
    df = final_data.drop('text', axis=1)
    tds = pq.tensor.make_TensorDataset(df, "label")
    assert(isinstance(tds, td.TensorDataset))

def test_transform_tds(final_data, ttypes, args):
    df = final_data.drop('text', axis=1)
    tds = pq.tensor.make_TensorDataset(df, "label")
    tds = pq.tensor.transform_TensorDataset(tds, 1, ttypes)
    assert(isinstance(tds, td.TensorDataset))


# -*- coding: utf-8 -*-
"""
Created on Thu Jan  6 19:05:26 2022

@author: laksh
"""
"""
References:
1. https://machinelearningmastery.com/how-to-develop-a-face-recognition-system-using-facenet-in-keras-and-an-svm-classifier/
2. https://www.kaggle.com/yhuan95/face-recognition-with-facenet
3. https://github.com/nyoki-mtl/keras-facenet - facenet_keras.h5 code DNN
"""

# import mtcnn
# # print version
# print(mtcnn.__version__)

import numpy as np
import os
# import pandas as pd
from mtcnn import MTCNN
from PIL import Image
# from tensorflow import keras
# facenet_model = keras.models.load_model('services/AMS/facenet_keras.h5')
from keras.models import load_model

facenet_model = load_model('services/AMS/facenet_keras.h5')

# extract a single face from a given photograph
def extract_face(filename, required_size=(160, 160), detector = MTCNN() ):
    image = Image.open(filename)
    image = image.convert('RGB')
    pixels = np.asarray(image)
    # Detecting face
    results = detector.detect_faces(pixels)
    # extract the bounding box from the first face
    x1, y1, width, height = results[0]['box']
    # deal with negative pixel index
    x1, y1 = abs(x1), abs(y1)
    x2, y2 = x1 + width, y1 + height
    # extract the face
    face = pixels[y1:y2, x1:x2]
    # resize pixels to the model size
    image = Image.fromarray(face)
    image = image.resize(required_size)
    face_array = np.asarray(image)
    return face_array

# Transfer learning to find a embedding for the uploaded image

def get_embedding(model, face):
    face = face.astype('float32')
    # standardization
    mean, std = face.mean(), face.std()
    face = (face-mean)/std
    # transfer face into one sample (3 dimension to 4 dimension)
    sample = np.expand_dims(face, axis=0)
    # make prediction to get embedding
    yhat = model.predict(sample)
    return yhat[0]

#Variables

def face_embedding(face_array, name):
    data = np.load('services/AMS/dataset_embeddings.npz')
    X, y = data['arr_0'], data['arr_1']
    # face = extract_face(filename)
    face = face_array
    emd = get_embedding(facenet_model, face)
    X = np.vstack( (X,emd) )
    y = np.vstack( (y, name) )
    np.savez_compressed('services/AMS/dataset_embeddings.npz', X, y)
    return emd


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
from tqdm import tqdm
import glob

n_neuron_combos_tried = np.array([20])
pre_time = -0.5
post_time = 1
bin_size = 0.1

def load_decoders(decoder_path, var = None, epoch = None, x_type = None, null=False):
    '''
    This load decoders that were run with decoders that each combination of neurons is free to
    have a different number of trials, depending on when then neurons were active
    var = the variable being decoded
    epoch = the aligment time
    x_type = residuals or raw spikes
    '''
    # First list all the "real"sessions
    f=[]
    if null == True:
        f= glob.glob(decoder_path+'/*null*p_summary.npy')
    else:
        f= glob.glob(decoder_path+'/*real*p_summary.npy')
    # generate big dataframe
    os.chdir(decoder_path)
    summary = pd.DataFrame()
    for f_path in tqdm(f):
        p_summary = np.load(f_path)
        mse_summary = np.load(f_path[:-13]+'mse_summary.npy')
        # Find optimal lambda
        # Get summary with optimal lambda
        acc = pd.DataFrame()
        for c in np.arange(np.shape(p_summary)[3]):
            predict = []
            predict_mse = []
            for b in np.arange(np.shape(p_summary)[2]):
                predict_f = []
                predict_f_mse = []
                for fold in np.arange(np.shape(p_summary)[0]):
                    for combo in np.arange(np.shape(p_summary)[4]):
                        predict_f.append(np.nanmean(p_summary[fold,:,b,c,combo])) # this nan mean is the same as selecting the lambda asigned, since only one value will be non nan
                        predict_f_mse.append(np.nanmean(mse_summary[fold,:,b,c,:]))
                predict.append(np.nanmean(predict_f))
                predict_mse.append(np.nanmean(predict_f_mse))
            acc_combo = pd.DataFrame()
            acc_combo['r'] = predict
            acc_combo['mse'] = predict_mse
            acc_combo['time_bin'] = np.arange(np.shape(p_summary)[2])
            acc_combo['n_neurons'] = n_neuron_combos_tried[c]
            acc = pd.concat([acc,acc_combo])
        if null==True:
            acc['region'] = f_path.split('null_')[1][:-34]
        else:
            acc['region'] = f_path[len(decoder_path)+6:-34]
        acc['mouse'] = f_path[-33:-27]
        acc['hemisphere'] = f_path[-15:-14]
        acc['date'] = f_path[-26:-16]
        if null == True:
            acc['iter'] = f_path.split('/')[-1].split('_null')[0]
            acc['type'] = 'null'
            acc['id'] =   acc['iter']+ acc['region'] + acc['type'] + \
                acc['date'] +acc['mouse'] + acc['hemisphere'] + acc['type']
        else:
            acc['type'] = 'real'
            acc['id'] =   acc['region'] + acc['type'] + \
                acc['date'] +acc['mouse'] + acc['hemisphere'] + acc['type']
        summary = pd.concat([summary,acc])
    summary  = summary.reset_index()
    summary['variable'] = var
    summary['epoch'] = epoch
    summary['x_type']  = x_type
    return summary

#nsummary = load_decoders('/jukebox/witten/Alex/decoders_raw_results/decoder_output_qchosen_cue_forget',
#                        var = 'qchosen_pre', epoch = 'cue', x_type = 'raw', null=True)
#nsummary.to_csv('/jukebox/witten/Alex/decoders_raw_results/nsummary_qchosen.csv')

nsummary = load_decoders('/jukebox/witten/Alex/decoders_raw_results/decoder_output_deltaq_cue_forget',
                        var = 'deltaq', epoch = 'cue', x_type = 'raw', null=True)
nsummary.to_csv('/jukebox/witten/Alex/decoders_raw_results/nsummary_deltaq.csv')


# -*- coding: utf-8 -*-
"""
Created on Mon Mar 30 13:40:33 2020

@author: Kyle
"""

# -*- coding: utf-8 -*-
"""
Created on Fri Mar 27 22:12:26 2020

This file is used for testing the simulations on a smaller scale. This file
is NOT used for the main results in Colangelo and Lee (2020).

@author: Kyle Colangelo
"""

import CL2020
import numpy as np
from sklearn import linear_model
from sklearn.ensemble import ExtraTreesRegressor
import os
from itertools import product

args_lasso1 = {
        'alpha':0.00418519,
        'max_iter':5000,
        'normalize':True,
        'tol':0.001
        }

args_lasso2 = {
        'alpha':0.00281957,
        'max_iter':5000,
        'normalize':True,
        'tol':0.001
        }

args_rf1 = {
        'n_estimators':1000,
        'max_depth':None,
        'min_samples_leaf':40,
        'min_samples_split':40
        }

args_rf2 = {
        'n_estimators':1000,
        'max_depth':None,
        'min_samples_leaf':40,
        'min_samples_split':40
        }

model_lasso1 = linear_model.Lasso(**args_lasso1)
model_lasso2 = linear_model.Lasso(**args_lasso2)

model_rf1 = ExtraTreesRegressor(**args_rf1)
model_rf2 = ExtraTreesRegressor(**args_rf2)

model_nn1 = CL2020.NeuralNet1(101)
model_nn2 = CL2020.NeuralNet2(100)

models = {
        'lasso': [model_lasso1, model_lasso2],
        'rf': [model_rf1, model_rf2],
        'nn': [model_nn1, model_nn2]
        }

basis = {
    'lasso':True,
    'rf':False,
    'nn':False
    }

J = 100
N = 500
L = 5
c = 1
t = 0
beta_hat= np.zeros(J) #initialize the numpy array that will store the parameter estimate
std_error = np.zeros(J) #initialize the numpy array that will store the standard error
ml='nn'

for i in range(J):
    print(1)
    X, T, Y = CL2020.DGP(N)
    model = CL2020.DDMLCT(models[ml][0],models[ml][1])
    model.fit(X,T,Y,t,L,h=h,basis=basis[ml],standardize=False)

    beta_hat[i] = model.beta
    std_error[i] = model.std_errors

E_Y_t = 1.2*t + (t**2)

t_stat = np.abs((beta_hat-E_Y_t)/std_error)
t_stat_new = t_stat[~np.isnan(t_stat)]
coverage_rate = np.nanmean((t_stat_new<1.96))
bias = (np.nanmean(beta_hat-E_Y_t)) #can't compute percent when truth is 0
rmse = np.sqrt(np.nanmean((beta_hat-E_Y_t)**2))

print(bias)
print(rmse)
print(coverage_rate)


from pyrogram import Client, filters
import asyncio
import time
from pyrogram.types import ChatPermissions, Message
from handlers.help import *

@Client.on_message(filters.command("block", ["."]) & filters.me)
async def block_user(client: Client, message: Message):
    if not message.chat.type == "private":
        await message.reply("Can't block the group LOL !")
    else:
        user_id=message.chat.id
        await message.edit("You have been blocked succesfully due to your sins !!")
        await client.block_user(int(user_id))

add_command_help(
    "block",
    [
        [".block", "To Block Someone."],
    ],
)


"""
python tools/display_res_rep.py --method orb --metric ms --trials 11
"""

import os
import argparse
import numpy as np

parser = argparse.ArgumentParser()
parser.add_argument('--trials', required=True, type=str, default='0', help='xp id')
parser.add_argument('--method', required=True, type=str, default='sift')
args = parser.parse_args()

res_dir = os.path.join('res', args.method, args.trials)
log_fn = os.path.join(res_dir, 'log.txt')
log_l = [l.split("\n")[0] for l in open(log_fn).readlines() ]

rep, ms = [],[]
for l in log_l:
    if 'rep' in l:
        rep.append(float(l.split("-")[0].split(":")[1]))

    if 'ms' in l:
        ms.append(float(l.split("-")[0].split(":")[1]))

rep = np.array(rep)
ms = np.array(ms)
print('rep:\t%.5f'%np.mean(rep))
print('ms:\t%.5f'%np.mean(ms))


import json
import torch
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, AutoConfig
from transformers import T5ForConditionalGeneration, T5Tokenizer

def serverless_pipeline(model_path='./model'):
    # initialize the model architecture and weights
    model = T5ForConditionalGeneration.from_pretrained(model_path)
    # initialize the model tokenizer
    tokenizer = T5Tokenizer.from_pretrained(model_path)
    def predict(context):
        """predicts the answer on an given question and context. Uses encode and decode method from above"""
        # encode the text into tensor of integers using the appropriate tokenizer
        inputs = tokenizer.encode("summarize: " + context, return_tensors="pt", max_length=512, truncation=True)
        outputs = model.generate(
                inputs,
                max_length=150,
                min_length=40,
                length_penalty=2.0,
                num_beams=4,
                early_stopping=True)
        answer = tokenizer.decode(outputs[0])
        return answer
    return predict

# initializes the pipeline
question_answering_pipeline = serverless_pipeline()

def handler(event, context):
    try:
        # loads the incoming event into a dictonary
        body = json.loads(event['body'])
        # uses the pipeline to predict the answer
        answer = question_answering_pipeline(context=body['context'])
        return {
            "statusCode": 200,
            "headers": {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*',
                "Access-Control-Allow-Credentials": True

            },
            "body": json.dumps({'answer': answer})
        }
    except Exception as e:
        print(repr(e))
        return {
            "statusCode": 500,
            "headers": {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*',
                "Access-Control-Allow-Credentials": True
            },
            "body": json.dumps({"error": repr(e)})
        }


import pandas as pd
import argparse
import numpy as np
import os
from sklearn.preprocessing import MinMaxScaler, Normalizer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

def data_preprocessing(data):

    le = LabelEncoder()

    data['Gender']= data.loc[:,['Gender']].apply(le.fit_transform)

    data['CLASS']= data.loc[:,['CLASS']].apply(le.fit_transform)

    predictors_df = data.iloc[:,:-1]

    target_df = data.iloc[:,-1:]

    scaler = MinMaxScaler(feature_range=(0,1))

    rescaled_df = pd.DataFrame(scaler.fit_transform(predictors_df), columns= predictors_df.columns)

    scaler = Normalizer()

    normalized_df = pd.DataFrame(scaler.fit_transform(rescaled_df), columns= rescaled_df.columns)

    x_train, x_test, y_train, y_test = train_test_split(normalized_df,target_df, test_size=0.3, random_state=42)

    data_path = './data_preprocessing/preprocessed_data'

    if not os.path.exists(data_path):
        os.makedirs(data_path)

    np.save(f'{data_path}/x_train.npy', x_train)
    np.save(f'{data_path}/x_test.npy', x_test)
    np.save(f'{data_path}/y_train.npy', y_train)
    np.save(f'{data_path}/y_test.npy', y_test)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--x_train')
    parser.add_argument('--x_test')
    parser.add_argument('--y_train')
    parser.add_argument('--y_test')
    args = parser.parse_args()
    data_preprocessing(args.x_train, args.x_test, args.y_train, args.y_test)


# -*- coding: utf-8 -*-
"""
Created on Tue Feb 25 08:44:29 2020

@author: Benjamin Pommier
"""

from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
import glob
import pandas as pd
import pickle
import numpy as np

# Load data
def load_data(path_train=None, path_dev=None, path_labels=None, partial='all'):

    if path_train is None:
        path_train = 'features/features_train.csv'
    if path_dev is None:
        path_dev = 'features/features_dev.csv'
    if path_labels is None:
        path_labels = 'features/labels.csv'

    hog = list(np.arange(50, 86, 1))
    hsv = list(np.arange(2, 50, 1))
    spatial = list([0,1])
    image = list([86])

    if partial == 'all':
        usecol = spatial + hsv + hog + image
    elif partial == 'spatial_hsv':
        usecol = spatial + hsv + image
    elif partial == 'spatial_hog':
        usecol = spatial + hog + image
    elif partial == 'hsv_hog':
        usecol = hsv + hog + image
    elif partial == 'hsv':
        usecol = hsv + image
    elif partial == 'hog':
        usecol = hog + image

    exception = 13 #Problematic image, label shape differs from image shape

    #Loading training set
    print('--- Loading Training Set ---')
    X_train = pd.read_csv(path_train, usecols=usecol)
    max_im = X_train.image.max()
    X_train = X_train[(X_train.image <= max_im) & (X_train.image != exception)]

    #Loading dev set
    print('--- Loading Dev Set ---')
    X_dev = pd.read_csv(path_dev, usecols=usecol)

    print('--- Loading Ground Truth ---')
    labels = pd.read_csv(path_labels)
    y_train = labels[(labels.image <= max_im) & (labels.image != exception)].label

    y_dev = labels[(labels.image > max_im) & (labels.image < 31)].label

    print('--- END OF DATA LOADING ---')
    return X_train, X_dev, y_train, y_dev

def train(X_train, y_train, model=None, gridsearch=False, filename_model='None'):
    if model is None:
        model = RandomForestClassifier(n_jobs=-1)
        #Model RF

    if gridsearch:
        if type(model) == type(LogisticRegression()):
            params = {'C':[0.01, 0.1, 1, 10]}
        elif type(model) == type(RandomForestClassifier()):
            params = {'n_estimators': [10, 30, 50], 'min_samples_leaf': [10, 100]}
        elif type(model) == type(SVC()):
            params = {'C': [0.1, 1, 10]}

        gridsearch = GridSearchCV(model, param_grid=params, cv=3, verbose=2, n_jobs=-1)

        gridsearch.fit(X_train, y_train)
        model = gridsearch.best_estimator_
        print(model)
        model.n_jobs = -1 #Setting te parameter afterwards otherwise None
        model.fit(X_train, y_train)
    else:
        model.n_jobs = -1
        model.fit(X_train, y_train)

    #Save the model to disk
    pickle.dump(model, open(filename_model+'.sav', 'wb'))

def evaluate(X_train, X_dev, y_train, y_dev, type_model='rf'):
    if type_model == 'rf' :
        best_model = pickle.load(open('model/random_forest.sav', 'rb'))
    elif type_model == 'logreg':
        best_model = pickle.load(open('model/logreg.sav', 'rb'))
    elif type_model == 'svc':
        best_model = pickle.load(open('model/svc.sav', 'rb'))
    else:
        best_model = pickle.load(open(type_model+'.sav', 'rb'))

    #Training set
    y_pred_train = best_model.predict(X_train)
    y_probas_train = best_model.predict_proba(X_train)
    print(classification_report(y_train, y_pred_train))
    pickle.dump(classification_report(y_train, y_pred_train, output_dict=True),
                open(type_model+'_results_train.pkl', 'wb'))

    #Dev set
    y_pred_dev= best_model.predict(X_dev)
    y_probas_dev= best_model.predict_proba(X_dev)
    print(classification_report(y_dev, y_pred_dev))
    pickle.dump(classification_report(y_dev, y_pred_dev, output_dict=True),
                open(type_model+'_results_dev.pkl', 'wb'))

    return y_probas_train, y_probas_dev

#%%Training
partial = ['all']
for prt in partial:
    X_train, X_dev, y_train, y_dev = load_data(partial=prt)

    rf = RandomForestClassifier(n_estimators=10, min_samples_leaf=100, n_jobs=-1)
    logreg = LogisticRegression(C=1, n_jobs=-1)
    # svc = SVC()

    train(X_train, y_train, model=rf, gridsearch=True, filename_model=prt+'_random_forest')
    train(X_train, y_train, model=logreg, gridsearch=True, filename_model=prt+'_logreg')

    evaluate(X_train, X_dev, y_train, y_dev, type_model=prt+'_random_forest')
    evaluate(X_train, X_dev, y_train, y_dev, type_model=prt+'_logreg')

#%%Visualisation

def visualize(labels=None, features=None, model=None, image_num=None, predict=True):
    ORIGINAL_PATH = 'data/FASSEG-frontal03/Original/'
    # ORIGINAL_PATH = 'data/perso/'
    LABELED_PATH = 'data/FASSEG-frontal03/Labeled/'
    EXTENSION = '.jpg'

    if image_num < 10:
        or_image = plt.imread(ORIGINAL_PATH + '00' + str(int(image_num)) + EXTENSION)
        lbl_image = plt.imread(LABELED_PATH + '00' + str(int(image_num)) + EXTENSION)
    elif (image_num >= 10) & (image_num < 100):
        or_image = plt.imread(ORIGINAL_PATH + '0' + str(int(image_num)) + EXTENSION)
        lbl_image = plt.imread(LABELED_PATH + '0' + str(int(image_num)) + EXTENSION)
    else:
        raise NotImplementedError

    # Prediction for a given image
    if predict:
        X = features[features.image == image_num]
        pred = model.predict_proba(X)
    else:
        pred = labels

    try:
        #Display
        plt.figure(figsize=(20,20))
        plt.subplot(331)
        plt.title('Original')
        plt.imshow(or_image)

        plt.subplot(332)
        plt.title('Ground truth')
        plt.imshow(lbl_image)

        plt.subplot(334)
        plt.title('Mouth') #nose eyes hair background skin
        plt.imshow(pred[:, 0].reshape((512, -1)), cmap='gray')

        plt.subplot(335)
        plt.title('Nose') #nose eyes hair background skin
        plt.imshow(pred[:, 1].reshape((512, -1)), cmap='gray')

        plt.subplot(336)
        plt.title('Eyes') #nose eyes hair background skin
        plt.imshow(pred[:, 2].reshape((512, -1)), cmap='gray')

        plt.subplot(337)
        plt.title('Hair') #nose eyes hair background skin
        plt.imshow(pred[:, 3].reshape((512, -1)), cmap='gray')

        plt.subplot(338)
        plt.title('Background') #nose eyes hair background skin
        plt.imshow(pred[:, 4].reshape((512, -1)), cmap='gray')

        plt.subplot(339)
        plt.title('Skin') #nose eyes hair background skin
        plt.imshow(pred[:, 5].reshape((512, -1)), cmap='gray')

        plt.tight_layout()
        plt.show()
    except:
        pass

# im_num = 5
# file = pd.read_csv('probability_maps_perso/' + str(im_num) + '.csv').to_numpy()
# visualize(labels = file, image_num=im_num, predict=False)

# im_num = 9
# mdl = pickle.load(open('model/all_random_forest.sav', 'rb'))
# visualize(features=X_train, model=mdl, image_num=im_num, predict=True)


import boto3
import json
import os
import uuid

sf = boto3.client("stepfunctions")
s3 = boto3.resource('s3')

def handler(event, context):
    for record in event["Records"]:
        bucket = record["s3"]["bucket"]["name"]
        key = record["s3"]["object"]["key"]

        obj = s3.Object(bucket, key)
        body = obj.get()['Body'].read().decode('utf-8')

        sf_response = sf.start_execution(
            stateMachineArn=os.environ["STATE_MACHINE_ARN"],
            name=str(uuid.uuid4()),
            input=json.dumps(json.loads(str(body))),
        )

        obj.delete()


#-*- coding: UTF-8 -*-
__author__ = 'chunlei3'

from django import template
from datetime import date, timedelta,datetime
from django.utils.timezone import utc

import __builtin__
import re

register = template.Library()

@register.filter
def get_item(dictionary, key):
    if dictionary == None:
        return None
    if not dictionary.has_key(key):
        return None
    return dictionary.get(key)

@register.filter
def item_in(array, key):
    return key in array

@register.filter
def inc(count):
    return count+1

@register.filter
def orderlen(obj):
    if obj == None :
        return 0
    count = 0;
    for x in obj:
        count = count + x.number
    return count

@register.filter
def totalPrice(obj):
    if obj == None :
        return 0
    count = 0.0;
    for x in obj:
        count = count + x.goods.offprice*x.number
    return count

@register.filter
def nick(obj):
    if obj == None  or len(obj)==0:
        return ""
    return obj[0].groupProfile.nick

@register.filter
def groupUrl(number,request):
    return "http://%s.5group.cn%s"  % (number,request.port)

@register.filter
def groupDate(dtime):
    now = datetime.utcnow().replace(tzinfo=utc)
    s = now - dtime
    if s.days > 0 :
        return "%s天前" % (s.days)
    elif s.seconds > 3600:
        return "%s小时前" % (s.seconds/3600)
    else:
        tmp = s.seconds/60
        if tmp ==0 :
            return "刚刚"
        else:
            return "%s分钟前" % (tmp)


import os
import json

from pyteal import *

router = Router(
    "offsig-demo",
    BareCallActions(
        no_op=OnCompleteAction.create_only(Approve()),
        update_application=OnCompleteAction.call_only(Reject()),
        delete_application=OnCompleteAction.call_only(Reject()),
        close_out=OnCompleteAction.call_only(Reject()),
        opt_in=OnCompleteAction.call_only(Approve()),
        clear_state=OnCompleteAction.never()
    ),
)

@router.method
def verify(data: abi.String, sig: abi.String, pub_key: abi.String) -> Expr:
    opup = OpUp(OpUpMode.OnCall)
    return Seq(
        opup.ensure_budget(Int(2000)),
        Assert(Ed25519Verify(data.get(), sig.get(), pub_key.get())),
        Approve()
    )

if __name__ == '__main__':
    path = os.path.dirname(os.path.abspath(__file__))

    # we use compile program here to get the resulting teal code and Contract definition
    # similarly we could use build_program to return the AST for approval/clear and compile it
    # ourselves, but why?
    approval, clear, contract = router.compile_program(
        version=6, optimize=OptimizeOptions(scratch_slots=True)
    )

    # Dump out the contract as json that can be read in by any of the SDKs
    with open(os.path.join(path, "contract.json"), "w") as f:
        f.write(json.dumps(contract.dictify(), indent=2))

    # Write out the approval and clear programs
    with open(os.path.join(path, "approval.teal"), "w") as f:
        f.write(approval)

    with open(os.path.join(path, "clear.teal"), "w") as f:
        f.write(clear)


import os
import re
import shutil
import sys
from typing import Dict, List
# from normalize import normalize_filename

ARCHIVES = "archives"
UNKNOWN = "unknown"

CATEGORIES: Dict[str, List] = {
    "images": ["jpeg", "png", "jpg", "svg"],
    "videos": ["avi", "mp4", "mov", "mkv"],
    "documents": ["doc", "docx", "txt", "pdf", "xlsx", "pptx"],
    "music": ["mp3", "ogg", "wav", "amr"],
    "archives": ["zip", "gz", "tar"],
    "unknown": [],
}

def define_category(file_path: str):

    global CATEGORIES

    extension = file_path.split(".")[-1]
    for category, category_extensions in CATEGORIES.items():
        if extension in category_extensions:
            return category

    CATEGORIES[UNKNOWN].append(extension)
    return UNKNOWN

def unpack_archive(archive_src: str, destination_folder: str):
    shutil.unpack_archive(archive_src, destination_folder)

def move_to_category_folder(src: str, destination: str):
    category = define_category(src)
    destination_folder: str = os.path.join(destination, category)
    if not os.path.exists(destination_folder):
        os.mkdir(destination_folder)
    if category == ARCHIVES:
        unpack_archive(src, destination_folder)
        return

    filename: str = os.path.split(src)[-1]
    new_filename = normalize_filename(filename)
    destination_filepath = os.path.join(destination_folder, new_filename)
    shutil.move(src, destination_filepath)

def arrange_folder(target_path: str, destination_folder: str = None):
    if destination_folder is None:
        destination_folder = target_path
    inner_files = os.listdir(target_path)
    for filename in inner_files:
        file_path: str = os.path.join(target_path, filename)
        if os.path.isdir(file_path):
            arrange_folder(file_path, destination_folder)
        elif os.path.isfile(file_path):
            move_to_category_folder(file_path, destination_folder)
        else:
            raise OSError

CYRILLIC_SYMBOLS = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяєіїґ'

TRANSLATION = (
    "a", "b", "v", "g", "d", "e", "e", "j", "z", "i", "j", "k", "l", "m", "n", "o", "p", "r", "s", "t", "u",
    "f", "h", "ts", "ch", "sh", "sch", "", "y", "", "e", "yu", "u", "ja", "je", "ji", "g")

TRANS = {}

for c, l in zip(CYRILLIC_SYMBOLS, TRANSLATION):
    TRANS[ord(c)] = l

    TRANS[ord(c.upper())] = l.upper()

def normalize_filename(filename):
    filename = filename.translate(TRANS)
    filename = re.sub(r'\W', '_', filename)

    return filename

def main():
    try:
        target_folder = sys.argv[1]
    except IndexError:
        print("Target folder doesn't defined!")
        return
    if not os.path.exists(target_folder):
        print("Bad path")
    arrange_folder(target_folder, target_folder)

if __name__ == "__main__":

    main()


import numpy as np
from rasterio.mask import mask
from shapely.geometry import *

class highest_pt:

    def __init__(self, pt_user, elevation):
        self.__pt_user = pt_user
        self.__elevation = elevation

    def get_pt_user(self):
        return self.__pt_user

    def get_elevation(self):
        return self.__elevation

    def get_highest_pt(self):
        pt_user = self.get_pt_user()
        elevation = self.get_elevation()

        # Create a buffer of user point
        buffered_zone = pt_user.buffer(5000)
        dataset, win_transform = mask(elevation, [buffered_zone], crop=True, nodata=np.nan)

        # Identify the highest point
        buffer_ele = dataset[0]
        max_ele = np.nanmax(dataset[0])
        re = np.where(dataset[0] == max_ele)
        re_x, re_y = win_transform * (re[1][0], re[0][0])  # Collect the first highest point

        return Point(re_x, re_y), max_ele, buffer_ele


from cx_Freeze import setup, Executable
import setuptools

executables = [
    Executable(
        'battle_city/__main__.py',
        targetName='BattleCity.exe',
        # base='Win32GUI',
        # icon='log.ico'
    )
]

includes = ['pygame']

zip_include_packages = ['pygame']

include_files = [
    'resources/*'
]

options = {
    'build_exe': {
        'include_msvcr': True,
        'includes': includes,
        'zip_include_packages': zip_include_packages,
        'build_exe': 'build_windows',
        'include_files': include_files,
    }
}

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

with open("requirements.txt") as f:
    requirements = [x.strip() for x in f]

setup(
    name="battle_city",
    version="1.2",
    author="Maxim Rusakov",
    description="just a Battle City game.",
    long_description=long_description,
    packages=setuptools.find_packages(),
    install_requires=requirements,
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires=">=3.7",
    executables=executables
)

# setuptools.setup(
#     name="battle_city",
#     version="1.2",
#     author="Maxim Rusakov",
#     description="just a Battle City game.",
#     long_description=long_description,
#     packages=setuptools.find_packages(),
#     install_requires=requirements,
#     classifiers=[
#         "Programming Language :: Python :: 3",
#         "License :: OSI Approved :: MIT License",
#         "Operating System :: OS Independent",
#     ],
#     python_requires=">=3.7",
# )


import requests
from django.shortcuts import render, redirect, HttpResponse
from django.views import View
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from Connector.models import SSCConnector
from .models import Agilecrmmodel
from requests.auth import HTTPBasicAuth
from django.contrib import messages

import logging

# Get an instance of a logger
logger = logging.getLogger(__name__)

class Save_agilecrm(LoginRequiredMixin, View):

    login_url = '/login/'
    redirect_field_name = 'redirect_to'

    def post(self, request):
        url  =  request.POST.get('url',None)
        username  = request.POST.get('username',None)
        api_key  = request.POST.get('api_key',None)
        agilecrm_data  =  Agilecrmmodel.objects.filter(source_id__user_id = request.user).first()
        if agilecrm_data:
            agilecrm_data.url = url
            agilecrm_data.username = username
            agilecrm_data.api_key = api_key
            agilecrm_data.save()
        else:
            source = SSCConnector.objects.filter(user_id = request.user).first()
            rapid = Agilecrmmodel(source_id = source, username = username, api_key = api_key, url= url)
            rapid.save()
        return redirect('/ssc_connector/ssc/')

def test_agilecrm(request):
    try:
        username = request.POST.get('username', None)
        api_key = request.POST.get('api_key', None)
        auth = HTTPBasicAuth(username, api_key)
        test_api = "dev/api/tickets/new-ticket"
        app_url = request.POST.get('url', None)
        test_api_url = "{}/{}".format(app_url, test_api)
        res = requests.get(url=test_api_url, auth=auth)
        if res.status_code == 200:
            logger.info("Agilecrm Test Connection:  %s ", res)
            return HttpResponse("Success")
    except Exception as e:
        logger.error("Unexpected Exception occured:  %s ", e)
        return e

@login_required(login_url='/login/')
def agilecrm_config(request):
    try:
        logger.info("Agilecrm configuration Request")
        agilecrm = Agilecrmmodel.objects.filter(source_id__user_id = request.user).first()
        if agilecrm:
            agilecrm.config = str(request.POST.dict())
            agilecrm.save()
            messages.success(request, f'AgileCRM configuration saved successfully..!!')
            return redirect('/ssc_connector/ssc/')
        else:
            return redirect('/ssc_connector/ssc/')
    except Exception as e:
        logger.error("Unexpected Exception occured:  %s ", e)
        return e


import requests
import os
from download_image import download_images
from dotenv import load_dotenv

IMAGES_ID = 99

def main():
  load_dotenv()
  images_folder = os.environ['IMAGES_FOLDER']

  spacex_url = f"https://api.spacexdata.com/v3/launches/{IMAGES_ID}"

  os.makedirs(images_folder, exist_ok=True)

  fetch_spacex_last_launch(spacex_url, images_folder)

def fetch_spacex_last_launch(url, images_folder):
  response = requests.get(url)
  response.raise_for_status()
  spacex_images = response.json()["links"]["flickr_images"]
  for index, image in enumerate(spacex_images):
    download_images(image, f"spacex{index}", os.path.splitext(image)[1], images_folder)

if __name__ == '__main__':
  main()


#!/usr/bin/env python
# -*- coding: utf-8  -*-
#encoding=utf-8

import tweepy
import time
import sys
import re
import random
from random import randint
from api_key import *

class TwitterCrawler():
    consumer_key = t_consumer_key
    consumer_secret = t_consumer_secret
    access_key = t_access_key
    access_secret = t_access_secret
    auth = None
    api = None

    def __init__(self):
        self.auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret)
        self.auth.set_access_token(self.access_key, self.access_secret)
        self.api = tweepy.API(self.auth, parser=tweepy.parsers.JSONParser())
        self.api.rate_limit_status()

    def re_init(self):
        self.auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret)
        self.auth.set_access_token(self.access_key, self.access_secret)
        self.api = tweepy.API(self.auth, parser=tweepy.parsers.JSONParser())

    def check_api_rate_limit(self, sleep_time):
        try:
            rate_limit_status = self.api.rate_limit_status()
        except Exception as error_message:
            if error_message['code'] == 88:
                "Sleeping for %d seconds." %(sleep_time)
                rate_limit_status['resources']['statuses']
                time.sleep(sleep_time)

        while rate_limit_status['resources']['statuses']['/statuses/user_timeline']['remaining'] < 10:
            "Sleeping for %d seconds." %(sleep_time)
            rate_limit_status['resources']['statuses']
            time.sleep(sleep_time)
            rate_limit_status = self.api.rate_limit_status()
        rate_limit_status['resources']['statuses']['/statuses/user_timeline']

    def crawl_user_profile(self, user_id):
        self.check_api_rate_limit(900)
        try:
            user_profile = self.api.get_user(user_id)
        except:
            return None
        return user_profile

    def crawl_user_tweets(self, user_id, count):
        self.check_api_rate_limit(900)
        try:
            tweets = self.api.user_timeline(user_id, count = count)
        except:
            tweets = None
        tried_count = 0
        while len(tweets) < count:
            try:
                tweets.extend(self.api.user_timeline(user_id, count = count))
            except:
                pass
            tried_count += 1
            if tried_count == 3:
                break
        return tweets[:count]

    def get_query_tweets(self, query, num):
        self.check_api_rate_limit(900)
        print(query)
        metadata = self.api.search(q=query,count=num)
        #print (metadata)
        tweets = metadata['statuses']
        tweet_list = []
        #print (len(tweets))
        for k in range(0, min(len(tweets), num)):
            dummy = tweets[k]['text'].encode('utf-8','ignore')
            #print dummy
            #unicode_word=re.findall(r'\w+',dummy)
            #tweet_list += [str(word) for word in unicode_word ]
            tweet_list.append(dummy)
        return tweet_list

def get_tweets(query):
    tc = TwitterCrawler()
    tc.check_api_rate_limit(900)
    tweet_list = tc.get_query_tweets(query, 10)
    blue_egg = "http://i.imgur.com/WR9koIb.png"
    green_egg = "http://i.imgur.com/3Raytqi.png"
    orange_egg = "http://i.imgur.com/1oMBqHq.jpg"
    purple_egg = "http://i.imgur.com/ULG3PXN.png"
    red_egg = "http://i.imgur.com/BwWYlGm.png"
    tweet_eggs = [ blue_egg, green_egg, orange_egg, purple_egg, red_egg ]

    html_a = '''

'''
                    #tweet
    html_c =        '''

Reply

Retweet

Favorite

                    ·

                    1 favorite

'''

    #print tweet_list
    message_to_server = ""
    if not tweet_list:
        return "no tweets"
    else:
    #print(tweet_list)
        for tweet in tweet_list:
            message_to_server = message_to_server + html_a + random.choice(tweet_eggs) + html_b + str(tweet) + html_c

        return message_to_server

    #Send to server

#if __name__ == "__main__":
#    main()


from turtle import Screen
from paddle import Paddle
from ball import Ball
from score import Score
import time

screen = Screen()
screen.setup(height=600, width=800)
screen.bgcolor("black")
screen.title("Pong Game")
screen.tracer(0)

computer_paddle = Paddle((350,0))
user_paddle = Paddle((-350,0))
small_ball = Ball()
score_board = Score()

screen.listen()
screen.onkey(key="Up", fun=computer_paddle.up)
screen.onkey(key="Down", fun=computer_paddle.down)
screen.onkey(key="w", fun=user_paddle.up)
screen.onkey(key="s", fun=user_paddle.down)

game_is_on = True
while game_is_on:
    time.sleep(small_ball.ball_speed)
    small_ball.move()

    #detect collision with wall
    if small_ball.ycor() > 280 or small_ball.ycor() < -280:
        small_ball.bounce_y()

    #detect collision with computer paddle
    if small_ball.distance(computer_paddle) < 50 and small_ball.xcor()>340:
        small_ball.bounce_x()

    #detect collision with computer paddle
    if small_ball.distance(computer_paddle) < 50 and small_ball.xcor()>320 or small_ball.distance(user_paddle) < 50 and small_ball.xcor()<-320:
        small_ball.bounce_x()

    #detect ball out of bounds
    if small_ball.xcor()>380:
        small_ball.refresh()
        score_board.user_score += 1
        score_board.score_update()

    if small_ball.xcor()<-380:
        small_ball.refresh()
        score_board.computer_score += 1
        score_board.score_update()
    screen.update()

    if score_board.computer_score == 2 or score_board.user_score == 2:
        game_is_on = False

screen.exitonclick()


from django.shortcuts import render, get_object_or_404, redirect
from fengyujc_blog.models import Post
from .models import Comment, Email
from .forms import CommentForm, EmailForm

# Create your views here.

def post_comment(request, post_pk):
    # 获取被评论的文章，后面需要把评论和被评论的文章关联起来
    post = get_object_or_404(Post, pk=post_pk)
    # HTTP 请求有 get 和 post 两种，一般用户通过表单提交数据都是通过 post 请求
    # 只有当用户的请求为 post 时才需要处理表单数据
    if request.method == 'POST':
        # 用户提交的数据存在 request.POST 中，这是一个类字典对象
        # 利用这些数据构造 CommentForm 的实例，就生成了 Django 的表单
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)  # 利用表单的数据生成 Comment 模型类的实例，但还不保存评论数据到数据库
            comment.post = post  # 将评论和被评论的文章关联起来
            comment.save()
            return redirect(post)
        else:
            comment_list = post.comment_set.all()  # 调用 xxx_set 属性获取一个类似于 objects 的模型管理器
            context = {
                'post': post,
                "form": form,
                "comment_list": comment_list
            }
            return render(request, 'fengyujc_blog/detail.html', context=context)
    return redirect(post)

def post_email(request):
    # HTTP 请求有 get 和 post 两种，一般用户通过表单提交数据都是通过 post 请求
    # 只有当用户的请求为 post 时才需要处理表单数据
    if request.method == 'POST':
        # 用户提交的数据存在 request.POST 中，这是一个类字典对象
        # 利用这些数据构造 EmailForm 的实例，就生成了 Django 的表单
        form = EmailForm(request.POST)
        if form.is_valid():
            email = form.save(commit=False)  # 利用表单的数据生成 Email 模型类的实例，但还不保存评论数据到数据库
            email.save()
            context = {
                "email_form": email,
            }
            return render(request, 'fengyujc_blog/email.html', context=context)
        else:
            context = {
                "form": form,
            }
            return render(request, 'fengyujc_blog/contact.html', context=context)

    return 'fengyujc_blog/contact.html'


import os
import numpy as np
import pandas as pd
from scipy import stats
from time import time, perf_counter
import matplotlib.pyplot as plt
import random

from numba import njit
import tensorflow as tf

from bayesflow.networks import InvertibleNetwork, InvariantNetwork
from bayesflow.amortizers import SingleModelAmortizer
from bayesflow.trainers import ParameterEstimationTrainer
from bayesflow.diagnostics import *
from bayesflow.models import GenerativeModel

@njit
def diffusion_trial(drift_g, drift_s, boundary_g, boundary_s, beta, e_g, nb, b, e_s, p_s, SSD, dc=1.0, dt=.01, max_steps=2000):
    """Simulates a trial from the diffusion model."""

    n_steps = 0.

    # go-accumulator starting point
    go_evidence = boundary_g * beta

    # is the current trial a stop trial?
    stop_trial = not(SSD == 0)

    # GO trial
    if stop_trial == False:
        while (go_evidence > 0 and go_evidence < boundary_g and n_steps < max_steps):

            # increment step
            n_steps += 1.0

            # "wait" until e_g (VET_go) has elapsed
            if (n_steps - e_g/dt) < 0:
                continue

            # DDM equation
            go_evidence += drift_g*dt + np.sqrt(dt) * dc * np.random.normal()

        t = n_steps * dt
        if go_evidence >= boundary_g:
            choicert = t + nb + b
        elif go_evidence <= 0:
            choicert = -t - nb - b
        else:
            choicert = 0
        return choicert

    # STOP trial
    else:
        stop_evidence = 0.
        while (go_evidence > 0 and go_evidence < boundary_g and stop_evidence < boundary_s and n_steps < max_steps):

            n_steps += 1.0

            # "wait" until e_g (VET_go) has elapsed
            if (n_steps - e_g/dt) < 0:
                continue

            go_evidence += drift_g*dt + np.sqrt(dt) * dc * np.random.normal()

            # "wait" until SSD and e_s (VET_stop) have elapsed
            if (n_steps - SSD/dt - e_s/dt) <= 0:
                continue

            stop_evidence += drift_s*dt + np.sqrt(dt) * dc * np.random.normal()

        # stop-accumulator boundary reached
        if stop_evidence >= boundary_s:

            # time between stimulus presentation and stop-accumulator finishing
            t_s = n_steps*dt

            # stop production time (in timestep size)
            p_s_steps = round(p_s/dt)
            for i in range(p_s_steps):

                n_steps += 1.0
                go_evidence += drift_g*dt + np.sqrt(dt) * dc * np.random.normal()

                # go-accumulator boundary reached
                if go_evidence >= boundary_g or go_evidence <= 0:

                    # time between stimulus presentation and go-accumulator finishing
                    t_g = n_steps*dt

                    # has the ballistic component begun?
                    if t_g + nb > t_s + p_s:
                        return 0
                    else:
                        return np.sign(go_evidence) * (t_g + nb + b)

            # stop-production time elapsed & go-accumulator boundary not reached
            return 0

        # go-accumulator boundary reached
        elif go_evidence >= boundary_g or go_evidence <= 0:

            t_g = n_steps*dt

            # non-ballistic component duration (in timestep size)
            nb_steps = round(nb/dt)
            for i in range(nb_steps):

                n_steps += 1.0
                stop_evidence += drift_s*dt + np.sqrt(dt) * dc * np.random.normal()

                # stop-accumulator boundary reached
                if stop_evidence >= boundary_s:

                    t_s = n_steps*dt

                    if t_g + nb < t_s + p_s:
                        return np.sign(go_evidence) * (t_g + nb + b)
                    else:
                        return 0

            # non-ballistic component elapsed & stop-accumulator boundary not reached
            return np.sign(go_evidence) * (t_g + nb + b)

        # max_steps reached, no decision
        else:
            return 0

@njit
def diffusion_condition(params, n_trials, SSDs):
    """Simulates a diffusion process over an entire condition."""

    drift_g, drift_s, boundary_g, boundary_s, beta, e_g, nb, b, e_s, p_s = params
    choicert = np.empty(n_trials)
    for i in range(n_trials):
        choicert[i] = diffusion_trial(drift_g, drift_s, boundary_g, boundary_s, beta, e_g, nb, b, e_s, p_s, SSD = SSDs[i])
    return choicert

def batch_simulator(prior_samples, n_obs, dt=0.005, s=1.0, max_iter=1e4):
    """
    Simulate multiple diffusion_model_datasets.
    """

    n_sim = prior_samples.shape[0] # number of sets of parameters
    sim_data = np.empty((n_sim, n_obs), dtype=np.float32)
    stop_arr = np.empty((n_sim, n_obs), dtype=np.float32)
    theta_stop = np.random.uniform(0.2, 0.4, n_sim)

    # Simulate diffusion data
    for i in range(n_sim):

        # Create SSDs
        SSDs = np.random.binomial(1, theta_stop[i], n_obs).astype(np.float32)
        for j in range(len(SSDs)):
            if SSDs[j] == 1:
                SSDs[j] = np.random.uniform(0.1,0.3)

        stop_arr[i] = SSDs
        sim_data[i] = diffusion_condition(prior_samples[i], n_obs, SSDs)

    # let's make this 3D, add SSDs
    sim_data = np.stack((sim_data, stop_arr), axis=-1)

    # add trial type
    sim_data_plus = np.empty((n_sim, n_obs, 6))
    sim_data_plus[:,:,:-4] = sim_data
    sim_data_plus[:,:,2] = (sim_data_plus[:,:,1] != 0) * 1

    # add whether response was corr./incorr.
    for i in range(n_sim):
        sim_data_plus[:,:,3][i] = ((sim_data_plus[:,:,0][i] != 0) != (sim_data_plus[:,:,2][i] == 1))

    # add VETs (e_g, e_s)
    for i in range(n_sim):
        sim_data_plus[:,:,4][i] = prior_samples[i, 5] # e_g
        sim_data_plus[:,:,5][i] = prior_samples[i, 8] # e_s

    return sim_data_plus

def prior(batch_size):
    """
    Samples from the prior 'batch_size' times.
    ----------

    Arguments:
    batch_size : int -- the number of samples to draw from the prior
    ----------

    Output:
    theta : np.ndarray of shape (batch_size, theta_dim) -- the samples batch of parameters
    """
    while True:
        # Prior ranges for the simulator:
        # 0. drift_g ~ U(-7.0, 7.0)
        # 1. drift_s ~ U(0.01, 7.0)
        # 2. boundary_g ~ U(0.1, 2.0)
        # 3. boundary_s ~ U(0.1, 2.0)
        # 4. beta ~ U(0.20, 0.80)
        p_samples = np.random.uniform(low=(-2.0, 0.01, 0.1, 0.1, 0.25),
                                      high=(2.0, 4.0, 2.0, 1.0, 0.75), size=(batch_size, 5))

        # ndt_go = e_g + nb + b
        # 5. e_g ~ U(0.15, 0.25) //i.e., VET_go
        # 6. nb ~ U(0.01, 0.2)
        # 7. b ~ U(0.01, 0.2)
        ndt_go_comp = np.random.uniform(low = (0.15, 0.01, 0.01),
                                        high = (0.25, 0.2, 0.2), size = (batch_size, 3))
        p_samples = np.c_[p_samples, ndt_go_comp]

        # ndt_stop = e_s + p_s
        # 8. e_s ~ U(0.15, 0.25) //i.e., VET_stop
        # 9. p_s ~ U(0.01, 0.1)
        ndt_stop_comp = np.random.uniform(low = (0.15, 0.01),
                                          high = (0.25, 0.1), size = (batch_size, 2))

        candidate_set = np.c_[p_samples, ndt_stop_comp]
        test_data = batch_simulator(candidate_set, 100).astype(np.float32)
        n_stop_trials = sum(test_data[:,:,2][0])
        n_succ_stops = sum((test_data[:,:,0][0] == 0) & (test_data[:,:,3][0] == 1))
        perc_succ = n_succ_stops / n_stop_trials * 100

        if perc_succ > 15:
            return candidate_set.astype(np.float32)

# check distribution of successful stopping
perc_succ = np.empty(10000)
for i in range(0,10000):
    true_params = prior(1)
    x = batch_simulator(true_params, 800).astype(np.float32)

    n_stop_trials = sum(x[:,:,2][0])
    n_succ_stops = sum((x[:,:,0][0] == 0) & (x[:,:,3][0] == 1))
    perc_succ[i] = n_succ_stops / n_stop_trials * 100

plt.figure()
plt.hist(perc_succ, bins = 100)
plt.show()

# Variable n_trials
def prior_N(n_min=60, n_max=300):
    """
    A prior or the number of observation (will be called internally at each backprop step).
    """

    return np.random.randint(n_min, n_max + 1)

#Test input & output
# timethen = time()
# print(batch_simulator(prior(50),100).shape)
# print('Test simulator took %.3f secs' % (time()-timethen))
summary_net = InvariantNetwork()
inference_net = InvertibleNetwork({'n_params': 10})
amortizer = SingleModelAmortizer(inference_net, summary_net)
generative_model = GenerativeModel(prior, batch_simulator)
trainer = ParameterEstimationTrainer(
    network=amortizer,
    generative_model=generative_model,
    checkpoint_path="checkpoint/model_1"
)

# Pre-simulated data (could be loaded from somewhere else)
n_sim = 5000
n_trials = 1600
true_params, sim_data = generative_model(n_sim, n_trials)

start_time = perf_counter()

## Round-based training
# losses = trainer.train_rounds(epochs=1, rounds=5, sim_per_round=200, batch_size=32, n_obs=n_trials)

# Experience-replay training
losses = trainer.train_experience_replay(epochs=1000,
                                         batch_size=64,
                                         iterations_per_epoch=200,
                                         capacity=100,
                                         n_obs=prior_N)

end_time = perf_counter()
duration = end_time - start_time
print(duration)

# Validate (quick and dirty)
n_param_sets = 300
n_samples = 1600
true_params = prior(n_param_sets)
x = batch_simulator(true_params, n_samples).astype(np.float32)
param_samples = amortizer.sample(x, n_samples=n_samples)
param_means = param_samples.mean(axis=0)
true_vs_estimated(true_params[(np.all(param_means[:, list(range(5,10))] > 0.01, axis = 1)), :],
                  param_means[(np.all(param_means[:, list(range(5,10))] > 0.01, axis = 1)), :],
                  ['drift_g', 'drift_s', 'boundary_g', 'boundary_s', 'beta', 'e_g', 'nb', 'b', 'e_s', 'p_s'],
                  filename = "NDDM_full_cut_no_stop")

columns = ['delta_g', 'delta_s', 'alpha_g', 'alpha_s', 'beta', 'e_g', 'nb', 'b', 'e_s', 'p_s']
def plot_est1_vs_est2(par_array, par1, par2):
    df = pd.DataFrame(par_array,
                      columns = columns)

    m, b = np.polyfit(df[par1], df[par2], 1)
    r_sq = np.corrcoef(df[par1], df[par2])[0,1]**2
    r_sq_box = 'R^2 = %.2f' % r_sq

    plt.scatter(df[par1], df[par2], color = 'black', alpha = 0.5)
    plt.plot(df[par1], m*df[par1]+b, color = 'red')
    plt.text(df[par1].min(), df[par2].min(), r_sq_box, fontsize = 12)
    plt.xlabel(par1)
    plt.ylabel(par2)
    plt.show()

# plot_est1_vs_est2(param_means, 'drift_s', 'drift_g')
# plot_est1_vs_est2(param_means, 'drift_s', 'boundary_g')
# plot_est1_vs_est2(param_means, 'delta_s', 'alpha_s')
# plot_est1_vs_est2(param_means, 'drift_s', 'beta')
# plot_est1_vs_est2(param_means, 'drift_s', 'C')

# plot_est1_vs_est2(param_means, 'boundary_s', 'drift_g')
# plot_est1_vs_est2(param_means, 'boundary_s', 'boundary_g')
# plot_est1_vs_est2(param_means, 'boundary_s', 'beta')
# plot_est1_vs_est2(param_means, 'boundary_s', 'C')

param_means_df = pd.DataFrame(param_means, columns = columns)

plt.figure(figsize=(12, 6))
# define the mask to set the values in the upper triangle to True
mask = np.triu(np.ones_like(param_means_df.corr(), dtype=np.bool))
heatmap = sns.heatmap(param_means_df.corr(), mask=mask, vmin=-1, vmax=1, annot=True, cmap='BrBG', fmt = '.2f')
heatmap.set_title('Posterior Parameter Estimate Correlation Matrix', fontdict={'fontsize':18}, pad=16);
plt.show()


import pynmea2
import matplotlib.pyplot as plt

def calcGap(sats, hdop, dphi, dlam, threshold, total_lon, total_lat, f = 1):
    isEnoughSats = sats > 3
    isFrequent = f >= 1
    isAcc = hdop < 4
    isDphi = dphi < threshold
    isDlam = dlam < threshold
    result = isEnoughSats and isFrequent and isAcc and isDphi and isDlam
    if result:
        plt.scatter(total_lon, total_lat, color='green', s=10, alpha=0.5)
    else:
        plt.scatter(total_lon, total_lat, color='red', s=10, alpha=0.5)
    print(
        sats,
        hdop,
        dphi,
        dlam,
        threshold,
        result
    )
    return result

filename = "nmea.csv"

lat2 = 55.7489
lon2 = 37.579

with open(filename, 'r') as f:
    for line in f:
        if line.startswith('$GPGGA'):
            msg = pynmea2.parse(line)
            lat = float(msg.latitude)
            lat_dir = msg.lat_dir
            lon = float(msg.longitude)
            lon_dir = msg.lon_dir
            if lat == 0 and lon == 0:
                continue
            num_s = msg.data[6]
            hdop = msg.data[7]
            total_lat = lat2 - lat
            total_lon = lon2 - lon
            calcGap(int(num_s), float(hdop), abs(total_lat), abs(total_lon), 0.0001, total_lon, total_lat)

plt.show()


# Codes used for the paper:
# "Automatic Design of Mechanical Metamaterial Actuators"
# by S. Bonfanti, R. Guerra, F. Font-Clos, R. Rayneau-Kirkhope, S. Zapperi
# Center for Complexity and Biosystems, University of Milan
# (c) University of Milan
#
#
######################################################################
#
# End User License Agreement (EULA)
# Your access to and use of the downloadable code (the "Code") is subject
# to a non-exclusive,  revocable, non-transferable,  and limited right to
# use the Code for the exclusive purpose of undertaking academic,
# governmental, or not-for-profit research. Use of the Code or any part
# thereof for commercial purposes is strictly prohibited in the absence
# of a Commercial License Agreement from the University of Milan. For
# information contact the Technology Transfer Office of the university
# of Milan (email: tto@unimi.it)
#
#######################################################################
from .metropolis import Metropolis
from .actuator import Actuator
from .spring import LinearSpring
from .images import occlusion_matrix

from typing import List
from typing import Set
from typing import Tuple
from typing import Dict
from typing import Optional
from typing import Union

from itertools import cycle
from pathlib import Path
import numpy as np
from pandas.io.pickle import to_pickle

import keras

class BayesianMetropolis(Metropolis):

    def __init__(
        self,
        actuator: Actuator,
        priors_model: keras.models.Sequential,
        priors_block_size: Tuple[int, int] = (64, 64),
    ):
        super().__init__(actuator)

        # set up model to compute priors
        self.priors_model = priors_model
        self._priors_block_size = priors_block_size

        # setup the figure to plot configurations
        # in raster mode, used for ML predictions
        self._setup_viz()
        self._compute_edges_priors()

    def to_pickle(self, path: Union[str, Path]) -> None:
        """
        Save BayesianMetropolis instance as serialized pickle file.

        Parameters
        ----------
        path : Union[str, Path]
            File path where the pickled object will be stored.

        Raises
        ------
        RuntimeError
            If file already exists
        RuntimeError
            If parent dir does not exist
        """
        assert isinstance(path, (str, Path))
        path = Path(path)
        # make sure directory exists
        directory = path.parent
        if not directory.is_dir():
            raise RuntimeError(
                f"Trying to write to non-existing directory {directory}")
        # make sure file does not exist
        if path.exists():
            raise RuntimeError(
                f"File already exists {path}. Refusing to overwrite!")
        # pickle
        # save a ref to the model
        priors_model = self.priors_model
        self.priors_model = None
        to_pickle(self, path)
        self.priors_model = priors_model

    def _compute_edges_priors(
        self,
        block_size: Optional[Tuple[int, int]] = None,
        keep_white_border: bool = True
    ) -> None:
        """compute the priors using heatmaps of expected change in efficiency"""
        if block_size is None:
            block_size = self._priors_block_size
        self._update_deltaeff_heatmaps(
            block_size=block_size,
            keep_white_border=keep_white_border
        )
        self._edges_delta_eff: Dict[LinearSpring, float] = {}
        # to identify pixels that matter
        current_image = self._edges_to_array(self.lattice.edges)[:, :, 0]
        for edge in self.lattice._possible_edges:
            # are we removing or adding?
            edges_tmp = self.lattice.edges.copy()
            if edge in edges_tmp:
                change_type = "remove_bond"
                edges_tmp.remove(edge)
            else:
                change_type = "add_bond"
                edges_tmp.add(edge)
            # identify pixels that correspond to the edge
            new_image = self._edges_to_array(edges_tmp)[:, :, 0]
            heatmap_locator = (new_image != current_image)
            heatmap = self._priors_heatmaps[change_type]
            # assign weight to the edge
            expected_delta_eff = np.mean(heatmap[heatmap_locator])
            self._edges_delta_eff[edge] = expected_delta_eff

    def _update_deltaeff_heatmaps(
        self,
        block_size: Tuple[int, int] = (50, 50),
        dpi: int = 100,
        keep_white_border: bool = False
    ) -> None:
        """
        Update the heatmaps used to compute priors of edges.

        Slides white/black blocks over the image of the current
        configuration and evaluates the change in efficiency
        that the ML priors model predicts.

        Parameters
        ----------
        block_size : Tuple[int, int], optional
            Size of the sliding block, by default (32, 32)
        dpi : int, optional
            Dots per inch used in heatmaps. Must match dpi of ML model.
        keep_white_border : bool
            Whether to restore the whiteness of the border around the image, even if the model
            gives non-white predictions for it. This is useful when models have undefined behaviour
            outside the 'active area' of the images.
        """
        self._priors_heatmaps: Dict[str, Dict[Tuple[int, int], float]] = {}
        for change_type in ["add_bond", "remove_bond"]:
            self._priors_heatmaps[change_type] = self._compute_expected_deltaeff_heatmap(
                block_size=block_size,
                change_type=change_type,
                dpi=dpi,
                keep_white_border=keep_white_border
            )

    def _compute_expected_deltaeff_heatmap(
        self,
        change_type="add_bond",
        block_size: Tuple[int, int] = (64, 64),
        dpi: int = 100,
        keep_white_border: bool = False
    ) -> Dict[Tuple[int, int], float]:
        # translate to black/white colors,
        # the logic is that you must
        # paint with black to add a bond,
        # paint with white to remove a bond
        block_color = {
            "add_bond": "black",
            "remove_bond": "white"
        }[change_type]
        # for the moment we only work with same
        # block size in w and h
        # but it shouldn't be too difficult to
        # deal with different values
        assert block_size[0] == block_size[1]
        _block_size = block_size[0]
        # get image representation of current configuration
        img = self._edges_to_array(self.lattice.edges, dpi=dpi)
        # compute the expected change in efficiency
        expected_deltaeff_heatmap = occlusion_matrix(
            model=self.priors_model,
            img=img,
            block_size=_block_size,
            block_color=block_color,
            keep_white_border=keep_white_border
        )
        return expected_deltaeff_heatmap