import socket
import struct
import os
import time
import hashlib

HOST = '192.168.1.76'
PORT = 8000
BUFFER_SIZE = 1024
FILE_NAME = 'usertrj.txt'   # Change to your file
FILE_SIZE = os.path.getsize(FILE_NAME)
HEAD_STRUCT = '128sIq32s'  # Structure of file head

def send_file():
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # Connect the socket to the server
    server_address = (HOST, PORT)

    #Calculate MD5
    print("Calculating MD5...")
    fr = open(FILE_NAME, 'rb')
    md5_code = hashlib.md5()
    md5_code.update(fr.read())
    fr.close()
    print("Calculating success")

    # Need open again
    fr = open(FILE_NAME, 'rb')
    # Pack file info(file name and file size)
    file_head = struct.pack('128sIq32s', b'usertrj.txt', len(FILE_NAME), FILE_SIZE, md5_code.hexdigest())

    try:
        # Connect
        sock.connect(server_address)
        print("Connecting to %s port %s" % server_address)
        # Send file info
        sock.send(file_head)
        send_size = 0
        print("Sending data...")
        time_start = time.time()
        while send_size < FILE_SIZE:
            if FILE_SIZE - send_size < BUFFER_SIZE:
                file_data = fr.read(FILE_SIZE - send_size)
                send_size = FILE_SIZE
            else:
                file_data = fr.read(BUFFER_SIZE)
                send_size += BUFFER_SIZE
            sock.send(file_data)
        time_end = time.time()
        print("Send success!")
        print("MD5 : %s" % md5_code.hexdigest())
        print("Cost %f seconds" % (time_end - time_start))
        fr.close()
        sock.close()
    except socket.errno as e:
        print("Socket error: %s" % str(e))
    except Exception as e:
        print("Other exception : %s" % str(e))
    finally:
        print("Closing connect")

if __name__ == '__main__':
    send_file()


import tkinter as tk
import sqlite3

def guardar_palabras():
    palabras = [entrada1.get(), entrada2.get(), entrada3.get(), entrada4.get(), entrada5.get()]

    # Conexión a la base de datos
    conexion = sqlite3.connect('basedatos.db')
    cursor = conexion.cursor()

    # Crear la tabla "palabras" si no existe
    cursor.execute('''CREATE TABLE IF NOT EXISTS palabras
                      (id INTEGER PRIMARY KEY AUTOINCREMENT,
                       palabra TEXT)''')

    # Eliminar las palabras anteriores en la tabla
    cursor.execute("DELETE FROM palabras")

    # Insertar las últimas 5 palabras en la tabla "palabras"
    for palabra in palabras:
        cursor.execute("INSERT INTO palabras (palabra) VALUES (?)", (palabra,))

    # Guardar cambios y cerrar conexión
    conexion.commit()
    conexion.close()
    ventana.destroy()

ventana = tk.Tk()
ventana.title("5 Palabras")
frase_inicio = "Si fueras 5 palabras, cuáles serías?:"
etiqueta_frase = tk.Label(ventana, text=frase_inicio)
etiqueta_frase.pack()
entrada1 = tk.Entry(ventana)
entrada1.pack()
entrada2 = tk.Entry(ventana)
entrada2.pack()
entrada3 = tk.Entry(ventana)
entrada3.pack()
entrada4 = tk.Entry(ventana)
entrada4.pack()
entrada5 = tk.Entry(ventana)
entrada5.pack()
boton = tk.Button(ventana, text="Aceptar", command=guardar_palabras)
boton.pack()
ventana.mainloop()

def mostrar_ventana1():
    ventana1 = tk.Toplevel()
    ventana1.title("Tus palabras")
    etiqueta1 = tk.Label(ventana1, text="Estas son tus palabras:")
    etiqueta1.pack()

    # Conexión a la base de datos
    conexion = sqlite3.connect('basedatos.db')
    cursor = conexion.cursor()

    # Consulta para recuperar las palabras
    cursor.execute("SELECT palabra FROM palabras")
    palabras = cursor.fetchall()

    for palabra in palabras:
        etiqueta = tk.Label(ventana1, text=palabra[0])
        etiqueta.pack()

    # Cerrar conexión
    conexion.close()

def mostrar_ventana2():
    ventana2 = tk.Toplevel()
    ventana2.title("Palabras nuevas")
    frase_inicio = "Si fueras 5 palabras, cuáles serías?:"
    etiqueta_frase = tk.Label(ventana2, text=frase_inicio)
    etiqueta_frase.pack()

    entrada1 = tk.Entry(ventana2)
    entrada1.pack()
    entrada2 = tk.Entry(ventana2)
    entrada2.pack()
    entrada3 = tk.Entry(ventana2)
    entrada3.pack()
    entrada4 = tk.Entry(ventana2)
    entrada4.pack()
    entrada5 = tk.Entry(ventana2)
    entrada5.pack()

    def guardar_palabras_nuevas():
        palabras = [entrada1.get(), entrada2.get(), entrada3.get(), entrada4.get(), entrada5.get()]

        # Conexión a la base de datos
        conexion = sqlite3.connect('basedatos.db')
        cursor = conexion.cursor()

        # Crear la tabla "palabras" si no existe
        cursor.execute('''CREATE TABLE IF NOT EXISTS palabras
                            (id INTEGER PRIMARY KEY AUTOINCREMENT,
                             palabra TEXT)''')

        # Eliminar las palabras anteriores en la tabla
        cursor.execute("DELETE FROM palabras")

        # Insertar las últimas 5 palabras en la tabla "palabras"
        for palabra in palabras:
            cursor.execute("INSERT INTO palabras (palabra) VALUES (?)", (palabra,))

        # Guardar cambios y cerrar conexión
        conexion.commit()
        conexion.close()

    boton = tk.Button(ventana2, text="Aceptar", command=guardar_palabras_nuevas)
    boton.pack()

ventana_principal = tk.Tk()
ventana_principal.title("Ventana Principal")

boton_ventana1 = tk.Button(ventana_principal, text="Tus palabras", command=mostrar_ventana1)
boton_ventana1.pack()

boton_ventana2 = tk.Button(ventana_principal, text="Palabras nuevas", command=mostrar_ventana2)
boton_ventana2.pack()

ventana_principal.mainloop()


from road import Road
from copy import deepcopy
from collections import deque
from vehicleGenerator import VehicleGenerators
import numpy as np
from scipy.spatial import distance
import random

class Simulator:
    def __init__(self, config = {}) -> None:
        self.setDefaultConfig()
        #update vals
        for attr, val in config.items():
            setattr(self, attr, val)

    def setDefaultConfig(self):
        #time
        self.t = 520.0
        #time step
        self.dt = 1/60
        #frames count
        self.frameCount = 0
        #roads
        self.roads = {}
        self.vehicleGens = deque()

        self.trafficSignals = deque()

    def createTrafficSignals(self, trafficSignal):
            self.trafficSignals.append(trafficSignal)

    def createRoad(self, start, end, startCross, endCross):
        road = Road(start, end, startCross, endCross)
        self.roads[(startCross, endCross)] = road
        # return road

    def createRoads(self, roadsList):
        for roadCoords in roadsList:
            self.createRoad(*roadCoords)

    def createRoadsFromGraph(self, graph):
        self.graph = graph
        for idx in range(len(graph)):
            start = graph[idx][0]
            if len(graph[idx][1]) > 0:
                for vertexIdx in graph[idx][1]:
                    end = (graph[vertexIdx][0][0], graph[vertexIdx][0][1])
                    length = distance.euclidean(start, end)
                    sin = (end[1] - start[1]) / length
                    cos = (end[0] - start[0]) / length
                    self.createRoad((start[0] - 0.3 * sin, start[1] + 0.3 * cos), (end[0] - 0.3 * sin, end[1] + 0.3 * cos), idx, vertexIdx)

    def createGen(self, genConfig):
        self.vehicleGens.append(VehicleGenerators(self, genConfig))

    def update(self):
        # Updating every road
        for roadKey in self.roads:
            road = self.roads[roadKey]
            if len(road.vehicles) > 0 and road.vehicles[0].currentRoadIndex + 1 < len(road.vehicles[0].path):
                vehicle = road.vehicles[0]
                nextRoad = self.roads[vehicle.path[vehicle.currentRoadIndex + 1]]
            else:
                road.update(self.dt, self.t)
                nextRoad = None
            road.update(self.dt, self.t, nextRoad)

        # Checking the roads for out of bounds vehicle
        for roadKey in self.roads:
            road = self.roads[roadKey]
            # If road does not have vehicles, then continue
            if len(road.vehicles) == 0: continue
            # If not
            vehicle = road.vehicles[0]
            # If the first vehicle is out of road bounds
            if vehicle.x >= road.length:
                #if vehicle just wanders:
                if len(vehicle.path) == 1:
                    vehicle.currentRoadIndex = 1
                    newVehicle = deepcopy(vehicle)
                    newVehicle.x = 0
                    crossRoad = self.graph[road.endCross]
                    if len(crossRoad[1]) > 0:
                        if newVehicle.decideToRide():
                            carNums = [len(self.roads[(road.endCross, k)].vehicles) for k in crossRoad[1]]
                            minNum = np.min(carNums)
                            minIdx = [i for i, x in enumerate(carNums) if x == minNum]
                            nextCross = crossRoad[1][random.choice(minIdx)]
                            self.roads[(road.endCross, nextCross)].vehicles.append(newVehicle)
                        else:
                            pass

                # If vehicle has a next road
                if vehicle.currentRoadIndex + 1 < len(vehicle.path):
                    # Updating the current road to next road
                    vehicle.currentRoadIndex += 1
                    # Creating a copy and reseting some vehicle properties
                    newVehicle = deepcopy(vehicle)
                    newVehicle.x = 0
                    # Adding it to the next road
                    nextRoadIndex = vehicle.path[vehicle.currentRoadIndex]
                    self.roads[nextRoadIndex].vehicles.append(newVehicle)
                # In all cases, removing it from its road
                road.vehicles.popleft()

        for signal in self.trafficSignals:
            signal.update(self)

        for gen in self.vehicleGens:
            gen.update()
            if (self.t >= 540 and self.t <= 660) or (self.t >= 1020 and self.t <= 1080):
                gen.vehicleRate = 190
            else:
                gen.vehicleRate = 40
        self.t += self.dt

        if self.t >= 1440:
            self.t = 0


"""
.. testsetup:: *

    from zasim.cagen.utils import *

"""
# This file is part of zasim. zasim is licensed under the BSD 3-clause license.
# See LICENSE.txt for details.

from ..features import HAVE_TUPLE_ARRAY_INDEX

from itertools import product
import numpy as np

if HAVE_TUPLE_ARRAY_INDEX:
    def offset_pos(pos, offset):
        """Offset a position by an offset. Any amount of dimensions should work.

        >>> offset_pos((1, ), (5, ))
        (6,)
        >>> offset_pos((1, 2, 3), (9, 8, 7))
        (10, 10, 10)"""
        if len(pos) == 1:
            return (pos[0] + offset[0],)
        else:
            return tuple([a + b for a, b in zip(pos, offset)])
else:
    def offset_pos(pos, offset):
        """Offset a position by an offset. Only works for 1d."""
        if isinstance(pos, tuple):
            pos = pos[0]
        if isinstance(offset, tuple):
            offset = offset[0]
        return pos + offset

def gen_offset_pos(pos, offset):
    """Generate code to offset a position by an offset.

    >>> gen_offset_pos(["i", "j"], ["foo", "bar"])
    ['i + foo', 'j + bar']"""
    return ["%s + %s" % (a, b) for a, b in zip(pos, offset)]

