'''
Autonomous snake which should differentiate between pies and posions
Reinforcement learning with Q-Learn algorithm using mainly Keras as library

This file will give an array of actions, which the snake execute autonomously.

After research the best algorithm to use, seems to be SARSA, based on
http://cs229.stanford.edu/proj2016spr/report/060.pdf
Most of the Code can be found from:
https://github.com/korolvs/snake_nn/blob/master/nn_1.py

Author: Claudio
'''

# dependencies
import SnakeGame
import config
import random
import numpy as np
import tflearn
import math
from tflearn.layers.core import input_data, fully_connected
from tflearn.layers.estimator import regression
from statistics import mean
from collections import Counter

class SnakeNN:
    # Constructor
    def __init__(self, initial_games = 100, test_games = 100, goal_steps = 100, learning_rate = 1e-2, filename = 'snake_nn.tflearn'):
        self.initial_games = initial_games
        self.test_games = test_games
        self.goal_steps = goal_steps
        self.learning_rate = learning_rate
        self.vector_actions = [
                            [[-1,0], 0],
                            [[0,1], 1],
                            [[1,0], 2],
                            [[0,-1], 3],
                            ]
        self.filename = filename

    # Do 100 Initial games with random steps
    def initial_population(self):
        training_data = []

        for _ in range(self.initial_games):
            game = SnakeGame.SnakeGame()
            _, _, snake, _, _ = game.start()
            previous_observation = self.generate_observation(snake)

            for _ in range(self.goal_steps):
                action, game_action = self.generate_action(snake)
                done, _, snake, _, _ = game.step(game_action)

                if done:
                    training_data.append([self.add_action_to_observation(previous_observation, action), 0])
                    break

                else:
                    training_data.append([self.add_action_to_observation(previous_observation, action), 1])
                    previous_observation = self.generate_observation(snake)

        print(len(training_data))
        return training_data

    # Generate an action for the snake
    def generate_action(self, snake):
        action = random.randint(0,2) - 1
        return action, self.get_game_action(snake, action)

    # Get the action for a game
    def get_game_action(self, snake, action):
        snake_direction = self.get_snake_direction_vector(snake)
        new_direction = snake_direction

        if action == -1:
            new_direction = self.turn_left(snake_direction)

        elif action == 1:
            new_direction = self.turn_right(snake_direction)

        for pair in self.vector_actions:
            if pair[0] == new_direction.tolist():
                game_action = pair[1]

        return game_action

    # Generating an observation
    def generate_observation(self, snake):
        snake_direction = self.get_snake_direction_vector(snake)
        barrier_left = self.direction_blocked(snake, self.turn_left(snake_direction))
        barrier_right = self.direction_blocked(snake, self.turn_right(snake_direction))
        barrier_front = self.direction_blocked(snake, snake_direction)
        return np.array([int(barrier_left), int(barrier_front), int(barrier_right)])

    # add an action to an observation
    def add_action_to_observation(self, observation, action):
        return np.append([action], observation)

    # Get the direction of the action of the snake
    def get_snake_direction_vector(self, snake):
        return np.array(snake[0]) - np.array(snake[1])

    # Boolean if the direction of the snake is blocked
    def direction_blocked(self, snake, direction):
        point = np.array(snake[0]) + np.array(direction)
        return point.tolist() in snake[:-1] or point[0] == 0 or point[1] == 0 or point[0] == config.screen_heigth+1 or point[1] == config.screen_width+1

    # Turn the snake to the right of its direction
    def turn_right(self, vector):
        return np.array([vector[1], -vector[0]])

    # Turn the snake to the left of its direction
    def turn_left(self, vector):
        return np.array([-vector[1], vector[0]])

    # Model the neural network with tensorflowb
    def model(self):
        network = input_data(shape=[None, 4, 1], name= 'input')
        network = fully_connected(network, 1, activation= 'linear')
        network = regression(network, optimizer= 'adam', learning_rate= self.learning_rate, loss= 'mean_square', name= 'target')
        model = tflearn.DNN(network, tensorboard_dir= 'log')
        return model

    # Train the neural network model
    def train_model(self, training_data, model):
        X = np.array([i[0] for i in training_data]).reshape(-1,4,1)
        y = np.array([i[1] for i in training_data]).reshape(-1,1)
        model.fit(X, y, n_epoch = 1, shuffle= True, run_id= self.filename)
        model.save(self.filename)
        return model

    # Visualize the game
    def visuluaize_game(self, model):
        game = SnakeGame.SnakeGame(gui= True)
        _, _, snake, _, _ = game.start()
        previous_observation = self.generate_observation(snake)

        for _ in range(self.goal_steps):
            predictions = []

            for action in range(-1,2):
                predictions.append(model.predict(self.add_action_to_observation(previous_observation, action).reshape(-1,4,1)))

            action = np.argmax(np.array(predictions))
            game_action = self.get_game_action(snake, action -1)
            done, _, snake, _, _ = game.step(game_action)

            if done:
                break
            else:
                previous_observation = self.generate_observation(snake)

    # Test the neural network model
    def test_model(self, model):
        steps_arr = []

        for _ in range(self.test_games):
            steps = 0
            game_memory = []
            game = SnakeGame.SnakeGame()
            _, _, snake, _, _ = game.start()
            previous_observation = self.generate_observation(snake)

            for _ in range(self.goal_steps):
                predictions = []
                for action in range(-1,2):
                    predictions.append(model.predict(self.add_action_to_observation(previous_observation,action).reshape(-1,4,1)))
                    action = np.argmax(np.array(predictions))
                    game_action = self.get_game_action(snake, action -1)
                    done, _, snake, _, _ = game.step(game_action)
                    game_memory.append([previous_observation, action])

                    if done:
                        break

                    else:
                        previous_observation = self.generate_observation(snake)
                        steps += 1
                steps_arr.append(steps)

            print('Average steps:',mean(steps_arr))
            print(Counter(steps_arr))

    # Train model
    def train(self):
        training_data = self.initial_population()
        nn_model = self.model()
        nn_model = self.train_model(training_data, nn_model)
        self.test_model(nn_model)

    # Visualize the game
    def visualize(self):
        nn_model = self.model()
        nn_model.load(self.filename)
        self.visuluaize_game(nn_model)

    # Test the model
    def test(self):
        nn_model = self.model()
        nn_model.load(self.filename)
        self.test_model(nn_model)

# Start the process from terminal
if __name__ == "__main__":
    #SnakeNN().train()
    #SnakeNN().visualize()
    SnakeNN().test()


import math

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

import vtk
from vtk.util.colors import tomato
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from vtkmodules.vtkFiltersSources import *
from vtkmodules.vtkRenderingCore import *

# Просто Pi
PI = math.pi

class MouseInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
    def __init__(self, parent=None):
        self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)

    def leftButtonPressEvent(self, obj, event):
        self.OnLeftButtonDown()

class Shape():

    def get_area(self):
        pass

    def get_perimeter(self):
        pass

    #Получение точек для отрисовки
    def get_mapper(self):
        pass

class Circle(Shape):

    def __init__(self, r=0):
        super().__init__()
        self.r = r

    def set_r(self, x):
        if x:
            self.r = float(x)

    def get_area(self):
        return PI*(self.r**2)

    def get_perimeter(self):
        return 2*PI*self.r

    #Получение точек для отрисовки
    def get_mapper(self):
        polygonSource = vtkRegularPolygonSource()
        polygonSource.GeneratePolygonOff()
        polygonSource.SetNumberOfSides(50)
        polygonSource.SetRadius(self.r)
        polygonSource.SetCenter(0.0, 0.0, 0.0)

        self.mapper = vtkPolyDataMapper()
        self.mapper.SetInputConnection(polygonSource.GetOutputPort())
        return self.mapper

class Sphere(Circle):

    def __init__(self):
        super().__init__()

    def get_area(self):
        return 4 * PI * self.r ** 2

    def get_volume(self):
        return 4/3 * PI * self.r ** 3

    #Получение точек для отрисовки
    def get_mapper(self):

        sphereSource = vtkSphereSource()
        sphereSource.SetCenter(0.0, 0.0, 0.0)
        sphereSource.SetRadius(self.r)
        sphereSource.SetPhiResolution(100)
        sphereSource.SetThetaResolution(100)

        self.mapper = vtkPolyDataMapper()
        self.mapper.SetInputConnection(sphereSource.GetOutputPort())

        return self.mapper

class Cylinder(Circle):
    def __init__(self, h=0.0):
        super().__init__()

        self.h = h

    def set_h(self, x):
        if x:
            self.h = float(x)

    def get_area(self):
        return 2 * PI * self.r * (self.h + self.r)

    def volume(self):
        return PI * self.r ** 2 * self.h

    #Получение точек для отрисовки
    def get_mapper(self):
        # source
        cylinder = vtk.vtkCylinderSource()
        cylinder.SetResolution(50)
        cylinder.SetRadius(self.r)
        cylinder.SetHeight(self.h)

        self.mapper = vtk.vtkPolyDataMapper()
        self.mapper.SetInputConnection(cylinder.GetOutputPort())
        return self.mapper

class Cone(Cylinder):
    def __init__(self):
        super().__init__()

    def get_area(self):
        return PI * self.r * (self.r + (self.r ** 2 + self.h * 2) ** 0.5)

    def volume(self):
        return 1/3 * PI * self.r ** 2 * self.h

    #Получение точек для отрисовки
    def get_mapper(self):

        source = vtk.vtkConeSource()
        source.SetResolution(100)
        source.SetRadius(self.r)
        source.SetHeight(self.h)

        self.mapper = vtk.vtkPolyDataMapper()
        self.mapper.SetInputConnection(source.GetOutputPort())
        return self.mapper

class Pyramid(Shape):
    # a - длина стороны
    def __init__(self,n=3, a=0, h=0):
        super().__init__()
        self.a = a
        self.h = h
        self.n = n
        self.r = 0.0

    def set_r(self):
        # по этой формуле рассматриваем r как бедро равнобедренного треугольника и через основание а и угол находим
        self.r = self.a / (2 * math.cos(math.radians((180 - 360 / self.n) / 2)))
    def set_a(self, x):
        if x:
            self.a = float(x)
            self.set_r()

    def set_h(self, x):
        if x:
            self.h = float(x)

    def set_n(self, x):
        if x:
            self.n = int(x)
            self.set_r()

    #Получение точек для отрисовки
    def get_mapper(self):

        source = vtk.vtkConeSource()
        source.SetResolution(self.n)
        source.SetRadius(self.r)
        source.SetHeight(self.h)

        self.mapper = vtk.vtkPolyDataMapper()
        self.mapper.SetInputConnection(source.GetOutputPort())
        return self.mapper

