from IPython.core.display import display

from controller.game_controller import GameController, AIElements
from pprint import pprint
import pandas as pd
class GameViewCLI():
    """
    A class used to show the view in CLI
    """

    def __init__(self):
        self.gc = GameController()
        self.board_size = 9
        self.board = [[None for i in range(self.board_size)] for j in range(self.board_size)]

    def start_game_2_players(self):
        """
        Start the game in 2 players mode (no AI)
        To play use these command:
        "exit" : exit the game
        action_key : input the action key stated in the command line.
        """
        state = self.gc.state
        while(not AIElements.is_over(state)):
            pprint(AIElements.get_state_dict(state))
            collected_action = AIElements.get_possible_action(state)
            print("List of Action : ")
            pprint(collected_action)
            self.gc.debug_print_board(state)
            print("Mana (White, Black) = " + str(AIElements.get_players_mana(state)))
            print("Rune List:")
            print(AIElements.get_rune_information(state))
            inp = input("command : ")
            if inp in collected_action.keys():
                state = AIElements.result_function(state,collected_action[inp])
            if inp == 'exit':
                break

    def start_game_2_experimental_(self):
        """
        Start the game in 2 players mode (no AI)
        To play use these command:
        "exit" : exit the game
        action_key : input the action key stated in the command line.
        """
        returned_params = self.gc.play_with_two_players_start()
        returned_task_controller = returned_params['task']

        while (not returned_task_controller == "END_GAME"):
            print(returned_task_controller)
            self.parse_returned_params_change_player(returned_params)
            pprint(pd.DataFrame(self.board))
            pprint(self.possible_action)
            pprint(self.rune_list)
            pprint(self.player_list)
            inp = input("command : ")
            if inp in self.possible_action_keys:
                self.gc.receive_input_action_play(inp, self.possible_action[inp])
                returned_params = self.gc.get_whattodo_view()
                returned_task_controller = returned_params['task']
            elif inp == 'exit':
                break
            else:
                print("Wrong Input")

    def start_game_ai_experimental_(self):
        """
        Start the game in 2 players mode (no AI)
        To play use these command:
        "exit" : exit the game
        action_key : input the action key stated in the command line.
        """
        # returned_params = self.gc.play_with_two_players_start()
        returned_params = self.gc.play_with_ai_white()

        returned_task_controller = returned_params['task']

        while (not returned_task_controller == "END_GAME"):
            print(returned_task_controller)
            if returned_task_controller == 'CHANGE_PLAYER':
                self.parse_returned_params_change_player(returned_params)
                pd.options.display.max_columns = 10
                display(pd.DataFrame(self.board))
                pprint(self.possible_action)
                pprint(self.rune_list)
                pprint(self.player_list)
            elif returned_task_controller == 'AI_MOVE':
                self.parse_returned_params_ai_move(returned_params)
                if 'end' in returned_params:
                    break

                print("AI MOVE")
                pprint(returned_params['ai_action'])
                pprint(pd.DataFrame(self.board))
                print("----")
                self.parse_returned_params_change_player(returned_params)
                pprint(pd.DataFrame(self.board))
                pprint(self.possible_action)
                pprint(self.rune_list)
                pprint(self.player_list)
            inp = input("command : ")
            if inp in self.possible_action_keys:
                self.gc.receive_input_action_play(inp, self.possible_action[inp])
                returned_params = self.gc.get_whattodo_view()
                returned_task_controller = returned_params['task']

            elif inp == 'exit':
                break
            else:
                print("Wrong Input")

    def parse_returned_params_ai_move(self,returned_params):
        self.board = [[None for i in range(self.board_size)] for j in range(self.board_size)]

        # Prev state
        self.black_king = returned_params['prev_state']['black_king']
        self._pawn_write_to_board(self.black_king)
        self.white_king = returned_params['prev_state']['white_king']
        self._pawn_write_to_board(self.white_king)

        self.black_pawn_list = returned_params['prev_state']['black_pawn_list']
        for black_pawn in self.black_pawn_list:
            self._pawn_write_to_board(black_pawn)

        self.white_pawn_list = returned_params['prev_state']['white_pawn_list']
        for white_pawn in self.white_pawn_list:
            self._pawn_write_to_board(white_pawn)

        self.player_list = returned_params['prev_state']['player_list']
        self.rune_list = returned_params['prev_state']['rune_list']
        for rune in self.rune_list:
            self._rune_write_to_board(rune)

    def parse_returned_params_change_player(self,returned_params):
        self.board = [[None for i in range(self.board_size)] for j in range(self.board_size)]
        self.black_king = returned_params['state']['black_king']
        self._pawn_write_to_board(self.black_king)
        self.white_king = returned_params['state']['white_king']
        self._pawn_write_to_board(self.white_king)

        self.black_pawn_list = returned_params['state']['black_pawn_list']
        for black_pawn in self.black_pawn_list:
            self._pawn_write_to_board(black_pawn)

        self.white_pawn_list = returned_params['state']['white_pawn_list']
        for white_pawn in self.white_pawn_list:
            self._pawn_write_to_board(white_pawn)

        self.player_list = returned_params['state']['player_list']
        self.rune_list = returned_params['state']['rune_list']
        for rune in self.rune_list:
            self._rune_write_to_board(rune)
        self.possible_action = returned_params['possible_action']
        self.possible_action_keys = returned_params['possible_action'].keys()

    def _rune_write_to_board(self, rune):
        self.board[rune['y']][rune['x']] = 'Runeeeeee'

    def _pawn_write_to_board(self, pawn):
        self.board[pawn['y']][pawn['x']] = pawn['pawn_type'][0:2] + 'i' \
                                                        + str(pawn['pawn_index']) \
                                                        + 'a' \
                                                        + str(pawn['atk']) \
                                                        + 'h' \
                                                        + str(pawn['hp']) \
                                                        + 'p' \
                                                        + str(pawn['step']) \
                                                        + 't' \
                                                        + str(pawn['status'])[0:2]

    def one_action(self, list_action):
        """
        Concatenate all list of actions to become one list

        ...

        Attributes
        ----------
        list_action : list
            a list of actions of Kings, Players, and Pawns

        Returns
        -------
        list
            a concatenated all list of actions.
        """
        action_list_completed = {}
        for act in list_action:
            if 'action' in act and len(act['action']) > 0:
                for key,value in act['action'].items():
                    action_list_completed[key] = value
        return action_list_completed


import logging
import os
import pickle
import atexit

class Vagrantfile:
    def __init__(self, environment, provider=None):
        """Represents a Vagrantfile.

        Args:
            environment (pyvagrant.environments.Environment): The environment
                to for this Vagrantfile.
            provider (str, optional): What provider to use. Defaults to the
                default provider.
        """
        self._environment = environment
        self._provider = provider or self._environment._vagrant.default_provider
        self.boxes = {}
        atexit.register(self._sync)

    def add_box(self, box, name=None):
        """Adds a box to this vagrantfile and give's it the name.

        Args:
            box (pyvagrant.box.Box): Box to add
            name (str, optional): What to name it (default is 'default')
        """

        if not isinstance(box, Box):
            LOGGER.error("Must add Box type to Vagrantfile. Got type: " + str(type(box_)))
            return

        if not name: name = "default"
        self.boxes[name] = {
            'box': box,
            'options': BoxOptions(vagrant=self._environment._vagrant, provider=self._provider),
        }

    def __str__(self):
        out = VAGRANTFILE_BASE

        config = ""
        for name, box in self.boxes.items():
            config += 'config.vm.define "{name}" do |box|\n'.format(name=name)
            config += 'box.vm.box   = ' + box['box'].name + '\n'
            # Box config
            config += 'end\n'

        return VAGRANTFILE_BASE.format(config=config)

    def _sync(self):
        """Sync's the current object to it's pickle file."""
        with open(self._environment._vagrantfile_pickle_path, "wb") as f:
            pickle.dump(self, f)

    def __repr__(self):
        return "<Vagrantfile " + self._environment.name + ">"

    # For pickling issues
    def __getstate__(self):
        return {'_provider': self._provider, 'boxes': self.boxes}

    def __setstate__(self, state):
        self.__dict__.update(state)
        # Make sure we sync at exit
        atexit.register(self._sync)

from .box import Box, BoxOptions

LOGGER = logging.getLogger(__name__)

VAGRANTFILE_BASE = r"""Vagrant.configure("2") do |config|
{config}
end"""


import pygame as pg
import Engine
from os import system

WIDTH = HEIGHT = 600
WIN = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption('MK Chess')
FPS = 60
bdsz = 600
sqsz = bdsz/8
pcnm = ['wp1', 'wp2', 'wp3', 'wp4', 'wR1', 'wR2', 'wN1', 'wN2',
        'bp1', 'bp2', 'bp3', 'bp4', 'bR1', 'bR2', 'bN1', 'bN2',
        'bB1', 'bB2', 'wB1', 'wB2', 'wp6', 'wp7', 'wp8', 'bp5',
        'bp6', 'bp7', 'bp8', 'wp5', 'wK' , 'wQ' , 'bK' , 'bQ' ]

pcload = [{'id': p,
           'img': pg.transform.scale(pg.image.load('pcs/' + p[:2] + '.png'), (sqsz, sqsz)),
           'pos': (0, 0),
           'type': p[:2]} for p in pcnm]

board = Engine.Board(pcload, sqsz)
board.resetboard()
sqsl = [(-1, -1), (-1, -1)]

def draw(click, mpos):
    drawboard()
    draweffects(click, mpos)
    drawpieces()
    drawmoving(mpos)
    pg.display.update()

def drawboard():
    cor = [pg.Color(255, 255, 255), pg.Color(255, 150, 150)]
    for x in range(8):
        for x1 in range(8):
            sqcor = cor[(x + x1) % 2]
            pg.draw.rect(WIN, sqcor, pg.Rect(x*sqsz, x1*sqsz, sqsz, sqsz))

def draweffects(click, mpos):
    if click:
        mx, my = mpos
        mx = mx // sqsz
        my = my // sqsz
        pg.draw.rect(WIN, (255, 100, 100), pg.Rect(mx*sqsz, my*sqsz, sqsz, sqsz))

def drawmoving(mpos):
    for pc in board.board:
        if board.moving is not None:
            if board.moving['id'] == pc['id']:
                mx, my = mpos
                mx -= sqsz / 2
                my -= sqsz / 2
                pc['pos'] = (-1, -1)
                WIN.blit(board.moving['img'], (mx, my))

def drawpieces():
    for pc in board.board:
        if pc['pos'] != (-1, -1):
            WIN.blit(pc['img'], pc['pos'])

def main():
    run = True
    clock = pg.time.Clock()
    click = False

    while run:

        clock.tick(FPS)
        mpos = pg.mouse.get_pos()
        for event in pg.event.get():
            if event.type == pg.QUIT:
                run = False
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_0:
                    board.resetboard()

            if event.type == pg.MOUSEBUTTONDOWN:
                a1, a2 = mpos[0] // sqsz, mpos[1] // sqsz
                a1, a2 = a1 * sqsz, a2 * sqsz
                click = True
                for e in board.board:
                    if e['pos'] == (a1, a2):
                        board.moving = e.copy()

            elif event.type == pg.MOUSEBUTTONUP:
                a1, a2 = mpos[0] // sqsz, mpos[1] // sqsz
                a1, a2 = a1 * sqsz, a2 * sqsz
                board.moving2 = (a1, a2)

                if click:
                    if board.moving['id'] != 'null':
                        val = board.validate(board.moving, board.moving2)
                        if val:
                            board.moving['pos'] = board.moving2
                            a = board.moving
                            board.stopmoving(a, board.moving2)
                        else:
                            board.failmove()
                    click = False

        draw(click, mpos)

    pg.quit()

