from turtle import Turtle

from constants import SCORE_POSITION, ALIGNMENT, FONT

class ScoreBoard(Turtle):
    """Class represents Score Board of the Turtle Crossing game"""
    def __init__(self):
        super().__init__()
        self.score = 0
        self.penup()
        self.hideturtle()
        self.goto(SCORE_POSITION)
        self.update()

    def increase_score(self):
        self.score += 1
        self.update()

    def update(self):
        self.clear()
        self.write(f'Level {self.score}', align=ALIGNMENT, font=FONT)

    def game_over(self):
        self.clear()
        self.goto(0, 0)
        self.write(f'Game Over. \nThe final score: {self.score}.', align=ALIGNMENT, font=FONT)


import sys
import pygame
from time import sleep
from setting_game import Settings
from chicken_ship import Ship
from bullet import Bullet
from chicken import Chicken
from game_start import GameStart
from score_board import ScoreBoard
from button import Button

class ChickenManage:
    def __init__(self):
        """Initialize the manage game"""
        pygame.init()
        # instance of settings
        self.settings = Settings()

        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        self.bg_color = self.settings.background_color
        pygame.display.set_caption("Shooting Chicken")
        self.starts = GameStart(self)
        self.score_board = ScoreBoard(self)
        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()
        self.chickens = pygame.sprite.Group()
        self.file_save = 'score.txt'
        self._create_fleet()
        self.HIGH_SCORE = []
        self.play_button = Button(self, "Play")

    def run_game(self):
        """Start the game"""
        while True:
            self._check_events()
            if self.starts.game_active:
                self.ship.update()
                self._update_bullets()
                self._update_chicken()
            # else:
            # self.file_handler_write(self.HIGH_SCORE)
            self._update_screen()

    def _check_events(self):
        # Watch keyboard the mouse event
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                self._check_play_button(mouse_pos)
            elif event.type == pygame.KEYDOWN:
                self._check_events_keydown(event)
            elif event.type == pygame.KEYUP:
                self._check_events_keyup(event)

    def _check_events_keydown(self, event):
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        elif event.key == pygame.K_q:
            sys.exit()
        elif event.key == pygame.K_SPACE or event.key == pygame.K_t:
            self._fire_bullet()

    def _check_events_keyup(self, event):
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False
        elif event.key == pygame.K_SPACE:
            self._fire_bullet()

    def _check_play_button(self, mouse_pos):
        """Colllist mouse button"""
        buton_clicked = self.play_button.rect.collidepoint(mouse_pos)
        if buton_clicked and not self.starts.game_active:
            # reset dinamic speed
            self.settings._dinamic_settings()
            # reset the game
            self.starts.reset_starts()
            self.starts.game_active = True

            # restore score and level
            self.starts.score = 0
            self.score_board.prep_score()

            self.starts.level_start = 0
            self.score_board.prep_level()

            self.score_board.prep_ship()
            # get rid of any remaining chickens and buulet
            self.chickens.empty()
            self.bullets.empty()

            self._create_fleet()
            self.ship.center_ship()

            pygame.mouse.set_visible(True)

    def _fire_bullet(self):
        """Create a new bullet and add it the group"""
        new_bullet = Bullet(self)
        self.bullets.add(new_bullet)

    def _update_bullets(self):
        self.bullets.update()
        # Get grid of bullet
        for bullet in self.bullets.copy():
            if bullet.rect.bottom <= 0:
                self.bullets.remove(bullet)
        self._check_bullet_chicken_collisions()

    def _check_bullet_chicken_collisions(self):
        # check for any bullet that have hitss chicken
        collisions = pygame.sprite.groupcollide(self.bullets, self.chickens, True, True)
        if collisions:
            for chicken in collisions.values():
                self.starts.score += self.settings.chicken_score * len(chicken)
            self.score_board.prep_score()
            self.score_board._check_high_score()
        if not self.chickens:
            # Desttroy bullet and crete new fleet
            self.bullets.empty()
            self._create_fleet()
            self.settings._increase_speed()
            # _increase_levels
            self.starts.level_start += 1
            self.score_board.prep_level()

    def _create_fleet(self):
        new_chicken = Chicken(self)
        chicken_width, chicken_height = new_chicken.rect.size
        # get x
        available_space_x = self.settings.screen_width - (2 * chicken_width)
        number_of_chicken_x = available_space_x // (2 * chicken_width)

        # get hight ship and get y
        ship_height = self.ship.rect.height
        available_space_y = self.settings.screen_height - 5 * chicken_height - ship_height
        number_of_rows = available_space_y // (2 * chicken_height)

        # Create full chicken
        for row in range(number_of_rows):
            for column in range(number_of_chicken_x):
                self._create_chicken(column, row)

    def _create_chicken(self, number_of_chicken_x, row_number):
        new_chicken = Chicken(self)
        chicken_width, chicken_height = new_chicken.rect.size
        new_chicken.x = chicken_width + 2 * chicken_width * number_of_chicken_x + 20
        new_chicken.rect.x = new_chicken.x
        new_chicken.rect.y = new_chicken.rect.height * 2 + 2 * new_chicken.rect.height * row_number
        self.chickens.add(new_chicken)

    def _update_chicken(self):
        self._check_fleet_edges()
        self.chickens.update()
        # chicken collisions with ship
        if pygame.sprite.spritecollideany(self.ship, self.chickens):
            self._ship_hit()
        self._check_chiken_edges_bottom()

    def _check_chiken_edges_bottom(self):
        screen_rect = self.screen.get_rect()
        for chicken in self.chickens.sprites():
            if chicken.rect.bottom >= screen_rect.bottom:
                self._ship_hit()
                break

    def _ship_hit(self):
        # decrease ships left
        if self.starts.ships_left > 0:
            self.starts.ships_left -= 1
            self.score_board.prep_ship()

            self.chickens.empty()
            self.bullets.empty()

            self._create_fleet()
            self.ship.center_ship()
            sleep(0.5)
        else:
            self.starts.game_active = False
            pygame.mouse.set_visible(True)

    def _check_fleet_edges(self):
        for chicken in self.chickens.sprites():
            if chicken.check_edges():
                self._change_fleet_direction()
                break

    def _change_fleet_direction(self):
        # fleet drop
        for chicken in self.chickens.sprites():
            chicken.rect.y += self.settings.fleet_drop_speed
        # chang direction fleet to left and right
        self.settings.fleet_direction *= -1

    def _update_screen(self):
        # Redraw screen during game begin
        self.screen.fill(self.bg_color)
        self.ship.blitme()

        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        self.chickens.draw(self.screen)
        self.score_board.show_score()
        if not self.starts.game_active:
            self.play_button.draw_button()
        # Make the most recenttly draw screen
        pygame.display.flip()

    # def file_handler_read(self):
    # with open(self.file_save, mode='r', encoding='utf-8') as file_opject:

    # def file_handler_write(self , HIGH_SCORE):
    #     with open(self.file_save, "a+", encoding='utf-8') as file_opject:
    #         HIGH_SCORE.append(self.starts.high_score)
    #         file_opject.write(f"{self.starts.high_score}")

if __name__ == "__main__":
    cs = ChickenManage()
    cs.run_game()


from urllib.request import urlopen

url = "http://olympus.realpython.org/profiles/dionysus"

page = urlopen(url)

html = page.read().decode('utf-8')

start_tag = "
Name: "
end_tag = "
"

start_index = html.find(start_tag) + len(start_tag)
end_index = html.find(end_tag)

print(html[start_index:end_index])

start_tag =  "Favorite Color: "
end_tag = "\n</center"

start_index = html.find(start_tag) + len(start_tag)
end_index = html.find(end_tag)

print(html[start_index:end_index])


import os, sys, joblib
import pandas as pd, numpy as np
from itertools import chain
from glob import glob
import math, pdb
from tqdm import tqdm
import matplotlib as mpl
import matplotlib.pyplot as plt
sys.path.append('./models')
from training import format_data, split_and_shuffle

def smoothing_corr():
    data = pd.read_pickle('./data/train_data.pd')
    grouped = data.groupby(['Location'])

    max_lag=20

    plt_data = np.zeros([max_lag, 4])

    for lag in tqdm(range(0, max_lag), desc='Progress'):
        for group in grouped:
            max_temp = group[1].Max_Temp.astype('float')
            min_temp = group[1].Min_Temp.astype('float')
            precip = group[1].Precip.astype('float')
            humidity = group[1].Humidity.astype('float')
            #mols = group[1].MoLS.astype('float')
            plt_data[lag,0]+=max_temp.autocorr(lag=lag)
            plt_data[lag,1]+=min_temp.autocorr(lag=lag)
            plt_data[lag,2]+=precip.autocorr(lag=lag)
            plt_data[lag,3]+=humidity.autocorr(lag=lag)
            #plt_data[lag,4]+=mols.autocorr(lag=lag)

    plt_data/=len(grouped)

    plt.figure()
    plt.plot(plt_data)
    plt.xticks(np.arange(0,max_lag,2))
    plt.xlabel('Lag (Days)')
    plt.ylabel('Average Sample Autocorrelation')
    plt.legend(['Max. Temp.', 'Min. Temp.', 'Precip.', 'RH'])#, 'MoLS'])
    plt.show()
    return

def shuffling_corr():
    data = pd.read_pickle('./data/train_data.pd')
    scaler = joblib.load('./data/data_scaler.gz')

    data = format_data(data, data_shape=[90,4], samples_per_city=2200, scaler=scaler)
    X, y = split_and_shuffle(data)
    X = np.reshape(X, (X.shape[0]*X.shape[1], X.shape[2]))

    max_temp = pd.Series(X[:,0])
    min_temp = pd.Series(X[:,1])
    precip = pd.Series(X[:,2])
    humidity = pd.Series(X[:,3])

    max_lag = 360

    plt_data = np.zeros([max_lag, 4])

    for lag in tqdm(range(0, max_lag), desc='Progress'):
        #mols = group[1].MoLS.astype('float')
        plt_data[lag,0]=max_temp.autocorr(lag=lag)
        plt_data[lag,1]=min_temp.autocorr(lag=lag)
        plt_data[lag,2]=precip.autocorr(lag=lag)
        plt_data[lag,3]=humidity.autocorr(lag=lag)
        #plt_data[lag,4]+=mols.autocorr(lag=lag)

    plt.figure()
    plt.plot(plt_data)
    plt.xticks(np.arange(0,max_lag,50))
    plt.xlabel('Lag (Days)')
    plt.ylabel('Autocorrelation')
    plt.legend(['Max. Temp.', 'Min. Temp.', 'Precip.', 'RH'])
    plt.show()

    return

if __name__ == '__main__':
    font={'size':16}
    plt.rc('font',**font)
    smoothing_corr()


from numpy import array, where, append

class CacheManager:
    def __init__(self):
        self.v = array([])
        self.i = array([])

    def add(self, name, value):
        self.i = append(self.i, name)
        self.v = append(self.v, value)

    def exists(self, name):
        try:
            t = where(self.i == name)[0][0]
            del t
            return True
        except:
            return False

    def get(self, name):
        try:
            i = where(self.i == name)[0][0]
            return self.v[i]
        except:
            return None

    def clear(self, dont_restore=False):
        del self.v, self.i
        if dont_restore: return
        self.v = array([])
        self.i = array([])


import os

from .utils import astrometric_utils

from drizzlepac import tweakreg

def align(expnames, **kwargs):
    """ Align exposures to a GAIA-based source catalog

    Parameters
    ===========
    expnames : str or list of strings
        Filename of exposure or list of filenames to be aligned to GAIA catalog

    """
    shift_name = kwargs.get('shift_name','shifts_gaia.txt')
    updatehdr = kwargs.get('updatehdr', False)
    wcsname = kwargs.get('wcsname', "TWEAK_GAIA")
    searchrad = kwargs.get('searchrad', 15.0)
    searchunits = kwargs.get('searchunits', 'arcseconds')
    ref_cat_file = kwargs.get('output', None)

    gaia_catalog = astrometric_utils.create_astrometric_catalog(expnames, **kwargs)
    gaia_catalog.write(ref_cat_file, format='ascii.no_header')

    if len(gaia_catalog) > 6:
        fitgeometry = 'general'
    else:
        fitgeometry = 'shift'

    tweakreg.TweakReg(expnames,
                      shiftfile=True,
                      interactive=False,
                      clean=True,
                      see2dplot=False,
                      updatehdr=updatehdr,
                      wcsname=wcsname,
                      outshifts=shift_name,
                      outwcs = shift_name.replace('.txt','_wcs.fits'),
                      refcat=ref_cat_file,
                      searchrad=searchrad,
                      searchunits=searchunits,
                      fitgeometry=fitgeometry)
    return gaia_catalog


