"""
Snake game view.
"""
import sys
import pygame
from snake_controller import get_mouse_position

def play_eaten_sound():
    """
    Plays a crunch sound.
    """
    food_eaten_sound = pygame.mixer.Sound('sounds/snake_eat_sound.wav')
    pygame.mixer.Sound.play(food_eaten_sound)

def play_click_sound():
    """
    Plays a click sound
    """
    click_sound = pygame.mixer.Sound('sounds/click_sound.wav')
    pygame.mixer.Sound.play(click_sound)

class View():
    """
    Pygame based view of a snake game.

    Attributes:
        _START_MENU: A png image of the start menu.
        _END_MENU: A png image of the end menu.
        _board: A Board instance representing the snake game to
            display.
        _screen: A display surface representing the window to display
            the rest of the game components on.
        _head_image: A png image representing the head of the snake.
        _start_menu_surface: A surface representing the start menu.
        _end_menu_surface: A surface representing the end menu.
        _start_menu_rect: A rect representing the start menu.
        _end_menu_rect: A rect representing the end menu.
    """
    _START_MENU = pygame.image.load('images/start_menu.png')
    _END_MENU = pygame.image.load('images/end_menu.png')

    def __init__(self, board):
        """
        Create a new view of a snake game.

        Args:
            _board: A Board instance representing the snake game to
                display.
            _screen: A display surface representing the window to display
                the rest of the game components on.
            _head_image: A png image representing the head of the snake.
            _start_menu_surface: A surface representing the start menu.
            _end_menu_surface: A surface representing the end menu.
            _start_menu_rect: A rect representing the start menu.
            _end_menu_rect: A rect representing the end menu.
        """

        self._board = board
        self._screen = pygame.display.set_mode((self._board.LENGTH + \
            self._board.BORDER_WIDTH,self._board.HEIGHT+self._board.BORDER_WIDTH))
        self._head_image = pygame.image.load('images/snake_left.png')
        self._start_menu_surface = pygame.display.set_mode((self._board.LENGTH + \
            self._board.BORDER_WIDTH,self._board.HEIGHT+self._board.BORDER_WIDTH))
        self._end_menu_surface = pygame.display.set_mode((self._board.LENGTH + \
            self._board.BORDER_WIDTH,self._board.HEIGHT+self._board.BORDER_WIDTH))
        self._start_menu_rect = self._START_MENU.get_rect(center = (300,300))
        self._end_menu_rect = self._END_MENU.get_rect(center = (300,300))

    def get_head_image(self):
        """
        Gets the correct image for the snake head.

        Gets the correct orientation of the snake head based on
        the snake's head position in relation to its body.
        """

        head_up = pygame.image.load('images/snake_up.png')
        head_down = pygame.image.load('images/snake_down.png')
        head_right = pygame.image.load('images/snake_right.png')
        head_left = pygame.image.load('images/snake_left.png')

        # figures out what image of the head to use based on
        # the relative position of the body and head
        head_orientation_x = self._board.snake.coordinates[1][0] - \
            self._board.snake.coordinates[0][0]
        head_orientation_y = self._board.snake.coordinates[1][1] - \
            self._board.snake.coordinates[0][1]
        if head_orientation_x == self._board.snake.GRID_SIZE and head_orientation_y == 0:
            self._head_image = head_left
        elif head_orientation_x == -self._board.snake.GRID_SIZE and head_orientation_y == 0:
            self._head_image = head_right
        elif head_orientation_x == 0 and head_orientation_y == self._board.snake.GRID_SIZE:
            self._head_image = head_up
        elif head_orientation_x == 0 and head_orientation_y == -self._board.snake.GRID_SIZE:
            self._head_image = head_down

    def draw(self, ate_potion):
        """
        Display a representation of the snake game.
        """

        pygame.init()
        pygame.mixer.init()
        pygame.display.set_caption("Ultimate Snake Game")

        icon = pygame.image.load('images/snake_icon.png')
        pygame.display.set_icon(icon)

        self._screen.fill('white')

        self.draw_apple()
        self.draw_border()
        self.draw_potion()
        self.draw_speed()

        if ate_potion:
            self.draw_invisible_snake()
        elif not ate_potion:
            self.draw_snake()
        self.draw_menus()
        self.draw_score()

        pygame.display.update()

    def draw_apple(self):
        """
        Displays the apple item.
        """
        # blit an image of an apple for the food
        apple_image = pygame.image.load('images/apple.png').convert_alpha()
        apple_rect = apple_image.get_rect()
        apple_rect.x = self._board.food.item_location[0]
        apple_rect.y = self._board.food.item_location[1]
        self._screen.blit(apple_image, apple_rect)

    def draw_potion(self):
        """
        Displays the potion item.
        """
        # blit an image of the potion for the invisibility potion
        potion_image = pygame.image.load('images/potion.png').convert_alpha()
        potion_rect = potion_image.get_rect()
        potion_rect.x = self._board.potion.item_location[0]
        potion_rect.y = self._board.potion.item_location[1]
        self._screen.blit(potion_image, potion_rect)

    def draw_speed(self):
        """
        Draws the speed boost item.
        """
        # blit an image of the speed boost for the speed boost item
        lightning_image = pygame.image.load(
            'images/lightning.png').convert_alpha()
        lightning_rect = lightning_image.get_rect()
        lightning_rect.x = self._board.speed_boost.item_location[0]
        lightning_rect.y = self._board.speed_boost.item_location[1]
        self._screen.blit(lightning_image, lightning_rect)

    def draw_snake(self):
        """
        Displays the snake head and body.
        """
       # get head image and blit
        self.get_head_image()
        head_rect = self._head_image.get_rect()
        head_rect.x = self._board.snake.coordinates[0][0]
        head_rect.y = self._board.snake.coordinates[0][1]
        self._screen.blit(self._head_image, head_rect)

        # get surface for each snake body chunk, and blit each one
        for segment in self._board.snake.coordinates[1:]:
            surface = pygame.Surface((30, 30))
            surface.fill(pygame.Color('blue'))
            segment_rect = surface.get_rect()
            segment_rect.x = segment[0]
            segment_rect.y = segment[1]
            self._screen.blit(surface, segment_rect)

    def draw_invisible_snake(self):
        """
        Displays invisible snake.

        Draws the invisible snake by not displaying anything.
        """

    def draw_border(self):
        """
        Displays the border frame around the screen.
        """
        # create frame around the game window
        # top line
        pygame.draw.rect(self._screen, (169, 169, 169), [
                         0, 0, self._board.LENGTH, self._board.BORDER_WIDTH*2])
        # bottom line
        pygame.draw.rect(self._screen, (169, 169, 169), [
                         0, self._board.HEIGHT, self._board.LENGTH, self._board.BORDER_WIDTH])
        # left line
        pygame.draw.rect(self._screen, (169, 169, 169), [
                         0, 0, self._board.BORDER_WIDTH, self._board.HEIGHT])
        # right line
        pygame.draw.rect(self._screen, (169, 169, 169), [
                         self._board.LENGTH, 0, self._board.BORDER_WIDTH, self._board.LENGTH +
                         self._board.BORDER_WIDTH])

    def draw_score(self):
        """
        Displays the score.
        """
        # display score
        score = str(self._board.score)
        font = pygame.font.SysFont(None, 60)
        score_text = font.render(f'Score: {score}', True, 'black')
        self._screen.blit(score_text, (30, 10))
        pygame.display.update()

    def draw_start_menu(self):
        """
        Displays the start menu.
        """
        self._start_menu_surface.blit(self._START_MENU, self._start_menu_rect)

    def draw_game_over(self):
        """
        Displays the game over menu.
        """
        self._end_menu_surface.blit(self._END_MENU, self._end_menu_rect)

    def draw_menus(self):
        """
        Draws each menu as needed.
        """
        # Draws starts screen.
        while self._board.start_game is False:
            self.draw_start_menu()
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                # Will exit start screen if mouse cursor clicks on start button.
                if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                    if self._start_menu_rect.collidepoint(get_mouse_position()):
                        play_click_sound()
                        self._board.start_game = True
        # Draws game over screen.
        while self._board.game_over is True:
            self._screen.fill('white')
            self.draw_game_over()
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                # Will trigger new game if mouse cursor clicks on restart button.
                if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                    if self._end_menu_rect.collidepoint(get_mouse_position()):
                        play_click_sound()
                        self._board.new_game = True
            break