MK-Chess-v0.2 ( python project )/Chess_UI.py


import json
import os

PATH_TO_DATASET = '../flickr30k_images'

with open(PATH_TO_DATASET + '/1.json', 'rb') as f:
    train_ann = json.load(f)
with open(PATH_TO_DATASET + '/test.json', 'rb') as f:
    test_ann = json.load(f)
with open(PATH_TO_DATASET + '/val.json', 'rb') as f:
    val_ann = json.load(f)

train_ids = [ann['filename'] for ann in train_ann]
test_ids = [ann['filename'] for ann in test_ann]
val_ids = [ann['filename'] for ann in val_ann]
dataset_ids = []
dataset_ids.extend(train_ids)
dataset_ids.extend(test_ids)
dataset_ids.extend(val_ids)
print(dataset_ids)

path_to_images = '../flickr30k_images/train/1'
for id in os.listdir(path_to_images):
    if id not in dataset_ids:
        os.remove(os.path.join(path_to_images,id))


import os
from torch.backends import cudnn

from config import Config
from utils.logger import setup_logger
from datasets import make_dataloader
from model import make_model
from solver import make_optimizer, WarmupMultiStepLR
from loss import make_loss
from processor import do_train

if __name__ == '__main__':
    cfg = Config()
    if not os.path.exists(cfg.LOG_DIR):
        os.mkdir(cfg.LOG_DIR)
    logger = setup_logger('{}'.format(cfg.PROJECT_NAME), cfg.LOG_DIR)
    logger.info("Running with config:\n{}".format(cfg.CFG_NAME))
    os.environ['CUDA_VISIBLE_DEVICES'] = cfg.DEVICE_ID

    cudnn.benchmark = True
    # This flag allows you to enable the inbuilt cudnn auto-tuner to find the best algorithm to use for your hardware.

    train_loader, val_loader, num_query, num_classes = make_dataloader(cfg)
    model = make_model(cfg, num_class=num_classes)

    loss_func, center_criterion = make_loss(cfg, num_classes=num_classes)

    optimizer, optimizer_center = make_optimizer(cfg, model, center_criterion)
    scheduler = WarmupMultiStepLR(optimizer, cfg.STEPS, cfg.GAMMA,
                                  cfg.WARMUP_FACTOR,
                                  cfg.WARMUP_EPOCHS, cfg.WARMUP_METHOD)

    do_train(
        cfg,
        model,
        center_criterion,
        train_loader,
        val_loader,
        optimizer,
        optimizer_center,
        scheduler,  # modify for using self trained model
        loss_func,
        num_query
    )


import cv2
import os
from concurrent.futures import ThreadPoolExecutor

def crop_img(resolution):
    aim_resolution = resolution

    files_num_f = len(os.listdir(os.path.dirname('/home/psc/Desktop/carset/Full_Frame/1080p/f/')))
    files_num_l = len(os.listdir(os.path.dirname('/home/psc/Desktop/carset/Full_Frame/1080p/l/')))
    files_num_r = len(os.listdir(os.path.dirname('/home/psc/Desktop/carset/Full_Frame/1080p/r/')))

    files_list_f = [i + 1 for i in range(files_num_f)]
    files_list_l = [i + 1 for i in range(files_num_l)]
    files_list_r = [i + 1 for i in range(files_num_r)]

    resolution_dir = '/home/psc/Desktop/carset/Cropped/' + aim_resolution

    if not os.path.exists(resolution_dir):
        os.makedirs(resolution_dir)
        os.makedirs(resolution_dir + '/f')
        os.makedirs(resolution_dir + '/l')
        os.makedirs(resolution_dir + '/r')
    else:
        if not os.path.exists(resolution_dir + '/f'):
            os.makedirs(resolution_dir + '/f')
        if not os.path.exists(resolution_dir + '/l'):
            os.makedirs(resolution_dir + '/l')
        if not os.path.exists(resolution_dir + '/r'):
            os.makedirs(resolution_dir + '/r')

    def crop_image(addr_in, addr_out, num):
        num_str = "{:0>6d}".format(num)
        read_address = addr_in + num_str + '.jpg'
        write_address = addr_out + num_str + '.jpg'
        img = cv2.imread(read_address)
        # get_resolution = lambda shape: (shape[1], shape[0])
        # img_resolution = get_resolution(img.shape)
        img_shape = img.shape[0:2]
        img_width = img_shape[1]
        y_crop_start = round(img_shape[0] * 0.4)
        y_crop_end = round(img_shape[0] * 0.6)

        cropped = img[y_crop_start: y_crop_end, 0: img_width]
        cv2.imwrite(write_address, cropped)
        print(addr_out + " finish " + str(num))

    call_crop_args_f = [['/home/psc/Desktop/carset/Full_Frame/' + aim_resolution + '/f/'] * files_num_f,
                        ['/home/psc/Desktop/carset/Cropped/' + aim_resolution + '/f/'] * files_num_f, files_list_f]
    call_crop_args_l = [['/home/psc/Desktop/carset/Full_Frame/' + aim_resolution + '/l/'] * files_num_l,
                        ['/home/psc/Desktop/carset/Cropped/' + aim_resolution + '/l/'] * files_num_l, files_list_l]
    call_crop_args_r = [['/home/psc/Desktop/carset/Full_Frame/' + aim_resolution + '/r/'] * files_num_r,
                        ['/home/psc/Desktop/carset/Cropped/' + aim_resolution + '/r/'] * files_num_r, files_list_r]

    with ThreadPoolExecutor(max_workers=24) as pool:
        pool.map(crop_image, *call_crop_args_f)
        pool.map(crop_image, *call_crop_args_l)
        pool.map(crop_image, *call_crop_args_r)

resolution_list = ['180p', '135p', '100p', '90p', '54p', '45p']

for res in resolution_list:
    crop_img(res)


#where noted, code adapted from http://code.enthought.com/projects/traits/docs/html/tutorials/traits_ui_scientific_app.html
import wx
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.backends.backend_wx import NavigationToolbar2Wx

from enthought.traits.api import Any, Instance
from enthought.traits.ui.wx.editor import Editor
from enthought.traits.ui.wx.basic_editor_factory import BasicEditorFactory

from threading import Thread
from time import sleep
from enthought.traits.api import *
from enthought.traits.ui.api import View, Item, Group, HSplit, Handler, VSplit, VGroup, HGroup
from enthought.traits.ui.menu import NoButtons
from matplotlib.figure import Figure
from scipy import *
from numpy import *
import wx
import urllib2, json, urllib
import Image, StringIO, ImageFilter, ImageOps

#class from http://code.enthought.com/projects/traits/docs/html/tutorials/traits_ui_scientific_app.html
#to display matplotlib figures
class _MPLFigureEditor(Editor):

	scrollable  = True

	def init(self, parent):
		self.control = self._create_canvas(parent)
		self.set_tooltip()

	def update_editor(self):
		pass

	def _create_canvas(self, parent):
		""" Create the MPL canvas. """
		# The panel lets us add additional controls.
		panel = wx.Panel(parent, -1, style=wx.CLIP_CHILDREN)
		sizer = wx.BoxSizer(wx.VERTICAL)
		panel.SetSizer(sizer)
		# matplotlib commands to create a canvas
		mpl_control = FigureCanvas(panel, -1, self.value)
		sizer.Add(mpl_control, 1, wx.LEFT | wx.TOP | wx.GROW)
		toolbar = NavigationToolbar2Wx(mpl_control)
		sizer.Add(toolbar, 0, wx.EXPAND)
		self.value.canvas.SetMinSize((10,10))
		return panel

#class from http://code.enthought.com/projects/traits/docs/html/tutorials/traits_ui_scientific_app.html
#to display matplotlib figures
class MPLFigureEditor(BasicEditorFactory):

	klass = _MPLFigureEditor

class Query(HasTraits):
	'''object representing the query search box'''
	query_string = String('Enter a search term')

class ControlPanel(HasTraits):
	""" object that controls the display and contains the user interface.
	"""

	#the search box
	query = Instance('Query', ())
	#the url display box
	url = String('Image URL will appear here')
	#the button to run the search query
	do_search = Button('Run Query')
	#the figure
	figure = Instance(Figure)

	#the bottons to do the image editing
	blur = Button('Blur')
	contour = Button('Contour')
	contrast = Button('Auto Contrast')
	revert = Button('Revert')
	edge = Button('Edges')

	#images that are being displayed
	self.images = {}

	#configure the view for the control panel
	view = View(VGroup(
					VGroup(
						HGroup(
							Item('query', show_label=False, resizable=True ),
							Item('do_search', show_label=False),
							style='custom',
						),
						label="Input",),
					VGroup(

							Item('url', show_label=False, style='readonly', springy=True,),

					label='Image URL',),
					HGroup(

							Item('blur', show_label=False,),
							Item('contour', show_label=False),
							Item('contrast', show_label=False),
							Item('edge', show_label=False),
							Item('revert', show_label=False),
					orientation='horizontal'),
				), width = 1.0,
				)

	def _do_search_fired(self):
		'''
		method called when the search botton is clicked
		'''

		#url to do the search
		searchString = 'http://api.bing.net/json.aspx?Appid=F03B735C20BDC55A5D86F403273383C55D36CDBA&'+urllib.urlencode({'query':self.query.query_string})+'&sources=image'
		#load the result
		result = json.load(urllib2.urlopen(searchString))
		resultList = result['SearchResponse']['Image']

		#check to see if any results were returned
		if 'Results' in resultList > 0:
			#if so, get the first url
			foundURL = resultList['Results'][0]['MediaUrl']

			#attempt to open the url corresponding to the image
			try:
				#get the image
				imgopener = urllib2.build_opener()
				imgopener.addheaders = [('User-agent', 'Mozilla/5.0')]
				imgpage = imgopener.open(foundURL)
				my_picture = imgpage.read()

				#change the contents of the url box
				self.url = result['SearchResponse']['Image']['Results'][0]['MediaUrl']

				#read the picture from the returned string
				img = Image.open(StringIO.StringIO(my_picture))

				#save the image and display it
				self.images = {}
				self.images['current'] = img
				self.images['original'] = img.copy()
				self.image_show(img)
			except:
				self.url = 'The url could no be opened'
		else:
			self.url = 'The search did not return any results'

	def _blur_fired(self):
		'''
		function for when the blur button is clicked--applies ImageFilter.BLUR filter to the image
		'''
		self.addFilter(ImageFilter.BLUR)

	def _contour_fired(self):
		'''
		function for when the contour button is clicked--applies ImageFilter.CONTOUR filter to the image
		'''
		self.addFilter(ImageFilter.CONTOUR)

	def _edge_fired(self):
		'''
		function for when the edge button is clicked--applies ImageFilter.EDGE_ENCHANCE_MORE filter to the image
		'''
		self.addFilter(ImageFilter.EDGE_ENHANCE_MORE)

	def _contrast_fired(self):
		'''
		function for when the contrast button is clicked--applies ImageOps.autocontrast method to the image
		'''
		self.images['current'] = ImageOps.autocontrast(self.images['current'])
		self.image_show(self.images['current'])

	def _revert_fired(self):
		'''
		restores the image to its original state
		'''
		self.images['current'] = self.images['original'].copy()
		self.image_show(self.images['current'])

	def addFilter(self, fil):
		'''
		method that applies a given filter fil to an image and then displays the result
		'''
		tostr = str(fil)
		if tostr not in self.images:
			self.images['current'] = self.images['current'].filter(fil)
		self.image_show(self.images['current'])

	def image_show(self, image):
		"""
		displays an image in the matplotlib window
		"""
		self.figure.axes[0].images=[]
		self.figure.axes[0].imshow(image, origin='lower')
		wx.CallAfter(self.figure.canvas.draw)