import logging
from datetime import datetime

import requests

from bot.nintendo.util import get_categories
from bot.nintendo.util import get_game_id

from commons.classes import Game

from commons.config import COUNTRIES
from commons.config import REGIONS
from commons.config import SYSTEMS

from commons.keys import ALIAS
from commons.keys import API
from commons.keys import EU
from commons.keys import REGION
from commons.keys import WEBSITE

LOG = logging.getLogger('nintendo.eu')

EUROPE = REGIONS[EU]

def fetch_games(system):
    start = 0
    limit = 200

    while True:
        LOG.info('Loading {} games (from {} to {})'.format(system, start, start + limit))

        url = EUROPE[API].format(system=SYSTEMS[system][ALIAS][EU], start=start, limit=limit)
        response = requests.get(url)

        json = response.json().get('response').get('docs', [])

        if not len(json):
            break

        for game in json:
            yield game

        start = start + limit

def list_games(system):
    for data in fetch_games(system):
        title = data.get('title')

        if not data.get('nsuid_txt'):
            # LOG.info('{} has no nsuid'.format(title))
            continue

        for product_id in data.get('product_code_txt', []):
            if '-' not in product_id:
                break
        else:
            LOG.info('{} is not a valid game'.format(title))
            continue

        # Getting nsuids for switch (7) or 3ds (5)
        nsuid = [code for code in data['nsuid_txt'] if code[0] in ['5', '7']][0]
        game_id = get_game_id(nsuid=nsuid, game_id=product_id)

        game = Game(_id=game_id, system=system)

        game.titles[EU] = title
        game.nsuids[EU] = nsuid
        game.release_dates[EU] = datetime.strptime(data.get('dates_released_dts')[0][:10], '%Y-%m-%d')

        game.categories = get_categories(data.get('game_categories_txt', []))

        game.free_to_play = data.get('price_sorting_f', 1) == 0

        game.published_by_nintendo = data.get('publisher', '') == 'Nintendo'
        game.number_of_players = data.get('players_to', 0)

        slug = data['url'].rsplit('/', 1)[-1]

        for country, details in COUNTRIES.items():
            if details[REGION] == EU and WEBSITE in details:
                game.websites[country] = details[WEBSITE].format(slug)

        yield game