from typing import List
def products_div(arr: List[int]) -> List[int]:
    prod = 1
    for x in arr:
        prod *= x
    res = [prod//x for x in arr]
    return res

'''
arr: [2, 3, 4, 5,]
l:   [2 23 234]
r:   [5 54 543]
res: [.-345, 2-45, 23-5, 234-.]

l = []
prod = 1
for i in range(len(arr)-1):
    prod *= arr[i]
    l.append(prod)
prod = 1
for i in range(len(arr)-1, 0, -1):
    prod *= arr[i]
    r.append(prod)

res = [1] * len(arr)
for i in range(len(arr) - 1):
    res[i] *= r[-i-1]
    res[i+1] = l[i]
return res
'''
def products(arr: List[int]) -> List[int]:
    l = []
    prod = 1
    for i in range(len(arr)-1):
        prod *= arr[i]
        l.append(prod)
    prod = 1
    r = []
    for i in range(len(arr)-1, 0, -1):
        prod *= arr[i]
        r.append(prod)

    res = [1] * len(arr)
    for i in range(len(arr) - 1):
        res[i] *= r[-i-1]
        res[i+1] = l[i]
    return res

arr = [2, 3, 4, 5,]
print(products(arr)) # [60, 40, 30, 24]


import json
import time

import requests

# 크롤링 대상 URL 리스트
PAGE_URL_LIST = [
    'http://example.com/1.page'
    'http://example.com/2.page',
    'http://example.com/3.page',
]

def fetch_pages():
    """페이지의 내용을 추출합니다"""
    # 처리 기록 전용 로그 파일을 append 모드로 엽니다
    f_info_log = open('crawler_info.log', 'a')

    # 오류 기록 전용 로그 파일을 append 모드로 엽니다
    f_error_log = open('crawler_error.log', 'a')

    # 추출 내용을 저장할 딕셔너리
    page_contents = {}

    # 터미널에 처리 시작을 출력하고, 로그 파일에도 메시지를 출력합니다.
    msg = "크롤링을 시작합니다\n"
    print(msg)
    f_info_log.write(msg)

    for page_url in PAGE_URL_LIST:
        r = requests.get(page_url, timeout=30)
        try:
            r.raise_for_status()  # 응답에 문제가 있으면 예외를 발생시킵니다.
        except requests.exceptions.RequestException as e:
            # requests와 관련된 예외가 발생하면
            # 터미널과 오류 로그에 오류를 출력합니다.
            msg = "[ERROR] {exception}\n".format(exception=e)
            print(msg)
            f_error_log.write(msg)
            continue  # 예외가 발생하면 반복을 중지하는게 아니라 건너 뜁니다.
        # 정상적으로 내용을 추출했다면 딕셔너리에 내용을 저장합니다.
        page_contents[page_url] = r.text
        time.sleep(1)  # 상대 사이트에 대한 부하를 고려해서 요청 간격을 설정합니다.

    f_info_log.close()
    f_error_log.close()

    return page_contents

if __name__ == '__main__':
    page_contents = fetch_pages()
    f_page_contents = open('page_contents.json', 'w')
    json.dump(page_contents, f_page_contents, ensure_ascii=False)
    f_page_contents.close()


import json
import re
from typing import Any

from aiohttp import ClientSession

from .exceptions import FailedToParseIntialData

class Client:
    """YouTube API client."""

    _session: ClientSession

    @classmethod
    async def new(cls, host: str = "https://www.youtube.com"):
        """Create a new YouTube client."""
        self = cls()
        self._session = ClientSession(base_url=host, raise_for_status=True)

        return self

    async def get_search_results(self, search_query: str) -> str:
        """Get YouTube search results."""
        async with self._session.get(
            "/results", params={"search_query": search_query}
        ) as response:
            return await response.text()

    async def close(self) -> None:
        """Close client session."""
        await self._session.close()

def get_initial_data(search_results: str) -> dict[str, Any]:
    """Get YouTube initial data."""
    initial_data_regex = re.compile(r"(var\ ytInitialData\ =\ )(.*);</script><script")
    match = initial_data_regex.search(search_results)
    if not match:
        raise FailedToParseIntialData
    return json.loads(match.group(2))


#!/usr/bin/env python3

# Simple Script to replace cron for Docker

import argparse
import sys
from subprocess import CalledProcessError, run
from time import sleep, time

def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("interval", help="Time in seconds between runs", type=int)
    args = parser.parse_args()

    print("Running gem mirror every %ds" % args.interval, file=sys.stderr)
    try:
        while True:
            start_time = time()

            try:
                run(['/tmp/run.sh'], check=True)
            except CalledProcessError as cpe:
                return cpe.returncode

            run_time = time() - start_time
            if run_time < args.interval:
                sleep_time = args.interval - run_time
                print("Sleeping for %ds" % sleep_time, file=sys.stderr)
                sleep(sleep_time)
    except KeyboardInterrupt:
        pass

    return 0

if __name__ == "__main__":
    sys.exit(main())


import pytest

from collections import defaultdict

class UndergroundSystem:
    def __init__(self):
        self.user_table = defaultdict(list)
        self.course_table = defaultdict(list)

    def checkIn(self, id: int, stationName: str, t: int) -> None:
        assert len(self.user_table[id]) == 0
        self.user_table[id].append((stationName, t))

    def checkOut(self, id: int, stationName: str, t: int) -> None:
        assert len(self.user_table[id]) == 1
        source = self.user_table[id].pop()

        travel_t = t - source[1]
        course = f"{source[0]}_{stationName}"
        self.course_table[course].append(travel_t)

    def getAverageTime(self, startStation: str, endStation: str) -> float:
        course = f"{startStation}_{endStation}"
        travel_ts = self.course_table[course]
        return round(sum(travel_ts) / len(travel_ts), 5)

@pytest.mark.parametrize(
    "action, value, expected",
    [
        (
            [
                "UndergroundSystem",
                "checkIn",
                "checkIn",
                "checkIn",
                "checkOut",
                "checkOut",
                "checkOut",
                "getAverageTime",
                "getAverageTime",
                "checkIn",
                "getAverageTime",
                "checkOut",
                "getAverageTime",
            ],
            [
                [],
                [45, "Leyton", 3],
                [32, "Paradise", 8],
                [27, "Leyton", 10],
                [45, "Waterloo", 15],
                [27, "Waterloo", 20],
                [32, "Cambridge", 22],
                ["Paradise", "Cambridge"],
                ["Leyton", "Waterloo"],
                [10, "Leyton", 24],
                ["Leyton", "Waterloo"],
                [10, "Waterloo", 38],
                ["Leyton", "Waterloo"],
            ],
            [
                None,
                None,
                None,
                None,
                None,
                None,
                None,
                14.00000,
                11.00000,
                None,
                11.00000,
                None,
                12.00000,
            ],
        ),
        (
            [
                "UndergroundSystem",
                "checkIn",
                "checkOut",
                "getAverageTime",
                "checkIn",
                "checkOut",
                "getAverageTime",
                "checkIn",
                "checkOut",
                "getAverageTime",
            ],
            [
                [],
                [10, "Leyton", 3],
                [10, "Paradise", 8],
                ["Leyton", "Paradise"],
                [5, "Leyton", 10],
                [5, "Paradise", 16],
                ["Leyton", "Paradise"],
                [2, "Leyton", 21],
                [2, "Paradise", 30],
                ["Leyton", "Paradise"],
            ],
            [None, None, None, 5.00000, None, None, 5.50000, None, None, 6.66667],
        ),
    ],
)
def test(action, value, expected):
    print()
    outputs = []
    obj = None
    for act, values in zip(action, value):
        if act == "UndergroundSystem":
            obj = UndergroundSystem()
            outputs.append(None)
        elif act == "checkIn":
            outputs.append(obj.checkIn(values[0], values[1], values[2]))
        elif act == "checkOut":
            outputs.append(obj.checkOut(values[0], values[1], values[2]))
        elif act == "getAverageTime":
            outputs.append(obj.getAverageTime(values[0], values[1]))

    assert expected == outputs

if __name__ == "__main__":
    sys.exit(pytest.main(["-s", "-v"] + sys.argv))


import socket
import copy
import struct
import time
import board
import adafruit_bno055
import subprocess

from button import ToggleButton
from serial_read import DeviceInterface

class UDPSocket:
    def __init__(self, IP_ADDR="192.168.0.198", PORT=65432):
        self.IP_ADDR = "192.168.0.198"
        self.APP_PORT = 65432

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    def send(self, data):
        self.socket.sendto(data, (self.IP_ADDR, self.APP_PORT))

def buttonToggleCallback():
    print("button pushed")

if __name__ == '__main__':

    # socket and toggle button
    s = UDPSocket()
    button = ToggleButton(17)

    # interface to stm board
    head_device = DeviceInterface(port="/dev/ttyACM0")
    head_device.start()

    # interface to handheld gyro
    i2c = board.I2C()
    hand_device = adafruit_bno055.BNO055_I2C(i2c)

    x,y,z,x_sign,y_sign,z_sign = hand_device.axis_remap
    hand_device.axis_remap = (z, x, y, x_sign, y_sign, z_sign)

    seq = 0

    itr = 0
    yaw_ref = 0
    while itr <= 1000:
        data = head_device.getAngles()
        yaw_ref += data[2]
        itr+=1

    yaw_ref /= itr

    print("starting")
    while True:
        start = time.time()

        angles_head = copy.deepcopy(head_device.getAngles())
        angles_head[2] = yaw_ref - angles_head[2]

        angles_hand = copy.deepcopy(hand_device.euler)

        # angles_head = (head_device.getAngles())
        # angles_hand = (hand_device.euler)

        if None in angles_head:
            continue

        if (angles_hand[1] == None):
            continue

        msg = bytearray(struct.pack('f', angles_head[0]))
        msg += bytearray(struct.pack('f', angles_head[1]))
        msg += bytearray(struct.pack('f', angles_head[2]))

        if button.getValue():
            val = angles_hand[1]/90.
            msg += bytearray(struct.pack('f', val))
        else:
            val = float(0.0)
            msg += bytearray(struct.pack('f', val))

        s.send(msg)

        end = time.time()
        if (end - start) >= 0.01:
            print("Deadline not met")


from setuptools import setup, find_packages
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='quantile_transformer_tf',
    version='1.2',
    description='An implementation of QuantileTransformer in tensorflow',
    long_description=long_description,
    url='https://github.com/yandexdataschool/QuantileTransformerTF',
    author='Nikita Kazeev',
    author_email='nikita.kazeev@cern.ch',
    license='Apache 2.0',
    classifiers=[
        'Development Status :: 4 - Beta',
        'Intended Audience :: Science/Research',
        'License :: OSI Approved :: Apache Software License',
        'Programming Language :: Python :: 3',
    ],
    packages=find_packages(),
    install_requires=['numpy>=1.13',
                      'tensorflow>=1.9']
)


###############################
# ProcessorBase.py
#
# Basic processor
###############################

import sys
import os

###############################
# Basic processor
###############################

class ProcessorBase:

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

	@property
	def description(self):
		raise NotImplementedError

	#
	# Actions avail with processor
	#

	@property
	def has_childs(self):
		return False
	def get_childs(self, callback=None):
		raise NotImplementedError #return None

	@property
	def has_hexdump(self):
		return False
	def get_hexdump(self, callback=None):
		raise NotImplementedError #return ""

	@property
	def has_details(self):
		return False
	def get_details(self, callback=None):
		raise NotImplementedError #return ""

	@property
	def can_save(self):
		return False
	def save(self, path, callback=None):
		raise NotImplementedError #return False

	#
	# Basic methods avail to all processors
	#

	def load(self, callback=None):
		if callback: callback()
		if not os.path.exists(self.file):
			print("{}: File '{}' doesn't exist".format(self.__name__,self.file))
			return None
		f = open(self.file, "rb", 0)
		if not f:
			print("{}: Error opening file '{}'".format(self.__name__,self.file))
			return None
		if callback: callback()
		data = f.readall()
		if callback: callback()
		f.close()
		return data

	#
	# Private methods avail to all processors
	#

	def _create_hexdump(self, data, start=0, length=-1, linelength=16, callback=None):
		dump = ""
		if length <= 0: length = len(data)
		#lines = int(length / linelength)
		#fragment = length % linelength
		#if fragment > 0:
		#	lines += 1
		lines = int((length+linelength-1) / linelength)
		l = 0
		while l < lines:
			if callback: callback()
			p = start + (l * linelength)
			addr = self._tohex(p,8)
			by = " "
			d = " "
			i = 0
			while i < linelength:
				if p < (start+length):
					val = data[p]
					by += " " + self._tohex(val,2)
					d += chr(val) if val >= 32 and val < 128 else "."
				else:
					by += "   "
					d += " "
				i += 1
				p += 1
				if i % 4 == 0: by += " "
			l += 1
			#s = addr + " " + by + " " + d
			#if l < lines: print(s)
			#else: sys.stdout.write(s)
			dump += addr + " " + by + " " + d + "\n"
		return dump

	def _tohex(self,val,length=2):
		s = hex(val).upper()[2:]
		return ("0000000000000000" + s)[-length:]


import numpy as np
from tqdm import tqdm
from collections import Counter
import pandas as pd

class PMI():
    def __init__(self, text, lang):
        self.text = text
        self.lang = lang

        self.p_1, self.c_1 = self.get_unigram_probs()
        self.bigram = self.get_joint_probs()
        self.distant = self.get_distant_probs()

    def get_unigram_probs(self):
        ''' compute all unigram probabilities '''
        text_size = len(self.text)
        counter_1 = Counter(self.text)
        # p_1(w_i) = c_1(w_i)/T
        p_1 = {word: count/text_size for word, count in counter_1.items()}
        return p_1, counter_1

    def get_joint_probs(self):
        '''
        calculate joint probability p(a,b)
        for all neighbouring words in text
        '''
        counts = Counter()
        # get counts of pairs of words
        for i, j in zip(self.text, self.text[1:]):
            counts[i, j] += 1
        total = sum(counts.values())
        # probability based on counts
        joint_probs = {k: v/total for k, v in counts.items()}
        return joint_probs

    def get_distant_probs(self):
        '''
        calculate joint probability p(a,b) for all words
        with distance at most 50 in either direction
        '''
        counts = Counter()
        for (i,a) in tqdm(enumerate(self.text)):
            if i < 50: ran, start = self.text[:i+50], 0
            elif i > len(self.text) - 50: ran, start = self.text[i-50:], i-50
            else: ran, start = self.text[i-50:i+50], i-50
#            ran, start = self.text[:i+50], i
            for (j,b) in enumerate(ran, start):
                if i != j:
                    counts[a, b] += 1
        total = sum(counts.values())
        # probability based on counts
        joint_probs = {k: v/total for k, v in counts.items()}
        return joint_probs

    def pmi(self, a, b, probs):
        '''
        I'(a,b) = log_2(p(a,b)/p(a)p(b)) = log_2(p(a|b)/p(a))
        '''
        joint_prob = probs[a, b]
        p_a = self.p_1[a]
        p_b = self.p_1[b]
        return np.log2(joint_prob/(p_a * p_b))

    def write_to_file(self, label, results):
        ''' save results '''
        with open('pmi_results'+self.lang+'.txt', 'a') as f:
            for (a, b), pmi in reversed(results):
                f.write('%s\t%s\t%s\t%1.4f\n' % (label, a, b, pmi))

    def run(self):
        results = {}
        for (i,a),(j,b) in zip(enumerate(self.text),enumerate(self.text[1:],1)):
            # disregard pairs in which one or both words
            # appear less than 10 times in the corpus
            if self.c_1[a] >= 10 and self.c_1[b] >= 10:
                results[a, b] = self.pmi(a, b, self.bigram)
        sorted_results = sorted(results.items(), key=lambda x:x[1])
        self.write_to_file('bigram', sorted_results)

        results = {}
        for (i,a) in enumerate(self.text):
            if i < 50: ran, start = self.text[:i+50], 0
            elif i > len(self.text) - 50: ran, start = self.text[i-50:], i-50
            else: ran, start = self.text[i-50:i+50], i-50
            for (j,b) in enumerate(ran, start):
                if i != j:
                    if self.c_1[a] >= 10 and self.c_1[b] >= 10:
                        results[a, b] = self.pmi(a, b, self.distant)
        sorted_results = sorted(results.items(), key=lambda x:x[1])
        self.write_to_file('distant', sorted_results)
        print(list(reversed(sorted_results[-50:])))

