import pickle
from tokenize import Floatnumber
import numpy as np
import re
# def SR():
#     pass
item_3d_path = "/home/ezxr/Documents/ibl_dataset_cvpr17_3852/training_image_ocr/items_3d.bin"
paddle_log = "logs_ibl/paddleClas_nn.txt"
hfnet_log = "logs_ibl/log_pyr_4.txt"
query_img_ocr_path = '/home/ezxr/Documents/ibl_dataset_cvpr17_3852/query_images_ocr/'
query_img_path = '/home/ezxr/Documents/ibl_dataset_cvpr17_3852/query_images_undistort/'
db_img_ocr_path = '/home/ezxr/Documents/ibl_dataset_cvpr17_3852/training_image_ocr/'
db_img_path = '/home/ezxr/Documents/ibl_dataset_cvpr17_3852/training_images_undistort/'
with open(item_3d_path, 'rb') as fb:
    items_3d = pickle.load(fb)
# ocr_output_path = '/home/ezxr/Documents/wxc/pic_ocr_flip/'
# print(len(items_3d))
dict_img_3d = {}
for id in range(0, len(items_3d)):
    # print(items_3d)

    item_3d = items_3d[id]
    if(len(item_3d) < 1):
        continue
    # print(item_3d[0])
    # break
    for img in item_3d[2]:
        img = re.sub('/./', '/', img)
        img = re.sub("//", "/", img)
        dict_img_3d[img] = id
for item in dict_img_3d:
    # print(item in dict_img_3d, item, dict_img_3d[item])
    pass
# print(len(dict_img_3d))
# with open(ocr_output_path + "dict_2dto3dindex.bin", "wb") as fp:
#     pickle.dump(dict_img_3d, fp)

def item_3d_diff(item1, item2):
    xyz1 = item1[0]
    xyz2 = item2[0]
    n1 = item1[1]
    n2 = item2[1]
    distance = np.linalg.norm(xyz1- xyz2)
    angle = np.dot(n1, n2)
    return angle, distance

def read_nn_log(log_path='logs/log_pyr_4.txt'):
    f = open(log_path, "r")
    lines = f.readlines()
    dict = {}
    for line in lines:
        if(line[0] == '*'):
            tempstr = ''
            seq = line.split(' ')
            img_name = tempstr.join(seq[1:-1])
            img_name = re.sub('_isFlip.*', '.jpg', img_name)
            img_name = re.sub('/./', '/', img_name)

            # print(img_name)
            dict[query_image].append((img_name, seq[-1][:-1]))
            pass
        else:
            tempstr = ''
            query_image = line.split(" ")[1:]
            query_image = tempstr.join(query_image[:])[:-1]
            query_image = re.sub('_isFlip.*', '.jpg', query_image)
            query_image = re.sub('/./', '/', query_image)

            # print(query_image)
            dict[query_image] = []
    # for item in dict:
    #     print(item)
    #     for nn in dict[item]:
    #         print("**", nn)
    return dict

dict_hfnet_nn = read_nn_log(hfnet_log)

dict_paddle_clas_nn = read_nn_log(paddle_log)

items_3d_now = set()
paddle_clas_item=[]
for query_img in dict_paddle_clas_nn:
    # print()
    # print(query_img)
    # print(dict_paddle_clas_nn[item])
    items_3d_now.clear()
    paddle_clas_item.clear()
    for nn_result, difference in dict_paddle_clas_nn[query_img]:
        if(float(difference) > 0.85 or nn_result not in dict_img_3d):
            # print("bad\t", nn_result, difference)
            continue
        # print("good\t", nn_result, difference, dict_img_3d[nn_result])
        paddle_clas_item.append(nn_result)
        items_3d_now.add(dict_img_3d[nn_result])
    # print("items_3d_now: ", items_3d_now)
    # for img in paddle_clas_item:
    #     print(img, dict_img_3d[img])
    items_3d_now_list = list(items_3d_now)

    # for item_3d_now in items_3d_now_list:
    #     print(items_3d[item_3d_now][0], items_3d[item_3d_now][1], item_3d_now)

    if(len(items_3d_now)==0):
        # print("Failed: no paddle_clas candidate, most likely candidate:")
        # for nn_result, difference in dict_hfnet_nn[query_img]:
        #     if(nn_result not in dict_img_3d):
        #         continue
        #     print("**", nn_result, dict_img_3d[nn_result])
        #     break
        continue
    elif(len(items_3d_now)==1 and len(paddle_clas_item) > 3):
        # print("Success: no duplicated logo: ")
        for nn_result, difference in dict_paddle_clas_nn[query_img]:
            if(float(difference) > 0.85 or nn_result not in dict_img_3d):
                continue
            # print("**", nn_result, dict_img_3d[nn_result])
            print(query_img[:-5].replace(query_img_ocr_path, ""), nn_result[:-5].replace(db_img_ocr_path, ""))
        continue
    else:
        if(len(items_3d_now)<3 and len(paddle_clas_item) > 3):
            item1 = items_3d[items_3d_now_list[0]]
            item2 = items_3d[items_3d_now_list[1]]
            angle, distance = item_3d_diff(item1, item2)
            if(angle > 0.5 and distance < 10):
                # print("Success: Few duplicated logo:")
                for nn_result in paddle_clas_item:
                    # print("**", nn_result, dict_img_3d[nn_result])
                    print(query_img[:-5].replace(query_img_ocr_path, ""), nn_result[:-5].replace(db_img_ocr_path, ""))

            else:
                # print("Duplicated logos: ")
                for nn_result, difference in dict_hfnet_nn[query_img]:
                    if(nn_result not in dict_img_3d):
                        continue
                    if(dict_img_3d[nn_result] not in items_3d_now):
                        # print(dict_img_3d[nn_result])
                        continue
                    # print("**", nn_result, dict_img_3d[nn_result])
                    print(query_img[:-5].replace(query_img_ocr_path, ""), nn_result[:-5].replace(db_img_ocr_path, ""))

        else:
            # print("Duplicated logos: ")
            for nn_result, difference in dict_hfnet_nn[query_img]:
                # print(nn_result, nn_result not in dict_img_3d)
                if(nn_result not in dict_img_3d):
                    continue
                if(dict_img_3d[nn_result] not in items_3d_now):
                    # print(dict_img_3d[nn_result])
                    continue
                print(query_img[:-5].replace(query_img_ocr_path, ""), nn_result[:-5].replace(db_img_ocr_path, ""))
                # print("**", nn_result, dict_img_3d[nn_result])


#coding=utf-8

import logging

logging.basicConfig(level=logging.DEBUG, format="[%(asctime)s] %(name)s:%(levelname)s: %(message)s")

'''
从本文开始分享requests相关知识，及如何用requests进行接口测试。

requests号称：是唯一的一个非转基因的Python HTTP库，人类可以安全享用。

'''

'''
下面我们看看requests的功能特色：

Keep-Alive & 连接池
国际化域名和URL
带持久化Cookie的会话
浏览器式的SSL认证
内容自动解码
basic/Digest认证
key/value Cookie管理
自动解压
Unicode响应
HTTP/HTTPS代理支持
文件分块上传
流下载
连接超时
分块请求
支持.netrc
'''

import  requests

if __name__ == '__main__':
    r=requests.get('https://api.github.com')

    status_code=r.status_code

    headers=r.headers

    content_type=r.headers['Content-type']

    code=r.encoding

    json_data=r.json()

    print("状态码： ", status_code)
    print("返回头： ", headers)
    print("content-type： ", content_type)
    print("编码：", code)
    print("文本内容： ", type(r.text),r.text)
    print("json串内容: ", type(json_data),json_data)


#import shots and delta default values
from . import *

from qchess.quantum_chess import QChess

class QuantumTestEngine():
    def __init__(self):
        self.posible_bstates = []
        self.done = False

    def add_board_state(self, state, prob):
        self.posible_bstates.append({'state': state, 'prob': prob, 'count': 0})

    def set_action(self, action):
        self.action = action

    def set_board_factory(self, width, height, func):
        self.width = width
        self.height = height
        self.board_factory = func

    def run_engine(self, n):
        assert(self.action)
        assert(self.posible_bstates)
        assert(self.width > 0 and self.height > 0)
        assert(self.board_factory)

        self.n = n

        for i in range(n):
            qchess = QChess(self.width, self.height)
            self.board_factory(qchess)
            self.action(qchess)

            for bstate in self.posible_bstates:
                if bstate['state'] == qchess.get_simplified_matrix():
                    bstate['count'] += 1
                    break

        self.done = True

    def run_tests(self, test_case, places=None, delta=None):
        assert(self.done)

        if display_probabilities:
            print()

            #we display the probabilites in another loop to be able
            #to display all even if assert fails
            for bstate in self.posible_bstates:
                print('Obtained: {} Expected: {}'.format(round(bstate['count']/self.n, 2), bstate['prob']))

        for bstate in self.posible_bstates:
            test_case.assertAlmostEqual(bstate['count']/self.n, bstate['prob'], places=places, delta=delta)


from twilio.rest import Client
import paho.mqtt.client as mqtt
import time

def message_handler(client, userdata, message):
	print("Message received ", str(message.payload.decode("utf-8")))
	print("Message topic ", message.topic)
	print("Message QOS ", message.qos)
	print("Message retain flag ", message.retain)
	command = str(message.payload.decode("utf-8"))

	if command == "Turn on":
		send_sms()

def send_sms():
	account_sid = ""
	auth_token = ""

	client = Client(account_sid, auth_token)

	message = client.messages.create(
		to = "+",
		from_ = "+19389999211",
		body = "Alert - Intrusion detected at house!")

	print(message.sid)

subs = mqtt.Client("sms-subscriber")
print("Connecting to broker")
#subs.connect("127.0.0.1:1883")
subs.connect("127.0.0.1")
print("Subscribing to SMS topic")
subs.on_message = message_handler
subs.loop_start()
subs.subscribe("house/sms")
time.sleep(1000)


#coding: utf-8
from django.core.cache import cache
from django.utils.functional import wraps
from cache_utils.utils import _cache_key, _func_info, _func_type, sanitize_memcached_key
from django.conf import settings

try:
    timeout = settings.CACHES['default']['TIMEOUT']
except:
    timeout = 300

def cached(timeout=timeout, group=None):
    """ Caching decorator. Can be applied to function, method or classmethod.
    Supports bulk cache invalidation and invalidation for exact parameter
    set. Cache keys are human-readable because they are constructed from
    callable's full name and arguments and then sanitized to make
    memcached happy.

    It can be used with or without group_backend. Without group_backend
    bulk invalidation is not supported.

    Wrapped callable gets `invalidate` methods. Call `invalidate` with
    same arguments as function and the result for these arguments will be
    invalidated.
    """

    if group:
        backend_kwargs = {'group': group}
        get_key = _cache_key
    else:
        backend_kwargs = {}
        def get_key(*args, **kwargs):
            return sanitize_memcached_key(_cache_key(*args, **kwargs))

    def _cached(func):

        func_type = _func_type(func)

        def full_name(args):
            # full name is stored as attribute on first call
            if not hasattr(wrapper, '_full_name'):
                name, _args = _func_info(func, args)
                wrapper._full_name = name
            return wrapper._full_name

        @wraps(func)
        def wrapper(*args, **kwargs):
            # try to get the value from cache
            key = get_key(full_name(args), func_type, args, kwargs)
            value = cache.get(key, **backend_kwargs)

            # in case of cache miss recalculate the value and put it to the cache
            if value is None:
                value = func(*args, **kwargs)
                cache.set(key, value, timeout, **backend_kwargs)
            return value

        def invalidate(*args, **kwargs):
            ''' invalidates cache result for function called with passed arguments '''
            key = get_key(full_name(args), 'function', args, kwargs)
            cache.delete(key, **backend_kwargs)

        def repopulate(*args, **kwargs):
            ''' repopulates cache result for function called with passed arguments '''
            key = get_key(full_name(args), func_type, args, kwargs)
            value = func(*args, **kwargs)
            cache.set(key, value, timeout, **backend_kwargs)

        wrapper.repopulate = repopulate
        wrapper.invalidate = invalidate
        return wrapper
    return _cached


from imdb import IMDb
API = IMDb()