"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        this.val = val
        this.left, this.right = None, None
Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
"""
from collections import deque
class Solution:
    # @param {TreeNode} root the root of binary tree
    # @return {ListNode[]} a lists of linked list
    def binaryTreeToLists(self, root):
        # Write your code here
        if not root:
            return []
        queue = deque([root])
        dummy = ListNode(-1)
        last = None
        res = []
        while queue:
            last = dummy
            for i in range(len(queue)):
                curr = queue.popleft()
                new_node = ListNode(curr.val)
                last.next = new_node
                last = last.next
                if curr.left:
                    queue.append(curr.left)
                if curr.right:
                    queue.append(curr.right)
            res.append(dummy.next)
        return res

2BFS/242. Convert Binary Tree to Linked Lists by Depth.py


import doctest
import unittest

import sys

sys.path.append('../yurlungur')

import yurlungur as yr
from yurlungur.core.env import installed, Substance

@unittest.skipUnless(installed("substance"), "Substance is not found")
class TestMaya(unittest.TestCase):
    def test_env(self):
        assert Substance()

    def test_maya(self):
        standalone.mayapy("import yurlungur as yr; print yr.name")

    @unittest.skip("only runtime")
    def test_cmds(self):
        uniform = yr.YNode().create("uniform")

        node = yr.YNode("Directional Warp")
        print(node, node.attr("$pixelsize"), node.attrs)
        normal = yr.YNode("Normal")
        print(normal)

        node.connect("unique_filter_output", normal, "inputNodeOutput.connector")
        print(node.parent())
        node.disconnect("unique_filter_output")
        print(node.parent())

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


#!/usr/bin/env python3
# phage_philter.py
import sys, argparse, os

# Setup program parser
parser = argparse.ArgumentParser()

parser.add_argument("-i", help="Input file", required=True)
parser.add_argument("-acc", "--accessory", nargs="?", help="Accessory file containing metadata (imgvr) or genera (virus)")
parser.add_argument("-gbk", "--genbank", help="File containing GenBank accession numbers")
parser.add_argument("-nc", "--refseq", help="File containing RefSeq accession numbers (NC_)")

args = parser.parse_args()

#######################################################################################################
# Setup various variables
delim = "\t"

phage_families = list(["Ackermannviridae","Myoviridae","Siphoviridae","Podoviridae", "Lipothrixviridae",\
 "Clavaviridae", "Corticoviridae", "Cystoviridae","Guttaviridae", "Inoviridae", "Leviviridae", "Microviridae",\
 "Plasmaviridae", "Pleolipoviridae", "Portogloboviridae", "Sphaerolipoviridae", "Spiraviridae", "Tectiviridae",\
 "Tristromaviridae", "Turriviridae"])
# Archaeal phage families: Ampullaviridae; Bicaudaviridae; Fuselloviridae; Globuloviridae; Rudiviridae; ~Tectiviridae (shared)

phages = ("phage", "PHAGE", "Phage")
#######################################################################################################
# Identify database
database = ((args.i).split("/")[-1]).split("_")[0]
taxon = (args.i).split("_")[1] # Not relevant for imgvr, which will just return "id" = UViG

#######################################################################################################
# Open input file from which phages will be extracted
try:
	infile = open(args.i, "r")
except IOError as err:
	sys.stderr.write("Error while loading file: {}\n".format(str(err)))
	sys.exit(1)

#######################################################################################################
# Acquire column names and index for row ([0])
header = (infile.readline().rstrip()).split(delim)
phage_list = list() # List storing index of phages
virus_list = list() # List storing index of other viruses
phage_list.append(0) # Add "sample" to phageset, automatically added to virus_list

#######################################################################################################
# Use detected database to retrieve relevant information
if database == "virus" or database == "kvit":
	sys.stdout.write("\"{}\" file is queried for filtering..\n".format(database))

#	genus_list = set()
#	identity = str.maketrans("","", "\"'[]") # used to clean names

	# Detects taxonomical level
	if taxon == "id": # Strain level
		sys.stdout.write("Detected strain level file... Filtering on accession numbers.\n")

		if args.refseq is not None:
			ncfile = open(args.refseq, "r")
		else:
			sys.stderr.write("RefSeq accession numbers required for strain detection...\nTerminating!")
			sys.exit(1)

		accessions = set()
		# The file from RefSeq contains only accession numbers (NC_XXXX)
		for line in ncfile:
			accession_num = (line.rstrip()).split()[0]
			accessions.add(accession_num)
		ncfile.close()

		if database == "kvit":
			try:
				if args.genbank is not None:
					gbkfile = open(args.genbank, "r")
				else:
					sys.stdout.write("Both RefSeq and GenBank accession numbers required for strain detection...\nTerminating!")
					sys.exit(1)

			except IOError as err:
				sys.stderr.write("Error while opening file: {}\n".format(str(err)))
				sys.exit(1)

			# The file from GenBank contains metadata
			for line in gbkfile:
				contents = line.strip().split(delim)
				accession_num = contents[0].split(".")[0] # Accession number excluding version
				accessions.add(accession_num)
			gbkfile.close()

		# Compare inputs
		for i,item in enumerate(header):
			item = item.split(".")[0] # Discard the accession version
			if item in accessions:
				#print("Phage: ", item)
				phage_list.append(i)
			elif item not in accessions:
				#print("Not phage: ", item)
				virus_list.append(i)

#	# Not used in project
#	if taxon == "species":
#		sys.stdout.write("Detected species level file...\n")
#
#		# for hte vira
#		if args.accessory is not None:
#			# Extracts all genera (and other stuff) from a taxonomy file.
#			sys.stdout.write("Trying to find phages from bacterial genera...\n")
#			try:
#				taxfile = open(args.accessory, "r")
#			except IOError as err:
#				sys.stderr.write("Error while opening file: {}\n".format(str(err)))
#				sys.exit(1)
#
#			# Only difference is naming of species
#			if database == "virus":
#				for line in taxfile:
#					genus_virus = (line.split(" ")[0]).translate(identity) + " virus"
#					genus_list.add(genus_virus)
#
#			elif database == "kvit":
#				for line in taxfile:
#					genus_virus = (line.split(" ")[0]).translate(identity) + "virus"
#					genus_list.add(genus_virus)
#
#		for i,item in enumerate(header):
#			# Check for phage in entry
#			for phage in phages:
#				if phage in item:
#					phage_list.append(i)
#
#			# Check for bacterial "<genus> virus" in entry
#			for genus in genus_list:
#				if genus in item:
#					phage_list.append(i)
#
#
#	# Not used in project
#	if taxon == "family":
#		sys.stdout.write("Detected family level file...\n")
#
#		for i,item in enumerate(header):
#			for phage_fam in phage_families:
#				if phage_fam in item:
#					phage_list.append(i)

#######################################################################################################
elif database == "imgvr":
	sys.stdout.write("\"{}\" file is being filtered...\n".format(database))

	if args.accessory is None:
		sys.stderr.write("### Error! Filtering IMG/VR requires a file containing metadata using flag \"-acc\"! ###\nTerminating...\n")
		sys.exit(1)

	else:
		sys.stdout.write("Acquiring data on bacterial phages from IMG/VR metadata...\n")
		acc_file = open(args.accessory, "r") # args.accessory = IMGVR_all_Sequence_information.tsv
		acc_header = acc_file.readline().split(delim)

		# Find index which indicate bacterial host origin
		for i,item in enumerate(acc_header):
			if item == "Host_domain":
				index = i
				break

		print("Creating UViG set...")
		uvig_set = set() # Entries of IMG/VR are denoted UViG (uncultivated viral genomes)

		# Make set of phage UViGs
		for line in acc_file:
			line = line.split(delim)
			if line[index] == "Bacteria":
				uvig_set.add(line[0])

		acc_file.close()

		# Query header items against the UViG set; fast lineook-up
		sys.stdout.write("Filtering bacterial phage UViGs from {}...\n".format((args.i).split("/")[-1]))
		for i,item in enumerate(header):
			if item in uvig_set:
				phage_list.append(i)
			else:
				virusset.append(i)

#######################################################################################################
# Define output name
#filter_path = os.path.dirname(args.i) + "/filtered/"
filter_path = os.getcwd() + "/filtered/"
if not os.path.exists(filter_path):
	os.mkdir(filter_path)

# Default output name derived from input name
outname_phage = filter_path + ((args.i).split("/")[-1]).split(".")[0] + ".phages.txt"
outname_virus = filter_path + ((args.i).split("/")[-1]).split(".")[0] + ".virus.txt"

sys.stdout.write("Writing phage file to:\t{}\n".format(outname_phage))
sys.stdout.write("Writing virus file to:\t{}\n".format(outname_virus))

outfile_phage = open(outname_phage, "w+")
outfile_virus = open(outname_virus, "w+")

#######################################################################################################
# Write data to file
infile.seek(0)
for line in infile:
	line = line.rstrip().split("\t")

	for index in phage_list:
		outfile_phage.write(line[index] + "\t")
	outfile_phage.write("\n")

	for index in virus_list:
		outfile_virus.write(line[index] + "\t")
	outfile_virus.write("\n")

infile.close()
outfile_phage.close()
outfile_virus.close()

#######################################################################################################


import numpy as np
from sklearn.tree import DecisionTreeRegressor

X_train = np.load('../asset/x_train.npy')
X_test = np.load('../asset/x_test.npy')
y_train = np.load('../asset/y_train.npy')
y_test = np.load('../asset/y_test.npy')

eeg_model = DecisionTreeRegressor(random_state=1)
eeg_model.fit(X_train,y_train)
eeg_model_result = eeg_model.predict(X_test)
print("Predictions:")
print(eeg_model_result)
print("Actuals:")
print(y_test)

new = y_test - eeg_model_result
count = 0
for i in new:
	if(i == 0):
		count = count + 1

print("\n\n Accuracy is : ", count/len(y_test))


from django.urls import path
from . import views
from user.views import UserDetailView,UserProfileEditView

app_name = "share_userpost"

urlpatterns = [
    path("",views.HomeView.as_view(),name="home"),
    path("event",views.EventView.as_view(),name="event"),
    path("all_posts",views.AllPostsView.as_view(),name="all_posts"),
    path("self_posts",views.SelfPostsView.as_view(),name="self_posts"),
    path("userpost-detail/<int:post_id>",views.PostDetailView.as_view(),name="detail"),
    path("userpost-detail/delete-post/<int:post_id>",views.DeletePostView.as_view(),name="delete"),
    path("userpost-detail/user/detail/<int:user_id>",UserDetailView.as_view(),name="user_detail"),
    path("userpost-detail/user/detail/edit/<int:user_id>",UserProfileEditView.as_view(),name="user_profile_edit")
]


ns = {'61850': 'http://www.iec.ch/61850/2003/SCL',
      'NSD': 'http://www.iec.ch/61850/2016/NSD',
      'efsk': 'http://www.fsk-ees.ru/61850/2020',
      'emt': 'http://www.mtrele.ru/npp/2021'}

nsURI = '{%s}' % (ns['emt'])
nsMap = {None : ns['emt']}

def indentXML(elem, level=0):
    i = "\n" + level*"\t"
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "\t"
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indentXML(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

def normalTag(tag):
    return ''.join(e for e in tag if e.isalnum())

def safeName(unit, lang):
    name = unit.find('./emt:Name[@lang="%s"]' % lang, ns)
    return '' if name is None else name.text

def safeDesc(unit, lang):
    desc = unit.find('./emt:Description[@lang="%s"]' % lang, ns)
    return '' if desc is None else ('\n'.join(line.strip() for line in desc.text.splitlines())).strip()

def is_compatible(conn, slot):
    if slot is None or conn is None: return False
    unit_type = slot.getparent().get('type')
    req_type = conn.get('unit') if 'unit' in conn.attrib else unit_type
    return all([
        unit_type == req_type, #required unit match
        slot.get('type') == conn.get('slot'), #slot is a same type
        int(slot.get('ver')) >= int(conn.get('ver'))]) # slot is newer (back-compatible)


import cocotb
from cocotb.triggers import Timer
from cocotb.result import TestFailure

class AdderModel():
    def __init__(self, a, b, cin):
        full_sum = a + b + cin
        self.sum = full_sum % 2
        self.cout = full_sum > 1

@cocotb.test()
def adder_basic_test(dut):
    for i in xrange(0,8):
        test_a = i%2
        test_b = (i >> 1)%2
        test_cin = (i >> 2)%2

        yield Timer(2)
        dut.a = test_a
        dut.b = test_b
        dut.cin = test_cin

        model = AdderModel(test_a, test_b, test_cin)

        yield Timer(2)
        if dut.sum != model.sum or dut.cout != model.cout:
            raise TestFailure("{0}+{1} (carry in: {2}) =  Failed"
                .format(test_a, test_b, test_cin))


import json
import logging
from copy import copy
from os import stat
from discord.ext import commands

log = logging.getLogger(__name__)

class Alias:
    def __init__(self, tweety):
        self.bot = tweety
        self.alias_map = {}
        try:
            with open('{}/data/{}'.format(self.bot.base, 'alias_map.json'), 'r') as f:
                self.alias_map = json.load(f)
        except FileNotFoundError as err:
            with open('{}/data/{}'.format(self.bot.base, 'alias_map.json'), 'x'):
                log.info('Could not find alias_map.json. Creating a new file.')
        except json.decoder.JSONDecodeError as err:
            if stat('{}/data/{}'.format(self.bot.base, 'alias_map.json')).st_size == 0:
                pass
            else:
                log.error('JSON: {}'.format(err))

    @commands.group()
    async def alias(self, ctx):  # !alias add <alias> <command (and args) without prefix>
        """Create your own alias (shortcut) that references a command """
        pass

    @alias.command()
    async def add(self, ctx, alias_name, *, command):
        uid = str(ctx.message.author.id)
        prefix = await self.bot.get_prefix(ctx)

        if not alias_name.startswith(tuple(prefix)) and not command.startswith(tuple(prefix)):
            try:
                if not uid in self.alias_map:
                    self.alias_map[uid] = {alias_name:command}
                else:
                    self.alias_map[uid].update({alias_name:command})

                with open('{}/data/{}'.format(self.bot.base, 'alias_map.json'), 'w') as f:
                    json.dump(self.alias_map, f)
                    await ctx.send('```[INFO] Alias "{}" created successfully.```'.format(alias_name))
            except IOError:
                log.error('Could not write to alias_map.json.')
        else:
            await ctx.send('```[ERROR] Alias or the command to execute can not start with a bot prefix.```')

    @alias.command(aliases=['del', 'delete', 'rm'])
    async def remove(self, ctx, alias_name):
        uid = str(ctx.message.author.id)

        try:
            del self.alias_map[uid][alias_name]
            await ctx.send('```[INFO] Alias "{}" removed.```'.format(alias_name))
        except KeyError:
            await ctx.send('```[ERROR] Could not find alias "{}".```'.format(alias_name))

    @alias.command()
    async def edit(self, ctx, alias_name, *, command):
        uid = str(ctx.message.author.id)

        if uid in self.alias_map:
            if alias_name in self.alias_map[uid]:
                self.alias_map[uid][alias_name] = command
            else:
                await ctx.send('```[ERROR] Could not find alias "{}".```'.format(alias_name))
        else:
            await ctx.send('```[ERROR] You have no registered aliases.```')

    @alias.command(name='list')  # →
    async def _list(self, ctx):
        uid = str(ctx.message.author.id)

        if uid in self.alias_map:
            prefix = await self.bot.get_prefix(ctx)
            aliases = self.alias_map[uid]
            msg = '\n'.join('{} → {}{}'.format(k, prefix[0], v) for k, v in aliases.items())

            await ctx.send('**Aliases for {}**\n```{}```'.format(ctx.author.name, msg))
        else:
            await ctx.send('```[INFO] {}```'.format('You have not created any aliases yet.'))

    @alias.error
    @add.error
    @remove.error
    @edit.error
    async def alias_error(self, ctx, error):
        if isinstance(error, commands.MissingRequiredArgument):
            await ctx.send('```[ERROR] {}```'.format(error))

    async def on_message(self, message):
        if message.author.bot:
            return

        if str(message.author.id) in self.alias_map:
            if message.content in self.alias_map[str(message.author.id)]:
                prefix = await self.bot.get_prefix(message)

                cmd = self.alias_map[str(message.author.id)][message.content]
                msg = copy(message)
                msg.content = '{}{}'.format(prefix[0], cmd)

                await self.bot.process_commands(msg)

def setup(bot):
    bot.add_cog(Alias(bot))


def scale(size, smooth, target, concurrency):
    futures = set()
    with concurrent.futures.ProcessPoolExecutor(max_workers=concurrency) as executor:
        for srcImage, targetImage in get_jobs(source, target):
            future = executor.submit(scale_one, size, smooth, srcImage, targetImage)
            futures.add(future)
        summary = wait_for(futures)
        if summary.canceled:
            executor.shutdown()
        return summary

def get_jobs(source, target):
    for name in os.listdir(source):
        yield os.path.join(source, name), os.paht.join(target, name)

def wait_for(futures):
    canceled = False
    copied = scaled = 0

    try:
        for future in concurrent.futures.as_completed(futures):
            err = futures.exception()
            if not err:
                result = future.result()
            elif isinstance(err. Image.Error):
                xx
            else:
                raise err

    except KeyboardInterrupt as err:
        canceled = True
        for future in futures:
            future.cancel()
    return Summary(len(futures), copied, scaled, canceled)


import cv2
from scipy.cluster.vq import vq, kmeans
from matplotlib import pyplot as plt
import numpy as np
import time as t
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
from pylab import *

#plt.figure(figsize=(10,8))
#plt.subplot(311)                             #plot in the first cell
#plt.subplots_adjust(hspace=.5)
#plt.title("Hue")
#plt.hist(np.ndarray.flatten(hue), bins=180)
#plt.subplot(312)                             #plot in the second cell
#plt.title("Saturation")
#plt.hist(np.ndarray.flatten(sat), bins=128)
#plt.subplot(313)                             #plot in the third cell
#plt.title("Luminosity Value")
#plt.hist(np.ndarray.flatten(val), bins=128)
#plt.show()

#dataframe = pd.DataFrame(hsv_image[:,:,0])
#dataframe.to_csv("./text/test_animal.csv",header=False,index=False)

def randrange(n,vmin,vmax):
    return (vmax-vmin)*np.random.rand(n)+vmin

def max_hue_detection(hue) :

    count_hue = [0] * 180

    for j in range(0, len(hue)):
        for i in range(0, len(hue[0])):
            hue_value = 0
            hue_value = int(hue[0][i])

            count_hue[hue_value] += 1

    temp_arr = [0] * 9 # n개로 나눈 구역안의 값 덧셈을 위한 배열
    compare_max = [0] * (int(len(count_hue) / len(temp_arr))) #덧셈한 결과를 저장하는 배열

    for j in range(0, len(compare_max)):  # 어느 구역의 색이 가장 많이 나왔는지 더하는 과정
        flag = j * len(temp_arr)
        for i in range(0, len(temp_arr)):
            temp_arr[i] = count_hue[flag + i]
        compare_max[j] = sum(temp_arr)

    result = np.argsort(compare_max)[::-1][:3]
    result = len(temp_arr) * result + int(len(temp_arr) / 2)
    result_value = np.sort(compare_max)[::-1][:3]

    return result,result_value

def max_sat_detection(sat) :

    count_sat = [0] * 256

    for j in range(0, len(sat)):
        for i in range(0, len(sat[0])):
            sat_value = 0
            sat_value = int(sat[0][i])

            count_sat[sat_value] += 1

    compare_max = [0] * 8 # 덧셈한 결과를 저장하는 배열
    temp_arr = [0] * (int(len(count_sat) / len(compare_max))) # n개로 나눈 구역안의 값 덧셈을 위한 배열

    for j in range(0, len(compare_max)):  # 어느 구역의 색이 가장 많이 나왔는지 더하는 과정
        flag = j * len(temp_arr)
        for i in range(0, len(temp_arr)):
            temp_arr[i] = count_sat[flag + i]
        compare_max[j] = sum(temp_arr)

    result = np.argsort(compare_max)[::-1][:3]
    result = len(temp_arr) * result + int(len(temp_arr) / 2)
    result_value = np.sort(compare_max)[::-1][:3]

    return result,result_value

def max_lum_detection(lum) :

    count_lum = [0] * 256

    for j in range(0, len(lum)):
        for i in range(0, len(lum[0])):
            lum_value = 0
            lum_value = int(lum[0][i])

            count_lum[lum_value] += 1

    compare_max = [0] * 8 # 덧셈한 결과를 저장하는 배열
    temp_arr = [0] * (int(len(count_lum) / len(compare_max))) # n개로 나눈 구역안의 값 덧셈을 위한 배열

    for j in range(0, len(compare_max)):  # 어느 구역의 색이 가장 많이 나왔는지 더하는 과정
        flag = j * len(temp_arr)
        for i in range(0, len(temp_arr)):
            temp_arr[i] = count_lum[flag + i]
        compare_max[j] = sum(temp_arr)

    result = np.argsort(compare_max)[::-1][:3] #내림차순 정렬 후 라스트 3개만
    result = len(temp_arr) * result + int(len(temp_arr)/2)
    result_value = np.sort(compare_max)[::-1][:3]

    return result,result_value

    target_image = './static/resources/*.*'
    img = cv2.imread(target_image)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    hsv_image = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    hue, sat, val = hsv_image[:, :, 0], hsv_image[:, :, 1], hsv_image[:, :, 2]

    color_detect = [0, 0, 0]  # 최종hsv top3 색 담을 공간
    for i in range(3):
        color_detect[i] = [max_hue_detection(hue)[0][i], max_sat_detection(sat)[0][i], max_lum_detection(val)[0][i]]

    print(max_hue_detection(hue)[1][0], max_sat_detection(sat)[1][0], max_lum_detection(val)[1][0], end=" ")
  #  fig = plt.figure(figsize=(8, 6))
   # ax = fig.add_subplot(111, projection='3d')
   # n = 1

    #xs = randrange(n, 0, 100)
    #ys = randrange(n, 0, 150)
    #zs = randrange(n, 0, 200)
   # the_fourth_dimension = randrange(n, 0, 3)

   # colors = cm.hsv(the_fourth_dimension/max(the_fourth_dimension))

   # colmap = cm.ScalarMappable(cmap=cm.hsv)
   # colmap.set_array(the_fourth_dimension)

    #yg = ax.scatter(xs, ys, zs, c=colors, marker='o')
  #  cb = fig.colorbar(colors)

  #  ax.set_xlabel('X Label')
  #  ax.set_ylabel('Y Label')
  #  ax.set_zlabel('Z Label')

  #  plt.show()

    print("색상 최대값 : ",max_hue_detection(hue))
    print("채도 최대값 : ",max_sat_detection(sat))
    print("명암 최대값 : ",max_lum_detection(val))

    print(color_detect)


import os
import logging
import argparse

from . import cli
from . import formatter

HISTORY_PATH = os.path.join(os.path.expanduser("~"), ".open-cli")

def main():
    """Open-CLI entry point."""
    args_parser = argparse.ArgumentParser(description='Open-CLI.')
    args_parser.add_argument('source', type=str, help='Open API spec source')
    args_parser.add_argument('-v', '--verbose', action='store_true', help='If set, set log level to debug')
    args_parser.add_argument('-t', '--history', type=str, default=HISTORY_PATH, help='history file path')
    args_parser.add_argument('-c', '--command', type=str, help='command to execute', required=False)
    args_parser.add_argument('-f', '--format', type=str,
                             choices=formatter.FORMATTERS.keys(), default=formatter.JSON,
                             help='Set the CLI output format')
    args_parser.add_argument('--header', nargs='+', default=[],
                             help='requests headers, usage: --header x-header-1:val-1 x-header-2:val2')

    args = args_parser.parse_args()

    logging.basicConfig(level=logging.DEBUG if args.verbose else logging.ERROR)

    open_cli = cli.OpenCLI(
        source=args.source,
        history_path=args.history,
        output_format=args.format,
        headers=args.header,
    )

    if args.command:
        return open_cli.execute(command=args.command)

    open_cli.run_loop()

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
import re
from datetime import datetime, time

from city_scrapers.constants import COMMISSION
from city_scrapers.spider import Spider

class ChiZoningBoardSpider(Spider):
    name = 'chi_zoning_board'
    agency_name = 'Chicago Department of Planning and Development'
    timezone = 'America/Chicago'
    allowed_domains = ['www.cityofchicago.org']
    start_urls = [
        'https://www.cityofchicago.org/city/en/depts/dcd/supp_info/zoning_board_of_appeals.html'
    ]

    def parse(self, response):
        """
        `parse` should always `yield` a dict that follows the Event Schema
        <https://city-bureau.github.io/city-scrapers/06_event_schema.html>.

        Change the `_parse_id`, `_parse_name`, etc methods to fit your scraping
        needs.
        """
        description = self.parse_description(response)
        columns = self.parse_meetings(response)
        for column in columns:
            year = column.xpath('preceding::strong[1]/text()').re_first(r'(\d{4})(.*)')
            meetings = column.xpath('text()[normalize-space()]').extract()
            for meeting in meetings:
                data = {
                    '_type': 'event',
                    'name': 'Zoning Board of Appeals',
                    'event_description': description,
                    'classification': COMMISSION,
                    'start': self._parse_start(meeting, year),
                    # Based on meeting minutes, board meetings appear to be all day
                    'all_day': True,
                    'location': {
                        'neighborhood': '',
                        'name': 'City Hall',
                        'address': '121 N. LaSalle St., in City Council chambers'
                    },
                    'sources': [{
                        'url': response.url,
                        'note': ''
                    }],
                }
                data['documents'] = self._parse_documents(column, data, response)
                data['end'] = {'date': data['start']['date'], 'time': None, 'note': ''}
                data['id'] = self._generate_id(data)
                data['status'] = self._generate_status(data)
                yield data

    @staticmethod
    def parse_description(response):
        desc_xpath = '//p[contains(text(), "The Zoning Board of Appeals")]/text()'
        description = response.xpath(desc_xpath).extract_first(default='').strip()
        return description

    @staticmethod
    def parse_meetings(response):
        meeting_xpath = """
            //td[preceding::p/strong[1]/text()[
                contains(., "Meeting Schedule")
                ]]"""
        return response.xpath(meeting_xpath)

    @staticmethod
    def _parse_start(meeting, year):
        m = re.search(r'(?P<month>\w+)\s(?P<day>\d+).*', meeting.strip())
        dt = datetime.strptime(m.group('month') + ' ' + m.group('day') + ' ' + year, '%B %d %Y')
        # time based on examining meeting minutes
        return {'date': dt.date(), 'time': time(9, 0), 'note': ''}

    @staticmethod
    def _parse_documents(item, data, response):
        month = data['start']['date'].strftime("%B")
        xp = './/a[contains(@title, "{0}")]'.format(month)
        documents = item.xpath(xp)
        if len(documents) >= 0:
            return [{
                'url': response.urljoin(document.xpath('@href').extract_first()),
                'note': document.xpath('text()').extract_first()
            } for document in documents]
        return []


import numpy as np
import matplotlib.pyplot as plt
from numpy import sqrt, pi

from archive.bisection import Bisection

def u(x):
    re = 8.0/(pi*x)*sqrt(1-(1/x)**2)
    im = 8.0/(pi*x**2)
    return -1/(re - 1j*im)

def v(x):
    re = x**2
    im = x
    return -3.0/(re - 1j*im)

def difference(x):
    return (u(x).real - v(x).real)

a = -10.0
b =  10
max_iter = 1000
h = []

x = 2
if x == 1:
    for xmid in Bisection(a, b, difference, max_iter):
        h.append([xmid, u(xmid).real, v(xmid).real, u(xmid).imag, v(xmid).imag])
    for i in range(1, 6):
        print(h[len(h)- i])
elif x == 2:
    x0 = np.linspace(a, b, 1000)
    u_real = u(x0).real
    u_imag = u(x0).imag
    plt.plot(x0, u_real, color='blue')
    plt.plot(x0, u_imag, color='red')
    plt.grid(True)
    plt.show()
elif x == 3:
    x0 = np.linspace(a, b, 1000)
    u_real = u(x0).real
    v_real = v(x0).real
    u_imag = u(x0).imag
    v_imag = v(x0).imag
    plt.plot(u_real, u_imag, color='red')
    plt.plot(v_real, v_imag, color='blue')
    plt.plot(-0.6420126, -0.3926991, 'ro')
    plt.grid(True)
    plt.show()

"""
mpl.rcParams['legend.fontsize'] = 10