class Main_Window(QWidget):

    def __init__(self):
        super().__init__()

        # в атрибуте будет храниться фигура, с которой работают в данный момент (при старте программы это пустая фигура)
        self.shape = Shape()

        # инициализация главного UI
        self.UI()

        # Массив для временных кнопок, лейблов и других элементов интерфейса
        self.UI_elements = []

        self.validator = QIntValidator(1, 99, self)
    def UI(self):

        # кнопка отрисовки фигруы
        b_draw = QPushButton("Нарисовать фигуру", self)
        b_draw.clicked.connect(lambda:  self.draw_shape(self.shape))
        b_draw.move(0, 30)

        #выбор фигуры для работы
        cb_shape_type = QComboBox(self)
        cb_shape_type.addItem("")
        cb_shape_type.addItems([
            "Круг",
           # "Квадрат",
           # "Прямоугольник",
           # "Треугольник",
           # "Трапеция",
           # "Ромб",
            "Сфера",
           # "Куб",
           # "Параллелепипед",
            "Пирамида",
            "Цилиндр",
            "Конус"
        ])

        cb_shape_type.currentTextChanged.connect(lambda: self.defining_interface(cb_shape_type.currentText()))

        self.setGeometry(300, 300, 600, 600)
        self.setWindowTitle('Кажется работает')
        self.show()

        # для того что бы окно отрисовки было показано сразу (можно убрать)
        self.draw_shape(self.shape)

    #Вызов окна отрисовки
    def draw_shape(self, shape):

        self.ren_win = Rendering_Window(shape)

    #показывает все элементы из массива
    def show_UI_elements(self):
        for element in self.UI_elements:
            element.show()

    # скрывает все элементы из массива и очищает его
    def clear_UI_elements(self):
        for element in self.UI_elements:
            element.hide()
        self.UI_elements.clear()

    # def update_UI_elements(self):
    #     for element in self.UI_elements:
    #         element.update()

    #определяет для какой фигуры показать интерфейс
    def defining_interface(self, shape_type):
        if shape_type == "":
            self.shape = Shape()
        elif shape_type == "Круг":
            self.interface_for_circle()
        elif shape_type == "Квадрат":
            pass
        elif shape_type == "Прямоугольник":
            pass
        elif shape_type == "Треугольник":
            pass
        elif shape_type == "Трапеция":
            pass
        elif shape_type == "Ромб":
            pass
        elif shape_type == "Сфера":
            self.interface_for_sphere()
        elif shape_type == "Куб":
            pass
        elif shape_type == "Параллелепипед":
            pass
        elif shape_type == "Пирамида":
            self.interface_for_pyramid()
        elif shape_type == "Цилиндр":
            self.interface_for_cylinder()
        elif shape_type == "Конус":
            self.interface_for_cone()

    def interface_for_circle(self):

        self.clear_UI_elements()

        self.shape = Circle()

        le_radius = QLineEdit(self)
        self.UI_elements.append(le_radius)
        le_radius.move(200, 100)
        le_radius.setValidator(self.validator)
        le_radius.textEdited.connect(lambda: self.shape.set_r(le_radius.text()))

        l_radius = QLabel("Радиус = ", self)
        self.UI_elements.append(l_radius)
        l_radius.move(140, 100)

        b_perimeter = QPushButton("Рассчитать периметр", self)
        self.UI_elements.append(b_perimeter)
        b_perimeter.setGeometry(400, 100, 200, 25)
        b_perimeter.clicked.connect(lambda: b_perimeter.setText(str(self.shape.get_perimeter())))

        b_area = QPushButton("Рассчитать площадь", self)
        self.UI_elements.append(b_area)
        b_area.setGeometry(400, 150, 200, 25)
        b_area.clicked.connect(lambda: b_area.setText(str(self.shape.get_area())))

        self.show_UI_elements()

    def interface_for_sphere(self):

        self.clear_UI_elements()
        #
        self.shape = Sphere()

        le_radius = QLineEdit(self)
        self.UI_elements.append(le_radius)
        le_radius.move(200, 100)
        le_radius.setValidator(self.validator)
        le_radius.textEdited.connect(lambda: self.shape.set_r(le_radius.text()))

        l_radius = QLabel("Радиус = ", self)
        self.UI_elements.append(l_radius)
        l_radius.move(140, 100)

        b_volume = QPushButton("Рассчитать объем", self)
        self.UI_elements.append(b_volume)
        b_volume.setGeometry(400, 100, 200, 25)
        b_volume.clicked.connect(lambda: b_volume.setText(str(self.shape.get_perimeter())))

        b_area = QPushButton("Рассчитать площадь", self)
        self.UI_elements.append(b_area)
        b_area.setGeometry(400, 150, 200, 25)
        b_area.clicked.connect(lambda: b_area.setText(str(self.shape.get_area())))

        self.show_UI_elements()

    def interface_for_cube(self):
        pass

    def interface_for_pyramid(self):

        self.clear_UI_elements()

        self.shape = Pyramid()

        le_radius = QLineEdit(self)
        self.UI_elements.append(le_radius)
        le_radius.move(200, 100)
        le_radius.setValidator(self.validator)
        le_radius.textEdited.connect(lambda: self.shape.set_a(le_radius.text()))

        l_radius = QLabel("Сторона = ", self)
        self.UI_elements.append(l_radius)
        l_radius.move(140, 100)

        le_height = QLineEdit(self)
        self.UI_elements.append(le_height)
        le_height.move(200, 150)
        le_height.setValidator(self.validator)
        le_height.textEdited.connect(lambda: self.shape.set_h(le_height.text()))

        l_height = QLabel("Высота = ", self)
        self.UI_elements.append(l_height)
        l_height.move(140, 150)

        le_num = QLineEdit(self)
        self.UI_elements.append(le_num)
        le_num.move(200, 200)
        le_num.setValidator(self.validator)
        le_num.textEdited.connect(lambda: self.shape.set_n(le_num.text()))

        l_num = QLabel("Кол-во сторон", self)
        self.UI_elements.append(l_num)
        l_num.move(100, 200)

        b_volume = QPushButton("Рассчитать объем", self)
        self.UI_elements.append(b_volume)
        b_volume.setGeometry(400, 100, 200, 25)
        b_volume.clicked.connect(lambda: b_volume.setText(str(self.shape.get_perimeter())))

        b_area = QPushButton("Рассчитать площадь", self)
        self.UI_elements.append(b_area)
        b_area.setGeometry(400, 150, 200, 25)
        b_area.clicked.connect(lambda: b_area.setText(str(self.shape.get_area())))

        self.show_UI_elements()

    def interface_for_cylinder(self):

        self.clear_UI_elements()
        #
        self.shape = Cylinder()

        le_radius = QLineEdit(self)
        self.UI_elements.append(le_radius)
        le_radius.move(200, 100)
        le_radius.setValidator(self.validator)
        le_radius.textEdited.connect(lambda: self.shape.set_r(le_radius.text()))

        l_radius = QLabel("Радиус = ", self)
        self.UI_elements.append(l_radius)
        l_radius.move(140, 100)

        le_height = QLineEdit(self)
        self.UI_elements.append(le_height)
        le_height.move(200, 150)
        le_height.setValidator(self.validator)
        le_height.textEdited.connect(lambda: self.shape.set_h(le_height.text()))

        l_height = QLabel("Высота = ", self)
        self.UI_elements.append(l_height)
        l_height.move(140, 150)

        b_volume = QPushButton("Рассчитать объем", self)
        self.UI_elements.append(b_volume)
        b_volume.setGeometry(400, 100, 200, 25)
        b_volume.clicked.connect(lambda: b_volume.setText(str(self.shape.get_perimeter())))

        b_area = QPushButton("Рассчитать площадь", self)
        self.UI_elements.append(b_area)
        b_area.setGeometry(400, 150, 200, 25)
        b_area.clicked.connect(lambda: b_area.setText(str(self.shape.get_area())))

        self.show_UI_elements()

    def interface_for_cone(self):

        self.clear_UI_elements()

        self.shape = Cone()

        le_radius = QLineEdit(self)
        self.UI_elements.append(le_radius)
        le_radius.move(200, 100)
        le_radius.setValidator(self.validator)
        le_radius.textEdited.connect(lambda: self.shape.set_r(le_radius.text()))

        l_radius = QLabel("Радиус = ", self)
        self.UI_elements.append(l_radius)
        l_radius.move(140, 100)

        le_height = QLineEdit(self)
        self.UI_elements.append(le_height)
        le_height.move(200, 150)
        le_height.setValidator(self.validator)
        le_height.textEdited.connect(lambda: self.shape.set_h(le_height.text()))

        l_height = QLabel("Высота = ", self)
        self.UI_elements.append(l_height)
        l_height.move(140, 150)

        b_volume = QPushButton("Рассчитать объем", self)
        self.UI_elements.append(b_volume)
        b_volume.setGeometry(400, 100, 200, 25)
        b_volume.clicked.connect(lambda: b_volume.setText(str(self.shape.get_perimeter())))

        b_area = QPushButton("Рассчитать площадь", self)
        self.UI_elements.append(b_area)
        b_area.setGeometry(400, 150, 200, 25)
        b_area.clicked.connect(lambda: b_area.setText(str(self.shape.get_area())))

        self.show_UI_elements()

class Rendering_Window(QMainWindow):
    def __init__(self, shape):
        super().__init__()

        # actor
        actor = vtk.vtkActor()
        actor.SetMapper(shape.get_mapper())
        actor.GetProperty().SetColor(tomato)
        # actor.RotateX(0)
        # actor.RotateY(0)

        # renderer
        ren = vtk.vtkRenderer()
        ren.AddActor(actor)
        ren.SetBackground(0.1, 0.2, 0.4)

        # interactor
        frame = QFrame()
        inter = QVTKRenderWindowInteractor(frame)
        inter.SetInteractorStyle(MouseInteractorStyle())

        ren_win = inter.GetRenderWindow()
        ren_win.AddRenderer(ren)

        # ren.ResetCamera()
        # ren.GetActiveCamera().Zoom(1)

        ren_win.Render()
        inter.Initialize()

        layout = QVBoxLayout()
        layout.addWidget(inter)
        frame.setLayout(layout)
        self.setCentralWidget(frame)

        self.setWindowTitle("Что то рисую.")
        self.setGeometry(300+600, 300, 600, 600)
        # self.centerOnScreen()
        self.show()

    # def centerOnScreen(self):
    #     res = QDesktopWidget().screenGeometry()
    #     self.move((res.width()/2) - (self.frameSize().width()/2),
    #               (res.height()/2) - (self.frameSize().height()/2))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_win = Main_Window()
    sys.exit(app.exec_())


#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

import Tkinter as Tk
from pingin import jstping
from ftping import login, download, upload

class Window(Tk.Tk):
    def __init__(self, parent):
        Tk.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.v1 = Tk.IntVar(value=1)
        self.v2 = Tk.StringVar(value=1)
        self.l0 = Tk.Label(self, text= "Server Status", fg='white', bg='black')
        self.l0.grid(column=0, row=0, sticky='EW')
        self.l1 = Tk.Label(self, text= "Host Status", fg='white', bg='black')
        self.l1.grid(column=0, row=2, sticky='EW')

        self.rd0 = Tk.Radiobutton(self, text = None, value=self.v1, bg='black', selectcolor='red')
        self.rd0.grid(column=1,row=0,sticky='EW')
        #self.rd0.deselect()
        self.rd1 = Tk.Radiobutton(self,text = None, value=self.v1, bg='black', selectcolor = 'red')
        self.rd1.grid(column=1,row=2,sticky='EW')
        #self.rd1.deselect()
        #ping_select(self)

        #button1 = Tk.Button(self,  text = "Click here", command=select)
        #button1.grid(column=3,row=0)

        #label = Tkinter.Label(self, anchor="w", fg="white", bg="black")
        #label.grid(column=0,row=1,columnspan=2,sticky="EW")

        self.grid_columnconfigure(0, weight=1)

        self.select()

    def select(self):

        #self.rd1.configure(selectcolor='red')
        server_stats = jstping()
        if server_stats == True:
            self.rd0.configure(selectcolor='green')
            self.rd0.select()
        else:
            self.rd0.select()

if __name__ == "__main__":
    #var = IntVar()
    root = Window(None)
    root.title('Music Box')
    root.configure(bg='black')
    root.mainloop()


import os

def search_files(folder_path, keyword):
    matches = []
    for root,dirs,files in os.walk(folder_path):
        for file in files:
            if file.endswith(".txt"):
                file_path = os.path.join(root,file) #list of files in the directory
                with open(file_path, 'r') as f:
                    content = f.read()
                    if keyword in content:
                        matches.append(file_path)
        return matches

folder_path = r'C:\Users\IHIMBRU\Desktop\search'
keyword = input("Enter keyword to search")