#deze functie vraagt aan de user welke titel de API moet opzoeken
#daarna checkt de functie of de titel legitiem is, zoniet dan wordt er weer aan de user gevraagt voor een titel
def get_first_movie():
    movie_name = input("Geef alstublieft de naam van een film op.\n")
    movies = API.search_movie(movie_name)

    if len(movies) > 0 and movies[0].movieID != '':
        return movies[0]
    else:
        print('De opgegeven film is niet gevonden')
        get_first_movie()

#deze fucnties zijn voor configurariteit zodat de user kan bepalen welke informatie er wordt getoond
def get_directors():
    try:
        print('Directors:')
        for director in movie_details['directors']:
            print(director['name'])
    except:
        print("Deze film heeft geen directors.")

def get_writers():
    try:
        print('writers:')
        for director in movie_details['writers']:
            print(writers)
    except:
        print("Deze film heeft geen writers.")

def get_genres():
    try:
        print('Genres:')
        for genre in movie_details['genres']:
            print(genre)
    except:
        print("Deze film heeft geen genres.")

def get_cast():
    try:
        print('cast:')
        for cast in movie_details['cast']:
            print(cast)
    except:
        print("Deze film heeft geen cast.")

#deze functie vragen aan de user welke informatie moet worden getoond
def get_user_input_directors():
    user_input = input("Wil je de directors zien? y/n\n")
    print('\n')
    if user_input == 'y':
        get_directors()
    elif user_input != 'n' and user_input != '':
        print('het opgegeven woord is niet herkend')
        get_user_input()

def get_user_input_writers():
    user_input = input("Wil je de writers zien? y/n\n")
    print('\n')
    if user_input == 'y':
        get_writers()
    elif user_input != 'n' and user_input != '':
        print('het opgegeven woord is niet herkend')
        get_user_input_writers()

def get_user_input_genres():
    user_input = input("Wil je de genres zien? y/n\n")
    print('\n')
    if user_input == 'y':
        get_genres()
    elif user_input != 'n' and user_input != '':
        print('het opgegeven woord is niet herkend')
        get_user_input_genres()

def get_user_input_cast():
    user_input = input("Wil je de cast zien? y/n\n")
    print('\n')
    if user_input == 'y':
        get_cast()
    elif user_input != 'n' and user_input != '':
        print('het opgegeven woord is niet herkend')
        get_user_input_cast()

first_movie = get_first_movie()
movie_details = API.get_movie(first_movie.movieID)
get_user_input_directors()
get_user_input_writers()
get_user_input_genres()
get_user_input_cast()


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

import json

from scrapy import Spider, Request
from ..items import ImgItem
from ..get_words import read_keywords_list

def check_contain_chinese(check_str):
    for ch in check_str:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True
    return False

def get_keyword(row_words):
    keywords = []
    for row_word in row_words:
        if check_contain_chinese(row_word) == True:
            keywords.append(row_word)
    return keywords

class ImgspiderSpider(Spider):
    name = "img_bd"

    def __init__(self, *args, **kwargs):
        self.allowed_domains = ["image.baidu.com", "www.baidu.com", 'ss3.bdstatic.com', 'ss2.bdstatic.com',
                                'ss1.bdstatic.com', 'ss0.bdstatic.com']

    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.54 Safari/536.5'
    }

    def start_requests(self):

        row_words = read_keywords_list()
        keywords = get_keyword(row_words)

        urls = [
            'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord+={0}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&word={0}&z=&ic=0&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&step_word={0}&pn={1}&rn=60&gsm=1e&1528441524820='.format(
                keyword, page * 60) for keyword in keywords for page in range(0, 401)]
        for url in urls:
            yield Request(url, headers=self.headers)

    def parse(self, response):
        img_items = json.loads(response.body)['data']
        for img_single in img_items:
            if 'middleURL' in list(img_single.keys()):
                img_url = img_single['middleURL']
                if img_url != "":
                    img_item = ImgItem()
                    print(img_url)
                    img_item['img_url'] = img_url
                    img_item['img_website'] = self.name.split('_')[-1]
                    img_item['img_name'] = img_url.split('/')[-1]
                    yield img_item


from configs import *
import sys
import matplotlib.pyplot as plt
import numpy as np
from game_env.wolfpack import *
import json
import time
from configs import *

from ray.rllib.env import MultiAgentEnv

from agent.basic import GeneralAgent
from gym.spaces import Box
from gym.spaces import Discrete

from configs import ALL_PLAYER_ACTIONS
print(DQNSetting.TOTAL_STEPS_PER_EPISODE)

class GatherMultEnv(MultiAgentEnv):

    def __init__(self):

        self.screen = None
        self.visual =DQNSetting.VISUAL_GUI
        self.env = None

        self.agents = {} #[]
        self.timestep_watch = Stopwatch()
        self.logger = Params().logger
        self.episode = None  # number of episode for training
        self.visualize = DQNSetting.VISUAL_DATA

        self.iteration = None
        self.n_steps = DQNSetting.TOTAL_STEPS_PER_EPISODE
        self.n_episodes = DQNSetting.TOTAL_NUM_EPISODE

        self.set_up()
        self.num_agents = len(self.agents)
        self.fig, self.ax = plt.subplots()
        plt.ion()

        self.full_observable = True

    def set_up(self):
        self.logger.warning("Command line environment setting up")
        with open('wolfpack.json') as cfg:
            env_config = json.load(cfg)
        self.env = EnvironmentWolfpack(env_config)
        self.logger.warning("Loading map successfully")
        self.start_time = time.time()

        self.iteration = 0

        """ Load the RL agent into the Game GUI. """
        self.agents = {'agent-0':GeneralAgent(), 'agent-1':GeneralAgent()}
        idx = 0
        for agent in self.agents.values():
            agent.player_idx = idx
            idx += 1

    def show(self, img):
        plt.imshow(img, interpolation='nearest')
        # plt.draw()
        plt.pause(.0001)

    def reset(self):
        self.timestep_watch.reset()
        self.env.new_episode()
        self.iteration = 0
        obs = {}
        for agent_id, agent in self.agents.items():
            agent.begin_episode()
            obs[agent_id]= self.env.player_list[agent.player_idx].convert_observation_to_rgb()
        return obs

    def move(self, agent, action):
        agent.step_in_an_episode = self.iteration
        observation = self.env.player_list[agent.player_idx].convert_observation_to_rgb()
        # agent.action = agent.act(observation)

        self.env.take_action(action, self.env.player_list[agent.player_idx]) #changes agent state
        self.env.move(agent.step_in_an_episode) # changes env state
        cur_reward = self.env.player_list[agent.player_idx].reward
        return observation, cur_reward, False

    def step(self, actions):
        # Main game loop.
        self.iteration += 1
        observations = {}
        rewards = {}
        dones = {}
        info = {}
        for agent_id, agent in self.agents.items():
            obs, reward, done= self.move(agent,actions[agent_id])
            observations[agent_id] = obs
            rewards[agent_id] = reward
            in_reward = agent.update_internal(actions[agent_id],\
                reward,\
                self.get_neigbors(agent_id, self.env.player_list[agent.player_idx].observable_view),\
                self.iteration)
            print(f' InR: {in_reward}')
            print(f'elibility trace {agent_id} {agent.eligibility_trace}')
            print(f'defections n{agent_id} {agent.defection_n}')
            dones[agent_id] = done
        dones["__all__"] = np.any(list(dones.values()))

        # self.agent_pos(
        self.show(observations['agent-0'])
        if in_reward < 0:
            import sys
            sys.exit()
        return observations, rewards, dones, info

    def get_neigbors(self, agent_id, agent_view):
        if self.full_observable:
            return [neigbor for id, neigbor in self.agents.items() if id != agent_id ]
        else:
            return [neigbor for id, neigbor in self.agents.items() if id != agent_id and \
                self.is_neibor_in_view(agent_view,self.env.player_list[neigbor.player_idx].get_position())]
    def is_neibor_in_view(self, agent_view, agent2_pos):
        # agent_view = x_min, x_max, y_min, y_max
        return agent2_pos.x >= agent_view[0] and agent2_pos.x <= agent_view[1]\
            and agent2_pos.y >=agent_view[2] and agent2_pos.y <= agent_view[3]

    def agent_pos(self, agent):
        return self.env.player_list[agent.player_idx].get_position()

    @property
    def action_space(self):
        return Discrete(len(ALL_PLAYER_ACTIONS))

    @property
    def observation_space(self):
        return Box(low=0, high=255, shape=(*GameSetting.player_view, 3), dtype=np.int64) #np.int8


#! -*- encoding:utf-8 -*-
from __future__ import with_statement

import sys
import re
import os
import time
import subprocess
import ConfigParser
import threading
import getopt

__doc__ = """
this script file is for start BBS spider.

usage:
-h: help info
-d: debug model
-r: True running
-e: occur exception and sys.exit()
-t: time end and sys.exit(), standard by second
-s: spider's name
"""

__author__ = "C.L.Tang"

#获取所有的爬虫的名称和线程数
def _get_spider():
    all_spider_dict = []
    cf = ConfigParser.ConfigParser()
    cf.read(''.join([os.getcwd(), os.sep, 'settings.conf']))
    s = cf.sections()
    for section in s:
        spider_name = cf.get(section, "spider_name")
        spider_num = cf.get(section, "spider_num")
        spider_dict = {spider_name : spider_num}
        all_spider_dict.append(spider_dict)
    return all_spider_dict

def start_make(all_spider_dict):
    #启动爬虫,根据传入参数判断一些行为，是否是测试模式，是否是真实运行。
    #测试模式的退出方法
    code = '''def funs(command):
    while 1:
        os.system(command)'''
    for spider_dict in all_spider_dict:
        #多线程同时执行du
        spidername = spider_dict.keys()[0]
        code_1 = code_make(spidername)
        code = ''.join([code, '\n', code_1])
        #code_make(spider_dict.keys())
    code = '\n' + code + 'ts = []\n'
    for spider_dict in all_spider_dict:
        spidername = spider_dict.keys()[0]
        spidernum = spider_dict[spidername]
        code = code + '\n'
        code_for = 'for i in range(%s):' % spidernum
        code = code + '\n' + code_for
        code_command = '    ts.append(threading.Thread(target = funs,args = (command_%s,)))' % spidername
        code = code + '\n' + code_command
    code = ''.join([code, '\n', '''
for i in ts:
    i.start()'''
    ])

    exec(code)

def code_make(spider_name):
    #生成代码对象.
    code = '''command_%s = 'scrapy crawl %s'
''' % (spider_name, spider_name)
    return code

def main():
    all_spider_dict  = _get_spider()
    start_make(all_spider_dict)

             #真实运行，要调用某个方法了
if __name__ == '__main__':

    main()


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

import numpy as np
import wavio

sr = 44100   # 44.1khz sampling rate
T  = 3      # wave duration in seconds
F  = [420 , 460 , 500 , 540 , 580 , 620 , 660 , 700 , 740, 780] # array of frequencies

t = np.linspace(0, T, T * sr) # create time array with T * sr samples
s = np.zeros(T * sr)          # initialize s array

for f in F:
    s += np.sin(2 * np.pi * f * t)  # add sin wave with this frequency

wavio.write("sine.wav", s, sr, sampwidth=3)


#http://networkx.github.io/documentation/latest/examples/graph/karate_club.html
#Zachary's Karate Club graph
#Data file from:
#http://vlado.fmf.uni-lj.si/pub/networks/data/Ucinet/UciData.htm
import networkx as nx
import matplotlib.pyplot as plt

f=open("kNodes.txt","a")
G=nx.karate_club_graph()
plt.figure(figsize=(8,8))
nx.draw_networkx(G)
plt.axis('off')
f.write("Node Degree")
for v in G:
    f.write('%s     %s\n' % (v,G.degree(v)))
plt.savefig("kClubGraph.png")
plt.show()


import sys
from typing import List, Dict, Iterable, Iterator, Tuple

"""Refer to https://stackoverflow.com/questions/19472530/representing-graphs-data-structure-in-python
"""

QUERY = "query"
NAME = "name"
DATA = "data"

burnin = {
    'name': 'burn-in',
    'query': 'serial number',
    'data': ['part number'],
}

pcbatracking = {
    'name': 'pcba tracking',
    'query': 'part number',
    'data': ['pcba number', 'converter number',],
}