fig = plt.figure()
ax = fig.gca(projection='3d')

z = np.linspace(a, b, 1000)

s = np.linspace(a, b, 1000)
x1 = u(s).real
y1 = u(s).imag
ax.plot(x1, y1, z, color='blue')

x2 = v(s).real
y2 = v(s).imag
ax.plot(x2, y2, z, color='red')

ax.plot(x1, y1, 0, color='black')
ax.plot(x2, y2, 0, color='black')

plt.show()
"""


#-------------------------------------------------------------------------------------------------------------------
# depthwise_conv_v2 - Balaji Adithya V
# Define custom torch function approx_depth_conv() and implementation of MBM algo done
# Create lookup table for referring while performing MBM
# ------------------------------------------------------------------------------------------------------------------

import torch
import numpy as np
import torchvision as tv
from torch import nn
import torch.nn.functional as F
from torch.nn.functional import unfold
from torch.utils.cpp_extension import load

#---------------------------------------------- HERE ---------------------------------------------------------------
# This snippet of code extracts row-wise data from reference_mbm.csv that contains the data for determining product
# value based on the 2 inputs.
#
# Lookup_table created where the inputs themslves act as the pointers needed to locate the MBM product
# (eg) i=25, j=64, then lookup_table[i][j] = MBM(i,j). In tis manner the look up time complexity reduces to O(1)

import csv
import pandas as pd
import time

filename = "reference_mbm.csv"
rows = []

with open(filename, 'r') as file:
	rows = list(csv.reader(file))

	for row in rows[1:]:
		row[0], row[1], row[2] = int(row[0]), int(row[1]), int(row[2])

lookup_table = np.zeros((256,256))
for row in rows[1:]:
	lookup_table[row[0]][row[1]] = row[2]

#-------------------------------------------------------------------------------------------------------------------
# approx_conv class
# Derived from the torch.autograd.Function class
# Performs in_feature * kernel where (*) represents depthwise-convolution operation
#
# out_channel = in_channel
#
# in_feature dimensions -> [batch_size, in_channel, in_height, in_width]
# kernel dimensions     -> [out_channel, 1, kh, kw]
#
# Create patches of input image based on kernel size; Kernel size is also reshaped -> done for efficient computation (parallelizing)
# patches dimensions -> [batch_size, in_channel, out_h*out_w, kh*kw]
# kernel dimensions  -> [in_channel, 1*kh*kw]
# result[b][c] = k[c]*patches[b][c] -> dimensions -> [out_channel, out_h, out_w]; where b denotes current batch, c=current channel
#--------------------------------------------------------------------------------------------------------------------

class depthwise_conv(torch.autograd.Function):
	@staticmethod

	def forward(ctx, in_feature, kernel, out_channel, stride, padding, dilation, groups, bias):
		print("I'm in conv fwd")
		print("input = {}".format(in_feature.shape))
		print("kernel= {}".format(kernel.shape))

		batch_size = in_feature.size(0)
		in_channels = in_feature.size(1)
		orig_h, orig_w = in_feature.size(2), in_feature.size(3)

		tmp = torch.abs(in_feature)
		mx = torch.max(tmp)
		mn = torch.min(tmp)
		mean = torch.mean(tmp)

		print(mx ,mn ,mean)

		#Kernel Dimenstions
		kh, kw = kernel.size(2), kernel.size(3)
		#Strides
		dh, dw = stride, stride

		pad = padding

		#output dimensions
		out_h = (orig_h+2*pad-kh)//dh + 1
		out_w = (orig_w+2*pad-kw)//dw + 1

		img = F.pad(input= in_feature, pad= (pad, pad, pad, pad), mode='constant', value= 0)

		patches = img.unfold(2,kh,dh).unfold(3,kw,dw).reshape(batch_size, in_channels, out_h*out_w, kh*kw)
		result = torch.zeros(batch_size, out_channel, out_h*out_w)
		kernel = kernel.reshape(out_channel, -1)

		for b in range(batch_size):
			x = patches[b]
			print(x.shape)
			for c in range(groups):
				start = time.time()
				for i in range(x.size(1)):
					r=0
					for j in range(kernel.size(1)):
						t1 = int((kernel[c][j]*10).round())
						t2 = int((x[c][i][j]*10).round())

						if(t1>255) : t1 =255
						if(t1<-255): t1 =-255
						if(t2>255) : t2 =255
						if(t2<-255): t2 =-255

						if((t1>0 and t2>0) or (t1<0 and t2<0)):
							t1=abs(t1)
							t2=abs(t2)
							sign=1
						else:
							t1=abs(t1)
							t2=abs(t2)
							sign=-1

						r += lookup_table[t1][t2]*sign

					result[b][c][i] = r/100

				end = time.time()
				print("time taken for channel {}/ {} in batch {} is {}s".format(c, groups, b, end-start))

		result = result.reshape(batch_size, out_channel, out_h, out_w)

		if bias is not None:
			result+= bias[None,:,None,None].expand_as(result)

		return result

#-------------------------------------------------------------------------------------------------
# approx_dconv class
# Derived from nn.Module class
# Used to register the parameters of the conv layer - weight, bias
# Call the depthwise_conv constructor to start depthwise convolution operation
#-------------------------------------------------------------------------------------------------

class approx_dconv(nn.Module):
	def __init__(self, in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias=False):
		super(approx_dconv, self).__init__()

		#Initialize misc. variables
		self.in_channels = in_channels
		self.out_channels = out_channels
		self.stride = stride
		self.dilation = dilation
		self.groups = groups
		self.padding = padding
		self.weight = nn.Parameter(torch.Tensor(out_channels, 1, kernel_size, kernel_size))
		self.bias = self.register_parameter('bias',None)
		# print("I'm here")

	def forward(self, x):
		# print("I'm in fwd of approx_dconv")
		return depthwise_conv.apply(x, self.weight, self.out_channels, self.stride, self.padding, self.dilation, self.groups, self.bias)


import os

from neo4j.v1 import GraphDatabase

from depc import create_app

app = create_app(environment=os.getenv("DEPC_ENV") or "default")

def is_active_node(start, end, node):
    """This function checks the end of life of a node"""
    return (start <= node.get("from", 0) <= end) or (
        node.get("from", 0) <= start <= node.get("to", float("inf"))
    )

def compute_active_periods(start, end, periods):
    active_periods = {}

    def _add_period(p_start, p_end):
        if end >= p_start and start <= p_end:
            if p_start < start:
                active_periods[start] = True
            else:
                active_periods[p_start] = True

            # WARNING : if p_end == start the key is rewritten
            # Example: start = 1545091200 and periods = [1545090000, 1545091200]
            active_periods[p_end] = False

    # Iterate over an even list of timestamps
    def _iterate_over_periods(list_ts):
        iter_periods = iter(list_ts)
        for p in iter_periods:
            _add_period(p, next(iter_periods))

    length = len(periods)

    if length == 1:
        ts = periods[0]
        if end >= ts:
            return {ts: True}
        return {}

    if length % 2 == 0:
        _iterate_over_periods(periods)
    else:
        last_ts = periods[-1]
        _iterate_over_periods(periods[:-1])
        _add_period(last_ts, end)

    return active_periods

def has_active_relationship(start, end, periods):
    active_periods = compute_active_periods(start, end, periods)
    return len(active_periods) > 0

def get_records(query, params={}):
    config = app.config["NEO4J"]

    driver = GraphDatabase.driver(
        config["uri"], auth=(config["username"], config["password"])
    )
    session = driver.session()

    return session.read_transaction(lambda tx: tx.run(query, params))


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

img = cv2.imread("enhancedPic.jpg")
img = cv2.resize(img, None, fx=0.2, fy=0.2, interpolation=cv2.INTER_CUBIC)
cv2.waitKey(0)
picSize = img.shape
smallW = int(picSize[1]/4)
smallH = int(picSize[0]/2)
for i in range(2):
    for j in range(4):
        imgSlice = img[smallH*i:smallH*i+smallH,
                       smallW*j:smallW*j+smallW]  # sliced small pic
        filename = "slicedPic{}_{}.jpg".format(i, j)
        cv2.rectangle(img, (smallW*j, smallH*i), (smallW*j+smallW,
                                                  smallH*i+smallH), (0, 128, 128), 3)  # draw boundary rectangle
        cv2.imwrite(filename, imgSlice)
cv2.waitKey(0)
cv2.destroyAllWindows()

# pre process
for i in range(2):
    for j in range(4):
        rawImg = cv2.imread("slicedPic{}_{}.jpg".format(i, j))
        img = cv2.cvtColor(rawImg, cv2.COLOR_BGR2GRAY)
        # img = cv2.GaussianBlur(img, (2, 2), 10)#gauss morph
        ret, thresholdedImg = cv2.threshold(img, 5, 255, cv2.THRESH_BINARY)

        validShape = [0, 0, 0, 0]

        okFlag = False
        for y in range(thresholdedImg.shape[0]):
            if okFlag:
                break
            for x in range(thresholdedImg.shape[1]):
                if thresholdedImg[y, x] != 255:
                    validShape[0] = y
                    okFlag = True
                    break
        okFlag = False
        for y in range(thresholdedImg.shape[0]-1, -1, -1):
            if okFlag:
                break
            for x in range(thresholdedImg.shape[1]):
                if thresholdedImg[y, x] != 255:
                    validShape[1] = y
                    okFlag = True
                    break
        okFlag = False
        for x in range(thresholdedImg.shape[1]):
            if okFlag:
                break
            for y in range(thresholdedImg.shape[0]):
                if thresholdedImg[y, x] != 255:
                    validShape[2] = x
                    okFlag = True
                    break
        okFlag = False
        for x in range(thresholdedImg.shape[1]-1, -1, -1):
            if okFlag:
                break
            for y in range(thresholdedImg.shape[0]):
                if thresholdedImg[y, x] != 255:
                    validShape[3] = x
                    okFlag = True
                    break

        boxHalfSize = int(max(
            validShape[1]-validShape[0], validShape[3]-validShape[2])*1.5/2)  # set 30% margin
        boxCenter = (int((validShape[1]+validShape[0])/2),
                     int((validShape[3]+validShape[2])/2))

        limitedImg = thresholdedImg[boxCenter[0]-boxHalfSize:boxCenter[0] +
                                    boxHalfSize, boxCenter[1]-boxHalfSize:boxCenter[1]+boxHalfSize]

        resized_image = ~cv2.resize(limitedImg, (28, 28))
        outFilename = "slicedPic{}_{}mono.jpg".format(i, j)
        cv2.imwrite(outFilename, resized_image)

dafaultstderr = sys.stderr
nullstderr=open(os.devnull, 'w')

print('loading Keras')
sys.stderr =nullstderr
from keras.models import load_model
sys.stderr = dafaultstderr

print('loading model')
sys.stderr =nullstderr
model = load_model('mnistModel.h5')
sys.stderr = dafaultstderr
X_pred = np.zeros(shape=(8, 28, 28))
index = 0
for i in range(2):
    for j in range(4):
        img = cv2.imread("slicedPic{}_{}mono.jpg".format(i, j))
        imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        X_pred[index] = cv2.resize(imgGray, (28, 28))
        index += 1

X_test = X_pred.reshape(8, 784).astype('float32') / 255
y_test = [1, 2, 3, 4, 5, 6, 7, 8]

print('pridicting...')
sys.stderr =nullstderr
y_predict = model.predict_classes(X_test)
sys.stderr = dafaultstderr
correctCount = len(np.nonzero(y_predict == y_test)[0])
print('correct:{},total:{},percentage:{:.1f}%'.format(
    correctCount, len(y_predict), 100*correctCount/len(y_predict)))


#!/usr/bin/env python3

import json
import requests
import sys
import time

URL_PREFIX = "http://{}:8181/restconf/{}"
INVENTORY_URL = URL_PREFIX + "/opendaylight-inventory:nodes"
TABLE_URL = INVENTORY_URL + "/node/{}/flow-node-inventory:table/{}"
FLOW_URL = TABLE_URL + "/flow/{}"
OP = "operational"
CFG = "config"
TABLE_ID = 0
LLDP = 0x88cc
ARP = 0x0806
HEADERS = {"content-type": "application/json", "accept": "application/json"}
AUTH = ("admin", "admin")

TIME_INTERVAL = 2 # check the flow rules every _ seconds

FLOW_RULE_TEMPLATE='''
{
    \"flow\": {
        \"id\": %s,
        \"match\": {
            \"ethernet-match\": {
                \"ethernet-type\": {
                    \"type\": %d
                }
            }
        },
        \"priority\": %d,
        \"idle-timeout\": 0,
        \"hard-timeout\": 0,
        \"table_id\": 0,
        \"instructions\": {
            \"instruction\": [
                {
                    \"order\": 0,
                    \"apply-actions\": {
                        \"action\": [
                            {
                                \"order\": 0,
                                \"output-action\": {
                                    \"output-node-connector\": \"CONTROLLER\",
                                    \"max-length\": 65535
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}
'''

def is_matching_ethernet_protocol(flow, protocol):
    try:
        match = flow['match']
        if len(match) > 1:
            return False
        ethernet_match = match['ethernet-match']
        if len(ethernet_match) > 1:
            return False
        return ethernet_match['ethernet-type']['type'] == protocol
    except Exception:
        return False

def is_sending_to_controller(flow):
    try:
        instructions = flow['instructions']['instruction']
        if len(instructions) > 1:
            return False
        actions = instructions[0]['apply-actions']['action']
        if len(actions) > 1:
            return False
        return actions[0]['output-action']['output-node-connector'].upper() == 'CONTROLLER'
    except Exception:
        return False

def to_hex(value):
    return '0x{:04x}'.format(value)

def check_and_install(controller_ip, node_id, proto):
    tbl_url = TABLE_URL.format(controller_ip, OP, node_id, TABLE_ID)

    response = requests.get(tbl_url, headers=HEADERS, auth=AUTH)
    checked = False
    try:
        table = response.json()['flow-node-inventory:table'][0]
        print(table)
        for flow in table['flow']:
            if not is_matching_ethernet_protocol(flow, proto):
                continue
            if not is_sending_to_controller(flow):
                continue
            checked = True
            break;
    except Exception:
        pass

    print('{} status: {}'.format(node_id, checked))

    if not checked:
        fl_url = FLOW_URL.format(controller_ip, CFG, node_id, TABLE_ID, to_hex(proto))
        priority = 100 if proto == LLDP else 1
        data = FLOW_RULE_TEMPLATE % ('\"{}\"'.format(to_hex(proto)), proto, priority)
        response = requests.put(fl_url, headers=HEADERS, data=data, auth=AUTH)
        print(response.status_code)

def stabilize(controller_ip):
    while True:
        try:
            oneshot_stabilize(controller_ip)
            time.sleep(TIME_INTERVAL)
        except KeyboardInterrupt as e:
            break
        except Exception as e:
            continue

def oneshot_stabilize(controller_ip):
    pattern = INVENTORY_URL
    url = pattern.format(controller_ip, OP)

    response = requests.get(url, headers=HEADERS, auth=AUTH)
    if response.ok:
        inventory = response.json()
        for node in inventory['nodes']['node']:
            node_id = node['id']
            print(node_id)
            check_and_install(controller_ip, node_id, LLDP)
            check_and_install(controller_ip, node_id, ARP)

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Usage: python {} CONTROLLER_IP".format(__file__))
        sys.exit()
    oneshot_stabilize(sys.argv[1])


import click
import pandas as pd
import numpy as np
import json
from os import path
from datetime import date
from urllib.request import urlopen

AVAPI_KEY_JSON = "avapi_key.json"
BASE = "https://www.alphavantage.co/query?function={}&symbol={}&apikey={}&datatype=csv"

STOCK_FUNC_LIST = {
    'intraday': "TIME_SERIES_INTRADAY",
    'daily':    "TIME_SERIES_DAILY",
    'weekly':   "TIME_SERIES_WEEKLY",
    'monthly':  "TIME_SERIES_MONTHLY",
    'global':   "GLOBAL_QUOTE"
}

CRYPTO_FUNC_LIST = {
    'rating':   "CRYPTO_RATING",
    'daily':    "DIGITAL_CURRENCY_DAILY",
    'weekly':   "DIGITAL_CURRENCY_WEEKLY",
    'monthly':  "DIGITAL_CURRENCY_MONTHLY"
}

INTERVAL_LIST = ["1min", "5min", "15min", "30min", "60min"]

class avapiJsonFileNotFoundError(Exception):
    """ Raised when an avapi_key.json file is not found"""
    pass

class avapiJsonEmptyError(Exception):
    """
    Raised when the 'avapi_key.json file is empty
    or there is no data within the 'avapi_key'
    """
    pass

class invalidFunctionError(Exception):
    """ Raised when an incorrect function argument is provided"""
    pass

class invalidIntervalError(Exception):
    """ Raised when an incorrect interval is provided to the 'intraday' argument"""
    pass

def read_key():
    """Try to read 'avapi_key.json' and return the 'key' field."""
    try:
        if (path.exists(AVAPI_KEY_JSON)):
            with open(AVAPI_KEY_JSON, 'r') as f:
                j = json.load(f)
                if not 'key' in j or len(j['key']) == 0:
                    raise avapiJsonEmptyError
                else:
                    return j['key']

        else:
            raise avapiJsonFileNotFoundError

    except avapiJsonFileNotFoundError:
        print("Error: 'avapi_key.json' file found. Try running 'setkey'.")
    except avapiJsonEmptyError:
        print(
            "Error: 'avapi_key.json' does not contain an API Key. Try running 'setkey'."
        )

def get_pandas_df(url, n):
    if n != 0:
        return pd.read_csv(url).head(n)
    else:
        return pd.read_csv(url)

@click.group(invoke_without_command=True)  # Allow users to call w/o command
@click.pass_context
@click.option('--verbose',
              '-v',
              is_flag=True,
              help="Increase output verbosity level.")
def main(ctx, verbose):
    """
    A command-line interface written in Python for the Alpha Vantage API.

    Author: Mac Fox
    """
    if ctx.invoked_subcommand is None:  # No command was specified
        click.echo("No command specified, try using '--help'")
    ctx.obj['VERBOSE'] = verbose

@main.command('setkey')
@click.option('--k',
              prompt="Alpha Vantage API Key",
              help="Alpha Vantage API Key")
def setkey(k):
    """Save an Alpha Vantage API key to 'avapi_key.json'."""
    avapi_key = {"key": k}
    with open("avapi_key.json", 'w') as f:
        json.dump(avapi_key, f)

@main.command('exrate')
@click.argument('from_currency')
@click.argument('to_currency')
@click.option('--save', is_flag=True, help="Save data to .csv file.")
def exrate(from_currency, to_currency, save):
    """
    Returns the realtime exchange rate for any pair of digital currency
    (e.g., BTC) or physical currency (e.g., USD).
    """
    url = ("https://www.alphavantage.co/query?"
           "function=CURRENCY_EXCHANGE_RATE"
           f"&from_currency={from_currency}"
           f"&to_currency={to_currency}"
           f"&apikey={read_key()}")

    data = json.loads(urlopen(url).read())
    df = pd.json_normalize(data['Realtime Currency Exchange Rate'])
    click.echo(df.to_string(index=False))

    if save:
        filename = f"{date.today()}_exrate_{f}_{t}.csv"
        df.to_csv(filename, index=False)

@main.command('stock')
@click.argument('function')
@click.option('-s', '-symbol', prompt="Symbol", help="TSLA AAPL GME...")
@click.option('-i', '-interval', default="30min", help="1min 5min 15min 30min or 60min. Default = 30min")
@click.option('-n', '-last_n', default=0, help="Only get last_n rows of data")
@click.option('--save', is_flag=True, help="Save data to .csv file.")
def stock(function, s, i, n, save):
    """
    Get a specific time series function for a stock of interest.

    FUNCTION: intraday daily weekly monthly global

    Example: python3 avapi.py stock intraday -s'TSLA' -i'15min'

    Example: python3 avapi.py stock daily -s'TSLA'

    Example: python3 avapi.py stock daily -s'TSLA' -n30
    """
    try:
        if function not in STOCK_FUNC_LIST:  # Make sure function argument is valid
            raise invalidFunctionError
        else:
            if function == "rating":
                url = BASE.format(
                    STOCK_FUNC_LIST["intraday"], s, read_key()) + f"&interval={i}"
                df = get_pandas_df(url, n)
                click.echo(df)

            else:  # Construct query and get data
                url = BASE.format(
                    STOCK_FUNC_LIST[function], s, read_key())
                df = get_pandas_df(url, n)
                click.echo(df)

            if save:
                filename = f"{date.today()}_{function}_{s}_.csv"
                df.to_csv(filename, index=False)

    except invalidFunctionError:
        click.echo(
            "Error: Invalid function argument:'{}'".format(function))
    except invalidIntervalError:
        click.echo("Error: Invalid interval option:'{}'".format(i))

@main.command('crypto')
@click.argument('function')
@click.option('-s', '-symbol', prompt="Symbol", help="TSLA AAPL GME...")
@click.option('-n', '-last_n', default=0, help="Only get last_n rows of data")
@click.option('--save', is_flag=True, help="Save data to .csv file.")
def crypto(function, s, n, save):
    """
    Get a specific time series function for a crypto currency of interest.
    """
    try:
        if function not in CRYPTO_FUNC_LIST:  # Make sure function argument is valid
            raise invalidFunctionError
        if function == 'rating':
            url = ("https://www.alphavantage.co/query?"
                   "function=CRYPTO_RATING"
                   f"&symbol={s}&apikey={read_key()}")
            data = json.loads(urlopen(url).read())
            df = pd.json_normalize(data["Crypto Rating (FCAS)"])
            click.echo(df.to_string(index=False))
        else:
            url = ("https://www.alphavantage.co/query?"
                   f"function={CRYPTO_FUNC_LIST[function]}"
                   f"&symbol={s}&market=USD&apikey={read_key()}"
                   "&datatype=csv")
            df = get_pandas_df(url, n)
            click.echo(df)

        if save:
            filename = f"{date.today()}_{function}_{s}_.csv"
            df.to_csv(filename, index=False)

    except invalidFunctionError:
        click.echo(
            "Error: Invalid function argument for 'avapi get':'{}'".format(function))
    except invalidIntervalError:
        click.echo("Error: Invalid interval option:'{}'".format(i))

if __name__ == "__main__":
    # pylint: disable=unexpected-keyword-arg
    # pylint: disable=no-value-for-parameter
    main(obj={})


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

# usage:
#   lb-run Ganga ganga download_paths.py [JOBNR]
from sys import argv

jobnum = int(argv[1])

with open('job_{}_downloadpaths.txt'.format(jobnum), 'w') as f:
    for subjob in jobs(jobnum).subjobs:
        if subjob.status != 'completed':
            continue

        f.write(subjob.outputfiles[0].lfn + '\n')


from posts.models import posts

__author__ = 'vdjke'
from django import forms

class PostsForms(forms.ModelForm):

    titulo = forms.CharField(max_length=240, min_length=8, required=True,
                             widget=forms.TextInput(attrs={
                                 'class': 'form-control',
                                 'name': 'titulo', 'id': 'titulo', 'placeholder': 'Escribe un titulo'}))
    cuerpo = forms.CharField(required=True,
                             widget=forms.Textarea(attrs={
                                 'class': 'form-control',
                                 'name': 'cuerpo',
                                 'id': 'cuerpo',
                                 'placeholder': 'Contenido del posts'}))
    categoria = forms.Select(attrs={'class': 'form-control'})
    tags = forms.CharField(max_length=240, min_length=10, required=True,
                           widget=forms.TextInput(attrs={'class': 'form-control',
                                 'placeholder': 'Etiquetas separadas por comas (,)', 'id': 'tags'}))
    img_file = forms.CharField(required=True,
                               widget=forms.TextInput(attrs={
                                   'class': 'form-control',
                                   'placeholder': 'Seleccion un archivo',
                                   'id': 'append-big-btn',
                                   'readonly': '',

                               }))
    permitir_comentarios = forms.BooleanField(required=False,
                                              widget=forms.CheckboxInput(
                                                  attrs={}))

    class Meta:
        model = posts
        fields = {'titulo', 'categoria', 'cuerpo', 'tags', 'permitir_comentarios', 'img_file'}
        widgets = {
            'categoria': forms.Select(attrs={'class': 'form-control'})
        }


from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index),

    url(r'^new', views.new),

    url(r'^signin', views.signin),

    url(r'^register', views.register),

    url(r'^edit', views.edit),

    url(r'^dashboard', views.dashboard),
]

# urlpatterns = [
#     url(r'^users/new', views.new),
# ]


import torch
from torch_geometric.utils import dense_to_sparse, sparse_to_dense

def test_dense_to_sparse():
    tensor = torch.Tensor([[3, 1], [2, 0]])
    edge_index, edge_attr = dense_to_sparse(tensor)

    assert edge_index.tolist() == [[0, 0, 1], [0, 1, 0]]
    assert edge_attr.tolist() == [3, 1, 2]

def test_sparse_to_dense():
    edge_index = torch.tensor([[0, 0, 1], [0, 1, 0]])
    edge_attr = torch.Tensor([3, 1, 2])

    tensor = sparse_to_dense(edge_index, edge_attr)
    assert tensor.tolist() == [[3, 1], [2, 0]]

    tensor = sparse_to_dense(edge_index)
    assert tensor.tolist() == [[1, 1], [1, 0]]


import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
	#A class to rep a single alien in the fleet

	def __init__(self,ai_game):
		#initializes the alien and set its start position
		super().__init__()
		self.screen=ai_game.screen
		self.settings=ai_game.settings

		#load the alien image and set its rect attribute
		self.image=pygame.image.load('images/alien.bmp')
		self.rect=self.image.get_rect()

		#start each new alien near the top left of the screen
		self.rect.x=self.rect.width
		self.rect.y=self.rect.height

		#store alien's exact horizonatal position
		self.x=float(self.rect.x)

	def check_edges(self):
		#return True if alien is at edge of screen
		screen_rect=self.screen.get_rect()

		if self.rect.right>=screen_rect.right or self.rect.left<=0:
			return True

	def update(self):
		#move the alien to the correct side
		self.x+=self.settings.alien_speed*self.settings.fleet_direction
		self.rect.x=self.x


#!/usr/bin/python3

import sys, getopt

encoding = {'a' : "9aLxO",
            'b' : "StaOL",
            'c' : "piHW8",
            'd' : "4QXBv",
            'e' : "LDkJp",
            'f' : "c0fCS",
            'g' : "iGiZz",
            'h' : "GsT20",
            'i' : "1BWcd",
            'j' : "1LVBz",
            'k' : "ABR9N",
            'l' : "b3tDp",
            'm' : "TL2hv",
            'n' : "uhHnQ",
            'o' : "8XcXH",
            'p' : "leCmS",
            'q' : "KuIfX",
            'r' : "X3ilW",
            's' : "mWnb5",
            't' : "nXZpK",
            'u' : "dQT4L",
            'v' : "DOtQr",
            'w' : "l5uVp",
            'x' : "DbH7V",
            'y' : "Lm8Fq",
            'z' : "Nov2R",
            'æ' : "hjQdG",
            'ø' : "Kafdg",
            'å' : "fQAh6",
            'A' : "4aL1B",
            'B' : "StaOL",
            'C' : "Th80G",
            'D' : "l4B94",
            'E' : "gMO01",
            'F' : "13Pme",
            'G' : "eF103",
            'H' : "G0Qk8",
            'I' : "x73Qe",
            'J' : "3zI8E",
            'K' : "k75Xx",
            'L' : "Gb45p",
            'M' : "5a7ZX",
            'N' : "Df4B6",
            'O' : "rK31M",
            'P' : "Ye75j",
            'Q' : "817xC",
            'R' : "Dok94",
            'S' : "vN88X",
            'T' : "jT36X",
            'U' : "mS54V",
            'V' : "mS54V",
            'W' : "oA49M",
            'X' : "rT13f",
            'Y' : "7A1iH",
            'Z' : "Rd30Q",
            'Æ' : "L6c0z",
            'Ø' : "A7c1T",
            'Å' : "Qg1N2",
            ' ' : "i0DQd"}

decoding = {'9aLxO' : "a",
            'StaOL' : "b",
            'piHW8' : "c",
            '4QXBv' : "d",
            'LDkJp' : "e",
            'c0fCS' : "f",
            'iGiZz' : "g",
            'GsT20' : "h",
            '1BWcd' : "i",
            '1LVBz' : "j",
            'ABR9N' : "k",
            'b3tDp' : "l",
            'TL2hv' : "m",
            'uhHnQ' : "n",
            '8XcXH' : "o",
            'leCmS' : "p",
            'KuIfX' : "q",
            'X3ilW' : "r",
            'mWnb5' : "s",
            'nXZpK' : "t",
            'dQT4L' : "u",
            'DOtQr' : "v",
            'l5uVp' : "w",
            'DbH7V' : "x",
            'Lm8Fq' : "y",
            'Nov2R' : "z",
            'hjQdG' : "æ",
            'Kafdg' : "ø",
            'fQAh6' : "å",
            '4aL1B' : "A",
            'StaOL' : "B",
            'Th80G' : "C",
            'l4B94' : "D",
            'gMO01' : "E",
            '13Pme' : "F",
            'eF103' : "G",
            'G0Qk8' : "H",
            'x73Qe' : "I",
            '3zI8E' : "J",
            'k75Xx' : "K",
            'Gb45p' : "L",
            '5a7ZX' : "M",
            'Df4B6' : "N",
            'rK31M' : "O",
            'Ye75j' : "P",
            '817xC' : "Q",
            'Dok94' : "R",
            'vN88X' : "S",
            'jT36X' : "T",
            'mS54V' : "U",
            'mS54V' : "V",
            'oA49M' : "W",
            'rT13f' : "X",
            '7A1iH' : "Y",
            'Rd30Q' : "Z",
            'L6c0z' : "Æ",
            'A7c1T' : "Ø",
            'Qg1N2' : "Å",
            'i0DQd' : " "}

help = '''
    python3 encoder.py [option] <string> ... [-e] <string> | [-d] <string
    wrap in quotes to encode strings with spaces

    -e, --encode <string>   encode string
    -d, --decode <string>   decode string
        '''

def main(argv):
    try:
        opts, args = getopt.getopt(argv,"he:d:",["ifile=","ofile="])
    except getopt.GetoptError:
        print (help)
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print(help)
            sys.exit()

        elif opt in ("-e", "--encode"):
            encode = arg
            encode_phrase = []

            try:
                for i in list(encode):
                    p = encoding[i]
                    encode_phrase.append(p)
                print('\n' + 'Encoded string: ' + ''.join(encode_phrase) + '\n')
            except:
                print('\n Invalid string, please try again. \n')

        elif opt in ("-d", "--decode"):
            decode = arg
            decode_phrase = []

            try:
                r = [decode[i:i+5] for i in range(0, len(decode), 5)]
                for i in r:
                    p = decoding[i]
                    decode_phrase.append(p)
                print('\n' + 'Decoded string: ' + ''.join(decode_phrase) + '\n')
            except:
                print('\n Invalid string, please try again. \n')

if __name__ == "__main__":
   main(sys.argv[1:])

# old code
'''
def encode():

    encode = input("\nEnter string to encode: ")
    encode_phrase = []

    for i in list(encode):
        p = encoding[i]
        encode_phrase.append(p)

    print('\n')
    print('Encoded string: ' + ''.join(encode_phrase))

def decode():

    decode = input("\nEnter string to decode: ")
    decode_phrase = []

    r = [decode[i:i+5] for i in range(0, len(decode), 5)]

    for i in r:
        p = decoding[i]
        decode_phrase.append(p)

    print('\n')
    print('Decoded string: ' + ''.join(decode_phrase))
'''


#Serialization = convert python obj into byte stream and save it to a file
import pickle

example_dict ={1:"word1",2:"word2",3:"word3"}

#serialization of a dictionary
pickle_out = open("dict.tar","wb")
#dump (object,file)
pickle.dump(example_dict,pickle_out)
pickle_out.close()

#deserialization
##pickle_in=open("dict.pickle","rb")
##new_dict=pickle.load(pickle_in)
###print (new_dict)

#--------------------------
example_list=["get","post","put","delete"]

with open('lista.pickle','wb') as f:
    pickle.dump(example_list,f)

with open('lista.pickle','rb') as f:
    pickle_out=pickle.load(f)
print(pickle_out)


#Answer
# idea : split a string into chracters, then check whether it matches to the target character.
# Original Answer : C => This version : Python 3.8

Matrix = [[0]*5 for i in range(5)]

Matrix[0][0] = "U"
Matrix[0][1] = "X"
Matrix[0][2] = "G"
Matrix[0][3] = "X"
Matrix[0][4] = "X"

Matrix[1][0] = "R"
Matrix[1][1] = "P"
Matrix[1][2] = "I"
Matrix[1][3] = "T"
Matrix[1][4] = "O"

Matrix[2][0] = "L"
Matrix[2][1] = "R"
Matrix[2][2] = "A"
Matrix[2][3] = "N"
Matrix[2][4] = "Q"

Matrix[3][0] = "P"
Matrix[3][1] = "E"
Matrix[3][2] = "E"
Matrix[3][3] = "Z"
Matrix[3][4] = "R"

Matrix[4][0] = "M"
Matrix[4][1] = "T"
Matrix[4][2] = "T"
Matrix[4][3] = "Y"
Matrix[4][4] = "S"

dx = [-1,-1,-1,1,1,1,0,0]
dy = [-1,0,1,-1,0,1,-1,1]

def hasWord(x,y, word):

    if (not inRange(x,y)):
        return False #

    if (Matrix[x][y] != word[0]):
        return False #

    if (len(word) == 1):
        return True #

    for direction in range(0, 8):
        nextX = x + dx[direction]
        nextY = y + dy[direction]

        if (hasWord(nextX, nextY, word[1:])):
            return True

def inRange(x,y) :
    return x <= 4 and y <= 4 and x >= 0 and y >= 0

print(hasWord(2,1,"REPEAT"))

# this answer utilizes the idea of spliting a string into characters.
# it effectively shrink the length of the code.
# this answer uses hasWord correctly and appropriately.
# this answer checks first "inRange()" and "len(word)"(or word check) to shrink length of code.
# it's really important to check the cases (true or false) first if we are using Recursive upon implementation.
# Also, it is important to know how to handle string with Python 3!

[Book]Algorithmic Problem Solving Strategies/Complete Serach/Boggle/Boggle_Answer.py


# Copyright 2021 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import os
import re
import sys

_CWD = os.getcwd()
_HERE_DIR = os.path.dirname(__file__)
_SRC_DIR = os.path.normpath(os.path.join(_HERE_DIR, '..', '..'))

sys.path.append(os.path.join(_SRC_DIR, 'third_party', 'node'))
import node
import node_modules

def main(argv):
  parser = argparse.ArgumentParser()
  parser.add_argument('--out_dir', required=True)
  parser.add_argument('--root_dir', required=True)
  parser.add_argument('--js_files', nargs='*', required=True)
  args = parser.parse_args(argv)

  js_files = [os.path.join(args.root_dir, f) for f in args.js_files]

  node.RunNode([
      node_modules.PathToTypescript(),
      '--declaration',
      '--allowJs',
      '--emitDeclarationOnly',
      '--removeComments',
      '--noResolve',
      '--rootDir',
      args.root_dir,
      '--outDir',
      args.out_dir,
  ] + js_files)

if __name__ == '__main__':
  main(sys.argv[1:])


#!/usr/bin/env python
"""Mixture model using MAP estimation.