results = search_files(folder_path,keyword)

if len(results) >0:
    print("Matching file")
    for file_path in results:
        print(file_path)

else:
    print("No matching files found")


# Nhóm 30
# Ngô Duy Tân-N19DCCN165
# Trương Hoàng Sang- N19DCCN157
import numpy as np
import matplotlib.pyplot as plt
import math as mt
from scipy.spatial import distance_matrix

def calculo_distancia(m_distancia,camino,n_ciudades):
    c_dis = 0
    for c1 in range(0, n_ciudades):
        suma_actual = m_distancia[camino[c1], camino[c1 - 1]]
        c_dis = suma_actual + c_dis
    return c_dis

def enc_vecino(camino,p1,p2):
    vecino = np.array(camino)
    vecino[p1] = camino[p2]
    vecino[p2] = camino[p1]
    return vecino

ciudades = np.array([[1,7],[7,5],[7,1],[3,1],[4,3]])
n_ciudades = len(ciudades)
m_distancia = distance_matrix(ciudades,ciudades)
camino_0 = np.random.choice(np.arange(n_ciudades),n_ciudades,False)

# Inicializacion de Temperatura
lk = 10
tk = 0.1
rmin = 0.95
ra = 0
b = 1000

camino = np.array(camino_0)
camino_correcto = np.array(camino_0)
distancia_actual = calculo_distancia(m_distancia, camino, n_ciudades)
mut_anterior = np.random.choice(np.arange(n_ciudades), 1)
camino = np.array(camino_0)
while ra < rmin:
    # for c2 in range (0,3):
    int_aceptado = 0
    intento = 0
    for l in range(0, lk):
        # Selección de posición a mutar
        posicion_cambio = np.random.choice(np.arange(5), 1)
        # Evitar mutar el mísmo índice 2 veces seguidas
        if posicion_cambio == mut_anterior:
            l = l - 1
            continue
        else:
            mut_anterior = np.array(posicion_cambio)
            # Mutar
            if posicion_cambio == 5:
                nuevo_camino = np.array(enc_vecino(camino, int(posicion_cambio) - 2, int(posicion_cambio) - 1))
            else:
                nuevo_camino = np.array(enc_vecino(camino, posicion_cambio, int(posicion_cambio) - 1))
            # Cálculo nueva distancia
            nueva_distancia = calculo_distancia(m_distancia, nuevo_camino, n_ciudades)
            # Comparación distancias
            if nueva_distancia < distancia_actual:
                camino = np.array(nuevo_camino)
                distancia_actual = nueva_distancia
                int_aceptado = int_aceptado + 1
                camino_correcto = np.array(nuevo_camino)
            else:
                prob = mt.exp(-(nueva_distancia - distancia_actual) / tk)
                dss = np.random.random()
                if dss < prob:
                    camino = np.array(nuevo_camino)
                    distancia_actual = nueva_distancia
                    int_aceptado = int_aceptado + 1
            intento = intento + 1
        l = l + 1
    tk = b * tk
    ra = int_aceptado / intento

# Enfriamiento
cam_tom = []
cont = [0]
mut_anterior = np.random.choice(np.arange(5), 1)
distancia_actual = calculo_distancia(m_distancia,camino,n_ciudades)
#while tk > .00000000001:
for c2 in range (0,100):
    for l in range(0,lk):
        posicion_cambio = np.random.choice(np.arange(5), 1)
        if posicion_cambio == mut_anterior:
            l = l - 1
            continue
        else:
            mut_anterior = np.array(posicion_cambio)
            if posicion_cambio == 5:
                nuevo_camino = np.array(enc_vecino(camino, int(posicion_cambio) - 2, int(posicion_cambio) - 1))
            else:
                nuevo_camino = np.array(enc_vecino(camino, posicion_cambio, int(posicion_cambio) - 1))
                nueva_distancia = calculo_distancia(m_distancia, nuevo_camino, n_ciudades)
            if nueva_distancia < distancia_actual:
                camino = np.array(nuevo_camino)
                distancia_actual = nueva_distancia
                cam_tom.append(camino)
            else:
                if tk > .0000001:
                    prob = mt.exp(-(nueva_distancia - distancia_actual) / tk)
                    dss = np.random.random()
                    if dss < prob:
                        camino = np.array(nuevo_camino)
                        distancia_actual = nueva_distancia
                        tk = 1
        l = l + 1
    tk = tk * np.random.randint(.2, 1)

print('-------------------------------------')
print('Minimal distance found:')
print(distancia_actual)
print('-------------------------------------')
print('Order of the cities in the trajectory:')
print(camino)

# Cálculo promedio/desviación estandar
caminos_tomados = np.array(cam_tom)
c3 = 0
dd = []
for e1 in caminos_tomados:
    d = calculo_distancia(m_distancia, e1, n_ciudades)
    dd.append(d)

y = np.array(dd)
x = np.linspace(1, len(y), len(y))

vec_camino = []
for e2 in camino:
    vec_camino.append(ciudades[e2])
cc = np.array(vec_camino)
print('-------------------------------------')
print('Coordinates of the cities in the trajectory:')
print(cc)


from chowdren.writers.objects import ObjectWriter

from chowdren.common import get_animation_name, to_c, make_color

from chowdren.writers.events import (ActionMethodWriter, ConditionMethodWriter,
    ExpressionMethodWriter, make_table, ActionWriter)

class UltimateFullcsreen(ObjectWriter):
    class_name = 'Fullscreen'
    static = True

class SetFullscreenType(ActionMethodWriter):
    custom = True

    def write(self, writer):
        parameter = self.convert_index(0)
        # if parameter != '0':
        #     print('Fullscreen type not supported:', parameter)
        #     raise NotImplementedError()

actions = make_table(ActionMethodWriter, {
    1 : 'manager->set_window(false)',
    0 : 'manager->set_window(true)',
    5 : 'manager->set_window_scale',
    4 : SetFullscreenType
})

conditions = make_table(ConditionMethodWriter, {
    1 : '!manager->is_fullscreen()'
})

expressions = make_table(ExpressionMethodWriter, {
    0 : 'WindowControl::get_x',
    1 : 'WindowControl::get_y'
})

def get_object():
    return UltimateFullcsreen


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('accounting', '0002_donateamount_user'),
    ]

    operations = [
        migrations.CreateModel(
            name='DonateGoal',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('amount', models.DecimalField(max_digits=9, decimal_places=2)),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('end', models.DateTimeField(null=True, blank=True)),
                ('description', models.TextField(null=True, blank=True)),
            ],
            options={
                'ordering': ('-created',),
            },
            bases=(models.Model,),
        ),
    ]


#!/usr/bin/env python3
'''
@author  Michele Tomaiuolo - http://www.ce.unipr.it/people/tomamic
@license This software is free - http://www.gnu.org/licenses/gpl.html
'''

import g2d

def sierpinski(position: (int, int), size: (int, int)):
    x, y = position
    w, h = size
    w3, h3 = w // 3, h // 3
    if w3 < 1 or h3 < 1:
        return
    for row in range(3):
        for col in range(3):
            pos = x + col * w3, y + row * h3
            if row == 1 and col == 1:
                g2d.fill_rect(pos, (w3, h3))
            else:
                sierpinski(pos, (w3, h3))

def main():
    g2d.init_canvas((243, 243))
    g2d.set_color((127, 0, 0))
    sierpinski((0, 0), (243, 243))
    g2d.main_loop()

main()


"""
Created by Jiabei, last modified 10/27/2022

Tests for SSNP ops.
"""
import os

os.environ["TF_CPP_MIN_LOG_LEVEL"] = '0'
os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"

import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops, test_util
from tensorflow.python.platform import test, tf_logging

try:
    from . import ssnp_ops
except ImportError:
    import ssnp_ops

rng = np.random.default_rng(1227)

class SSNPTest(test.TestCase):
    FORWARD = True
    XY_SHAPE = (512, 512)
    BATCH = 8
    dz = 1

    def ssnp_test_body(self, shape, dtype):
        in_arr = rng.random(shape, np.float64) + rng.random(shape, np.float64) * 1j - (0.5 + 0.5j)
        with self.cached_session(force_gpu=True):
            in1_tensor = tf.constant(in_arr, dtype=dtype)
            in2_tensor = tf.constant(in_arr, dtype=dtype)
            out1_tensor, out2_tensor = tf.scatt_lib.ssnp(in1_tensor, in2_tensor, res=(1, 2, 3))
            tf_logging.info(f"shape: {out1_tensor.shape}, {out2_tensor.shape}")

    @test_util.run_cuda_only
    def test_free_c64(self):
        self.ssnp_test_body(shape=self.XY_SHAPE, dtype=tf.complex64)

if __name__ == '__main__':
    test.main()


from django.shortcuts import render, redirect
from app.forms.business_form import BusinessForm
from app.models.buisness import Business
from app.models.neighborhood import Neighborhood
from django.http import JsonResponse

def buisness(request):
    data = Business.objects.all()
    if request.method == "POST":
        form = BusinessForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect("buisness")
    else:
        form = BusinessForm()
    return render(request, "dashboard/buisness.html", {"form": form, "data": data})

def get_neighborhoods(request):
    region_id = request.GET.get("region_id")
    neighborhoods = Neighborhood.objects.filter(region_id=region_id).values(
        "id", "name"
    )
    return JsonResponse({"neighborhoods": list(neighborhoods)})


class Solution:
    def longestObstacleCourseAtEachPosition(self, obstacles):
        n = len(obstacles)

        # Create a list to store the lengths of the longest obstacle course at each position
        dp = []
        ans = []

        for height in obstacles:
            # Find the index where the current obstacle can be inserted in the sorted dp list
            index = self.binarySearch(dp, height)

            # Update the dp list and ans list
            if index == len(dp):
                dp.append(height)
                ans.append(index + 1)
            else:
                dp[index] = height
                ans.append(index + 1)

        return ans

    def binarySearch(self, dp, target):
        left, right = 0, len(dp) - 1
        while left <= right:
            mid = (left + right) // 2
            if dp[mid] <= target:
                left = mid + 1
            else:
                right = mid - 1
        return left


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

def add_noise(series, noise_level):
    return series * (1 + noise_level * np.random.randn(len(series)))

def target_encode(trn_series=None,
                  tst_series=None,
                  target=None,
                  min_samples_leaf=1,
                  smoothing=1,
                  noise_level=0):
    """
    Smoothing is computed like in the following paper by Daniele Micci-Barreca
    https://kaggle2.blob.core.windows.net/forum-message-attachments/225952/7441/high%20cardinality%20categoricals.pdf
    trn_series : training categorical feature as a pd.Series
    tst_series : test categorical feature as a pd.Series
    target : target data as a pd.Series
    min_samples_leaf (int) : minimum samples to take category average into account
    smoothing (int) : smoothing effect to balance categorical average vs prior
    """
    assert len(trn_series) == len(target)
    assert trn_series.name == tst_series.name
    temp = pd.concat([trn_series, target], axis=1)
    # Compute target mean
    averages = temp.groupby(by=trn_series.name)[target.name].agg(["mean", "count"])
    # Compute smoothing
    smoothing = 1 / (1 + np.exp(-(averages["count"] - min_samples_leaf) / smoothing))
    # Apply average function to all target data
    prior = target.mean()
    # The bigger the count the less full_avg is taken into account
    averages[target.name] = prior * (1 - smoothing) + averages["mean"] * smoothing
    averages.drop(["mean", "count"], axis=1, inplace=True)
    # Apply averages to trn and tst series
    ft_trn_series = pd.merge(
        trn_series.to_frame(trn_series.name),
        averages.reset_index().rename(columns={'index': target.name, target.name: 'average'}),
        on=trn_series.name,
        how='left')['average'].rename(trn_series.name + '_mean').fillna(prior)
    # pd.merge does not keep the index so restore it
    ft_trn_series.index = trn_series.index
    ft_tst_series = pd.merge(
        tst_series.to_frame(tst_series.name),
        averages.reset_index().rename(columns={'index': target.name, target.name: 'average'}),
        on=tst_series.name,
        how='left')['average'].rename(trn_series.name + '_mean').fillna(prior)
    # pd.merge does not keep the index so restore it
    ft_tst_series.index = tst_series.index
    return add_noise(ft_trn_series, noise_level), add_noise(ft_tst_series, noise_level)