def dedent_python_code(code):
    '''
    Dedent a bit of python code, like this:

    >>> print dedent_python_code("""# update the histogram
    ...     if result != center:
    ...         self.target.histogram[result] += 1""")
    # update the histogram
    if result != center:
        self.target.histogram[result] += 1
    '''

    lines = code.split("\n")
    resultlines = [lines[0]] # the first line shall never have any whitespace.
    if len(lines) > 1:
        common_whitespace = len(lines[1]) - len(lines[1].lstrip())
        if common_whitespace > 0:
            for line in lines[1:]:
                white, text = line[:common_whitespace], line[common_whitespace:]
                assert line == "" or white.isspace()
                resultlines.append(text)
        else:
            resultlines.extend(lines[1:])
    return "\n".join(resultlines)

def rule_nr_to_multidim_rule_arr(number, digits, base=2):
    """Given the rule `number`, the number of cells the neighbourhood has
    (as `digits`) and the `base` of the cells, this function calculates the
    multidimensional rule table for computing that rule."""

    if base < 256: dtype = "int8"
    else: dtype = "int16" # good luck with that.

    res = np.zeros((base,) * digits, dtype=dtype)
    entries = base ** digits
    blubb = base ** entries
    for position in product(*([xrange(base-1, -1, -1)] * digits)):
        blubb /= base
        d = int(number // (blubb))
        number -= d * (blubb)
        res[position] = d

    return res

def rule_nr_to_rule_arr(number, digits, base=2):
    """Given a rule `number`, the number of cells the neighbourhood has
    (as `digits`) and the `base` of the cells, this function calculates the
    lookup array for computing that rule.

    >>> rule_nr_to_rule_arr(110, 3)
    [0, 1, 1, 1, 0, 1, 1, 0]
    >>> rule_nr_to_rule_arr(26, 3, 3)
    [2, 2, 2, ...]
    """
    entries = base ** digits
    result = [0 for index in range(entries)]
    blubb = base ** entries
    for e in range(entries - 1, -1, -1):
        blubb /= base
        d = int(number // (blubb))
        number -= d * (blubb)
        result[e] = d

    return result

def elementary_digits_and_values(neighbourhood, base=2, rule_arr=None):
    """From a neighbourhood, the base of the values used and the array that
    holds the results for each combination of neighbourhood values, create a
    list of dictionaries with the neighbourhood values paired with their
    result_value ordered by the position like in the rule array.

    If the rule_arr is None, no result_value field will be generated."""
    digits_and_values = []
    offsets = neighbourhood.offsets
    names = neighbourhood.names
    digits = len(offsets)

    for i in range(base ** digits):
        values = rule_nr_to_rule_arr(i, digits, base)
        asdict = dict(zip(names, values))
        digits_and_values.append(asdict)

    if rule_arr is not None:
        if not isinstance(rule_arr, np.ndarray) or len(rule_arr.shape) == 1:
            indices = enumerate(xrange(base ** digits))
        else:
            indices = enumerate(reversed(list(product(*([xrange(base-1,-1,-1)] * digits)))))

        for index, rule_idx in indices:
            digits_and_values[index].update(result_value = rule_arr[rule_idx])
    return digits_and_values


# Given the names and grades for each student in a class of  students, store them in a
# nested list and print the name(s) of any student(s) having the second lowest grade.

# Note: If there are multiple students with the second lowest grade, order their names alphabetically and print each name on a new line.

# Print the name(s) of any student(s) having the second lowest grade in.
# If there are multiple students, order their names alphabetically and print each one on a new line.

std_data = []
n = int(input())

# accept name and grade

for i in range(n):
    name = input()
    grade = float(input())
    std_data.append([name,grade])

std_set = set()
for i in range(n):
    std_set.add(std_data[i][1])

stdList = list(std_set)
stdList.sort()
grade_list = []

# Fetching data of student whoes marks are same as 2nd lowest student

for i in range(n):
    if stdList[1] ==  std_data[i][1]:
        grade_list.append(std_data[i])
grade_list.sort()
for i in range(len(grade_list)):
    print(grade_list[i][0])


"""
URL configuration for backend project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions

schema_view = get_schema_view(
    openapi.Info(
        title="SoC Portal API",
        default_version="v1",
        description="Test description",
        terms_of_service="https://www.google.com/policies/terms/",
        contact=openapi.Contact(email="contact@wncc.local"),
        license=openapi.License(name="BSD License"),
    ),
    permission_classes=[],
    public=True,
)

urlpatterns = [
    path("admin/", admin.site.urls),
    path("api/accounts/", include("accounts.urls")),
    path("api/dashboard/", include("dashboard.urls")),
    path("api/projects/", include("projects.urls")),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [
    path(
        "swagger<format>/", schema_view.without_ui(cache_timeout=0), name="schema-json"
    ),
    path(
        "swagger/",
        schema_view.with_ui("swagger", cache_timeout=0),
        name="schema-swagger-ui",
    ),
    path("redoc/", schema_view.with_ui("redoc", cache_timeout=0), name="schema-redoc"),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import sys
from collections import defaultdict

def tpsortutil(u, visited, stack, cur):
    visited[u] = True
    for i in graph[u]:
        if not visited[i]:
            tpsortutil(i, visited, stack, cur)
        elif i in cur:
            return
    stack.append(u)

def topologicalsort(graph, vertices):
    visited = [False] * vertices
    stack = []
    for i in range(vertices):
        cur = set()
        if not visited[i] and graph[i]:
            tpsortutil(i, visited, stack, cur)
    del cur
    stack = stack[::-1]
    print(stack)

if __name__ == "__main__":
    vertices = int(input())
    graph = defaultdict(list)
    edges = int(input())
    for _ in range(edges):
        edge = [int(x) for x in input().split()]
        graph[edge[0]].append(edge[1])
    print(graph)
    topologicalsort(graph, vertices)


import streamlit as st
import calculator_logic

st.title("Calculator App")

num1 = st.number_input("Enter the first number:")
num2 = st.number_input("Enter the second number:")
operation = st.selectbox("Select an operation", calculator_logic.OPERATIONS)

if st.button("Calculate"):
    result = calculator_logic.calculate(num1, num2, operation)
    st.success(f"The result is {result}")

# Define a function to display the signature
def display_signature():
    st.markdown(
        """
        <style>
        .signature {
            font-size: 1rem;
            font-style: italic;
            text-align: center;
            padding: 1rem 0;
            color: #333;
            transition: color 0.5s ease-in-out;
        }
        .signature:hover {
            color: #007bff;
        }
        </style>
        """
        , unsafe_allow_html=True
    )
    st.markdown(
        """
        <div class="signature">
        Made with ❤ by Shib Kumar Saraf
        </div>
        """
        , unsafe_allow_html=True
    )

# Add the signature to your Streamlit app
display_signature()


import numpy as np
from sklearn.linear_model import LogisticRegression
from random import randrange
from math import ceil, floor

# Загрузка данных из текстового файла
data = np.genfromtxt('данные двумерная модель.txt', skip_header=1)  # Пропустить первую строку с названиями столбцов

# Разделение данных на факторы (x) и зависимую переменную (y)
X = data[:, :-1]  # Первые 6 столбцов
y = data[:, -1]   # Последний столбец

# Создание и обучение модели множественной регрессии
model = LogisticRegression()
model.fit(X, y)

# Вывод коэффициентов регрессии
print("Коэффициенты регрессии:")
print("a1, a2:", [round(x, 3) for x in model.coef_[0]])
print("b (пересечение):", round(model.intercept_[0], 3))

X_min = [float('inf'), float('inf')]
X_max = [float('-inf'), float('-inf')]
for x in X:
    for i in range(len(x)):
        if x[i] < X_min[i]:
            X_min[i] = x[i]
        if x[i] > X_max[i]:
            X_max[i] = x[i]

R_X = []
for i in range(len(X_min)):
    R_X.append(randrange(ceil(X_min[i]), floor(X_max[i])))

print(R_X)

# Спрогнозировать новое значение y на основе заданных факторов (замените значения x_new)
x_new = np.array([R_X])
y_pred = model.predict(x_new)
print(f"Прогнозное значение y для новых данных: {y_pred[0]:.2f}")

print(f'{model.coef_[0][0]:.3f} {model.coef_[0][1]:.3f}, {model.intercept_[0]:.3f}')


# -*- coding: utf-8 -*-
# @Author : yxn
# @Date : 2022/1/27 20:46
# @IDE : PyCharm(2021.3.1) Python3.98
def toStr(n, base):
    """递归实现"""
    converSting = "0123456789ABCDEF"
    if n < base:
        return converSting[n]
    else:
        return toStr(n // base, base) + converSting[n % base]

if __name__ == '__main__':
    print(toStr(100, 16))
    # 获取最大递归深度
    import sys

    print(sys.getrecursionlimit())  # 1000

13_递归-转换任意进制.py

13_递归-转换任意进制.py


import torch
import torch.nn as nn
import torch.nn.functional as F
from _polytope_ import Polytope, Face
import utilities as utils
from collections import OrderedDict
import numpy as np

import time
import copy
import convex_adversarial.convex_adversarial as ca

import full_lp as flp

class PLNN(nn.Module):
    #TODO: determine if building net addition was necessary
    # add some explanations for some methods
    """ Simple piecewise neural net.
        Fully connected layers and ReLus only
    """
    def __init__(self, layer_sizes=None, bias=True, dtype=torch.FloatTensor):
        super(PLNN, self).__init__()

        if layer_sizes is None:
            layer_sizes = [32, 64, 128, 64, 32, 10]
        self.layer_sizes = layer_sizes
        self.dtype = dtype
        self.fcs = []
        self.bias = bias
        self.net = self.build_network(layer_sizes)

    def build_network(self, layer_sizes):
        layers = OrderedDict()

        num = 1
        for size_pair in zip(layer_sizes, layer_sizes[1:]):
            size, next_size = size_pair
            layer = nn.Linear(size, next_size, bias=self.bias).type(self.dtype)
            layers[str(num)] = layer
            self.fcs.append(layer)
            num = num + 1
            layers[str(num)] = nn.ReLU()
            num = num + 1

        del layers[str(num-1)]      # No ReLU for the last layer

        net = nn.Sequential(layers).type(self.dtype)
        print(self.layer_sizes)

        return net

    def get_parameters(self):
        params = []
        for fc in self.fcs:
            fc_params = [elem for elem in fc.parameters()]
            for param in fc_params:
                params.append(param)

        return params

    def config_str_to_config_list(self, config_str):
        """ Given str of configs, converts to list of torch tensors of right
            layer sizes
        """

        assert isinstance(config_str, str)
        assert len(config_str) ==  sum(self.layer_sizes[1:-1])
        splits = []
        running_idx = 0
        for el in self.layer_sizes[1:-1]:
            layer_config = config_str[running_idx:running_idx + el]
            layer_config = torch.Tensor([float(el) for el in layer_config])
            # Do some cuda nonsense here?
            splits.append(layer_config)
            running_idx += el
        return splits

    def relu_config(self, x, return_pre_relus=True):
        pre_relus = self.forward_by_layer(x)

        configs = [(pre_relu.squeeze() > 0).type(torch.float32)
                   for pre_relu in pre_relus]
        if return_pre_relus:
            return pre_relus, configs
        else:
            return configs

    def make_adversarial_constraints(self, polytope, true_label,
                                     domain):
        """ Given a config computes the linear map in terms of this config
            for all neurons INCLUDING the output neurons (logits) and generates
            the polytope constraints for the neuron config and
            constraints for each of the decision boundaries

            configs - as usual
            true_label -

        """

        # Make all the adversarial_constraints:

        #if(x) = Ax + b (in R^#logits)
        # adversarial constraints are:
        #   f_true(x) - f_j(x) = 0 (for all j != true)
        #  ~ which is ~
        #   <a_true, x> + b_true - <a_j, x> - b_j = 0
        #  ~ which is ~
        # <a_true - a_j, x> = b_j - b_true

        total_a = polytope.linear_map['A']
        total_b = polytope.linear_map['b']

        num_logits = total_a.shape[0]

        facets = []
        true_a = total_a[true_label]
        true_b = total_b[true_label]

        for i in range(num_logits):
            if i == true_label:
                continue
            dec_bound = {'A': true_a - total_a[i],
                         'b': total_b[i] - true_b}
            new_facet = polytope.facet_constructor(None, facet_type='decision',
                                                   extra_tightness=dec_bound)
            if new_facet.fast_domain_check():
                facets.append(new_facet)
        return facets

    def compute_polytope_config(self, configs, comparison_form_flag=False,
                                uncertain_constraints=None, as_tensor=False):

        lambdas = [torch.diag(config) for config in configs]
        js = [torch.diag(-2 * config + 1) for config in configs]

        # Compute Z_k = W_k * x + b_k for each layer
        wks = [self.fcs[0].weight]
        bks = [self.fcs[0].bias]
        for (i, fc) in enumerate(self.fcs[1:]):
            current_wk = wks[-1]
            current_bk = bks[-1]
            current_lambda = lambdas[i]
            precompute = fc.weight.matmul(current_lambda)
            wks.append(precompute.matmul(current_wk))
            bks.append(precompute.matmul(current_bk) + fc.bias)

        a_stack = []
        b_stack = []
        for j, wk, bk in zip(js, wks, bks):
            a_stack.append(j.matmul(wk))
            b_stack.append(-j.matmul(bk))
        if as_tensor:
            return {'a_stack': a_stack,
                    'b_stack': b_stack,
                    'total_a': wks[-1],
                    'total_b': bks[-1]}

        polytope_A = utils.as_numpy(torch.cat(a_stack, dim=0))
        polytope_b = utils.as_numpy(torch.cat(b_stack, dim=0))

        if(comparison_form_flag):
            polytope_A, polytope_b = utils.comparison_form(polytope_A, polytope_b)

        return {'poly_a': polytope_A,
                'poly_b': polytope_b,
                'configs': configs,
                'total_a': wks[-1],
                'total_b': bks[-1]
                }

    def compute_polytope(self, x, comparison_form_flag=False, as_tensor=False):
        pre_relus, configs = self.relu_config(x, return_pre_relus=True)
        poly_out = self.compute_polytope_config(configs, comparison_form_flag,
                                                as_tensor=as_tensor)
        poly_out['pre_relus'] = pre_relus
        return poly_out

    def compute_matrix(self, configs):
        M = torch.eye(self.layer_sizes[0])

        for config, fc, layer_size in zip(configs, self.fcs, self.layer_sizes):
            nullifier = torch.Tensor([config.numpy() for _ in range(0, layer_size)])
            M_layer_prime = fc.weight * torch.transpose(nullifier, 0, 1)
            M = torch.matmul(M_layer_prime, M)

        M = torch.matmul(self.fcs[-1].weight, M)
        return M

    def forward_by_layer(self, x):
        pre_relus = []

        x = x.view(-1, self.layer_sizes[0])
        for fc in self.fcs[:-1]:
            x = fc(x)
            pre_relus.append(x.clone())
            x = F.relu(x)
        return pre_relus

    def forward(self, x):
        x = x.view(-1, self.layer_sizes[0])
        for fc in self.fcs[:-1]:
            x = F.relu(fc(x))
        return self.fcs[-1](x) # No ReLu on the last one

    def compute_interval_bounds(self, domain_obj, compute_logit_bounds=False,
                                as_tensor=False):
        """ For each neuron computes a bound for the range of values each
            pre-ReLU can take.
        ARGS:
            domain_obj : Domain - object used to hold bounding boxes
            on_off_format: boolean - if True, we return the more fine-grained
                                     list which displays if neurons are on or
                                     off, instead of stable
        RETURNS:
            returned_bounds : list of tensors giving pre-Relu bounds
            uncertain_set: list of tensors with 1 if uncertain about this
                           neuron in the list
            list of length (# fully connected layers - 1), where each element
            is a tensor of shape (num_neurons, 2) for the bounds for the preReLU
        """

        box = domain_obj.box_to_tensor()
        # setup + asserts
        assert all(box[:, 0] <= box[:, 1])

        # Redoing this one more time

        # Redo this but doing it right :
        midpoint_matrix = torch.Tensor([[1.0], [1.0]]) / 2.0
        ranges_matrix = torch.Tensor([[-1.0], [1.0]]) / 2.0
        returned_bounds = []
        dead_set = [] # list of tensors, 1 if always on or off
        working_bounds = box
        current_low, current_high = box[:, 0], box[:, 1]

        if compute_logit_bounds:
            layers_to_check = self.fcs
        else:
            layers_to_check = self.fcs[:-1]

        for fc in layers_to_check:
            weight, bias = fc.weight, fc.bias

            weight_pos, weight_neg = utils.split_tensor_pos(weight)
            new_high = (torch.matmul(weight_pos, current_high) +
                        torch.matmul(weight_neg, current_low))
            new_low = (torch.matmul(weight_pos, current_low) +
                       torch.matmul(weight_neg, current_high))
            if bias is not None:
                new_high += bias
                new_low += bias
            returned_bounds.append(torch.stack([new_low, new_high], dim=1))

            current_low = F.relu(new_low)
            current_high = F.relu(new_high)

        if as_tensor:
            return returned_bounds
        else:
            return [utils.as_numpy(_) for _ in returned_bounds]

    def compute_improved_ia_bounds(self, domain_obj):
        """ Implements the improved interval bounds as presented here:
            https://arxiv.org/pdf/1809.03008.pdf (appendix C)
            [also done with gradients pushed through so we can build RS loss ]

            # CODE HEAVILY BORROWED FROM https://github.com/MadryLab/relu_stable/blob/master/models/MNIST_improved_ia.py
            # (but we're transposed from that code)
        """

        box = domain_obj.box_to_tensor()
        init_lows = box[:, 0]
        init_highs = box[:, 1]
        assert all(init_lows <= init_highs) # assert lows less than highs
        layers_to_check = self.fcs[:-1] # set the

        intermed_lows, intermed_highs = [], []

        # define the recursive call
        def recurs(layer_num, lows, highs, weights, biases):
            assert len(lows) == len(highs) == len(weights) == len(biases) == layer_num
            # current layer
            low = lows[0]
            high = highs[0]
            weight = weights[0]
            bias = biases[0]

            # Base case
            if layer_num == 1:
                weight_pos, weight_neg = utils.split_tensor_pos(weight)
                next_low = (torch.matmul(weight_pos, init_lows) +
                            torch.matmul(weight_neg, init_highs) + bias)
                next_high = (toch.matmul(weight_pos, init_highs) +
                             torch.matmul(weight_neg, init_lows) + bias)
                return next_low, next_high

            # Recursive case
            prev_weight = weights[1]
            prev_bias = biases[1]

            # Compute W_A, W_N (need to zero out COLUMNS here)
            w_a = torch.matmul(weight, (low > 0).diag_embed())
            w_n = weight - w_a
            w_n_pos, w_n_neg = utils.split_tensor_pos(w_n)

            w_prod = torch.matmul(w_a, prev_weight)
            b_prod = torch.matmul(w_a, prev_bias)

            # Compute prev layer bounds
            prev_low = (torch.matmul(w_n_pos, low) +
                        torch.matmul(w_n_neg, high) + bias)
            prev_high = (torch.matmul(w_n_pos, high) +
                         torch.matmul(w_n_neg, low) + bias)

            # Recurse
            deeper_lows, deeper_highs = recurs(layer_num - 1, lows[1:], highs[1:],
                                               [w_prod] + weights[2:],
                                               [b_prod] + biases[2:])
            return (prev_low + deeper_lows, prev_high + deeper_highs)

        # compute the lower and upper bounds for all neurons
        running_lows = [init_lows]
        running_highs = [init_highs]
        running_weights = [self.fcs[0].weight]
        running_biases = [self.fcs[0].bias]

        for layer_num, layer in enumerate(self.fcs[:-1]):
            new_lows, new_highs = recurs(layer_num + 1, running_lows, running_highs,
                                         running_weights, running_biases)
            running_lows = [new_lows] + running_lows
            running_highs = [new_highs] + running_highs
            running_weights = self.fcs[layer_num + 1].weight
            running_biases = self.fcs[layer_num + 1].bias
        return running_lows[::-1], running_highs[::-1]

    def compute_full_lp_bounds(self, domain_obj):
        """ Compute the full linear program values.
            Code here is in a different file
        """
        return flp.compute_full_lp_bounds(self, domain_obj)

    def compute_dual_lp_bounds(self, domain_obj):
        """ Use KW to actually find the bounds. Uses L_inf bounds to help
            get better bounds
        """
        low_bounds = torch.Tensor(domain_obj.box_low)
        high_bounds = torch.Tensor(domain_obj.box_high)
        midpoint = ((low_bounds + high_bounds) / 2.0).view(1, -1)
        box_bounds = (low_bounds, high_bounds)

        dual_net = ca.DualNetwork(self.net, midpoint, domain_obj.linf_radius,box_bounds=box_bounds).dual_net
        bounds, dead_set = [], []
        for el in dual_net:
            if isinstance(el, ca.DualReLU):
                bounds.append(torch.cat((el.zl.view(-1, 1), el.zu.view(-1, 1)),
                                        dim=1))
                dead_set.append(~el.I.squeeze())
        return bounds

    def compute_dual_ia_bounds(self, domain_obj):
        """ Use both interval analysis and dual bounds to get best bounds """

        ia = self.compute_interval_bounds(domain_obj)
        dd = self.compute_dual_lp_bounds(domain_obj)
        bounds = []
        for i, d in zip(ia, dd):
            stacked = torch.stack((i, d))
            new_lows = torch.max(stacked[:, :, 0], dim=0)[0]
            new_highs = torch.min(stacked[:, :, 1], dim=0)[0]
            new_bounds = torch.stack((new_lows, new_highs), dim=1)
            bounds.append(new_bounds)
        return bounds

    def fast_lip_all_vals(self, x, l_q, on_off_neurons):
        """ Does the fast_value for all possible c's """
        num_logits = self.fcs[-1].out_features
        if not isinstance(x, torch.Tensor):
            true_label = self(torch.Tensor(x)).max(1)[1].item()
        else:
            true_label = self(x).max(1)[1].item()

        c_vecs, lip_values = [], []
        for i in range(num_logits):
            if true_label == i:
                continue
            c_vec = torch.zeros(num_logits)
            c_vec[true_label] = 1.0
            c_vec[i] = -1.0
            lip_value = self.fast_lip(c_vec, l_q, on_off_neurons)
            c_vecs.append(c_vec)
            lip_values.append(lip_value)

        return c_vecs, lip_values

    def fast_lip(self, c_vector, l_q, on_off_neurons):
        """
        Pytorch implementation of fast_lip. Might be buggy? Who knows?
        see : https://arxiv.org/pdf/1804.09699.pdf for details

        INPUTS:
            c_vector: tensor that multiplies the output vector:
                      we compute gradient of c^Tf(x)
            l_q : int - q_norm of lipschitzness that we compute
                        (is dual norm: e.g. if bounds come from an l_inf box,
                         this should be 1)
            on_off_neurons : list of LongTensors (entries in -1, 0 or 1)
                             corresponding to the set of
                             (off, uncertain, on, respectively) neurons
                             inside the domain
        RETURNS:
            upper bound on lipschitz constant
        """

        ######################################################################
        #   First generate inputs needed by fast_lip algorithm               #
        ######################################################################

        # --- split off active and uncertain neurons
        # -1 means off (don't care)
        #  0 means UNCERTAIN
        #  1 means ACTIVE

        active_neuron_list, uncertain_neuron_list = [], []
        for neuron_by_layer in on_off_neurons:
            active_neuron_list.append((neuron_by_layer == 1))
            uncertain_neuron_list.append((neuron_by_layer == 0))

        # --- get list of weights, initialize placeholders
        weights = [layer.weight for layer in self.fcs[:-1]]
        weights.append(c_vector.matmul(self.fcs[-1].weight).view(1, -1))

        constant_term = weights[0]
        lowers = [torch.zeros_like(constant_term)]
        uppers = [torch.zeros_like(constant_term)]

        ######################################################################
        #   Loop through layers using the _bound_layer_grad subroutine       #
        ######################################################################

        for i in range(len(weights) - 1):
            subroutine_out = self._bound_layers_grad(constant_term, lowers[-1],
                                                     uppers[-1],
                                                     weights[i + 1],
                                                     active_neuron_list[i],
                                                     uncertain_neuron_list[i])
            constant_term, upper, lower = subroutine_out
            lowers.append(lower)
            uppers.append(upper)

        ######################################################################
        #   Finalize and return the output                                   #
        ######################################################################

        low_bound = (constant_term + lowers[-1]).abs()
        upp_bound = (constant_term + uppers[-1]).abs()

        layerwise_max = torch.where(low_bound > upp_bound, low_bound, upp_bound)
        return torch.norm(layerwise_max, p=l_q).item()

    def _bound_layers_grad(self, constant_term, lower, upper, weight,
                           active_neurons, uncertain_neurons):
        """ Subroutine for fast_lip.
            Assume weight has shape [m, n]

        ARGS: (let's make sure the types and shapes all mesh)
            constant_term: floatTensor shape (n, n_0)
            lower: floatTensor shape (n, n_0)
            upper: floatTensor shape (n, n_0)
            weight: floatTensor shape (m, n)
            active_neurons: torch.Tensor shape (n,)
            uncertain_neurons: torch.Tensor shape (n,)
        RETURNS:
            new constant term, lower, and upper, each with shape (m, n_0)
        """

        # ASSERTS ON SHAPES FOR DEBUGGING
        n_0 = self.layer_sizes[0]
        n = weight.shape[1]
        assert constant_term.shape == (n, n_0)
        assert lower.shape == (n, n_0)
        assert upper.shape == (n, n_0)
        assert active_neurons.shape == (n,)
        assert uncertain_neurons.shape == (n,)

        # Make diagonals and split weights by +/-
        active_diag = torch.diag(active_neurons).float()
        uncertain_diag = torch.diag(uncertain_neurons).float()
        pos_weight, neg_weight = utils.split_tensor_pos(weight)

        # Compute the new constant_term
        new_constant_term = weight.matmul(active_diag).matmul(constant_term)

        # Make new upper bounds/lower bounds
        cons_low = constant_term + lower
        _, neg_cons_low = utils.split_tensor_pos(cons_low)
        cons_upp = constant_term + upper
        pos_cons_upp, _ = utils.split_tensor_pos(cons_upp)

        new_upper = (pos_weight.matmul(active_diag).matmul(upper) +
                     neg_weight.matmul(active_diag).matmul(lower) +
                     neg_weight.matmul(uncertain_diag).matmul(neg_cons_low) +
                     pos_weight.matmul(uncertain_diag).matmul(pos_cons_upp))

        new_lower = (pos_weight.matmul(active_diag).matmul(lower) +
                     neg_weight.matmul(active_diag).matmul(upper) +
                     pos_weight.matmul(uncertain_diag).matmul(neg_cons_low) +
                     neg_weight.matmul(uncertain_diag).matmul(pos_cons_upp))
        return new_constant_term, new_upper, new_lower

class PLNN_seq(PLNN):
    """ Simple piecewise neural net.
        Fully connected layers and ReLus only

        built from nn.Sequential
    """

    def __init__(self, sequential, layer_sizes, dtype=torch.FloatTensor):
        super(PLNN_seq, self).__init__(layer_sizes, dtype)
        self.fcs = [layer for layer in sequential if type(layer) == nn.Linear]

        self.net = sequential

class LinearRegionCollection(object):
    """ Takes a ReturnObj and builds a lot of linear regions and stores them
    """

    def __init__(self, plnn_obj, return_obj, objective_vec=None,
                 do_setup=False):
        self.plnn_obj = plnn_obj
        self.return_obj = return_obj
        self.collection = {}
        for config in return_obj.seen_polytopes:
            self.collection[config] = LinearRegion(plnn_obj, config,
                                                   return_obj=return_obj,
                                                   objective_vec=objective_vec,
                                                   do_setup=do_setup)

    def get_maximum_lipschitz_constant(self):
        return max(_.get_lipschitz_constant()
                   for _ in self.collection.values())

    def gradient_angle_list(self):
        """ Gets the gradient angles between neighboring linear regions """
        angle_list = {}
        for (u, v) in self.return_obj.polytope_graph.keys():
            u_grad = self.collection[u].get_gradient()
            v_grad = self.collection[v].get_gradient()
            angle_list[(u, v)] = utils.angle(u_grad, v_grad)
        return angle_list

    def gradient_magnitude_diff_list(self, grad_fxn=None):
        """ Gets the magnitude of gradient difference
            between neighboring linear regions
        """

        if grad_fxn is None:
            grad_fxn = lambda u, v: torch.norm(u - v).item()
        output = {}
        for (u, v) in self.return_obj.polytope_graph.keys():
            u_grad = self.collection[u].get_gradient()
            v_grad = self.collection[v].get_gradient()
            output[(u, v)] = grad_fxn(u_grad, v_grad)
        return output

    def get_greedy_lipschitz_components(self):
        """ Returns dict of str -> [str1, ..., ] mapping locally maximal
            linear regions to the set of regions that will greedily
            approach this local max
        """
        # Let's just be really naive about this

        def get_ascent_neighbor(node):
            """ Gets the neighbor that has highest lipschitz constant
                Returns None if nothing has higher than this one
            """

            current = node.get_lipschitz_constant()
            neighbors = [(_, _.get_lipschitz_constant())
                          for _ in node.get_neighbors()]
            max_neighbor = max(neighbors, key=lambda p: p[1])
            if max_neighbor[1] > current:
                return max_neighbor[0]
            return None

        def greedy_search_single_node(start_config):
            """ Start with a single sign_config and do greedy search
                to find max_lipschitz constant. Return the sign_config
                of the greedy search output
            """
            current_node = self.collection[start_config]
            while True:
                next_node = get_ascent_neighbor(current_node)
                if next_node is None:
                    break
                else:
                    current_node = next_node
            return current_node.sign_config

        greedy_output = {}
        for config in self.collection.keys():
            greedy_parent = greedy_search_single_node(config)
            if greedy_parent not in greedy_output:
                greedy_output[greedy_parent] = []
            greedy_output[greedy_parent].append(config)

        return greedy_output

class LinearRegion(object):
    """ Holds info and shortcuts to work with linear regions """
    @classmethod
    def process_return_obj(cls, plnn_obj, return_obj, objective_vec=None,
                           do_setup=False):
        """ Given a GeoCertReturn object, will build a linear region for
            all of the 'seen polytopes' and return the outputs in a
            dict keyed on teh sign_configs
        """

        output = {}
        for config in return_obj.seen_polytopes:
            output[config] = cls(plnn_obj, config,
                                 return_obj=return_obj,
                                 objective_vec=objective_vec,
                                 do_setup=do_setup)
        return output

    def __init__(self, plnn_obj, sign_config, return_obj=None,
                 objective_vec=None, do_setup=False):
        """ Initializes a Linear Region object
        ARGS:
            plnn_obj - the network this region is linear for
            sign_config - the neuron configuration of the region
            return_obj : GeoCertReturn object - if not None is an
                         output of GeoCert which contains info about
                         the linear regions.
        """
        super(LinearRegion, self).__init__()
        self.plnn_obj = plnn_obj
        self.sign_config = sign_config
        self.hex_config = hex(int(self.sign_config, 2))
        self.return_obj = return_obj
        self.objective_vec = objective_vec

        # setting up attributes to be stored later
        self._polytope_config = None
        self.polytope = None
        self.linear_map = None
        self.jacobian = None
        self.largest_sv = None

        if do_setup:
            self.setup()

    def __repr__(self):
        return "LinearRegion: %s" % self.hex_config

    def get_neighbors(self):
        """ If the return obj is not None, will error. Otherwise will
            return a list of neighboring LinearRegion objects
        """
        assert self.return_obj is not None
        neigbor_list = []
        for edge in self.return_obj.polytope_graph:
            if self.sign_config == edge[0]:
                neigbor_idx = 1
            elif self.sign_config == edge[1]:
                neigbor_idx = 0
            else:
                continue
            neigbor_list.append(edge[neigbor_idx])

        return [LinearRegion(self.plnn_obj, neigbor_config,
                             return_obj=self.return_obj,
                             objective_vec=self.objective_vec)
                for neigbor_config in neigbor_list]

    def _get_polytope_config(self):
        if self._polytope_config is not None:
            return self._polytope_config

        plnn_obj = self.plnn_obj
        config = plnn_obj.config_str_to_config_list(self.sign_config)
        self._polytope_config = plnn_obj.compute_polytope_config(config)
        return self._polytope_config

    def setup(self):
        self.get_polytope()
        self.get_linear_map()
        self.get_jacobian()
        self.get_largest_singular_value()

    def get_polytope(self):
        """ For this linear region will return the polytope for which
            the neural net satisfies the given neuron configuration
        """
        if self.polytope is not None:
            return self.polytope

        _polytope_config = self._get_polytope_config()
        self.polytope = {'A': _polytope_config['poly_a'],
                         'b': _polytope_config['poly_b']}
        return self.polytope

    def get_linear_map(self):
        """ For this linear region will return a torch.nn.Linear
            object corresponding to the linear map at this neuron
            configuration
        """
        if self.linear_map is not None:
            return self.linear_map
        _polytope_config = self._get_polytope_config()
        A = nn.Parameter(_polytope_config['total_a'])
        b = nn.Parameter(_polytope_config['total_b'])
        linear_map = nn.Linear(*A.shape)
        linear_map.weight = A
        linear_map.bias = b

        self.linear_map = linear_map
        return self.linear_map

    def get_jacobian(self):
        """ For this linear region will get the jacobian at this
            linear piece
        """
        if self.jacobian is not None:
            return self.jacobian

        linear_map = self.get_linear_map()
        self.jacobian = linear_map.weight
        return self.jacobian

    def get_largest_singular_value(self):
        """ Will return the largest singular value of the jacobian
            of this linear region
        """
        if self.largest_sv is not None:
            return self.largest_sv

        jacobian = self.get_jacobian()
        self.largest_sv = jacobian.svd().S[0].item()
        return self.largest_sv

    def get_gradient(self):
        assert self.objective_vec is not None
        return self.objective_vec.matmul(self.get_jacobian())

    def get_lipschitz_constant(self):
        if self.objective_vec is not None:
            return self.objective_vec.matmul(self.get_jacobian()).norm().item()
        else:
            return self.get_largest_singular_value()


from django.urls import path

from . import views

app_name = 'blog'

urlpatterns = [
    path('', views.index, name="index"),
    path('detalhes/<int:pk>/<slug:slug>', views.detail, name="details"),
    path('post/novo/', views.post, name="new_post"),
    path('editar/post/<int:pk>', views.edit, name="edit"),
    path('filter/<int:pk>/<str:username>', views.filter, name="filter"),
    path('search/', views.search, name="search"),
    path('delete/<int:pk>', views.delete, name="delete_post"),
    path('contato/', views.contato, name="contato"),
]


import pygame
from pygame.locals import KEYDOWN, K_ESCAPE, QUIT
from os import path
import parameters.enums as en
from Objects.player import Player, Wall
from Objects.map import Map
from Objects.robot import Robot
import numpy as np
from Objects.machinery import Machinery, Destiny
import sys
import torch
from Objects.utils import GameBuffer
import pickle

class Game(pygame.sprite.Sprite):
    SCORE = 0

    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((en.WIDTH, en.HEIGHT))
        self.clock = pygame.time.Clock()
        self.load_data()
        self.checkpoint = "robotCheckpoint" if path.exists("robotCheckpoint") else None

    def load_data(self):
        game_folder = path.dirname(__file__)
        self.map = Map(game_folder + "/Static/map.txt")

    def new(self):
        """Initialize all variables."""
        self.buffer = GameBuffer()
        self.allSprites = pygame.sprite.Group()
        self.machineryParts = pygame.sprite.Group()
        self.destination = pygame.sprite.Group()
        self.robots = pygame.sprite.Group()
        self.walls = pygame.sprite.Group()
        self.part = Machinery(self)
        for j, row in enumerate(self.map.data):
            for i, col in enumerate(row):
                if col == "1":
                    Wall(self, i, j)
                elif col == "D":
                    self.destiny = Destiny(self, i, j)
                elif col == "P":
                    self.player = Player(self, self.buffer, i, j)
                elif col == "R":
                    self.robot = Robot(self, self.buffer, i, j, self.checkpoint)

    def quit(self):
        pygame.quit()
        sys.exit()

    def events(self):
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    self.robot.save()
                    self.quit()
            if event.type == QUIT:
                self.robot.save()
                self.quit()

    def get_screen(self):
        screen = pygame.transform.scale(self.screen, (60, 60))
        screen = np.array(pygame.surfarray.array3d(screen))
        screen = screen.transpose((2, 1, 0))
        return torch.from_numpy(screen)

    def run(self):
        self.playing = True
        prevScore = self.SCORE
        while self.playing:
            self.dt = self.clock.tick(en.FPS) / 1000
            screenMatrix = self.get_screen()
            self.events()
            self.updates(windowPixel=screenMatrix)
            self.draw()
            if prevScore != self.SCORE:
                self.robot.train()
                prevScore = self.SCORE

    def updates(self, **args):
        for sprite in self.allSprites:
            if isinstance(sprite, Robot) or isinstance(sprite, Player):
                sprite.update(args["windowPixel"])
            else:
                sprite.update()

    def draw_grid(self):
        for x in range(0, en.WIDTH, en.TILE_SIZE):
            pygame.draw.line(self.screen, en.LIGHTGREY, (x, 0), (x, en.HEIGHT))
        for y in range(0, en.HEIGHT, en.TILE_SIZE):
            pygame.draw.line(self.screen, en.LIGHTGREY, (0, y), (en.WIDTH, y))

    def draw(self):
        self.screen.fill(en.BGCOLOR)
        self.draw_grid()
        self.allSprites.draw(self.screen)
        pygame.display.flip()

g = Game()
while True:
    g.new()
    g.run()


import json
from random import uniform

import matplotlib.pyplot as plt

class Scanner:
    def __init__(self, data_filename, n_neighbours):
        self.data_filename = data_filename
        self.n_neighbours = n_neighbours

    def scanner(self, visualize_data=False):
        f = open(self.data_filename, encoding="utf-8")
        content = f.read()
        data = json.loads(content)
        data = sorted(data, key=lambda x: x["coordinates"])

        avg_area_price = 0
        result = []
        differences = []
        for apartment in range(len(data)):
            avg_neigh_price = 0

            neighbours = self.find_neighbours(data[apartment], data)
            for n in neighbours:
                avg_neigh_price += n["price"]
            avg_neigh_price /= self.n_neighbours

            if data[apartment]["price"] < avg_neigh_price:
                # append apartment data and difference between
                # average prices of neighbours and apartment price
                result.append(
                    [data[apartment], avg_neigh_price - data[apartment]["price"]]
                )
            avg_area_price += data[apartment]["price"]
            differences.append([data[apartment], neighbours])
        avg_area_price /= len(data)

        for el in result:
            # append difference between average price in search area
            # and apartment price (profitability)
            el.append(avg_area_price - el[0]["price"])

        # sort data by apartment profitability
        result = sorted(result, key=lambda x: x[1], reverse=True)

        if visualize_data:
            self.visualize_n_neighbours(differences)
        return result

    def find_neighbours(self, apartment, data):
        data = sorted(
            data,
            key=lambda x: (
                (apartment["coordinates"][0] - x["coordinates"][0]) ** 2
                + (apartment["coordinates"][1] - x["coordinates"][1]) ** 2
            )
            ** 0.5,
        )

        neighbours = data[: self.n_neighbours]
        return neighbours

    @staticmethod
    def visualize_n_neighbours(differences):
        colors = ["b", "g", "r", "c", "m", "y", "k"]
        colors_i = 0
        for diff in differences:
            color = colors[colors_i % len(colors)]
            colors_i += 1

            el_coord = diff[0]["coordinates"]
            for i in range(len(diff[1])):
                diff_coord = diff[1][i]["coordinates"]

                # add small number to coordinates so that lines on plot do not overlap
                d = uniform(0.003, 0.005)
                plt.plot(
                    [el_coord[0] + d, diff_coord[0] + d],
                    [el_coord[1] + d, diff_coord[1] + d],
                    "-ro",
                    color=color,
                )
        plt.show()


from __future__ import unicode_literals
import frappe
from frappe import _

def execute(filters=None):
	columns, data = [], []
	columns=get_columns()
	data=get_data(filters,columns)
	return columns, data

def get_columns():
	return [
		{
			"label": _("Loan"),
			"fieldname": "name",
			"fieldtype": "Link",
			"options": "Bank Loan",
			"width": 120
		},
		{
			"label": _("Provider Bank"),
			"fieldname": "loan_type",
			"fieldtype": "Link",
			"options": "Loan Type",
			"width": 120
		},
		{
			"label": _("Drawn Bank"),
			"fieldname": "bank_account",
			"fieldtype": "Link",
			"options": "Bank",
			"width": 100
		},
		{
			"label": _("Status"),
			"fieldname": "status",
			"fieldtype": "Data",
			"width": 120
		},
		{
			"label": _("Loan Amount"),
			"fieldname": "loan_amount",
			"fieldtype": "Currency",
			"width": 100
		},
		{
			"label": _("Posting Date"),
			"fieldname": "posting_date",
			"fieldtype": "Date",
			"width": 120
		},
		{
			"label": _("Start Date"),
			"fieldname": "repayment_start_date",
			"fieldtype": "Date",
			"width": 120
		},
		{
			"label": _("Payment Freq"),
			"fieldname": "payment_frequency",
			"fieldtype": "Data",
			"width": 120
		},
		{
			"label": _("Repayment Amount"),
			"fieldname": "repayment_amount",
			"fieldtype": "Currency",
			"width": 120
		},
		{
			"label": _("Rate Of Interest %"),
			"fieldname": "rate_of_interest",
			"fieldtype": "Float",
			"width": 100
		},
		{
			"label": _("Periods / Paid"),
			"fieldname": "periods_paid",
			"fieldtype": "Data",
			"width": 100
		},
		{
			"label": _("Administrative Expenses Amount"),
			"fieldname": "administrative_expenses_amount",
			"fieldtype": "Currency",
			"width": 120
		},
		{
			"label": _("Total Principal Payable"),
			"fieldname": "total_principal_payable",
			"fieldtype": "Currency",
			"width": 120
		},
		{
			"label": _("Total Principal Paid"),
			"fieldname": "total_principal_paid",
			"fieldtype": "Currency",
			"width": 120
		},
		{
			"label": _("Total Interest Payable"),
			"fieldname": "total_interest_payable",
			"fieldtype": "Currency",
			"width": 120
		},
		{
			"label": _("Total Interest Paid"),
			"fieldname": "total_interest_paid",
			"fieldtype": "Currency",
			"width": 120
		},
		{
			"label": _("Total Payable Amount"),
			"fieldname": "total_payment",
			"fieldtype": "Currency",
			"width": 120
		},
		{
			"label": _("Total Amount Paid"),
			"fieldname": "total_amount_paid",
			"fieldtype": "Currency",
			"width": 120
		}

	]

def get_data(filters, columns):
	item_price_qty_data = []
	item_price_qty_data = get_item_price_qty_data(filters)
	return item_price_qty_data

def get_item_price_qty_data(filters):
	conditions = ""
	if filters.get("from_date"):
		conditions += " and a.posting_date>=%(from_date)s"
	if filters.get("to_date"):
		conditions += " and a.posting_date<=%(to_date)s"
	if filters.get("loan_type"):
		conditions += " and a.loan_type=%(loan_type)s"
	item_results = frappe.db.sql("""
				select
					a.name as name,
					a.loan_type as loan_type,
					a.bank_account as bank_account,
					a.status as status,
					a.loan_amount as loan_amount,
					a.posting_date as posting_date,
					a.repayment_start_date as repayment_start_date,
					a.payment_frequency as payment_frequency,
					a.repayment_amount as repayment_amount,
					a.rate_of_interest as rate_of_interest,
					a.administrative_expenses_amount as administrative_expenses_amount,
					a.total_principal_payable as total_principal_payable,
					a.total_principal_paid as total_principal_paid,
					a.total_interest_payable as total_interest_payable,
					a.total_interest_paid as total_interest_paid,
					a.total_payment as total_payment,
					CONCAT_WS(" / ",a.no_of_repayments,  (select count(name) from `tabBank Loan Repayment Schedule` b where b.parent = a.name and b.is_paid =1)) as periods_paid,
					a.total_amount_paid as total_amount_paid
					from `tabBank Loan` a
				where
					docstatus =1
					{conditions}
		"""
		.format(conditions=conditions), filters, as_dict=1)

	#price_list_names = list(set([item.price_list_name for item in item_results]))

	#buying_price_map = get_price_map(price_list_names, buying=1)
	#selling_price_map = get_price_map(price_list_names, selling=1)

	result = []
	if item_results:
		for item_dict in item_results:
			data = {
				'name': item_dict.name,
				'loan_type': item_dict.loan_type,
				'bank_account': item_dict.bank_account,
				'status': item_dict.status,
				'loan_amount': item_dict.loan_amount,
				'posting_date': item_dict.posting_date,
				'repayment_start_date': item_dict.repayment_start_date,
				'payment_frequency': item_dict.payment_frequency,
				'repayment_amount': item_dict.repayment_amount,
				'rate_of_interest': item_dict.rate_of_interest,
				'administrative_expenses_amount': item_dict.administrative_expenses_amount,
				'total_principal_payable': item_dict.total_principal_payable,
				'total_principal_paid': item_dict.total_principal_paid,
				'total_interest_payable': item_dict.total_interest_payable,
				'total_interest_paid': item_dict.total_interest_paid,
				'total_payment': item_dict.total_payment,
				'periods_paid': item_dict.periods_paid,
				'total_amount_paid': item_dict.total_amount_paid,
			}
			result.append(data)

	return result

def get_price_map(price_list_names, buying=0, selling=0):
	price_map = {}

	if not price_list_names:
		return price_map

	rate_key = "Buying Rate" if buying else "Selling Rate"
	price_list_key = "Buying Price List" if buying else "Selling Price List"

	filters = {"name": ("in", price_list_names)}
	if buying:
		filters["buying"] = 1
	else:
		filters["selling"] = 1

	pricing_details = frappe.get_all("Item Price",
		fields = ["name", "price_list", "price_list_rate"], filters=filters)

	for d in pricing_details:
		name = d["name"]
		price_map[name] = {
			price_list_key :d["price_list"],
			rate_key :d["price_list_rate"]
		}

	return price_map


import hashlib
import random
import sqlite3
from typing import List, Optional

import more_itertools
import numpy as np
import pandas as pd
import scipy.spatial
import skimage.transform

from carla_real_traffic_scenarios import DT
from carla_real_traffic_scenarios.ngsim import DatasetMode
from carla_real_traffic_scenarios.opendd.dataset import OpenDDDataset, Place
from carla_real_traffic_scenarios.utils.carla import RealTrafficVehicle, find_best_matching_model
from carla_real_traffic_scenarios.utils.transforms import Transform, Vector3, Vector2

def extract_utm_trajectory_from_df(df) -> List[Transform]:
    trajectory = df[['UTM_X', 'UTM_Y', 'UTM_ANGLE']].values
    return [Transform(Vector3(x, y, 0), Vector2(np.cos(angle), np.sin(angle))) for x, y, angle in trajectory]

class Utm2CarlaMapper:

    def __init__(self, place: Place):
        image_middle = np.array(place.image_size) // 2
        pix2utm_transform = skimage.transform.AffineTransform(np.array(
            [[place.world_params[0], place.world_params[2], place.world_params[4]],
             [place.world_params[1], place.world_params[3], place.world_params[5]],
             [0, 0, 1]]))
        self.pix2utm_transformer = pix2utm_transform

        utm2pix_transform = skimage.transform.AffineTransform(pix2utm_transform._inv_matrix)
        self.utm2pix_transformer = utm2pix_transform

        map_center_utm = np.array(place.map_center_utm.as_numpy()[:2])
        reflect_matrix = np.array([[1, 0, 0], [0, -1, 0], [0, 0, 1]], dtype='float32')  # reflect over Y axis
        self.utm2carla_transformer = skimage.transform.AffineTransform(translation=-map_center_utm) + \
                                     skimage.transform.AffineTransform(matrix=reflect_matrix) + \
                                     place.correction_transform

    def utm2pix(self, transform: Transform):
        return self._transform_with_convert(transform, self.utm2pix_transformer)

    def pix2utm(self, transform: Transform):
        return self._transform_with_convert(transform, self.pix2utm_transformer)

    def utm2carla(self, transform: Transform):
        return self._transform_with_convert(transform, self.utm2carla_transformer)

    def _transform_with_convert(self, transform: Transform, transformer: skimage.transform.AffineTransform):
        position = transform.position.as_numpy()[:2]
        position = position.reshape(-1, 2)
        orientation = transform.orientation.as_numpy()
        orientation = orientation.reshape(-1, 2)

        position, orientation = self.transform(position, orientation, transformer)

        position = Vector2.from_numpy(position.squeeze()).to_vector3(0)
        orientation = Vector2.from_numpy(orientation.squeeze())
        return Transform(position, orientation)

    def transform(self, positions: np.ndarray, orientations: np.ndarray,
                  transformer: skimage.transform.AffineTransform):
        orientations = positions + orientations
        positions = transformer(positions)
        orientations = transformer(orientations)
        orientations = orientations - positions
        return positions, orientations

class OpenDDVehicle:

    def __init__(self, df, transformer) -> None:
        self._df = df

        self.id = int(df.OBJID.iloc[0])
        self.width_m = float(df.WIDTH.iloc[0])
        self.length_m = float(df.LENGTH.iloc[0])
        self._model = find_best_matching_model(self.width_m, self.length_m)

        self._frame = 0
        self._transformer = transformer

        self._max_frame = len(df)
        self.trajectory_utm = extract_utm_trajectory_from_df(self._df)
        self.trajectory_carla = self._map_trajectory_to_carla(self.trajectory_utm)

    def set_end_of_trajectory_timestamp(self, timestamp_end_s):
        df = self._df
        df = df[df.TIMESTAMP < timestamp_end_s]
        self._max_frame = len(df)
        self.trajectory_utm = extract_utm_trajectory_from_df(df)
        self.trajectory_carla = self._map_trajectory_to_carla(self.trajectory_utm)
        self._df = df

    def step(self):
        self._frame += 1

    @property
    def type_id(self):
        return self._model.type_id

    @property
    def speed_mps(self):
        return self._df.V.iloc[self._frame]

    @property
    def velocity(self):
        return (self.transform_carla.orientation * self.speed_mps).to_vector3(0)

    @property
    def transform_utm(self):
        return self.trajectory_utm[self._frame]

    @property
    def transform_carla(self):
        return self.trajectory_carla[self._frame]

    @property
    def has_finished(self) -> bool:
        return self._frame >= self._max_frame - 1

    def as_real_traffic_car(self):
        timestamp = self._df.TIMESTAMP.iloc[self._frame]
        debug_string = f'id={self.id} fm={self._frame} ts={timestamp:0.2f}'
        return RealTrafficVehicle(self.id, self.type_id, timestamp,
                                  self.width_m, self.length_m, self.transform_carla,
                                  self.speed_mps,
                                  debug_string)

    def _map_trajectory_to_carla(self, trajectory_utm) -> List[Transform]:
        trajectory_carla = []
        for transform_utm in trajectory_utm:
            transform_carla = self._transformer.utm2carla(transform_utm)
            transform_carla = \
                Transform(transform_carla.position.with_z(self._model.z_offset), transform_carla.orientation)
            trajectory_carla.append(transform_carla)
        return trajectory_carla

MIN_EPISODE_LENGTH_STEPS = 10 / DT

def _resample_df(df, target_timedelta_s):
    # create timedelta index from TIMESTAMP column (pd.Grouper uses it)
    df = df.set_index(pd.TimedeltaIndex(df.TIMESTAMP, 's'))
    # group by OBJID and resample TimedeltaIndex to target fps
    freq_ms = int(target_timedelta_s * 1000)
    grouper = df.groupby([pd.Grouper(freq=f'{freq_ms}ms'), 'OBJID'])
    df = grouper.first()  # take last observation from grouped bins
    df = df.reset_index(level=['OBJID'])  # recover OBJID column
    df['TIMESTAMP'] = df.index.to_series().dt.total_seconds()
    return df

def _find_ego_vehicle_with_time_frame(place, session_df, ego_id=None):
    all_objids = list(set(session_df.OBJID.to_list()))
    explicit_ego_id = ego_id is not None
    while True:
        ego_id = ego_id if explicit_ego_id else random.choice(all_objids)
        obj_df = session_df[session_df.OBJID == ego_id]
        start_idx, stop_idx = _trim_trajectory_utm_to_entry_end_exit(place, obj_df)
        if not explicit_ego_id and (start_idx is None or stop_idx is None or start_idx >= stop_idx):
            continue

        timestamp_start_s = obj_df.iloc[start_idx].TIMESTAMP if start_idx is not None else None
        timestamp_end_s = obj_df.iloc[stop_idx].TIMESTAMP if stop_idx is not None else None
        return ego_id, timestamp_start_s, timestamp_end_s

def _trim_trajectory_utm_to_entry_end_exit(place, obj_df):
    exits_utm = np.array([exit.as_numpy() if exit else np.zeros(2) for entry, exit in place.roads_utm])
    entries_utm = np.array([entry.as_numpy() if exit else np.zeros(2) for entry, exit in place.roads_utm])
    trajectory_utm = obj_df[['UTM_X', 'UTM_Y']].values

    dm_entries = scipy.spatial.distance_matrix(entries_utm, trajectory_utm)
    entries_distances_m = np.min(dm_entries, axis=1)
    nearest_entry_idx = np.argmin(entries_distances_m)  # idx of nearest entry
    # trajectory idx where vehicle pass nearest roundabout entry
    trajectory_start_idx = np.argmin(dm_entries[nearest_entry_idx])
    min_distance_from_nearest_entry = dm_entries[nearest_entry_idx][trajectory_start_idx]

    MAX_DISTANCE_FROM_WP_M = 2
    PRE_ENTRY_DISTANCE_M = 20
    # ensure that it passes entry not more than MAX_DISTANCE_FROM_WP_M
    if min_distance_from_nearest_entry > MAX_DISTANCE_FROM_WP_M:
        trajectory_start_idx = None
    elif trajectory_start_idx > 0:
        # take 1st index from part of trajectory distanced not more than PRE_ENTRY_DISTANCE_M
        trajectory_start_idx = np.where(
            dm_entries[nearest_entry_idx][:trajectory_start_idx] < PRE_ENTRY_DISTANCE_M
        )[0][0]

    dm_exits = scipy.spatial.distance_matrix(exits_utm, trajectory_utm)
    exit_distances_m = np.min(dm_exits, axis=1)
    nearest_exit_idx = np.argmin(exit_distances_m)
    trajectory_end_idx = np.argmin(dm_exits[nearest_exit_idx])
    min_distance_from_nearest_exit = dm_exits[nearest_exit_idx][trajectory_end_idx]
    # ensure that it passes exit not more than MAX_DISTANCE_FROM_WP_M
    if min_distance_from_nearest_exit > MAX_DISTANCE_FROM_WP_M:
        trajectory_end_idx = None
    else:
        trajectory_end_idx = trajectory_end_idx + np.where(
            dm_exits[nearest_exit_idx][trajectory_end_idx:] < PRE_ENTRY_DISTANCE_M
        )[0][-1]

    return trajectory_start_idx, trajectory_end_idx

def _determine_split(session_name, ego_id, start, stop) -> DatasetMode:
    split_frac = 0.8
    start, stop = int(round(start, 0)), int(round(stop, 0))
    hash_num = int(hashlib.sha1(f'{session_name},{ego_id},{start},{stop}'.encode('utf-8')).hexdigest(), 16)
    if (hash_num % 100) / 100 < split_frac:
        return DatasetMode.TRAIN
    else:
        return DatasetMode.VALIDATION

class OpenDDRecording():

    def __init__(self, *, dataset: OpenDDDataset, timedelta_s: float = DT,
                 dataset_mode: DatasetMode = DatasetMode.TRAIN) -> None:
        self._dataset = dataset
        self._dataset_mode = dataset_mode
        self._env_vehicles = {}
        self._df: Optional[pd.DataFrame] = None
        self._frame = 0
        self._timedelta_s = timedelta_s
        self._timestamps = []
        self._session_name: Optional[str] = None

        self._transformer: Optional[Utm2CarlaMapper] = None

    def reset(self, session_name, seed=None):
        if self._df is not None:
            del self._df

        self._session_name = session_name
        with sqlite3.connect(self._dataset.db_path) as conn:
            df = pd.read_sql(f'select * from {session_name}', conn)
            # for now do not extract pedestrians, bicycles and trailers
            df = df[~df.CLASS.str.contains('Pedestrian|Bicycle|Trailer')]
            df = _resample_df(df, self._timedelta_s)
            self._timestamps = np.arange(df.TIMESTAMP.min(),
                                         df.TIMESTAMP.max() + self._timedelta_s,
                                         self._timedelta_s)
            self._df = df

        # search for train/validation roundabout pass
        dataset_mode = None
        if seed is not None:
            random.seed(seed)
        while dataset_mode != self._dataset_mode:
            ego_id, timestamp_start_s, timestamp_end_s = _find_ego_vehicle_with_time_frame(self.place, self._df)
            dataset_mode = _determine_split(session_name, ego_id, timestamp_start_s, timestamp_end_s)

        self._frame = np.where(np.isclose(self._timestamps, timestamp_start_s, 0.0001))[0][0] + 1
        self._env_vehicles = {}

        self._transformer = Utm2CarlaMapper(self.place)
        return ego_id, timestamp_start_s, timestamp_end_s

    def step(self) -> List[RealTrafficVehicle]:
        timestamp_s = self._timestamps[self._frame]
        vehicles_current_ids = self._df[
            np.isclose(self._df.TIMESTAMP, timestamp_s)
        ].OBJID.to_list()

        for vehicle_id in vehicles_current_ids:
            if vehicle_id not in self._env_vehicles:
                # TODO: check if x/y smoothing is not required (in ngsim dataset there is smoothing in 15 frames wnd)
                new_vehicle_df = self._df[
                    (self._df.OBJID == vehicle_id) &
                    ((self._df.TIMESTAMP >= timestamp_s) | np.isclose(self._df.TIMESTAMP, timestamp_s))
                ]
                self._env_vehicles[vehicle_id] = OpenDDVehicle(new_vehicle_df, self._transformer)

        self._env_vehicles = {k: v for k, v in self._env_vehicles.items() if not v.has_finished}

        real_traffic_vehicles = [v.as_real_traffic_car() for v in self._env_vehicles.values()]
        if real_traffic_vehicles:
            if len(real_traffic_vehicles) > 1:
                assert all([
                    np.isclose(v1.timestamp_s, v2.timestamp_s)
                    for v1, v2 in more_itertools.windowed(real_traffic_vehicles, 2)
                ]), (
                    self._session_name,
                    [v.debug for v in real_traffic_vehicles],
                )
            assert np.isclose(real_traffic_vehicles[0].timestamp_s, timestamp_s), \
                (real_traffic_vehicles[0].timestamp_s, timestamp_s)

        self._frame += 1
        for v in self._env_vehicles.values():
            v.step()

        return real_traffic_vehicles

    def close(self):
        pass

    @property
    def place(self) -> Place:
        place_name = self._session_name.split('_')[0]
        return self._dataset.places[place_name]

    @property
    def session_name(self) -> str:
        return self._session_name

    @property
    def timestamp_s(self) -> float:
        return self._timestamps[self._frame]

    @property
    def transformer(self):
        return self._transformer

    def get_df_by_objid(self, ego_id):
        return self._df[self._df.OBJID == ego_id]

    @property
    def has_finished(self):
        return self._frame >= len(self._timestamps) - 1


from django.http import JsonResponse
from .models import Task

def _get_all_tasks():
    task_objects = Task.objects.all()[:30]
    tasks = []
    for task_obj in task_objects:
        task = task_obj.get_as_dict()
        tasks.append(task)
    return tasks

def index(request):
    if request.method == 'GET':
        return get_all(request)
    elif request.method == 'POST':
        code = 200
        error = None
        task_for_response = None

        task_description = request.POST.get('task[description]')
        task_category = request.POST.get('task[category]')
        task_date_added = request.POST.get('task[dateAdded]')
        if (task_description is None) or (task_category is None)\
                or (task_date_added is None):
            code = 400
            error = 'No task data submitted'
        else:
            task = Task()
            task.description = task_description
            task.category = task_category
            task.date_added = task_date_added
            task.save()
            task_for_response = task.get_as_dict()

        response = {
            'task': task_for_response,
            'code': code,
            'error': error
        }
        return JsonResponse(response)

def get_all(request):
    tasks = _get_all_tasks()
    response = {
        "tasks": tasks,
        "code": 200
    }
    return JsonResponse(response)

def update_task(request, task_id):
    code = 200
    error = None
    task_for_response = None

    task = Task.objects.get(id=task_id)
    if task is None:
        code = 404
        error = f'No task found for id {task_id}'
    else:
        task_description = request.POST.get('task[description]')
        task_category = request.POST.get('task[category]')
        task_completed = request.POST.get('task[completed]')
        if (task_description is None) or (task_category is None)\
                or (task_completed is None):
            code = 400
            error = 'No task data submitted'
        else:
            task.description = task_description
            task.category = task_category
            task.completed = task_completed in ('true', 'True', '1')
            task.save()
            task_for_response = task.get_as_dict()

    response = {
        'task': task_for_response,
        'code': code,
        'error': error
    }
    return JsonResponse(response)

def delete_completed(request):
    Task.objects.filter(completed=True).delete()
    return get_all(request)


import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import matplotlib.pyplot as plt

from keras.models import Sequential
from keras.layers import Dense , Dropout , Lambda, Flatten
from keras.optimizers import Adam ,RMSprop
from sklearn.model_selection import train_test_split
from sklearn import cross_validation
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator

from keras.utils.np_utils import to_categorical

from keras.models import  Sequential
from keras.layers.core import  Lambda , Dense, Flatten, Dropout
from keras.callbacks import EarlyStopping
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D

from keras.optimizers import RMSprop
from keras.preprocessing import image

import tensorflow as tf

##Reading Data

def contrast(x):
    if float(x)>205:
        return 1
    elif float(x)<50:
        return 0
    else:
        return float(x)/255

train = pd.read_csv('I:\\Centrale\\Machine Learning\\Kaggle-3-MNIST\\train.csv', converters = dict([(i+1, contrast) for i in range(28*28)]))
#train = pd.read_csv('I:\\Centrale\\Machine Learning\\Kaggle-3-MNIST\\train.csv')
print(train.shape)
train.head()

test= pd.read_csv('I:\\Centrale\\Machine Learning\\Kaggle-3-MNIST\\test.csv', converters = dict([(i, contrast) for i in range(28*28)]))
#test= pd.read_csv('I:\\Centrale\\Machine Learning\\Kaggle-3-MNIST\\test.csv')
print(test.shape)
test.head()

##Data Preprocessing

X_train = (train.iloc[:,1:].values).astype('float32') # all pixel values
y_train = train.iloc[:,0].values.astype('int32') # only labels i.e targets digits
X_test = test.values.astype('float32')

##Display data repartition

"""repartition =[0 for i in range(10)]
for i in range(y_train.shape[0]):
    repartition[list(y_train[i,:]).index(max(y_train[i,:]))]+=1
sections=[i for i in range(10)]
plt.bar(sections, repartition, align='center', alpha=0.7)
plt.show()"""

##Data augmentation

def convert_2d(x):
    """x: 2d numpy array. m*n data image.
       return a 3d image data. m * height * width * channel."""
    if len(x.shape) == 1:
        m = 1
        height = width = int(np.sqrt(x.shape[0]))
    else:
        m = x.shape[0]
        height = width = int(np.sqrt(x.shape[1]))

    x_2d = np.reshape(x, (m, height, width, 1))

    return x_2d

def crop_image(x, y, min_scale):
    """x: 2d(m*n) numpy array. 1-dimension image data;
       y: 1d numpy array. The ground truth label;
       min_scale: float. The minimum scale for cropping.
       return zoomed images.
       This function crops the image, enlarges the cropped part and uses it as augmented data."""
    # convert the data to 2-d image. images should be a m*h*w*c numpy array.
    images = convert_2d(x)
    # m is the number of images. Since this is a gray-scale image scale from 0 to 255, it only has one channel.
    m, height, width, channel = images.shape

    # tf tensor for original images
    img_tensor = tf.placeholder(tf.int32, [1, height, width, channel])
    # tf tensor for 4 coordinates for corners of the cropped image
    box_tensor = tf.placeholder(tf.float32, [1, 4])
    box_idx = [0]
    crop_size = np.array([height, width])
    # crop and resize the image tensor
    cropped_img_tensor = tf.image.crop_and_resize(img_tensor, box_tensor, box_idx, crop_size)
    # numpy array for the cropped image
    cropped_img = np.zeros((m, height, width, 1))

    with tf.Session() as sess:

        for i in range(m):

            # randomly select a scale between [min_scale, min(min_scale + 0.05, 1)]
            rand_scale = np.random.randint(min_scale * 100, np.minimum(min_scale * 100 + 5, 100)) / 100
            # calculate the 4 coordinates
            x1 = y1 = 0.5 - 0.5 * rand_scale
            x2 = y2 = 0.5 + 0.5 * rand_scale
            # lay down the cropping area
            box = np.reshape(np.array([y1, x1, y2, x2]), (1, 4))
            # save the cropped image
            cropped_img[i:i + 1, :, :, :] = sess.run(cropped_img_tensor, feed_dict={img_tensor: images[i:i + 1], box_tensor: box})

    # flat the 2d image
    cropped_img = np.reshape(cropped_img, (m, -1))

    return cropped_img

def translate(x, y, dist):
    """x: 2d(m*n) numpy array. 1-dimension image data;
       y: 1d numpy array. The ground truth label;
       dist: float. Percentage of height/width to shift.
       return translated images.
       This function shift the image to 4 different directions.
       Crop a part of the image, shift it and fill the left part with 0."""
    # convert the 1d image data to a m*h*w*c array
    images = convert_2d(x)
    m, height, width, channel = images.shape

    # set 4 groups of anchors. The first 4 int in a certain group lay down the area we crop.
    # The last 4 sets the area to be moved to. E.g.,
    # new_img[new_top:new_bottom, new_left:new_right] = img[top:bottom, left:right]
    anchors = []
    anchors.append((0, height, int(dist * width), width, 0, height, 0, width - int(dist * width)))
    anchors.append((0, height, 0, width - int(dist * width), 0, height, int(dist * width), width))
    anchors.append((int(dist * height), height, 0, width, 0, height - int(dist * height), 0, width))
    anchors.append((0, height - int(dist * height), 0, width, int(dist * height), height, 0, width))

    # new_images: d*m*h*w*c array. The first dimension is the 4 directions.
    new_images = np.zeros((4, m, height, width, channel))
    for i in range(4):
        # shift the image
        top, bottom, left, right, new_top, new_bottom, new_left, new_right = anchors[i]
        new_images[i, :, new_top:new_bottom, new_left:new_right, :] = images[:, top:bottom, left:right, :]

    new_images = np.reshape(new_images, (4 * m, -1))
    y = np.tile(y, (4, 1)).reshape((-1, 1))

    return new_images

def add_noise(x, y, noise_lvl):
    """x: 2d(m*n) numpy array. 1-dimension image data;
       y: 1d numpy array. The ground truth label;
       noise_lvl: float. Percentage of pixels to add noise in.
       return images with white noise.
       This function randomly picks some pixels and replace them with noise."""
    m, n = x.shape
    # calculate the # of pixels to add noise in
    noise_num = int(noise_lvl * n)

    for i in range(m):
        # generate n random numbers, sort it and choose the first noise_num indices
        # which equals to generate random numbers w/o replacement
        noise_idx = np.random.randint(0, n, n).argsort()[:noise_num]
        # replace the chosen pixels with noise from 0 to 255
        x[i, noise_idx] = np.random.randint(0, 255, noise_num)

    noisy_data = x.astype("int")

    return noisy_data

def rotate_image(x, y, max_angle):
    """x: 2d(m*n) numpy array. 1-dimension image data;
       y: 1d numpy array. The ground truth label;
       max_angle: int. The maximum degree for rotation.
       return rotated images.
       This function rotates the image for some random degrees(0.5 to 1 * max_angle degree)."""
    images = convert_2d(x)
    m, height, width, channel = images.shape

    img_tensor = tf.placeholder(tf.float32, [m, height, width, channel])

    # half of the images are rotated clockwise. The other half counter-clockwise
    # positive angle: [max/2, max]
    # negative angle: [360-max/2, 360-max]
    rand_angle_pos = np.random.randint(max_angle / 2, max_angle, int(m / 2))
    rand_angle_neg = np.random.randint(-max_angle, -max_angle / 2, m - int(m / 2)) + 360
    rand_angle = np.transpose(np.hstack((rand_angle_pos, rand_angle_neg)))
    np.random.shuffle(rand_angle)
    # convert the degree to radian
    rand_angle = rand_angle / 180 * np.pi

    # rotate the images
    rotated_img_tensor = tf.contrib.image.rotate(img_tensor, rand_angle)

    with tf.Session() as sess:
        rotated_imgs = sess.run(rotated_img_tensor, feed_dict={img_tensor: images})

    rotated_imgs = np.reshape(rotated_imgs, (m, -1))

    return rotated_imgs

print("Augment the data...")
cropped_imgs = crop_image(X_train, y_train, 0.9)
translated_imgs = translate(X_train, y_train, 0.1)
noisy_imgs = add_noise(X_train, y_train, 0.1)
rotated_imgs = rotate_image(X_train, y_train, 10)

augmented = np.vstack((X_train, cropped_imgs, translated_imgs, noisy_imgs, rotated_imgs))
#augmented is the new data composed with : the basic dataset, the cropped dataset, the shifted dataset (1 for each 4 directions), the noisy dataset and the rotated dataset

print("Done!", X_train.shape)

##Save

header = 'label'
for i in range(784):
    header+=',pixel'+str(i)

np.savetxt('C:\\Users\\Nicolas\\Desktop\\augmented_data.csv', np.array([[int(y_train[i%X_train.shape[0]])]+list(augmented[i,:]) for i in range(augmented.shape[0])]),fmt='%i', delimiter = ',',newline='\n', header = header, comments='')

##Save2
pd.DataFrame(augmented).to_csv('I:\\Centrale\\Machine Learning\\Kaggle-3-MNIST\\augmented_data.csv', index = False)


from flask import Flask
from flask import Flask, request, render_template, send_file

app = Flask(__name__)

@app.route('/cookiestealer/', methods=['GET'])
def cookieStealer():
    filename = 'cookiemonster.jpg'
    print("This is the cookie: \n")
    print(request.cookies)
    print("")

    return send_file(filename, mimetype='image/jpeg')

if __name__ == '__main__':

    app.run(port=3100, debug=True)


#!/usr/bin/env python
from __future__ import absolute_import

import apache_beam as beam
import argparse
import json
import logging
import sys
import urllib

from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.options.pipeline_options import SetupOptions
from google.cloud import bigquery
from google.cloud import storage
from raxxla_transforms import data_transforms
from raxxla_transforms import table_schemas

# Global vars
logger = logging.getLogger('raxxla_loader')
formatter = '%(asctime)s     %(message)s'
logging.basicConfig(level=logging.INFO, format=formatter)
table_list = ['bodies', 'systems', 'powerplay', 'population', 'stations']
arg_list = table_list + ['all']
runner_list = ['DataflowRunner', 'DirectRunner']

# Configure flags
flags = argparse.ArgumentParser(description='Initialize Raxxla BigQuery tables from EDSM data.')
flags.add_argument('--project', help='ID of the Google Cloud project to use.')
flags.add_argument('--dataset', help='Name of the BigQuery dataset to store EDSM data in.')
flags.add_argument('--bucket', help='Name of the GCS bucket to store EDSM data in.')
flags.add_argument('--runner', help='Name of the Beam runner type to use for the pipeline.', choices=runner_list)
flags.add_argument('--upload_to_gcs', help='Upload EDSM files to GCS from local download.', choices=arg_list, nargs='+')
flags.add_argument('--delete', help='Delete tables from BigQuery.', choices=arg_list, nargs='+')
flags.add_argument('--download', help='Download files from EDSM into Google Cloud Storage.', choices=arg_list, nargs='+')
flags.add_argument('--upload_to_bq', help='Write converted values to BigQuery.  Requires files to be staged in GCS.', choices=arg_list, nargs='+')
args = flags.parse_args()

class remove_blank_rows(beam.DoFn):
    def process(self, element):
        if element is not None:
            yield element
        else:
            return

def delete_bq_data(tables, project_id, dataset_id):
    client = bigquery.Client()
    dataset_ref = client.dataset(dataset_id)
    tables = set(tables)
    try:
        for table in tables:
            table_ref = dataset_ref.table(table)
            delete_string = 'Deleted ' + project_id + '.' + dataset_id + '.' + table
            client.delete_table(table_ref)
            logger.info(delete_string)
    except Exception as e:
        delete_error_string = 'Unable to delete EDSM BQ tables: ' + str(e)
        logger.error(delete_error_string)
        sys.exit()

def download_edsm_files(files):
    edsm_urls = {
        'bodies': 'https://www.edsm.net/dump/bodies.json',
        'systems': 'https://www.edsm.net/dump/systemsWithCoordinates.json',
        'powerplay': 'https://www.edsm.net/dump/powerPlay.json',
        'population': 'https://www.edsm.net/dump/systemsPopulated.json',
        'stations': 'https://www.edsm.net/dump/stations.json'
    }

    files = set(files)
    try:
        for file in files:
            dl_string = 'Downloading ' + file + ' file from EDSM...'
            logger.info(dl_string)
            download_url = edsm_urls[file]
            download_path = '/tmp/' + file
            urllib.urlretrieve(download_url, download_path)

    except Exception as e:
        download_error_string = 'Unable to download EDSM files: ' + str(e)
        logger.error(download_error_string)
        sys.exit()

def upload_to_bigquery(files, project_id, dataset_id, bucket_id, runner, pipeline_options):
    files = set(files)

    try:
        for file in files:
            import_string = 'Importing ' + file + ' file into BigQuery...'
            logger.info(import_string)

            table_spec = project_id + ':' + dataset_id + '.' + str(file)
            if runner == 'DataflowRunner':
                file_path = 'gs://' + str(bucket_id) + '/' + str(file)
            elif runner == 'DirectRunner':
                file_path = '/tmp/' + str(file)

            with beam.Pipeline(options=pipeline_options) as p:
                json_lines = p | beam.io.ReadFromText(file_path)

                if file == 'bodies':
                    schema = table_schemas.bodies
                    rows = json_lines | beam.Map(data_transforms.transform_bodies)
                elif file == 'systems':
                    schema = table_schemas.systems
                    rows = json_lines | beam.Map(data_transforms.transform_systems)
                elif file == 'powerplay':
                    schema = table_schemas.powerplay
                    rows = json_lines | beam.Map(data_transforms.transform_powerplay)
                elif file == 'population':
                    schema = table_schemas.population
                    rows = json_lines | beam.Map(data_transforms.transform_population)
                elif file == 'stations':
                    schema = table_schemas.stations
                    rows = json_lines | beam.Map(data_transforms.transform_stations)

                if schema and rows:
                    bq_loader = rows | beam.ParDo(remove_blank_rows()) | beam.io.WriteToBigQuery(
                        table_spec,
                        schema=schema,
                        write_disposition=beam.io.BigQueryDisposition.WRITE_EMPTY,
                        create_disposition=beam.io.BigQueryDisposition.CREATE_IF_NEEDED,
                        method='DEFAULT',
                        batch_size=500)
                else:
                    raise Exception('Unable to assemble rows for upload.  Check upstream repo for schema updates.')

    except Exception as e:
        bq_upload_error_string = 'Unable to load EDSM files: ' + str(e)
        logger.error(bq_upload_error_string)
        sys.exit()

def upload_to_gcs(files, project_id, bucket):
    try:
        for file in files:
            blob = bucket.blob(file)
            file_path = '/tmp/' + str(file)
            upload_log_string = 'Uploading ' + file + ' to GCS.'
            logger.info(upload_log_string)
            blob.upload_from_filename(file_path)
    except Exception as e:
        gcs_upload_error_string = 'Unable to upload EDSM files to GCS: ' + str(e)
        logger.error(gcs_upload_error_string)
        sys.exit()

def main(argv=None):
    parser = argparse.ArgumentParser()
    known_args, pipeline_args = parser.parse_known_args(argv)
    project_id = args.project
    dataset_id = args.dataset
    bucket_id = args.bucket

    if args.delete:
        if 'all' in args.delete:
            delete_bq_data(table_list, project_id, dataset_id)
        else:
            delete_bq_data(args.delete, project_id, dataset_id)

    if args.download:
        if 'all' in args.download:
            download_edsm_files(table_list)
        else:
            download_edsm_files(args.download)

    if args.upload_to_gcs:
        storage_client = storage.Client()

        try:
            gcs_bucket = storage_client.get_bucket(bucket_id)
        except Exception as e:
            logger.warning('GCS bucket not found, creating...')
            gcs_bucket = storage_client.create_bucket(bucket_id)

        if 'all' in args.upload_to_gcs:
            upload_to_gcs(table_list, project_id, gcs_bucket)
        else:
            upload_to_gcs(args.upload_to_gcs, project_id, gcs_bucket)

    if args.upload_to_bq:
        staging_location = '--staging_location=gs://' + bucket_id + '/staging'
        temp_location = '--temp_location=gs://' + bucket_id + '/temp'

        pipeline_args.extend([
            '--job_name=raxxla-loader',
            '--setup_file=./setup.py',
            staging_location,
            temp_location,
        ])

        pipeline_options = PipelineOptions(pipeline_args)
        pipeline_options.view_as(SetupOptions).save_main_session = True
        runner = args.runner

        if 'all' in args.upload_to_bq:
            upload_to_bigquery(table_list, project_id, dataset_id, bucket_id, runner, pipeline_options)
        else:
            upload_to_bigquery(args.upload_to_bq, project_id, dataset_id, bucket_id, runner, pipeline_options)

if __name__ == '__main__':
  main()


from buildbot.worker import Worker
from maxscale.config.workers import WORKER_CREDENTIALS

def workerConfiguration():
    """Create worker configuration for use in BuildBot configuration"""
    configuration = []
    for credentials in WORKER_CREDENTIALS:
        configuration.append(Worker(credentials["name"], credentials["password"]))
    return configuration

def workerNames(host=""):
    """Create a list of worker names that can be used in build configuration"""
    workers = []
    for credentials in WORKER_CREDENTIALS:
        if host in credentials["host"]:
            workers.append(credentials["name"])
    return workers

def workersOnHosts(*hosts):
    """
    Create a list of worker names that run on the specified hosts
    hosts (list): names of the host names to use
    """
    workers = []
    for credentials in WORKER_CREDENTIALS:
        if credentials["host"] in hosts:
            workers.append(credentials["name"])
    return workers

def workerToHostMap():
    """Creates dictionary with worker name mapped to host"""
    workerToHost = {}
    for credentials in WORKER_CREDENTIALS:
        workerToHost[credentials["name"]] = credentials["host"]
    return workerToHost