We posit a collapsed mixture of Gaussians.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import edward as ed
import numpy as np
import tensorflow as tf

from edward.models import PointMass
from edward.stats import dirichlet, invgamma, multivariate_normal_diag, norm
from edward.util import get_dims, log_sum_exp

class MixtureGaussian:
  """
  Mixture of Gaussians

  p(x, z) = [ prod_{n=1}^N sum_{k=1}^K pi_k N(x_n; mu_k, sigma_k) ]
            [ prod_{k=1}^K N(mu_k; 0, cI) Inv-Gamma(sigma_k; a, b) ]
            Dirichlet(pi; alpha)

  where z = {pi, mu, sigma} and for known hyperparameters a, b, c, alpha.

  Parameters
  ----------
  K : int
    Number of mixture components.
  D : float, optional
    Dimension of the Gaussians.
  """
  def __init__(self, K, D):
    self.K = K
    self.D = D
    self.n_vars = (2 * D + 1) * K

    self.a = 1.0
    self.b = 1.0
    self.c = 3.0
    self.alpha = tf.ones([K])

  def log_prob(self, xs, zs):
    """Return scalar, the log joint density log p(xs, zs)."""
    x = xs['x']
    pi, mus, sigmas = zs['pi'], zs['mu'], zs['sigma']
    log_prior = dirichlet.logpdf(pi, self.alpha)
    log_prior += tf.reduce_sum(norm.logpdf(mus, 0.0, self.c))
    log_prior += tf.reduce_sum(invgamma.logpdf(sigmas, self.a, self.b))

    # log-likelihood is
    # sum_{n=1}^N log sum_{k=1}^K exp( log pi_k + log N(x_n; mu_k, sigma_k) )
    # Create a K x N matrix, whose entry (k, n) is
    # log pi_k + log N(x_n; mu_k, sigma_k).
    N = get_dims(x)[0]
    matrix = []
    for k in range(self.K):
      matrix += [tf.ones(N) * tf.log(pi[k]) +
                 multivariate_normal_diag.logpdf(x,
                 mus[(k * self.D):((k + 1) * self.D)],
                 sigmas[(k * self.D):((k + 1) * self.D)])]

    matrix = tf.pack(matrix)
    # log_sum_exp() along the rows is a vector, whose nth
    # element is the log-likelihood of data point x_n.
    vector = log_sum_exp(matrix, 0)
    # Sum over data points to get the full log-likelihood.
    log_lik = tf.reduce_sum(vector)

    return log_prior + log_lik