if __name__ == '__main__':
    for lang in ['EN', 'CZ']:
        with open('TEXT'+lang+'1.txt', 'r', encoding='iso-8859-2') as f:
            text = [word.strip() for word in f.readlines()]

        pmi = PMI(text, lang)

        # prepare results file
        with open('pmi_results'+lang+'.txt', 'w') as f:
            f.write('WORD1\tWORD2\tPMI\n')

        # run experiments
        results = pmi.run()


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

import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as pl
from mpl_toolkits.basemap import Basemap

# llcrnrlat,llcrnrlon,urcrnrlat,urcrnrlon
# are the lat/lon values of the lower left and upper right corners
# of the map.
# lat_ts is the latitude of true scale.
# resolution = 'c' means use crude resolution coastlines.
pl.clf()
m = Basemap(projection='merc', llcrnrlat=-80, urcrnrlat=80, \
            llcrnrlon=-180, urcrnrlon=180, lat_ts=20, resolution='c')
m.drawcoastlines()
m.fillcontinents(color='coral', lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-90., 91., 30.))
m.drawmeridians(np.arange(-180., 181., 60.))
m.drawmapboundary(fill_color='aqua')
m.drawstates()
pl.title("Mercator Projection")
pl.show()

pl.savefig('basemap.png')


from django.conf import settings
from django.contrib.sites.models import Site

from .models import SiteSettings

class SiteSettingsMiddleware:

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

    def __call__(self, request):
        try:
            request.wf_site = Site.objects._get_site_by_request(request)
            settings.SITE_ID = request.wf_site.id
        except Site.DoesNotExist:
            request.wf_site = None

        try:
            request.sitesettings = SiteSettings.objects.get(site=request.wf_site)
        except SiteSettings.DoesNotExist:
            request.sitesettings = None

        return self.get_response(request)


#!/usr/bin/env python3

"""
RBE/CS549 Spring 2023: Computer Vision

Author(s):
Uday Sankar (usankar@wpi.edu)
Mechatronics, Robotics and Automation Engineering,
Worcester Polytechnic Institute
"""

# Dependencies:
# opencv, do (pip install opencv-python)
# skimage, do (apt install python-skimage)
# termcolor, do (pip install termcolor)

import torch
import torchvision
from torch.utils.tensorboard import SummaryWriter
from torchvision import datasets, transforms
from torch.optim import AdamW
from torchvision.datasets import CIFAR10
import cv2
import sys
import os
import numpy as np
import random
import skimage
import PIL
import os
import glob
import random
from skimage import data, exposure, img_as_float
import matplotlib.pyplot as plt
import time
from torchvision.transforms import ToTensor
import argparse
import shutil
import string
from termcolor import colored, cprint
import math as m
from tqdm.notebook import tqdm
# import Misc.ImageUtils as iu
from Network.Network import CIFAR10Model
from Misc.MiscUtils import *
from Misc.DataUtils import *

# setting the device as 'cuda'
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
print(f'Device being used: {device}')

# global variables for testing
ModelNumber = 1
CheckPointPath = 'Checkpoints/BasicNet/14model.ckpt'  # BasiNet by default

# Don't generate pyc codes
sys.dont_write_bytecode = True

def GenerateBatch(TrainSet, TrainLabels, ImageSize, MiniBatchSize):
    """
    Inputs:
    TrainSet - Variable with Subfolder paths to train files
    NOTE that Train can be replaced by Val/Test for generating batch corresponding to validation (held-out testing in this case)/testing
    TrainLabels - Labels corresponding to Train
    NOTE that TrainLabels can be replaced by Val/TestLabels for generating batch corresponding to validation (held-out testing in this case)/testing
    ImageSize is the Size of the Image
    MiniBatchSize is the size of the MiniBatch

    Outputs:
    I1Batch - Batch of images
    LabelBatch - Batch of one-hot encoded labels
    """
    I1Batch = []
    LabelBatch = []

    ImageNum = 0
    while ImageNum < MiniBatchSize:
        # Generate random image
        RandIdx = random.randint(0, len(TrainSet)-1)

        ImageNum += 1

          ##########################################################
          # Add any standardization or data augmentation here!
          ##########################################################

        I1, Label = TrainSet[RandIdx]

        # Append All Images and Mask
        I1Batch.append(I1)
        LabelBatch.append(torch.tensor(Label))

    return torch.stack(I1Batch).to(device), torch.stack(LabelBatch).to(device)

def PrettyPrint(NumEpochs, DivTrain, MiniBatchSize, NumTrainSamples, LatestFile):
    """
    Prints all stats with all arguments
    """
    print('Number of Epochs Training will run for ' + str(NumEpochs))
    print('Factor of reduction in training data is ' + str(DivTrain))
    print('Mini Batch Size ' + str(MiniBatchSize))
    print('Number of Training Images ' + str(NumTrainSamples))
    if LatestFile is not None:
        print('Loading latest checkpoint with the name ' + LatestFile)

def TrainOperation(TrainLabels, NumTrainSamples, ImageSize, ModelNumber,
                   NumEpochs, MiniBatchSize, SaveCheckPoint, CheckPointPath,
                   DivTrain, LatestFile, TrainSet, LogsPath, TestSet, TestLabels):
    """
    Inputs:
    TrainLabels - Labels corresponding to Train/Test
    NumTrainSamples - length(Train)
    ImageSize - Size of the image
    NumEpochs - Number of passes through the Train data
    MiniBatchSize is the size of the MiniBatch
    SaveCheckPoint - Save checkpoint every SaveCheckPoint iteration in every epoch, checkpoint saved automatically after every epoch
    CheckPointPath - Path to save checkpoints/model
    DivTrain - Divide the data by this number for Epoch calculation, use if you have a lot of dataor for debugging code
    LatestFile - Latest checkpointfile to continue training
    TrainSet - The training dataset
    LogsPath - Path to save Tensorboard Logs
    Outputs:
    Saves Trained network in CheckPointPath and Logs to LogsPath
    """
    # Initialize the model
    model = CIFAR10Model(ModelNumber)
    model = model.to(device)
    ###############################################
    # Fill your optimizer of choice here!
    ###############################################
    Optimizer = AdamW(model.parameters(), lr=1e-3)

    # Tensorboard
    # Create a summary to monitor loss tensor
    Writer = SummaryWriter(LogsPath)
    # adding the graph of the model to tensorboard for visualization
    Writer.add_graph(model, GenerateBatch(TrainSet, TrainLabels, ImageSize, MiniBatchSize)[0])

    if LatestFile is not None:
        CheckPoint = torch.load(CheckPointPath + LatestFile + '.ckpt')
        # Extract only numbers from the name
        StartEpoch = int(''.join(c for c in LatestFile.split('a')[0] if c.isdigit()))
        model.load_state_dict(CheckPoint['model_state_dict'])
        print('Loaded latest checkpoint with the name ' + LatestFile + '....')
    else:
        StartEpoch = 0
        print('New model initialized....')

    for Epochs in tqdm(range(StartEpoch, NumEpochs)):
        train_acc_plot = 0
        train_loss_plot = 0
        NumIterationsPerEpoch = int(NumTrainSamples/MiniBatchSize/DivTrain)
        for PerEpochCounter in tqdm(range(NumIterationsPerEpoch)):
            Batch = GenerateBatch(TrainSet, TrainLabels, ImageSize, MiniBatchSize)

            # Predict output with forward pass
            LossThisBatch = model.training_step(Batch)

            Optimizer.zero_grad()
            LossThisBatch.backward()
            Optimizer.step()

            # Save checkpoint every some SaveCheckPoint's iterations
            if PerEpochCounter % SaveCheckPoint == 0:
                # Save the Model learnt in this epoch
                SaveName =  CheckPointPath + str(Epochs) + 'a' + str(PerEpochCounter) + 'model.ckpt'

                torch.save({'epoch': Epochs,'model_state_dict': model.state_dict(),'optimizer_state_dict': Optimizer.state_dict(),'loss': LossThisBatch}, SaveName)
                print('\n' + SaveName + ' Model Saved...')

            result = model.validation_step(Batch)
            train_acc_plot += result['acc']
            train_loss_plot += result['loss']
            model.epoch_end(Epochs*NumIterationsPerEpoch + PerEpochCounter, result)
            # Tensorboard
            # Writer.add_scalar('LossEveryIter', result["loss"], Epochs*NumIterationsPerEpoch + PerEpochCounter)
            # Writer.add_scalar('Accuracy', result["acc"], Epochs*NumIterationsPerEpoch + PerEpochCounter)

            # # If you don't flush the tensorboard doesn't update until a lot of iterations!
            # Writer.flush()

        model.eval()
        test_loss_plot = 0
        test_acc_plot = 0
        with torch.no_grad():
            Batch_ = GenerateBatch(TestSet, TestLabels, ImageSize, MiniBatchSize)
            test_result = model.validation_step(Batch_)
            test_loss_plot += test_result["loss"]
            test_acc_plot += test_result["acc"]

        Writer.add_scalars('Training/Testing Loss', {'TrainLossPerEpoch': train_loss_plot/NumIterationsPerEpoch, 'TestLossPerEpoch': test_loss_plot}, Epochs)
        Writer.add_scalars('Training/Testing Accuracy', {'TrainAccuracyPerEpoch': train_acc_plot/NumIterationsPerEpoch, 'TestAccuracyPerEpoch': test_acc_plot}, Epochs)
        Writer.flush()

        # Save model every epoch
        SaveName = CheckPointPath + str(Epochs) + 'model.ckpt'
        torch.save({'epoch': Epochs,'model_state_dict': model.state_dict(),'optimizer_state_dict': Optimizer.state_dict(),'loss': LossThisBatch}, SaveName)
        print('\n' + SaveName + ' Model Saved...')