pretest = {
    'name': 'pretest',
    'query': 'converter number',
    'data': ['pcba number']
}

safety = {
    'name': 'safety',
    'query': 'pcba number'
}

pcba = {
    'name': 'main PCBA',
    'query': 'serial number',
}

def find_entry_candidates(node_descriptions: List[Dict[str, str]], main_query: str) -> Iterator:
    """Generate entry candidates for the future control flow graph.

    CFG = control flow graph

    :param node_descriptions: list of dictionaries, representing high level descriptions of data set nodes
    :param main_query: string, representing the main query used to determine the start of CFG
    :return: tuple(string, dictionary), representing the dictionary and its name

    """

    for node in node_descriptions:
        try:
            if node.get(QUERY) == main_query:
                yield (node.get(NAME), node)

        except (KeyError,):
            continue

def validate_entry_candidate(node: Dict[str, str]):
    pass

def generate_connections(raw_nodes: Dict) -> List[Tuple[str, str]]:
    """Generate a list of connections between nodes.

    :param raw_nodes: list of dictionaries, representing a list of data set nodes
    :return: list of tuples of (str, str), where each tuple represents a connection between one data set and another

    """

    connections = []

    for node in raw_nodes:

        # Get node's data
        data = node.get(DATA)

        # make connections from node's data
        for n in [x for x in raw_nodes if x is not node]:

            # get query
            query = n.get(QUERY)

            try:
                if query in data:
                    connections.append((node.get(NAME), n.get(NAME)))

            except (TypeError,):
                continue

    return connections

def get_all_nodes(connections: List[Tuple[str, str]]) -> List[str]:

    nodes = set()

    for node1, node2 in connections:
        nodes.add(node1)
        nodes.add(node2)

    return list(nodes)

def debug_(*args, **kwargs):

    first_query = 'serial number'
    raw_nodes = [burnin, pcbatracking, pretest, safety, pcba]

    gen = find_entry_candidates(node_descriptions=raw_nodes, main_query=first_query)
    entry_node_str, entry_node_obj = next(gen)
    print(entry_node_str, entry_node_obj)

    connections = generate_connections(raw_nodes=raw_nodes)
    print(connections)

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


#! python

__author__ = 'Lucas'

import itertools
from nose.tools import assert_equals

def powers(lst):
    c = 0
    r = 0
    while c <= len(lst):
        r += len(list(itertools.combinations(lst, c)))
        c += 1
    return r

assert_equals(powers([1, 2, 3]), 8)


import sys

sys.path.append('..')

from components.backbones import get_backbone
from components.datasets import get_dataset
from components.optimizer import Adam
from components.utils import test_masks
from components.utils import get_cr
from components.learner_cavia import Learner
from torch.utils.data import DataLoader

import torch.nn.functional as F
import numpy as np

import argparse
import torch
import os

SECOND = True

PATH_BASE = None

PATH_DATA = None
N_CLS = None
DATA = None
DATA_SINGLE = None

NAME_DATA = None
NAME_BACKBONE = None

SECOND = True