def build_toy_dataset(N):
  pi = np.array([0.4, 0.6])
  mus = [[1, 1], [-1, -1]]
  stds = [[0.1, 0.1], [0.1, 0.1]]
  x = np.zeros((N, 2), dtype=np.float32)
  for n in range(N):
    k = np.argmax(np.random.multinomial(1, pi))
    x[n, :] = np.random.multivariate_normal(mus[k], np.diag(stds[k]))

  return x

ed.set_seed(42)
x_train = build_toy_dataset(500)

K = 2
D = 2
model = MixtureGaussian(K, D)

qpi = PointMass(params=ed.to_simplex(tf.Variable(tf.random_normal([K - 1]))))
qmu = PointMass(params=tf.Variable(tf.random_normal([K * D])))
qsigma = PointMass(params=tf.exp(tf.Variable(tf.random_normal([K * D]))))

data = {'x': x_train}
inference = ed.MAP({'pi': qpi, 'mu': qmu, 'sigma': qsigma}, data, model)
inference.run(n_iter=500, n_minibatch=10)


import random

answers = []

def bridge_keeper():

    questions = ["What is your favourite colour?", "What is the airspeed velocity of an unladen swallow?","What is the capital of Assyria?"]
    correct_answer = "African or European?"

    print("STOP!!!!\nThose who approach the Bridge of Death must answer me these questions three, ere the other side they see.\n")
    # the built in input function can prompt with a string, and then return whatever the user has input into the console as a string.  Even if it's a number!!!
    name = input("What is your name?\n")
    quest = input("What is your quest?\n")
    random_question = random.randint(0,len(questions)-1)
    third = input(f"{questions[random_question]}\n")

    # What is your favourite colour?
    if random_question == 0:
        # checks to see if the color has been guessed already.
        if third in answers:
            print("You have been casted into gorge!!!\n")
            return True
        else:
            answers.append(third)
            print("Right. Off you go.\n")
            return True
    # What is the velocity of an unladen swallow?
    elif random_question == 1:
        if third == correct_answer:
            print("Wait... I don't know.  AHHHHHHH!!!!!!\nThe bridge keeper was casted into the gorge.")
            return False
        else:
            print("You have been casted into gorge!!!\n")
            return True
    # What is the capital of Assyria?
    elif random_question == 2:
        # checks for correct answer
        if third == "Ninevah":
            print("Right. Off you go.\n")
            return True
        else:
            print("You have been casted into gorge!!!\n")
            return True

