import re
import time
import pickle
import pandas as pd
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
from collections import Counter, defaultdict

class Parse:

    def __init__(self):
        self.stop_words = stopwords.words('english')
        self.dictionary = defaultdict(set)
        self.tweets_words_locations = dict()
        self.capitals_counter = Counter()
        self.words_dual_representation = []
        self.words_capital_representation = []
        self.seen_capital = set()

    def parse_sentence(self, text, do_stem: bool):
        """
        This function tokenize, remove stop words and apply lower case for every word within the text
        :param text:
        :return:
        """
        text_tokens = self.number_tokenizer(text)  # parse percent
        if "percent" in text_tokens or "percentage" in text_tokens:
            text_tokens = self.percentage_tokenizer(text_tokens)  # parse numbers

        ans = ""
        string = ""
        for w in text_tokens.split():
            ans = self.remove_single_chars(w)
            if "http" in w or "https" in w or "www" in w:
                ans = self.url_tokenizer(ans)
            elif "-" in ans or "/" in ans or "&" in ans:
                ans = self.hyphen_fixer(ans)
            elif "#" in ans or "_" in ans:  # parse hashtags
                ans = self.hash_tag_tokenizer(ans)

            string = string + " " + ans

        text_tokens = word_tokenize(string)  # tokenization

        # if stemming is necessary
        if do_stem:
            text_tokens = self.stemmer(text_tokens)
           # print("after stemming:", text_tokens)

        text_tokens_without_stopwords = []#[w for w in text_tokens if w.lower() not in self.stop_words]
        for w in text_tokens:
            if w.lower() not in self.stop_words:
                text_tokens_without_stopwords.append(w)
                if w[0].isupper():
                    self.seen_capital.add(w)

        self.capitals_counter.update(text_tokens_without_stopwords)

        return text_tokens_without_stopwords

    def parse_doc(self, doc_as_named_tuple, do_stem: bool = False):
        """
        This function takes a tweet document as list and break it into different fields
        :param doc_as_list: list re-preseting the tweet.
        :return: Document object with corresponding fields.
        """
        tweet_id = doc_as_named_tuple.tweet_id
        full_text = doc_as_named_tuple.full_text
        tokenized_text = self.parse_sentence(full_text, do_stem)

        # the following dict will hold the words locations for a specific tweet
        tweet_words_locations = defaultdict(list)

        # add words to dict
        for location, term in enumerate(tokenized_text):
                self.dictionary[term].add(tweet_id)
                tweet_words_locations[term].append(location)

        # keep tweet words locations
        self.tweets_words_locations[tweet_id] = tweet_words_locations

    def parse_batch_of_docs(self, df: pd.DataFrame):
        """

        :param df:
        :return:
        """
        # parse each tweet and insert result to a document
        for row in df.itertuples():
            self.parse_doc(row)

    def parse_corpus(self, dfs: list):
        """
        :param df:
        :return:
        """
        while dfs:
            df = dfs.pop()
            print(f"batch size to parse: {df.shape[0]} {time.ctime()}")
            self.parse_batch_of_docs(df)
            print(f'finish parse batch {time.ctime()}')

        # fetch words with capital letter
        print('tokens seen with capital', len(self.seen_capital))
        print('total number of tokens seen', len(self.capitals_counter))
        self.words_dual_representation = set([word for word in self.seen_capital if word.lower() in self.capitals_counter.keys()])
        self.words_capital_representation = self.seen_capital-self.words_dual_representation

        # save words counter as pickle
        with open('words_counter.pickle', 'wb') as handle:
            pickle.dump(self.capitals_counter, handle, protocol=pickle.HIGHEST_PROTOCOL)
        del self.capitals_counter
        print('capital councapitals_counterter as been deleted')

        # change words to all capital
        for capital in self.words_capital_representation: # list of words need to be change appearnce to capital
            self.dictionary[capital.upper()] = self.dictionary.pop(capital)

        # change words to all lower
        for capital in self.words_dual_representation:
            self.dictionary[capital.lower()].union(self.dictionary.pop(capital))

    def hash_tag_tokenizer(self, word: str) -> str:
        string = ""

        # if word == "RT":
        #     continue
        string = word
        word = word[1:]
        if "_" in word:
            split_by_underscore = " ".join(word.split("_"))
            string = string + " " + split_by_underscore
            return string
        else:
            split_by_caps = " ".join([a for a in re.split('([A-Z][a-z]+)', word) if a])
            string = string + " " + split_by_caps
            return string

        return word

    def url_tokenizer(self, w: str) -> str:
        string = ""
        url = []
        # if len(w) < 2:
        #     continue
        url = (re.split(r"\W+", w))
        url_to_string = ' '.join(url)
        string = url_to_string

        return string

    def stemmer(self, tokens: str) -> str:
        ps = PorterStemmer()
        string = ""
        for word in tokens.split():
            string = string + ps.stem(word) + " "

        return string

    def percentage_tokenizer(self, tokens: str) -> str:
        string = ""
        ans = False
        for w in tokens.split():
            if w == "RT":
                continue
            if ans:
                if w == "percent" or w == "percentage":
                    string = string + "%" + " "
                    ans = False
                    continue
                else:
                    string = string + " "
            try:
                if float(w):
                    string = string + w
                    ans = True
                    continue
            except ValueError:
                string = string + w + " "

        return string

    def number_tokenizer(self, tokens: str) -> str:
        string = ""
        kmb = ""
        number = False
        for w in tokens.split():
            if w == "RT":
                continue
            if number:
                if w == "Thousand":
                    kmb = kmb + "K"
                    if kmb == "KK":
                        string = string[:-1] + "M"
                        kmb = ""
                        continue
                    elif kmb == "MK" or kmb == "KM":
                        string = string[:-1] + "B"
                        kmb = ""
                        continue
                    else:
                        string = string + "K"
                        continue
                elif w == "Million":
                    kmb = kmb + "M"
                    if kmb == "MK" or kmb == "KM":
                        string = string[:-1] + "B"
                        kmb = ""
                        continue
                    else:
                        string = string + "M"
                        continue
                elif w == "Billion":
                    string = string + "B"
                    continue
                else:
                    string = string + " "
                    number = False
            kmb = ""
            try:
                if float(w):
                    num = float(w)
                    if num < 1000:
                        number = True
                        string = string + w
                        continue
                    elif num < 1000000:
                        num = num / 1000
                        num = round(num, 3)
                        num = str(num)
                        if num.endswith(".0"):
                            num = num.replace(".0", "")
                        string = string + num + "K"
                        kmb = kmb + "K"
                        number = True
                        continue
                    elif num < 1000000000.00:
                        num = num / 1000000
                        num = round(num, 3)
                        num = str(num)
                        if num.endswith(".0"):
                            num = num.replace(".0", "")
                        string = string + num + "M"
                        kmb = kmb + "M"
                        number = True
                        continue
                    elif num >= 1000000000:
                        num = num / 1000000000.000
                        num = round(num, 3)
                        num = str(num)
                        if num.endswith(".0"):
                            num = num.replace(".0", "")
                        string = string + num + "B"
                        number = True
                        continue
            except:
                string = string + w + " "

        return string

    def remove_single_chars(self, w: str) -> str:
        string = ""
        w = re.sub(r'[^a-zA-Z0-9\s@#]', '', w)
        # w = w.replace("!", "").replace("?", "").replace(":", "").replace("’", "").replace("…", "").replace(";", "")
        # w = w.replace('"', '').replace("”", "").replace("'", "").replace("*", "").replace("😂", "").replace("“", "")
        # w = w.replace("❤", "").replace("😭", "").replace(",", "")
        if "." in w:
            try:
                if float(w):
                    []
            except:
                w = w.replace(".", "")
        if len(w) > 1 or w.isdigit():
            string = w

        return string

    def hyphen_fixer(self, w: str) -> str:
        string = ""

        if "-" in w:
            # dual_string = w
            string = " ".join(w.split("-")) + " "
        elif "/" in w:
            # dual_string = w
            string = " ".join(w.split("/")) + " "
        elif "&" in w:
            # dual_string = w
            string = " ".join(w.split("&")) + " "
        else:
            string = w
        return string


from keras.engine import Layer
from keras import backend as K
from keras import initializers

class Multi_Dim_Attention(Layer):
    """
    2D attention from "A Structured Self-Attentive Sentence Embedding" (2017)
    """

    def __init__(self, ws1, ws2, punish, init='glorot_normal', **kwargs):
        self.kernel_initializer = initializers.get(init)
        self.weight_ws1 = ws1
        self.weight_ws2 = ws2
        self.punish = punish
        super(Multi_Dim_Attention, self).__init__(** kwargs)

    def build(self, input_shape):
        self.Ws1 = self.add_weight(shape=(input_shape[-1], self.weight_ws1),
                                   initializer=self.kernel_initializer,
                                   trainable=True,
                                   name='{}_Ws1'.format(self.name))
        self.Ws2 = self.add_weight(shape=(self.weight_ws1, self.weight_ws2),
                                   initializer=self.kernel_initializer,
                                   trainable=True,
                                   name='{}_Ws2'.format(self.name))
        self.batch_size = input_shape[0]
        super(Multi_Dim_Attention, self).build(input_shape)

    def compute_mask(self, input, input_mask=None):
        return None

    def call(self, x, mask=None):
        uit = K.tanh(K.dot(x, self.Ws1))
        ait = K.dot(uit, self.Ws2)
        ait = K.permute_dimensions(ait, (0, 2, 1))
        A = K.softmax(ait, axis=1)
        M = K.batch_dot(A, x)
        if self.punish:
            A_T = K.permute_dimensions(A, (0, 2, 1))
            tile_eye = K.tile(K.eye(self.weight_ws2), [self.batch_size, 1])
            tile_eye = K.reshape(
                tile_eye, shape=[-1, self.weight_ws2, self.weight_ws2])
            AA_T = K.batch_dot(A, A_T) - tile_eye
            P = K.l2_normalize(AA_T, axis=(1, 2))
            return M, P
        else:
            return M

    def compute_output_shape(self, input_shape):
        if self.punish:
            out1 = (input_shape[0], self.weight_ws2, input_shape[-1])
            out2 = (input_shape[0], self.weight_ws2, self.weight_ws2)
            return [out1, out2]
        else:
            return (input_shape[0], self.weight_ws2, input_shape[-1])


from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import RandomForestClassifier

def predict(parms, dataDict, typ):
    assert typ in ["C", "R"], "specify Classifier or Regressor with 'C' or 'R'"

    if typ == "R":
        m = RandomForestRegressor(**parms)
    else:
        m = RandomForestClassifier(**parms)
    model = m.fit(dataDict["trainX"], dataDict["trainY"])
    preds = model.predict(dataDict["testX"])
    return model, preds


#!/usr/bin/env python
# coding=utf-8
# author: zengyuetian
# 此代码仅供学习与交流，请勿用于商业用途。
# 获取城市挂牌二手房数量

import time
from lib.spider.base_spider import SPIDER_NAME
from bs4 import BeautifulSoup
from lib.zone.city import cities
import requests

numbers = dict()

def get_ershou_number(city):
    url = "https://{0}.{1}.com/ershoufang/".format(city, SPIDER_NAME)
    print(url)
    response = requests.get(url)
    html = response.content
    soup = BeautifulSoup(html, "lxml")
    element = soup.find('h2', class_='total')
    number = int(element.text.split(" ")[1].strip())
    numbers[city] = number

if __name__ == '__main__':
    start = time.time()
    for key, value in cities.items():
        # print(key, value)
        get_ershou_number(key)
    for k, v in numbers.items():
        print(cities[k], v)
    print("cost {0} seconds".format(time.time() - start))