def main():
    """
    Inputs:
    None
    Outputs:
    Runs the Training and testing code based on the Flag
    """
    # Parse Command Line arguments
    Parser = argparse.ArgumentParser()
    Parser.add_argument('--CheckPointPath', default='./Checkpoints/', help='Path to save Checkpoints, Default: ../Checkpoints/')
    Parser.add_argument('--NumEpochs', type=int, default=15, help='Number of Epochs to Train for, Default:15')
    Parser.add_argument('--DivTrain', type=int, default=1, help='Factor to reduce Train data by per epoch, Default:1')
    Parser.add_argument('--MiniBatchSize', type=int, default=32, help='Size of the MiniBatch to use, Default:32')
    Parser.add_argument('--LoadCheckPoint', type=int, default=0, help='Load Model from latest Checkpoint from CheckPointsPath?, Default:0')
    Parser.add_argument('--LogsPath', default='./Logs/', help='Path to save Logs for Tensorboard, Default=Logs/')
    Parser.add_argument('--ModelNumber', default=1, help='Model Type: 1-BasicNet, 2-BatchNormNet, 3-ResNet, 4-ResNeXt, 5-DenseNet, Default:1')

    Args = Parser.parse_args()
    NumEpochs = Args.NumEpochs
    DivTrain = float(Args.DivTrain)
    MiniBatchSize = Args.MiniBatchSize
    LoadCheckPoint = Args.LoadCheckPoint
    CheckPointPath = Args.CheckPointPath
    LogsPath = Args.LogsPath

    model_number_dict = {1: 'BasicNet', 2: 'BatchNormNet', 3: 'ResNet', 4: 'ResNeXt', 5: 'DenseNet'}
    global ModelNumber
    ModelNumber = int(Args.ModelNumber)
    # ModelNumber = int(input("Select model type (1-BasicNet, 2-BatchNormNet, 3-ResNet, 4-ResNeXt, 5-Densenet): "))
    # print(f"Selected Model Type: {model_number_dict[ModelNumber]}")

    # # setting the checkpoint and logs path based on the model selected
    CheckPointPath = CheckPointPath + model_number_dict[ModelNumber] + '/'
    LogsPath = LogsPath + model_number_dict[ModelNumber] + '/'

    # Default Hyperparameters
    NumEpochs = 15
    TrainSet = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=ToTensor())
    TestSet = CIFAR10(root='data/', train=False, transform=ToTensor())

    TestLabelsPath = "./TxtFiles/LabelsTest.txt"

    TestLabelsPath = "./TxtFiles/LabelsTest.txt"
    TestLabels = ReadLabels(TestLabelsPath)

    # Setup all needed parameters including file reading
    # SaveCheckPoint, ImageSize, NumTrainSamples, TrainLabels, NumClasses = SetupAll(TestLabelsPath, CheckPointPath)
    SaveCheckPoint, ImageSize, NumTrainSamples, TrainLabels, NumClasses = SetupAll(TestLabelsPath, CheckPointPath)

    # Find Latest Checkpoint File
    if LoadCheckPoint==1:
        LatestFile = FindLatestModel(CheckPointPath)
    else:
        LatestFile = None

    # Pretty print stats
    PrettyPrint(NumEpochs, DivTrain, MiniBatchSize, NumTrainSamples, LatestFile)

    TrainOperation(TrainLabels, NumTrainSamples, ImageSize, ModelNumber,
                NumEpochs, MiniBatchSize, SaveCheckPoint, CheckPointPath,
                DivTrain, LatestFile, TrainSet, LogsPath, TestSet, TestLabels)

if __name__ == '__main__':
    main()


import urllib.request
from bs4 import BeautifulSoup

f = open("Newyork_articles.txt", 'w')
html = 'http://www.nytimes.com/'
open_url = urllib.request.urlopen(html)
soup = BeautifulSoup(open_url, 'html.parser')
article_headings = soup.find_all(class_="indicate-hover")
head = "Articles for Today:\n"
i = 0
f.write(head)
for heading in article_headings:
    i += 1
    f.write("\n"+str(i)+"."+heading.string+"\n")
f.close()


from rackmond import configure_rackmond

# flag
#  ascii   0x80XX
#  2'comp  0x4NXX
#  decimal 0x20XX
#  bitmap 1,2 0x10XX

reglist = [
    {"begin": 0x0, "length": 8, "flags": 0x8000},  # MFR_MODEL  # ascii
    {"begin": 0x10, "length": 8, "flags": 0x8000},  # MFR_DATE  # ascii
    {"begin": 0x20, "length": 8, "flags": 0x8000},  # FB Part #  # ascii
    {"begin": 0x30, "length": 4, "flags": 0x8000},  # HW Revision  # ascii
    {"begin": 0x38, "length": 4, "flags": 0x8000},  # FW Revision  # ascii
    {"begin": 0x40, "length": 16, "flags": 0x8000},  # MFR Serial #  # ascii
    {"begin": 0x60, "length": 4, "flags": 0x8000},  # Workorder #  # ascii
    {
        "begin": 0x68,  # PSU Status
        "length": 1,
        "keep": 10,  # 10-sample ring buffer
        "flags": 0x1000 | 1,
    },
    {
        "begin": 0x69,  # Battery Status
        "length": 1,
        "keep": 10,  # 10-sample ring buffer
        "flags": 0x1000 | 1,
    },
    {
        "begin": 0x6B,  # BBU Battery Mode
        "length": 1,
        "keep": 10,  # 10-sample ring buffer
        "flags": 1,
    },
    {
        "begin": 0x6C,  # BBU Battery Status
        "length": 1,
        "keep": 10,  # 10-sample ring buffer
        "flags": 1,
    },
    {"begin": 0x6D, "length": 1, "keep": 10},  # BBU Cell Voltage 1
    {"begin": 0x6E, "length": 1, "keep": 10},  # BBU Cell Voltage 2
    {"begin": 0x6F, "length": 1, "keep": 10},  # BBU Cell Voltage 3
    {"begin": 0x70, "length": 1, "keep": 10},  # BBU Cell Voltage 4
    {"begin": 0x71, "length": 1, "keep": 10},  # BBU Cell Voltage 5
    {"begin": 0x72, "length": 1, "keep": 10},  # BBU Cell Voltage 6
    {"begin": 0x73, "length": 1, "keep": 10},  # BBU Cell Voltage 7
    {"begin": 0x74, "length": 1, "keep": 10},  # BBU Cell Voltage 8
    {"begin": 0x75, "length": 1, "keep": 10},  # BBU Cell Voltage 9
    {"begin": 0x76, "length": 1, "keep": 10},  # BBU Cell Voltage 10
    {"begin": 0x77, "length": 1, "keep": 10},  # BBU Cell Voltage 11
    {"begin": 0x78, "length": 1, "keep": 10},  # BBU Cell Voltage 12
    {"begin": 0x79, "length": 1, "keep": 10},  # BBU Cell Voltage 13
    {"begin": 0x7A, "length": 1, "keep": 10},  # BBU Temp 1
    {"begin": 0x7B, "length": 1, "keep": 10},  # BBU Temp 2
    {"begin": 0x7C, "length": 1, "keep": 10},  # BBU Temp 3
    {"begin": 0x7D, "length": 1, "keep": 10},  # BBU Temp 4
    {"begin": 0x7E, "length": 1, "keep": 10},  # BBU Relative State of Charge
    {"begin": 0x7F, "length": 1, "keep": 10},  # BBU Absolute State of Charge
    {
        "begin": 0x80,  # Input VAC
        "length": 1,
        "keep": 10,
        "flags": 0x4600,  # 2'comp N=6
    },
    {"begin": 0x81, "length": 1, "keep": 10},  # BBU Battery Voltage
    {
        "begin": 0x82,  # Input Current AC
        "length": 1,
        "keep": 10,
        "flags": 0x4A00,  # 2'comp N=10
    },
    {"begin": 0x83, "length": 1, "keep": 10},  # BBU Battery Current
    {
        "begin": 0x84,  # Battery Voltage
        "length": 1,
        "keep": 10,
        "flags": 0x4900,  # 2'comp N=9
    },
    {"begin": 0x85, "length": 1, "keep": 10},  # BBU Average Current
    {
        "begin": 0x86,  # Battery Current Output
        "length": 1,
        "flags": 0x4800,  # 2'comp N=8
    },
    {"begin": 0x87, "length": 1, "keep": 10},  # BBU Remaining Capacity
    {
        "begin": 0x88,  # Battery Current Input
        "length": 1,
        "flags": 0x4C00,  # 2'comp N=12
    },
    {"begin": 0x89, "length": 1, "keep": 10},  # BBU Full Charge Capacity
    {
        "begin": 0x8A,  # Output Voltage (main converter)
        "length": 1,
        "keep": 10,
        "flags": 0x4B00,  # 2'comp N=11
    },
    {"begin": 0x8B, "length": 1, "keep": 10},  # BBU Run Time to Empty
    {
        "begin": 0x8C,  # Output Current (main converter)
        "length": 1,
        "keep": 10,
        "flags": 0x4600,  # 2'comp N=6
    },
    {"begin": 0x8D, "length": 1, "keep": 10},  # BBU Average Time to Empty
    {
        "begin": 0x8E,  # IT Load Voltage Output
        "length": 1,
        "flags": 0x4900,  # 2'comp N=9
    },
    {"begin": 0x8F, "length": 1},  # BBU Charging Current
    {
        "begin": 0x90,  # IT Load Current Output
        "length": 1,
        "flags": 0x4C00,  # 2'comp N=12
    },
    {"begin": 0x91, "length": 1, "keep": 10},  # BBU Charging Voltage
    {"begin": 0x92, "length": 1, "flags": 0x4600},  # Bulk Cap Voltage  # 2'comp N=6
    {"begin": 0x93, "length": 1, "keep": 10},  # BBU Cycle Count
    {
        "begin": 0x94,  # Input Power
        "length": 1,
        "keep": 10,
        "flags": 0x4300,  # 2'comp N=3
    },
    {"begin": 0x95, "length": 1},  # BBU Design Capacity
    {
        "begin": 0x96,  # Output Power
        "length": 1,
        "keep": 10,
        "flags": 0x4300,  # 2'comp N=3
    },
    {"begin": 0x97, "length": 1},  # BBU Design Voltage
    {"begin": 0x98, "length": 1, "flags": 0x2000},  # RPM Fan 0  # Decimal
    {"begin": 0x99, "length": 1},  # BBU At Rate
    {"begin": 0x9A, "length": 1, "flags": 0x2000},  # RPM Fan 1  # Decimal
    {"begin": 0x9B, "length": 1, "keep": 10},  # BBU At Rate Time to Full
    {
        "begin": 0x9C,  # BBU At Rate Time to Empty
        "length": 1,
        "keep": 10,
        "flags": 0x2000,  # Decimal
    },
    {"begin": 0x9D, "length": 1, "keep": 10},  # BBU At Rate OK
    {"begin": 0x9E, "length": 1, "flags": 0x4700},  # Temp 0  # 2'comp N=7
    {"begin": 0x9F, "length": 1},  # BBU Temp
    {"begin": 0xA0, "length": 1, "flags": 0x4700},  # Temp 1  # 2'comp N=7
    {"begin": 0xA1, "length": 1},  # BBU Max Error
    {
        "begin": 0xD0,  # General Alarm Status Register
        "flags": 0x1000,  # Bitmap
        "length": 1,
    },
    {
        "begin": 0xD1,  # PFC Alarm Status Register
        "flags": 0x1000,  # Bitmap
        "length": 1,
    },
    {
        "begin": 0xD2,  # LLC Alarm Status Register
        "flags": 0x1000,  # Bitmap
        "length": 1,
    },
    {
        "begin": 0xD3,  # Current Feed Alarm Status Register
        "flags": 0x1000,  # Bitmap
        "length": 1,
    },
    {
        "begin": 0xD4,  # Auxiliary Alarm Status Register
        "flags": 0x1000,  # Bitmap
        "length": 1,
    },
    {
        "begin": 0xD5,  # Battery Charger Alarm Status Register
        "flags": 0x1000,  # Bitmap
        "length": 1,
    },
    {
        "begin": 0xD7,  # Temperature Alarm Status Register
        "flags": 0x1000,  # Bitmap
        "length": 1,
    },
    {
        "begin": 0xD8,  # Fan Alarm Status Register
        "flags": 0x1000,  # Bitmap
        "length": 1,
    },
    {
        "begin": 0xD9,  # Communication Alarm Status Register
        "flags": 0x1000,  # Bitmap
        "length": 1,
    },
    {"begin": 0x106, "length": 1},  # BBU Specification Info
    {"begin": 0x107, "length": 1},  # BBU Manufacturer Date
    {"begin": 0x108, "length": 1},  # BBU Serial Number
    {"begin": 0x109, "length": 2},  # BBU Device Chemistry
    {"begin": 0x10B, "length": 2},  # BBU Manufacturer Data
    {"begin": 0x10D, "length": 8},  # BBU Manufacturer Name
    {"begin": 0x115, "length": 8},  # BBU Device Name
    {"begin": 0x11D, "length": 4},  # FB Battery Status
    {"begin": 0x121, "length": 1},  # SoH results
]

def main():
    configure_rackmond(reglist, verify_configure=True)

if __name__ == "__main__":
    main()


import glfw
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np