class MainWindowHandler(Handler):
	'''
	class to control exiting from the window
	'''
	def close(self, info, is_OK):
		return True

class MainWindow(HasTraits):
	"""
	the main window that controls everything
	adapted from http://code.enthought.com/projects/traits/docs/html/tutorials/traits_ui_scientific_app.html
	"""

	#the figure
	figure = Instance(Figure)

	#the control panel
	panel = Instance(ControlPanel)

	def _figure_default(self):
		'''
		set the defaults for the figure
		'''
		figure = Figure()
		figure.add_axes([0.05, 0.04, 0.9, 0.92])
		figure.axes[0].get_xaxis().set_ticks([])
		figure.axes[0].get_yaxis().set_ticks([])
		return figure

	def _panel_default(self):
		'''
		set the defaults for the control panel
		'''
		return ControlPanel(figure=self.figure)

	#display everything
	view = View(VSplit(Item('figure', editor=MPLFigureEditor(),
							dock='vertical'),
					   Item('panel', style="custom"),
					   show_labels=False,
					  ),
				resizable=True,
				height=0.9, width=0.5,
				handler=MainWindowHandler(),
				buttons=NoButtons)

if __name__ == '__main__':
	MainWindow().configure_traits()


import setuptools

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

setuptools.setup(
    name="IS_415_Project",
    version="0.0.2",
    author="Tate Gillespie",
    author_email="tate.gillespie@gmail.com",
    packages=["IS_415_Project"],
    description="A package for the IS 415 Project",
    long_description=description,
    long_description_content_type="text/markdown",
    url="https://github.com/tatertot365/415-Project-Package",
    license='MIT',
    python_requires='>=3.8',
    install_requires=[]
)


import matplotlib.pyplot as plt
from  matplotlib.colors import LinearSegmentedColormap
from random import random, seed
import numpy as np

seed(1234)

''' ---------- Plot Grid ---------- '''
data = np.fromfile("Cluster", dtype=np.int32, count=-1)
n2 = len(data)
L  = int(np.sqrt(n2))
Data = np.reshape(data, [L, L], order='A')
fig, ax = plt.subplots(figsize=(8,8))
ax.matshow(Data.astype(bool), cmap='Greys',  interpolation='nearest', origin='lower')
ax.set_xlabel("$x$")
ax.set_ylabel("$y$")
ax.xaxis.set_ticks_position('bottom')
plt.savefig("Grid.png", transparent=True, dpi=300)
plt.show()

n = int(Data.max())

''' ----- Define Random Color Map -----'''
colors = [(0,0,0)] + [(random(),random(),random()) for i in range(n)]
new_map = LinearSegmentedColormap.from_list('new_map', colors, N=n+1)

''' ---------- Plot Grid ---------- '''
fig, ax = plt.subplots(figsize=(8,8))
ax.matshow(Data, cmap=new_map,  interpolation='nearest', origin='lower')
ax.set_xlabel("$x$")
ax.set_ylabel("$y$")
ax.xaxis.set_ticks_position('bottom')
plt.savefig("Cluster.png", transparent=True, dpi=300)
plt.show()

''' ---------- Zoom Plot ---------- '''
extent = (0, L, 0, L)
fig, ax = plt.subplots(figsize=(8,8))
ax.matshow(Data, cmap=new_map,  interpolation='nearest', origin='lower', extent=extent)
axins = ax.inset_axes(
    [0.5, 0.5, 0.47, 0.47],
    xlim=(L/3, 2*L/5), ylim=(L/3, 2*L/5), xticklabels=[], yticklabels=[])
axins.matshow(Data, cmap=new_map,  interpolation='nearest', origin='lower', extent=extent)
ax.indicate_inset_zoom(axins, edgecolor="black", alpha=1)
ax.set_xlabel("$x$")
ax.set_ylabel("$y$")
ax.xaxis.set_ticks_position('bottom')
plt.savefig("ZoomCluster.png", transparent=True, dpi=300)
plt.show()


import pandas as pd
import numpy as np
import json
import gzip
import os
import pathlib

class DataParsing(object):
    """Reads in zipped json.gz and produces dataframe with each row an individual read"""

    def __init__(self, raw_data):
        self.raw_data = raw_data

    def find_keys(self, d: dict) -> list:
        """
        Extracts all keys from a dictionary recursively
        """
        keys = []
        for key, value in d.items():
            # recursively adds keys to list
            if isinstance(value, dict):
                keys.append(key)
                keys.extend(self.find_keys(value))
            else:
                keys.append(key)
        return keys

    def parse_seq_pos0_only(self, d: dict):
        """For each read of 9 data points, return lists of their info"""
        keys = self.find_keys(d)
        # extract info from each set of keys including adjacent positions
        transcript_id = keys[0]
        position = keys[1]
        sequences = [keys[2][0:5], keys[2][1:6], keys[2][2:7]]
        reads = d[keys[0]][keys[1]][keys[2]]
        transcript_id = [transcript_id] * len(reads)
        seq_pos, seq_seq, seq_dtime, seq_sd, seq_mean = [], [], [], [], []
        m1_seq, m1_dtime, m1_sd, m1_mean = [], [], [], []
        p1_seq, p1_dtime, p1_sd, p1_mean = [], [], [], []
        for read in reads:
            seq_pos.append(position)
            seq_seq.append(sequences[1])
            seq_dtime.append(read[3])
            seq_sd.append(read[4])
            seq_mean.append(read[5])
            m1_seq.append(sequences[0])
            m1_dtime.append(read[0])
            m1_sd.append(read[1])
            m1_mean.append(read[2])
            p1_seq.append(sequences[2])
            p1_dtime.append(read[6])
            p1_sd.append(read[7])
            p1_mean.append(read[8])
        return (
            transcript_id,
            seq_pos,
            seq_seq,
            seq_dtime,
            seq_sd,
            seq_mean,
            m1_seq,
            m1_dtime,
            m1_sd,
            m1_mean,
            p1_seq,
            p1_dtime,
            p1_sd,
            p1_mean,
        )

    def parse_data_pos0(self, data):
        """Iterate through data and extract data into lists for df creation"""
        (
            transcript_id,
            seq_pos,
            seq_seq,
            seq_dtime,
            seq_sd,
            seq_mean,
            m1_seq,
            m1_dtime,
            m1_sd,
            m1_mean,
            p1_seq,
            p1_dtime,
            p1_sd,
            p1_mean,
        ) = ([], [], [], [], [], [], [], [], [], [], [], [], [], [])
        for entry in data:
            (
                t_id,
                s_pos,
                s_seq,
                s_dtime,
                s_sd,
                s_mean,
                m_seq,
                m_dtime,
                m_sd,
                m_mean,
                p_seq,
                p_dtime,
                p_sd,
                p_mean,
            ) = self.parse_seq_pos0_only(entry)
            transcript_id.extend(t_id)
            seq_pos.extend(s_pos)
            seq_seq.extend(s_seq)
            seq_dtime.extend(s_dtime)
            seq_sd.extend(s_sd)
            seq_mean.extend(s_mean)
            m1_seq.extend(m_seq)
            m1_dtime.extend(m_dtime)
            m1_sd.extend(m_sd)
            m1_mean.extend(m_mean)
            p1_seq.extend(p_seq)
            p1_dtime.extend(p_dtime)
            p1_sd.extend(p_sd)
            p1_mean.extend(p_mean)
        return (
            transcript_id,
            seq_pos,
            seq_seq,
            seq_dtime,
            seq_sd,
            seq_mean,
            m1_seq,
            m1_dtime,
            m1_sd,
            m1_mean,
            p1_seq,
            p1_dtime,
            p1_sd,
            p1_mean,
        )

    def replace_T_with_U(self, seq: str):
        """
        Replaces T bases with U since they are equivalent
        """
        return seq.replace("T", "U")

    def remove_overlapping_m1(self, seq: str):
        return seq[0]

    def remove_overlapping_p1(self, seq: str):
        return seq[4]

    def unlabelled_data(self) -> pd.DataFrame:
        """
        Returns dataframe where each row is a read
        """
        # Decompressing .json.gz into a json file
        output_file = "data.json"
        with gzip.open(self.raw_data, "rb") as gzipped_file:
            with open(output_file, "wb") as json_file:
                # Read the compressed data and write it to the output file
                json_data = gzipped_file.read()
                json_file.write(json_data)
        print("step 1 complete")

        data = []
        with open("data.json", "r") as file:
            for line in file:
                data.append(json.loads(line))
        print("step 2 complete")
        os.remove("data.json")

        (
            transcript_id,
            seq_pos,
            seq_seq,
            seq_dtime,
            seq_sd,
            seq_mean,
            m1_seq,
            m1_dtime,
            m1_sd,
            m1_mean,
            p1_seq,
            p1_dtime,
            p1_sd,
            p1_mean,
        ) = self.parse_data_pos0(data)
        print("step 3 complete")

        df = pd.DataFrame(
            {
                "transcript_id": transcript_id,
                "transcript_position": seq_pos,
                "sequence": seq_seq,
                "dwell_time": seq_dtime,
                "sd": seq_sd,
                "mean": seq_mean,
                "m1_seq": m1_seq,
                "m1_dtime": m1_dtime,
                "m1_sd": m1_sd,
                "m1_mean": m1_mean,
                "p1_seq": p1_seq,
                "p1_dtime": p1_dtime,
                "p1_sd": p1_sd,
                "p1_mean": p1_mean,
            }
        )

        df["sequence"] = df["sequence"].apply(self.replace_T_with_U)
        df["m1_seq"] = df["m1_seq"].apply(self.replace_T_with_U)
        df["p1_seq"] = df["p1_seq"].apply(self.replace_T_with_U)
        # df["m1_seq"] = df["m1_seq"].apply(self.remove_overlapping_m1)
        # df["p1_seq"] = df["p1_seq"].apply(self.remove_overlapping_p1)

        print("DATA PARSING SUCCESSFUL")
        return df

class SummariseDataByTranscript(object):
    """
    Summarise data by calculating summary statistics, grouping by sequence
    """

    def __init__(self, df: pd.DataFrame):
        self.df = df
        self.numerical = [
            "dwell_time",
            "sd",
            "mean",
            "m1_dtime",
            "m1_sd",
            "m1_mean",
            "p1_dtime",
            "p1_sd",
            "p1_mean",
        ]
        self.group = [
            "transcript_id",
            "transcript_position",
            "sequence",
            "m1_seq",
            "p1_seq",
        ]

    def summarise(self) -> pd.DataFrame:
        """_summary_

        Returns:
            pd.DataFrame: calculates mean and variance of each float feature
        """
        df_mean = self.df.groupby(self.group)[self.numerical].mean().reset_index()
        df_mean = df_mean.rename(
            columns={
                "dwell_time": "dwell_time_mean",
                "sd": "sd_mean",
                "mean": "mean_mean",
                "m1_dtime": "m1_dtime_mean",
                "m1_sd": "m1_sd_mean",
                "m1_mean": "m1_mean_mean",
                "p1_dtime": "p1_dtime_mean",
                "p1_sd": "p1_sd_mean",
                "p1_mean": "p1_mean_mean",
            }
        )
        df_var = (
            self.df.groupby(self.group)[self.numerical]
            .var()
            .reset_index()[self.numerical]
        )
        df_var = df_var.rename(
            columns={
                "dwell_time": "dwell_time_var",
                "sd": "sd_var",
                "mean": "mean_var",
                "m1_dtime": "m1_dtime_var",
                "m1_sd": "m1_sd_var",
                "m1_mean": "m1_mean_var",
                "p1_dtime": "p1_dtime_var",
                "p1_sd": "p1_sd_var",
                "p1_mean": "p1_mean_var",
            }
        )
        new_df = pd.concat([df_mean, df_var], axis=1)
        count = self.df.groupby(self.group).count().reset_index()["sd"]
        new_df["count"] = count
        print("DATA SUMMARISATION SUCCESSFUL")
        return new_df