isGuessing = True

while isGuessing == True:
    isGuessing = bridge_keeper()


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

import sys
sys.path.append('/home/takagi/git/analytics/analytics_tool')
import utility.db_utility as db_util
from datetime import datetime as dt
import datetime
import commands
import utility.device_master as dev_master
import utility.date_func as date_func

argvs = sys.argv
install_date = int(argvs[1].replace("-", ""))
d007 = date_func.ndaysago(install_date, -7)
d014 = date_func.ndaysago(install_date, -14)
d035 = date_func.ndaysago(install_date, -35)
d060 = date_func.ndaysago(install_date, -60)
d100 = date_func.ndaysago(install_date, -100)
d180 = date_func.ndaysago(install_date, -180)
d370 = date_func.ndaysago(install_date, -370)
d730 = date_func.ndaysago(install_date, -730)

user_data = {}
#DB
db = {
    "meta"   : { "server" : "inhousedb.smartsrv.in",
                 "db_name" : "statistics",
                 "table" : ["mnl_metalog"],
                 "colum" : "*"
                 },
    }

#インストールユーザーを抽出する
logdb = db_util.db_command("takagi", "7TgYiaI9", db["meta"]["server"], db["meta"]["db_name"])
sql = "select * from mnl_metalog where regdate = " + str(install_date)
log = logdb.db_anyselect(sql)
for v in log:
     user_data.setdefault(v["uid"], {})
     user_data[v["uid"]].setdefault(v["app_id"], {})
     user_data[v["uid"]][v["app_id"]].setdefault("regdate", int(v["regdate"]))
     user_data[v["uid"]][v["app_id"]].setdefault("os_type", v["os_type"])
     user_data[v["uid"]][v["app_id"]].setdefault("p007", 0)
     user_data[v["uid"]][v["app_id"]].setdefault("p014", 0)
     user_data[v["uid"]][v["app_id"]].setdefault("p035", 0)
     user_data[v["uid"]][v["app_id"]].setdefault("p060", 0)
     user_data[v["uid"]][v["app_id"]].setdefault("p100", 0)
     user_data[v["uid"]][v["app_id"]].setdefault("p180", 0)
     user_data[v["uid"]][v["app_id"]].setdefault("p370", 0)
     user_data[v["uid"]][v["app_id"]].setdefault("p730", 0)
     if int(v["date"]) <= d007:
          user_data[v["uid"]][v["app_id"]]["p007"] += float(v["pay_all"])
     if int(v["date"]) <= d014:
          user_data[v["uid"]][v["app_id"]]["p014"] += float(v["pay_all"])
     if int(v["date"]) <= d035:
          user_data[v["uid"]][v["app_id"]]["p035"] += float(v["pay_all"])
     if int(v["date"]) <= d060:
          user_data[v["uid"]][v["app_id"]]["p060"] += float(v["pay_all"])
     if int(v["date"]) <= d100:
          user_data[v["uid"]][v["app_id"]]["p100"] += float(v["pay_all"])
     if int(v["date"]) <= d180:
          user_data[v["uid"]][v["app_id"]]["p180"] += float(v["pay_all"])
     if int(v["date"]) <= d370:
          user_data[v["uid"]][v["app_id"]]["p370"] += float(v["pay_all"])
     if int(v["date"]) <= d730:
          user_data[v["uid"]][v["app_id"]]["p730"] += float(v["pay_all"])