import gc
gc.enable()

from basic_processing import *
from base_model import *

trn_df, sub_df, train_id, test_id, y = read_data()

import os
if os.path.exists('../input/train.csv'):
    target = pd.read_csv('../input/train.csv')['target']
else:
    target = pd.read_csv('../dropcal_one_hot/train.csv')['target']

train_features = [
    "ps_car_13",  # : 1571.65 / shadow  609.23
    "ps_reg_03",  # : 1408.42 / shadow  511.15
    "ps_ind_05_cat",  # : 1387.87 / shadow   84.72
    "ps_ind_03",  # : 1219.47 / shadow  230.55
    "ps_ind_15",  # :  922.18 / shadow  242.00
    "ps_reg_02",  # :  920.65 / shadow  267.50
    "ps_car_14",  # :  798.48 / shadow  549.58
    "ps_car_12",  # :  731.93 / shadow  293.62
    "ps_car_01_cat",  # :  698.07 / shadow  178.72
    "ps_car_07_cat",  # :  694.53 / shadow   36.35
    "ps_ind_17_bin",  # :  620.77 / shadow   23.15
    "ps_car_03_cat",  # :  611.73 / shadow   50.67
    "ps_reg_01",  # :  598.60 / shadow  178.57
    "ps_car_15",  # :  593.35 / shadow  226.43
    "ps_ind_01",  # :  547.32 / shadow  154.58
    "ps_ind_16_bin",  # :  475.37 / shadow   34.17
    "ps_ind_07_bin",  # :  435.28 / shadow   28.92
    "ps_car_06_cat",  # :  398.02 / shadow  212.43
    "ps_car_04_cat",  # :  376.87 / shadow   76.98
    "ps_ind_06_bin",  # :  370.97 / shadow   36.13
    "ps_car_09_cat",  # :  214.12 / shadow   81.38
    "ps_car_02_cat",  # :  203.03 / shadow   26.67
    "ps_ind_02_cat",  # :  189.47 / shadow   65.68
    "ps_car_11",  # :  173.28 / shadow   76.45
    "ps_car_05_cat",  # :  172.75 / shadow   62.92
    "ps_calc_09",  # :  169.13 / shadow  129.72
    "ps_calc_05",  # :  148.83 / shadow  120.68
    "ps_ind_08_bin",  # :  140.73 / shadow   27.63
    "ps_car_08_cat",  # :  120.87 / shadow   28.82
    "ps_ind_09_bin",  # :  113.92 / shadow   27.05
    "ps_ind_04_cat",  # :  107.27 / shadow   37.43
    "ps_ind_18_bin",  # :   77.42 / shadow   25.97
    "ps_ind_12_bin",  # :   39.67 / shadow   15.52
    "ps_ind_14",  # :   37.37 / shadow   16.65
    "ps_car_11_cat"  # Very nice spot from Tilii : https://www.kaggle.com/tilii7
]
# add combinations
combs = [
    ('ps_reg_01', 'ps_car_02_cat'),
    ('ps_reg_01', 'ps_car_04_cat'),
]
import time
from sklearn.preprocessing import LabelEncoder

start = time.time()
for n_c, (f1, f2) in enumerate(combs):
    name1 = f1 + "_plus_" + f2
    print('current feature %60s %4d in %5.1f'
          % (name1, n_c + 1, (time.time() - start) / 60))
    print('\r' * 75)
    trn_df[name1] = trn_df[f1].apply(lambda x: str(x)) + "_" + trn_df[f2].apply(lambda x: str(x))
    sub_df[name1] = sub_df[f1].apply(lambda x: str(x)) + "_" + sub_df[f2].apply(lambda x: str(x))
    # Label Encode
    lbl = LabelEncoder()
    lbl.fit(list(trn_df[name1].values) + list(sub_df[name1].values))
    trn_df[name1] = lbl.transform(list(trn_df[name1].values))
    sub_df[name1] = lbl.transform(list(sub_df[name1].values))

    train_features.append(name1)

trn_df = trn_df[train_features]
sub_df = sub_df[train_features]
from basic_processing import *
from base_model import *

def feature_interact(train, test, interaction_pairs, interaction_method='_', suffix=''):
    # interaction_pairs are two feature name in train's feature
    from sklearn.preprocessing import LabelEncoder
    import logging

    for idx, item in enumerate(interaction_pairs):
        logging.info("processing feature %s %s" % (item[0], item[1]))
        name = item[0] + '|' + item[1] + suffix

        if interaction_method == '_':
            train[name] = train[item[0]].apply(lambda x: str(x)) + "_" + train[item[1]].apply(lambda x: str(x))
            test[name] = test[item[0]].apply(lambda x: str(x)) + "_" + test[item[1]].apply(lambda x: str(x))

        lbl = LabelEncoder()
        lbl.fit(list(train[name].values) + list(test[name].values))
        train[name] = lbl.transform(list(train[name].values))
        test[name] = lbl.transform(list(test[name].values))

    return train, test

trn_df, sub_df = feature_interact(trn_df, sub_df, [("ps_car_13", "ps_ind_05_cat"),
                                             ("ps_ind_05_cat", "ps_reg_03"),
                                             # ("ps_car_13", "ps_ind_17_bin"),
                                             # ("ps_ind_05_cat", "ps_ind_17_bin")
                                            ])

f_cats = [f for f in trn_df.columns if "_cat" in f]

for f in f_cats:
    trn_df[f + "_avg"], sub_df[f + "_avg"] = target_encode(trn_series=trn_df[f],
                                                           tst_series=sub_df[f],
                                                           target=target,
                                                           min_samples_leaf=200,
                                                           smoothing=10,
                                                           noise_level=0)

train = trn_df.values
test = sub_df.values

print (train.shape, test.shape, target.shape)
# Value |   colsample_bytree |     gamma |   max_depth |   min_child_weight |   subsample |
# [35m   0.35368 |
#             0.4719 |
#    9.6957 |
#      3.3838 |
#            10.3150 |
#      0.5351 |

from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from rgf.sklearn import RGFClassifier
import sys

from numba import jit
@jit
def eval_gini(y_true, y_prob):
    """
    Original author CPMP : https://www.kaggle.com/cpmpml
    In kernel : https://www.kaggle.com/cpmpml/extremely-fast-gini-computation
    """
    y_true = np.asarray(y_true)
    y_true = y_true[np.argsort(y_prob)]
    ntrue = 0
    gini = 0
    delta = 0
    n = len(y_true)
    for i in range(n-1, -1, -1):
        y_i = y_true[i]
        ntrue += y_i
        gini += y_i * delta
        delta += 1 - y_i
    gini = 1 - 2 * gini / (ntrue * (n - ntrue))
    return gini

model_set = {}
model_set['xgb1'] = {
    'name': 'xgb1',
    'mdl_type': 'xgb',
    'param': {
        'objective': 'binary:logistic',
        # 'booster' : 'gbtree',
        'learning_rate': 0.07,
        'n_estimators': 2000,
        'max_depth': 9,
        'scale_pos_weight': 1.0,
        'seed': 1024,

        'subsample': 0.54,
        'colsample_bytree': 0.35,
        'gamma':0.47,

        'min_child_weight': 10,
        # 'reg_lambda': 1.3,
        # 'reg_alpha': 8.0,
        # 'missing': -1,
        'n_jobs': 7,
    },
    'metric': eval_gini,
}

if __name__ == '__main__':
    print (sys.argv)

    if len(sys.argv) >= 2:
        model = model_set[sys.argv[1]]
    else:
        model = model_set['xgb1']

    metric = model['metric']
    if model['mdl_type'] == 'xgb':
        mdl = XGBClassifier(**model['param'])
    elif model['mdl_type'] == 'lgb':
        mdl = LGBMClassifier(**model['param'])
    elif model['mdl_type'] == 'rgf':
        mdl = RGFClassifier(**model['param'])

    train_pred, test_pred, mean, std, full_score = five_fold_with_baging(train, y,
                            test, train_id, test_id, metric, mdl, mdl_type=model['mdl_type'], seed=1024,
                            stratified=False, n_splits=1, n_folds=5, n_bags=1, early_stop=50, verbose=False)
    write_csv(train_pred, test_pred, mdl_name=model['name']+'-interaction', mean=mean, overall_mean=full_score, std=std, bag=-1)


import smooth_fitting_genetic as GA

casses = []
class Point:
    def __init__( self , x  , y ):
        self.x = x
        self.y = y

"""
EX for the data :
casses =
[
    {
     'degree'  : 4,
     'points' : [ { x : 5 , y : 4 } , { x : 2 , y : 3 } ]
    },
    {
     'degree'  : 10,
     'points' : [ { x : 14 , y : 7 } , { x : 4 , y : 6 } , { x : 5 , y : 2 } , { x : 2.1 , y : 3.1 }]
    }

]
"""

# ---------------------- get cases from the file  ---------------

file = open("input.txt", "r")
numOfCases = int ( file.readline() )

for iCases in range(0 , numOfCases):
    numOfPoints = int ( file.readline() )
    degree = int( file.readline() )
    points = []
    for iPoint in range(0 , numOfPoints):
        p = file.readline()
        points.append( Point( int(p[0]) , int(p[1]) ))

    casses.append( { 'degree' : degree , 'points' : points })

# ------------------------------------------------------------------

# ------------- main ----------------

for  i , case in enumerate(casses):
    selectedChromosome = GA.select_chromosome(case['degree'] , case['points'])
    print('case %s :' % i)
    for point in selectedChromosome:
        print( point.x , point.y )
    print('\n')

smooth curve fitting [ floating point ]/smooth_curve.py


def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
    #순서대로 처리할 수 없고, 인덱스에 따른 처리를 할 것이기 때문에 배열을 미리 생성
    answer = [0] * len(temperatures)
    stack = []
    for i, cur in enumerate(temperatures):
        #현재 온도가 스택 값보다 높다면 처리 후 저장
        #[73] -> [74] -> [75, 71, 69] -> [75] -> [76, 73]
        while stack and cur > temperatures[stack[-1]]:
            last = stack.pop()
            answer[last] = i - last
        stack.append(i)
    return answer


import openrouteservice
from openrouteservice import convert

import os
import json
'''
install ors library:
https://openrouteservice-py.readthedocs.io/en/latest/

get your api key:
https://openrouteservice.org/dev/#/home

paste it into key.txt (keep your key to yourself!)
'''
#print(os.getcwd())

routes = []

start = (8.34234,48.23424)
end = (8.34423,48.26424)
coords = (start, end)
keyfile = open('key.txt', 'r')
key = keyfile.read()
client = openrouteservice.Client(key=key) # Specify your personal API key
# decode_polyline needs the geometry only
geometry = client.directions(coords)['routes'][0]['geometry']
routes.append(geometry)

with open('route.geojson', 'w') as outfile:
    json.dump(geometry, outfile)

decoded = convert.decode_polyline(geometry)

print(decoded)


import argparse
from collections import OrderedDict
import csv