import streamlit as st
import pandas as pd
import numpy as np
import matplotlib as plt
from pandas_profiling import ProfileReport
from streamlit_pandas_profiling import st_profile_report

# Page Confurigation
st.set_page_config(
    page_title = 'Home'
)
# Title
st.markdown('''
## **Exploratory Data Analysis with Python**
### So what is EDA ?
- Exploratory Data Analysis is a part in Data Analysis of performing initial investigations in data in order to discover patterns , check for outliers or to make hypothesis and check assumptions with the **power** of Statistics and graphical representations.
''')

# SideBar
with st.sidebar.header('Upload CSV Data'):
    uploaded_file = st.sidebar.file_uploader("Drag and drop your CSV Files Here")

# Context
st.markdown('''
### Let's begin with an Example Data :
To to EDA with your data, upload files in SideBar.
''')

# Pandas_profiling
if uploaded_file is not None:
    # Reading CSV
    @st.cache
    def load_csv():
        csv = pd.read_csv(uploaded_file)
        return csv
    df = load_csv()
    pr = ProfileReport(df , explorative = True)
    st.table(df.head())
    st_profile_report(pr)
else :
    st.info('Awaiting File to be Uploaded')
    if st.button('Example DataSet'):
        @st.cache
        def load_data():
            csv = pd.read_csv('./Datasets/Sample - Superstore.csv')
            return csv
        df = load_data()
        pr = ProfileReport(df , explorative = True)
        st.header("Example of SuperStore Orders -->")
        st.table(df.head())
        st_profile_report(pr)


import numpy as np
from simple_dqn import Agent
from collections import deque
import gym
import os
import sys
import argparse

parent_path = os.path.join(sys.path[0], '../')
sys.path.insert(1, parent_path)

from utils import preprocess_frame_dqn

def main(args):
    env_name = args.env_name
    env = gym.make(env_name)
    env.seed(0)

    num_games = 5
    load_checkpoint = True
    best_score = 0
    agent = Agent(gamma=0.99, epsilon=0.0, alpha=0.0001,
                  input_dims=(104,80,4), n_actions=env.action_space.n, mem_size=25000,
                  eps_min=0.02, batch_size=32, replace=1000, eps_dec=1e-5, env_name=env_name)

    try:
        agent.load_models()
    except:
        print('No DQN models found for %s in models folder' % env_name)
        raise

    scores, eps_history = [], []
    n_steps = 0

    for i in range(num_games):
        done = False
        observation = env.reset()
        frame_queue = deque(maxlen=4)

        observation = preprocess_frame_dqn(observation)
        for j in range(4):
            frame_queue.append(observation)
        observation = np.concatenate(frame_queue, axis=2)

        score = 0
        while not done:
            action = agent.choose_action(observation)
            next_frame, reward, done, info = env.step(action)

            n_steps += 1
            score += reward

            frame_queue.pop()
            frame_queue.appendleft(preprocess_frame_dqn(next_frame))

            observation_ = np.concatenate(frame_queue, axis=2)

            observation = observation_

        scores.append(score)

        avg_score = np.mean(scores[-100:])
        print('episode: ', i,'score: ', score, ' average score %.3f' % avg_score, 'epsilon %.2f' % agent.epsilon, 'steps', n_steps)

        eps_history.append(agent.epsilon)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=('Test DQN'))
    parser.add_argument('--env_name', type=str, help='name of environment', default="PongDeterministic-v4")
    args = parser.parse_args()
    main(args)


from typing import List
from functools import cache
import sys
from collections import defaultdict

class Solution:
	def pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:
		if not matrix:
			return []

		R, C = len(matrix), len(matrix[0])

		def nbrs(i, j):
			return [(r, c) for r, c in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)] if 0 <= r < R and 0 <= c < C]

		def dfsVisit(i, j, visited):
			visited.add((i, j))
			for r, c in nbrs(i, j):
				if (r, c) not in visited and matrix[i][j] <= matrix[r][c]:
					dfsVisit(r, c, visited)
			visited.add((i, j))

		def visitPacific():
			visited = set()
			for j in range(C):
				if (0, j) not in visited:
					dfsVisit(0, j, visited)

			for i in range(R):
				if (i, 0) not in visited:
					dfsVisit(i, 0, visited)

			return visited

		def visitAtlantic():
			visited = set()
			for j in range(C):
				if (R-1, j) not in visited:
					dfsVisit(R-1, j, visited)

			for i in range(R):
				if (i, C-1) not in visited:
					dfsVisit(i, C-1, visited)

			return visited

		return list(visitAtlantic().intersection(visitPacific()))

print(Solution().pacificAtlantic([[1, 2, 2, 3, 5],
								  [3, 2, 3, 4, 4],
								  [2, 4, 5, 3, 1],
								  [6, 7, 1, 4, 5],
								  [5, 1, 1, 2, 4]]))


import os
import sys

def get_poopler_win32():
    import requests
    import py7zr

    if Path(Path.cwd().joinpath('lib/poppler-0.68.0')).is_dir():
        for path, directories, files in os.walk(str(Path.cwd())):
            if 'bin' in directories:
                poopler_dir = Path(path).joinpath('bin')
                return poopler_dir

    else:
        URL = 'https://blog.alivate.com.au/wp-content/uploads/2018/10/poppler-0.68.0_x86.7z'
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'}

        filename = os.path.basename(URL)
        response = requests.get(URL,  headers=headers)

        if response.status_code == 200:
            with open(filename, 'wb') as out:
                out.write(response.content)

            with py7zr.SevenZipFile(filename, mode='r') as z:
                z.extractall('lib')

            for path, directories, files in os.walk(str(Path.cwd())):
                if 'bin' in directories:
                    poopler_dir = Path(path).joinpath('bin')
                    return poopler_dir

if sys.platform == 'win32':
    from pathlib import Path

    poopler_dir = get_poopler_win32()
    pytesseract_path = Path.cwd().joinpath('lib/Tesseract-OCR/tesseract.exe')

elif sys.platform == 'linux':

    from subprocess import STDOUT, check_call
    check_call(['apt-get', 'install', '-y', 'python-distutils-extra tesseract-ocr tesseract-ocr-eng \
                        libopencv-dev libtesseract-dev libleptonica-dev python-all-dev swig libcv-dev python-opencv \
                        python-numpy python-setuptools build-essential subversion tesseract-ocr-eng tesseract-ocr-dev\
                        libleptonica-dev python-all-dev swig libcv-dev'],
               stdout=open(os.devnull, 'wb'), stderr=STDOUT)

    check_call(['apt-get', 'install', '-y', 'poppler-utils'],
               stdout=open(os.devnull, 'wb'), stderr=STDOUT)

elif sys.platform == 'darwin':

    os.system('ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 2> /dev/null')
    os.system('brew install poppler')
    os.system('brew install tesseract')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author:rachpt

from os import path, mkdir, listdir, remove
from threading import Thread
from tkinter import messagebox, Tk, Menu, Frame, PhotoImage

from utils import status, setting, mannual

class MainPage(Tk):

    PROC_PATH = path.abspath(path.realpath(__file__))
    ROOT = path.dirname(PROC_PATH)
    if not path.exists(path.join(ROOT, "config")):
        mkdir(path.join(ROOT, "config"))
    Config_Path = path.join(ROOT, "config/user_info.pickle")
    Cookie_Path = path.join(ROOT, "config/cookie.pickle")

    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)

        self.author = "rachpt"
        self.version = "v0.3.1"
        self.param_ok = False
        # self.is_checking = True
        self.Ck = Thread(target=self.check, args=())

        # 设置窗口大小
        self.X = self.winfo_screenwidth()
        self.Y = self.winfo_screenheight()
        self.geometry(
            "%dx%d+%d+%d" % (750, 540, (self.X - 750) / 2, (self.Y - 540) / 2)
        )
        self.resizable(False, False)
        self.title("自动预定羽毛球场地 - %s" % self.version)
        self.icon_path = path.join(self.ROOT, "config/favicon.gif")
        if not path.isfile(self.icon_path):
            from utils.pic import favicon_gif, get_pic

            get_pic(favicon_gif, self.icon_path)
        self.call("wm", "iconphoto", self._w, PhotoImage(file=self.icon_path))
        self.frames = {}
        self.create_page()

    def check(self):
        while True:
            if self.param_ok:
                break
            else:
                self.auto_appointmant(True)
                break

    def show_frame(self, page_name):
        """
        Show a frame for the given page name
        """
        frame = self.frames[page_name]
        frame.tkraise()

    def settings(self):
        self.show_frame("SettingPage")

    def auto_appointmant(self, auto=False):
        if auto:
            self.frames["SettingPage"].verification(auto)
        else:
            self.show_frame("RunPage")
        if self.param_ok and auto:
            self.show_frame("RunPage")
        elif not auto:
            pass
        else:
            # messagebox.showinfo('请有效配置', '--'*28+'\n还没有配置登录信息，或者登录信息有误\n请重新配置！\n')
            self.show_frame("SettingPage")

    def how_to_use(self):
        self.show_frame("HowToUsePage")

    def about(self):
        messagebox.showinfo(
            title="About",
            message="-" * 20
            + "\nAuthor：{author}\nVersion：{version}".format(
                author=self.author, version=self.version
            )
            + "\nLICENSE：MIT 许可证"
            + "\n\n更新日期：2019.12.09",
        )

    def call_back(self):
        if messagebox.askokcancel("Quit", "退出后无法自动预定，确定退出程序?\n"):
            self.destroy()
            for file_ in listdir():
                _, ext = path.splitext(file_)
                if ext == ".log":
                    remove(file_)

    def create_page(self):
        container = Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        for F in (status.RunPage, setting.SettingPage, mannual.HowToUsePage):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            """
            put all of the utils in the same location;
            the one on the top of the stacking order
            will be the one that is visible.
            """
            frame.grid(row=0, column=0, sticky="nsew")

        # 设置主菜单
        menu = Menu(self)

        # 设置子菜单
        submenu = Menu(menu, tearoff=0)
        submenu.add_command(label="设置", command=self.settings)
        menu.add_cascade(label="设置", menu=submenu)

        # 预约子菜单
        submenu = Menu(menu, tearoff=0)
        submenu.add_command(label="自动预约", command=self.auto_appointmant)
        menu.add_cascade(label="自动预约", menu=submenu)

        # 帮助子菜单
        submenu = Menu(menu, tearoff=0)
        submenu.add_command(label="使用说明", command=self.how_to_use)
        submenu.add_command(label="关于", command=self.about)
        menu.add_cascade(label="帮助", menu=submenu)

        self.config(menu=menu)

        # 起始界面
        self.show_frame("SettingPage")
        self.Ck.daemon = True
        self.Ck.start()

if __name__ == "__main__":
    app = MainPage()
    app.protocol("WM_DELETE_WINDOW", app.call_back)
    app.mainloop()


import sys
import json
import numpy as np
import time

def read_input(file_name):
    document = []
    with open(file_name, 'r', encoding='utf-8') as fp:
        for each_line in fp:
            each_line = each_line.strip()
            document.append(each_line)
    return document

def cal_transition_matrix(model_data):
    transition_list = []
    # transition_list.append(list(model_data['start_states'].values()))
    transition_states = model_data['transition_states']
    for key, val in transition_states.items():
        transition_list.append(list(val.values()))
    transition_mat = np.array(transition_list)
    return transition_mat

def cal_emission_matrix(model_data):
    emission_states = model_data['emission_states']
    emission_list = []
    for key, val in emission_states.items():
        emission_list.append(list(val.values()))
    emission_mat = np.array(emission_list)
    return emission_mat