class MergeData(object):
    """
    Merges unlabelled data with either the labels (for model training)
    or with additional features queried from outside sources
    """

    def __init__(self, parsed_data: pd.DataFrame, raw_info, data_path: pathlib.Path):
        self.parsed_data = parsed_data
        self.raw_info = raw_info
        self.data_path = data_path

    def merge_with_labels(self):
        data_info = pd.read_csv(self.raw_info, delimiter="\,")
        data_info["transcript_position"] = data_info["transcript_position"].astype(
            "str"
        )
        merged_data = pd.merge(
            self.parsed_data,
            data_info,
            on=["transcript_id", "transcript_position"],
            how="left",
        )

        print("DATA MERGING SUCCESSFUL")
        return merged_data

    def merge_with_features(self):
        """
        Merge with csv data(queried from R script) on transcript id
        """
        data_info = pd.read_csv(self.raw_info, header=[0])
        if data_info.shape[0] == 0:
            return self.parsed_data
        data_info.rename(
            columns={"ensembl_transcript_id": "transcript_id"}, inplace=True
        )
        merged_data = pd.merge(
            self.parsed_data, data_info, on=["transcript_id"], how="left"
        )
        merged_data["relative_sequence_position"] = np.round(
            (merged_data["transcript_position"].astype(float))
            / merged_data["transcript_length"],
            5,
        )
        outliers = merged_data[merged_data["relative_sequence_position"] >= 1]
        outliers.to_pickle(self.data_path / "outliers_length.pkl")
        print("DATA MERGING SUCCESSFUL")
        return merged_data

    def write_data_for_R(self, data_type: str = "labelled"):
        """
        Writes data to be used for R querying into data path, as well as store intermediate Df(with labels) for later use
        """
        if data_type == "labelled":
            df = self.merge_with_labels()
        elif data_type == "unlabelled":
            df = self.parsed_data
        else:
            print(
                f"{data_type} is not a valid argument, it has to be either labelled or unlabelled"
            )
            return
        bmart = df[["transcript_id", "transcript_position"]]
        bmart.to_csv(self.data_path / "bmart.csv")
        df.to_pickle(self.data_path / "interm.pkl")


import os
os.system("cls")

from art import logo

print(logo)

bid_person = {}

def find_highest_bidder(array_bids):
    highest_bidder = 0
    name_winner = ""
    for bidder in array_bids:
        bid_num = array_bids[bidder]
        if bid_num > highest_bidder:
            highest_bidder = bid_num
            name_winner = bidder
    print(f"The winner is {name_winner} with a bid of ${highest_bidder}")

check_bid = False
while not check_bid:
    name = input("What is your name? ")
    bid_price = float(input("What is your bid? $ "))
    bid_person[name] = bid_price
    countinue_bid = input("Are there any other bidders? Type 'yes' or 'no'. ")
    if countinue_bid == "no":
        os.system("cls")
        check_bid = True
        find_highest_bidder(bid_person)
    else:
        os.system("cls")
        check_bid = False


from aiogram import types, Dispatcher
from aiogram.dispatcher import FSMContext
from workers.tournament import TournamentWorker
from callbacks import *
from data_csv_engine import read_headers, is_registered
from handlers.register import register_state_handler
from handlers.utils import get_controls_keyboard, get_controls_answer, add_or_finish_match
from settings import bot
from states import GameStorage

async def add_game_command_handler(message: types.Message):

    async with TournamentWorker() as worker:
        if not await worker.is_registered_tournament(message.chat.id):
            return await add_or_finish_match(message)
        callback = types.CallbackQuery()
        callback.message = message
        await add_game_handler(callback)

# async def add_game_handler(callback: types.CallbackQuery):
#     try:
#         teams = await read_headers(callback.message.chat.id)
#         return await choose_first_team(callback, teams)
#     except FileNotFoundError:
#         await register_state_handler(callback)

async def add_game_handler(callback: types.CallbackQuery):
    async with TournamentWorker() as worker:
        tournament = await worker.get_last_tournament(callback.message.chat.id)
        teams = await worker.get_teams(tournament.id)
        return await choose_first_team(callback, teams)

async def choose_first_team(callback, available_teams):
    await callback.message.delete()
    keyboard_button_container = types.InlineKeyboardMarkup(resize_keyboard=True)
    for team in available_teams:
        keyboard_button_container.add(types.InlineKeyboardButton(team.name, callback_data=team_callback.new(team.id)))
    await GameStorage.first_team.set()
    return await bot.send_message(callback.message.chat.id, "Оберіть першу команду",
                                  reply_markup=keyboard_button_container)

async def choose_second_team(callback: types.CallbackQuery, state: FSMContext):
    team_id = int(team_callback.parse(callback.data)['team_id'])
    async with TournamentWorker() as worker:
        team = await worker.team_repo.get(id=team_id)
        tournament = await worker.get_last_tournament(callback.message.chat.id)
        available_teams = await worker.get_teams(tournament.id)
        available_teams.remove(team)

    async with state.proxy() as data:
        data['first_team'] = team_id
    await GameStorage.next()
    keyboard_button_container = types.InlineKeyboardMarkup(resize_keyboard=True)
    for team in available_teams:
        keyboard_button_container.add(types.InlineKeyboardButton(team.name, callback_data=team_callback.new(team.id)))
    return await callback.message.edit_text("Оберіть другу команду команду", reply_markup=keyboard_button_container)

async def show_controls_buttons(callback: types.CallbackQuery, state: FSMContext):
    team_id = int(team_callback.parse(callback.data)['team_id'])
    async with state.proxy() as data:
        data['second_team'] = team_id

    await state.update_data(first_result=0)
    await state.update_data(second_result=0)
    state_data = await state.get_data()
    await GameStorage.next()

    async with TournamentWorker() as worker:
        first_team = await worker.get_team(int(state_data["first_team"]))
        second_team = await worker.get_team(int(state_data["second_team"]))

    kb = await get_controls_keyboard(first_team, second_team)

    return await callback.message.edit_text(f"{first_team.name} vs {second_team.name}"
                                                 f"({state_data['first_result']}:{state_data['second_result']})",
                                  reply_markup=kb)

def register_add_handlers(dp: Dispatcher):
    dp.register_message_handler(add_game_command_handler, commands='add')
    dp.register_callback_query_handler(add_game_handler, lambda c: c.data == "add_game")
    dp.register_callback_query_handler(choose_second_team, lambda c: c.data.startswith(team_callback.prefix), state=GameStorage.first_team)
    dp.register_callback_query_handler(show_controls_buttons, lambda c: c.data.startswith(team_callback.prefix), state=GameStorage.second_team)
    dp.register_callback_query_handler(
        get_controls_answer,
        lambda c: c.data.startswith(plus_team_score.prefix) or c.data.startswith(minus_team_score.prefix),
        state=GameStorage.results
    )


# -*- coding: utf-8 -*-

from django.db import models

from cms.models import CMSPlugin

class GistPluginModel(CMSPlugin):

	gist_user = models.CharField('GitHub User',
		blank=False,
		default='',
		help_text=u'Please supply the username of the GitHub user',
		max_length=32,
	)

	gist_id = models.CharField('Gist ID',
		blank=False,
		default='',
		help_text=u'Please supply the ID of the gist',
		max_length=32,
	)

	filename = models.CharField('GitHub User',
		blank=True,
		default='',
		help_text=u'Optional. Supply a filename',
		max_length=64,
	)

	def __unicode__(self):
		return u'%s:%s:%s' % (self.gist_user, self.gist_id, self.filename, )


import torch
import torch.nn as nn

class ConvBlock(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, activation=None, batchnorm=False, groups=1):
        super().__init__()

        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, groups=groups)
        self.batchnorm = nn.BatchNorm2d(out_channels) if batchnorm else None
        self.activation = activation if activation else None

    def forward(self, x):
        x = self.conv(x)

        if self.batchnorm:
            x = self.batchnorm(x)

        if self.activation:
            x = self.activation(x)

        return x

class ResidualBlock(nn.Module):
    def __init__(self, in_channels, out_channels):
        super().__init__()

        self.conv1 = ConvBlock(in_channels, out_channels, activation=nn.ReLU(True))
        self.conv2 = ConvBlock(out_channels, out_channels)
        self.relu = nn.ReLU(True)

    def forward(self, x):
        _x = x
        x = self.conv1(x)
        x = self.conv2(x)
        x = x + _x
        x = self.relu(x)

        return x

class CascadingBlock(nn.Module):
    def __init__(self, in_channels, out_channels):
        super().__init__()

        self.res_block1 = ResidualBlock(in_channels, out_channels)
        self.conv1 = nn.Conv2d(out_channels*2, out_channels, kernel_size=1, stride=1)

        self.res_block2 = ResidualBlock(out_channels, out_channels)
        self.conv2 = nn.Conv2d(out_channels*3, out_channels, kernel_size=1, stride=1)

        self.res_block3 = ResidualBlock(out_channels, out_channels)
        self.conv3 = nn.Conv2d(out_channels*4, out_channels, kernel_size=1, stride=1)

    def forward(self, x):
        inp = x

        r1 = self.res_block1(x)
        o1 = self.conv1(torch.cat([inp, r1], 1))

        r2 = self.res_block2(o1)
        o2 = self.conv2(torch.cat([inp, r1, r2], 1))

        r3 = self.res_block3(o2)
        o3 = self.conv3(torch.cat([inp, r1, r2, r3], 1))

        return o3

class EResidualBlock(nn.Module):
    def __init__(self, in_channels, out_channels, groups):
        super().__init__()

        self.conv1 = ConvBlock(in_channels, out_channels, activation=nn.ReLU(True), groups=groups)
        self.conv2 = ConvBlock(out_channels, out_channels, activation=nn.ReLU(True), groups=groups)
        self.conv3 = ConvBlock(out_channels, out_channels, kernel_size=1, stride=1, padding=0)
        self.relu = nn.ReLU(True)

    def forward(self, x):
        _x = x
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)
        x = x + _x
        x = self.relu(x)

        return x

class ECascadingBlock(nn.Module):
    def __init__(self, in_channels, out_channels, groups):
        super().__init__()

        self.res_block = EResidualBlock(in_channels, out_channels, groups)
        self.conv1 = nn.Conv2d(out_channels*2, out_channels, kernel_size=1, stride=1)
        self.conv2 = nn.Conv2d(out_channels*3, out_channels, kernel_size=1, stride=1)
        self.conv3 = nn.Conv2d(out_channels*4, out_channels, kernel_size=1, stride=1)

    def forward(self, x):
        inp = x

        r1 = self.res_block(x)
        o1 = self.conv1(torch.cat([inp, r1], 1))

        r2 = self.res_block(o1)
        o2 = self.conv2(torch.cat([inp, r1, r2], 1))

        r3 = self.res_block(o2)
        o3 = self.conv3(torch.cat([inp, r1, r2, r3], 1))

        return o3