#集計
summary = {}

for uid, data0 in user_data.items():
     for app_id, data in data0.items():
          summary.setdefault(data["regdate"], {})
          summary[data["regdate"]].setdefault(app_id, {})
          summary[data["regdate"]][app_id].setdefault(data["os_type"], {})
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("uu", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("puu_007", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("sum_007", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("puu_014", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("sum_014", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("puu_035", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("sum_035", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("puu_060", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("sum_060", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("puu_100", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("sum_100", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("puu_180", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("sum_180", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("puu_370", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("sum_370", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("puu_730", 0)
          summary[data["regdate"]][app_id][data["os_type"]].setdefault("sum_730", 0)
          summary[data["regdate"]][app_id][data["os_type"]]["uu"] += 1
          if data["p007"] > 0:
               summary[data["regdate"]][app_id][data["os_type"]]["puu_007"] += 1
               summary[data["regdate"]][app_id][data["os_type"]]["sum_007"] += data["p007"]
          if data["p014"] > 0:
               summary[data["regdate"]][app_id][data["os_type"]]["puu_014"] += 1
               summary[data["regdate"]][app_id][data["os_type"]]["sum_014"] += data["p014"]
          if data["p035"] > 0:
               summary[data["regdate"]][app_id][data["os_type"]]["puu_035"] += 1
               summary[data["regdate"]][app_id][data["os_type"]]["sum_035"] += data["p035"]
          if data["p060"] > 0:
               summary[data["regdate"]][app_id][data["os_type"]]["puu_060"] += 1
               summary[data["regdate"]][app_id][data["os_type"]]["sum_060"] += data["p060"]
          if data["p100"] > 0:
               summary[data["regdate"]][app_id][data["os_type"]]["puu_100"] += 1
               summary[data["regdate"]][app_id][data["os_type"]]["sum_100"] += data["p100"]
          if data["p180"] > 0:
               summary[data["regdate"]][app_id][data["os_type"]]["puu_180"] += 1
               summary[data["regdate"]][app_id][data["os_type"]]["sum_180"] += data["p180"]
          if data["p370"] > 0:
               summary[data["regdate"]][app_id][data["os_type"]]["puu_370"] += 1
               summary[data["regdate"]][app_id][data["os_type"]]["sum_370"] += data["p370"]
          if data["p730"] > 0:
               summary[data["regdate"]][app_id][data["os_type"]]["puu_730"] += 1
               summary[data["regdate"]][app_id][data["os_type"]]["sum_730"] += data["p730"]
#出力
fname = "/home/takagi/git/analytics/analytics_tool/data_temp/summary_ltv" + str(install_date)
out = open(fname, 'w')

for reg, d0 in summary.items():
     for app, d1 in d0.items():
          for os, d2 in d1.items():
               arpu007 = d2["sum_007"] / d2["uu"] if d2["uu"] != 0 else 0
               arppu007 = d2["sum_007"] / d2["puu_007"] if d2["puu_007"] != 0 else 0
               arpu014 = d2["sum_014"] / d2["uu"] if d2["uu"] != 0 else 0
               arppu014 = d2["sum_014"] / d2["puu_014"] if d2["puu_014"] != 0 else 0
               arpu035 = d2["sum_035"] / d2["uu"] if d2["uu"] != 0 else 0
               arppu035 = d2["sum_035"] / d2["puu_035"] if d2["puu_035"] != 0 else 0
               arpu060 = d2["sum_060"] / d2["uu"] if d2["uu"] != 0 else 0
               arppu060 = d2["sum_060"] / d2["puu_060"] if d2["puu_060"] != 0 else 0
               arpu100 = d2["sum_100"] / d2["uu"] if d2["uu"] != 0 else 0
               arppu100 = d2["sum_100"] / d2["puu_100"] if d2["puu_100"] != 0 else 0
               arpu180 = d2["sum_180"] / d2["uu"] if d2["uu"] != 0 else 0
               arppu180 = d2["sum_180"] / d2["puu_180"] if d2["puu_180"] != 0 else 0
               arpu370 = d2["sum_370"] / d2["uu"] if d2["uu"] != 0 else 0
               arppu370 = d2["sum_370"] / d2["puu_370"] if d2["puu_370"] != 0 else 0
               arpu730 = d2["sum_730"] / d2["uu"] if d2["uu"] != 0 else 0
               arppu730 = d2["sum_730"] / d2["puu_730"] if d2["puu_730"] != 0 else 0

               yyyymmdd, yyyymm, year, month, date = date_func.get_yyyymmdd(reg)
               record = yyyymmdd + "\t" + yyyymm + "\t" + year + "\t" + month + "\t" + date + "\t"
               record += app + "\t" + os + "\t"
               record += str(arpu007) + "\t" + str(arppu007) + "\t" + str(d2["puu_007"]) + "\t"
               record += str(arpu014) + "\t" + str(arppu014) + "\t" + str(d2["puu_014"]) + "\t"
               record += str(arpu035) + "\t" + str(arppu035) + "\t" + str(d2["puu_035"]) + "\t"
               record += str(arpu060) + "\t" + str(arppu060) + "\t" + str(d2["puu_060"]) + "\t"
               record += str(arpu100) + "\t" + str(arppu100) + "\t" + str(d2["puu_100"]) + "\t"
               record += str(arpu180) + "\t" + str(arppu180) + "\t" + str(d2["puu_180"]) + "\t"
               record += str(arpu370) + "\t" + str(arppu370) + "\t" + str(d2["puu_370"]) + "\t"
               record += str(arpu730) + "\t" + str(arppu730) + "\t" + str(d2["puu_730"]) + "\n"

               out.write(record)
out.close()

#load
st = db_util.db_command("takagi", "7TgYiaI9", "inhousedb.smartsrv.in", "statistics")
st.db_load_replace(fname, "mnl_kpi_ltv")

#delete
commands.getoutput('rm ' + fname)


"""empty message

Revision ID: 342502d70cac
Revises: a4f302707dd4
Create Date: 2018-06-02 23:09:29.892798

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '342502d70cac'
down_revision = 'a4f302707dd4'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('offers',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=30), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('offer_articles',
    sa.Column('offer_id', sa.Integer(), nullable=True),
    sa.Column('article_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['article_id'], ['articles.id'], ),
    sa.ForeignKeyConstraint(['offer_id'], ['offers.id'], )
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('offer_articles')
    op.drop_table('offers')
    # ### end Alembic commands ###


import argparse, os

def parse():
  parser = argparse.ArgumentParser(description="Evaluate the truth")
  parser.add_argument('files', type=str, nargs="*")
  parser.add_argument('-v', '--verbose', action="store_true", help='Verbose output')
  args = parser.parse_args()
  if len(args.files) == 0:
    raise Exception("No file arguments found")
  files = args.files
  importedFiles = []
  errorFiles = []
  successFiles = []
  for currentFile in files:
    if (os.path.isfile(currentFile)):
      importedFiles.append((open(currentFile, 'r').readlines()))
      successFiles.append(currentFile)
    else:
      errorFiles.append(currentFile)
  if len(errorFiles) != 0:
    if len(successFiles) != 0:
      return args, importedFiles
    else:
      raise Exception("No files found in supplied file arguments", errorFiles)
  return args, importedFiles


#%%
import os
os.chdir('../')
#%%
from luciferml.supervised.classification import Classification
import pandas as pd
# %%

# %%
dataset = pd.read_csv('examples\Social_Network_Ads.csv')
dataset.head()

# %%
# %%
X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1]

# %%
predictors = ['all']

for predictor in predictors:
    classifier = Classification(
        predictor=predictor, params={}, cv_folds=2, epochs=10, tune=True)
    classifier.fit(X, y)
    regression = classifier.result()
# %%


from sklearn.datasets import fetch_openml
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
import numpy as np

def get_data():
    X, y = fetch_openml('mnist_784', version=1, return_X_y=True, as_frame=True)
    X.to_feather('datasets/mnist_784.feather')
    pd.DataFrame(y).to_feather('datasets/mnist_784_labels.feather')

def load_data():
    return pd.read_feather('datasets/mnist_784.feather'), pd.read_feather('datasets/mnist_784_labels.feather')

def prepare_mnist_dataset():
    X, y = load_data()

    train_set_size = 60000
    X_train, X_test, y_train, y_test = X[:train_set_size], X[train_set_size:], y[:train_set_size], y[train_set_size:]

    X_train = X_train.sample(frac=1, random_state=42).reset_index(drop=True)
    y_train = y_train.sample(frac=1, random_state=42).reset_index(drop=True)

    return X_train.to_numpy(), X_test.to_numpy(), y_train.values.ravel(), y_test.values.ravel()

def train_knn(X_train, y_train):
    knn_params = {
        'n_neighbors': [1, 3, 5, 10],
        'weights': ['uniform', 'distance'],
    }

    knn_gscv = GridSearchCV(KNeighborsClassifier(), knn_params, cv=3, scoring='accuracy', n_jobs=1, verbose=10)
    knn_gscv.fit(X_train, y_train)
    return knn_gscv

# best: {'n_neighbors': 3, 'weights': 'distance'}