gCamAng = 0.

def myLookAt(eye,at,up):
    w = (eye-at)/np.sqrt(np.dot(eye-at,eye-at))
    u = np.cross(up, w)/np.sqrt(np.dot(np.cross(up, w), np.cross(up,w)))
    v = np.cross(w, u)

    M= np.array([[u[0], u[1], u[2], -np.dot(u, eye)],
                 [v[0], v[1], v[2], np.dot(-v, eye)],
                 [w[0], w[1], w[2], np.dot(-w, eye)],
                 [0, 0, 0, 1]])

    glMultMatrixf(M.T)

def myOrtho(l, r, b, t, n, f):
    Morth = np.array([[2/(r-l),0 , 0,-(r+l)/(r-l)],
                     [ 0, 2/(t-b), 0, -(t+b)/(t-b)],
                     [ 0, 0, -2/(f-n), -(f+n)/(f-n)],
                     [ 0, 0, 0, 1]])

def render(camAng):
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glEnable(GL_DEPTH_TEST)
# set the current matrix to the identity matrix
    glLoadIdentity()
# use orthogonal projection (right-multiply the current matrix by "projection" matrix - we'll see details later)

    myOrtho(left, right, bottom, top, near, far)
    #glOrtho(-1,1, -1,1, -10,10)

# rotate "camera" position (right-multiply the current matrix by viewing matrix)
# try to change parameters

    eye = np.array([1*np.sin(camAng),.5,1*np.cos(camAng)])
    up = np.array([0, 1, 0])
    at = np.array([0, 0, 0])
    myLookAt(eye, at, up)

    myOrtho()

# draw coordinates
    glBegin(GL_LINES)
    glColor3ub(255, 0, 0)
    glVertex3fv(np.array([0.,0.,0.]))
    glVertex3fv(np.array([1.,0.,0.]))
    glColor3ub(0, 255, 0)
    glVertex3fv(np.array([0.,0.,0.]))
    glVertex3fv(np.array([0.,1.,0.]))
    glColor3ub(0, 0, 255)
    glVertex3fv(np.array([0.,0.,0]))
    glVertex3fv(np.array([0.,0.,1.]))
    glEnd()

def key_callback(window, key, scancode, action, mods):
    global gCamAng
# rotate the camera when 1 or 3 key is pressed or repeated
    if action==glfw.PRESS or action==glfw.REPEAT:
        if key==glfw.KEY_1:
            gCamAng += np.radians(-10)
        elif key==glfw.KEY_3:
            gCamAng += np.radians(10)

def main():
    if not glfw.init():
        return
    window = glfw.create_window(640,640,'Lecture8', None,None)
    if not window:
        glfw.terminate()
        return
    glfw.make_context_current(window)
    glfw.set_key_callback(window, key_callback)
    while not glfw.window_should_close(window):
        glfw.poll_events()
        render(gCamAng)
        glfw.swap_buffers(window)
    glfw.terminate()
if __name__ == "__main__":
    main()


def gridRound( pos, w, h, roundToTopLeft=True, trueRounding=False ):
    """gridRound( pos, w, h, roundToTopLeft=True )\n""" \
    """This function rounds a given pos variable to the nearest lower or upper multiples \n""" \
    """ of w and h in their respective directions. roundToTopLeft=True means it rounds towards the topleft. \n""" \
    """ trueRounding means round to the closest corner, not topleft or bottomright."""

    xRemainder, yRemainder = pos[0]%w, pos[1]%h

    newPosition = [ pos[0] - xRemainder, pos[1] - yRemainder ]

    if trueRounding:
        if float(xRemainder)/w > 0.5:
            newPosition[0] += w
        if float(yRemainder)/h > 0.5:
            newPosition[1] += h
    elif not roundToTopLeft:
        newPosition[0] += w
        newPosition[1] += h

    return newPosition


import sys
from random import shuffle

with open("all_eqs.txt") as f:
  eq = f.readlines()

with open("all_text.txt") as f:
  txt = f.readlines()

dat = list(range(len(txt)))
shuffle(dat)

valeq = open("val.eq",'w')
valtxt = open("val.txt",'w')
for i in dat[:200]:
  valeq.write(eq[i])
  valtxt.write(txt[i])

valeq = open("test.eq",'w')
valtxt = open("test.txt",'w')
for i in dat[200:400]:
  valeq.write(eq[i])
  valtxt.write(txt[i])

valeq = open("train.eq",'w')
valtxt = open("train.txt",'w')
for i in dat[400:]:
  valeq.write(eq[i])
  valtxt.write(txt[i])


import asyncio
import async_timeout
import aiohttp

class DiscordAttachmentHandler:
    def __init__(self):
        self.loop = asyncio.get_event_loop()

    @staticmethod
    async def fetch_json(session, url):
        async with async_timeout.timeout(10):
            async with session.get(url) as response:
                return await response.json()

    async def get_content(self, url):
        async with aiohttp.ClientSession() as session:
            return await self.fetch_json(session, url)

    def get_ship_list(self, file_url, logger):
        try:
            return self.loop.run_until_complete(self.get_content(file_url))
        except asyncio.TimeoutError:
            logger.error("Could not download attachment. Asyncio timeout error.")


import subprocess
import py_compile
import os
from hashlib import md5
from datetime import datetime

from django.db import models
from django.conf import settings

from .constants import TYPES

def get_filename(instance, filename):
    now = datetime.now()
    base = now.strftime('utility_files/%Y/%m/%d')
    hash = md5(str(now).encode('utf-8'))
    return os.path.join(base, '{}_{}'.format(hash.hexdigest(), filename))

class Utility(models.Model):
    class Meta:
        verbose_name = 'verktyg'
        verbose_name_plural = 'verktyg'

    def __str__(self):
        return self.name

    name = models.CharField('namn', max_length=100)
    description = models.CharField('beskrivning', blank=True, null=True, max_length=100)
    type = models.CharField('typ', choices=TYPES, max_length=100)

class PythonUtility(Utility):
    class Meta:
        verbose_name = 'pythonverktyg'
        verbose_name_plural = verbose_name

    def execute(self):
        filename = self.code.name
        compiled_filename = filename + 'c'
        path = os.path.join(settings.MEDIA_ROOT, filename)
        compiled_path = os.path.join(settings.MEDIA_ROOT, compiled_filename)
        utility_root = os.path.join(settings.MEDIA_ROOT, 'utility_files')

        for root, dirs, files in os.walk(utility_root):
            if not compiled_filename in files:
                try:
                    # Append file at path to 'utility_files/base.py' and then compile the resulting file
                    py_compile.compile(path, cfile=compiled_path, doraise=True)
                except py_compile.PyCompileError:
                    return None
        return compiled_filename

    code = models.FileField('pythonkod', upload_to=get_filename, blank=True, null=True, help_text='En .py-fil med interpreterbar Pythonkod.')

class CommandLineUtility(Utility):
    class Meta:
        verbose_name = 'kommandoradsverktyg'
        verbose_name_plural = verbose_name

    def execute(self):
        cmd = self.command_line.split()
        output = None
        try:
            output = subprocess.check_output(
                cmd,
                stderr=subprocess.STDOUT,
                universal_newlines=True,
            )
        except subprocess.CalledProcessError as e:
            return {
                'success': False,
                'exit_status': e.returncode,
                'output': e.output,
            }

        return {
            'success': True,
            'exit_status': 0,
            'output': output,
        }

    command_line = models.CharField('kommandorad', max_length=100)


import boto3
import random
import json

topic = 'arn:aws:sns:us-east-1:511086078676:temps'
client = boto3.client('sns')

def lambda_handler(event, context):

    #id de la persona (editar con el id real)
    #id = '11223344'

    #simulador de aparato de medir la temperatura
    temp = random.uniform(36,40)
    print(temp)

    mensaje = {
        #'id' : id,
        'temp' : '%.2f' % round(temp, 2)
    }

    #publica mensaje en el topic
    response = client.publish(
        TopicArn = topic,
        Message = json.dumps(mensaje)
        )

    print(response)


import time
from pathlib import Path
import shutil
import torch
import torch.nn
from torch.utils.tensorboard import SummaryWriter
import torch.backends.cudnn as cudnn
from . import utils

class Bone:
    def __init__(self,
                 model,
                 datasets,
                 criterion,
                 optimizer,
                 scheduler=None,
                 scheduler_after_ep=True,
                 early_stop_epoch=None,
                 metric_fn=None,
                 metric_increase=False,
                 batch_size=8,
                 num_workers=4,
                 resume=False,
                 data_parallel=False,
                 seed=0,
                 weights_path='weights/best_model.pth',
                 log_dir='logs/experiment'):
        self.model = model
        self.criterion = criterion
        self.optimizer = optimizer
        self.scheduler = scheduler
        self.scheduler_after_ep = scheduler_after_ep
        self.early_stop_epoch = early_stop_epoch
        self.metric_fn = criterion if metric_fn is None else metric_fn
        self.metric_increase = metric_increase
        self.batch_size = batch_size
        self.num_workers = num_workers
        self.resume = resume
        self.data_parallel = data_parallel
        self.seed = seed
        self.weights_path = Path(weights_path)
        self.log_dir = Path(log_dir)
        self.epochs_count = 0
        self.logger = utils.get_logger()

        self.recreate_experiment_folders(from_scratch=False)
        utils.set_seed(seed)

        self.dataloaders = {  # TODO: automatically handel all in loop
            'train': torch.utils.data.DataLoader(datasets['train'],
                                                 batch_size=batch_size,
                                                 shuffle=True,
                                                 num_workers=num_workers),
            'val': torch.utils.data.DataLoader(datasets['val'],
                                               batch_size=batch_size,
                                               shuffle=False,
                                               num_workers=num_workers)
        }

        if self.resume:
            if not self.weights_path.exists():
                self.logger.error('Resume is not possible, no weights')
            else:
                self.logger.info(f'Resuming from {self.weights_path}')
                checkpoint = torch.load(self.weights_path)
                self.model.load_state_dict(checkpoint)
                # TODO: bug, move model to device

        self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
        if self.device == 'cuda' and data_parallel:
            self.model = torch.nn.DataParallel(self.model)

    def recreate_experiment_folders(self, from_scratch=False):
        if from_scratch:
            if self.weights_path.parent.exists():
                self.weights_path.unlink()
            if self.log_dir.exists():
                shutil.rmtree(self.log_dir)

        self.weights_path.parent.mkdir(exist_ok=True)
        self.log_dir.mkdir(parents=True, exist_ok=True)

        self.phase_writer = {
            'train': SummaryWriter(self.log_dir / 'train'),
            'val': SummaryWriter(self.log_dir / 'val')
        }

    def step(self, inputs, labels, phase):
            inputs = inputs.to(self.device)
            labels = labels.to(self.device)

            self.optimizer.zero_grad()
            with torch.set_grad_enabled(phase == 'train'):
                outputs = self.model(inputs)
                loss = self.criterion(outputs, labels)
                metric = self.metric_fn(outputs, labels)

                if phase == 'train':
                    loss.backward()
                    self.optimizer.step()

            return loss.cpu().data.numpy(), metric.cpu().data.numpy()

    def epoch(self, epoch_num, phase):
        running_loss = 0
        running_metric = 0
        pbar = utils.get_pbar(self.dataloaders[phase],
                              f'{phase} {epoch_num + 1}/{self.epochs_count}')

        if phase == 'val' and self.scheduler and not self.scheduler_after_ep:
            self.scheduler.step()

        for i, (inputs, labels) in enumerate(self.dataloaders[phase]):
            loss, metric = self.step(inputs, labels, phase)

            running_loss += loss * inputs.size(0)
            running_metric += metric * inputs.size(0)
            utils.update_pbar(pbar, loss, metric)

            step = epoch_num * len(self.dataloaders['train']) + i
            self.phase_writer[phase].add_scalar('batch/loss', loss,
                                                global_step=step)
            self.phase_writer[phase].add_scalar('batch/metric', metric,
                                                global_step=step)

        running_loss /= len(self.dataloaders[phase].dataset)
        running_metric /= len(self.dataloaders[phase].dataset)
        utils.update_pbar(pbar, running_loss, running_metric)
        pbar.close()

        self.phase_writer[phase].add_scalar('epoch/loss', running_loss,
                                            global_step=epoch_num)
        self.phase_writer[phase].add_scalar('epoch/metric', running_metric,
                                            global_step=epoch_num)

        if phase == 'val':
            if self.scheduler and self.scheduler_after_ep:
                self.scheduler.step(running_metric)
            lr = utils.get_lr(self.optimizer)
            self.phase_writer[phase].add_scalar('epoch/lr', lr,
                                                global_step=epoch_num)

        return running_loss, running_metric

    def fit(self, epochs_count, from_scratch=False):
        if from_scratch:
            self.recreate_experiment_folders(from_scratch)

        start_time = time.time()
        self.epochs_count = epochs_count
        epoch_without_improvement = 0
        best_metric = None

        def is_better(new_m, old_m, eps=1e-5):
            if old_m is None:
                return True
            return new_m > old_m + eps if self.metric_increase else \
                new_m < old_m - eps

        for epoch_num in range(epochs_count):
            for phase in ['train', 'val']:  # TODO: test phase
                if phase == 'train':
                    self.model.train()
                else:
                    self.model.eval()
                loss, metric = self.epoch(epoch_num, phase)

                if phase == 'val':
                    if is_better(metric, best_metric):
                        best_metric = metric
                        if self.data_parallel:
                            torch.save(self.model.module.state_dict(),
                                       self.weights_path)
                        else:
                            torch.save(self.model.state_dict(),
                                       self.weights_path)
                        epoch_without_improvement = 0
                        self.logger.debug('Val metric improved')
                    else:
                        epoch_without_improvement += 1
                        self.logger.debug(f'Val metric did not improve for '
                                          f'{epoch_without_improvement} epochs')

            if self.early_stop_epoch is not None and\
                    epoch_without_improvement == self.early_stop_epoch:
                self.logger.info('Early stopping')
                break

        time_elapsed = time.time() - start_time
        self.logger.info(f'Training complete in {time_elapsed/60:.0f}m'
                         f' {time_elapsed%60:.0f}s')
        self.logger.info(f'Best val metric: {best_metric:.4f}')