class UpsampleBlock(nn.Module):
    def __init__(self, channels, scale, groups):
        super().__init__()
        self.scale = scale

        self.scale2 = nn.Sequential(
            ConvBlock(channels, 4*channels, activation=nn.ReLU(True), groups=groups),
            nn.PixelShuffle(2)
        )

        self.scale3 = nn.Sequential(
            ConvBlock(channels, 9*channels, activation=nn.ReLU(True), groups=groups),
            nn.PixelShuffle(3)
        )

        self.scale4 = nn.Sequential(
            self.scale2,
            self.scale2
        )

    def forward(self, x):
        if self.scale == 2:
            return self.scale2(x)
        elif self.scale == 3:
            return self.scale3(x)
        elif self.scale == 4:
            return self.scale4(x)
        else:
            raise NotImplementedError


import angr
import claripy

flag_length = 32

base_address = 0x00100000
success_addr = 0x0010169b
failure_addr = 0x001016ca

project = angr.Project("./angr_man", main_opts = {"base_addr" : base_address})
#create vector
flag_chars = [claripy.BVS(f"flag_char{i}", 8) for i in range(flag_length)]
flag = claripy.Concat(*flag_chars +  [claripy.BVV(b'\n')])

state = project.factory.full_init_state(
		args=["./angr_man"],
		add_options = angr.options.unicorn,
		stdin=flag
	)

simgr = project.factory.simulation_manager(state)
simgr.explore(find=success_addr, avoid=failure_addr)

if len(simgr.found) > 0:
	for found in simgr.found:
		print(found.posix.dumps(0))


# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Hell is other peoples' code

import bpy
from bpy.types import (
    Panel,
    Curve,
    SurfaceCurve
)

from . import utils

class CUSTOM_UL_addon_list(bpy.types.UIList):
    EMPTY = 1 << 0

    use_filter_empty: bpy.props.BoolProperty(
        name="Filter Unused",
        default=True,
        options=set(),
        description="Whether to filter cameras with zero users",
    )
    use_filter_empty_reverse: bpy.props.BoolProperty(
        name="Reverse Empty",
        default=False,
        options=set(),
        description="Reverse empty filtering",
    )
    use_filter_name_reverse: bpy.props.BoolProperty(
        name="Reverse Name",
        default=False,
        options=set(),
        description="Reverse name filtering",
    )

    def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
        active = True if index == getattr(active_data, active_propname) else False
        root = layout.row(align=True)
        root.label(text='', icon='FILE_SCRIPT')

        root.prop(item, "name", text='')
        root.prop(item, 'filter_mode', icon_only=True, expand=True)

    def draw_filter(self, context, layout):
        # Nothing much to say here, it's usual UI code...
        row = layout.row()
        row.alignment = 'RIGHT'
        # row.label(text="Filters, eventually")

    def filter_items(self, context, data, propname):

        items = getattr(data, propname)
        flt_flags = []
        flt_neworder = []

        helper_funcs = bpy.types.UI_UL_list

        if self.filter_name:
            flt_flags = helper_funcs.filter_items_by_name(self.filter_name, self.bitflag_filter_item, items, "name",
                                                          reverse=self.use_filter_name_reverse)
        if not flt_flags:
            flt_flags = [self.bitflag_filter_item] * len(items)

        # Filter by emptiness.
        # if self.use_filter_empty:
        #     for i, cam in enumerate(items):
        #         if cam.users < 1:
        #             flt_flags[i] |= ~self.EMPTY

        if not flt_flags:
            flt_flags = [self.bitflag_filter_item] * len(items)

        return flt_flags, flt_neworder

class CUSTOM_UL_workspace_list(bpy.types.UIList):
    EMPTY = 1 << 0

    use_filter_empty: bpy.props.BoolProperty(
        name="Filter Unused",
        default=True,
        options=set(),
        description="Whether to filter cameras with zero users",
    )
    use_filter_empty_reverse: bpy.props.BoolProperty(
        name="Reverse Empty",
        default=False,
        options=set(),
        description="Reverse empty filtering",
    )
    use_filter_name_reverse: bpy.props.BoolProperty(
        name="Reverse Name",
        default=False,
        options=set(),
        description="Reverse name filtering",
    )

    def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):

        active = True if index == getattr(active_data, active_propname) else False

        indexed = True if item.name in getattr(active_data, "workspaces") else False

        root = layout.column(align=True)

        header = root.row(align=True)
        header.use_property_split = False
        header.label(text=item.name, icon='WORKSPACE')
        if not indexed:
            op = header.operator(
                "bam.create_filter",
                text="Create Filter",
            )
            op.workspace = item.name

    def draw_filter(self, context, layout):
        # Nothing much to say here, it's usual UI code...
        row = layout.row()
        row.alignment = 'RIGHT'
        # row.label(text="Filters, eventually")

    def filter_items(self, context, data, propname):

        workspaces = getattr(data, propname)
        flt_flags = []
        flt_neworder = []

        helper_funcs = bpy.types.UI_UL_list

        if self.filter_name:
            flt_flags = helper_funcs.filter_items_by_name(self.filter_name, self.bitflag_filter_item, workspaces, "name",
                                                          reverse=self.use_filter_name_reverse)
        if not flt_flags:
            flt_flags = [self.bitflag_filter_item] * len(workspaces)

        # Filter by emptiness.
        # if self.use_filter_empty:
        #     for i, cam in enumerate(workspaces):
        #         if cam.users < 1:
        #             flt_flags[i] |= ~self.EMPTY

        if not flt_flags:
            flt_flags = [self.bitflag_filter_item] * len(workspaces)

        return flt_flags, flt_neworder

class BAM_PT_workspace_manager(Panel):
    bl_idname = "BAM_PT_workspace_manager"
    bl_space_type = 'TEXT_EDITOR'
    bl_region_type = 'UI'
    bl_category = "BAM"
    bl_label = "BAM Workspace Manager"
    bl_options = {'DRAW_BOX', 'HEADER_LAYOUT_EXPAND',  }
    bl_owner_use_filter = False

    def draw(self, context):
        wm = context.window_manager

        layout = self.layout
        root = layout.column(align=True)

        addons = bpy.context.preferences.addons
        prefs = addons["BlenderAddonManager"].preferences

        # root.label(text=str(prefs.wslist_index))

        # prefs = utils.get_prefs(context)

        root.template_list("CUSTOM_UL_workspace_list",      # the class that defines the filter functions and drawing
                           "",                              # No Clue
                           context.blend_data,              # Where to get the data
                           "workspaces",                    # The data to get
                           prefs,                           # Where the index is
                           "wslist_index",                  # Index name
                           rows=1,
                           maxrows=4
                           )

        active_workspace = bpy.context.blend_data.workspaces[prefs.wslist_index]

        workspace_data = None
        for space in prefs.workspaces:
            if space.name == active_workspace.name:
                workspace_data = space
                break

        row = root.row(align=True)
        row.scale_y = 2
        row.label(text=active_workspace.name)

        # root.prop(
        #     active_workspace,
        #     "use_filter_by_owner"
        # )
        root.prop(
            active_workspace,
            "object_mode"
        )

        if workspace_data:
            root.prop(
                workspace_data,
                "fallback_mode"
            )

            row = root.row(align=True)
            row.template_list("CUSTOM_UL_addon_list",
                            "",
                            workspace_data,
                            "addons",
                            workspace_data,
                            "addon_index",
                            rows=1,
                            maxrows=4
                            )

            col = row.column(align=True)
            op = col.operator(
                "bam.modify_filter",
                text="",
                icon='ADD',
            )
            op.operation = 'ADD'
            op.workspace = workspace_data.name
            op.addon = 'Configure Me'

            active_addon = None
            if len(workspace_data.addons) > 0:
                active_addon = workspace_data.addons[workspace_data.addon_index]

                op = col.operator(
                    "bam.modify_filter",
                    text="",
                    icon='REMOVE',
                )
                op.operation = 'REMOVE'
                op.workspace = workspace_data.name
                op.addon = active_addon.name

        # body = root.split(factor=0.1, align=True)

        # body_left = body.row()

        # body_right = body.row()

        # body_right.label(text=str(active))
        # body_right.prop(
        #     item,
        #     "use_filter_by_owner",
        #     # text="",
        # )
        # body_right.prop(
        #     item,
        #     "object_mode",
        #     text="",
        # )


#!/usr/bin/env python3

from datetime import datetime
from urllib.request import urlopen
from aws_pager import aws_pager
import boto3
import json
import os
import sys
if sys.version_info >= (3, 11): from datetime import UTC
else: import datetime as datetime_fix; UTC=datetime_fix.timezone.utc

def msg(value, temp=False):
    # Helper to show a message, including temporary status messages
    if 'last' not in msg.__dict__:
        msg.last = ""
    if len(msg.last) > 0:
        if len(msg.last) > len(value):
            extra = len(msg.last) - len(value)
            to_show = "\b" * extra + " " * extra + "\r" + value
        else:
            to_show = "\r" + value
    else:
        to_show = value
    if temp:
        print(to_show, end="", flush=True)
        msg.last = value
    else:
        print(to_show, flush=True)
        msg.last = ""

def cache_json(desc, final, save_data_filename):
    if save_data_filename is None:
        return

    def serialize_for_compare(data):
        # Force a deep copy by serialize/unserializing
        data = json.loads(json.dumps(data))
        # Ignore the _meta key when comparing
        data["_meta"] = "-iognore-"
        # Serialize to a set format
        data = json.dumps(data, sort_keys=True)
        return data

    updated_data = serialize_for_compare(final)

    # Load the old version if it's available
    old_data = ""
    if os.path.isfile(save_data_filename):
        with open(save_data_filename, "rt", encoding="utf-8") as f:
            old_data = json.load(f)
            old_data = serialize_for_compare(old_data)

    if old_data == updated_data:
        msg("The " + desc + " in " + save_data_filename + " has not changed.")
    else:
        # Dump out the data
        with open(save_data_filename, "wt", newline="\n", encoding="utf-8") as f:
            json.dump(final, f, indent=4, sort_keys=True)
            f.write("\n")
        msg("All done, created " + save_data_filename + " for " + desc)

def get_regions(save_data_filename=None):
    ssm = boto3.client('ssm', region_name="us-east-1")

    # The final data to save
    final = {"_meta": {
        "_comment": "This file is generated by " + os.path.split(__file__)[-1] + ", run that script to update it automatically.",
        "data downloaded": datetime.now(UTC).replace(tzinfo=None).strftime("%Y-%m-%dT%H:%M:%SZ",
    )}}

    final["regions"] = {}

    msg("Loading regions from boto3...", temp=True)

    for i, (_, parameter) in enumerate(aws_pager(ssm, 'get_parameters_by_path', 'Parameters', Path="/aws/service/global-infrastructure/regions")):
        region = parameter['Name'].split("/")[-1]
        msg(f"Loading region #{i+1},'{region}' from boto3...", temp=True)
        details = ssm.get_parameter(Name=f"/aws/service/global-infrastructure/regions/{region}/longName")
        desc = details["Parameter"]["Value"]
        final["regions"][region] = desc

    cache_json("region data", final, save_data_filename)

    return final