def process_coordinates(reader):
    """
    Each row is single tab delimited string of three values
    """
    data = OrderedDict()
    total_rows = 0

    for raw_row in reader:
        total_rows += 1

        row = raw_row[0].split("\t")

        chromosome_name = row[0]
        start_position = row[1]
        end_position = row[2]

        key = f"{chromosome_name}{start_position}{end_position}"

        data[key] = [
            chromosome_name,
            start_position,
            end_position,
        ]

    return data, total_rows

def process_differential_coordinates(reader):
    """
    Each row is a list of 7 values
    """
    data = OrderedDict()
    total_rows = 0

    for row in reader:

        total_rows += 1

        chromosome_name = row[1]
        start_position = row[2]
        end_position = row[3]
        strand = row[4]
        padj = row[5]
        log2FoldChange = row[6]

        key = f"{chromosome_name}{start_position}{end_position}{strand}{padj}{log2FoldChange}"

        data[key] = [
            chromosome_name,
            start_position,
            end_position,
            strand,
            padj,
            log2FoldChange,
        ]

    return data, total_rows

def remove_duplicates(input_csv_path):
    """
    Opens a CSV file and returns back a dictionary of unique rows
    Columns should be: ID, chromosome_name, start_position, end_position, strand, padj, log2FoldChange
    """
    data = OrderedDict()
    total_rows = 0
    csv_type = ""

    # Open CSV, iterate over each row, only insert unique rows into dictionary
    with open(input_csv_path, "r") as csv_file:
        reader = csv.reader(csv_file)

        for row in reader:
            # Check if CSV is Coordinates or Differential Coordinates
            if row[0] == "\ufeff'ID'":
                csv_type = "diff"
                break
            else:
                # Coordinates CSV
                csv_type = "coor"
                break

        if csv_type == "diff":
            data, total_rows = process_differential_coordinates(reader)
        elif csv_type == "coor":
            data, total_rows = process_coordinates(reader)
        else:
            print(
                "Something bad happened! Columns don't match coordinate or differential coordinate format"
            )

    return data, total_rows, csv_type

def write_output_csv(output_csv_path, data, csv_type):
    """
    Accepts a dictionary of rows and writes a new CSV
    Columns should be: ID, chromosome_name, start_position, end_position, strand, padj, log2FoldChange
    """
    count = 1

    # Open a second output file and write only unique rows
    with open(output_csv_path, "w", newline="") as csv_file:
        writer = csv.writer(csv_file, delimiter="\t")

        # First row, header
        if csv_type == "diff":

            header = [
                "'ID'",
                "'chromosome_name'",
                "'start_position'",
                "'end_position'",
                "'strand'",
                "'padj'",
                "'log2FoldChange'",
            ]
            writer.writerow(header)

            # Prepend a unique id and write to csv
            for key, value in data.items():
                writer.writerow([count] + value)

                count += 1
        else:
            # Prepend a unique id and write to csv
            for key, value in data.items():
                writer.writerow(value)

                count += 1

    return count - 1

# python3 remove_duplicates.py input.csv output.csv
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Remove duplicate rows from CSV file")
    parser.add_argument("input_csv_path", type=str, help="Path to source CSV")
    parser.add_argument("output_csv_path", type=str, help="Path to output CSV")
    args = parser.parse_args()

    unique_rows, total_rows, csv_type = remove_duplicates(args.input_csv_path)
    rows_written = write_output_csv(args.output_csv_path, unique_rows, csv_type)
    duplicates = total_rows - rows_written

    print(
        f"Looked at {total_rows} rows. Found {duplicates} duplicates. Found {rows_written} unique rows."
    )


from switchyard.lib.userlib import *
from SpanningTreeMessage import *
from time import sleep

def get_stp_packet_to_bytes(root_id, hops, src, dst):
    spanningTreeMessage = SpanningTreeMessage(root_id=root_id, hops_to_root=hops, switch_id=src)
    Ethernet.add_next_header_class(EtherType.SLOW, SpanningTreeMessage)
    stp_packet = Ethernet(src=src, dst=dst, ethertype=EtherType.SLOW) + spanningTreeMessage
    return stp_packet.to_bytes()

def is_stp_packet(packet):
    return "SpanningTreeMessage" in str(packet)

#net:           the net object
#interface:     specific port of switch's ports
#root_id:       argument of SpanningTreeMessage()
#hops_to_root:  argument of SpanningTreeMessage()
#switch_id:     argument of SpanningTreeMessage()
#This method generates a stp packet in bytes and send it to its destination from a specific interface
def send_stp_packet(net, interface, root_id, hops_to_root, switch_id):
    stp_in_bytes = get_stp_packet_to_bytes(root_id, hops_to_root, switch_id, 'ff:ff:ff:ff:ff:ff')
    stp_packet = Packet(stp_in_bytes)
    net.send_packet(interface.name, stp_packet)

#input int timestamp, str input_port, Packet packet
#return dict() packet
def parse_stp_packet(timestamp, incoming_interface, recv_packet):
    packet = dict()
    stp_packet_str = str(recv_packet)
    spanning_tree_message = stp_packet_str[(stp_packet_str.find("SpanningTreeMessage ") + len("SpanningTreeMessage ")):]
    stm = spanning_tree_message.split(', ')
    root = stm[0][stm[0].find('root: ') + len('root: '):]
    hops_to_root = stm[1][stm[1].find('hops-to-root: ') + len('hops-to-root: '):]
    switch_id = stm[2][stm[2].find('switch_id: ')+len('switch_id: '): len(stm[2]) - 1]
    packet["root"] = root
    packet["hops_to_root"] = int(hops_to_root)
    packet["switch_id"] = switch_id
    packet["timestamp"] = timestamp
    packet["incoming_interface"] = incoming_interface
    return packet

class Switch:
    def __init__(self, id, ports):
        self.id = id      #switch id
        self.hops = 0    #hops to the root_switch
        self.root_switch_id = id     #netwrok root_switch switch
        self.port_status = dict()
        for port in ports:
            self.port_status[port] = 0    #0 for forward, 1 for block
        self.last_stp_time = None   #the time at which the last spanning tree message was received
        self.root_interface = None    #the interface on which spanning tree message from the perceived root arrives

    def set_last_stp_time(self, last_stp_time):
        self.last_stp_time = last_stp_time

    def get_last_stp_time(self):
        return self.last_stp_time

    def get_root_interface(self):
        return self.get_root_interface

    def needs_flood(self, stp_packet):
        if stp_packet['incoming_interface'] == self.root_interface: #8
            self.hops = stp_packet['hops_to_root'] + 1
            self.root_switch_id = stp_packet['root']
            self.last_stp_time = stp_packet['timestamp']
            self.root_interface = stp_packet['incoming_interface']
            stp_packet['switch_id'] = self.id
            stp_packet['hops_to_root'] = self.hops
            return True, stp_packet
        if stp_packet['root'] < self.root_switch_id:  #9
            self.hops = stp_packet['hops_to_root'] + 1
            self.root_switch_id = stp_packet['root']
            self.root_interface = stp_packet['incoming_interface']
            self.last_stp_time = stp_packet['timestamp']
            stp_packet['switch_id'] = self.id
            stp_packet['hops_to_root'] = self.hops
            for keys in self.port_status.keys():
                self.port_status[keys] = 0
            return True, stp_packet
        if stp_packet['root'] > self.root_switch_id: #10
            self.port_status[stp_packet['incoming_interface']] = 0
            return False, None
        if stp_packet['root'] == self.root_switch_id: #11
            if (stp_packet['hops_to_root'] + 1 < self.hops) or (stp_packet['hops_to_root'] + 1 == self.hops and self.root_switch_id > stp_packet['switch_id']):
                self.port_status[stp_packet['incoming_interface']] = 0
                self.port_status[self.root_interface] = 1
                self.root_interface = stp_packet['incoming_interface']
                self.hops = stp_packet['hops_to_root'] + 1
                self.root_switch_id = stp_packet['root']
                self.last_stp_time = stp_packet['timestamp']
                stp_packet['switch_id'] = self.id
                stp_packet['hops_to_root'] = self.hops
                return True, stp_packet
            if stp_packet['hops_to_root'] + 1 > self.hops:
                self.port_status[stp_packet['incoming_interface']] = 1
                return False, None
            else:
                self.port_status[stp_packet['incoming_interface']] = 1
                return False, None

class Node:
    def __init__(self, key, value):
        self.prev = None
        self.next = None
        self.key = key
        self.value = value

class LRUCache:
    def __init__(self):
        self.capacity = 5
        self.head = Node(-1, -1)
        self.tail = Node(-1, -1)
        self.head.next = self.tail
        self.tail.prev = self.head
        self.key_node_dict = dict()
        self.val_key_dict = dict()

    def _updateOrder(self, node):
        node.prev = self.head
        node.next = self.head.next
        node.prev.next = node
        node.next.prev = node

    def _restart(self, node):
        node.prev.next = node.next
        node.next.prev = node.prev

    def get(self, key):
        if key not in self.key_node_dict.keys():
            return None
        node = self.key_node_dict[key]
        self._restart(node)
        self._updateOrder(node)
        return node.value

    def add(self, key, value):
        node = None
        if value in self.val_key_dict.keys():
            old_key = self.val_key_dict[value]
            del self.val_key_dict[value]
            node = self.key_node_dict[old_key]
            del self.key_node_dict[old_key]
            self._restart(node)
        self.val_key_dict[value] = key

        if key in self.key_node_dict.keys():
            node = self.key_node_dict[key]
            node.value = value
            self._restart(node)
        else:
            if len(self.key_node_dict) == self.capacity:
                del self.key_node_dict[self.tail.prev.key]
                self._restart(self.tail.prev)
            node = Node(key, value)
            self.key_node_dict[key] = node
        self._updateOrder(node)

def main(net):
    my_interfaces = net.interfaces()
    mymacs = set([intf.ethaddr for intf in my_interfaces])
    myports = set([intf.name for intf in my_interfaces])
    lru = LRUCache()

    #Setup current_switch
    id = min(mymacs)
    currentSwitch = Switch(str(id), myports)
    for interface in my_interfaces:
        send_stp_packet(net, interface, currentSwitch.root_switch_id, 0 , currentSwitch.id)

    while True:
        try:
            timestamp, input_port, packet = net.recv_packet()
        except NoPackets:
            if currentSwitch.root_switch_id == currentSwitch.id:
                sleep(2)
                for interface in my_interfaces:
                    send_stp_packet(net, interface, currentSwitch.root_switch_id, currentSwitch.hops, currentSwitch.id)
            continue
        except Shutdown:
            return

        if is_stp_packet(packet):
            currentSwitch.set_last_stp_time(timestamp)
            #packet_root, packet_hops, packet_switch_id = parse_stp_packet(packet)
            received_packet = parse_stp_packet(timestamp, input_port, packet)
            needs_flood, new_stp_packet = currentSwitch.needs_flood(received_packet)
            if needs_flood:
                for interface in my_interfaces:
                    if interface.name != input_port:
                        send_stp_packet(net, interface, new_stp_packet['root'], new_stp_packet['hops_to_root'], new_stp_packet['switch_id'])

        else:
            if timestamp - currentSwitch.get_last_stp_time() > 10:
                currentSwitch = Switch(str(id), myports)

            lru.add(packet[0].src, input_port)
            #log_debug ("In {} received packet {} on {}".format(net.name, packet, input_port))

            if packet[0].dst in mymacs:
                log_debug ("Packet intended for me")
            else:
                output_port = lru.get(packet[0].dst)
                if output_port is not None:
                    net.send_packet(output_port, packet)

                else:
                    for interface in my_interfaces:
                        if interface.name != input_port and currentSwitch.port_status[interface.name] == 0:
                            net.send_packet(interface.name, packet)
    net.shutdown()

P1 Spanning Tree Protocol/myswitch_stp.py


import cv2 as cv #computer vision