import numpy as np
import pickle as pkl
import networkx as nx
import scipy.sparse as sp
from scipy.sparse.linalg.eigen.arpack import eigsh
import sys
from scipy.sparse.linalg import norm as sparsenorm
from scipy.linalg import qr
# from sklearn.metrics import f1_score

def parse_index_file(filename):
    """Parse index file."""
    index = []
    for line in open(filename):
        index.append(int(line.strip()))
    return index

def sample_mask(idx, l):
    """Create mask."""
    mask = np.zeros(l)
    mask[idx] = 1
    return np.array(mask, dtype=np.bool)

#
# def calc_f1(y_true, y_pred):
#     y_true = np.argmax(y_true, axis=1)
#     y_pred = np.argmax(y_pred, axis=1)
#     return f1_score(y_true, y_pred, average="micro"), f1_score(y_true, y_pred, average="macro")
#

#
# def load_data(dataset_str):
#     """Load data."""
#     names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
#     objects = []
#     for i in range(len(names)):
#         with open("data/ind.{}.{}".format(dataset_str, names[i]), 'rb') as f:
#             if sys.version_info > (3, 0):
#                 objects.append(pkl.load(f, encoding='latin1'))
#             else:
#                 objects.append(pkl.load(f))
#
#     x, y, tx, ty, allx, ally, graph = tuple(objects)
#     test_idx_reorder = parse_index_file("data/ind.{}.test.index".format(dataset_str))
#     test_idx_range = np.sort(test_idx_reorder)
#
#     if dataset_str == 'citeseer':
#         # Fix citeseer dataset (there are some isolated nodes in the graph)
#         # Find isolated nodes, add them as zero-vecs into the right position
#         test_idx_range_full = range(min(test_idx_reorder), max(test_idx_reorder)+1)
#         tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1]))
#         tx_extended[test_idx_range-min(test_idx_range), :] = tx
#         tx = tx_extended
#         ty_extended = np.zeros((len(test_idx_range_full), y.shape[1]))
#         ty_extended[test_idx_range-min(test_idx_range), :] = ty
#         ty = ty_extended
#
#     features = sp.vstack((allx, tx)).tolil()
#     features[test_idx_reorder, :] = features[test_idx_range, :]
#     adj = nx.adjacency_matrix(nx.from_dict_of_lists(graph))
#
#     labels = np.vstack((ally, ty))
#     labels[test_idx_reorder, :] = labels[test_idx_range, :]
#
#     idx_test = test_idx_range.tolist()
#     idx_train = range(len(y))
#     idx_val = range(len(y), len(y)+500)
#
#     train_mask = sample_mask(idx_train, labels.shape[0])
#     val_mask = sample_mask(idx_val, labels.shape[0])
#     test_mask = sample_mask(idx_test, labels.shape[0])
#
#     y_train = np.zeros(labels.shape)
#     y_val = np.zeros(labels.shape)
#     y_test = np.zeros(labels.shape)
#     y_train[train_mask, :] = labels[train_mask, :]
#     y_val[val_mask, :] = labels[val_mask, :]
#     y_test[test_mask, :] = labels[test_mask, :]
#
#     return adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask
#

def load_data(dataset_str):
    """Load data."""
    names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
    objects = []
    for i in range(len(names)):
        with open("data/ind.{}.{}".format(dataset_str, names[i]), 'rb') as f:
            if sys.version_info > (3, 0):
                objects.append(pkl.load(f, encoding='latin1'))
            else:
                objects.append(pkl.load(f))

    x, y, tx, ty, allx, ally, graph = tuple(objects)
    test_idx_reorder = parse_index_file("data/ind.{}.test.index".format(dataset_str))
    test_idx_range = np.sort(test_idx_reorder)

    if dataset_str == 'citeseer':
        # Fix citeseer dataset (there are some isolated nodes in the graph)
        # Find isolated nodes, add them as zero-vecs into the right position
        test_idx_range_full = range(min(test_idx_reorder), max(test_idx_reorder)+1)
        tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1]))
        tx_extended[test_idx_range-min(test_idx_range), :] = tx
        tx = tx_extended
        ty_extended = np.zeros((len(test_idx_range_full), y.shape[1]))
        ty_extended[test_idx_range-min(test_idx_range), :] = ty
        ty = ty_extended

    features = sp.vstack((allx, tx)).tolil()
    features[test_idx_reorder, :] = features[test_idx_range, :]
    adj = nx.adjacency_matrix(nx.from_dict_of_lists(graph))

    labels = np.vstack((ally, ty))
    labels[test_idx_reorder, :] = labels[test_idx_range, :]

    idx_test = test_idx_range.tolist()
    idx_train = range(len(ally)-500)
    idx_val = range(len(ally)-500, len(ally))

    train_mask = sample_mask(idx_train, labels.shape[0])
    val_mask = sample_mask(idx_val, labels.shape[0])
    test_mask = sample_mask(idx_test, labels.shape[0])

    y_train = np.zeros(labels.shape)
    y_val = np.zeros(labels.shape)
    y_test = np.zeros(labels.shape)
    y_train[train_mask, :] = labels[train_mask, :]
    y_val[val_mask, :] = labels[val_mask, :]
    y_test[test_mask, :] = labels[test_mask, :]

    return adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask

def load_data_original(dataset_str):
    """Load data."""
    names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
    objects = []
    for i in range(len(names)):
        with open("data/ind.{}.{}".format(dataset_str, names[i]), 'rb') as f:
            if sys.version_info > (3, 0):
                objects.append(pkl.load(f, encoding='latin1'))
            else:
                objects.append(pkl.load(f))

    x, y, tx, ty, allx, ally, graph = tuple(objects)
    test_idx_reorder = parse_index_file("data/ind.{}.test.index".format(dataset_str))
    test_idx_range = np.sort(test_idx_reorder)

    if dataset_str == 'citeseer':
        # Fix citeseer dataset (there are some isolated nodes in the graph)
        # Find isolated nodes, add them as zero-vecs into the right position
        test_idx_range_full = range(min(test_idx_reorder), max(test_idx_reorder)+1)
        tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1]))
        tx_extended[test_idx_range-min(test_idx_range), :] = tx
        tx = tx_extended
        ty_extended = np.zeros((len(test_idx_range_full), y.shape[1]))
        ty_extended[test_idx_range-min(test_idx_range), :] = ty
        ty = ty_extended

    features = sp.vstack((allx, tx)).tolil()
    features[test_idx_reorder, :] = features[test_idx_range, :]
    adj = nx.adjacency_matrix(nx.from_dict_of_lists(graph))

    labels = np.vstack((ally, ty))
    labels[test_idx_reorder, :] = labels[test_idx_range, :]

    idx_test = test_idx_range.tolist()
    idx_train = range(len(y))
    idx_val = range(len(y), len(y)+500)

    train_mask = sample_mask(idx_train, labels.shape[0])
    val_mask = sample_mask(idx_val, labels.shape[0])
    test_mask = sample_mask(idx_test, labels.shape[0])

    y_train = np.zeros(labels.shape)
    y_val = np.zeros(labels.shape)
    y_test = np.zeros(labels.shape)
    y_train[train_mask, :] = labels[train_mask, :]
    y_val[val_mask, :] = labels[val_mask, :]
    y_test[test_mask, :] = labels[test_mask, :]

    return adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask

def sparse_to_tuple(sparse_mx):
    """Convert sparse matrix to tuple representation."""
    def to_tuple(mx):
        if not sp.isspmatrix_coo(mx):
            mx = mx.tocoo()
        coords = np.vstack((mx.row, mx.col)).transpose()
        values = mx.data
        shape = mx.shape
        return coords, values, shape

    if isinstance(sparse_mx, list):
        for i in range(len(sparse_mx)):
            sparse_mx[i] = to_tuple(sparse_mx[i])
    else:
        sparse_mx = to_tuple(sparse_mx)

    return sparse_mx

def nontuple_preprocess_features(features):
    """Row-normalize feature matrix and convert to tuple representation"""
    rowsum = np.array(features.sum(1))
    r_inv = np.power(rowsum, -1).flatten()
    r_inv[np.isinf(r_inv)] = 0.
    r_mat_inv = sp.diags(r_inv)
    features = r_mat_inv.dot(features)
    return features

def preprocess_features(features):
    """Row-normalize feature matrix and convert to tuple representation"""
    rowsum = np.array(features.sum(1))
    r_inv = np.power(rowsum, -1).flatten()
    r_inv[np.isinf(r_inv)] = 0.
    r_mat_inv = sp.diags(r_inv)
    features = r_mat_inv.dot(features)
    return sparse_to_tuple(features)

def normalize_adj(adj):
    """Symmetrically normalize adjacency matrix."""
    adj = sp.coo_matrix(adj)
    rowsum = np.array(adj.sum(1))
    d_inv_sqrt = np.power(rowsum, -0.5).flatten()
    d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
    d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
    return adj.dot(d_mat_inv_sqrt).transpose().dot(d_mat_inv_sqrt).tocoo()

def nontuple_preprocess_adj(adj):
    adj_normalized = normalize_adj(sp.eye(adj.shape[0]) + adj)
    # adj_normalized = sp.eye(adj.shape[0]) + normalize_adj(adj)
    return adj_normalized.tocsr()

def column_prop(adj):
    column_norm = sparsenorm(adj, axis=0)
    # column_norm = pow(sparsenorm(adj, axis=0),2)
    norm_sum = sum(column_norm)
    return column_norm/norm_sum

def mix_prop(adj, features, sparseinputs=False):
    adj_column_norm = sparsenorm(adj, axis=0)
    if sparseinputs:
        features_row_norm = sparsenorm(features, axis=1)
    else:
        features_row_norm = np.linalg.norm(features, axis=1)
    mix_norm = adj_column_norm*features_row_norm

    norm_sum = sum(mix_norm)
    return mix_norm / norm_sum