def get_pricing(save_data_filename=None):
    # These JSON objects are used by the AWS S3 Pricing Page at
    # https://aws.amazon.com/s3/pricing/
    urls = {
        "costs": "https://b0.p.awsstatic.com/pricing/2.0/meteredUnitMaps/s3/USD/current/s3.json",
        "deep": "https://b0.p.awsstatic.com/pricing/2.0/meteredUnitMaps/s3glacierdeeparchive/USD/current/s3glacierdeeparchive.json",
    }

    # The final data to save
    final = {"_meta": {
        "_comment": "This file is generated by " + os.path.split(__file__)[-1] + ", run that script to update it automatically.",
        "data downloaded": datetime.now(UTC).replace(tzinfo=None).strftime("%Y-%m-%dT%H:%M:%SZ",
    )}}

    # Load the data for each URL in turn
    for key in urls:
        msg(f"Getting {key} pricing data...", temp=True)
        urls[key] = json.load(urlopen(urls[key]))
        final["_meta"][key + " updated"] = urls[key]["manifest"]["hawkFilePublicationDate"]

    # Get all of the regions, since these JSON files from AWS drive user-visible
    # data, all of the regions are the full English description of the region.
    regions = [x for x in urls['costs']['regions'] if len(x)]

    # Load the region names, and handle the edge cases with slightly different names
    with open("s3_regions.json") as f:
        all_regions = json.load(f)
        all_regions = {y: x for x, y in all_regions["regions"].items()}
        # This is both 'AWS GovCloud (US-East)' and 'AWS GovCloud (US)'
        all_regions["AWS GovCloud (US)"] = "us-gov-west-1"
        # The "Europe" regions are also "EU" regions
        for region_name in [x for x in all_regions if x.startswith("Europe")]:
            all_regions[region_name.replace("Europe ", "EU ")] = all_regions[region_name]

    # Raise an exception if there's a not yet known region
    for region_name in regions:
        if region_name not in all_regions:
            raise Exception(region_name + " is not a known region!")

    for region in regions:
        # Pull out the data for this region
        temp = {}
        final[all_regions[region]] = temp

        with open("s3_cost_classes.json") as f:
            s3_cost_classes = json.load(f)

        # Pull out the costs from each field in turn
        for s3_cost in s3_cost_classes['classes']:
            if region in urls[s3_cost['page_source']]['regions']:
                temp[s3_cost['desc']] = urls[s3_cost['page_source']]['regions'][region][s3_cost['page_desc']]['price']
                temp[s3_cost['desc']] = temp[s3_cost['desc']].rstrip('0')
            else:
                print(f"\nWARNING: {s3_cost} not found in {region}", flush=True)
                temp[s3_cost['desc']] = "0.00"

    cache_json("pricing data", final, save_data_filename)

    return final

if __name__ == "__main__":
    get_regions(save_data_filename="s3_regions.json")
    get_pricing(save_data_filename="s3_pricing_data.json")


import torch
import torch.nn as nn
import torch.nn.functional as F

#decay .95, .25, 1e-5
class VectorQuantizerEMA(nn.Module):
    def __init__(self, embedding_dim, num_embeddings, groups=1, commitment_cost=0.25, decay=0.95, epsilon=1e-5):
        super(VectorQuantizerEMA, self).__init__()

        print('num codes', num_embeddings)

        self._embedding_dim = embedding_dim
        self._num_embeddings = num_embeddings

        self._embedding = nn.Embedding(self._num_embeddings, self._embedding_dim)
        self._embedding.weight.data.normal_()
        self._commitment_cost = commitment_cost

        self.register_buffer('_ema_cluster_size', torch.zeros(num_embeddings))
        self._ema_w = nn.Parameter(torch.Tensor(num_embeddings, self._embedding_dim))
        self._ema_w.data.normal_()

        self._decay = decay
        self._epsilon = epsilon

    def forward(self, inputs, reinit_code=None):
        # BCHW -> BHWC
        #inputs = inputs.permute(0, 2, 3, 1).contiguous()
        input_shape = inputs.shape

        # Flatten
        flat_input = inputs.view(-1, self._embedding_dim)

        # May be we can even use cosine distance.
        distances = (torch.sum(flat_input**2, dim=1, keepdim=True)
                    + torch.sum(self._embedding.weight**2, dim=1)
                    - 2 * torch.matmul(flat_input, self._embedding.weight.t()))

        #print('distances before', distances[0])

        #distances *= self._ema_cluster_size.unsqueeze(0).repeat(distances.shape[0], 1)

        #distances *= torch.sqrt(self._ema_cluster_size.unsqueeze(0).repeat(distances.shape[0], 1)) #was using

        distances *= torch.sqrt(torch.sqrt(torch.sqrt(self._ema_cluster_size.unsqueeze(0).repeat(distances.shape[0], 1))))

        #distances *= torch.exp((self._ema_cluster_size.unsqueeze(0).repeat(distances.shape[0], 1) / 20.0))

        #print('distances after', distances[0])

        # Encoding
        encoding_indices = torch.argmin(distances, dim=1).unsqueeze(1)
        encodings = torch.zeros(encoding_indices.shape[0], self._num_embeddings, device=inputs.device)
        encodings.scatter_(1, encoding_indices, 1)

        # Quantize and unflatten
        quantized = torch.matmul(encodings, self._embedding.weight).view(input_shape)

        #  EMA to update the embedding vectors
        if self.training:
            self._ema_cluster_size = self._ema_cluster_size * self._decay + \
                                     (1 - self._decay) * torch.sum(encodings, 0)

            #print('distances shape', distances.shape)
            #print('encodings shape', encodings.shape)
            #print('encodings', encodings.min(), encodings.max(), encodings.mean())
            #print('encodings sum', torch.sum(encodings, 0).shape)

            # Laplace smoothing of the cluster size
            n = torch.sum(self._ema_cluster_size.data)
            self._ema_cluster_size = (
                (self._ema_cluster_size + self._epsilon)
                / (n + self._num_embeddings * self._epsilon) * n)

            dw = torch.matmul(encodings.t(), flat_input)
            self._ema_w = nn.Parameter(self._ema_w * self._decay + (1 - self._decay) * dw)

            self._embedding.weight = nn.Parameter(self._ema_w / self._ema_cluster_size.unsqueeze(1))

            #print('clusters shape', self._ema_cluster_size.shape)
            #print('clusters', self._ema_cluster_size.data.cpu().numpy().tolist())

            #raise Exception()

        # Loss
        e_latent_loss = F.mse_loss(quantized.detach(), inputs)
        loss = self._commitment_cost * e_latent_loss

        # Straight Through Estimator
        quantized = inputs + (quantized - inputs).detach()
        avg_probs = torch.mean(encodings, dim=0)
        perplexity = torch.exp(-torch.sum(avg_probs * torch.log(avg_probs + 1e-10)))

        # BHWC -> BCHW
        return quantized.contiguous(), loss, encoding_indices


import sys
from scipy.spatial import distance

def lt(i, j): return i < j

def gt(i, j): return i > j

class O():
    """
    Default class which everything extends.
    """
    def __init__(self, **d):
        self.has().update(**d)

    def has(self):
        return self.__dict__

    def update(self, **d):
        self.has().update(d)
        return self

    def __repr__(self):
        show = [':%s %s' % (k,self.has()[k])
            for k in sorted(self.has().keys())
            if k[0] is not "_"]
        txt = ' '.join(show)
        if len(txt) > 60:
            show=map(lambda x: '\t'+x+'\n',show)
        return '{'+' '.join(show)+'}'

    def __getitem__(self, item):
        return self.has().get(item)

    def __setitem__(self, key, value):
        self.has()[key] = value

def norm(x, low, high):
    """
    Method to normalize value
    between 0 and 1
    """
    nor = (x - low)/(high - low)
    if nor > 1:
        return 1
    elif nor < 0:
        return 0
    return nor

"""
Returns true if both are similar. i.e. terminate, no improvement in mean objective scores
"""
def earlyTermination(prev_era, cur_era, model):
    prev_obj_score = [model.eval(can) for can in prev_era]
    cur_obj_score = [model.eval(can) for can in cur_era]

    eps = 0.01

    for i in range(model.m):
        prev_sum_obj_i = sum(o[i] for o in prev_obj_score)
        cur_sum_obj_i = sum(o[i] for o in cur_obj_score)
        if model.objs[i].better(cur_sum_obj_i, prev_sum_obj_i): return False
    return True

"""
Checks convergence of a population with baseline population
Args:
  base_pop = baseline population
  final_pop = final population
  model = model under optimization
returns:
  Average distance of nearest points in base_pop to final_pop.
"""
def divergence_from_baseline(base_pop, final_pop, model):
    print(len(base_pop), len(final_pop))
    assert len(base_pop) == len(final_pop)
    dist_from_hell = 0
    for can1 in final_pop:
        d = sys.maxint
        for can2 in base_pop:
            d1 = distance.euclidean(model.eval(can1), model.eval(can2))
            if d1 <= d:
                d = d1;
                nearest_neighbour = can2
        dist_from_hell += distance.euclidean(model.eval(can1), model.eval(nearest_neighbour))
    return dist_from_hell/len(base_pop)

def log_base_and_final(baseline_population, population, model_instance, fp):
    fp.write("\n Baseline Population: \n")
    for can in baseline_population:
        fp.write(', '.join( "{0:.5f}".format(x) for x in model_instance.eval(can)))
        fp.write('\n')

    fp.write("\n Final Population: \n")

    for can in population:
        fp.write(', '.join( "{0:.5f}".format(x) for x in model_instance.eval(can)))
        fp.write('\n')


import pygame
from pygame import image, transform

from game.animals import Animal

class Cat(Animal):
    """
    Cat that will chase mice
    """
    def __init__(self, environment, _speed):
        """
        Creates the cat with the proper image
        """
        super().__init__(environment, _speed)

        self._wall_modifier = 0
        image_name = "media/Cat.png"
        self._root_image = transform.rotate(image.load(image_name), 180)
        self.image = self._root_image

    def process_movement(self, input_event):
        """
        Process user input to find out how to move
        :param input_event event Keyboard input provided by the user
        """
        if input_event.type == pygame.KEYUP:
            self._handle_key_up(input_event)
        elif input_event.type == pygame.KEYDOWN:
            self._handle_key_down(input_event)

    def _handle_key_down(self, input_event):
        """
        Handle what happense when the user presses down a key
        :param input_event Key press
        """
        if input_event.key == pygame.K_UP:
            self._y_move = -self._speed
        elif input_event.key == pygame.K_DOWN:
            self._y_move = self._speed
        elif input_event.key == pygame.K_LEFT:
            self._x_move = -self._speed
        elif input_event.key == pygame.K_RIGHT:
            self._x_move = self._speed
        self._adjust_bearing()

    def _handle_key_up(self, input_event):
        """
        Handle what happense when the user releases a key
        :param input_event Key press
        """
        if input_event.key == pygame.K_UP or \
                input_event.key == pygame.K_DOWN:
            self._y_move = 0
        elif input_event.key == pygame.K_LEFT or \
                input_event.key == pygame.K_RIGHT:
            self._x_move = 0
        self._adjust_bearing()


# created by Sean Sungil Kim                  < https://github.com/kimx3314 >
# used for Stanford Cars vehicle detection pre-processing (cropping)
#          utilizing the bounding boxes provided by Stanford

# training set only
# change the directory according to your device before using
# meant to be used on the Stanford Cars Dataset by classes folder
# which can be downloaded here: https://www.kaggle.com/jutrera/stanford-car-dataset-by-classes-folder
# post pre-processed dataset can be found here: https://www.kaggle.com/sungtheillest/vehicledetected-stanford-cars-data-classes-folder

import os
import numpy as np
import pandas as pd
import glob
import cv2

train_dir = os.listdir('C:/Users/sungi/Desktop/stanford-car-dataset-by-classes-folder/car_data/car_data/train/')
train_csv = pd.read_csv('C:/Users/sungi/Desktop/stanford-car-dataset-by-classes-folder/anno_train.csv', header = None)
data_labels = np.array(pd.read_csv('C:/Users/sungi/Desktop/stanford-car-dataset-by-classes-folder/names.csv',\
                                   header = None))

idx = np.unique(np.array([label[0][0:-5] for label in data_labels]), return_index = True)[1]
labels_wo_year = np.array([np.array([label[0][0:-5] for label in data_labels])[idx] for idx in sorted(idx)])