img = cv.imread('C:\\Users\\buglow\\Desktop\\shapes.png')   #bringing the photo by path

                              # ====> Segmantation | pre-processing <====

imgGrey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)                               #convert to gray scale
_, thrash = cv.threshold(imgGrey, 240, 255, cv.THRESH_BINARY)               #cnvt ->b&w (src, sabet, abyad, el method(if > sabet -->put abyad)
contours, _ = cv.findContours(thrash, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
                                                                            # Contours can be explained simply as a
                                                                            # curve joining all the continuous points
                                                                            # (along the boundary), having same color or intensity.

#view for clarification
cv.imshow("Original", img) #give window with the title shapes contain the image
cv.waitKey(0)              #wait any key
cv.destroyAllWindows()     #to destroy all after the key :B

cv.imshow("GRAY Scale", imgGrey) #give window with the title shapes contain the image
cv.waitKey(0)                    #wait any key
cv.destroyAllWindows()           #to destroy all after the key :B

for contour in contours:
                                # ====> Do the Job like a boss  <====

    epsilon = 0.01*cv.arcLength(contour, True)               #calc el epsilon value by kikking el coeff. f el arc-length of countour
    approx = cv.approxPolyDP(contour, epsilon, True)         #extracting points in a 2d array

    cv.drawContours(img, [approx], -1, (0, 150, 255), 6)     #draw on src contours with approx points, all, color, thickness

    x = approx.ravel()[0] -7 #ravel return flated array  wierdoo :P
    y = approx.ravel()[1] -7
#print(approx, len(approx))

                        #  ====> Take decision According to the number of points <=====

    if len(approx) == 3:
        cv.putText(img, "Triangle", (x, y), cv.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
    elif len(approx) == 4:
        x1, y1, w, h = cv.boundingRect(approx) #Xtract (first point) and (width and height)
        ratio = float(w)/h
        if 0.8 <= ratio <= 1.3:
            cv.putText(img, "Square", (x, y), cv.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0)) #src, text, set(point), fontType, fontSize, RGBcolor
        else:
            cv.putText(img, "Rectangle", (x, y), cv.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
    elif len(approx) == 5:
        cv.putText(img, "Pentagon", (x, y), cv.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
    elif len(approx) == 6:
        cv.putText(img, "Hexagon", (x, y), cv.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
    elif len(approx) == 10:
        cv.putText(img, "Star", (x, y), cv.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
    else:
        cv.putText(img, "Circle", (x, y), cv.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))

cv.imshow("Final Result", img) #give window with the title shapes contain the image
cv.waitKey(0)                  #wait any key
cv.destroyAllWindows()         #to destroy all after the key :B
#----------------------------|
#                            |
# Done on 7:40 10/3/2020     |
# Paulo's Team:              |
#   Paulo Emil 2             |
#   Gamal Ashraf Gamal 2     |
#   Akram Diaa Abaas 2       |
#   Remon Wagde Marzouk 3    |
#----------------------------|


from flask import Flask, render_template, request
import requests
import json
import os

from dotenv import load_dotenv
load_dotenv()

TENOR_API_KEY = os.getenv("TENOR_API_KEY")

app = Flask(__name__)

# route the function
@app.route('/')
def index():
    """Return homepage."""
    query = request.args.get("search_gif")
    payload = {
        "q": query,
        "key": "3OGJ9M5CUDUK",
        "limit" : "10"
    }
    data = None
#     # sets the apikey and limit

# request GET api, receive api, returns searched gifs
    r = requests.get("https://api.tenor.com/v1/search", params=payload)
    if r.status_code == 200:
        # load the GIFs using the urls for the smaller GIF sizes
        data = r.json()
        data = data["results"]
    else:
        data = None
    return render_template("index.html", data=data)

    # get the top 10 GIFs for the search term
    # links to terminal
if __name__ == '__main__':
    app.run(debug=True)


import time
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
from itertools import cycle

def get_labels(df, label):
    y_train = df[label]
    return y_train

def get_test_dataset():
    df_test = pd.read_csv("test.csv")
    df_test_labels = pd.read_csv("test_labels.csv")

    #ignoring the comments that have a label of -1;
    #per kaggle: value of -1 indicates it was not used for scoring;
    ignore_idx = df_test_labels.index[df_test_labels["toxic"] == -1].tolist()

    df_test_labels = df_test_labels[df_test_labels["toxic"] != -1]
    df_test = df_test.drop(ignore_idx)
    return df_test, df_test_labels

def main():
    labels = ["toxic", "severe_toxic", "obscene", "threat",
              "insult", "identity_hate"]
    accuracy = []
    df_train = pd.read_csv("train.csv")
    df_test, df_test_labels = get_test_dataset()

    X_train = df_train["comment_text"]
    X_test = df_test["comment_text"]

    vectorizer = TfidfVectorizer()

    train_vectors = vectorizer.fit_transform(X_train)
    test_vectors = vectorizer.transform(X_test)

    fpr = dict()
    tpr = dict()
    roc_auc = dict()

    for i, label in enumerate(labels):
        y_train = get_labels(df_train, label)

        clf = RandomForestClassifier(n_estimators=100)

        clf.fit(train_vectors,np.array(y_train))
        predict = clf.predict(test_vectors)

        y_test = get_labels(df_test_labels,label)
        fpr[i], tpr[i], _ = roc_curve(y_test,predict)
        roc_auc[i] = auc(fpr[i], tpr[i])

        acc_score = round(accuracy_score(y_test,predict),
                          2)
        accuracy.append(acc_score)

        print("Accuracy score on {}: {}"
              .format(label, acc_score))

    fpr["micro"], tpr["micro"], _ = roc_curve(y_test, predict)
    roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])

    all_fpr = np.unique(np.concatenate([fpr[i] for i in range(len(labels))]))

    # Then interpolate all ROC curves at this points
    mean_tpr = np.zeros_like(all_fpr)
    for i in range(len(labels)):
        mean_tpr += np.interp(all_fpr, fpr[i], tpr[i])

    # Finally average it and compute AUC
    mean_tpr /= len(labels)

    fpr["macro"] = all_fpr
    tpr["macro"] = mean_tpr
    roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])

    plt.figure()
    lw = 2
    plt.plot(fpr["micro"], tpr["micro"],
             label='micro-average ROC curve (area = {0:0.2f})'
                   ''.format(roc_auc["micro"]),
             color='deeppink', linestyle=':', linewidth=4)

    plt.plot(fpr["macro"], tpr["macro"],
             label='macro-average ROC curve (area = {0:0.2f})'
                   ''.format(roc_auc["macro"]),
             color='navy', linestyle=':', linewidth=4)

    colors = cycle(['aqua', 'darkorange', 'cornflowerblue'])
    for i, color in zip(range(len(labels)), colors):
        plt.plot(fpr[i], tpr[i], color=color, lw=lw,
                 label='ROC curve of class {0} (area = {1:0.2f})'
                 ''.format(i, roc_auc[i]))

    plt.plot([0, 1], [0, 1], 'k--', lw=lw)
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')

    plt.legend(loc="lower right")
    plt.show()

##    plt.plot(labels, accuracy)
##    plt.xlabel("Toxicity")
##    plt.ylabel("Accuracy")
##    plt.show()

start = time.time()
main()
end = time.time()
print("Time: {}".format(end-start))


import numpy as np
from permutation_tensor import permutation
#This function returns a cross product of two vectors
def cross_product(a,b):
    n = np.zeros(len(a))
    for k in range(0,len(n)):
        for i in range(0,len(a)):
            for j in range(0,len(b)):
                n[k] = n[k]+(permutation(i,j,k)*a[i]*b[j]) #Calculating cross product of two vectors and assigning it to a vector
    return n
a = [1,2,3]
b = [6,8,9]
print(cross_product(a,b))


class Solution(object):
    def compareVersion(self, version1, version2):
        """
        :type version1: str
        :type version2: str
        :rtype: int
        """
        nums1 = version1.split(".")
        nums2 = version2.split(".")
        m, n = len(nums1), len(nums2)

        for i in range(max(m,n)):
            int1 = int(nums1[i]) if i < m else 0
            int2 = int(nums2[i]) if i < n else 0
            if int1 != int2:
                return 1 if int1 > int2 else -1
        return 0


import cv2
import numpy as np
from time import sleep
import pandas as pd

label_file = 'F:/The Stuffs/Awear/Final_Project/myotron_control/data_collection/test_1/video_labels.xlsx'
video_path = 'F:/The Stuffs/Awear/Final_Project/myotron_control/data_collection/test_1/test_3m.mp4'
sim_file = 'F:/The Stuffs/Awear/Final_Project/myotron_control/Myotron_Controll_codes/sim_data/test_windowed.npy'
test_pred_file = 'test_pred_frame.npy'

test_pred_frame = list(np.load(test_pred_file))
for i in range(22):
    test_pred_frame.insert(0,0)
print(len(test_pred_frame))

emg_data = np.load(sim_file)
print('Sim Data = ',emg_data.shape)

labels = pd.read_excel(label_file)['labels']
ts = 180

cap = cv2.VideoCapture(video_path)

property_id = int(cv2.CAP_PROP_FRAME_COUNT)
length = int(cv2.VideoCapture.get(cap, property_id))
print('Total Frames = ', length )
frame_number = int(ts*30)

label_frames = []
dist = int(length/labels.shape[0])

for x in labels:
    for j in range(30):
        label_frames.append(x)
diff = length - len(label_frames)
for i in range(diff):
    label_frames.insert(0,0)
print(len(label_frames))

def ResizeWithAspectRatio(image, width=None, height=None, inter=cv2.INTER_AREA):
    dim = None
    (h, w) = image.shape[:2]

    if width is None and height is None:
        return image
    if width is None:
        r = height / float(h)
        dim = (int(w * r), height)
    else:
        r = width / float(w)
        dim = (width, int(h * r))

    return cv2.resize(image, dim, interpolation=inter)

label_txt = ['Rest','Suplination','Pronation','Flexion','Extension','Radial','Ulnar','Closed Extended','Grasp']

wrist_moves = ['Suplination - CW (rot axis Mid finger)',
               'Pronation - CCW (rot axis Mid finger)',
               'Suplination - CW (rot axis lit finger)',
               'Pronation - CCW (rot axis lit finger)',
               'Flexion - Bend Towards',' Extension - Bend Backward',
               'Radial - Left - CCW',
               'Radial - Righ - CW',
               'Extension with closed hand']

frame_number = 0
while True:
    ret, frame = cap.read() # Read the frame
    print('Frame '+str(frame_number)+'/'+str(length)+' '+label_txt[label_frames[frame_number]])

    # font
    font = cv2.FONT_HERSHEY_SIMPLEX

    # org
    org = (50, 100)

    # fontScale
    fontScale = 3

    # Blue color in BGR
    color = (255, 255, 255)

    # Line thickness of 2 px
    thickness = 5

    # Using cv2.putText() method
    frame = cv2.putText(frame, label_txt[label_frames[frame_number]], org, font,
                   fontScale, color, thickness, cv2.LINE_AA)

    frame = cv2.putText(frame, wrist_moves[test_pred_frame[frame_number]], (50,200), font,
                   2, (0,0,255), thickness, cv2.LINE_AA)

    cv2.imshow('Time['+str(ts)+']',ResizeWithAspectRatio(frame,width=350))
    frame_number = frame_number+1
    if frame_number==length-1:
        break
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.waitKey(0)
cv2.destroyAllWindows()


import boto3
from src.config import YandexS3Config

def get_query_search(query_search: str = None):
    if query_search:
        return query_search.strip()