def preprocess_adj(adj):
    """Preprocessing of adjacency matrix for simple GCN model and conversion to tuple representation."""
    # adj_appr = np.array(sp.csr_matrix.todense(adj))
    # # adj_appr = dense_lanczos(adj_appr, 100)
    # adj_appr = dense_RandomSVD(adj_appr, 100)
    # if adj_appr.sum(1).min()<0:
    #     adj_appr = adj_appr- (adj_appr.sum(1).min()-0.5)*sp.eye(adj_appr.shape[0])
    # else:
    #     adj_appr = adj_appr + sp.eye(adj_appr.shape[0])
    # adj_normalized = normalize_adj(adj_appr)

    # adj_normalized = normalize_adj(adj+sp.eye(adj.shape[0]))
    # adj_appr = np.array(sp.coo_matrix.todense(adj_normalized))
    # # adj_normalized = dense_RandomSVD(adj_appr,100)
    # adj_normalized = dense_lanczos(adj_appr, 100)

    adj_normalized = normalize_adj(sp.eye(adj.shape[0]) + adj)
    # adj_normalized = sp.eye(adj.shape[0]) + normalize_adj(adj)
    return sparse_to_tuple(adj_normalized)

from lanczos import lanczos
def dense_lanczos(A,K):
    q = np.random.randn(A.shape[0], )
    Q, sigma = lanczos(A, K, q)
    A2 = np.dot(Q[:,:K], np.dot(sigma[:K,:K], Q[:,:K].T))
    return sp.csr_matrix(A2)

def sparse_lanczos(A,k):
    q = sp.random(A.shape[0],1)
    n = A.shape[0]
    Q = sp.lil_matrix(np.zeros((n,k+1)))
    A = sp.lil_matrix(A)

    Q[:,0] = q/sparsenorm(q)

    alpha = 0
    beta = 0

    for i in range(k):
      if i == 0:
        q = A*Q[:,i]
      else:
        q = A*Q[:,i] - beta*Q[:,i-1]
      alpha = q.T*Q[:,i]
      q = q - Q[:,i]*alpha
      q = q - Q[:,:i]*Q[:,:i].T*q # full reorthogonalization
      beta = sparsenorm(q)
      Q[:,i+1] = q/beta
      print(i)

    Q = Q[:,:k]

    Sigma = Q.T*A*Q
    A2 = Q[:,:k]*Sigma[:k,:k]*Q[:,:k].T
    return A2
    # return Q, Sigma

def dense_RandomSVD(A,K):
    G = np.random.randn(A.shape[0],K)
    B = np.dot(A,G)
    Q,R =qr(B,mode='economic')
    M = np.dot(Q, np.dot(Q.T, A))
    return sp.csr_matrix(M)

def construct_feed_dict(features, support, labels, labels_mask, placeholders):
    """Construct feed dictionary."""
    feed_dict = dict()
    feed_dict.update({placeholders['labels']: labels})
    feed_dict.update({placeholders['labels_mask']: labels_mask})
    feed_dict.update({placeholders['features']: features})
    feed_dict.update({placeholders['support'][i]: support[i] for i in range(len(support))})
    feed_dict.update({placeholders['num_features_nonzero']: features[1].shape})
    return feed_dict

def chebyshev_polynomials(adj, k):
    """Calculate Chebyshev polynomials up to order k. Return a list of sparse matrices (tuple representation)."""
    print("Calculating Chebyshev polynomials up to order {}...".format(k))

    adj_normalized = normalize_adj(adj)
    laplacian = sp.eye(adj.shape[0]) - adj_normalized
    largest_eigval, _ = eigsh(laplacian, 1, which='LM')
    scaled_laplacian = (2. / largest_eigval[0]) * laplacian - sp.eye(adj.shape[0])

    t_k = list()
    t_k.append(sp.eye(adj.shape[0]))
    t_k.append(scaled_laplacian)

    def chebyshev_recurrence(t_k_minus_one, t_k_minus_two, scaled_lap):
        s_lap = sp.csr_matrix(scaled_lap, copy=True)
        return 2 * s_lap.dot(t_k_minus_one) - t_k_minus_two

    for i in range(2, k+1):
        t_k.append(chebyshev_recurrence(t_k[-1], t_k[-2], scaled_laplacian))

    return sparse_to_tuple(t_k)


"""
多头注意力机制:
    每个头开始从词义层面分割输出的张量，也就是每个头都想获得一组Q，K，V进行注意力机制的计算，
    但是句子中的每个词的表示只获得一部分，
    也就是只分割了最后一维的词嵌入向量. 这就是所谓的多头.
    将每个头的获得的输入送到注意力机制中, 就形成了多头注意力机制
多头注意力机制的作用:
    这种结构设计能让每个注意力机制去优化每个词汇的不同特征部分，从而均衡同一种注意力机制可能产生的偏差，
    让词义拥有来自更多元的表达，从而提升模型效果
"""
import copy
import torch
import math
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

class MultiHeadAttention(nn.Module):

    """在类的初始化时, 会传入三个参数，head代表头数，embedding_dim代表词嵌入的维度，
       dropout代表进行dropout操作时置0比率，默认是0.1."""

    def __init__(self, head, embedding_dim, dropout=0.1):
        super(MultiHeadAttention, self).__init__()

        # 在函数中，首先使用了一个测试中常用的assert语句，判断h是否能被d_model整除，
        # 这是因为我们之后要给每个头分配等量的词特征.也就是embedding_dim/head个.
        assert embedding_dim % head == 0

        # 得到每个头获得的分割词向量维度d_k
        self.d_k= embedding_dim // head

        self.head=head

        # 然后获得线性层对象，通过nn的Linear实例化，它的内部变换矩阵是embedding_dim x embedding_dim，然后使用clones函数克隆四个，
        # 为什么是四个呢，这是因为在多头注意力中，Q，K，V各需要一个，最后拼接的矩阵还需要一个，因此一共是四个.
        self.linears=self.clones(nn.Linear(embedding_dim,embedding_dim),4)

        self.attn=None

        self.dropout=nn.Dropout(p=dropout)

    # 首先需要定义克隆函数, 因为在多头注意力机制的实现中, 用到多个结构相同的线性层.
    # 我们将使用clone函数将他们一同初始化在一个网络层列表对象中. 之后的结构中也会用到该函数.
    """用于生成相同网络层的克隆函数, 它的参数module表示要克隆的目标网络层, N代表需要克隆的数量"""

    def clones(self,module, N):
        return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])

    """生成向后遮掩的掩码张量, 参数size是掩码张量最后两个维度的大小, 它的最后两维形成一个方阵"""
    def subsequentMask(self,size):
        # 在函数中, 首先定义掩码张量的形状
        attnShape=(1,size,size)
        # 然后使用np.ones方法向这个形状中添加1元素,形成上三角阵, 最后为了节约空间,
        # 再使其中的数据类型变为无符号8位整形unit8
        subsequent_mask=np.triu(np.ones(attnShape),k=1).astype('uint8')
        # 最后将numpy类型转化为torch中的tensor, 内部做一个1 - 的操作,
        # 在这个其实是做了一个三角阵的反转, subsequent_mask中的每个元素都会被1减,
        # 如果是0, subsequent_mask中的该位置由0变成1
        # 如果是1, subsequent_mask中的该位置由1变成0
        return torch.from_numpy(1-subsequent_mask)

    """注意力计算规则"""
    def attention(self,query,key,value,mask=None,dropout=None):
        # 在函数中, 首先取query的最后一维的大小, 一般情况下就等同于我们的词嵌入维度, 命名为d_k
        d_k = query.size(-1)
        # 按照注意力公式, 将query与key的转置相乘, 这里面key是将最后两个维度进行转置, 再除以缩放系数根号下d_k, 这种计算方法也称为缩放点积注意力计算.
        # 得到注意力得分张量scores

        scores=torch.matmul(query,key.transpose(-2,-1)) / math.sqrt(d_k)  # 最后两个维度做矩阵乘积
        # print("scores before masked: ", scores,scores.shape)
        # 接着判断是否使用掩码张量
        if mask is not None:
        # 使用tensor的masked_fill方法, 将掩码张量和scores张量每个位置一一比较, 如果掩码张量处为0
        # 则对应的scores张量用-1e9这个值来替换
            scores=scores.masked_fill(mask==0,-1e9)
        # print("scores after masked: ",scores,scores.shape)
        # 对scores的最后一维进行softmax操作, 使用F.softmax方法, 第一个参数是softmax对象, 第二个是目标维度.
        # 这样获得最终的注意力张量
        p_attn=F.softmax(scores,dim=-1)

        # 判断是否使用dropout进行随机置0
        if dropout is not None:
            p_attn=dropout(p_attn)

        # print("value : ",value,value.shape)
        # 最后, 根据公式将p_attn与value张量相乘获得最终的query注意力表示, 同时返回注意力张量
        return torch.matmul(p_attn,value),p_attn

    """前向逻辑函数, 它的输入参数有四个，前三个就是注意力机制需要的Q, K, V，
       最后一个是注意力机制中可能需要的mask掩码张量，默认是None. """
    def forward(self,query,key,value,mask=None):

        if mask is not None:
            mask=mask.unsqueeze(0)

        # 接着，我们获得一个batch_size的变量，他是query尺寸的第1个数字，代表有多少条样本.
        batch_size=query.size(0)

        # 之后就进入多头处理环节
        # 首先利用zip将输入QKV与三个线性层组到一起，然后使用for循环，将输入QKV分别传到线性层中，
        # 做完线性变换后，开始为每个头分割输入，这里使用view方法对线性变换的结果进行维度重塑，多加了一个维度h，代表头数，
        # 这样就意味着每个头可以获得一部分词特征组成的句子，其中的-1代表自适应维度，
        # 计算机会根据这种变换自动计算这里的值.然后对第二维和第三维进行转置操作，
        # 为了让代表句子长度维度和词向量维度能够相邻，这样注意力机制才能找到词义与句子位置的关系，
        # 从attention函数中可以看到，利用的是原始输入的倒数第一和第二维.这样我们就得到了每个头的输入.
        query,key,value=[model(x).view(batch_size,-1,self.head,self.d_k).transpose(1,2) for model,x in zip(self.linears,(query,key,value))]
        # print("query.shape before attention",query.shape)
        # 得到每个头的输入后，接下来就是将他们传入到attention中，
        # 这里直接调用我们之前实现的attention函数.同时也将mask和dropout传入其中.
        x,self.attn=self.attention(query,key,value,mask,dropout=self.dropout)

        # 通过多头注意力计算后，我们就得到了每个头计算结果组成的4维张量，我们需要将其转换为输入的形状以方便后续的计算，
        # 因此这里开始进行第一步处理环节的逆操作，先对第二和第三维进行转置，然后使用contiguous方法，
        # 这个方法的作用就是能够让转置后的张量应用view方法，否则将无法直接使用，
        # 所以，下一步就是使用view重塑形状，变成和输入形状相同.
        # print("x shape",x.shape)
        x=x.transpose(1,2).contiguous().view(batch_size,-1,self.head*self.d_k)
        # print("x shape",x.shape)  # 这一步，x维度回到最初的

        # 最后使用线性层列表中的最后一个线性层对输入进行线性变换得到最终的多头注意力结构的输出.
        return self.linears[-1](x)


from flask import Flask, render_template, request
import os
from Prediction import deep_ocr, easy_ocr
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField
from wtforms.validators import InputRequired, Email, Length

# webserver gateway interface
app = Flask(__name__)
Bootstrap(app)

BASIC_PATH = os.getcwd()
UPLOAD_PATH = os.path.join(BASIC_PATH, 'static/upload/')