pd.DataFrame(labels_wo_year).to_csv('C:/Users/sungi/Desktop/stanford-car-dataset-by-classes-folder/wo_year_names.csv',\
                                    header = False, index = False)

os.chdir('C:/Users/sungi/Desktop/stanford-car-dataset-by-classes-folder/car_data/car_data/train/')
for folder in train_dir:
    folder_read = folder + '/*.jpg'

    # reading from each class directory folder
    filelist = glob.glob(folder_read)
    all_files = [fname [-9:] for fname in filelist]
    sc_data = np.array([cv2.imread(fname) for fname in filelist])
    for i in range(len(sc_data)):
        file_dir = folder + '/' + all_files[i]
        annot = train_csv.loc[train_csv[0] == all_files[i]]
        x1 = annot[1][annot.index[0]]
        y1 = annot[2][annot.index[0]]
        x2 = annot[3][annot.index[0]]
        y2 = annot[4][annot.index[0]]
        cv2.imwrite(file_dir, sc_data[i][y1:y2, x1:x2])


import datetime
from distutils.log import Log

from deepblu_tools.models import deepblu as dm
from deepblu_tools.models import uddf as um

# Logbook (contains multiple logs, media, divespots,
# gas definitions, buddies, equipment)
class DeepbluLogBook:
    def __init__(self, posts: list, deepblu_user: "dm.DeepbluUser", max_posts: int):
        self.logs = []

        for post in posts:
            if (
                max_posts is not None and len(self.logs) >= max_posts
            ):  # max posts reached; stop appending
                break

            self.logs.append(dm.DeepbluLog(post.get("diveLog"), post.get("medias")))

        self.get_unique_media()
        self.get_unique_dive_bases()
        self.get_unique_dive_spots()
        self.get_unique_gas_definitions()
        self.get_unique_buddies()
        self.get_unique_equipment()
        self.owner = deepblu_user.to_person_type()
        if self.owner:
            self.owner.equipment = um.EquipmentType(
                divecomputer=[e.to_uddf() for e in self.equipment]
            )

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

    # Below functions eliminate duplicates from the summaries
    def get_unique_equipment(self):
        self.equipment = []
        for log in self.logs:
            for item in log.dive_gear.equipment:
                if not self.find_attr_by_id("equipment", item.id):
                    self.equipment.append(item)

    def get_unique_buddies(self):
        self.buddies = []
        for log in self.logs:
            for buddy in log.buddies:
                if not self.find_attr_by_id("buddies", buddy.id):
                    self.buddies.append(buddy)

    def get_unique_dive_spots(self):
        self.dive_spots = []
        for log in self.logs:
            if not self.find_attr_by_id("dive_spots", log.dive_spot.id):
                self.dive_spots.append(log.dive_spot)

    def get_unique_dive_bases(self):
        self.dive_bases = []
        for log in self.logs:
            if not self.find_attr_by_id("dive_bases", log.dive_base.id):
                self.dive_bases.append(log.dive_base)

    def get_unique_gas_definitions(self):
        self.gas_definitions = []
        for log in self.logs:
            if hasattr(log.dive_gear, "gas_definition"):
                if hasattr(log.dive_gear.gas_definition, "id"):
                    if not self.find_attr_by_id(
                        "gas_definitions", log.dive_gear.gas_definition.id
                    ):
                        self.gas_definitions.append(log.dive_gear.gas_definition)

    def get_unique_media(self):
        self.media = []
        for log in self.logs:
            for medium in log.media:
                if not self.find_attr_by_id("media", medium.id):
                    self.media.append(medium)

    def find_attr_by_id(self, attribute: str, id: str) -> list:
        try:
            return [item for item in getattr(self, attribute) if item.id == id][0]
        except Exception:
            return []

    def to_uddf(self) -> um.Uddf:
        uddf = um.Uddf(version="3.2.2")
        uddf.generator = um.Generator(
            name="Deepblu Backup Tool",
            version="2.0.0",
            type=um.GeneratorType.CONVERTER,
            manufacturer=um.ManufacturerType(
                id="bluppfisk",
                name="Sander Van de Moortel",
                contact=um.ContactType(
                    email="sander.vandemoortel@gmail.com",
                    homepage=["https://github.com/bluppfisk/deepblu-tools"],
                ),
            ),
            datetime=datetime.datetime.now().isoformat(),
        )
        uddf.divesite = um.Divesite(
            site=[s.to_uddf() for s in self.dive_spots],
            divebase=[b.to_uddf() for b in self.dive_bases],
        )
        uddf.profiledata = um.Profiledata(
            repetitiongroup=um.RepetitiongroupType(
                id="rep", dive=[d.to_uddf() for d in self.logs]
            )
        )
        uddf.gasdefinitions = um.Gasdefinitions(
            mix=[g.to_uddf() for g in self.gas_definitions]
        )
        uddf.diver = um.Diver(
            owner=self.owner, buddy=[b.to_uddf() for b in self.buddies]
        )
        uddf.mediadata = um.Mediadata([m.to_uddf() for m in self.media])

        return uddf


"""
File:           plot_utils.py
Author:         Pavlina Koutecka
Date:           09/04/2020
Description:    This file some basic utilities to improve the plotting process.
                Currently, these utilities are implemented:

                > plot confusion matrix - make a nice plot of a confusion matrix
                > plot smoothed curve - smoothen stored accuracy and loss curves.
"""

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

import sys
import pathlib
parent_path = str(pathlib.Path().absolute().parent)
sys.path.append(parent_path)
import configuration as cfg
from laplotter import LossAccPlotter