def viterbi_decoding(line, states, transition_mat, emission_mat, initial_states, word_dict):
    time_points = line.split(' ')
    states_len = len(states)
    time_points_len = len(time_points)
    probabilty = np.zeros((states_len, time_points_len))
    back_pointer = np.zeros((states_len, time_points_len))
    if word_dict.get(time_points[0]) is not None:
        emission_index = word_dict.get(time_points[0])
        probabilty[:, 0] = initial_states * emission_mat[emission_index, :]
    else:
        probabilty[:, 0] = initial_states

    for t in range(1, time_points_len):
        for i in range(0, states_len):
            emission_val = emission_mat[word_dict.get(time_points[t])][i] if word_dict.get(time_points[t]) is not None else 1
            mul_val = probabilty[:, t-1] * transition_mat[:, i]
            probabilty[i, t] = np.max([mul_val * emission_val])
            back_pointer[i, t] = np.argmax([mul_val])
    most_proba_state = np.argmax([probabilty[:, time_points_len-1]])

    labels = []
    for j in range(time_points_len-1, -1, -1):
        labels.append(states[most_proba_state])
        most_proba_state = int(back_pointer[most_proba_state, j])
    labels.reverse()
    ans_list = []
    for i in range(0, len(labels)):
        ans_list.append(time_points[i]+'/'+labels[i])
    ans = ' '.join(ans_list)
    return ans

if __name__ == "__main__":
    input_file = sys.argv[1]
    output_file = "hmmoutput.txt"
    model_file = "hmmmodel.txt"
    document = read_input(input_file)
    start = time.time()
    with open(model_file) as f:
        model_data = json.load(f)
    length = len(model_data['transition_states'])
    transition_mat = cal_transition_matrix(model_data)
    emission_mat = cal_emission_matrix(model_data)
    words = list(model_data['emission_states'].keys())
    words_index = list(range(0, len(words)))
    word_dict = dict(zip(words, words_index))
    states = list(model_data['start_states'].keys())
    states_val = np.array(list(model_data['start_states'].values()))
    fout = open(output_file, "w")
    for line in document:
        result = viterbi_decoding(line, states, transition_mat, emission_mat, states_val, word_dict)
        fout.write('%s\n' % result)

    fout.close()
    print(time.time() - start)


#paper, sissors, rock
class Game():
    def __init__(self):
        self.players = []
        self.options = ['Paper', 'Rock', 'Scissors']
        self.choice = []

    def verify(self, winner):
        value_p1 = self.choice[0]
        value_p2 = self.choice[1]
        if value_p1 == value_p2:
            print('\nEqual result!! lets go another round...')
            winner = 0
        else:
            if value_p1 == 1 and value_p2 == 2:
                print('Congratulations {} you are the winner!!'.format(self.players[0].name))
            else:
                if value_p1 == 1 and value_p2 == 3:
                    print('Congratulations {} you are the winner!!'.format(self.players[1].name))
                else:
                    if value_p1 == 2 and value_p2 == 1:
                        print('Congratulations {} you are the winner!!'.format(self.players[1].name))
                    else:
                        if value_p1 == 2 and value_p2 == 3:
                            print('Congratulations {} you are the winner!!'.format(self.players[0].name))
                        else:
                            if value_p1 == 3 and value_p2 == 1:
                                print('Congratulations {} you are the winner!!'.format(self.players[0].name))
                            else:
                                print('Congratulations {} you are the winner!!'.format(self.players[1].name))
                winner = 1
        return winner

    def menu(self):
        print('Welcome to game of: rock, paper, scissors\n')
        #append player to game list
        for i in range(2):
            name = input('Player {} insert your name: '.format(i+1))
            self.players.append(Player(i+1, name))
        #the turns init
        winner = 0
        while winner != 1:
            for i in range(2):
                print('\n* {} select a choice: '.format(self.players[i].name))
                print('1) {}'.format(self.options[0]))
                print('2) {}'.format(self.options[1]))
                print('3) {}'.format(self.options[2]))
                option = int(input('Enter your option: '))
                self.choice.append(option)
            winner = self.verify(winner)
            self.choice.clear()

class Player():
    def __init__(self, id, name):
        self.id = id
        self.name = name

#main

if __name__ == '__main__':
    game = Game()
    game.menu()


from tkinter import *
import PyPDF2
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfile
# from functions import display_logo, display_textbox, extract_images, display_icon, resize_image, display_images
from tkinter.ttk import Combobox
import time
import os

def quit():
    global root
    root.quit()

def open_file():
    browse_text.set('Wczytywanie...')
    file = askopenfile(parent=root, mode='rb', title='Wybierz plik', filetype=[("Plik .mpg", "*.mpg")])
    if file:
        browse_text.set('Wybrano.')
        browse_btn = Button(root, textvariable=browse_text, command=lambda: open_file(), font=("Helvetica", 12),
                            bg="green", fg="black", height=1, width=12)
        browse_btn.grid(column=1, row=3, pady=5)
        global selected_path
        selected_path = file.name

def open_video():
    cam_text.set('Wczytywanie...')
    time.sleep(1)

def open_cam():
    cam_enable = 1

def display_logo(url, row, column):
    img = Image.open(url)
    #resize image
    img = img.resize((int(img.size[0]/1.5),int(img.size[1]/1.5)))
    img = ImageTk.PhotoImage(img)
    img_label = Label(image=img, bg="white")
    img_label.image = img
    img_label.grid(column=column, row=row, rowspan=2, sticky=NW, padx=20, pady=40)

def start_detection(selected_model, selected_path, selected_resolution):
    if selected_resolution == "1920x1080":
        resolution_width = 1920
        resolution_hight = 1080

    elif selected_resolution == "1280x720":
        resolution_width = 1280
        resolution_hight = 720

    elif selected_resolution == "640x360":
        resolution_width = 640
        resolution_hight = 360

    if selected_model == "Faster-RCNN v1 640x640":
        if selected_resolution == "1920x1080": resolution = [1920, 1080]
        os.chdir("C:/Object_detection_faster_rcnn/models-master/research/")
        command = 'python testowo.py {} {} {}'.format(selected_path, resolution_width, resolution_hight)
        os.system(command)

    elif selected_model == "SSD EfficientDet D0 512x512":
        os.chdir("C:/Object_detection_efficientdet/windows_v1.8.0/models-master/research")
        command = 'python testowo.py'
        os.system(command)

    elif selected_model == "SSD MobileNet v1 640x640":
        os.chdir("C:/Object_detection_mobile/models-master/research")
        command = 'python testowo.py'
        os.system(command)

root = Tk()
root.geometry('+%d+%d'%(350,10)) #place GUI at x=350, y=10
root.title('Praca inżynierska Badełek Piotr')
root.iconbitmap("wat_logo.ico")

#header area - logo & name
header = Frame(root, width=495, height=300, bg="white")
header.grid(columnspan=3, rowspan=2, row=0, column=0)

#main content area - text and image extraction
main_content = Frame(root, width=501, height=355, bg="#20bebe")
main_content.grid(columnspan=3, rowspan=4, row=3)

icon = Image.open("app_header.png")

icon = ImageTk.PhotoImage(icon)
icon_label = Button(image=icon, width=495, height=300)
icon_label.image = icon
icon_label.grid(column=0, row=0, columnspan=3, rowspan=2)

# Browse source button
source_text = Label(root, text="Wybierz źródło detekcji:", font=("Helvetica", 12, "bold"), bg="#20bebe", fg="black")
source_text.grid(column=0, row=3, pady=5)

# Browse video Button
browse_text = StringVar()
browse_btn = Button(root, textvariable=browse_text, command=lambda:open_file(), font=("Helvetica",12), bg="#20bebe", fg="black", height=1, width=12)
browse_text.set("Z pliku video")
browse_btn.grid(column=1, row=3, pady=5)
selected_path = 0

# Camera button
cam_text = StringVar()
cam_btn = Button(root, textvariable=cam_text, command=lambda:open_video(), font=("Helvetica",12), bg="#20bebe", fg="black", height=1, width=12)
cam_text.set("Kamera")
cam_btn.grid(column=2, row=3, pady=5)

# Neural network model browse
nn_text = Label(root, text="Wybierz model sieci:", font=("Helvetica", 12, "bold"), bg="#20bebe", fg="black")
nn_text.grid(column=0, row=4)
selected_model = StringVar()

suwak = Combobox(root, textvariable=selected_model, state="readonly")
suwak['values'] = ("SSD EfficientDet D0 512x512", "Faster-RCNN v1 640x640", "SSD MobileNet v1 640x640")
suwak.grid(row=4, column=1, columnspan=2)

# Resolution browse
selected_resolution = StringVar()
res_text = Label(root, text="Wybierz rozdzielczość obrazu:", font=("Helvetica", 12, "bold"), bg="#20bebe", fg="black")
res_text.grid(column=0, row=5)
suwak = Combobox(root, textvariable=selected_resolution, state="readonly")
suwak['values'] = ("1920x1080", "1280x720", "640x360")
suwak.grid(row=5, column=1, columnspan=2)

#Start detection button
start_text = StringVar()
start_btn = Button(root, textvariable=start_text, font=("Raleway",12), bg="green", fg="white", height=1, width=15, command=lambda:start_detection(selected_model.get(), selected_path,
                                                                                selected_resolution.get()))
start_text.set("Rozpocznij detekcje")
start_btn.grid(row=6, column=0, columnspan=3)

root.mainloop()


from setuptools import setup

with open('README.md', encoding='utf8') as f:
    long_description = f.read()

setup(
    name='powerline-travis-ci',
    description='A Powerline segment for fetching and showing the latest travis-ci build state',
    long_description=long_description,
    long_description_content_type='text/markdown',
    version='0.1.post1',
    keywords='powerline travis-ci travis ci build state status segment terminal cli',
    license='MIT',
    author='DeepSpace2',
    author_email='deepspace2@gmail.com',
    url='https://github.com/DeepSpace2/powerline-travis-ci',
    packages=['powerline_travis_ci'],
    classifiers=[
        'Environment :: Console',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Topic :: Terminals'
    ]
)


import argparse
import os
import torch
from torch.utils.data import DataLoader
import albumentations as A
import time
import copy
import logging
import numpy as np
import torch.nn.functional as F
from torch.nn.modules import CrossEntropyLoss, BCEWithLogitsLoss
from torch.autograd import Variable
from core.configs.default_seg import _C as cfg
from core.utils.utils import weights_init_normal, UnNormalize, adjust_param, setup_logger, _data_part
from core.models.focal_loss import FocalLoss2d
from core.models.build import build_seg_model
from core.models.output_discriminator import OutputDiscriminator
from core.datasets.seg_dataset import SegDataset
from core.utils.utils import *
from evaluate import evl

os.environ['CUDA_LAUNCH_BLOCKING'] = "1"