@app.route('/', methods=['POST', 'GET'])
def index():
    if request.method == 'POST':
        # get the uploaded file
        uploaded_file = request.files['image']
        # get the name of the uploaded file
        file_name = uploaded_file.filename
        # create the path for saving the uploaded file
        save_path = os.path.join(UPLOAD_PATH, file_name)
        # saving the uploaded file
        uploaded_file.save(save_path)
        print(file_name, 'was uploaded successfully!')
        #plate_number = deep_ocr(save_path, file_name)
        plate_number = easy_ocr(save_path, file_name)
        #print(plate_number)
        return render_template('index.html', upload=True, uploaded_image=file_name, text=plate_number)
    return render_template('index.html', upload=False)

if __name__ == "__main__":
    app.run(debug=True)


'''
Created on Apr 21, 2014

@author: Borja
'''

import os.path

import xlrd

import __data__

class XslReader(object):

    def __init__(self):
        if not os.path.exists(__data__.path()):
            os.makedirs(__data__.path())

        self._data_path = __data__.path();

    def load_indicator_sheet(self, file_name):
        inpath = os.path.join(self._data_path, os.path.basename(file_name))

        workbook = xlrd.open_workbook(inpath)
        worksheet = workbook.sheet_by_name("Indicators")

        data_dictionary = {}

        for curr_col in range (0, worksheet.ncols):
            field_name = worksheet.cell_value(0, curr_col).decode("UTF-8")
            data_dictionary[field_name] = worksheet.cell_value(1, curr_col);

        return data_dictionary

    def load_organization_sheet(self, file_name):
        inpath = os.path.join(self._data_path, os.path.basename(file_name))

        workbook = xlrd.open_workbook(inpath)
        worksheet = workbook.sheet_by_name("Organization")

        data_dictionary = {}

        data_dictionary["Name"] = worksheet.cell_value(1, 1).decode("UTF-8")
        data_dictionary["Description_EN"] = worksheet.cell_value(2, 1).decode("UTF-8")
        data_dictionary["Description_ES"] = worksheet.cell_value(3, 1).decode("UTF-8")
        data_dictionary["Description_FR"] = worksheet.cell_value(4, 1).decode("UTF-8")
        data_dictionary["URL"] = worksheet.cell_value(5, 1).decode("UTF-8")
        data_dictionary["Logo"] = worksheet.cell_value(6, 1).decode("UTF-8")

        data_dictionary["License_Name"] = worksheet.cell_value(9, 1).decode("UTF-8")
        data_dictionary["License_Description"] = worksheet.cell_value(10, 1).decode("UTF-8")
        data_dictionary["License_Republish"] = worksheet.cell_value(11, 1)
        data_dictionary["License_URL"] = worksheet.cell_value(12, 1).decode("UTF-8")

        return data_dictionary

    def load_xsl(self, file_name):
        inpath = os.path.join(self._data_path, os.path.basename(file_name))

        workbook = xlrd.open_workbook(inpath)
        worksheet = workbook.sheet_by_name("Values")

        data_matrix = [[0 for x in xrange(worksheet.ncols)] for x in xrange(worksheet.nrows)]

        for curr_row in range (0, worksheet.nrows):
            for curr_col in range (0, worksheet.ncols):
                #print "%s,%s   ---- %s" %(curr_row, curr_col, worksheet.cell_value(curr_row, curr_col))
                if worksheet.cell_type(curr_row, curr_col) == 1:  # text cell
                    data_matrix[curr_row][curr_col] = worksheet.cell_value(curr_row, curr_col).encode("UTF-8");
                else:
                    data_matrix[curr_row][curr_col] = worksheet.cell_value(curr_row, curr_col);

        return data_matrix


# -*- coding: utf-8 -*-
"""
Created on Mon Mar  2 19:36:10 2020

@author: lnajt
"""

# -*- coding: utf-8 -*-
"""
Created on Sun Jul 15 11:57:08 2018

@author: MGGG

"""

import networkx as nx
import random
import numpy as np
##############

'''Wilsons Algorithm'''

def random_spanning_tree_wilson(graph):
    '''The David Wilson random spanning tree algorithm'''
    tree_edges = []
    root = random.choice(list(graph.nodes()))
    hitting_set = set ( [ root])
    allowable_set = set(graph.nodes()).difference(hitting_set)
    len_graph = len(graph)
    len_hitting_set = 1
    while len_hitting_set < len_graph:
        start_node = random.choice(list(allowable_set))
        trip = random_walk_until_hit(graph, start_node, hitting_set)
        new_branch, branch_length = loop_erasure(trip)
        for i in range(branch_length - 1):
            tree_edges.append( [ new_branch[i], new_branch[i + 1]])
        for v in new_branch[:-1]:
            hitting_set.add(v)
            len_hitting_set += 1
            allowable_set.remove(v)
    tree = nx.DiGraph()

    for node in graph.nodes:
        node_attributes = list(graph.nodes[node].keys())
        tree.add_node(node)
        for attr in node_attributes:
            tree.nodes[node][attr] = graph.nodes[node][attr]
    tree.add_edges_from(tree_edges)
    return tree

def simple_random_walk_variable_length(graph,node, walk_length):
    '''does a random walk of length walk_length'''
    wet = set([node])
    trip = [node]
    while len(wet) < walk_length:
        next_step = random.choice(list(graph.neighbors(node)))
        wet.add(next_step)
        trip.append(next_step)
        node = next_step
    return trip, wet

def forward_tree_variable_length(graph,node, walk_length):
    '''builds the forward tree in Broders algorithm, using a walk of length
    walk_length'''

    walk, wet = simple_random_walk_variable_length(graph, node, walk_length)
    edges = []
    for vertex in list(wet):
        if (vertex != walk[0]):
            first_occurance = walk.index(vertex)
            edges.append( [walk[first_occurance], walk[first_occurance-1]])
    return edges, wet

def random_tree_variable_length(graph, walk_length):
    '''runs Broders algorithm to produce a tree of length walk_length'''
    tree_edges, wet = forward_tree_variable_length(graph, random.choice(list(graph.nodes())), walk_length)
    tree = nx.DiGraph()
    for node in list(wet):
        tree.add_node(node)

    tree.add_edges_from(tree_edges)
    return tree

def random_spanning_tree_wilson_with_starting(graph, starting_tree):
    #The David Wilson random spanning tree algorithm
    tree_edges = list(starting_tree.edges())
    hitting_set = set(starting_tree.nodes())
    allowable_set = set(graph.nodes()).difference(hitting_set)
    len_graph = len(graph)
    len_hitting_set = len(hitting_set)
    while len_hitting_set < len_graph:
        start_node = random.choice(list(allowable_set))
        trip = random_walk_until_hit(graph, start_node, hitting_set)
        new_branch, branch_length = loop_erasure(trip)
        #print(branch_length)
        for i in range(branch_length - 1):
            tree_edges.append( [ new_branch[i], new_branch[i + 1]])
        for v in new_branch[:-1]:
            hitting_set.add(v)
            len_hitting_set += 1
            allowable_set.remove(v)
    tree = nx.DiGraph()

    tree.add_edges_from(tree_edges)
    return tree

def random_walk_until_hit(graph, start_node, hitting_set):
    '''Does a random walk from start_node until it hits the hitting_set

    :graph: input graph
    :start_node: the node taht the graph starts at
    :hitting_set: the set to stop at, i.e. the tree we are building up

    '''

    current_node = start_node
    trip = [current_node]
    while current_node not in hitting_set:
        current_node = random.choice(list(graph.neighbors(current_node)))
        trip.append(current_node)
    return trip

def loop_erasure(trip):
    '''erases loops from a trip

    :trip: input of node names...
    '''
    n = len(trip)
    loop_erased_walk_indices = []
    last_occurance = n - trip[::-1].index(trip[0]) - 1
    loop_erased_walk_indices.append(last_occurance)
    branch_length = 0
    while trip[loop_erased_walk_indices[-1]] != trip[-1]:
        last_occurance = n -  trip[::-1].index(trip[loop_erased_walk_indices[-1]])  -1
        loop_erased_walk_indices.append(last_occurance + 1)
        branch_length += 1
    loop_erased_trip = [trip[i] for i in loop_erased_walk_indices]

    return (loop_erased_trip, branch_length + 1)

def statistics():
    samples = 100
    graph = nx.grid_graph([20,20])
    W_trees = []
    for i in range(samples):
        W_trees.append( nx.to_undirected(random_spanning_tree_wilson(graph)))


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

class BiDirectionalTreeGRU(nn.Module):
    def __init__(self, n_hidden=None, n_iters=1):
        super().__init__()
        self.n_hidden = n_hidden
        self.n_iters = n_iters

        self.down_root = nn.Linear(n_hidden, n_hidden)
        self.down_gru = AnyBatchGRUCell(n_hidden, n_hidden)
        self.up_leaf = nn.Linear(n_hidden, n_hidden)
        self.up_gru = AnyBatchGRUCell(n_hidden, n_hidden)

    def forward(self, up_embeddings, down_embeddings, levels, children, n_inners):
        for _ in range(self.n_iters):
            self.down_the_tree(states, up_embeddings, down_embeddings, levels, children, n_inners)
            self.up_the_tree(states, up_embeddings, down_embeddings, levels, children, n_inners)

    def up_the_tree(self, up_embeddings, down_embeddings, levels, children, n_inners):

        zero = torch.zeros(1).long(); one = torch.ones(1).long()
        if torch.cuda.is_available(): zero = zero.cuda(); one = one.cuda()

        for i, nodes in enumerate(levels[::-1]):
            j = n_levels - 1 - i
            try:
                inner = nodes[:n_inners[j]]
            except ValueError:
                inner = []
            try:
                outer = nodes[n_inners[j]:]
            except ValueError:
                outer = []

            if len(inner) > 0:
                try:
                    u_k_inners = u_k[:n_inners[j]]
                except ValueError:
                    u_k_inners = []
                try:
                    u_k_leaves = u_k[n_inners[j]:]
                except ValueError:
                    u_k_leaves = []

                h_L = embeddings[j+1][children[inner, zero]]
                h_R = embeddings[j+1][children[inner, one]]

                hhu = torch.cat((h_L, h_R, u_k_inners), 1)
                r = self.fc_r(hhu)
                if self.bn: r = self.bn_r(r)
                r = F.sigmoid(r)

                h_H = self.fc_h(r * hhu)
                if self.bn: h_H = self.bn_h(h_H)
                h_H = self.activation(h_H)

                z = self.fc_z(torch.cat((h_H, hhu), -1))
                if self.bn: z = self.bn_z(z)

                z_H = z[:, :n_hidden]               # new activation
                z_L = z[:, n_hidden:2*n_hidden]     # left activation
                z_R = z[:, 2*n_hidden:3*n_hidden]   # right activation
                z_N = z[:, 3*n_hidden:]             # local state
                z = torch.stack([z_H,z_L,z_R,z_N], 2)
                z = F.softmax(z)

                h = ((z[:, :, 0] * h_H) +
                     (z[:, :, 1] * h_L) +
                     (z[:, :, 2] * h_R) +
                     (z[:, :, 3] * u_k_inners))

                try:
                    embeddings.append(torch.cat((h, u_k_leaves), 0))
                except AttributeError:
                    embeddings.append(h)

            else:
                embeddings.append(u_k)

    def down_the_tree(self, up_embeddings, down_embeddings, levels, children, n_inners):

        down_embeddings[0] = F.tanh(self.down_root(up_embeddings[0])) # root nodes

        zero = torch.zeros(1).long(); one = torch.ones(1).long()
        if torch.cuda.is_available(): zero = zero.cuda(); one = one.cuda()

        for j, nodes in enumerate(levels[:-1]):

            down_parent = down_embeddings[j]
            up_L = up_embeddings[j+1][children[nodes, zero]]
            up_R = up_embeddings[j+1][children[nodes, one]]

            down_L = self.down_gru(up_L, down_parent)
            down_R = self.down_gru(up_R, down_parent)

            h = Variable(torch.zeros(down_L.size()[0] * 2, down_L.size()[1]))
            h[children[nodes, zero]] = down_L
            h[children[nodes, one]] = down_R
            down_embeddings[j] = h