session = boto3.session.Session()
s3 = session.client(
    service_name='s3',
    endpoint_url='https://storage.yandexcloud.net',
    aws_access_key_id=YandexS3Config.AWS_ACCESS_KEY_ID,
    aws_secret_access_key=YandexS3Config.AWS_SECRET_ACCESS_KEY,
    region_name='ru-central1'
)


"""
seed the database with admins
"""

import sqlite3
conn = sqlite3.connect('hellrpgdb.db')
c = conn.cursor()

c.execute(
	"""CREATE TABLE user (
	id INTEGER PRIMARY KEY,
	username TEXT,
	password TEXT,
	last_played INTEGER
	);"""
)

conn.commit()
conn.close()
print("Your database was created")

# db = 'hellrpgdb.db'

# user = ["Andrew", "Super User", "Cheese", "Andrew.Kolansky@gmail.com" ]

# conn = sqlite3.connect(hellrpgdb.db)
# c = conn.cursor()

# print("Replacing old data")
# c.execute("DELTE FROM user")

# for user in USERS:
# 	c.execute("""INSERT INTO user("user_name",
# 	 "user_type", "password", "email") VALUES(?,?,?,?)
# 		""",(user[0],user[1], user[2], user[3], user[4]))

# conn.commit()
# c.close()

# print("Looks like we're all good")


from django.urls import path
from . import views

app_name = 'learn'
urlpatterns = [
    path('dash', views.dash, name='dash'),
    path('my-account/', views.my_account, name='my-account'),
    path('select-lesson/<int:level>', views.select_lesson, name='select-lesson'),
    path('lesson/<int:level>/<int:id>', views.lesson, name='lesson'),
    path('add-remove-homework/', views.add_remove_homework, name='add-remove-homework'),
    path('homework/', views.homework, name='homework'),
    path("check-username-exists/", views.ajax_check_username_exists, name='check-username-exists'),
    path('adjust-progress/', views.adjust_progress, name='adjust-progress'),
    path('no-cheating/', views.no_cheating, name='no-cheating'),

]


"""
https://www.hackerrank.com/challenges/count-triplets-1/forum?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dictionaries-hashmaps
You are given an array and you need to find number of tripets of indices  such that the elements at those indices are in geometric progression for a given common ratio  and .
"""
from functools import lru_cache
from collections import Counter

@lru_cache(maxsize=100)
def is_power(x, n, p=0):
    if x == 1: return p
    if x < 1: return -1
    return is_power(x/n, n, p+1)

@lru_cache(maxsize=1000)
def deep(x, n, i, last):
    print(f'STATUS: x: {x}, n: {n}, i: {i}, last: {last}')
    if i == 3: return 1
    if not x: return 0
    # special case: triplet empty
    if is_power(x[0], n) > -1 and last == None:
        return deep(x[1:], n, i+1, x[0]) + deep(x[1:], n, i, last)
    # general case
    if is_power(x[0], n) > -1 and is_power(x[0], n) - is_power(last, n) == 1:
        return deep(x[1:], n, i+1, x[0]) + deep(x[1:], n, i, last)
    else:
        return deep(x[1:], n, i, last)

def countTriplets(x, n):
    """the stack frames are not enought for gigantic amounts of data...
    """
    x = tuple(x)
    if not x or not n: return
    return deep(x, n, 0, None)

def countTriplets(arr, r):
    """one paass memory efficient way
    we will prepare counters for the next possible element,
    and when the next element arrives we accumulate the ocurrences of the previous matches
    """
    c_2, c_3 = Counter(), Counter()
    n_triplets = 0
    for e in arr:
        # print(f'arr: {arr}, e: {e}, c_3: {c_3}, c_2: {c_2}, n_triplets: {n_triplets}')
        if e in c_3:
            n_triplets += c_3[e]
        if e in c_2:
            c_3[e*r] += c_2[e]
        c_2[e*r] += 1
    return n_triplets

# test
# test = (1,2,2,4)
# n = 2
test = (1,3,9,9,27,81)
n = 3
print(f'testing with: {test}')
print(f'ans: {countTriplets(test,n)}')


import os
import cv2
import glob
import json
import tqdm
import numpy as np
from scipy.spatial.transform import Slerp, Rotation

import trimesh

import torch
from torch.utils.data import DataLoader

from .utils import get_rays

# ref: https://github.com/NVlabs/instant-ngp/blob/b76004c8cf478880227401ae763be4c02f80b62f/include/neural-graphics-primitives/nerf_loader.h#L50
def nerf_matrix_to_ngp(pose, scale=0.33):
    # for the fox dataset, 0.33 scales camera radius to ~ 2
    new_pose = np.array([
        [pose[1, 0], -pose[1, 1], -pose[1, 2], pose[1, 3] * scale],
        [pose[2, 0], -pose[2, 1], -pose[2, 2], pose[2, 3] * scale],
        [pose[0, 0], -pose[0, 1], -pose[0, 2], pose[0, 3] * scale],
        [0, 0, 0, 1],
    ], dtype=np.float32)
    return new_pose

def visualize_poses(poses, size=0.1):
    # poses: [B, 4, 4]

    axes = trimesh.creation.axis(axis_length=4)
    sphere = trimesh.creation.icosphere(radius=1)
    objects = [axes, sphere]

    for pose in poses:
        # a camera is visualized with 8 line segments.
        pos = pose[:3, 3]
        a = pos + size * pose[:3, 0] + size * pose[:3, 1] + size * pose[:3, 2]
        b = pos - size * pose[:3, 0] + size * pose[:3, 1] + size * pose[:3, 2]
        c = pos - size * pose[:3, 0] - size * pose[:3, 1] + size * pose[:3, 2]
        d = pos + size * pose[:3, 0] - size * pose[:3, 1] + size * pose[:3, 2]

        segs = np.array([[pos, a], [pos, b], [pos, c], [pos, d], [a, b], [b, c], [c, d], [d, a]])
        segs = trimesh.load_path(segs)
        objects.append(segs)

    trimesh.Scene(objects).show()

def get_view_direction(thetas, phis):
    #                   phis [B,]; thetas: [B,]
    # front = 0         0-90
    # side (left) = 1   90-180
    # back = 2          180-270
    # side (right) = 3  270-360
    # top = 4                        0-45
    # bottom = 5                     135-180
    res = np.zeros(phis.shape[0], dtype=np.int64)
    # first determine by phis
    res[phis < (np.pi / 2)] = 0
    res[(phis >= (np.pi / 2)) & (phis < np.pi)] = 1
    res[(phis >= np.pi) & (phis < (3 * np.pi / 2))] = 2
    res[(phis >= (3 * np.pi / 2)) & (phis < (2 * np.pi))] = 3
    # override by thetas
    res[thetas < (np.pi / 4)] = 4
    res[thetas > (3 * np.pi / 4)] = 5
    return res

def rand_poses(size, device, radius=1, theta_range=[np.pi/3, 2 * np.pi/3], phi_range=[0, 2*np.pi]):
    ''' generate random poses from an orbit camera
    Args:
        size: batch size of generated poses.
        device: where to allocate the output.
        radius: camera radius
        theta_range: [min, max], should be in [0, \pi]
        phi_range: [min, max], should be in [0, 2\pi]
    Return:
        poses: [size, 4, 4]
    '''

    def normalize(vectors):
        return vectors / (torch.norm(vectors, dim=-1, keepdim=True) + 1e-10)

    thetas = torch.rand(size, device=device) * (theta_range[1] - theta_range[0]) + theta_range[0]
    phis = torch.rand(size, device=device) * (phi_range[1] - phi_range[0]) + phi_range[0]

    centers = torch.stack([
        radius * torch.sin(thetas) * torch.sin(phis),
        radius * torch.cos(thetas),
        radius * torch.sin(thetas) * torch.cos(phis),
    ], dim=-1) # [B, 3]

    # lookat
    forward_vector = - normalize(centers)
    up_vector = torch.FloatTensor([0, -1, 0]).to(device).unsqueeze(0).repeat(size, 1) # confused at the coordinate system...
    right_vector = normalize(torch.cross(forward_vector, up_vector, dim=-1))
    up_vector = normalize(torch.cross(right_vector, forward_vector, dim=-1))

    poses = torch.eye(4, dtype=torch.float, device=device).unsqueeze(0).repeat(size, 1, 1)
    poses[:, :3, :3] = torch.stack((right_vector, up_vector, forward_vector), dim=-1)
    poses[:, :3, 3] = centers

    return poses

class NeRFDataset:
    def __init__(self, opt, device, type='train', H=128, W=128, radius=3, fovy=90, size=100):
        super().__init__()

        self.opt = opt
        self.device = device
        self.type = type # train, val, test

        self.H = H
        self.W = W
        self.radius = radius
        self.fovy = fovy
        self.size = size

        self.training = self.type in ['train', 'all']
        self.num_rays = self.opt.num_rays if self.training else -1

        fl_y = self.H / (2 * np.tan(np.radians(self.fovy) / 2))
        fl_x = fl_y
        cx = self.H / 2
        cy = self.W / 2
        self.intrinsics = np.array([fl_x, fl_y, cx, cy])

        # [debug] visualize poses
        #poses = rand_poses(100, 'cpu', radius=self.radius).detach().numpy()
        #visualize_poses(poses)

    def collate(self, index):

        B = len(index) # always 1

        # random pose
        poses = rand_poses(B, self.device, radius=self.radius)

        # sample a low-resolution but full image for CLIP
        rays = get_rays(poses, self.intrinsics, self.H, self.W, -1)

        return {
            'H': self.H,
            'W': self.W,
            'rays_o': rays['rays_o'],
            'rays_d': rays['rays_d'],
        }

    def dataloader(self):
        loader = DataLoader(list(range(self.size)), batch_size=1, collate_fn=self.collate, shuffle=self.training, num_workers=0)
        loader._data = self # an ugly fix... we need to access error_map & poses in trainer.
        return loader


from typing import Mapping, MutableMapping, Optional, Sequence

from tfx import types
from tfx.orchestration import metadata
from tfx.orchestration.portable.mlmd import execution_lib
from tfx.proto.orchestration import execution_result_pb2

from ml_metadata.proto import metadata_store_pb2

def publish_cached_execution(
    metadata_handler: metadata.Metadata,
    contexts: Sequence[metadata_store_pb2.Context],
    execution_id: int,
    output_artifacts: Optional[MutableMapping[str,
                                              Sequence[types.Artifact]]] = None,
) -> None:
  """Marks an existing execution as using cached outputs from a previous execution.

  Args:
    metadata_handler: A handler to access MLMD.
    contexts: MLMD contexts to associated with the execution.
    execution_id: The id of the execution.
    output_artifacts: Output artifacts of the execution. Each artifact will be
      linked with the execution through an event with type OUTPUT.
  """
  [execution] = metadata_handler.store.get_executions_by_id([execution_id])
  execution.last_known_state = metadata_store_pb2.Execution.CACHED

  execution_lib.put_execution(
      metadata_handler,
      execution,
      contexts,
      input_artifacts=None,
      output_artifacts=output_artifacts)