def train(cfg):
    logger = logging.getLogger("segmentation.trainer")
    logger.info("Start training")
    device = cfg.MODELS.DEVICE
    model = build_seg_model(cfg).to(device)
    save_path = cfg.OUTPUT_DIR + "/model"
    if cfg.TRAIN.EPOCH >0:
        model.load_state_dict(torch.load(f"{save_path}/model.pt"))

    discriminator = OutputDiscriminator(cfg.MODELS.CLASSES).to(device)
    if cfg.TRAIN.LOSS == "ce":
        criterion = CrossEntropyLoss(ignore_index=255).to(device)
    elif cfg.TRAIN.LOSS == "focal":
        criterion = FocalLoss2d(ignore_index=255).to(device)
    else:
        raise Exception("not impletment yet")
    bce_loss = BCEWithLogitsLoss().to(device)
    optimizer = torch.optim.Adam(model.parameters(), cfg.TRAIN.LR)
    optimizer_D = torch.optim.Adam(discriminator.parameters(), cfg.TRAIN.LR_D)
    scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, min_lr=0.00002, patience=20)

    trans = A.Compose([
        A.RandomCrop(cfg.DATASETS.SIZE, cfg.DATASETS.SIZE),
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5),
        A.RandomRotate90(p=0.5)
    ])

    _all, _train = _data_part(cfg.DATASETS.SOURCE_PART)
    ds = SegDataset(cfg.DATASETS.SOURCE_DATASET_PATH, all=_all, train=_train, transform=trans)
    # ds = SegDatatset(dataset_path, all=True, transform=trans)
    dataloader = DataLoader(ds, batch_size=cfg.TRAIN.BATCH_SIZE, num_workers=cfg.TRAIN.N_CPU, shuffle=True)

    _all, _train = _data_part(cfg.DATASETS.VAL_PART)
    val_ds = SegDataset(cfg.DATASETS.VAL_DATASET_PATH, all=_all, train=_train, transform=trans)
    val_dataloader = DataLoader(val_ds, batch_size=cfg.TRAIN.BATCH_SIZE, num_workers=cfg.TRAIN.N_CPU, shuffle=True)

    _all, _train = _data_part(cfg.DATASETS.TARGET_PART)
    target_ds = SegDataset(cfg.DATASETS.TARGET_DATASET_PATH, all=_all, train=_train, transform=trans, iter_len=len(ds), get_label=False)
    target_dataloader = DataLoader(target_ds, batch_size=cfg.TRAIN.BATCH_SIZE, num_workers=cfg.TRAIN.N_CPU, shuffle=True)

    source_label = 0
    target_label = 1

    save_iou = 0
    save_model = None
    os.makedirs(save_path , exist_ok=True)
    start_t = time.time()
    for epoch in range(cfg.TRAIN.EPOCH, cfg.TRAIN.TOTAL_EPOCH):
        model.train()
        discriminator.train()
        adv_loss_sum = 0
        epoch_loss = 0
        d_loss_sum = 0
        source_iter = enumerate(dataloader)
        target_iter = enumerate(target_dataloader)
        t1 = time.time()
        # train
        for i in range(len(dataloader)):
            optimizer.zero_grad()
            optimizer_D.zero_grad()

            # train segmentation module

            # don't accumulate gradients of Driscriminator
            for param in discriminator.parameters():
                param.requires_grad = False

            _, (input, target) = source_iter.__next__()
            input = input.to(device)
            target = target.to(device).long()
            y_pred = model.forward(input)

            if cfg.MODELS.BACKBONE == "MiT":
                y_pred = F.interpolate(y_pred, scale_factor=4)

            loss = criterion(y_pred, target)
            loss.backward()

            if cfg.MODELS.OUT_ADV:
                # train with target
                _, (input_target, _) = target_iter.__next__()
                input_target = input_target.to(device)
                y_pred_target = model.forward(input_target)

                if cfg.MODELS.BACKBONE == "MiT":
                    y_pred_target = F.interpolate(y_pred_target, scale_factor=4)

                d_out = discriminator.forward(F.softmax(y_pred_target, dim=1))
                adv_loss = bce_loss(d_out, Variable(torch.FloatTensor(d_out.data.size()).fill_(source_label)).to(device))
                adv_loss = adv_loss*cfg.LOSS.ADV
                # print("adv_loss:{}".format(adv_loss))
                adv_loss.backward()

                # train discriminator

                for param in discriminator.parameters():
                    param.requires_grad = True

                y_pred = y_pred.detach()
                y_pred_target = y_pred_target.detach()

                d_out = discriminator(F.softmax(y_pred, dim=1))
                d_loss_1 = bce_loss(d_out, Variable(torch.FloatTensor(d_out.data.size()).fill_(source_label)).to(device))

                d_out = discriminator(F.softmax(y_pred_target, dim=1))
                d_loss_2 = bce_loss(d_out, Variable(torch.FloatTensor(d_out.data.size()).fill_(target_label)).to(device))

                d_loss = d_loss_1 + d_loss_2
                # print("d_loss:{}".format(d_loss))
                d_loss.backward()
                optimizer_D.step()

                adv_loss_sum += adv_loss.data
                d_loss_sum += d_loss.data

            optimizer.step()

            # log info while training

            epoch_loss += loss.data

        t2 = time.time()
        logger.info('epoch={}\ti={}\tloss:{:.3f}\tadv_loss:{:.5f}\td_loss:{:.3f}\ttime:{:.3f} s\tlr:{:.6f}'.format
                      (epoch, i, epoch_loss / len(dataloader), adv_loss_sum/ len(dataloader),
                      d_loss_sum/ len(dataloader), t2 - t1, optimizer.param_groups[0]['lr']))
        scheduler.step(epoch_loss)

        # validation using images of target domain

        val_loss = 0
        iou_sum = np.zeros((7,))
        f1_sum = np.zeros((7,))
        model.eval()
        discriminator.eval()
        with torch.no_grad():
            for i, (input, target) in enumerate(val_dataloader):
                input = input.to(device)
                target = target.to(device).long()
                y_pred = model.predict(input)
                if cfg.MODELS.BACKBONE == "MiT":
                    y_pred = F.interpolate(y_pred, scale_factor=4)
                val_loss += criterion(y_pred, target).data
                iou_sum += iou(y_pred, target)
                f1_sum += f1(y_pred, target)
            avg_loss = val_loss / len(val_dataloader)
            c_iou = iou_sum/len(val_dataloader)
            c_f1 = f1_sum/len(val_dataloader)
            logger.info(f"epoch={epoch}\tavg_loss={avg_loss}\tiou={c_iou}\tf1={c_f1}\n")

        if c_iou[-1] >= save_iou:
            save_iou = c_iou[-1]
            logger.info(f"save model with iou=\n{c_iou}")
            model.eval()
            save_model = copy.deepcopy(model)
            torch.save(model.state_dict(), f'{save_path}/model.pt')

    return save_model

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-cfg",
        "--config_file",
        default="",
        metavar="FILE",
        help="path to config file",
        type=str,
    )
    parser.add_argument(
        "-opts",
        help="Modify config options using the command-line",
        default="",
        nargs=argparse.REMAINDER,
    )
    args = parser.parse_args()

    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
    logger = setup_logger("segmentation", cfg.OUTPUT_DIR)
    logger.info(args)
    logger.info("Loaded configuration file {}".format(args.config_file))
    with open(args.config_file, "r") as cf:
        config_str = "\n" + cf.read()
        logger.info(config_str)
    logger.info("Running with config:\n{}".format(cfg))

    model = train(cfg)

    _all, _train = _data_part(cfg.DATASETS.EVL_PART)
    trans = A.Compose([A.RandomCrop(cfg.DATASETS.SIZE, cfg.DATASETS.SIZE)])
    val_ds = SegDataset(cfg.DATASETS.EVL_DATASET_PATH, all=_all, train=_train, transform=trans)

    evl(model, val_ds, logger, cfg.OUTPUT_DIR+"/res", cfg.DATASETS.EVL_BATCH, cfg.DATASETS.EVL_GENERATE)

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 15 08:42:32 2017

@author: yashtawade
"""

#
#Importing necessary packages and libraries
from keras.models import Sequential #Initializes the NN
from keras.layers import Convolution2D #Convulution Step - Add convolutional layers
from keras.layers import MaxPooling2D #Pooling Step - Add pooling layers
from keras.layers import Flatten #Flattening Step - Pooled feature maps to large feature vectors
from keras.layers import Dense

#Initializing the CNN
classifier = Sequential() #Creating an object 'classifier' of the Sequential class

#Convolution layer with feature detector set as (32,3,3) and input_shape=(64, 64, 3) with the rectifier activation function
classifier.add(Convolution2D(32, 3, 3, input_shape=(64, 64, 3), activation='relu'))

#Pooling done using a pool size of 2X2
classifier.add(MaxPooling2D(pool_size=(2,2)))

#2nd convolutional layer
classifier.add(Convolution2D(32, 3, 3, activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))

#3rd convolutional layer with 64 feature detectors
classifier.add(Convolution2D(64, 3, 3, activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))

#Flattening done using the Flatten method which 'flattens' the pooled feature map into a one dimensional vector
classifier.add(Flatten())

#Adding the Hidden Layer i.e Fully connected layer
classifier.add(Dense(output_dim = 128, activation = 'relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(output_dim = 1, activation = 'sigmoid')) #Sigmoid as we have binary outcome - Cat or Dog

#Compiling the CNN choosing the loss function as binary cross entropy
classifier.compile(optimizer = 'adam',loss = 'binary_crossentropy', metrics = ['accuracy'])

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(
        'path of the file',#path in the directory
        target_size=(64, 64), #resizing the images
        batch_size=32, #batches of 32 images
        class_mode='binary')

test_set = test_datagen.flow_from_directory(
        'Path of the file',
        target_size=(150, 150),
        batch_size=32,
        class_mode='binary')

classifier.fit_generator(
        training_set,
        steps_per_epoch=8000, #Images in the training set
        epochs=25,
        validation_data=test_set,
        validation_steps=2000)  #Images in the test set

#Making a single prediction
import numpy as np
from keras.preprocessing import image #Imported in order to load the test image -> my_test_image
my_test_image = image.load_img('Path of the file',target_size=(64, 64))
my_test_image = image.img_to_array(my_test_image) #Converts 64X64 image into 64X64X3 Array
my_test_image = np.exapnd_dims(my_test_image,axis = 0)#
classifier.predict(my_test_image)
training_set.class_indices


class Solution(object):
    # minHeap 用内置函数维护一个最小堆
    def maxSlidingWindow(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        n = len(nums)
        # 注意 Python 默认的优先队列是小根堆
        q = [(-nums[i], i) for i in range(k)]
        heapq.heapify(q)

        res = [-q[0][0]]
        for i in range(k, n):
            heapq.heappush(q, (-nums[i], i))
            while q[0][1] <= i - k:
                heapq.heappop(q)
            res.append(-q[0][0])

        return res

    # deque双端队列
    def maxSlidingWindow(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        n = len(nums)
        if n == 0:
            return []
        res = []
        # 滑动窗口，注意：保存的是索引值
        window = deque()

        for i in range(n):
            # 当左边界到当前位置距离等于k，那么将它弹出
            if i >= k and i - k == window[0]:
                window.popleft()
            # 如果滑动窗口非空，新进来的数比前一个数（也就是队尾的数）要大，pop队尾
            while window and nums[i] >= nums[window[-1]]:
                window.pop()
            # 入队
            window.append(i)

            # 队首一定是滑动窗口的最大值的索引
            if i >= k - 1:
                res.append(nums[window[0]])
        return res


N=int(input())
k=int(input())
start, end=1, k

while start<=end:
    middle=(start+end)//2
    s=0
    for i in range(1,N+1):
        s+=min(middle//i, N)
    if s>=k:
        ans=middle
        end=middle-1
    else:
        start=middle+1

print(ans)


#!/usr/bin/env python

"""
This module contains classes and functions for reading and writing files
in different formats for MD simulations including so-named "_at_" format
for intrinsic MD codes from BSU laboratory of theoretical investigations
and computer modeling.

The reading is made to the structure class
The writing is made from the structure class

The module can be imported or executed as main program