def magnitude(args, config, path_load, device):
    print('Load net from', path_load)
    save_dict = torch.load(path_load)
    state_dict = save_dict['state_dict']
    target = save_dict['target']

    net = Learner(config, args.num_context_params, args.context_in).to(device)
    net.load_state_dict(state_dict)

    # Load data
    data = DATA_SINGLE(PATH_DATA, args.imgsz, 'train', target)
    db = DataLoader(data, batch_size=24, shuffle=True, num_workers=8, pin_memory=True)
    data_test = DATA_SINGLE(PATH_DATA, args.imgsz, 'test', target)
    db_test = DataLoader(data_test, batch_size=32, shuffle=False, num_workers=8, pin_memory=True)

    if 'masks_dict' in save_dict.keys():
        masks_dict = save_dict['masks_dict']
    else:
        masks_dict = dict()

    # prune
    for _, alpha in enumerate(args.cr_steps):
        weights_map = net.weights_map
        for name in net.layers_name:
            name_w, name_b = weights_map[name]

            w = state_dict[name_w]
            b = state_dict[name_b]

            w_reshape_abs = torch.abs(torch.reshape(w, [-1]))

            index_w = torch.sum(w_reshape_abs == 0) + int(w_reshape_abs.size(0) * alpha)
            threshold_w = torch.sort(w_reshape_abs)[0][index_w]

            mask_w = (torch.abs(w) >= threshold_w).cpu().numpy().astype(np.int32)
            state_dict[name_w] = w * torch.tensor(mask_w, dtype=torch.float32, requires_grad=True).to(device)
            masks_dict[name_w] = mask_w

            b_abs = torch.abs(b)

            index_b = torch.sum(b_abs == 0) + int(b.size(0) * alpha)
            threshold_b = torch.sort(b_abs)[0][index_b]

            mask_b = (b_abs >= threshold_b).cpu().numpy().astype(np.int32)
            state_dict[name_b] = b * torch.tensor(mask_b, dtype=torch.float32, requires_grad=True).to(device)
            masks_dict[name_b] = mask_b

        net.load_state_dict(state_dict)

        test_masks(masks_dict, net.state_dict())
        cr = get_cr(net, masks_dict)

        # Retrain
        for epoch in range(args.epoch_retrain):
            opt = Adam(net.parameters(), masks_dict, lr=0.001 * 0.1 ** (epoch // 5))
            for step, (x, y) in enumerate(db):
                net.train()
                x, y = x.to(device), y.to(device)

                logits = net(x)
                loss = F.cross_entropy(logits, y)

                opt.zero_grad()
                loss.backward()
                opt.step()

                if (step + 1) % 10 == 0:
                    net.eval()
                    correct = 0.
                    for x, y in db_test:
                        x, y = x.to(device), y.to(device)

                        logits_q = net(x)

                        with torch.no_grad():
                            pred_q = F.softmax(logits_q, dim=1).argmax(dim=1)
                            correct += torch.eq(pred_q, y).sum().item()

                    acc = correct / len(db_test.dataset)
                    print('\rEpoch', epoch, 'step', step + 1, 'loss', '%.4f' % loss.cpu().detach().numpy(), 'Test',
                          step, 'acc', '%.4f' % acc, end='')
        print('')

        # Save
        path_save = '/'.join(path_load.split('/')[:-1])
        if not os.path.exists(path_save):
            os.makedirs(path_save)

        path_model = '%s/net-cr_%.2f.pkl' % (path_save, cr)
        print('Save as %s' % path_model)
        torch.save(
            {
                'state_dict': net.state_dict(),
                'masks_dict': masks_dict,
                'target': target,
                'cr': cr,
                'config': config
            },
            path_model
        )

def exp(n_way,
        k_spt,
        dataset,
        backbone,
        task,
        path_model=None,
        k_qry=15,
        cr_steps=[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.05],
        epoch_test=5,
        epoch_retrain=10,
        epoch=60000,
        num_context_params=100,
        context_in=[False, False, True, False, False],
        imgsz=84,
        imgc=3,
        task_num=4,
        meta_lr=1e-3,
        update_lr=0.01,
        update_step=5,
        update_step_test=10,
        log_print=False):
    argparser = argparse.ArgumentParser()
    # prune
    argparser.add_argument('--cr_steps', type=list, help='cr at each iteration', default=cr_steps)
    argparser.add_argument('--epoch_test', type=int, help='number of test times', default=epoch_test)
    argparser.add_argument('--epoch_retrain', type=int, help='number of retraining', default=epoch_retrain)
    # maml
    argparser.add_argument('--num_context_params', type=int, help='number of context parameter vector',
                           default=num_context_params)
    argparser.add_argument('--context_in', type=list, help='whether add context in each layer', default=context_in)
    argparser.add_argument('--log_print', type=bool, help='print logs for meta training', default=log_print)
    argparser.add_argument('--epoch', type=int, help='total number of task sets', default=epoch)
    argparser.add_argument('--n_way', type=int, help='n way', default=n_way)
    argparser.add_argument('--k_spt', type=int, help='k shot for support set', default=k_spt)
    argparser.add_argument('--k_qry', type=int, help='k shot for query set', default=k_qry)
    argparser.add_argument('--imgsz', type=int, help='image size', default=imgsz)
    argparser.add_argument('--imgc', type=int, help='number of channels for images', default=imgc)
    argparser.add_argument('--task_num', type=int, help='meta batch size, namely task num', default=task_num)
    argparser.add_argument('--meta_lr', type=float, help='meta-level outer learning rate', default=meta_lr)
    argparser.add_argument('--update_lr', type=float, help='task-level inner update learning rate', default=update_lr)
    argparser.add_argument('--update_step', type=int, help='task-level inner update steps', default=update_step)
    argparser.add_argument('--update_step_test', type=int, help='update steps for fine tunning',
                           default=update_step_test)
    args = argparser.parse_args()

    torch.manual_seed(222)
    torch.cuda.manual_seed_all(222)
    np.random.seed(222)

    device = torch.device('cuda')

    # 获得config
    config = get_backbone(backbone, n_way, imgsz, imgsz)

    # 获得dataset
    global DATA, DATA_SINGLE, PATH_DATA, N_CLS, PATH_BASE, NAME_DATA, NAME_BACKBONE
    DATA, DATA_SINGLE, PATH_DATA, N_CLS, PATH_BASE = get_dataset(dataset)
    NAME_DATA = dataset
    NAME_BACKBONE = backbone

    if task == 'magnitude':
        magnitude(args, config, path_model, device)
    else:
        raise RuntimeError


import pygame
import AgentEnvironment  # (setting global variables for window)
import CargoesManager

def gameLoop(listOfCargoes, listOfCargoesInOrder):
    pygame.init()
    pygame.display.set_caption('Artificial Intelligence Project')  # window title
    clock = pygame.time.Clock()
    time_elapsed_since_last_action = 0
    running = False
    paused = False
    alertPrinted = False
    loadedC = 0
    loadedA = 0
    loadedR = 0
    cargoToLoad = None
    i = 0
    currentPos = (0, 0)
    print("\nPRESS SPACE TO START LOADING...")
    while True:
        AgentEnvironment.drawEnvironment()
        AgentEnvironment.drawCargoes(listOfCargoes)
        AgentEnvironment.writeText(str(loadedC), 55, 55)
        AgentEnvironment.writeText(str(loadedA), 255, 55)
        AgentEnvironment.writeText(str(loadedR), 455, 55)

        dt = clock.tick(10)  # speed, number of fps as parameter
        time_elapsed_since_last_action += dt

        if time_elapsed_since_last_action > 100 and i < len(listOfCargoes) and running and not paused:
            for cargo in listOfCargoes:
                if cargo == listOfCargoesInOrder[i] and cargo.toBeLoaded == 0:
                    cargo.toBeLoaded = 1
                    if cargo.destination == 'Copenhagen':
                        loadedC += 1
                        print("\n" + str(i+1) + ". Loading cargo to Copenhagen...")
                        # find shortest path for the crane using A* algorithm
                        CargoesManager.findPathTo(cargo.x, cargo.y, currentPos)
                        currentPos = (cargo.x, cargo.y)
                        i += 1
                    elif cargo.destination == 'Antwerp':
                        loadedA += 1
                        print("\n" + str(i+1) + ". Loading cargo to Antwerp...")
                        # find shortest path for the crane using A* algorithm
                        CargoesManager.findPathTo(cargo.x, cargo.y, currentPos)
                        currentPos = (cargo.x, cargo.y)
                        i += 1
                    else:
                        loadedR += 1
                        print("\n" + str(i+1) + ". Loading cargo to Rotterdam...")
                        # find shortest path for the crane using A* algorithm
                        CargoesManager.findPathTo(cargo.x, cargo.y, currentPos)
                        currentPos = (cargo.x, cargo.y)
                        i += 1
                    break
            time_elapsed_since_last_action = 0

        if i == len(listOfCargoes) and not alertPrinted:
            print("\nALL CARGOES LOADED.")
            alertPrinted = True

        for event in pygame.event.get():  # events handling loop
            if event.type == pygame.QUIT:
                pygame.quit()  # after clicking x, closes the game window
                quit()  # IDLE quit
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if running == False:
                        running = True
                    else:
                        if paused == True:
                            paused = False
                        else:
                            paused = True
        pygame.display.update()

def Main():
    # read cargoes from file and put them into list
    listOfCargoes = CargoesManager.prepareCargoesForFuzzyController()

    # print cargoes for fuzzy controller
    CargoesManager.printCargoesForFuzzyController(listOfCargoes)

    # compute fuzzPriority parameter
    CargoesManager.computeFuzzyPriority(listOfCargoes)

    # write data into a file for decision tree
    CargoesManager.prepareCargoesForDecisionTree(listOfCargoes)

    # print cargoes for decision tree
    CargoesManager.printCargoesForDecisionTree(listOfCargoes)

    # determine order using decision tree
    listOfCargoesInOrder = CargoesManager.determineOrder(listOfCargoes)

    # prepare final data to be written into file and displayed
    CargoesManager.prepareFinalData(listOfCargoesInOrder)

    # write output file
    CargoesManager.writeOutputFile(listOfCargoesInOrder)

    # print cargoes in order
    CargoesManager.printCargoesInOrder(listOfCargoesInOrder)

    # set cargoes in environment
    CargoesManager.setCargoes(listOfCargoes)

    # find positions of cargoes in order
    CargoesManager.findCargoesPos(listOfCargoes, listOfCargoesInOrder)

    # draw environment and load cargoes
    gameLoop(listOfCargoes, listOfCargoesInOrder)

Main()


#!/usr/bin/env python3

import click
from sqlalchemy.orm import Session

from ambuda import database as db
from ambuda.seed.utils.itihasa_utils import create_db

engine = create_db()

@click.group()
def cli():
    pass

@cli.command()
def list_dicts():
    """List dictionaries in the database."""
    with Session(engine) as session:
        dicts = session.query(db.Dictionary).all()
    for d in dicts:
        print(d.slug)

@cli.command()
@click.argument("slug")
def delete_dict(slug: str):
    """Delete the given dictionary."""
    with Session(engine) as session:
        d = session.query(db.Dictionary).where(db.Dictionary.slug == slug).first()
        if d:
            session.delete(d)
            session.commit()

@cli.command()
def list_texts():
    """List texts in the database."""
    with Session(engine) as session:
        texts = session.query(db.Text).all()
    for t in texts:
        print(t.slug)

@cli.command()
@click.argument("slug")
def delete_text(slug: str):
    """Delete the given text."""
    with Session(engine) as session:
        text = session.query(db.Text).where(db.Text.slug == slug).first()
        if text:
            session.delete(text)
            session.commit()

@cli.command()
@click.argument("username")
def delete_user(username: str):
    """Delete the given user."""
    with Session(engine) as session:
        u = session.query(db.User).where(db.User.username == username).first()
        if u:
            session.delete(u)
            session.commit()

if __name__ == "__main__":
    cli()


import random
import networkx as nx
import matplotlib.pyplot as plt
import datetime as dt

all_colors = ["red", "green", "blue"]#, "yellow"]#, "pink", "purple"]

class Backtracking:
    def __init__(self):
        self.create_graph()
        self.create_color_mapping()

    """
    Create an undirected graph with a random number of nodes and edges
    """
    def create_graph(self):
        # number of nodes
        self.n = random.randint(5, 5)

        print(str(self.n) + " VERTICES")

        # number of edges
        self.e = random.randint(5, 8)

        print(str(self.e) + " EDGES")

        # create graph
        self.graph = nx.Graph()

        # add nodes
        for i in range(1, self.n + 1):
            self.graph.add_node(i)

        maxNr = (self.n * (self.n - 1)) / 2

        print(str(maxNr) + " MAX NUMBER OF EDGES")

        # add edges
        for i in range(self.e):
            start = random.randint(1, self.n)
            end = random.randint(1, self.n)
            while (start == end or self.graph.has_edge(start, end)) and self.graph.number_of_edges() < maxNr:
                if self.graph.number_of_edges(start) == self.n - 1:
                    start = random.randint(1, self.n)
                end = random.randint(1, self.n)

            self.graph.add_edge(start, end)

        print(str(self.graph.number_of_edges()) + " TOTAL EDGES")

    def create_color_mapping(self):
        # self.available_colors = []
        # for color in all_colors:
        #     if random.randint(0, 1) == 0:
        #         self.available_colors.append(color)
        #
        # if len(self.available_colors) <= 1:
        #     self.available_colors = all_colors

        # self.available_colors = all_colors

        self.available_colors = all_colors

    """
    Plot the graph
    """
    def plot_graph(self, colors):
        nx.draw(self.graph, node_color=colors, with_labels=True)
        plt.savefig("graph_" + str(self.n) + "_" + str(self.e) + ".png")
        plt.show()

    def get_colors(self, candidate):
        colors = []
        for c in candidate:
            colors.append(self.available_colors[c])
        return colors

    def is_valid(self, candidate):
        if len(candidate) > self.n:
            return False

        for neighbour in self.graph[len(candidate)]:
            if len(candidate) > neighbour and candidate[neighbour - 1] == candidate[-1]:
                return False
        # for x in candidate[:-1]:
        #     if x == candidate[-1]:
        #         return False
        return True

    def is_solution(self, candidate):
        return len(candidate) == self.n

    def back(self):
        candidate = [-1]
        start = dt.datetime.now()
        solutions = []

        while len(candidate) > 0:
            chosen = False
            while not chosen and candidate[-1] < len(self.available_colors) - 1:
                candidate[-1] += 1
                chosen = self.is_valid(candidate)
            if chosen:
                if self.is_solution(candidate):
                    # self.plot_graph(self.get_colors(candidate))
                    print((dt.datetime.now() - start).total_seconds())
                    solutions.append(self.get_colors(candidate))
                    if len(solutions) == 10:
                        return solutions
                candidate.append(-1)
            else:
                candidate = candidate[:-1]

        return solutions


from django.http import HttpResponse
from django.shortcuts import render, redirect
from .models import *
from django.contrib.auth.decorators import login_required

def classboard (reqeust):
    return render(reqeust, 'classboard2.html')

def list (request):
    # context['id'] = request.session['user_name']
    loginSession = request.session['user_name']
    print('list - ', loginSession)
    boards = Quiz.objects.all()
    print('list request -', type(boards), boards)
    context = {'boards': boards , 'id' : loginSession }
    return render(request, 'list2.html', context)

def postForm (reqeust):
    print ('request postFrom =')
    return render(reqeust, 'post2.html')

def post (request):
    loginSession = request.session['user_name']
    print('post - ', loginSession)
    user = User.objects.get(username=loginSession)
    print('post user -', user.id)
    title = request.POST['title']
    content = request.POST['content']
    explanation = request.POST['explanation']
    answer = request.POST['answer']
    classType = request.POST['classType']
    board = Quiz(title=title, content=content, explanation=explanation,
                 answer=answer, classType=classType , writer=user)
    board.save()
    return redirect('hello_app:list')

def read (request, id):
    board = Quiz.objects.get(id=id)
    board.save
    print('read result 2 - ', board)
    context = {'board' : board }
    context['id'] = request.session['user_name']
    context['writer'] = str(board.writer)
    return render(request, 'read2.html', context)

def modifyFrom (request):
    id = request.POST['id']
    board = Quiz.objects.get(id=id)
    context = {'board': board }
    return render (request, 'modify2.html', context)

def modify (request):
    id = request.POST['id']
    title = request.POST['title']
    content = request.POST['content']
    explanation = request.POST['explanation']
    answer = request.POST['answer']
    print ('request -', title, content, explanation, answer)
    board = Quiz.objects.get(id=id)
    board.title = title
    board.content = content
    board.explanation = explanation
    board.answer = answer
    board.save()
    return redirect('hello_app:list')

def remove (request):
    id = request.POST['id']
    print('request remove -', id)
    Quiz.objects.get(id=id).delete()
    return redirect('hello_app:list')

def answerFrom (request):
    id = request.POST['id']
    board = Quiz.objects.get(id=id)
    context = {'board' : board}
    return render (request, 'answer2.html', context)

def answer (request):
    id = request.POST['id']
    re_answer = request.POST['re_answer']
    print ('request -', re_answer)
    board = Quiz.objects.get(id=id)
    if re_answer == board.answer:
        print ('정답입니다!!', re_answer)
        return redirect('hello_app:list')
    else:
        print('오답 입니다!!', re_answer)
        return HttpResponse('오답 입니다!!')


import retro
import random
import numpy as np
import pickle

actions_list = [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                # [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
                # [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
                [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
                [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]]

actions_size = len(actions_list)

def to_grayscale(img):
    return np.mean(img, axis=0).astype(np.uint8)

def downsample(img):
    return img[::2, ::2]

def preprocess(img):
    return np.array_str(to_grayscale(to_grayscale(img)))

env = retro.make(game='SuperMarioKart-Snes')
start_state = env.reset()
preprocessed_start_state = preprocess(start_state)
initial_vec = np.zeros(actions_size)

q_table = {preprocessed_start_state: initial_vec}

# Hyperparameters
alpha = 0.1
gamma = 0.6
epsilon = 1
eps_decay = 0.99999970
eps_min = 0.1

for i in range(1, 100000):
    env.reset()
    state = preprocessed_start_state
    reward = 0
    done = False

    while not done:
        if random.uniform(0, 1) < epsilon:
            # action = env.action_space.sample() # Explore action space
            action = random.choice(actions_list)
            action_index = actions_list.index(action)
        else:
            action_index = np.argmax(q_table[state])
            action = actions_list[action_index]
        epsilon *= eps_decay
        epsilon = max(eps_min, epsilon)

        pre_next_state, reward, done, info = env.step(action)
        next_state = preprocess(pre_next_state)

        old_value = q_table[state][action_index]

        if next_state not in q_table.keys():
            q_table[next_state] = initial_vec
            #print("TRIGGERED", count)

        #print(q_table)
        next_max = np.max(q_table[next_state])
        new_value = (1 - alpha) * old_value + alpha * (reward + gamma * next_max)
        q_table[state][action_index] = new_value

        state = next_state
        #env.render()

    if i % 100 == 0:
        print("Episode: " + str(i), "epsilon = " + str(epsilon))
        with open('q_tables/q_table_' + str(i) + '.pickle' , 'wb') as handle:
            pickle.dump(q_table, handle, protocol=pickle.HIGHEST_PROTOCOL)

with open('q_tables/q_table_FINAL.pickle', 'wb') as handle:
        pickle.dump(q_table, handle, protocol=pickle.HIGHEST_PROTOCOL)
print("Training finished.\n")


from django.contrib import admin
from django.contrib import admin
from django.urls import path

from django.conf.urls import url
from rest_framework_jwt.views import obtain_jwt_token
from portfolio import views
from portfolio.views import RegisterView

urlpatterns = [

    path('admin/', admin.site.urls),
    path('auth/', obtain_jwt_token),
    path('register/', RegisterView.as_view(), name='auth_register'),
    path('', views.customer_list),
    url(r'^api/customers/$', views.customer_list),
    url(r'^api/customers/(?P<pk>[0-9]+)$', views.getCustomer),
    path('investments/', views.investment_list),
    url(r'^api/investments/$', views.investment_list),
    url(r'^api/investments/(?P<pk>[0-9]+)$', views.getInvestment),
    path('stocks/', views.stock_list),
    url(r'^api/stocks/$', views.stock_list),
    url(r'^api/stocks/(?P<pk>[0-9]+)$', views.getStock),
    url(r'^api/investmentsbycustId/(?P<pk>[0-9]+)$', views.getInvestmentByCustId),
    url(r'^api/stocksbycustId/(?P<pk>[0-9]+)$', views.getStockByCustId),
    path('mutualfunds/', views.mutualfunds_list),
    url(r'^api/mutualfunds/$', views.mutualfunds_list),
    url(r'^api/mutualfunds/(?P<pk>[0-9]+)$', views.getMutualFunds),

]


# v.2 - Algorithm seems to be correct
#       Removed some unnecessary loops but still have: "Failed test #12. Time limit exceeded"
#       Looks like they want O(nlog) but not O(n^2)
#
# Задача на программирование повышенной сложности: наибольшая невозрастающая подпоследовательность
# Дано целое число 1≤n≤105и массив A[1…n], содержащий неотрицательные целые числа, не превосходящие 109.
# Найдите наибольшую невозрастающую подпоследовательность в A. В первой строке выведите её длину k,
# во второй — её индексы 1≤i1<i2<…<ik≤n (таким образом, A[i1]≥A[i2]≥…≥A[in]).
#
# Sample Input:
# 5
# 5 3 4 4 2
# Sample Output:
# 4
# 1 3 4 5
#
import sys

def main():
    # Get input data
    n = int(input())
    print(n)
    # a = [int(i) for i in input().split()]
    reader = (tuple(map(int, line.split()) for line in sys.stdin))
    a, b = list(reader)
    print(n, a, b)
    exit()

    d = []
    p = []
    max_val, max_indx = 0, 0
    for i in range(0, n):
        # print('i:', i)
        d.append(1)
        p.append(0)
        for j in range(0, i):
            # print('j:', j)
            if a[j] >= a[i] and d[j]+1 > d[i]:
                d[i] = d[j] + 1
                p[i] = j+1
                if d[i] > max_val:
                    max_indx = i
                    max_val = d[i]
            # print(max_indx, max_val)
    # print(d)
    # print(max_indx, max_val)
    # print(p)

    # Build the sequence from p[]
    r = []
    indx = max_indx + 1
    while True:
        r.append(indx)
        indx = p[indx-1]
        if indx == 0:
            break

    # print(r)

    # Print results
    print(d[max_indx])
    for i in range(len(r)):
        print(r.pop(), end=' ')

if __name__ == "__main__":
    main()


from flask import Flask, request, send_file

import os
import io
from tacotron.hparams import create_hparams
from tacotron.inference import infer, model
import librosa.display
import numpy as np
import torch

from dotenv import load_dotenv

APP_ROOT = os.path.dirname(__file__)
dotenv_path = os.path.join(APP_ROOT, '.env')
load_dotenv(dotenv_path, verbose=True)

TACOTRON_CHECKPOINT = "./output/checkpoint_15500"
PARALLEL_WAVENET_CHECKPOINT_FOLDER = "./parallel_wavenet_vocoder/checkpoint"
SEQUENTIAL_WAVENET_CHECKPOINT_FOLDER = "./parallel_wavenet_vocoder/checkpoint/20180510_mixture_lj_checkpoint_step000320000_ema.pth"

WAVENET_OUTPUT_FOLDER = "./audio_out"
WAVENET_PRESET = "./parallel_wavenet_vocoder/presets/ljspeech_gaussian.json"

PORT = 7070

# os.environ.putenv("TACOTRON_CHECKPOINT", "./output/checkpoint_15500")
# os.environ.putenv("PARALLEL_WAVENET_CHECKPOINT_FOLDER", "./parallel_wavenet_vocoder/checkpoint")
# os.environ.putenv("SEQUENTIAL_WAVENET_CHECKPOINT_FOLDER", "./parallel_wavenet_vocoder/checkpoint/20180510_mixture_lj_checkpoint_step000320000_ema.pth")

# os.environ.putenv("WAVENET_OUTPUT_FOLDER", "./audio_out")
# os.environ.putenv("WAVENET_PRESET", "./parallel_wavenet_vocoder/presets/ljspeech_gaussian.json")

PORT = 7070

from tacotron_wavenet import tacotron_model, predict_spectrogram, parallel_wavenet_generate, nvidia_to_mama_mel

import uuid

# Utility class to have dot notation for dict
class dotdict(dict):
     """dot.notation access to dictionary attributes"""
     __getattr__ = dict.get
     __setattr__ = dict.__setitem__
     __delattr__ = dict.__delitem__

app = Flask(__name__)

tactron_hparams = create_hparams()
tacotron_m = tacotron_model(tactron_hparams, TACOTRON_CHECKPOINT)

@app.route("/synthesize", methods=["POST"])
def synthetize():
    """
    Route for synthesis
    Expectes a form-url-encoded form

    FORM:
        text: the text to synthesis
        engine: parallel or sequential, wavenet engine to use
        checkpoint: wavenet checkpoint to use
    """

    # Mel spectrogram generation
    sentence = request.form["text"]

    (text, mels) = predict_spectrogram(tacotron_m, sentence)

    mels = nvidia_to_mama_mel(create_hparams(
        "distributed_run=False,mask_padding=False"), mels)

    # Wavenet model loading
    checkpoint_number = request.form["checkpoint"]
    checkpoint_filename = 'checkpoint_step' + '{:0>9}'.format(checkpoint_number) + '.pth'
    checkpoint_path = os.path.join(PARALLEL_WAVENET_CHECKPOINT_FOLDER, checkpoint_filename)

    wavenet_type = "student"
    if request.form["engine"] == "sequential":
        wavenet_type = "teacher"
        checkpoint_path = SEQUENTIAL_WAVENET_CHECKPOINT_FOLDER

    # Waveform generation
    waveform = parallel_wavenet_generate(
        (text, mels), checkpoint_path, WAVENET_PRESET, wavenet_type)
    from parallel_wavenet_vocoder.hparams import hparams

    # write to file
    # As of now, The only way I know of to get WAV file, is to
    # write it to disk, then read it again
    filename = str(uuid.uuid4()) + '.wav'
    filepath = os.path.join('.', 'audio_out', filename)

    import librosa
    librosa.output.write_wav(filepath, waveform, sr=hparams.sample_rate)

    wav_file = open(filepath, "rb")
    wav_bytes = io.BytesIO(wav_file.read())
    wav_file.close()

    return send_file(wav_bytes,
                    attachment_filename=filename,
                    mimetype='audio/wav')

def _load_ljspeech():
    from parallel_wavenet_vocoder.preprocess import preprocess
    import multiprocessing
    from parallel_wavenet_vocoder import ljspeech

    num_workers = multiprocessing.cpu_count()

    preprocess(ljspeech, os.path.join('.', 'ljspeech'), os.path.join('.', 'tacotron_output', 'eval'), num_workers)

@app.route("/preprocess", methods=["GET"])
def preprocess():
    _load_ljspeech()

    return {'success': True}

global_step = 0
global_test_step = 0
global_epoch = 0

@app.route("/train-student", methods=["GET"])
def train_student():
    from datetime import datetime
    from tensorboardX import SummaryWriter
    from parallel_wavenet_vocoder.hparams import hparams, hparams_debug_string
    from parallel_wavenet_vocoder.train_student import build_model, get_data_loaders, share_upsample_conv, train_loop, save_checkpoint, restore_parts
    # _load_ljspeech()
    use_cuda = torch.cuda.is_available()

    checkpoint_dir = os.path.join('.', 'parallel_wavenet_vocoder', 'checkpoint')
    checkpoint_teacher_path = os.path.join('.', 'wavenet_vocoder', '20180510_mixture_lj_checkpoint_step000320000_ema.pth')
    data_root = os.path.join('.', 'tacotron_output', 'eval')
    speaker_id = None
    preset = os.path.join('.', 'wavenet_vocoder', '20180510_mixture_lj_checkpoint_step000320000_ema.json')

    # Load preset if specified
    if preset is not None:
        with open(preset) as f:
            hparams.parse_json(f.read())
    # Override hyper parameters
    # hparams.parse(args["--hparams"])
    assert hparams.name == "wavenet_vocoder"
    print(hparams_debug_string())

    fs = hparams.sample_rate

    os.makedirs(checkpoint_dir, exist_ok=True)

    # Dataloader setup
    data_loaders = get_data_loaders(data_root, speaker_id, test_shuffle=True)

    device = torch.device("cuda" if use_cuda else "cpu")

    # Model
    teacher = build_model(name='teacher').to(device)
    student = build_model(name='student').to(device)

    if hparams.share_upsample_conv:
        student_train_params = share_upsample_conv(teacher, student)
    else:
        student_train_params = student.parameters()

    receptive_field = student.receptive_field
    print("Receptive field (samples / ms): {} / {}".format(
        receptive_field, receptive_field / fs * 1000))

    optimizer = torch.optim.Adam(student_train_params,
                           lr=hparams.initial_learning_rate, betas=(
        hparams.adam_beta1, hparams.adam_beta2),
        eps=hparams.adam_eps, weight_decay=hparams.weight_decay,
        amsgrad=hparams.amsgrad)

    # if checkpoint_restore_parts is not None:
    #     restore_parts(checkpoint_restore_parts, student)

    # Load checkpoints
    assert checkpoint_teacher_path is not None
    restore_parts(checkpoint_teacher_path, teacher)
    for param in teacher.parameters():
        param.requires_grad = False

    # if checkpoint_student_path is not None:
    #     load_checkpoint(checkpoint_student_path, student, optimizer, reset_optimizer)

    # Setup summary writer for tensorboard
    # if log_event_path is None:
    log_event_path = "log/run-test" + str(datetime.now()).replace(" ", "_")
    os.makedirs(log_event_path, exist_ok=True)
    print("TensorBoard event log path: {}".format(log_event_path))
    writer = SummaryWriter(log_dir=log_event_path)

    global global_step, global_epoch, global_test_step

    # Train!
    try:
        train_loop(device, student, teacher, data_loaders, optimizer, writer,
                   checkpoint_dir=checkpoint_dir)
    except KeyboardInterrupt:
        print("Interrupted!")
        pass
    finally:
        save_checkpoint(
            device, student, optimizer, global_step, checkpoint_dir, global_epoch)

    print("Finished")
    return {'success': True}

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=os.environ['PORT'])


#Mini project console ERP system
emp_data = []
while True:
	print("Select any option")
	print("1.Add Employee")
	print("2.Delete Employee")
	print("3.Search Employee")
	print("4.Change Employee data")
	print("5.Display Employeee details")
	print("6.EXIT")
	ch = int(input("Enter the Choice: "))
	if ch is None:
		print("No employees in the list")
	elif ch == 1:
		#Add employee
		name = input("Enter name of the employee to add: ")
		emp_data.append(name)
	elif ch == 2:
		#Delete employee
		print(emp_data)
		name = input("Enter the name of the employee to delete: ")
		emp_data.remove(name)
	elif ch ==3:
		#Search employee
		name = input("Enter the name of the employee to search: ")
		if name in emp_data:
			print(name +" is in the list")
		else:
			print(name +" is not in the list")
	elif ch == 4:
		#Change the employee data
		name = input("Enter the employee name to change: ")
		pos = emp_data.index(name)
		new_name = input("Enter new employee name: ")
		emp_data[pos] = new_name
	elif ch == 5:
		#Display eemployee details
#		print(emp_data)
		for i in range(0,len(emp_data)):
			print(i+1,".",emp_data[i])
			i=i+1
	elif ch == 6:
		#To exit
		break
	else:
		print("Invalid choice")


#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Jonas Wacker'

from gensim.models import Word2Vec
from os.path import join, dirname, abspath, basename
import logging
import sys
import time
import multiprocessing
import wikidump2text
import druid
import nltk
import codecs

def data_directory():
    return join(dirname(dirname(abspath(__file__))), 'data')

corpus_path = join(data_directory(), 'enwiki-latest-pages-articles12.xml-p001825001p002425000.bz2')
wiki_text_output_path = join(data_directory(), 'enwiki-latest-pages-articles12.txt')
word2vec_output_path = join(data_directory(), 'enwiki-latest-pages-articles.word2vec')

logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
program = basename(sys.argv[0])
logger = logging.getLogger(program)

# Convert Wikipedia XML dump into .txt format
# wikidump2text.convert(corpus_path, wiki_text_output_path)

# Load Multiword Expressions as Dictionary
stopwords_path = join(data_directory(), 'stopwords_en.txt')
druid_path = join(data_directory(), 'druid_en.bz2')
druid_dict = druid.DruidDictionary(druid_path, stopwords_path, cutoff_score=0.0)

# Train the word2vec model

class MySentences(object):
    def __init__(self, filename):
        self.filename = filename
        self.stemmer = nltk.stem.PorterStemmer()

    def __iter__(self):
        # One line contains one wiki article.
        self.corpus = codecs.open(self.filename, 'r', encoding='utf-8')
        for line in self.corpus:
            ngrams = druid_dict.find_ngrams(line.lower().split())
            yield [self.stemmer.stem(token) for token in ngrams]

sentences = MySentences(wiki_text_output_path)  # a memory-friendly iterator
# bigram_transformer = Phrases(sentences)
# logger.info("Finished transforming bigrams. Time needed: " + str(time.time() - start_time))
start_time = time.time()
model = Word2Vec(sentences, size=100, window=5, min_count=5, workers=multiprocessing.cpu_count())

# trim unneeded model memory = use(much) less RAM
model.init_sims(replace=True)
model.save(word2vec_output_path)

logger.info("Finished building Word2Vec model. Time needed: " + str(time.time() - start_time))


'''
This problem was asked by Google.

Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.

'''

class BinaryNode(object):

    def __init__(self,id):
        self.id = id
        self.data = id
        self.ptr_left = None
        self.ptr_right = None

    def __str__(self): return str(self.id)

class BinaryTree(object):

    def __init__(self):
        self.__head = None
        self.items = 0

    def pre_order(self):

        # print yourself, go left, go right

        print('Start Pre Order')
        if not self.__head is None:
            self.__pre_order_helper(self.__head)

    def __pre_order_helper(self,current_node):

        print(current_node)

        if current_node.ptr_left:
                self.__pre_order_helper(current_node.ptr_left)

        if current_node.ptr_right:
                self.__pre_order_helper(current_node.ptr_right)

    def in_order(self):

        # keep going left, print yourself, go right

        print('Start In Order')
        if not self.__head is None:
            self.__in_order_helper(self.__head)

    def __in_order_helper(self,current_node):

        if current_node.ptr_left:
            self.__in_order_helper(current_node.ptr_left)

        print(current_node)

        if current_node.ptr_right:
            self.__in_order_helper(current_node.ptr_right)

    def post_order(self):

        # go left, go right, print yourself on the way back

        print('Start Post Order')
        if not self.__head is None:
            self.__post_order_helper(self.__head)

    def __post_order_helper(self,current_node):

        if current_node.ptr_left:
            self.__post_order_helper(current_node.ptr_left)

        if current_node.ptr_right:
            self.__post_order_helper(current_node.ptr_right)

        print(current_node)

    def add(self,id):

        if self.__head is None:
            self.__head = BinaryNode(id)
            self.items = self.items + 1
        else:
            self.__add_helper(id,self.__head)

    def __add_helper(self,id,current_node):

        if id < current_node.id:
             if current_node.ptr_left:
                self.__add_helper(id,current_node.ptr_left)
             else:
                current_node.ptr_left = BinaryNode(id)
                self.items = self.items + 1
        else:
            if current_node.ptr_right:
                self.__add_helper(id,current_node.ptr_right)
            else:
                current_node.ptr_right = BinaryNode(id)
                self.items = self.items + 1

        #self.balance_tree()

    def height(self,*args,**kwargs):
        # not implemented properly
        #
        #

        if kwargs.get('id'):
            node = self.bfs(kwargs['id'])
            return self.__is_balanced_helper(node)
        elif kwargs.get('node'):
            return self.__is_balanced_helper(kwargs['node'])
        else:
            if self.__head is None:
                return 0
            else:
                return self.__is_balanced_helper(self.__head)

    def is_balanced(self):

        if self.__head is None:
            return True
        else:
            return self.__is_balanced_helper(self.__head)>=0

    def __is_balanced_helper(self,parent):

        if not parent:
            return 0

        left_sub = self.__is_balanced_helper(parent.ptr_left)
        right_sub = self.__is_balanced_helper(parent.ptr_right)

        if (left_sub < 0 or
            right_sub < 0 or
            abs(left_sub - right_sub) > 1):
            return -1
        else:
            return max(left_sub,right_sub)+1

    def bfs(self,id):
        if self.__head is None:
            return None
        else:
            return self.__bfs_helper(self.__head,id)

    def __bfs_helper(self,head_node,id):

        node_queue = []
        node_queue.append(head_node)

        while node_queue:

            current_node = node_queue.pop(0)

            if current_node.id == id:
                return current_node
            else:
                if current_node.ptr_left:
                    node_queue.append(current_node.ptr_left)
                if current_node.ptr_right:
                    node_queue.append(current_node.ptr_right)
        return None

    def dfs(self,id):
        if self.__head is None:
            return None
        else:
            return self.__dfs_helper(self.__head,id)

    def __dfs_helper(self,head_node,id):

        node_stack = []
        node_stack.append(head_node)

        while node_stack:

            current_node = node_stack.pop()

            if current_node.id == id:
                return current_node
            else:
                if current_node.ptr_right:
                    node_stack.append(current_node.ptr_right)
                if current_node.ptr_left:
                    node_stack.append(current_node.ptr_left)
        return None

    def serialize(self):

        # this works with a bin search tree because of the comparison....
        # this would need to either a output inorder and preorder traversals (pre to create the treen, in order to find children nodes)
        # or we could store the null nodes too so that we know when part of the tree ends...
        # solutions somewhat depend on (scope, known data set, etc)

        serialized_string = ['']

        self.__serialize_helper(self.__head,serialized_string)

        return serialized_string[0]

    def __serialize_helper(self,current_node,serialized_string):

        serialized_string[0] = serialized_string[0] + current_node.data

        if current_node.ptr_left:
            self.__serialize_helper(current_node.ptr_left,serialized_string)

        if current_node.ptr_right:
            self.__serialize_helper(current_node.ptr_right,serialized_string)

    def deserialize(self,tree_string):

        [ self.add(item) for item in tree_string ]
        return None

def main():

    items = ['F','B','G','A','D','C','E','I','H']

    bin_tree = BinaryTree()
    for item in items:
        bin_tree.add(item)

    bin_tree.pre_order()

    first_tree = bin_tree.serialize()
    print("-"*10)

    new_tree = BinaryTree()
    new_tree.deserialize(first_tree)
    new_tree.pre_order()

if __name__ == '__main__':
    main()


import open3d as o3d
import numpy as np
import copy
import argparse

def main():
	parser = argparse.ArgumentParser(description='Mesh reconstruction.')
	parser.add_argument('--input', type=str)
	parser.add_argument('--strategy', type=str, default="Poisson", help='reconstruction strategy: Poisson / BPA')
	parser.add_argument('--use_filter', type=bool, default=False, help='use filter after recon or not')
	args = parser.parse_args()
	pcd_name = args.input
	Strategy = args.strategy
	Filter = args.use_filter

	# read point cloud (*.ply)
	pcd = o3d.io.read_point_cloud('./data/'+pcd_name)
	# remove outliers
	distances = pcd.compute_nearest_neighbor_distance()
	avg_dist = np.mean(distances)
	radius = 3 * avg_dist
	pcd_crop, _ = pcd.remove_radius_outlier(nb_points=5,radius=radius)
	# estimate normals
	pcd_crop.estimate_normals()

	# surface reconstruction
	if Strategy == "Poisson":
		mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(pcd_crop, depth=8, width=0, scale=1.1, linear_fit=False)[0]
		obox = pcd_crop.get_oriented_bounding_box()
		mesh_temp = mesh.crop(obox)
		# o3d.visualization.draw_geometries([obox,mesh_temp])
	elif Strategy == "BPA":
		distances = pcd_crop.compute_nearest_neighbor_distance()
		avg_dist = np.mean(distances)
		radius = 3 * avg_dist
		mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_ball_pivoting(pcd_crop,o3d.utility.DoubleVector([radius, radius * 2]))
		mesh_temp = mesh.simplify_quadric_decimation(100000)
		mesh_temp.remove_degenerate_triangles()
		mesh_temp.remove_duplicated_triangles()
		mesh_temp.remove_duplicated_vertices()
		mesh_temp.remove_non_manifold_edges()
		# o3d.visualization.draw_geometries([mesh_temp])

	# filter
	if Filter == True:
		mesh_temp = mesh_temp.filter_smooth_simple(number_of_iterations=1)

	# remove spurious triangles
	with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
		triangle_clusters, cluster_n_triangles, _ = mesh_temp.cluster_connected_triangles()
	triangle_clusters = np.asarray(triangle_clusters)
	cluster_n_triangles = np.asarray(cluster_n_triangles)
	mesh_out = copy.deepcopy(mesh_temp)
	triangles_to_remove = cluster_n_triangles[triangle_clusters] < 500
	mesh_out.remove_triangles_by_mask(triangles_to_remove)
	# o3d.visualization.draw_geometries([mesh_out])

	o3d.io.write_triangle_mesh("result/"+pcd_name, mesh_out)

if __name__=='__main__':
	main()


#Часы
from tkinter import *
import time

root1 = Tk()
root1.title("Время:")
time1 = 'time now:'
clock = Label(root1, font=('times', 20, 'bold'), bg='white')
clock.pack(fill=BOTH, expand=1)

def tick():
    global time1
    time2 = time.strftime('%Y: %D: %H:%M:%S')
    if time2 != time1:
        time1 = time2
        clock.config(text=time2)
    clock.after(200, tick)
root1.geometry("210x70+1100+100")
tick()

import datetime
import sqlite3
import tkinter

now = datetime.datetime.now()

def inserting(infa,text,cat):

    # Создаем соединение с нашей базой
    conn = sqlite3.connect('timing.db')

    cursor = conn.cursor()
    # Делаем INSERT запрос к базе данных
    cursor.execute("insert into timing_of_remindes values (?, ?, ?)",[(infa),(text),(cat)])
    conn.commit()
    # Проверяем результат
    cursor.execute("SELECT reminding FROM timing_of_remindes ORDER BY reminding LIMIT 3")
    results = cursor.fetchall()
    print(results)

    # Не забываем закрыть соединение с базой данных
    conn.close()

## Удаление данных
import sqlite3

conn = sqlite3.connect("timing.db")
cursor = conn.cursor()

#выбираем прошедшие данные чтобы их вывести...
sql = "SELECT * FROM timing_of_remindes WHERE time < ?"
cursor.execute(sql, [(now)])
#print(cursor.fetchall()) # or use fetchone()
listings=cursor.fetchall()

#...Удаляем их
sql = "DELETE FROM timing_of_remindes WHERE time < ?"
cursor.execute(sql,[(now)])

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

#text=input("Укажите напоминание:")
#then_time=input("Укажите дату, на которую поставить напоминание. Введите дату в формате: год.месяц.день.часы.минуты ")
now = datetime.datetime.now()

# Окно ввода данных...
def click():
    window.destroy()
    root.destroy()
    root1.destroy()

def click_p():
    lab.config(text=('Добавлено'))
    label.config(text=(entry1.get()))
    label_t.config(text=datetime.datetime(int(entry2.get()), int(entry3.get()), int(entry4.get()), int(entry5.get()), int(entry6.get())))
    conf=datetime.datetime(int(entry2.get()), int(entry3.get()), int(entry4.get()), int(entry5.get()), int(entry6.get()))
    label_cat.config(text=entryCat.get())

    inserting(conf, entry1.get(), entryCat.get())

#Создаем кнопки и поля ввода данных
window=tkinter.Tk()
frame=tkinter.Frame(window)
frame.pack()

label1=tkinter.Label(frame, text="Укажите напоминание: ",font=('Courier', 14))
label1.pack()

entry1=tkinter.Entry(frame)
entry1.pack()

labelCat=tkinter.Label(frame, text="Укажите категорию: ",font=('Courier', 14))
labelCat.pack()

entryCat=tkinter.Entry(frame)
entryCat.pack()

label2=tkinter.Label(frame, text="Формат: год,месяц,день,часы,минуты: ",font=('Courier', 14))
label2.pack()

entry2=tkinter.Entry(frame)
entry2.pack()
entry3=tkinter.Entry(frame)
entry3.pack()
entry4=tkinter.Entry(frame)
entry4.pack()
entry5=tkinter.Entry(frame)
entry5.pack()
entry6=tkinter.Entry(frame)
entry6.pack()

lab=tkinter.Label(frame)
lab.pack()

label=tkinter.Label(frame)
label.pack()

label_t=tkinter.Label(frame)
label_t.pack()

label_cat=tkinter.Label(frame)
label_cat.pack()

button1=tkinter.Button(frame,text="Сохранить!",command=click_p)
button1.pack()

button2=tkinter.Button(frame,text="Выйти",command=click)
button2.pack()

#Делаем оформление позиций
label1.grid(row=0,column=1)
entry1.grid(row = 1, column =1)
labelCat.grid(row = 2, column =1)
entryCat.grid(row = 3, column =1)

label2.grid(row = 4, column =1)
entry2.grid(row = 5, column = 0)
entry3.grid(row = 5, column = 1)
entry4.grid(row=5,   column=2)
entry5.grid(row=5,column=3)
entry6.grid(row=5,column=4)
button1.grid(row = 6, column = 1)
button2.grid(row=7,   column=1)
lab.grid(row=8,column=1)
label.grid(row=8,column=2)
label_t.grid(row=9,column=1)
label_cat.grid(row=8, column=3)

#Окно просроченных данных
from tkinter import *
root = Tk()
root.title("Ваши прошедшие напоминания")
frame1=Frame(root)
frame1.pack()

labeln=Label(frame1)
labeln.pack()
label2=Label(frame1)
label2.pack()

labeln.grid(row=0,column=1)
label2.grid(row = 1, column =1)
t=''
now=datetime.datetime.now()
for i in range(len(listings)):
    delta= now-datetime.datetime.strptime(listings[i][0],'%Y-%m-%d %H:%M:%S')
    t += str(listings[i][1])+" "+"("+str(listings[i][2])+")"+"\n"+"Просрочено на: "+str(delta.days)+" дней,"#+str(delta.hours)+"часов"+"\n"
    labeln.config(text=t)

t1=''
#for i in range(len(listings)):
#    t += str(listings[i][2]) + "\n"
#    label2.config(text=t)

root.geometry("500x300+900+600")
root.mainloop()
###################################
window.mainloop()
root1.mainloop(  )


def datastructuring(data, timeout):
    # import packages
    import numpy as np
    import pandas as pd
    import time, tqdm
    import geopy.geocoders  # GeoPy - see https://pypi.org/project/geopy/
    from geopy.geocoders import Nominatim # retrieve coordinates from addresses etc.
    geopy.geocoders.options.default_user_agent = 'my_app/1'
    geopy.geocoders.options.default_timeout = timeout

    # read data
    raw_data = pd.DataFrame(data)

    # Keep the columns we want to keep and name them
    sorted_data = raw_data.iloc[:,[0,2,3,4,7,8,12,14,15,16,18,21]]
    sorted_data.columns = ['Address', 'Rooms', 'Area', 'Land_area', 'Owner_expense',
                           'Energy_mark', 'Price', 'Days_on_market',
                           'Zip_code', 'Town', 'Price_development', 'Sqm_price']

    # Unpack all latitudes and longitudes and add to dataframe
    longitude = []
    latitude = []
    raw_data['geometry']

    for i, row in raw_data['geometry'].iteritems():
        if type(row) == str:
            coordinates = row.split('[', 1)[1]  # after '['
            longitude.append(float(coordinates.split(',', 1)[0]))  # before ','
            lat = coordinates.split(', ', 1)[1]  # after ', '
            latitude.append(float(lat.split(']', 1)[0]))  # before ']'
        else:
            longitude.append(None)
            latitude.append(None)
    # Add latitude and longitude to sorted dataframe.
    sorted_data.insert(loc=0, column='Latitude', value=latitude)
    sorted_data.insert(loc=0, column='Longitude', value=longitude)

    # Convert zip_code from str to int
    sorted_data['Zip_code'] = sorted_data['Zip_code'].astype(int, inplace=True)
    # Sort only zipcodes from Copenhagen
    cph = sorted_data[(sorted_data.Zip_code < 3000)].copy()
    cph.reset_index(inplace=True, drop=True)

    # Make a column of indexes for energy mark
    energysaving = []
    for i, row in cph['Energy_mark'].iteritems():
        if row == 'G':
            energysaving.append(0)
        elif row == 'F':
            energysaving.append(1)
        elif row == 'E':
            energysaving.append(2)
        elif row == 'D':
            energysaving.append(3)
        elif row == 'C':
            energysaving.append(4)
        elif row == 'B':
            energysaving.append(5)
        elif row == 'A':
            energysaving.append(6)
        elif row == 'A10':
            energysaving.append(7)
        elif row == 'A15':
            energysaving.append(8)
        elif row == 'A20':
            energysaving.append(9)
        else:
            energysaving.append(None)
    cph.insert(loc=0, column='Energy_saving', value=energysaving)

    # Set all missing values with energy_saving to mean.
    cph.Energy_saving.fillna(4.0, inplace=True)  # impute mean to 424 missing observations (11 pct.)

    # Create 'floor' variable
    floor = []
    for i, row in cph['Address'].iteritems():
        if ',' in row:
            sec_part = row.split(', ', 1)[1] # split once, keep 2nd part
            if sec_part[:2].isdigit():  # 399 with two or more digits (unindentified floor, 362 >= '20')
                floor_int = int(sec_part[0])  # assume 1st digit indicates floor
                floor.append(floor_int)
            elif sec_part[0].isdigit():
                floor_int = int(sec_part[0])
                floor.append(floor_int)
            else:
                floor.append(int(0))
        else:
            floor.append(int(0))
    cph.insert(loc=0, column='Floor', value=floor)

    ##########################################################################
    #           Code missing latitudes and longitudes using GeoPy            #
    ##########################################################################
    address_simple = []
    for i, row in cph['Address'].iteritems():
        if 'George Marshalls Vej' in row:
            address_simple.append('Fiskerihavnsgade 8')
        elif 'Amerika Plads' in row:
            address_simple.append(row.replace('Plads', 'Pl.'))
        elif 'HUSBÅD' in row:
            address_simple.append(row.split(' - HUSBÅD', 1)[0])  # Keep first part
        else:
            address_simple.append(row.split(',', 1)[0]) # split once, keep 1st part
    cph.insert(loc=0, column='Address_simple', value=address_simple)

    town_simple = []
    for i, row in cph['Town'].iteritems():
        if 'København' in row:
            town_simple.append('København')  # Keep 'København' only
        elif 'Nordhavn' in row:
            town_simple.append('København')  # Keep 'København' only
        else:
            town_simple.append(row)
    cph.insert(loc=0, column='Town_simple', value=town_simple)
    cph['Full_address'] = cph['Address_simple'].map(str) + ', ' + cph['Zip_code'].map(str) + ' ' + cph['Town_simple']

    # Thoose with missing latitude and longitude from scrape
    cph['Missing'] = cph['Longitude'].isnull()
    cph['Full_add'] = cph['Full_address']*cph['Missing']

    # Retrieve coordinates from column of addresses
    geolocator = Nominatim()
    # geolocator.headers  # check header
    # geolocator.timeout  # check time_out
    lati = []
    longi = []

    for row in tqdm.tqdm(cph['Full_add']):
        row_string = str(row)
        if len(row_string) > 0:
            location = geolocator.geocode(row_string)
            if isinstance(location, geopy.location.Location):
                lati.append(float(location.latitude))
                longi.append(float(location.longitude))
            else:
                print('Not found: ',row_string)
                lati.append(None)
                longi.append(None)
        else:
            lati.append(None)
            longi.append(None)
    cph.insert(loc=0, column='Lati', value=lati)
    cph.insert(loc=0, column='Longi', value=longi)
    cph['Latitude'] = cph['Latitude'].fillna(cph['Lati'])
    cph['Longitude'] = cph['Longitude'].fillna(cph['Longi'])
    cph.isnull().sum()
    # Drop columns
    # cph = cph.drop(['Longi', 'Lati', 'Town_simple', 'Address_simple',
    #     'Energy_mark', 'Town', 'Full_address', 'Missing', 'Full_add'], axis=1)

    ##########################################################################
    #                      Append municipality onto dataset                  #
    ##########################################################################
    # Get data from Statistics Denmark with zip_code:
    # Get zip codes and municipalities
    url_post = 'https://www.dst.dk/ext/4393839853/0/kundecenter/Tabel-Postnumre-kommuner-og-regioner--xlsx'
    df_muni = pd.read_excel(url_post)
    df2_muni = df_muni[4:]
    df2_muni.rename(columns={'Postnumre, kommuner og regioner, 1.1.2016':'Zip','Unnamed: 1':'Municipality','Unnamed: 2':'Region'}, inplace=True)

    # Split data: we want to seperate zip code and village as well as municipality number and municipality
    zip_split = pd.DataFrame(df2_muni.Zip.str.split(' ',1).tolist(),
                                       columns = ['Zip','Village'])

    mun_split = pd.DataFrame(df2_muni.Municipality.str.split(' ',1).tolist(),
                                       columns = ['Mun. no.','Municipality'])

    merge = pd.concat([zip_split, mun_split], axis=1, sort=False)

    #Construct new variable that only contain municpalities with zip code below 3000
    mun_zip = merge[['Zip','Municipality']]
    mun_zip['Int zip'] = mun_zip['Zip'].astype(int)
    our_sample = mun_zip[(mun_zip['Int zip'] < 3000)]
    # Drop string zip_code column:
    our_sample.drop('Zip', axis=1 ,inplace=True)
    # Give common name to this datasets zip code and our:
    our_sample.rename(columns={'Int zip': 'Zip_code'}, inplace=True)
    # Keep last duplicate of kommunes
    our_sample.drop_duplicates(keep='last', subset='Zip_code', inplace=True)
    # Now merge datasets on zip code:
    cph_merged= pd.merge(cph, our_sample, on='Zip_code', how='left')

    ##########################################################################
    #                 Sort columns in order which makes sence                #
    ##########################################################################
    cph_kom =cph_merged.reindex(columns=['Address', 'Zip_code', 'Municipality',
        'Latitude', 'Longitude', 'Floor', 'Rooms', 'Area', 'Land_area',
        'Price', 'Sqm_price', 'Price_development', 'Owner_expense',
        'Energy_saving', 'Days_on_market'])

    ##########################################################################
    #                   Other measures of (total) expenses                   #
    ##########################################################################
    # Create log variable of square meter price
    cph_kom.insert(loc=11, column='log_sqm_price', value=np.log(cph_kom.Sqm_price))

    # Owner expenses pr. square meter
    cph_kom.insert(loc=14, column='Sqm_owner_expenses', value=np.divide(cph_kom.Owner_expense, cph_kom.Area))

    # Standard-Finance (total yearly expenses)
    yearly_expenses = []
    first_year_expenses = []
    for houseprice, ownerexp in zip(cph_kom.Price, cph_kom.Owner_expense):
        """Vi antager at køberne selvfinansiere de 20 % af købssummen således:"""
        remaining = houseprice % 5000  # rest, når der deles med 5.000
        price = houseprice*0.05
        if remaining < 2500:
            price = int(price / 5000) * 5000  # runder ned
        else:
            price = int((price + 5000) / 5000) * 5000  # runder op
        Cashticket = max(price,25000)  # Kontant udbetaling på 5% af den kontante købesum oprundet til nærmeste kr. 5.000 dog minimum kr. 25.000.
        Mortgage = (houseprice*0.8)*0.03  # Der lånes 80% i realkreditinstitutet til en ÅOP på 3% efter skat.
        Bankloan = (houseprice*0.2 - Cashticket)*0.066  # De resterende ca. 15% lånes i banken til en ÅOP på 6.6% efter skat banklånet
        yearly_expenses.append(12*ownerexp + Bankloan + Mortgage)
        first_year_expenses.append(12*ownerexp + Bankloan + Mortgage + Cashticket)
    cph_kom.insert(loc=15, column='Yearly_expenses', value=yearly_expenses)
    cph_kom.insert(loc=16, column='First_year_expenses', value=first_year_expenses)
    sqm_yearly_exp = (cph_kom.Yearly_expenses / cph_kom.Area)
    cph_kom.insert(loc=17, column='Sqm_yearly_expenses', value=(sqm_yearly_exp))
    cph_kom.insert(loc=18, column='log_sqm_yearly_exp', value=np.log(sqm_yearly_exp))

    print(cph_kom.isnull().sum())

    return cph_kom

# [Search for a single item in GeoPy:]
# import packages
# !pip3 install tqdm
# !pip3 install geopy
# import numpy as np
# import pandas as pd
# import time, tqdm
# import geopy.geocoders  # GeoPy - see https://pypi.org/project/geopy/
# from geopy.geocoders import Nominatim # retrieve coordinates from addresses etc.
# geopy.geocoders.options.default_user_agent = 'my_app/1'
# geolocator = Nominatim()
# location = geolocator.geocode('Sølvgade Skole')
#
# if isinstance(location, geopy.location.Location):
#     print('Address:', location.address,
#         '\nLatitude:', location.latitude,
#         '\nLongitude:', location.longitude)
# else:
#     print(type(location),
#     '\nWas found:', isinstance(location, geopy.location.Location))


import csv
from matplotlib import pylab
from pylab import *
import pylab
import numpy as np
import uuid
from numpy import vstack,array
from scipy.cluster.vq import *
from flask import Flask,render_template,request
import pypyodbc
import time
import random
import urllib
import datetime
import json

app = Flask(__name__)
coloumn_names = ["pclass","survived","name","sex","age","sibsp","parch","ticket","fare","cabin","embarked","boat","body","home.dest"]
myfile = open("titanic3.csv","r")
csv_reader = csv.DictReader(myfile, fieldnames=coloumn_names)
next(csv_reader)

@app.route('/')
def index():
  return render_template('index.html')

mylist = []
@app.route('/kmeans', methods=['GET', 'POST'])
def main():
        attribute1 = request.form['attribute1']
        attribute2 = request.form['attribute2']
        clusters = request.form['clusters']
        K_clusters = int(clusters)
        mylist = getdata(attribute1,attribute2)
        data = []
        cdist=[]
        data = array(mylist)
        cent, pts = kmeans2(data,K_clusters)
        disCluster = []
        for i in range(len(cent)):
            x1 = cent[i][0]
            y1 = cent[i][1]
            x1 = float("{0:.3f}".format(x1))
            y1 = float("{0:.3f}".format(y1))

            for j in range(i+1,len(cent)):
                dc = {}
                x2 = cent[j][0]
                y2 = cent[j][1]
                x2 = float("{0:.3f}".format(x2))
                y2 = float("{0:.3f}".format(y2))
                dist = np.sqrt((x1-x2)*2 + (y1-y2)*2)
                cdist.append(dist)
                dc['dist'] = "Distance between cluster " + str(i) + " and cluster " + str(j) + " is: " + str(dist)
                disCluster.append(dc)
                print (disCluster)
                print ("Distance between cluster " + str(i) + " and cluster " + str(j) + " is: " + str(dist))
        clr = ([1, 1, 0.0],[0.2,1,0.2],[1,0.2,0.2],[0.3,0.3,1],[0.0,1.0,1.0],[0.6, 0.6,0.1],[1.0,0.5,0.0],[1.0,	0.0, 1.0],[0.6,	0.2, 0.2],[0.1,0.6,0.6],[0.0,0.0,0.0],[0.8,1.0,1.0],[0.70,0.50,0.50],[0.5,0.5,0.5],[0.77,0.70,0.00])
        colors = ([(clr)[i] for i in pts])
        clr_dict = {"yellow":0,"green":0,"red":0,"blue":0,"cyan":0}
        pdict=[]
        for x in colors:
            if str(x) == "[1, 1, 0.0]":
                clr_dict["yellow"] += 1
            if str(x) == "[0.2, 1, 0.2]":
                clr_dict["green"] += 1
            if str(x) == "[1, 0.2, 0.2]":
                clr_dict["red"] += 1
            if str(x) == "[0.3, 0.3, 1]":
                clr_dict["blue"] += 1
            if str(x) == "[0, 1.0, 1.0]":
                clr_dict["cyan"] += 1
            if str(x) == "[0.6, 0.6,0.1]":
                clr_dict["deepolive"] += 1
            if str(x) == "[1.0,	0.5, 0.0]":
                clr_dict["orange"] += 1
            if str(x) == "[1.0,	0.0, 1.0]":
                clr_dict["magenta"] += 1
            if str(x) == "[0.6,	0.2, 0.2]":
                clr_dict["ruby"] += 1
            if str(x) == "[0.1,	0.6, 0.6]":
                clr_dict["deepteal"] += 1
            if str(x) == "[0.0,	0.0, 0.0]":
                clr_dict["black"] += 1
            if str(x) == "[0.8,	1.0, 1.0]":
                clr_dict["palecyan"] += 1
            if str(x) == "[0.70, 0.50,	0.50]":
                clr_dict["dirtyviolet"] += 1
            if str(x) == "[0.5,	0.5, 0.5]":
                clr_dict["gray"] += 1
            if str(x) == "[0.77, 0.70, 0.00]":
                clr_dict["olive"] += 1

        f_write='Cluster,Count\r\n'
        cnt=0
        print (clr_dict)
        for i in clr_dict:
            if clr_dict[i] == 0:
                continue
            string = str(cnt) + " : " + str(clr_dict[i])
            pdict.append(string)
            print ("No of points in cluster with " + str(i) + " is: " + str(clr_dict[i]))
            f_write+= str(cnt)+','+str(clr_dict[i])+'\r\n'
            cnt += 1
        with open("templates/d3chart.csv",'wb') as nfile:
            nfile.write(f_write.encode("utf-8"))
        pylab.scatter(data[:,0],data[:,1], c=colors)
        pylab.scatter(cent[:,0],cent[:,1], marker='o', s = 400, linewidths=3, c='none')
        pylab.scatter(cent[:,0],cent[:,1], marker='x', s = 400, linewidths=3)

        pylab.savefig("static/kmeans6.png")

        return render_template('index.html',cdist=cdist,pdict=pdict, disCluster = disCluster)

def getdata(attr1,attr2):
    c = 0
    for row in csv_reader:
        c += 1
        if c == 5000:
            break
        pair = []
        if row[attr1] == "":
            row[attr1] = 0
        if row[attr2] == "":
            row[attr2] = 0
        x = float(row[attr1])
        y = float(row[attr2])
        pair.append(x)
        pair.append(y)
        mylist.append(pair)
    return mylist

@app.route('/show', methods=['GET', 'POST'])
def show():
  return render_template('show.html')

@app.route('/Bargraph', methods=['GET', 'POST'])
def bargraph():
  return render_template('d3barchart.html')

@app.route('/Piegraph', methods=['GET', 'POST'])
def Piegraph():
  return render_template('d3piechart.html')

if __name__ == '__main__':
  app.run()


import csv
import numpy as np
import torch
import ignite.handlers as hdlrs
from ignite.engine import Events
from ignite.handlers import EarlyStopping
from tqdm import tqdm

class ModelCheckpoint(hdlrs.ModelCheckpoint):
    @property
    def best_model(self):
        with open(self._saved[-1][1][0], mode='rb') as f:
            state_dict = torch.load(f)

        return state_dict

class LRScheduler(object):
    def __init__(self, scheduler, loss):
        self.scheduler = scheduler
        self.loss = loss

    def __call__(self, engine):
        loss_val = engine.state.metrics[self.loss]
        self.scheduler.step(loss_val)

    def attach(self, engine):
        engine.add_event_handler(Events.COMPLETED, self)
        return self

class Tracer(object):
    def __init__(self, val_metrics, save_path=None, save_interval=1):
        self.metrics = ['loss']
        self.loss = []
        self._batch_trace = []
        self.save_path = save_path
        self.save_interval = save_interval

        template = 'val_{}'
        for k in val_metrics:
            name = template.format(k)
            setattr(self, name, [])
            self.metrics.append(name)

    def _initalize_traces(self, engine):
        for k in self.metrics:
            getattr(self, k).clear()

    def _save_batch_loss(self, engine):
        self._batch_trace.append(engine.state.output)

    def _trace_training_loss(self, engine):
        avg_loss = np.mean(self._batch_trace)
        self.loss.append(avg_loss)
        self._batch_trace.clear()

    def _trace_validation(self, engine):
        metrics = engine.state.metrics
        template = 'val_{}'
        for k, v in metrics.items():
            trace = getattr(self, template.format(k))
            trace.append(v)

    def attach(self, trainer, evaluator=None):
        trainer.add_event_handler(Events.STARTED, self._initalize_traces)
        trainer.add_event_handler(
            Events.ITERATION_COMPLETED, self._save_batch_loss)
        trainer.add_event_handler(
            Events.EPOCH_COMPLETED, self._trace_training_loss)

        if evaluator is not None:
            evaluator.add_event_handler(
                Events.COMPLETED, self._trace_validation)

        if self.save_path is not None:
            trainer.add_event_handler(
                Events.EPOCH_COMPLETED, self._save_at_interval)

        return self

    def _save_at_interval(self, engine):
        if engine.state.iteration % self.save_interval == 0:
            self.save_traces()

    def save_traces(self):
        for loss in self.metrics:
            trace = getattr(self, loss)
            with open('{}/{}.csv'.format(self.save_path, loss), mode='w') as f:
                wr = csv.writer(f, quoting=csv.QUOTE_ALL)
                for i, v in enumerate(trace):
                    wr.writerow([i + 1, v])

class Logger(object):
    def __init__(self, loader, log_interval, pbar=None, desc=None):
        n_batches = len(loader)
        self.desc = 'iteration-loss: {:.5f}' if desc is None else desc
        self.pbar = pbar or tqdm(
            initial=0, leave=False, total=n_batches,
            desc=self.desc.format(0)
        )
        self.log_interval = log_interval
        self.running_loss = 0
        self.n_batches = n_batches

    def _log_batch(self, engine):
        self.running_loss += engine.state.output

        iter = (engine.state.iteration - 1) % self.n_batches + 1
        if iter % self.log_interval == 0:
            self.pbar.desc = self.desc.format(
                engine.state.output)
            self.pbar.update(self.log_interval)

    def _log_epoch(self, engine):
        self.pbar.refresh()
        tqdm.write("Epoch: {} - avg loss: {:.5f}"
            .format(engine.state.epoch, self.running_loss / self.n_batches))
        self.running_loss = 0
        self.pbar.n = self.pbar.last_print_n = 0

    def _log_validation(self, engine):
        metrics = self.evaluator.state.metrics

        message = []
        for k, v in metrics.items():
            message.append("{}: {:.5f}".format(k, v))
        tqdm.write('\tvalidation: ' + ' - '.join(message))

    def attach(self, trainer, evaluator=None, metrics=None):
        trainer.add_event_handler(Events.ITERATION_COMPLETED, self._log_batch)
        trainer.add_event_handler(Events.EPOCH_COMPLETED, self._log_epoch)
        trainer.add_event_handler(Events.COMPLETED, lambda x: self.pbar.close())

        if evaluator is not None and metrics is None:
            raise ValueError('')

        if evaluator is not None:
            self.evaluator = evaluator
            trainer.add_event_handler(Events.EPOCH_COMPLETED, self._log_validation)

        return self


import glob
import os
import numpy as np
from skimage import io, transform

def read_img(path, weight, height, dimension):
    cate = [path + x for x in os.listdir(path) if
            os.path.isdir(path + x)]

    images = []
    labels = []
    for idx, folder in enumerate(cate):
        print(folder)
        for im in glob.glob(folder + '/*.jpg'):

            img = io.imread(im)
            # weight * height * 3(RGB)
            img = transform.resize(img, (weight, height, dimension))
            # print(img.shape)
            images.append(img)
            # print(images)

            labels.append(idx)

    return np.asarray(images, np.float32), np.asarray(labels, np.int32)

# 打乱 & 分割数据
# input :
# data(样本图像), label(样本标签), ratio(训练样本 / 测试样本)
# return :
# [x_train(训练样本), y_train(训练样本标签), x_val(测试样本), y_val(测试样本标签)]
def shuffle_and_period(data, label, ratio):
    num_example = data.shape[0]
    arr = np.arange(num_example)
    np.random.shuffle(arr)
    data = data[arr]
    label = label[arr]

    s = np.int(num_example * ratio)

    # 训练
    x_train = data[:s]
    y_train = label[:s]

    # 验证
    x_val = data[s:]
    y_val = label[s:]

    return x_train, y_train, x_val, y_val

#  定义批量提取数据函数 :
#  inputs为输入数据
#  targets为标签数据
#  batch_size为每一个batch数据集的大小
#  shuffle代表是否打乱
def mini_batches(inputs=None, targets=None, batch_size=None,
                 shuffle=False):
    assert len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)
    for start_idx in range(0, len(inputs) - batch_size + 1, batch_size):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batch_size]
        else:
            excerpt = slice(start_idx, start_idx + batch_size)
        yield inputs[excerpt], targets[excerpt]

# 用于测试读取数据
def read_one_image(path, weight, height):
    img = io.imread(path)
    img = transform.resize(img, (weight, height))
    return np.asarray(img)