def plot_confusion_matrix(cm, target_names, path, title='Confusion matrix', cmap=None, normalize=True):
    """
    Given a sklearn confusion matrix (cm), make a nice plot

    :param cm: confusion matrix from sklearn.metrics.confusion_matrix
    :param target_names: given classification classes such as [0, 1, 2]
                         the class names, for example: ['high', 'medium', 'low']
    :param title: the text to display at the top of the matrix
    :param cmap: the gradient of the values displayed from matplotlib.pyplot.cm
                 see http://matplotlib.org/examples/color/colormaps_reference.html
                 plt.get_cmap('jet') or plt.cm.Blues
    :param normalize:  If False, plot the raw numbers
                       If True, plot the proportions
    :return: confusion matrix as an object to plot

    References:
        http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
        https://www.kaggle.com/grfiv4/plot-a-confusion-matrix
    """

    if cmap is None:
        cmap = plt.get_cmap('Blues')

    plt.figure(figsize=(6, 4))
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()

    if target_names is not None:
        tick_marks = np.arange(len(target_names))
        plt.xticks(tick_marks, target_names)
        plt.yticks(tick_marks, target_names, rotation='vertical')

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    thresh = cm.max() / 1.5 if normalize else cm.max() / 2
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        if normalize:
            plt.text(j, i, "{:0.4f}".format(cm[i, j]),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")
        else:
            plt.text(j, i, "{:,}".format(cm[i, j]),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")

    plt.ylim([3.5, -0.5])  # [1.5, -0.5]
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.savefig(path)
    plt.show()

def plot_smoothed_curve(path, train_acc_csv, val_acc_csv, train_loss_csv, val_loss_csv):
    """
    Use the laplotter.py to smoothen some stored runs of accuracy and loss curves.

    :param path: path to where the result will be stored
    :param train_acc_csv: training accuracy curve to be smoothen
    :param val_acc_csv: validation accuracy curve to be smoothen
    :param train_loss_csv: training loss curve to be smoothen
    :param val_loss_csv: validation loss curve to be smoothen
    :return:
    """

    plotter = LossAccPlotter(title=f"Performance of the {cfg.hyperparameter.model_title}",
                             save_to_filepath=path,
                             show_regressions=False,
                             show_averages=True,
                             show_loss_plot=True,
                             show_acc_plot=True,
                             show_plot_window=True,
                             x_label="Epoch")

    train_acc = np.genfromtxt(train_acc_csv, delimiter=',')
    val_acc = np.genfromtxt(val_acc_csv, delimiter=',')
    train_loss = np.genfromtxt(train_loss_csv, delimiter=',')
    val_loss = np.genfromtxt(val_loss_csv, delimiter=',')

    for index, _ in enumerate(train_acc):
        plotter.add_values(train_acc[index, 0], acc_train=train_acc[index, 1], loss_train=train_loss[index, 1], redraw=False)
    for index, _ in enumerate(val_acc):
        plotter.add_values(val_acc[index, 0], acc_val=val_acc[index, 1], loss_val=val_loss[index, 1], redraw=False)

    # redraw once at the end
    plotter.redraw()
    plotter.block()


from programs.FPS_BPM_Calc import fpsbpmlooper
from programs.ffmpeg_local import compile_video
from termcolor import colored
import subprocess
import os
import termcolor

COLORS = termcolor

def main():
    while True:
        print("")
        for i in range(2):
            print("////////////////")
        print(colored("   MEOW v0.1   ", 'red', ))
        for i in range(2):
            print("////////////////")

        print(colored("\n Available Programs:\n",
              'red', attrs=['reverse',]))
        print(colored("1.", 'cyan') + " MEOW FPS / BPM ")
        print(colored("2.", 'cyan') + " MEOW SAMPLER ")
        print(colored("3.", 'cyan') + " PNG to MP4 ")
        print(colored("4.", 'cyan') + " PNG to GIF ")

        # Get the user's choice
        choice = input(colored("\nEnter number to RUN: ", 'red'))

        try:
            # Run the selected script
            if choice == "1":
                input_fps = int(
                    input(colored("Enter the FPS value: ", 'red')))
                input_bpm = int(
                    input(colored("Enter the BPM value: ", 'red')))
                fpsbpmlooper(fps=input_fps, bpm=input_bpm)
            elif choice == "2":
                subprocess.run(['python', './programs/yt_to_mp3.py'])
            elif choice == "3":
                input_path = input((colored("Input Path: ", 'red')))
                input_path = os.path.dirname(input_path)
                compile_video(directory=input_path)
            elif choice == "4":
                subprocess.run(['python', './programs/png_to_gif.py'])
            else:
                print("Invalid choice.")
                continue
        except Exception as e:
            # if an error occurs, print the error message and start over
            print(e)
            continue

if __name__ == "__main__":
    main()


# pylint: disable=C0413
import sys
import os
import tkinter as tk
from PIL import Image, ImageTk
import numpy as np

sys.path.insert(0, os.getcwd())
from geo_mapping.geo_mapper import MapFitter, MapCoordinateTransformer

class TkGeoMapper:
    '''
    Given a latitude and longitude series describing a track.
    Fetches a map that fits the data. Adds the map to a canvas. Draws
    the track on the canvas overlaying the map.

    This was used for MapFitter images and other geo mapper classes.

    Look at GeoMapScrubber for a more useful widget that allows scrubbing
    forward and backward on a track in a map.
    '''
    def __init__(self,
                 latitude_series: np.ndarray,
                 longitude_series: np.ndarray,
                 marker_positions=None,
                 track_color='red'
                ):

        self._latitude_series = latitude_series
        self._longitude_series = longitude_series
        self._marker_positions = marker_positions
        self._track_color = track_color

        if self._marker_positions is None:
            self._add_markers = False
        else:
            self._add_markers = True

        assert self._latitude_series.size == self._longitude_series.size, \
               (f'latitude_series ({self._latitude_series.size}) '
                f'must be same size as longitude_series ({self._longitude_series.size})')

        self._map_fitter = MapFitter(self._latitude_series,
                                     self._longitude_series,
                                     add_markers=self._add_markers,
                                    )

        self._map_coordinate_transformer = \
            MapCoordinateTransformer(self._map_fitter.get_center_latitude(),
                                     self._map_fitter.get_center_longitude(),
                                     self._map_fitter.get_map_scaled_width(),
                                     self._map_fitter.get_map_scaled_height(),
                                     self._map_fitter.get_zoom(),
                                     self._map_fitter.get_scale(),
                                    )

        # laxy init
        self._root = None
        self._canvas = None
        self._map_on_canvas = None
        self._map_image = None
        self._x_y_series = None
        self._x_y_series_flattened = None

    def _setup_ui(self):
        self._root = tk.Tk()

        self._canvas = tk.Canvas(master=self._root,
                                 width=self._map_fitter.get_map_scaled_width(),
                                 height=self._map_fitter.get_map_scaled_height(),
                                )
        self._canvas.grid(row=0, column=0)
        self._map_on_canvas = self._canvas.create_image(0, 0, anchor='nw')

        self._canvas.itemconfig(self._map_on_canvas, image=self._get_map_image())

        self._canvas.create_line(*self._get_x_y_series_flattened(),
                                 fill=self._track_color,
                                )

        self._add_marker_dots()

    def _add_marker_dots(self):
        if self._marker_positions is not None:
            for position in self._marker_positions:
                y = self._map_coordinate_transformer.get_y_for_latitude(position[0])
                x = self._map_coordinate_transformer.get_x_for_longitude(position[1])
                self._canvas.create_oval(x-2, y-2, x+2, y+2, fill='blue')

    def _get_map_image(self):
        if self._map_image is None:
            self._map_image = ImageTk.PhotoImage(Image.open(self._map_fitter.get_map()))
        return self._map_image

    def _get_x_y_series(self):
        if self._x_y_series is None:
            r = []
            for idx in range(self._latitude_series.size):
                x = self._map_coordinate_transformer.get_x_for_longitude(self._longitude_series[idx]) # pylint: disable=C0301
                y = self._map_coordinate_transformer.get_y_for_latitude(self._latitude_series[idx])
                r.append([x, y])
            self._x_y_series = r
        return self._x_y_series

    def _get_x_y_series_flattened(self):
        if self._x_y_series_flattened is None:
            self._x_y_series_flattened = [item for sublist in self._get_x_y_series() for item in sublist] # pylint: disable=C0301
        return self._x_y_series_flattened

    def run(self):
        self._setup_ui()
        self._root.mainloop()
        return self


import collections
import itertools
import logging
import smtplib
import time
import typing as t
from email.mime.text import MIMEText

import arrow
import click
import jinja2
from crontab import CronTab
from insights_app.alerts_client import AlertsClient
from insights_app.smtp_client import smtp_client

INSIGHTS_DELTA = {"days": -2}
logger = logging.getLogger(__name__)

DJANGO_DT_FORMAT = "YYYY-MM-DDTHH:mm:ss"

def select_price_minmax(from_dt: arrow.Arrow, alerts_client: AlertsClient):
    """Select the earliest and the latest occurency of each item for each alert."""
    # Get lists of records for each combination of email, item_id and currency
    # Sorted by updated_date (ascended)
    results = alerts_client.get_alert_items(
        updated_at__gte=from_dt.format(DJANGO_DT_FORMAT),
        ordering="alert__email,item_id,currency,updated_at",
    )

    groups = [
        (group_key, list(vals))
        for group_key, vals in itertools.groupby(
            results, key=lambda item: (item.alert.email, item.item_id, item.currency)
        )
    ]

    # For each group get earliest and latest record
    groups = [(group_key, (vals[0], vals[-1])) for group_key, vals in groups]

    return groups

def prepare_email_data(groups, decrease_percent: float) -> t.Dict[str, t.List[t.Dict]]:
    """Analyse items to find price decrease."""
    if decrease_percent < 0 or decrease_percent > 1:
        raise ValueError("Decrease percent should be in [0;1] range")

    # For each user create list of changed items
    email_data = collections.defaultdict(list)
    for group_key, (earliest_item, latest_item) in groups:
        email, _, currency = group_key
        old_price = earliest_item.price
        new_price = latest_item.price

        if new_price > old_price * (1 - decrease_percent):
            logger.debug("Skip %s > %s * %s", new_price, old_price, decrease_percent)
            continue

        email_data[email].append(
            {
                "currency": currency,
                "old_price": old_price,
                "new_price": new_price,
                "title": latest_item.title,
                "web_url": latest_item.web_url,
            }
        )

    logger.debug("Send email data %s", email_data)
    return email_data

def send_emails(
    smtp: smtplib.SMTP,
    email_data: t.Dict[str, t.List[t.Dict]],
    sender_email: str,
):
    env = jinja2.Environment(
        loader=jinja2.PackageLoader("insights_app"),
        autoescape=jinja2.select_autoescape(),
    )
    template = env.get_template("price_decrease_email.html.j2")

    for receiver_email, items in email_data.items():
        content = template.render(items=items)
        message = MIMEText(content, "html")
        message["From"] = sender_email
        message["To"] = receiver_email
        message["Subject"] = "Insights about your alerts"
        smtp.sendmail(sender_email, receiver_email, message.as_string())

def price_decrease_job(
    alerts_client: AlertsClient,
    smtp: smtplib.SMTP,
    sender_email: str,
    decrease_percent: int,
):
    from_dt = arrow.utcnow().shift(**INSIGHTS_DELTA)
    groups = select_price_minmax(from_dt=from_dt, alerts_client=alerts_client)
    email_data = prepare_email_data(groups=groups, decrease_percent=decrease_percent)
    send_emails(smtp=smtp, email_data=email_data, sender_email=sender_email)

@click.command(name="price_decrease_job")
@click.option("--schedule", envvar="SCHEDULE")
@click.option("--email-host", envvar="EMAIL_HOST")
@click.option("--email-port", envvar="EMAIL_PORT", type=int)
@click.option("--email-host-user", envvar="EMAIL_HOST_USER")
@click.option("--email-host-password", envvar="EMAIL_HOST_PASSWORD")
@click.option("--sender-email", envvar="SENDER_EMAIL")
@click.option("--alerts-svc-url", envvar="ALERTS_SVC_URL")
@click.option("--decrease-percent", envvar="DECREASE_PERCENT", type=float, default=0.02)
def cli(
    schedule,
    email_host,
    email_port,
    email_host_user,
    email_host_password,
    sender_email,
    alerts_svc_url,
    decrease_percent,
):
    alerts_client = AlertsClient(url=alerts_svc_url)
    scheduler = CronTab(schedule)

    while True:
        time.sleep(scheduler.next(default_utc=True))
        click.echo("Start task")
        with smtp_client(
            email_host,
            email_port,
            email_host_user,
            email_host_password,
        ) as smtp:
            price_decrease_job(
                alerts_client,
                smtp,
                sender_email,
                decrease_percent,
            )
        click.echo("OK")

if __name__ == "__main__":
    cli()


import torch
from typeguard import check_argument_types

from espnet.nets.pytorch_backend.transformer.layer_norm import LayerNorm

class VariancePredictor(torch.nn.Module):
    """Variance predictor module.

    This is a module of variacne predictor described in `FastSpeech 2:
    Fast and High-Quality End-to-End Text to Speech`_.

    .. _`FastSpeech 2: Fast and High-Quality End-to-End Text to Speech`:
        https://arxiv.org/abs/2006.04558

    """

    def __init__(
        self,
        idim: int,
        n_layers: int = 2,
        n_chans: int = 384,
        kernel_size: int = 3,
        bias: bool = True,
        dropout_rate: float = 0.5,
    ):
        """Initilize duration predictor module.

        Args:
            idim (int): Input dimension.
            n_layers (int): Number of convolutional layers.
            n_chans (int): Number of channels of convolutional layers.
            kernel_size (int): Kernel size of convolutional layers.
            dropout_rate (float): Dropout rate.

        """
        assert check_argument_types()
        super().__init__()
        self.conv = torch.nn.ModuleList()
        for idx in range(n_layers):
            in_chans = idim if idx == 0 else n_chans
            self.conv += [
                torch.nn.Sequential(
                    torch.nn.Conv1d(
                        in_chans,
                        n_chans,
                        kernel_size,
                        stride=1,
                        padding=(kernel_size - 1) // 2,
                        bias=bias,
                    ),
                    torch.nn.ReLU(),
                    LayerNorm(n_chans, dim=1),
                    torch.nn.Dropout(dropout_rate),
                )
            ]
        self.linear = torch.nn.Linear(n_chans, 1)

    def forward(self, xs: torch.Tensor, x_masks: torch.Tensor = None) -> torch.Tensor:
        """Calculate forward propagation.

        Args:
            xs (Tensor): Batch of input sequences (B, Tmax, idim).
            x_masks (ByteTensor): Batch of masks indicating padded part (B, Tmax).

        Returns:
            Tensor: Batch of predicted sequences (B, Tmax, 1).

        """
        xs = xs.transpose(1, -1)  # (B, idim, Tmax)
        for f in self.conv:
            xs = f(xs)  # (B, C, Tmax)

        xs = self.linear(xs.transpose(1, 2))  # (B, Tmax, 1)

        if x_masks is not None:
            xs = xs.masked_fill(x_masks, 0.0)

        return xs


from itertools import combinations
from collections import deque

def solution(n, wires):
    ans = n
    for combi in combinations(wires,len(wires)-1):
        hash = {i:[] for i in range(n+1)}
        for relation in combi:
            x,y = relation
            hash[x].append(y)
            hash[y].append(x)

        visited = [0]* (n+1)

        start = combi[0][0]
        q = deque(hash[start])
        visited[start] = 1
        while q:
            tmp = q.popleft()
            if not visited[tmp]:
                visited[tmp] = 1
                for i in range(len(hash[tmp])):
                    if not visited[hash[tmp][i]]:
                        q.append(hash[tmp][i])
        cnt = sum(visited)
        ans = min(ans, abs(n-2*cnt))

    return ans

BruteForce/프로그래머스 전력망을 둘로 나누기.py

프로그래머스 전력망을 둘로 나누기.py


# -*- coding: utf-8 -*-
"""
Created on Mon May 18 00:24:01 2020

@author: israe
"""

#Resizing and Cropping

import cv2

img = cv2.imread("resources/lena_color.tiff")

#Resizing
imgResize = cv2.resize(img, (300,200))

#Cropped is catch some part of matrix height first and width second for cv2
imgCropped = img[0:200, 0:200]

cv2.imshow("Orig Image", img)
cv2.imshow("Resized Image", imgResize)
cv2.imshow("Cropped Image", imgCropped)
cv2.waitKey(0)


with open('input.txt', 'r') as file:
    from collections import deque
    N = int(file.readline())
    edges = {}
    for i in range(1, N+1):
        for j, is_connected in enumerate(list(map(int, file.readline().split()))):
            if is_connected:
                if i in edges:
                    edges[i].append(j+1)
                else:
                    edges[i] = [j+1]
    start, end = map(int, file.readline().split())
    is_visited = [False] * (N+1)
    to_traverse = deque()
    to_traverse.append((start,1))
    is_reacheble = False
    shortest_length = -1
    if start == end:
        is_reacheble = True
        shortest_length = 0
    while to_traverse and not is_reacheble:
        current_node, current_depth = to_traverse.pop()
        is_visited[current_node] = True
        for neighbor in edges.get(current_node, []):
            if neighbor == end:
                is_reacheble = True
                shortest_length = current_depth
            elif not is_visited[neighbor]:
                to_traverse.appendleft([neighbor,current_depth+1])
    print(shortest_length)

Algorithms and Data Structures/Part 2/6. Breadth First Search (BFS)/36.py