"""

class structure():
    def __init__(self):
        self.n_at = None # number of atoms (integer)
        self.n_mark_at = 10 # number of atom markers (only for intrinsic codes)
        self.mark_at = [] # n_mark_at array of atom markers (only for intrinsic codes)
        self.i_at = [] # n_at array of atomic indexes (floats)
        self.r_at = [] # 3*n_at array of atomic coordinates (floats)
        self.f_at = [] # 3*n_at array of atomic forces (floats)
        self.v_at = [] # 3*n_at array of atomic velocities (floats)
        self.sizex = None # size of simulation cell in x direction (float)
        self.sizey = None # size of simulation cell in y direction (float)
        self.sizez = None # size of simulation cell in z direction (float)
        self.a_lattice3 = [1.0,1.0,1.0] # lattice constants (usually do not matter at all as sizex, sizey, sizez is sufficient)
        self.n_type_at = None # number of atomic types (integer)
        self.i_type_at = [] # n_at array of atomic types (integers)
        self.i_mass_at = [] # n_at array of atomic masses (floats)
        self.type_at = [] # n_type_at array of different atom types (integers)
        self.mass_at = [] # n_type_at array of masses of different atom types (floats)

    def __str__(self):
        nl = '\n'
        return  f"Structure with {self.n_at} atoms and {self.n_type_at} atom types\n" + \
                f"Sizes: {self.sizex} {self.sizey} {self.sizez}\n" + \
                f"Coordinates of atoms in angstroms:\n" + \
                f"{nl.join([str(r[0])+' '+str(r[1])+' '+str(r[2]) for r in self.r_at[0:5]])}\n" + \
                f" ... \n" + \
                f"{nl.join([str(r[0])+' '+str(r[1])+' '+str(r[2]) for r in self.r_at[-5:]])}\n"

    # def mass_at

def read_lmp_data(filename=None):

    st = structure()
    print('read_lmp_data filename:',filename)
    f = open(filename)
    lmp_data = f.readlines()
    f.close()

    mass_reading_mode = False
    atom_reading_mode = False
    for line in lmp_data[1:]:
        if len(line.split()) == 0:
            continue # skip line if it is empty
        elif 'atoms' in line:
            # write the content of the line with the number of atoms
            st.n_at = int(line.split()[0])
        elif 'atom types' in line:
            # write the content of the line with the number of atom types
            st.n_type_at = int(line.split()[0])
        elif 'xlo' and 'xhi' in line:
            # write the content of the line with xlo xhi limits
            st.sizex = float(line.split()[1]) - float(line.split()[0]) # xhi - xlo
        elif 'ylo' and 'yhi' in line:
            # write the content of the line with ylo yhi limits
            st.sizey = float(line.split()[1]) - float(line.split()[0]) # yhi - ylo
        elif 'zlo' and 'zhi' in line:
            # write the content of the line with zlo zhi limits
            st.sizez = float(line.split()[1]) - float(line.split()[0]) # zhi - zlo
        elif 'Masses' in line:
            mass_reading_mode = True
            atom_reading_mode = False
            continue
        elif 'Atoms' in line:
            mass_reading_mode = False
            atom_reading_mode = True
            continue

        if mass_reading_mode:
            st.type_at.append(int(line.split()[0]))
            st.mass_at.append(float(line.split()[1]))
            # st.type_mass_at

        if atom_reading_mode:
            i_at = int(line.split()[0]) # read atomic number
            t_at = int(line.split()[1]) # read atomic type
            x_at = float(line.split()[2]) # read atomic x coordinate
            y_at = float(line.split()[3]) # read atomic y coordinate
            z_at = float(line.split()[4]) # read atomic z coordinate

            st.i_at.append(i_at)
            st.i_type_at.append(t_at)
            st.r_at.append([x_at,y_at,z_at])
            st.f_at.append([0.0,0.0,0.0])
            st.v_at.append([0.0,0.0,0.0])
            st.i_mass_at.append(st.mass_at[st.type_at.index(t_at)])

    return st

def read_rv_at(filename=None):

    st = structure()
    print('read_rv_at filename:',filename)
    # f = open(filename)
    # rv_at = f.readlines()
    # f.close()

    f = open(filename)
    line = f.readline().split()
    st.n_at = int(line[0])
    st.n_mark_at = int(line[1])
    line = f.readline().split()
    size = [float(i) for i in line]
    st.sizex = size[0]
    st.sizey = size[1]
    st.sizez = size[2]
    line = f.readline().split()
    st.a_lattice3 = [float(i) for i in line]
    fline = f.read().split()
    f.close()

    iw=0
    # r_at=[]
    for i in range(st.n_at):
        x=float(fline[iw]); iw+=1
        y=float(fline[iw]); iw+=1
        z=float(fline[iw]); iw+=1
        st.r_at.append([x,y,z])

    # v_at=[]
    for i in range(st.n_at):
        vx=float(fline[iw]); iw+=1
        vy=float(fline[iw]); iw+=1
        vz=float(fline[iw]); iw+=1
        st.v_at.append([vx,vy,vz])

    # mass_at=[]
    for i in range(st.n_at):
        mass=float(fline[iw]); iw+=1
        st.i_mass_at.append(mass)

    # i_sort_at=[]
    for i in range(st.n_at):
        i_type=int(fline[iw]); iw+=1
        st.i_type_at.append(i_type)

    # num_at_r=[]
    for i in range(st.n_at):
        num_at=int(fline[iw]); iw+=1
        st.i_at.append(num_at)

    st.type_at = list(set(st.i_type_at))
    st.n_type_at = len(st.type_at)
    st.mass_at = list(set(st.i_mass_at))

    return st

def read_r_at(filename=None):

    """ Function for reading files in simple r_at format for MSD calculations"""
    st = structure()
    print('read_r_at filename:',filename)

    f = open(filename)
    line = f.readline().split()
    st.n_at = int(line[0])
    st.n_mark_at = 0
    line = f.readline().split()
    size = [float(i) for i in line]
    st.sizex = size[0]
    st.sizey = size[1]
    st.sizez = size[2]
    st.a_lattice3 = [st.sizex, st.sizey, st.sizez]
    fline = f.read().split()
    f.close()

    iw=0
    # r_at=[]
    for i in range(st.n_at):
        x=float(fline[iw]); iw+=1
        y=float(fline[iw]); iw+=1
        z=float(fline[iw]); iw+=1
        st.r_at.append([x,y,z])

    # v_at=[]
    for i in range(st.n_at):
        vx=0.0
        vy=0.0
        vz=0.0
        st.v_at.append([vx,vy,vz])

    # mass_at=[]
    for i in range(st.n_at):
        mass=0.0
        st.i_mass_at.append(mass)

    # i_sort_at=[]
    for i in range(st.n_at):
        i_type=0.0
        st.i_type_at.append(i_type)

    # num_at_r=[]
    for i in range(st.n_at):
        num_at=i
        st.i_at.append(num_at)

    st.type_at = list(set(st.i_type_at))
    st.n_type_at = len(st.type_at)
    st.mass_at = list(set(st.i_mass_at))

    return st

def write_rf_at(st,filename=None):
    """
    This function takes Structure() object as input.
    Forces will be zeros as they are unknown!

    """

    if filename == None:
        filename = 'at_rf'

    with open(filename,'w') as f:
        f.write('{0:5} 10\n'.format(st.n_at))
        f.write('{0:9.6f} {1:9.6f} {2:9.6f}  \n'.format(st.sizex, st.sizey,st.sizez))
        f.write('{0:9.6f} {1:9.6f} {2:9.6f}  \n'.format(1, 1, 1))

        for r_at_i in st.r_at:
            f.write('{0:9.6f}  {1:9.6f}  {2:9.6f}  '.format(r_at_i[0],r_at_i[1],r_at_i[2]))
        f.write('\n')

        for f_at_i in st.f_at:
            f.write('{0:9.6f}  {1:9.6f}  {2:9.6f}  '.format(f_at_i[0],f_at_i[1],f_at_i[2]))
        f.write('\n')

        for i in st.i_mass_at:
            f.write(str(i)+' ')

        f.write('\n')
        for i in st.i_type_at:
            f.write(str(i)+' ')
        f.write('\n')

        for i in range(st.n_at):
            f.write(str(i+1)+' ')
        f.write('\n')

        for i in range(st.n_at):
            f.write('T ')
        f.write('\n')

        for i in range(10):
            for j in range(st.n_at):
                f.write('F ')
            f.write('\n')

        f.close()
    return

def write_rv_at(st,filename=None):
    """
    This function takes Structure() object as input.
    Forces will be zeros as they are unknown!

    """

    if filename == None:
        filename = 'at_rv'

    with open(filename,'w') as f:
        f.write('{0:5} 10\n'.format(st.n_at))
        f.write('{0:9.6f} {1:9.6f} {2:9.6f}  \n'.format(st.sizex, st.sizey,st.sizez))
        f.write('{0:9.6f} {1:9.6f} {2:9.6f}  \n'.format(1, 1, 1))

        for r_at_i in st.r_at:
            f.write('{0:9.6f}  {1:9.6f}  {2:9.6f}  '.format(r_at_i[0],r_at_i[1],r_at_i[2]))
        f.write('\n')

        for v_at_i in st.v_at:
            f.write('{0:9.6f}  {1:9.6f}  {2:9.6f}  '.format(v_at_i[0],v_at_i[1],v_at_i[2]))
        f.write('\n')

        for i in st.i_mass_at:
            f.write(str(i)+' ')

        f.write('\n')
        for i in st.i_type_at:
            f.write(str(i)+' ')
        f.write('\n')

        for i in range(st.n_at):
            f.write(str(i+1)+' ')
        f.write('\n')

        for i in range(st.n_at):
            f.write('T ')
        f.write('\n')

        for i in range(10):
            for j in range(st.n_at):
                f.write('F ')
            f.write('\n')

        f.close()

    return

def write_lmp_data(st,filename=None):

    # Checking if n_type_at == type_at[-1]
    if st.n_type_at < st.type_at[-1]:
        type_at_old = st.type_at
        type_at_new = [i+1 for i in range(st.n_type_at)]
        delta_type_at = [i-j for i,j in zip(type_at_old,type_at_new)]
        st.type_at = type_at_new
        i_type_at_old = st.i_type_at
        i_type_at_new = []
        # i_type_at_new = [i for i in st.i_type_at]
        for i_type in i_type_at_old:
            for type_at, delta in zip(type_at_old,delta_type_at):
                if i_type == type_at:
                    i_type_at_new.append(i_type-delta)
        st.i_type_at = i_type_at_new

    # Cheking if cell is centered around 0:
    x_centered = False
    y_centered = False
    z_centered = False
    for r in st.r_at:
        x = r[0]; y = r[1]; z = r[2]
        if x < -0.5:
            x_centered = True
        if y < -0.5:
            y_centered = True
        if z < -0.5:
            z_centered = True

    with open(filename,'w') as f:
        f.write('Start file for LAMMPS generated by md_format_converter_mi.py\n')
        f.write('{} atoms\n'.format(st.n_at))
        f.write('{} atom types\n'.format(st.n_type_at))
        f.write('\n')
        # writing sizes of cell
        if x_centered:
            f.write('{0:9.6f} {1:9.6f} xlo xhi\n'.format(-st.sizex/2,st.sizex/2))
        elif not x_centered:
            f.write('0.0 {0:9.6f} xlo xhi\n'.format(st.sizex))

        if y_centered:
            f.write('{0:9.6f} {1:9.6f} ylo yhi\n'.format(-st.sizey/2,st.sizey/2))
        elif not y_centered:
            f.write('0.0 {0:9.6f} ylo yhi\n'.format(st.sizey))

        if z_centered:
            f.write('{0:9.6f} {1:9.6f} zlo zhi\n'.format(-st.sizez/2,st.sizez/2))
        elif not z_centered:
            f.write('0.0 {0:9.6f} zlo zhi\n'.format(st.sizez))

        f.write('\n')
        f.write('Masses \n')
        f.write('\n')
        # for type_at, mass_at in zip(range(1,len(st.type_at)+1),st.mass_at):
        for type_at, mass_at in zip(st.type_at,st.mass_at):
            f.write('{0} {1:9.6f}\n'.format(type_at,mass_at))
        f.write('\n')
        f.write('Atoms \n')
        f.write('\n')
        for i_num, i_type, i_r_at in zip(st.i_at,st.i_type_at,st.r_at):
            f.write('{0} {1} {2:9.6f} {3:9.6f} {4:9.6f}\n'.format(i_num,i_type,i_r_at[0],i_r_at[1],i_r_at[2]))
        f.write('\n')
        f.close()

    return

def print_instructions():
    print('This program converts files in different formats for MD simulation codes\
 including the so-named "_at_" format for intrinsic MD codes\
 from BSU laboratory of theoretical investigations\
 and computer modeling authored by A.G. Lipnitskii.')
    print('\n')
    print('Usage: md_format_converter_mi.py mode infile outfile')
    print('\n')
    print('modes: ')
    print(' --lmpdata2rvat or -ldarv - convert LAMMPS data file to rv_at file (r means coordinates, v means velocities)')
    print(' --lmpdata2rfat or -ldarf - convert LAMMPS data file to rf_at file (r means coordinates, f means forces)')
    print(' --rvat2lmpdata or -arvld - convert rv_at file to LAMMPS data file')
    print(' --rfat2lmpdata or -arfld - convert rf_at file to LAMMPS data file')

if __name__=='__main__':
    import sys
    try:
        mode    = sys.argv[1]
        infile  = sys.argv[2]
        outfile = sys.argv[3]
    except:
        print_instructions()
        print('\n')
        print('Error: Not all arguments are pointed out!')
        print('\n')
        sys.exit(1)

    print('mode:', mode)
    print('infile:', infile)
    print('outfile:', outfile)

    if mode == '--lmpdata2rvat' or mode == '-ldarv':
        print('Converting from LAMMPS data file to rv_at file...')
        st = read_lmp_data(infile)
        write_rv_at(st,outfile)

    elif mode == '--lmpdata2rfat' or mode == '-ldarf':
        print('Converting from LAMMPS data file to rf_at file...')
        st = read_lmp_data(infile)
        write_rf_at(st,outfile)

    elif mode == '--rvat2lmpdata' or mode == '-arvld':
        print('Converting from rv_at file to LAMMPS data file...')
        st = read_rv_at(infile)
        write_lmp_data(st,outfile)

    elif mode == '--rfat2lmpdata' or mode == '-arfld':
        print('Converting from rf_at file to LAMMPS data file...')
        st = read_rv_at(infile)
        write_lmp_data(st,outfile)

    else:
        print('Error in arguments!')
        print_instructions()
        sys.exit(1)


import networkx as nx
import pandas as pd
import random
import os
import matplotlib.pyplot as plt

class WSN:
    def __init__(self, config) -> None:
        self.config = config
        self.g = nx.Graph()
        self._initalize_wsn()

    def _initalize_wsn(self):
        self._create_nodelist_from_config()
        self._create_edgelist_from_config()
        self._create_edgelist_from_csv("resources/edgelist.csv")
        self._create_nodelist_from_csv("resources/nodelist.csv")
        self._visualize_graph()

    def get_neighbor_node_data(self, node_id):
        return self.g.adj[node_id]

    def _create_edgelist_from_csv(self, path):
        edgelist = pd.read_csv(path)
        # Add edges and edge attributes
        for i, elrow in edgelist.iterrows():
            if (
                elrow[3] == True
            ):  # if node 1 and node 2 are neighbor then there is edge between them
                self.g.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict())

    def _create_nodelist_from_csv(self, path):
        nodelist = pd.read_csv(path)
        # Add node attributes
        for i, nlrow in nodelist.iterrows():
            self.g.add_node(nlrow["id"], attr_dict=nlrow[1:].to_dict())

    def _save_dataframe(self, df, file_name, path="resources"):
        df.to_csv(os.path.join(path, file_name), encoding="utf-8", index=False)

    def _create_coordinate(self):
        x_coord, y_coord = self.config.get_area
        no_of_nodes = self.config.get_no_of_nodes
        coords = [
            {
                "id": id,
                "X": random.randint(0, x_coord),
                "Y": random.randint(0, y_coord),
                "data": 0,
                "power": self.config.get_remaining_power_of_node,
                "status": True,
            }
            for id in range(no_of_nodes)
        ]
        return coords

    def _calculate_eculidean_dist(self, node_1, node_2):
        return round(
            ((node_1["X"] - node_2["X"]) ** 2 + (node_1["Y"] - node_2["Y"]) ** 2)
            ** 0.5,
            2,
        )

    def _create_edgelist_from_config(self):
        edgelist = []
        coords = self._create_coordinate()

        for node_1 in coords:
            for node_2 in coords:
                if node_1 != node_2:
                    dist_bt_nodes = self._calculate_eculidean_dist(node_1, node_2)
                    is_neighbor = dist_bt_nodes < self.config.get_tx_range
                    color = "green" if is_neighbor else "red"
                    edgelist.append(
                        {
                            "node_1": node_1["id"],
                            "node_2": node_2["id"],
                            "distance": dist_bt_nodes,
                            "neighbor": is_neighbor,
                            "color": color,
                        }
                    )

        edgelist = pd.DataFrame.from_dict(edgelist)
        self._save_dataframe(edgelist, "edgelist.csv")

    def _create_nodelist_from_config(self):
        coords = self._create_coordinate()
        nodelist = pd.DataFrame.from_dict(coords)
        self._save_dataframe(nodelist, "nodelist.csv")

    def _visualize_graph(self):
        # Define node positions data structure (dict) for plotting
        node_positions = {
            node[0]: (node[1]["attr_dict"]["X"], node[1]["attr_dict"]["Y"])
            for node in self.g.nodes(data=True)
        }

        # # Preview of node_positions with a bit of hack (there is no head/slice method for dictionaries).
        # print(dict(list(node_positions.items())[0:5]))

        # Define data structure (list) of edge colors for plotting
        edge_colors = [e[2]["attr_dict"]["color"] for e in self.g.edges(data=True)]

        # # Preview first 10
        # print(edge_colors[0:10])

        plt.figure(figsize=(8, 6))
        nx.draw(
            self.g,
            pos=node_positions,
            edge_color=edge_colors,
            node_size=10,
            node_color="black",
        )
        plt.savefig("resources/wsn.png")
        plt.title("Graph Representation of Sleeping Giant Trail Map", size=15)
        # plt.show()

if __name__ == "__main__":
    from utils.config import Config

    c = Config("config.yaml")
    w = WSN(c)
    w._create_nodelist_from_config()
    w._create_edgelist_from_config()
    w._create_edgelist_from_csv("resources/edgelist.csv")
    w._create_nodelist_from_csv("resources/nodelist.csv")
    w._visualize_graph()


import math
from typing import Dict, List, Union, Tuple, Any

import pygame

from pygame_gui.core.interfaces import IUIManagerInterface
from pygame_gui.core.colour_gradient import ColourGradient
from pygame_gui.core.drawable_shapes.drawable_shape import DrawableShape
from pygame_gui.core.utility import basic_blit

class RectDrawableShape(DrawableShape):
    """
    A rectangle shape for UI elements has theming options for a border, a shadow, colour
    gradients and text.

    :param containing_rect: The layout rectangle that surrounds and controls the size of this shape.
    :param theming_parameters: Various styling parameters that control the final look of the shape.
    :param states: The different UI states the shape can be in. Shapes have different surfaces
                   for each state.
    :param manager: The UI manager.

    """

    def __init__(self,
                 containing_rect: pygame.Rect,
                 theming_parameters: Dict[str, Any],
                 states: List[str],
                 manager: IUIManagerInterface):
        super().__init__(containing_rect, theming_parameters, states, manager)

        self.has_been_resized = False

        self.full_rebuild_on_size_change()

    def full_rebuild_on_size_change(self):
        """
        Completely rebuilds the rectangle shape from it's dimensions and parameters.

        Everything needs rebuilding if we change the size of the containing rectangle.

        """
        # clamping border and shadow widths so we can't form impossible negative sized surfaces
        super().full_rebuild_on_size_change()

        self.base_surface = None

        if self.shadow_width > min(math.floor(self.containing_rect.width / 2),
                                   math.floor(self.containing_rect.height / 2)):
            self.shadow_width = min(math.floor(self.containing_rect.width / 2),
                                    math.floor(self.containing_rect.height / 2))

        self.shadow_width = max(self.shadow_width, 0)

        if self.border_width > min(math.floor((self.containing_rect.width -
                                               (self.shadow_width * 2)) / 2),
                                   math.floor((self.containing_rect.height -
                                               (self.shadow_width * 2)) / 2)):
            self.border_width = min(math.floor((self.containing_rect.width -
                                                (self.shadow_width * 2)) / 2),
                                    math.floor((self.containing_rect.height -
                                                (self.shadow_width * 2)) / 2))

        self.border_width = max(self.border_width, 0)

        if self.shadow_width > 0:
            self.click_area_shape = pygame.Rect((self.containing_rect.x + self.shadow_width,
                                                 self.containing_rect.y + self.shadow_width),
                                                (self.containing_rect.width -
                                                 (2 * self.shadow_width),
                                                 self.containing_rect.height -
                                                 (2 * self.shadow_width)))
            shadow = self.ui_manager.get_shadow(self.containing_rect.size,
                                                shadow_width=self.shadow_width,
                                                corner_radius=self.shadow_width)
            if shadow is not None:
                self.base_surface = shadow

        else:
            self.click_area_shape = self.containing_rect.copy()

        if self.base_surface is None:
            self.base_surface = pygame.surface.Surface(self.containing_rect.size,
                                                       flags=pygame.SRCALPHA,
                                                       depth=32)

        self.border_rect = pygame.Rect((self.shadow_width,
                                        self.shadow_width),
                                       (self.click_area_shape.width, self.click_area_shape.height))

        self.background_rect = pygame.Rect((self.border_width + self.shadow_width,
                                            self.border_width + self.shadow_width),
                                           (self.click_area_shape.width - (2 * self.border_width),
                                            self.click_area_shape.height - (2 * self.border_width)))
        if 'disabled' in self.states and self.active_state == self.states['disabled']:
            self.redraw_all_states(force_full_redraw=True)
        else:
            self.redraw_all_states()

    def collide_point(self, point: Union[pygame.math.Vector2,
                                         Tuple[int, int],
                                         Tuple[float, float]]) -> bool:
        """
        Tests if a point is colliding with our Drawable shape's 'click area' hot spot.

        :param point: The point to test.

        :return: True if we are colliding.

        """
        return bool(self.click_area_shape.collidepoint(int(point[0]), int(point[1])))

    def set_dimensions(self, dimensions: Union[pygame.math.Vector2,
                                               Tuple[int, int],
                                               Tuple[float, float]]):
        """
        Changes the size of the rectangle shape. Relatively expensive to do.

        :param dimensions: The new dimensions.

        """
        if (dimensions[0] == self.containing_rect.width and
                dimensions[1] == self.containing_rect.height):
            return False
        self.containing_rect.width = int(dimensions[0])
        self.containing_rect.height = int(dimensions[1])
        self.click_area_shape.width = int(dimensions[0]) - (2 * self.shadow_width)
        self.click_area_shape.height = int(dimensions[1]) - (2 * self.shadow_width)

        self.has_been_resized = True

        self.full_rebuild_on_size_change()

        return True

    def set_position(self, point: Union[pygame.math.Vector2,
                                        Tuple[int, int],
                                        Tuple[float, float]]):
        """
        Move the shape. Only really impacts the position of the 'click_area' hot spot.

        :param point: The new position to move it to.

        """
        self.containing_rect.x = int(point[0])
        self.containing_rect.y = int(point[1])
        self.click_area_shape.x = int(point[0]) + self.shadow_width
        self.click_area_shape.y = int(point[1]) + self.shadow_width

    def redraw_state(self, state_str: str, add_text: bool = True):
        """
        Redraws the shape's surface for a given UI state.

        :param add_text:
        :param state_str: The ID string of the state to rebuild.

        """
        border_colour_state_str = state_str + '_border'
        bg_colour_state_str = state_str + '_bg'
        text_colour_state_str = state_str + '_text'
        text_shadow_colour_state_str = state_str + '_text_shadow'
        image_state_str = state_str + '_image'

        found_shape = None
        shape_id = None
        if 'filled_bar' not in self.theming and 'filled_bar_width_percentage' not in self.theming:
            shape_id = self.shape_cache.build_cache_id('rectangle', self.containing_rect.size,
                                                       self.shadow_width,
                                                       self.border_width,
                                                       self.theming[border_colour_state_str],
                                                       self.theming[bg_colour_state_str])

            found_shape = self.shape_cache.find_surface_in_cache(shape_id)
        if found_shape is not None:
            self.states[state_str].surface = found_shape.copy()
        else:
            self.states[state_str].surface = self.base_surface.copy()

            if self.border_width > 0:

                if isinstance(self.theming[border_colour_state_str], ColourGradient):
                    border_shape_surface = pygame.surface.Surface(self.border_rect.size,
                                                                  flags=pygame.SRCALPHA, depth=32)
                    border_shape_surface.fill(pygame.Color('#FFFFFFFF'))
                    self.states[state_str].surface.blit(border_shape_surface,
                                                        self.border_rect,
                                                        special_flags=pygame.BLEND_RGBA_SUB)
                    self.theming[border_colour_state_str].apply_gradient_to_surface(
                        border_shape_surface)
                    basic_blit(self.states[state_str].surface,
                               border_shape_surface, self.border_rect)
                else:
                    self.states[state_str].surface.fill(self.theming[border_colour_state_str],
                                                        self.border_rect)

            if isinstance(self.theming[bg_colour_state_str], ColourGradient):
                background_shape_surface = pygame.surface.Surface(self.background_rect.size,
                                                                  flags=pygame.SRCALPHA, depth=32)
                background_shape_surface.fill(pygame.Color('#FFFFFFFF'))
                self.states[state_str].surface.blit(background_shape_surface,
                                                    self.background_rect,
                                                    special_flags=pygame.BLEND_RGBA_SUB)
                self.theming[bg_colour_state_str].apply_gradient_to_surface(
                    background_shape_surface)
                basic_blit(self.states[state_str].surface,
                           background_shape_surface, self.background_rect)
            else:
                self.states[state_str].surface.fill(self.theming[bg_colour_state_str],
                                                    self.background_rect)

            if 'filled_bar' in self.theming and 'filled_bar_width_percentage' in self.theming:
                bar_rect = pygame.Rect(self.background_rect.topleft,
                                       (int(self.theming['filled_bar_width_percentage'] *
                                            self.background_rect.width),
                                        self.background_rect.height))
                if isinstance(self.theming['filled_bar'], ColourGradient):
                    bar_shape_surface = pygame.surface.Surface(bar_rect.size,
                                                               flags=pygame.SRCALPHA,
                                                               depth=32)
                    bar_shape_surface.fill(pygame.Color('#FFFFFFFF'))
                    self.states[state_str].surface.blit(bar_shape_surface, bar_rect,
                                                        special_flags=pygame.BLEND_RGBA_SUB)
                    self.theming['filled_bar'].apply_gradient_to_surface(bar_shape_surface)
                    basic_blit(self.states[state_str].surface,
                               bar_shape_surface, bar_rect)
                else:
                    self.states[state_str].surface.fill(self.theming['filled_bar'], bar_rect)

            if self.states[state_str].cached_background_id is not None:
                self.shape_cache.remove_user_from_cache_item(
                    self.states[state_str].cached_background_id)
            if (not self.has_been_resized
                    and ((self.containing_rect.width * self.containing_rect.height) < 40000)
                    and (shape_id is not None
                         and self.states[state_str].surface.get_width() <= 1024
                         and self.states[state_str].surface.get_height() <= 1024)):
                self.shape_cache.add_surface_to_cache(self.states[state_str].surface.copy(),
                                                      shape_id)
                self.states[state_str].cached_background_id = shape_id

        self.finalise_images_and_text(image_state_str, state_str,
                                      text_colour_state_str,
                                      text_shadow_colour_state_str,
                                      add_text)

        self.states[state_str].has_fresh_surface = True
        self.states[state_str].generated = True


import pandas as pd
import numpy as np
import quagev as qg
from numpy import asarray

BP = 3

# Import data
ScaleExS = pd.read_csv("inputs/Scaling Exponents_Short.csv")
ScaleExL = pd.read_csv("inputs/Scaling Exponents_Long.csv")
ParGev = pd.read_csv("inputs/ParamGEV_NCM_Obs.csv")

betaS = ScaleExS.iloc[0, 1]
betaL = ScaleExL.iloc[0, 1]

TShi = ParGev.iloc[0, ParGev.shape[1]-1]
TAlpha = ParGev.iloc[1, ParGev.shape[1]-1]
TKappa = ParGev.iloc[2, ParGev.shape[1]-1]

# Duration
Dur = [15, 30, 45, 60, 120, 180, 360, 720, 1440]

# One-Moment Scaling method
ND = len(Dur)-1

tShi = []
tAlpha = []
tKappa = []

for i in range(0, ND+1):
    tKappa.append(TKappa)

# Long Period
for i in range(BP, ND):
    T = Dur[ND]
    t = Dur[i]
    tAlphaCalc = np.exp(betaL * (np.log(t) - np.log(T)) + np.log(TAlpha))
    tAlpha.append(tAlphaCalc)
    tShiCalc = np.exp(betaL * (np.log(t) - np.log(T)) + np.log(TShi))
    tShi.append(tShiCalc)

tAlpha.append(TAlpha)
tShi.append(TShi)

# Short Period
for i in range(0, BP):
    T = Dur[BP]
    t = Dur[i]
    tAlphaCalc = np.exp(betaS * (np.log(t) - np.log(T)) + np.log(tAlpha[i]))
    tAlpha.insert(0, tAlphaCalc)
    tShiCalc = np.exp(betaS * (np.log(t) - np.log(T)) + np.log(tShi[i]))
    tShi.insert(0, tShiCalc)

tAlpha.sort()
tShi.sort()

# Save Scaled-GEV Parameters
npArray = [tShi, tAlpha, tKappa]
df = pd.DataFrame(data=npArray, columns=["D_15min", "D_30min", "D_45min", "D_1h", "D_2h", "D_3h", "D_6h", "D_12h", "D_24h"])
df.index = ['tShi'] + ['tAlpha'] + ['tKappa']
df.to_csv("output/ParamGEV_Scaled_NCM_OneMom.csv")

# Estimate Quantiles and Save Quantiles

# Plotting position
# Cunnane Formula: T=(N+0.2)/(m-0.4)
ScaledParmGEV = asarray([tShi, tAlpha, tKappa])
N = 40
P = []
for i in range(1, N+1):
    tCalc = (N+0.2) / (i-0.4)
    pCalc = 1 / tCalc
    P.append(pCalc)

# Estimating Quantiles
# Using imported quagev.py method
Q = np.zeros(shape=(N, ND+1))
for i in range(ND+1):
    for j in range(N):
        Q[j, i] = (qg.quagev(P[j], ScaledParmGEV[:,i]))

quantileFrame = pd.DataFrame(data=Q, columns=["D_15min", "D_30min", "D_45min", "D_1h", "D_2h", "D_3h", "D_6h", "D_12h", "D_24h"])

# Save the estimated quantiles
quantileFrame.to_csv("output/Quantiles_NCM_OneMom_Scaled.csv")


input_path = "./input.txt"
selector_list = [1,5,9,13,17,21,25,29,33]
move_amount_index = 1
from_index = 3
to_index = 5

def main():
    f = open(input_path, "r")
    i = 0
    cargo = [[],[],[],[],[],[],[],[],[]]
    amount, src, dest = 0, 0, 0
    buffer_list = []

    for line in f:
        data = line.strip("\n")
        if i < 8 and len(data)>1:
            for selector in selector_list:
                if data[selector] != " ":
                    cargo[selector_list.index(selector)].insert(0, data[selector])
        elif i > 9 :#and i < 15:
            buffer_list.clear()
            operation = data.split()
            amount = int(operation[move_amount_index])
            src = int(operation[from_index])-1
            dest = int(operation[to_index])-1
            print(data)
            print("b4")
            print(cargo[src])
            print(cargo[dest])
            for amount_counter in range(amount,0,-1):
                buffer_list.append(cargo[src].pop(-amount_counter))
                print(amount_counter)
                print(buffer_list)
            cargo[dest].extend(buffer_list)
            print("After")
            print(cargo[src])
            print(cargo[dest])
            print("--------------------------------------------------------------")

        i += 1
    somestring = ""
    for item in cargo:
        somestring += item[-1]
    print("result? " + somestring)

if __name__ == "__main__":
    main()


import numpy as np
from einops import reduce, rearrange, repeat
import torch
import pickle
from functools import partial

class AttentionExtractor():
    def __init__(self, function=None, *args, **kwargs):
        if isinstance(function, str):
            self.reduction_function = getattr(self, function)
        else:
            self.reduction_function = function

        if args or kwargs:
            self.reduction_function = partial(self.reduction_function, *args, **kwargs)

    def __call__(self, inp, *args, **kwargs):
        """ Called with: Iterations x Layers x Channels X Height x Width

        :param inp: tensor
        :return: attention map
        """
        assert inp.ndim == 5
        out = self.reduction_function(inp, *args, **kwargs)
        assert out.ndim == 5
        return out

    def all_mean(self, inp):
        return inp.mean(dim=(0, 1, 2), keepdim=True)

    def diffusion_steps_mean(self, x, steps):
        assert x.size()[2] == 1
        return x[-steps:, :, 0].mean(dim=(0, 1), keepdim=True)

    def relevant_token_step_mean(self, x, tok_idx, steps):
        return x[-steps:, :, tok_idx:(tok_idx+1)].mean(dim=(0, 1), keepdim=True)

    def all_token_mean(self, x, steps):
        return x[-steps:].mean(dim=(0, 1), keepdim=True)

    def multi_relevant_token_step_mean(self, x, tok_idx, steps):
        res = None
        for tok_id in tok_idx:
            if res is None:
                res = x[-steps:, :, tok_id:(tok_id+1)].mean(dim=(0, 1), keepdim=True)
            else:
                res += x[-steps:, :, tok_id:(tok_id+1)].mean(dim=(0, 1), keepdim=True)

        res = res.mean(dim=(0,1), keepdim=True)
        return res

def get_attention_masks(data_obj, attention_dir):
    path = data_obj["seg_path"].replace("segmentation", attention_dir)
    path = path[:-4] + ".pkl"
    with open(path, "rb") as fp:
        obj = pickle.load(fp)
    return obj

def print_attention_info(attention):
    print(f"Num Forward passes: {len(attention)}, Depth:{len(attention[0])}")
    for i in range(len(attention[0])):
        print(f"Layer: {i} - {attention[0][i].size()}")

def reorder_attention_maps(attention):
    for i in range(len(attention)):
        for j in range(len(attention[i])):
            layer = attention[i][j]
            map_size = int(np.sqrt(layer.size()[-2]))
            layer = rearrange(layer.squeeze(dim=1), 'b (h w) tok -> b tok h w', h=map_size, w=map_size)
            attention[i][j] = layer
    return attention

def normalize_attention_map_size(attention_maps):
    for iteration in range(len(attention_maps)): # trough layers / diffusion steps
        for layer in range(len(attention_maps[iteration])):
            attention_map = attention_maps[iteration][layer]# B x num_resblocks x numrevdiff x H x W
            if attention_map.size()[-1] != 64:
                upsampling_factor = 64 // attention_map.size()[-1]
                attention_map = repeat(attention_map, 'b tok h w -> b tok (h h2) (w w2)',h2=upsampling_factor, w2=upsampling_factor)
            attention_maps[iteration][layer] = rearrange(attention_map, "b tok h w -> b 1 tok h w")

        attention_maps[iteration] = rearrange(torch.cat(attention_maps[iteration], dim=1), "b depth tok h w -> b 1 depth tok h w")
    attention_maps = torch.cat(attention_maps, dim=1)
    return attention_maps

def get_latent_slice(batch, opt):
    ds_slice = []
    for slice_ in batch["slice"]:
        if slice_.start is None:
            ds_slice.append(slice(None, None, None))
        else:
            ds_slice.append(slice(slice_.start // opt.f, slice_.stop // opt.f, None))
    return tuple(ds_slice)


import cv2
import numpy as np

input_fn = "Demo/00001_input.jpg"

def my_guidedFilter_oneChannel(srcImg, guidedImg, rad=9, eps=1e-8):
    srcImg = srcImg / 255.0
    guidedImg = guidedImg / 255.0
    img_shape = np.shape(srcImg)

    P_mean = cv2.boxFilter(srcImg, -1, (rad, rad), normalize=True)
    I_mean = cv2.boxFilter(guidedImg, -1, (rad, rad), normalize=True)

    I_square_mean = cv2.boxFilter(np.multiply(guidedImg, guidedImg), -1, (rad, rad), normalize=True)
    I_mul_P_mean = cv2.boxFilter(np.multiply(srcImg, guidedImg), -1, (rad, rad), normalize=True)

    var_I = I_square_mean - np.multiply(I_mean, I_mean)
    cov_I_P = I_mul_P_mean - np.multiply(I_mean, P_mean)

    a = cov_I_P / (var_I + eps)
    b = P_mean - np.multiply(a, I_mean)

    a_mean = cv2.boxFilter(a, -1, (rad, rad), normalize=True)
    b_mean = cv2.boxFilter(b, -1, (rad, rad), normalize=True)

    dstImg = np.multiply(a_mean, guidedImg) + b_mean

    return dstImg * 255.0

def my_guidedFilter_threeChannel(srcImg, guidedImg, rad=9, eps=0.01):
    img_shape = np.shape(srcImg)

    dstImg = np.zeros(img_shape, dtype=float)

    for ind in range(0, img_shape[2]):
        dstImg[:, :, ind] = my_guidedFilter_oneChannel(srcImg[:, :, ind],
                                                       guidedImg[:, :, ind], rad, eps)

    dstImg = dstImg.astype(np.uint8)

    return dstImg

def main():
    img = cv2.imread(input_fn)
    print(np.shape(img))

    dstimg = my_guidedFilter_threeChannel(img, img, 15, 1)
    print(np.shape(dstimg))
    cv2.imshow('base', dstimg)
    cv2.waitKey(0)
    cv2.imwrite('Demo/00001_base.jpg', dstimg)
    detail = img - dstimg
    cv2.imshow('detail', detail)
    cv2.waitKey(0)
    cv2.imwrite('Demo/00001_detail.jpg', detail)

if __name__ == '__main__':
    main()


import requests
import datetime
import json
from bs4 import BeautifulSoup, element

# we store every trade made in an object of the class Trade
class  Trade:

    # the class has an attribute for the name of both teams
    # and a list of assets which they traded
    def __init__(self, team, date, inflow=None, outflow=None):
        self.__team = team
        if inflow is None:
            self.__inflow = []
        else:
            self.__inflow = inflow
        if outflow is None:
            self.__outflow = []
        else:
            self.__outflow = outflow

        self.__date = datetime.datetime.strptime(date, "%B %d, %Y").date()
        self.__season = self.__date.year+1 if self.__date.month > 6 else self.__date.year

    def getTeam(self):
        return self.__team

    def getInflow(self):
        return self.__inflow

    def getOutflow(self):
        return self.__outflow

    def getDate(self):
        return self.__date

    def setDate(self, date):
        self.__date = date

    def addInflow(self, assets):
        for asset in assets:
            self.__inflow.append(asset)

    def addOutflow(self, assets):
        for asset in assets:
            self.__outflow.append(asset)

    def getJSON(self):
        return {"date": self.__date.strftime("%d-%m-%Y"),
                "team": self.__team,
                "season": self.__season,
                "inflow": self.__inflow,
                "outflow": self.__outflow}

    def __str__(self):
        return self.__team1 + " - " + self.__team2

# function gets an broken up html content
def parseTrade(array, date):
    # we can skip the first entry because it only contains 'the'
    for i in range(2, len(array)):
        # because every line has a typical form:
        # The <team> traded <players>, picks ... to the <team> for <players> and picks
        # we only need the index of 'to the' to partition the sentence correctly
        index = str(array[i]).find("to the")
        if index > -1:
            year = date.split(",")[-1].strip()
            assets_1 = parseAssets(array[2:i+1], year)
            assets_2 = parseAssets(array[i+2:], year)
            return [Trade(array[1].contents[0], date, assets_2, assets_1),
                    Trade(array[i+1].contents[0], date, assets_1, assets_2)]
    return []

# this function handles 3-teams trades or any n-team trades
def parse3TeamTrade(array, date):
    # in a 3-team trade every transaction is listed, for example
    #   Atlanta Hawks traded Jeff Teague to the Indiana Pacers;
    # so we want to get the position of traded and to the
    # with that knowledge we know the position of the teams and the assets
    trade_index = []
    for idx,val in enumerate(array):
        if isinstance(val, str) and val.find("traded") != -1:
            start = idx
        if isinstance(val, str) and val.find("to the") != -1:
            end = idx
            trade_index.append((start, end))

    # after we got the start and end indices we need to get the idx of the teams
    # to add the assets to the correct team
    teams = []
    for index in trade_index:
        t1_idx = isCreated(array[index[0]-1].contents[0], teams)
        t2_idx = isCreated(array[index[1]+1].contents[0], teams)
        year = date.split(",")[-1].strip()
        # if it is not created then we need to create it
        if t1_idx == -1:
            teams.append(Trade(array[index[0]-1].contents[0], date))
            t1_idx = len(teams)-1
        # if it is not created then we need to create it
        if t2_idx == -1:
            teams.append(Trade(array[index[1]+1].contents[0], date))
            t2_idx = len(teams)-1

        # get the assets with our function parseAssets
        # we use the knowledge about the position to slice the array correctly
        if index[0] < index[1]:
            assets = parseAssets(array[index[0]:index[1]], year)
        else:
            assets = parseAssets(array[index[0]:index[1]+1], year)
        # for the first team it loses the assets
        teams[t1_idx].addOutflow(assets)
        # and the second team receives the assets
        teams[t2_idx].addInflow(assets)

    return teams

def isCreated(teamname, teams):
    for idx, team in enumerate(teams):
        if teamname == team.getTeam():
            return idx

    return -1

def commaAndSplitting(text, year):
    # we know every concatenation is either done by ',' or 'and'
    # so we split the sentence accordingly
    text_split_comma = text.split(",")
    text_split_and = text_split_comma[-1].split("and")
    if len(text_split_and) > 1:
        # if we find 'and' then we split again and the last entry of text_split_comma is now irrelevant(duplicate)
        text_split_comma.pop(-1)
        and_dps = parseDraftPicks(text_split_and, year)
        if and_dps:
            return parseDraftPicks(text_split_comma, year) + and_dps
        else:
            return parseDraftPicks(text_split_comma, year)
    else:
        return parseDraftPicks(text_split_comma, year)

def parseDraftPicks(stringarr, year):
    draft_picks = []
    for descr in stringarr:
        words = descr.split(" ")
        if "pick" in words:
            if "a" == words[0]:
                draft_picks.append({"draft_pick":{"round": int(words[2][0]),
                                    "year": year if words[1] == "future" else int(words[1])}})
            elif "Future" == words[0]:
                draft_picks.append({"draft_pick": {"round": int(words[1][0]), "year": year }})
            else:
                draft_picks.append({"draft_pick":{"round": int(words[3][0]),
                                    "year": year if words[2] == "future" else int(words[2])}})

    return draft_picks

def parseAssets(array, year):
    # these are all the fillwords used
    fillwords = [" traded ", " and ", " for ", ", ", " to the "]
    # the list to save all assets
    assets = []
    if len(array) == 1:
        # this means there is only a string
        # because players are extra entríes
        if array[0][0:8] == " traded ":
            assets = commaAndSplitting(array[0][8:-8], year)
        else:
            assets = commaAndSplitting(array[0][5:array[0].find(".")], year)

    elif len(array) > 1:
        # again we can skip the first entry because it only states 'traded'
        for elem in array[1:]:
            # we look up if it only contains fill words
            if elem in fillwords:
                continue
            else:
                # we know a player has a <a href= > Tag
                # so we can asked if the element is a Tag
                # then the Name is saved in elem.contents[0]
                if isinstance(elem, element.Tag):
                    assets.append({"player":{"playername": elem.contents[0], "link": elem["href"]}})
                else:
                    # we know it is not a player or a fillword,
                    # some the element holds information about draft picks or cash considerations
                    # we have to figure out if the string starts with an irrelevant ',' or 'and'
                    # we cut it out and use the commaAndSplitting function
                    # again we filter cash considerations
                    if elem[0] == ',':
                        start = 2
                    else:
                        start = 5
                    draft_picks = commaAndSplitting(elem[start:elem.find(".")], year)
                    if draft_picks:
                        assets = assets + commaAndSplitting(elem[start:elem.find(".")], year)

    return assets

if __name__ == '__main__':

    # use requests to get the website
    r = requests.get("http://www.basketball-reference.com/leagues/NBA_2013_transactions.html")

    # use BeautifulSoup to parse the html
    soup = BeautifulSoup(r.content, "html5lib")

    # find the column with the data
    contents = soup.find("ul", {"class": "page_index"})
    # filter all transaction by the date
    days = contents.find_all("li")

    trades = []
    for day in days:
        # find the actual date
        date = day.find("span").contents[0]
        # all transactions of a day are in <p>-Tags stored
        deals = day.find_all("p")
        for deal in deals:
            # if transaction consists of too few elements
            # it cannot be a trade
            if len(deal.contents) < 3:
                continue
            # if a coach resigns with a team then the structure of the sentence is different
            # so we have to check it
            if isinstance(deal.contents[2], element.Tag):
                continue
            # serach explicitly for the keyword 'traded'
            if deal.contents[2].find("traded") > -1:
                # call the parseTrade function and add the returning Trade object
                if deal.contents[0].find("-team trade") != -1:
                    trade_parts = parse3TeamTrade(deal.contents, date)
                else:
                    trade_parts = parseTrade(deal.contents,date)

                if not trade_parts:
                    print("ERROR")
                else:
                    for trade in trade_parts:
                        trades.append(trade)

    # create a json string of our scraped data
    json_data = json.dumps([trade.getJSON() for trade in trades])

    # storing the data in a json file
    with  open("trades/trades_2013.json", "w+") as file:
        json.dump(json_data, file)