def publish_succeeded_execution(
    metadata_handler: metadata.Metadata,
    execution_id: int,
    contexts: Sequence[metadata_store_pb2.Context],
    output_artifacts: Optional[MutableMapping[str,
                                              Sequence[types.Artifact]]] = None,
    executor_output: Optional[execution_result_pb2.ExecutorOutput] = None
) -> None:
  """Marks an existing execution as success.

  Also publishes the output artifacts produced by the execution. This method
  will also merge the executor produced info into system generated output
  artifacts. The `last_know_state` of the execution will be changed to
  `COMPLETE` and the output artifacts will be marked as `LIVE`.

  Args:
    metadata_handler: A handler to access MLMD.
    execution_id: The id of the execution to mark successful.
    contexts: MLMD contexts to associated with the execution.
    output_artifacts: Output artifacts skeleton of the execution, generated by
      the system. Each artifact will be linked with the execution through an
      event with type OUTPUT.
    executor_output: Executor outputs. `executor_output.output_artifacts` will
      be used to update system-generated output artifacts passed in through
      `output_artifacts` arg. There are three contraints to the update: 1. The
        keys in `executor_output.output_artifacts` are expected to be a subset
        of the system-generated output artifacts dict. 2. An update to a certain
        key should contains all the artifacts under that key. 3. An update to an
        artifact should not change the type of the artifact.

  Raises:
    RuntimeError: if the executor output to a output channel is partial.
  """
  output_artifacts = output_artifacts or {}
  if executor_output:
    if not set(executor_output.output_artifacts.keys()).issubset(
        output_artifacts.keys()):
      raise RuntimeError(
          'Executor output %s contains more keys than output skeleton %s.' %
          (executor_output, output_artifacts))
    for key, artifact_list in output_artifacts.items():
      if key not in executor_output.output_artifacts:
        continue
      updated_artifact_list = executor_output.output_artifacts[key].artifacts
      if len(artifact_list) != len(updated_artifact_list):
        raise RuntimeError(
            'Partially update an output channel is not supported')

      for original, updated in zip(artifact_list, updated_artifact_list):
        if original.type_id != updated.type_id:
          raise RuntimeError('Executor output should not change artifact type.')
        original.mlmd_artifact.CopyFrom(updated)

  # Marks output artifacts as LIVE.
  for artifact_list in output_artifacts.values():
    for artifact in artifact_list:
      artifact.mlmd_artifact.state = metadata_store_pb2.Artifact.LIVE

  [execution] = metadata_handler.store.get_executions_by_id([execution_id])
  execution.last_known_state = metadata_store_pb2.Execution.COMPLETE

  execution_lib.put_execution(
      metadata_handler, execution, contexts, output_artifacts=output_artifacts)

def publish_failed_execution(metadata_handler: metadata.Metadata,
                             contexts: Sequence[metadata_store_pb2.Context],
                             execution_id: int) -> None:
  """Marks an existing execution as failed.

  Args:
    metadata_handler: A handler to access MLMD.
    contexts: MLMD contexts to associated with the execution.
    execution_id: The id of the execution.
  """
  [execution] = metadata_handler.store.get_executions_by_id([execution_id])
  execution.last_known_state = metadata_store_pb2.Execution.FAILED

  execution_lib.put_execution(metadata_handler, execution, contexts)

def publish_internal_execution(
    metadata_handler: metadata.Metadata,
    contexts: Sequence[metadata_store_pb2.Context],
    execution_id: int,
    output_artifacts: Optional[MutableMapping[str,
                                              Sequence[types.Artifact]]] = None
) -> None:
  """Marks an exeisting execution as as success and links its output to an INTERNAL_OUTPUT event.

  Args:
    metadata_handler: A handler to access MLMD.
    contexts: MLMD contexts to associated with the execution.
    execution_id: The id of the execution.
    output_artifacts: Output artifacts of the execution. Each artifact will be
      linked with the execution through an event with type INTERNAL_OUTPUT.
  """
  [execution] = metadata_handler.store.get_executions_by_id([execution_id])
  execution.last_known_state = metadata_store_pb2.Execution.COMPLETE

  execution_lib.put_execution(
      metadata_handler,
      execution,
      contexts,
      output_artifacts=output_artifacts,
      output_event_type=metadata_store_pb2.Event.INTERNAL_OUTPUT)

def register_execution(
    metadata_handler: metadata.Metadata,
    execution_type: metadata_store_pb2.ExecutionType,
    contexts: Sequence[metadata_store_pb2.Context],
    input_artifacts: Optional[MutableMapping[str,
                                             Sequence[types.Artifact]]] = None,
    exec_properties: Optional[Mapping[str, types.Property]] = None,
) -> metadata_store_pb2.Execution:
  """Registers a new execution in MLMD.

  Along with the execution:
  -  the input artifacts will be linked to the execution.
  -  the contexts will be linked to both the execution and its input artifacts.

  Args:
    metadata_handler: A handler to access MLMD.
    execution_type: The type of the execution.
    contexts: MLMD contexts to associated with the execution.
    input_artifacts: Input artifacts of the execution. Each artifact will be
      linked with the execution through an event.
    exec_properties: Execution properties. Will be attached to the execution.

  Returns:
    An MLMD execution that is registered in MLMD, with id populated.
  """
  execution = execution_lib.prepare_execution(
      metadata_handler, execution_type, metadata_store_pb2.Execution.RUNNING,
      exec_properties)
  return execution_lib.put_execution(
      metadata_handler, execution, contexts, input_artifacts=input_artifacts)


import torch
import src.config as config

def process_data(description, target, tokenizer, max_len):

    tokens_desc = tokenizer.tokenize(description)
    # Account for [CLS] and [SEP] with "- 2"
    if len(tokens_desc) > max_len - 2:
        tokens_desc = tokens_desc[:(max_len - 2)]

    # The convention in BERT is:
    # (a) For sequence pairs:
    #  tokens:   [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
    #  type_ids: 0   0  0    0    0     0       0 0    1  1  1  1   1 1
    # (b) For single sequences:
    #  tokens:   [CLS] the dog is hairy . [SEP]
    #  type_ids: 0   0   0   0  0     0 0
    #
    # Where "type_ids" are used to indicate whether this is the first
    # sequence or the second sequence. The embedding vectors for `type=0` and
    # `type=1` were learned during pre-training and are added to the wordpiece
    # embedding vector (and position vector). This is not *strictly* necessary
    # since the [SEP] token unambigiously separates the sequences, but it makes
    # it easier for the model to learn the concept of sequences.
    #
    # For classification tasks, the first vector (corresponding to [CLS]) is
    # used as as the "sentence vector". Note that this only makes sense because
    # the entire model is fine-tuned.
    tokens = ["[CLS]"] + tokens_desc + ["[SEP]"]
    segment_id = [0] * len(tokens)

    input_id = tokenizer.convert_tokens_to_ids(tokens)

    # The mask has 1 for real tokens and 0 for padding tokens. Only real
    # tokens are attended to.
    input_mask = [1] * len(input_id)

    # Zero-pad up to the sequence length.
    padding = [0] * (max_len - len(input_id))
    input_id += padding
    input_mask += padding
    segment_id += padding

    if target:
        label_ids = list(target.astype(float))
        data_out = {
            'input_id':    input_id,
            'input_mask':  input_mask,
            'segment_id':  segment_id,
            'label_ids':   label_ids
        }
    else:
        data_out = {
            'input_id':   input_id,
            'input_mask': input_mask,
            'segment_id': segment_id
        }

    return data_out

class CustomDataset(torch.utils.data.Dataset):

    def __init__(self, description, target=None):
        self.description = description
        self.target = target
        self.tokenizer = config.TOKENIZER
        self.max_len = config.MAX_SEQ_LENGTH

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

    def __getitem__(self, item):
        if self.target:
            data = process_data(
                    self.description[item],
                    self.target[item],
                    self.tokenizer,
                    self.max_len
            )
            output = {
                'input_id':     torch.tensor(data["input_id"], dtype=torch.long),
                'input_mask':   torch.tensor(data["input_mask"], dtype=torch.long),
                'segment_id':   torch.tensor(data["segment_id"], dtype=torch.long),
                'label_ids':    torch.tensor(data["label_ids"], dtype=torch.long),
            }
        else:
            data = process_data(
                    self.description[item],
                    self.target,  # passing targets as None
                    self.tokenizer,
                    self.max_len
            )
            output = {
                'input_id':   torch.tensor(data["input_id"], dtype=torch.long),
                'input_mask': torch.tensor(data["input_mask"], dtype=torch.long),
                'segment_id': torch.tensor(data["segment_id"], dtype=torch.long)
            }

        return output


from .settings import *
from dotenv import load_dotenv

load_dotenv('.env.production')

# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DEBUG = False

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

TORTOISE_INIT = {
    "db_url": f"postgres://{os.getenv('POSTGRES_USER')}:{os.getenv('POSTGRES_PASSWORD')}@{os.getenv('POSTGRES_HOST')}:{os.getenv('POSTGRES_PORT')}/{os.getenv('POSTGRES_DB')}",
    "modules": {
        "models": ["chat.tortoise_models"]
     }
}

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [(os.getenv('REDIS_HOST'), os.getenv('REDIS_PORT'))],
        },
    },
}


from django.contrib import admin

from sagepaypi.models import CardIdentifier, Transaction, TransactionResponse

class ReadOnlyAdmin:

    def has_add_permission(self, request):
        return False

    def has_change_permission(self, request, obj=None):
        return False

class CardIdentifierAdmin(ReadOnlyAdmin, admin.ModelAdmin):
    list_display = [
        'pk',
        'display_text',
        'first_name',
        'last_name',
        'created_at'
    ]

class TransactionResponseAdmin(ReadOnlyAdmin, admin.StackedInline):
    model = TransactionResponse

    def has_delete_permission(self, request, obj=None):
        return False

class TransactionAdmin(ReadOnlyAdmin, admin.ModelAdmin):
    inlines = [
        TransactionResponseAdmin
    ]
    list_display = [
        'pk',
        'type',
        'amount',
        'currency',
        'status',
        'transaction_id',
        'instruction',
        'created_at'
    ]

admin.site.register(CardIdentifier, CardIdentifierAdmin)
admin.site.register(Transaction, TransactionAdmin)


# 931. Minimum Falling Path Sum
class Solution:
    def minFallingPathSum(self, A: List[List[int]]) -> int:
        '''
        DP.
        dp[r][c] = A[r][c] + min(dp[r-1][c], dp[r-1][c-1], dp[r-1][c+1]),
        beware of the boundaries.
        If we are not allowed to modify input, we can use 2 1-D arrays.
        '''
        for row in range(1, len(A)):
            for col in range(len(A[0])):
                col1 = max(0, col - 1)
                col2 = min(col + 1, len(A[0]) - 1)
                A[row][col] += min(A[row-1][col], A[row-1][col1], A[row-1][col2])
        return min(A[-1])


# classify data w/o linear correspondence
# works well for hi-dimensional data
# hard margins:
# divides data into multiple classes using a hyper-plane
# picks 2 points as support vectors with identical dist.
# usually with the greatest possible margin
# kernels bring in another dimension (ie: 2D to 3D)
# soft margins: allows outliers so hyperplane would perform better

import sklearn
from sklearn import datasets
from sklearn import svm
from sklearn import metrics

cancer = datasets.load_breast_cancer()

# print(cancer.feature_names)
# sees all features
print(cancer.target_names)
# sees all targets

x = cancer.data
y = cancer.target

x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x, y, test_size=0.2)

print(y_train)
# shows data as 0 or 1
# 0 is malignant
# 1 is benign

classes = ["malignant", "benign"]

clf = svm.SVC(kernel="linear", C=2)
# kernel is the type of eq.
# C is how big the soft margin is going to be
clf.fit(x_train, y_train)

y_pred = clf.predict(x_test)

acc = metrics.accuracy_score(y_test, y_pred)

print(acc)

Support Vector Machines (SVM)/Sklearn.py


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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Document',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('text', models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name='Filth',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('beg', models.IntegerField()),
                ('end', models.IntegerField()),
                ('text', models.CharField(max_length=255)),
                ('document', models.ForeignKey(to='django_scrubadub.Document')),
            ],
        ),
        migrations.CreateModel(
            name='FilthType',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=255)),
                ('detector_cls', models.CharField(max_length=255)),
                ('parameters', models.TextField()),
            ],
        ),
        migrations.AddField(
            model_name='filth',
            name='type',
            field=models.ForeignKey(to='django_scrubadub.FilthType'),
        ),
    ]