'''
Created on 20. 4. 2017

@author: T.Filip
'''

import sys
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = r'C:\Program Files\Python35-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Program Files\Python35-32\tcl\tk8.6'

build_exe_options = {
        #"optimize": 2,
        #"includes": [],
        #"compressed": True,
        #"copy_dependent_files": True,
        #"create_shared_zip": False,
        #"append_script_to_exe": True,
        #"include_in_shared_zip": False,

        #"include_files":[('vPlot.ui'),
        #                 ('vPlot.py'),
        #                 ('company.jpg')],
        #"include_msvcr": True,
        "packages": ["pygame"],
        #"build_exe": {"packages":["pygame"],
        #                 "include_files": ["img"]}
    }

setup(
    name = "rpg",
    version = "3.6",
    description = "rpg",
    options = {"build_exe":{"packages": ["pygame"]}},
    executables = [Executable("BP.py", base = "Win32GUI")])


# -*- coding: utf-8 -*-
"""
Created on Wed Dec 18 12:09:25 2019

@author: Novin
"""
import requests
import json
from newsapi.newsapi_client import NewsApiClient
from datetime import datetime, timedelta
import errors
from transformers import AutoTokenizer, AutoModelForSequenceClassification

def getCryptoNews(startDate, endDate):
    try:
        newsapi = NewsApiClient(api_key='caea8ad1719e40e0a08d563ae3405891')
        news = []
        bitcoin_articles = newsapi.get_everything(q='bitcoin',
                                                  sources='crypto-coins-news,bloomberg,reuters,google-news',
                                                  domains='cnn,bloomberg,reuters,google',
                                                  from_param=startDate,
                                                  to=endDate,
                                                  language='en')
        BTC_articles = newsapi.get_everything(q='btc',
                                              sources='crypto-coins-news,bloomberg,reuters,google-news',
                                              domains='cnn,bloomberg,reuters,google',
                                              from_param=startDate,
                                              to=endDate,
                                              language='en')
        crypto_articles = newsapi.get_everything(q='cryptocurrency',
                                                 sources='crypto-coins-news,bloomberg,reuters,google-news',
                                                 domains='cnn,bloomberg,reuters,google',
                                                 from_param=startDate,
                                                 to=endDate,
                                                 language='en')
        blockchain_articles = newsapi.get_everything(q='blockchain',
                                                     sources='crypto-coins-news,bloomberg,reuters,google-news',
                                                     domains='cnn,bloomberg,reuters,google',
                                                     from_param=startDate,
                                                     to=endDate,
                                                     language='en')

        for item in bitcoin_articles['articles']:
            news.append(item)

        for item in BTC_articles['articles']:
            news.append(item)

        for item in crypto_articles['articles']:
            news.append(item)
        for item in blockchain_articles['articles']:
            news.append(item)

    except ConnectionError:
        raise errors.DataProvidingException(message="Faild to connect google News API", code=1)
    except:
        raise errors.DataProvidingException(message="Faild to fetch news from google News API", code=1)

    return (news)

def JsonItemStandard(newsItem,classifier):
    # title : News Headline
    # articleBody : News content
    # pubDate : news timestamp
    # keywords : news keywords
    # author : author of news
    # url : url
    # summary : Breif summary about news
    # provider : news provider
    # guid : Guid code
    # pair : explicitly tag by news provider and all the folllowing
    # indicator are based on this pair
    # pivotPoint : Pivot indicator at the time of news release time
    # trendIndex : Trend Index indicator at the news release time
    # ObosIndex : ObosIndex Indicator at the  news release time
    try:

        CryptoOtions = {'btcusd', 'bitcoin', 'cryptocurrency',
                        'ethusd', 'etherium', 'crypto', 'xpr',
                        'ripple', 'altcoin', 'crypto'}
        CommoditiesOptions = {'oil', 'gold', 'silver', 'wti', ',brent', 'commodities', 'xauusd', 'metals'}
        item = {}
        item['title'] = newsItem['title']
        item['articleBody'] = newsItem['description']
        currentDate = datetime.strptime(newsItem['publishedAt'], '%Y-%m-%dT%H:%M:%SZ')
        # currentDateString = currentDate.strftime('%a, %d %b %Y %H:%M:%S Z')
        # print(currentDateString)
        item['pubDate'] = int(currentDate.timestamp())
        item['keywords'] = ['bitcoin', 'btc', 'cryptocurrency']
        item['author'] = newsItem['author']
        # item['sentiment'], item['sentimentScore'], item['vector'] = predict(item)
        item['link'] = newsItem['url']
        item['provider'] = newsItem['source']['name']
        item['summary '] = ''
        item['thImage'] = newsItem['urlToImage']
        item['category'] = 'Cryptocurrency'
        results = classifier(item['title'])
        for result in results:
            item['Negative'] = result[0]['score']
            item['neutral'] = result[1]['score']
            item['Positive'] = result[2]['score']

        return item
    except:
        raise errors.DataProvidingException(message="Fail to standard news Item!", code=1)

def saveInMongo1(newsItem,classifier):
    try:

        for item in newsItem:
            item = JsonItemStandard(item,classifier)
            querry = {'link': str(item['link'])}
            # mydoc = mycol.find(querry)
            exist = checkForExist1(querry)
            if not exist:
                # item = JsonItemStandard (item)
                url = 'http://localhost:5000/Robonews/v1/news'
                resp = requests.post(url, json=item)
                print(resp.text)

        print('+---------------------------------------------+')
        return True
    except OSError:
        raise errors.DataProvidingException("Failed to save news in MongoEngine", code=1)
    except :
        raise errors.DataProvidingException("Failed to save news in MongoEngine", code=2)

def checkForExist1(query):
    try:

        url = 'http://localhost:5000/Robonews/v1/news'
        resp = requests.get(url, params=query)
        resp = json.loads(resp.text)
        return resp['data']
    except:
        raise errors.DataProvidingException(message="Failed to check news Existance!", code=1)

def bitcoinNewsApi(classifier):
    try:

        endDate = datetime.today()
        startDate = datetime.today() - timedelta(days=29)
        # load RSS File From Url
        now = datetime.now()
        print('Crawling of Bitcoin News from API Started ' + now.strftime('%a, %d %b %Y %H:%M:%S Z') + '!!')
        print('+---------------------------------------------+')
        newsitems = getCryptoNews(startDate, endDate)
        # store news items in a csv file
        saveInMongo1(newsitems,classifier)
    except errors.DataProvidingException as err:
        print("Error : {error} from source number {code} ".format(error=err.message, code=err.code))
    except:
        print("Error : Irregular Error from source number {code} ".format(code=1))

def main():
    bitcoinNewsApi()

if __name__ == "__main__":
    # calling mpai2n function
    main()


# importing key modules
import streamlit as st
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
import seaborn as sns
from PIL import Image,ImageFilter,ImageEnhance

# Title and Subheader for start of app

st.title("A Simple App using Iris Dataset")
st.text("using Streamlit")
st.header("Exploratory Data Analysis")

# Loading data

my_dataset = "iris.csv"

# To Improve speed and cache data

@st.cache(persist=True)

# define function for reading data

def load_data(dataset):
	df = pd.read_csv(os.path.join(dataset))
	return df

# Show Dataset top values and last values

if st.checkbox("Preview DataFrame"):
	data = load_data(my_dataset)
	if st.button("First 5 rows"):
		st.write(data.head())
	if st.button("Last 5 rows"):
		st.write(data.tail())
	else:
		st.write(data.head(2))

# Show No. of features in Data

if st.checkbox("Show All Features' Name"):
	data = load_data(my_dataset)
	st.text("Features in Data")
	st.write(data.columns)

# Dimensions

if st.checkbox("Show me Dimensions of my Data"):
    data_dim = st.radio('What Dimension Do You Want to Show', ('Rows', 'Columns'))
    if data_dim == 'Rows':
        data = load_data(my_dataset)
        st.text("Showing Length of Rows")
        st.write(len(data))
    if data_dim == 'Columns':
        data = load_data(my_dataset)
        st.text("Showing Length of Columns")
        st.write(data.shape[1])

# Summary

if st.checkbox("Show Summary of Dataset"):
	data = load_data(my_dataset)
	st.write(data.describe())

# Selection
if st.checkbox("Show each feature"):
    species_option = st.selectbox('Select Columns',('sepal_length','sepal_width','petal_length','petal_width','species'))
    data = load_data(my_dataset)
    if species_option == 'sepal_length':
	    st.write(data['sepal_length'])
    elif species_option == 'sepal_width':
	    st.write(data['sepal_width'])
    elif species_option == 'petal_length':
	    st.write(data['petal_length'])
    elif species_option == 'petal_width':
	    st.write(data['petal_width'])
    elif species_option == 'species':
	    st.write(data['species'])
    else:
	    st.write("Select A Column")

# Show Plots

if st.checkbox("Simple  Plot with Matplotlib "):
	data = load_data(my_dataset)
	data.plot()
	st.pyplot()

# Show Correlation Plots

if st.checkbox("Simple Correlation Plot with Seaborn "):
	data = load_data(my_dataset)
	st.write(sns.heatmap(data.corr(),annot=True))
	# Use Matplotlib to render seaborn
	st.pyplot()

# Show Plots

if st.checkbox("Bar Plot of Groups/Counts"):
	data = load_data(my_dataset)
	v_counts = data.groupby('species')
	st.bar_chart(v_counts)

# Iris Image Manipulation
@st.cache
def load_image(img):
	im =Image.open(os.path.join(img))
	return im

# Image Type
species_type = st.radio('What is the Iris Species do you want to see?',('Setosa','Versicolor','Virginica'))

if species_type == 'Setosa':
	st.text("Showing Setosa Species")
	st.image(load_image('iris-setosa.jpg'))
elif species_type == 'Versicolor':
	st.text("Showing Versicolor Species")
	st.image(load_image('iris_versicolor.jpg'))
elif species_type == 'Virginica':
	st.text("Showing Virginica Species")
	st.image(load_image('iris_virginica.jpg'))

# Show Image
if st.checkbox("Show Image/Hide Image"):
	my_image = load_image('iris-setosa.jpg')
	enh = ImageEnhance.Contrast(my_image)
	num = st.slider("Set Your Contrast Number",1.0,3.0)
	img_width = st.slider("Set Image Width",300,500)
	st.image(enh.enhance(num),width=img_width)


# coding=utf-8
import requests
import re
import json
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re

from bs4 import BeautifulSoup

class GoodListController():
    __url = ""
    __headers = {}

    def __init__(self, headers):
        __headers = headers
        return

    def openGoodsUrl(self, url):
        __url = url
        driver = webdriver.Chrome()

        driver.get(__url)

        htmlText = driver.page_source

        soup = BeautifulSoup(htmlText, "html.parser")

        script = soup.body.script

        lala = re.findall(r'var _showcase_components =\s+((.*?])+)', script.string)

        if lala:
            tt = lala[0][0].decode('utf-8')
            # print(type(lala[0][0]))

            hh = json.loads(tt)
            # print(hh[0],type(hh[0]))
            goods = hh[0][u'goods']  # 当前页中所有的道具信息

        # for x in goods:
        # 	print("\r\n")
        # 	print(x[u'alias'], x[u'title'])
        # pass

        # sou = re.findall(r'\"alias\":\"(\w+)', script.string)
        # sou = self.delRepetition(sou)
        # title = re.findall(r'\"title":\"(.+?)\"', script.string)
        # title = self.delRepetition(title)

        # for x in sou:
        # 	print x
        # pass

        # for x in title:
        # 	print x.decode('unicode_escape')
        # 	pass

        driver.quit()
        return goods

    # 去掉数组重复的项，并保留原来的顺序
    def delRepetition(self, arr):
        newArr = []
        for x in arr:
            if x not in newArr:
                newArr.append(x)
        pass
        return newArr

# url = "https://h5.youzan.com/v2/allgoods/16975645?reft=1531274513986&spm=f70430959"


# Green-Ball Python Mentoring.
'''
Napisz program, który podzieli podany ciąg po spacjach i każdy wyraz wypisze w nowej linijce
'''

wpis = input("Napisz dowolne zdanie\n")

lista = wpis.split()

for i in lista:
    if i==' ':
        print("\n")
    else:
        print(i)


import csv
import json
import os
import pickle as pkl
import random
import re
from collections import defaultdict

import parlai.core.build_data as build_data
from parlai.scripts.train_model import setup_args
import requests
from Levenshtein import distance as levenshtein_distance

def _split_data(redial_path):
    # Copied from https://github.com/RaymondLi0/conversational-recommendations/blob/master/scripts/split-redial.py
    data = []
    for line in open(os.path.join(redial_path, "train_data.jsonl"), encoding="utf-8"):
        data.append(json.loads(line))
    random.shuffle(data)
    n_data = len(data)
    split_data = [data[: int(0.9 * n_data)], data[int(0.9 * n_data):]]

    with open(os.path.join(redial_path, "train_data.jsonl"), "w", encoding="utf-8") as outfile:
        for example in split_data[0]:
            json.dump(example, outfile)
            outfile.write("\n")
    with open(os.path.join(redial_path, "valid_data.jsonl"), "w", encoding="utf-8") as outfile:
        for example in split_data[1]:
            json.dump(example, outfile)
            outfile.write("\n")

def _entity2movie(entity, abstract=""):
    # strip url
    x = entity[::-1].find("/")
    movie = entity[-x:-1]
    movie = movie.replace("_", " ")

    # extract year
    pattern = re.compile(r"\d{4}")
    match = re.findall(pattern, movie)
    year = match[0] if match else None
    # if not find in entity title, find in abstract
    if year is None:
        pattern = re.compile(r"\d{4}")
        match = re.findall(pattern, abstract)
        if match and 1900 < int(match[0]) and int(match[0]) < 2020:
            year = match[0]

    # recognize (20xx film) or (film) to help disambiguation
    pattern = re.compile(r"\(.*film.*\)")
    match = re.findall(pattern, movie)
    definitely_is_a_film = match != []

    # remove parentheses
    while True:
        pattern = re.compile(r"(.+)( \(.*\))")
        match = re.search(pattern, movie)
        if match:
            movie = match.group(1)
        else:
            break
    movie = movie.strip()

    return movie, year, definitely_is_a_film

DBPEDIA_ABSTRACT_PATH = "dbpedia/short_abstracts_en.ttl"
DBPEDIA_PATH = "dbpedia/mappingbased_objects_en.ttl"

def _build_dbpedia(dbpedia_path):
    movie2entity = {}
    movie2years = defaultdict(set)
    with open(dbpedia_path, encoding="utf-8") as f:
        for line in f.readlines():
            if line.startswith("#"):
                continue
            entity, line = line[: line.index(" ")], line[line.index(" ") + 1:]
            _, line = line[: line.index(" ")], line[line.index(" ") + 1:]
            abstract = line[:-4]
            movie, year, definitely_is_a_film = _entity2movie(entity, abstract)
            if (movie, year) not in movie2entity or definitely_is_a_film:
                movie2years[movie].add(year)
                movie2entity[(movie, year)] = entity
    return {"movie2years": movie2years, "movie2entity": movie2entity}

def _load_kg(path):
    kg = defaultdict(list)
    with open(path, encoding="utf-8") as f:
        for line in f.readlines():
            tuples = line.split()
            if tuples and len(tuples) == 4 and tuples[-1] == ".":
                h, r, t = tuples[:3]
                # TODO: include property/publisher and subject/year, etc
                if "ontology" in r:
                    kg[h].append((r, t))
    return kg

def _extract_subkg(kg, seed_set, n_hop, log_file):
    print("Seed num " + str(len(seed_set)))
    log_file.write("Seed num " + str(len(seed_set)) + "\n")
    subkg = defaultdict(list)
    subkg_hrt = set()

    ripple_set = []
    for h in range(n_hop):
        memories_h = []
        memories_r = []
        memories_t = []

        if h == 0:
            tails_of_last_hop = seed_set
        else:
            tails_of_last_hop = ripple_set[-1][2]

        for entity in tails_of_last_hop:
            for tail_and_relation in kg[entity]:
                h, r, t = entity, tail_and_relation[0], tail_and_relation[1]
                if (h, r, t) not in subkg_hrt:
                    subkg[h].append((r, t))
                    subkg_hrt.add((h, r, t))
                memories_h.append(h)
                memories_r.append(r)
                memories_t.append(t)

        ripple_set.append((memories_h, memories_r, memories_t))

    return subkg

def build(opt):
    # get path to data directory
    dpath = os.path.join(opt["datapath"], "redial")
    # define version if any
    version = None
    log_file = open(os.path.join(dpath, "log.txt"), "a")

    # check if data had been previously built
    # if not build_data.built(dpath, version_string=version):
    if True:
        print("[building data: " + dpath + "]")

        # make a clean directory if needed
        if build_data.built(dpath):
            # an older version exists, so remove these outdated files.
            build_data.remove_dir(dpath)
        build_data.make_dir(dpath)

        # download the data.
        fname = "redial_dataset.zip"
        url = "https://github.com/ReDialData/website/raw/data/" + fname  # dataset URL
        build_data.download(url, dpath, fname)

        # uncompress it
        build_data.untar(dpath, fname)

        _split_data(dpath)

        dbpedia = _build_dbpedia(DBPEDIA_ABSTRACT_PATH)
        movie2entity = dbpedia["movie2entity"]
        movie2years = dbpedia["movie2years"]

        # print(movie2entity)
        kg = _load_kg(DBPEDIA_PATH)

        # Match REDIAL movies to dbpedia entities
        movies_with_mentions_path = os.path.join(dpath, "movies_with_mentions.csv")
        with open(movies_with_mentions_path, "r", encoding="utf-8") as f:
            reader = csv.reader(f)
            id2movie = {int(row[0]): row[1] for row in reader if row[0] != "movieId"}
        id2entity = {}
        count = 0
        previous_non_matched = 0
        for movie_id in id2movie:
            movie = id2movie[movie_id]
            pattern = re.compile(r"(.+)\((\d+)\)")
            match = re.search(pattern, movie)
            if match is not None:
                name, year = match.group(1).strip(), match.group(2)
            else:
                name, year = movie.strip(), None
            if year is not None:
                if (name, year) in movie2entity:
                    id2entity[movie_id] = movie2entity[(name, year)]
                else:
                    if len(movie2years) == 1:
                        id2entity[movie_id] = movie2entity[(name, movie2years[name][0])]
                    else:
                        # id2entity[movie_id] = None
                        previous_non_matched += 1
                        ent = get_entity(name, kg, log_file)
                        print("Final output")
                        print(ent)
                        log_file.write("Final output"+"\n")
                        if ent is None:
                            log_file.write("None")
                        else:
                            log_file.write(ent)
                        log_file.write("\n")
                        id2entity[movie_id] = ent
                        if ent is None:
                            count += 1

            else:
                if (name, year) in movie2entity:
                    id2entity[movie_id] = movie2entity[(name, year)]
                else:
                    # id2entity[movie_id] = None
                    previous_non_matched += 1
                    ent = get_entity(name, kg, log_file)
                    print("Final output")
                    print(ent)
                    log_file.write("Final output"+"\n")
                    if ent is None:
                        log_file.write("None")
                    else:
                        log_file.write(ent)
                    log_file.write("\n")
                    id2entity[movie_id] = ent
                    if ent is None:
                        count += 1
                #     id2entity[movie_id] = (
                #     movie2entity[(name, year)] if (name, year) in movie2entity else None
                # )
        # HACK: make sure movies are matched to different entities
        matched_entities = set()
        for movie_id in id2entity:
            if id2entity[movie_id] is not None:
                if id2entity[movie_id] not in matched_entities:
                    matched_entities.add(id2entity[movie_id])
                else:
                    previous_non_matched += 1
                    movie = id2movie[movie_id]
                    pattern = re.compile(r"(.+)\((\d+)\)")
                    match = re.search(pattern, movie)
                    if match is not None:
                        name, year = match.group(1).strip(), match.group(2)
                    else:
                        name, year = movie.strip(), None
                    # print("In hack: movie name: " + name)
                    ent = get_entity(name, kg, log_file)
                    print("Final output")
                    print(ent)
                    log_file.write("Final output"+"\n")
                    if ent is None:
                        log_file.write("None")
                    else:
                        log_file.write(ent)
                    log_file.write("\n")
                    if ent is None:
                        id2entity[movie_id] = None
                        count += 1
                    else:
                        if ent not in matched_entities:
                            matched_entities.add(ent)
                            id2entity[movie_id] = ent
                            # print("Inserting entity")
                            # print(ent)
                        else:
                            id2entity[movie_id] = None
                            count += 1

        print("Not matched movies: " + str(count))
        print("Previous Not matched movies: " + str(previous_non_matched))
        log_file.write("Not matched movies: " + str(count) + "\n")
        log_file.write("Previous Not matched movies: " + str(previous_non_matched) + "\n")

        # Extract sub-kg related to movies

        subkg = _extract_subkg(
            kg,
            [
                id2entity[k]
                for k in id2entity
                if id2entity[k] is not None and kg[id2entity[k]] != []
            ],
            2,
            log_file
        )

        not_empty_id = 0
        for movie_id in id2entity:
            if id2entity[movie_id] is not None:
                subkg[id2entity[movie_id]].append(('self_loop', id2entity[movie_id]))
                not_empty_id += 1
            else:
                subkg[movie_id].append(('self_loop', movie_id))
        print("Not empty id num "+str(not_empty_id))
        log_file.write("Not empty id num "+str(not_empty_id) + "\n")
        entities = set([k for k in subkg]) | set([x[1] for k in subkg for x in subkg[k]])
        entity2entityId = dict([(k, i) for i, k in enumerate(entities)])
        relations = set([x[0] for k in subkg for x in subkg[k]])
        relation2relationId = dict([(k, i) for i, k in enumerate(relations)])
        subkg_idx = defaultdict(list)
        for h in subkg:
            for r, t in subkg[h]:
                subkg_idx[entity2entityId[h]].append((relation2relationId[r], entity2entityId[t]))
        movie_ids = []
        for k in id2entity:
            movie_ids.append(entity2entityId[id2entity[k]] if id2entity[k] is not None else entity2entityId[k])

        pkl.dump(id2entity, open(os.path.join(dpath, "id2entity.pkl"), "wb"))
        pkl.dump(dbpedia, open(os.path.join(dpath, "dbpedia.pkl"), "wb"))
        pkl.dump(subkg_idx, open(os.path.join(dpath, "subkg.pkl"), "wb"))
        pkl.dump(entity2entityId, open(os.path.join(dpath, "entity2entityId.pkl"), "wb"))
        pkl.dump(relation2relationId, open(os.path.join(dpath, "relation2relationId.pkl"), "wb"))
        pkl.dump(movie_ids, open(os.path.join(dpath, "movie_ids.pkl"), "wb"))

        # mark the data as built
        build_data.mark_done(dpath, version_string=version)
        log_file.close()

def get_entity(movie_name, kg, log_file):
    print("###############################################")
    print(movie_name)
    log_file.write("###############################################"+"\n")
    log_file.write(movie_name+"\n")
    if len(movie_name) <= 0:
        return None
    threshold_dist_with_film = 10
    threshold_dist_without_film = 10
    url = "http://localhost:4999/processQuery"
    payload_without_film = {"nlquery": movie_name}
    res_without_film = requests.post(url, json=payload_without_film, headers={"Content-Type": "application/json"})
    print(res_without_film.json())
    log_file.write(json.dumps(res_without_film.json()))
    log_file.write("\n")
    payload_with_film = {"nlquery": movie_name+" film"}
    res_with_film = requests.post(url, json=payload_with_film, headers={"Content-Type": "application/json"})
    print(res_with_film.json())
    log_file.write(json.dumps(res_with_film.json()))
    log_file.write("\n")
    entities_without_film = []
    keys = ["0", "1", "2", "3", "4"]
    if "rerankedlists" in res_without_film.json():
        for key in keys:
            if key in res_without_film.json()["rerankedlists"]:
                if len(res_without_film.json()["rerankedlists"][key]) > 0:
                    entities_list = res_without_film.json()["rerankedlists"][key]
                    list_len = len(entities_list)
                    for i in range(list_len):
                        # generated_ent = "<" + entities_list[i][1] + ">"
                        # if kg[generated_ent] != []:
                        entities_without_film.append(entities_list[i][1])

    entities_with_film = []
    if "rerankedlists" in res_with_film.json():
        for key in keys:
            if key in res_with_film.json()["rerankedlists"]:
                if len(res_with_film.json()["rerankedlists"][key]) > 0:
                    entities_list = res_with_film.json()["rerankedlists"][key]
                    list_len = len(entities_list)
                    for i in range(list_len):
                        # generated_ent = "<" + entities_list[i][1] + ">"
                        # if kg[generated_ent] != []:
                        entities_with_film.append(entities_list[i][1])

    for ent in entities_with_film:
        if ent.endswith("_(film)"):
            movie_name_extracted = get_movie_name_from_entity(ent[:-7])
            dist = levenshtein_distance(movie_name_extracted, movie_name)
            dist_percentage = (dist * 100) / len(movie_name)
            if dist_percentage <= threshold_dist_with_film:
                return "<" + ent + ">"
        else:
            entities_without_film.append(ent)

    lowest_dist = 100000000
    lowest_ent = None
    for ent in entities_without_film:
        movie_name_extracted = get_movie_name_from_entity(ent)
        dist = levenshtein_distance(movie_name_extracted, movie_name)
        dist_percentage = (dist*100)/len(movie_name)
        if dist_percentage <= threshold_dist_without_film and dist_percentage < lowest_dist:
            lowest_dist = dist_percentage
            lowest_ent = "<" + ent + ">"

    return lowest_ent

def get_movie_name_from_entity(entity):
    frost_nixon = "http://dbpedia.org/resource/Frost/Nixon"
    farenheit = "http://dbpedia.org/resource/Fahrenheit_9/11"
    if (entity == frost_nixon) or (entity == farenheit):
        x = find_nth_overlapping(entity[::-1], "/", 2)
    else:
        x = entity[::-1].find("/")
    movie = entity[-x:]
    return movie.replace("_", " ")

def find_nth_overlapping(haystack, needle, n):
    start = haystack.find(needle)
    while start >= 0 and n > 1:
        start = haystack.find(needle, start+1)
        n -= 1
    return start

if __name__ == "__main__":
    parser = setup_args()
    parser.set_defaults(
        task="redial",
        dict_tokenizer="split",
        model="kbrd",
        dict_file="saved/tmp",
        model_file="saved/kbrd",
        fp16=True,
        batchsize=256,
        n_entity=64368,
        n_relation=214,
        # validation_metric="recall@50",
        validation_metric="base_loss",
        validation_metric_mode='min',
        validation_every_n_secs=30,
        validation_patience=5,
        tensorboard_log=True,
        tensorboard_tag="task,model,batchsize,dim,learningrate,model_file",
        tensorboard_metrics="loss,base_loss,kge_loss,l2_loss,acc,auc,recall@1,recall@10,recall@50",
    )
    opt = parser.parse_args()
    build(opt)


class Solution:
    def findKth(self,num,n,K):
        def quick_sort(num):
            if len(num) <= 1:
                return num
            pivot =num[0]
            left = quick_sort([x for x in num if x<pivot])
            right = quick_sort([x for x in num if x>pivot])
            return left + [pivot] + right
        num = quick_sort(num)
        return num[n-k]

if __name__ == '__main__':
    numbers= [1,3,5,2,2]
    num=5
    k=3
    s= Solution()
    print(s.findKth(numbers,num,k))


import jdatetime
import datetime
import locale
from django.contrib.auth.models import User
from main.models import Teacher, Student, Manager
from manager.models import ManagerForm
from django.db import models
from django.utils import timezone
from django.utils.timezone import now, timedelta
from django.utils import timezone
from django.utils.timezone import utc
from finglish import f2p
from django.utils.timezone import localtime
from khayyam import *

# Create your models here.
class TeacherForm(models.Model):
    user = models.OneToOneField(Teacher, on_delete=models.CASCADE, null=True, blank=True)
    avatar = models.ImageField(upload_to='uploads/teacher', default="defaults/avatar/default.jpg")
    phone_number = models.CharField(max_length=11, null=True, blank=True)

    def __str__(self):
        return self.user.get_full_name()

class ClassRoom(models.Model):
    name = models.CharField(max_length=1000)
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, blank=True, null=True)
    # GRADE = (
    #    ('12', 'کنکوری'),
    #    ('11', 'پایه یازدهم'),
    #    ('10', 'پایه دهم'),
    # )
    # grade = models.CharField(choices=GRADE, max_length=2, blank=True, null=True)
    # FIELD = (
    #    ('1', 'علوم تجربی'),
    #    ('2', 'ریاضی و فیزیک'),
    #    ('3', 'انسانی'),
    # )
    # field = models.CharField(choices=FIELD, max_length=2, blank=True, null=True)
    students = models.ManyToManyField(Student, blank=True)
    initial_date = models.DateTimeField(default=now)
    start_date = models.DateField()
    end_date = models.DateField()
    class_link = models.CharField(max_length=1000, blank=True, null=True)
    class_cap = models.IntegerField()
    is_active = models.BooleanField(default=False)
    open_signup = models.BooleanField(default=False)

    def __str__(self):
        return self.name

class Question(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
    body = models.TextField()
    LAYOUT = (
        ('horizontal', "افقی"),
        ('vertical', "عمودی"),
        ('2in2', "دو در دو")
    )
    choice_layout = models.CharField(choices=LAYOUT, max_length=10, default="horizontal")
    choice_1 = models.TextField()
    choice_2 = models.TextField()
    choice_3 = models.TextField()
    choice_4 = models.TextField()
    choice_5 = models.TextField()
    correct_ans = models.CharField(max_length=1, blank=True, null=True)
    verbose_ans = models.TextField(null=True, blank=True)
    grades = models.CharField(max_length=1000, blank=True, null=True)
    SOURCE = (
        ('Author', 'تالیفی'),
        ('2', 'کنکور سراسری'),
        ('3', 'المپیاد'),
        ('Sanjesh', 'سنجش'),
        ('kanoon', 'قلم چی'),
        ('Gozine2', 'گزینه دو'),
        ('7', 'سایر'),
    )
    source = models.CharField(choices=SOURCE, max_length=10, default='1')
    LEVEL = (
        ('1', 'ساده'),
        ('2', 'متوسط'),
        ('3', 'دشوار'),
        ('4', 'پیل افکن'),
    )
    level = models.CharField(choices=LEVEL, max_length=1, blank=True, null=True)
    is_publish = models.BooleanField(default=False)
    is_descriptive = models.BooleanField(default=False)
    on_write = models.BooleanField(default=False)
    is_edited = models.BooleanField(default=False)
    visibility = models.BooleanField(default=True)
    created_at = models.DateTimeField(default=timezone.now)

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

class TempQuestion(models.Model):
    related_question = models.OneToOneField(Question, on_delete=models.CASCADE)
    editor_user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
    body = models.TextField()
    LAYOUT = (
        ('horizontal', "افقی"),
        ('vertical', "عمودی"),
        ('2in2', "دو در دو")
    )
    choice_layout = models.CharField(choices=LAYOUT, max_length=10, default="horizontal")
    choice_1 = models.TextField()
    choice_2 = models.TextField()
    choice_3 = models.TextField()
    choice_4 = models.TextField()
    choice_5 = models.TextField()
    correct_ans = models.CharField(max_length=1, blank=True, null=True)
    verbose_ans = models.TextField(null=True, blank=True)
    grades = models.CharField(max_length=1000, blank=True, null=True)
    SOURCE = (
        ('Author', 'تالیفی'),
        ('2', 'کنکور سراسری'),
        ('3', 'المپیاد'),
        ('Sanjesh', 'سنجش'),
        ('kanoon', 'قلم چی'),
        ('Gozine2', 'گزینه دو'),
        ('7', 'سایر'),
    )
    source = models.CharField(choices=SOURCE, max_length=10, default='1')
    LEVEL = (
        ('1', 'ساده'),
        ('2', 'متوسط'),
        ('3', 'دشوار'),
        ('4', 'پیل افکن'),
    )
    level = models.CharField(choices=LEVEL, max_length=1, blank=True, null=True)
    is_publish = models.BooleanField(default=False)
    is_descriptive = models.BooleanField(default=False)
    on_write = models.BooleanField(default=False)
    created_at = models.DateTimeField(default=timezone.now)

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

class Exam(models.Model):
    creator = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
    name = models.CharField(max_length=1000, blank=True, null=True)
    info = models.TextField(blank=True, null=True)
    className = models.ManyToManyField(ClassRoom, blank=True)
    initial_date = models.DateTimeField(default=now, blank=True, null=True)
    hold_from = models.DateTimeField(blank=True, null=True)
    hold_to = models.DateTimeField(blank=True, null=True)
    preview = models.BooleanField(default=False)
    code = models.CharField(max_length=100, blank=True, null=True)
    suggested_time = models.TimeField(null=True, blank=True)
    is_online = models.BooleanField(default=True)
    is_submit = models.BooleanField(default=False)
    is_edit = models.BooleanField(default=False)
    is_add = models.BooleanField(default=False)
    is_publish = models.BooleanField(default=False)
    examKey = models.CharField(max_length=1000, blank=True, null=True)  # answers: 0,1,2,3,4,5,-
    keyMapper = models.CharField(max_length=1000, blank=True, null=True)  # answers: 0,1,2,3,4,5,-

    def __str__(self):
        jdatetime.set_locale(locale='fa_IR')
        date = jdatetime.date.fromgregorian(
            day=self.initial_date.day,
            month=self.initial_date.month,
            year=self.initial_date.year
        )
        return f2p(date.strftime('%d %B %y'))

class QuestionPack(models.Model):
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, blank=True, null=True)
    name = models.CharField(max_length=1000, blank=True, null=True)
    second = models.IntegerField(default=0)
    minute = models.IntegerField(default=0)
    hour = models.IntegerField(default=0)
    info = models.TextField(blank=True, null=True)
    questions = models.ManyToManyField(Question)
    submit = models.BooleanField(default=False)

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

class ExamQuestion(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    exam = models.ForeignKey(Exam, on_delete=models.CASCADE)
    STATE = (
        ('del', "حذف شده"),
        ('add', 'اضافه شده'),
        ('submit', "ثبت شده")
    )
    state = models.CharField(choices=STATE, max_length=6, default="add")
    position = models.IntegerField(default=0)

    def __str__(self):
        return str(self.exam) + " - " + str(self.position)

class ERun(models.Model):
    name = models.CharField(max_length=1000, null=True, blank=True)
    info = models.TextField(null=True, blank=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    class_name = models.ManyToManyField(ClassRoom, blank=True)
    add_on_user = models.ManyToManyField(Student, blank=True)
    TYPE = (
        ('float', 'شناور'),
        ('fix', 'ایستا')
    )
    type = models.CharField(choices=TYPE, max_length=5, default="float")
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    time = models.CharField(max_length=1000, default="")
    reject = models.BooleanField(default=False)
    is_publish = models.BooleanField(default=True)
    participant_num = models.IntegerField(default=0)
    repeat_num = models.IntegerField(default=1)
    has_negative_point = models.BooleanField(default=False)
    negative_point = models.CharField(max_length=10, blank=True, null=True)

    def __str__(self):
        return self.name

    @property
    def is_active(self):
        if self.start_time < now() < self.end_time:
            return True
        else:
            return False

    def jd_start_time(self):
        time = localtime(self.start_time)
        date = jdatetime.datetime.fromgregorian(
            minute=time.minute,
            hour=time.hour,
            day=time.day,
            month=time.month,
            year=time.year
        )
        return date.strftime("%A %B")

    def jd_end_time(self):
        time = localtime(self.end_time)
        # locale.setlocale(utc)
        #
        date = jdatetime.datetime.fromgregorian(
            minute=time.minute,
            hour=time.hour,
            day=time.day,
            month=time.month,
            year=time.year
        )
        return date.strftime("%d %B %y %H:%M")

class ExamERun(models.Model):
    name = models.CharField(max_length=1000, blank=True, null=True)
    exam = models.ForeignKey(Exam, on_delete=models.CASCADE)
    e_run = models.ForeignKey(ERun, on_delete=models.CASCADE)
    time = models.TimeField()
    random_question = models.BooleanField(default=False)
    random_choice = models.BooleanField(default=False)
    position = models.IntegerField(default=0)

    def __str__(self):
        return str(self.exam) + " - " + str(self.e_run)

class Report(models.Model):
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, blank=True, null=True)
    # className = models.ForeignKey(Class, on_delete=models.CASCADE, blank=True, null=True)
    title = models.CharField(max_length=1000, null=True, blank=True)
    text = models.TextField()
    is_seen = models.BooleanField(default=False)
    date_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        jdatetime.set_locale("fa_IR")
        date = jdatetime.datetime.fromgregorian(
            second=self.date_time.second,
            minute=self.date_time.minute,
            hour=self.date_time.hour,
            day=self.date_time.day,
            month=self.date_time.month,
            year=self.date_time.year
        )
        output = self.teacher.user.get_full_name() + " - " + f2p(date.strftime("%d %B %y"))
        return output

    def get_teacher_avatar(self):
        avatar = TeacherForm.objects.get(user=self.teacher).avatar
        return avatar.url

    def get_time_diff(self):
        now_time = datetime.datetime.utcnow().replace(tzinfo=utc)
        time_diff = (now_time - self.date_time).total_seconds()
        SECOND = 1
        MINUTE = 60 * SECOND
        HOUR = 60 * MINUTE
        DAY = 24 * HOUR
        MONTH = 30 * DAY

        if time_diff < MINUTE:
            return "لحظاتی قبل"
        elif time_diff < 2 * MINUTE:
            return "یک دقیقه قبل"
        elif time_diff < 60 * MINUTE:
            minute = int(time_diff / 60)
            return str(minute) + " دقیقه قبل"
        elif time_diff < 120 * MINUTE:
            return "یک ساعت قبل"
        elif time_diff < 24 * HOUR:
            hour = int(time_diff / 3600)
            return str(hour) + " ساعت قبل"
        elif time_diff < 48 * HOUR:
            return "دیروز"
        elif time_diff < 30 * DAY:
            day = int(time_diff / 86400)
            return str(day) + " روز قبل"
        elif time_diff < 12 * MONTH:
            month = int(time_diff / 2592000)
            if month == 1:
                return "یک ماه قبل"
            return str(month) + " ماه قبل"
        else:
            year = int(time_diff / (MONTH * 12))
            if year == 1:
                return "پارسال"
            return str(year) + " سال قبل"

class ReportReply(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
    report = models.ForeignKey(Report, on_delete=models.CASCADE, blank=True, null=True)
    text = models.TextField()
    date_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        jdatetime.set_locale("fa_IR")
        date = jdatetime.datetime.fromgregorian(
            second=self.date_time.second,
            minute=self.date_time.minute,
            hour=self.date_time.hour,
            day=self.date_time.day,
            month=self.date_time.month,
            year=self.date_time.year
        )
        output = self.user.get_full_name() + " - " + f2p(date.strftime("%d %B %y"))
        return output

    def get_sender_avatar(self):
        try:
            avatar = TeacherForm.objects.get(user=self.user.teacher).avatar
            return avatar.url
        except:
            avatar = ManagerForm.objects.get(user=self.user.manager).avatar
            return avatar.url

    def get_time_diff(self):
        now_time = datetime.datetime.utcnow().replace(tzinfo=utc)
        time_diff = (now_time - self.date_time).total_seconds()
        SECOND = 1
        MINUTE = 60 * SECOND
        HOUR = 60 * MINUTE
        DAY = 24 * HOUR
        MONTH = 30 * DAY

        if time_diff < MINUTE:
            return "لحظاتی قبل"
        elif time_diff < 2 * MINUTE:
            return "یک دقیقه قبل"
        elif time_diff < 60 * MINUTE:
            minute = int(time_diff / 60)
            return str(minute) + " دقیقه قبل"
        elif time_diff < 120 * MINUTE:
            return "یک ساعت قبل"
        elif time_diff < 24 * HOUR:
            hour = int(time_diff / 3600)
            return str(hour) + " ساعت قبل"
        elif time_diff < 48 * HOUR:
            return "دیروز"
        elif time_diff < 30 * DAY:
            day = int(time_diff / 86400)
            return str(day) + " روز قبل"
        elif time_diff < 12 * MONTH:
            month = int(time_diff / 2592000)
            if month == 1:
                return "یک ماه قبل"
            return str(month) + " ماه قبل"
        else:
            year = int(time_diff / (MONTH * 12))
            if year == 1:
                return "پارسال"
            return str(year) + " سال قبل"

class ReportAttach(models.Model):
    TYPE = (
        ("report", "گزارش"),
        ("report_replay", "پاسخ گزارش")
    )
    type = models.CharField(choices=TYPE, max_length=20, default="report")
    report = models.ForeignKey(Report, on_delete=models.CASCADE, blank=True, null=True)
    report_replay = models.ForeignKey(ReportReply, on_delete=models.CASCADE, blank=True, null=True)
    file = models.FileField(upload_to='reports/')
    uploaded_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.file.name

    def get_file_size(self):
        file_size = self.file.size
        if file_size < 1000:
            file_size = str(file_size) + " B"
        elif 1000 < file_size < 1000000:
            file_size = str(int(file_size / 1000)) + " KB"
        else:
            file_size = str(int(file_size / 1000000)) + " MB"
        return file_size


import numpy as np

def forward(z):
    """forward pass for sparsemax
    this will process a 2d-array $z$, where axis 1 (each row) is assumed to be
    the the z-vector.
    """

    # sort z
    z_sorted = np.sort(z, axis=1)[:, ::-1]

    # calculate k(z)
    z_cumsum = np.cumsum(z_sorted, axis=1)
    k = np.arange(1, z.shape[1] + 1)
    z_check = 1 + k * z_sorted > z_cumsum
    # use argmax to get the index by row as .nonzero() doesn't
    # take an axis argument. np.argmax return the first index, but the last
    # index is required here, use np.flip to get the last index and
    # `z.shape[axis]` to compensate for np.flip afterwards.
    k_z = z.shape[1] - np.argmax(z_check[:, ::-1], axis=1)

    # calculate tau(z)
    tau_sum = z_cumsum[np.arange(0, z.shape[0]), k_z - 1]
    tau_z = ((tau_sum - 1) / k_z).reshape(-1, 1)

    return np.maximum(0, z - tau_z)

def jacobian(z):
    """jacobian for sparsemax
    this will process a 2d-array $z$, where axis 1 (each row) is assumed to be
    the the z-vector.
    """

    # Construct S(z)
    # Possibly this could be reduced to just calculating k(z)
    p = forward(z)
    s = p > 0
    s_float = s.astype('float64')

    # row-wise outer product
    # http://stackoverflow.com/questions/31573856/theano-row-wise-outer-product-between-two-matrices
    jacobian = s_float[:, :, np.newaxis] * s_float[:, np.newaxis, :]
    jacobian /= - np.sum(s, axis=1)[:, np.newaxis, np.newaxis]

    # add delta_ij
    obs, index = s.nonzero()
    jacobian[obs, index, index] += 1

    return jacobian

def Rop(z, v):
    """Jacobian vector product (Rop) for sparsemax
    This calculates [J(z_i) * v_i, ...]. `z` is a 2d-array, where axis 1
    (each row) is assumed to be the the z-vector. `v` is a matrix where
    axis 1 (each row) is assumed to be the `v-vector`.
    """

    # Construct S(z)
    p = forward(z)
    s = p > 0

    # Calculate \hat{v}, which will be a vector (scalar for each z)
    v_hat = np.sum(v * s, axis=1) / np.sum(s, axis=1)

    # Calculates J(z) * v
    return s * (v - v_hat[:, np.newaxis])


from random import randint
from numpy import random, arange, array
import numpy as np
import sys
MIN = 0
def randomint(begin, end):
    return randint(begin, end)
def createfile(filepath, numvertical):
    a = [[0 for x in range(numvertical)] for y in range(numvertical)]
    for i in range(0,numvertical):
        for j in range(0, numvertical):
            if i != j:
                if numvertical < 200:   #  64, 128
                    if random.choice(arange(0,2), p=[0.50, 0.50]) == 0:
                        a[i][j] = randomint(1,999)
                elif numvertical < 800: # 256, 512
                    if random.choice(arange(0,2), p=[0.40, 0.60]) == 0:
                        a[i][j] = randomint(1,999)
                elif numvertical < 2500: # 1024, 2048
                    if random.choice(arange(0,2), p=[0.20, 0.80]) == 0:
                        a[i][j] = randomint(1,999)
                elif numvertical < 10000:    # 4096, 8192
                    if random.choice(arange(0,2), p=[0.15, 0.85]) == 0:
                        a[i][j] = randomint(1,999)
                else:   # 16384
                    if random.choice(arange(0,2), p=[0.10, 0.90]) == 0:
                        a[i][j] = randomint(1,999)

    a = np.array(a)
    a_sym = np.tril(a) + np.tril(a, -1).T

    count_number_dif_zero = np.count_nonzero(a_sym)
    number_of_edges = count_number_dif_zero / 2

    file1 = open(filepath + ".txt","w")
    file1.write(str(numvertical)+"\n")
    for i in range(0,numvertical):
        for j in range(0, numvertical):
            file1.write(str(a_sym[i][j]))
            file1.write(" ")
        file1.write("\n")
    file1.close()

    file2 = open(filepath + "_mpi.txt", "w")
    file2.write(str(numvertical) + " " + str(number_of_edges) + "\n")
    for i in range(numvertical):
        for j in range(i+1, numvertical):
            if a_sym[i][j] != 0:
                file2.write(str(i) + " " + str(j) + " " + str(a_sym[i][j]) + "\n")
    file2.close()

    del a
    del a_sym
if __name__ == '__main__':
    if (len(sys.argv) < 3):
        print ('You need to put the file_path and number_of_node_in_graph.')
    else:
        numvertical = int(sys.argv[2])
        filepath = sys.argv[1]
        createfile(filepath=filepath, numvertical=numvertical)


import argparse
import os

from set.ds3d  import HaN_OAR_v2 as ProbSet
import random
from models.vnet import VNet
from tqdm import tqdm
from metrics import *
import time
import matplotlib.pyplot as plt
import numpy as np
from torch.utils.data import DataLoader
import torch

@torch.no_grad()
def predict(config, test_on, is_train, fold):
    if config.model_type not in ['VNet',]:
        print('ERROR!! model_type should be selected in VNet/')
        print('Your input for model_type was %s' % config.model_type)
        return

    # #train_set = ProbSet(config.train_path)
    # valid_set = ProbSet(config.valid_path,is_train=False)
    test_set = ProbSet(config.test_path,is_train=is_train, is_aug=False, return_params=True, test_on=test_on, fold=fold)
    # print(len(valid_set), len(test_set))
    #train_loader = DataLoader(train_set, batch_size=config.batch_size)
    # valid_loader = DataLoader(valid_set, batch_size=config.batch_size)
    test_loader = DataLoader(test_set, batch_size=config.batch_size)

    net = VNet()

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    net.to(device)
    # print(config.model_type, net)

    net.load_state_dict(torch.load(config.net_path))
    net.eval()

    DC = 0.  # Dice Coefficient
    length = 0
    iou = 0
    for i, (imgs, gts, _, case) in enumerate(test_loader):

        #path = path[0] # 因为经过了loader被wrap进了元组 又因为batchsize=1
        case = case[0]

        imgs = imgs.to(device)
        gts = gts.round().long().to(device)

        outputs = net(imgs)
        print(gts.cpu().shape, imgs.shape, outputs.shape)
        # torch.Size([1, 1, 128, 128, 128]) torch.Size([1, 1, 128, 128, 128]) torch.Size([1, 14, 128, 128, 128])
        #print(path)
        ious = IoU(gts.detach().cpu().squeeze().numpy().reshape(-1),
                   outputs.detach().cpu().squeeze().argmax(dim=0).numpy().reshape(-1), num_classes=14)
        print(ious)
        print(np.array(ious).mean())
        iou += np.array(ious).mean()
        #print(path)
        #output_id = path.split('/')[-1]
        np.save('/mnt/EXTRA/datasets/competitions/aug/{}/{}/vnet-fold{}-z128-halved-clahe.npy'.format(TEST_ON,case,fold), outputs.detach().cpu().squeeze().numpy())
        print(case,outputs.detach().cpu().squeeze().numpy().shape)

        # for j in range(70,128):
        #     plt.figure()
        #     plt.subplot(2,2,1)
        #     # plt.imshow(np.array(imgs.cpu().squeeze()[j,0]))
        #     plt.imshow(np.array(imgs.cpu().squeeze()[j]))
        #     plt.colorbar()
        #     plt.subplot(2, 2, 2)
        #     plt.title(np.unique(np.array(gts.cpu().detach().numpy().squeeze()[j])))
        #     plt.imshow(np.array(gts.cpu().detach().numpy().squeeze()[j]))
        #     plt.colorbar()
        #     plt.subplot(2, 2, 3)
        #     plt.title(np.unique(outputs.cpu().detach().numpy().squeeze().argmax(axis=0)[j]))
        #     plt.imshow(outputs.cpu().detach().numpy().squeeze().argmax(axis=0)[j].reshape(128,128))
        #     #plt.imshow(outputs.cpu().detach().numpy().squeeze()[8,j].reshape(128, 128))
        #     plt.colorbar()
        #     plt.show()
        #     time.sleep(2)

    print('######', iou/10)
            # np.save('/mnt/HDD/datasets/HaN_OAR/image70.npy', np.array(imgs.cpu().squeeze()[i]))
            # np.save('/mnt/HDD/datasets/HaN_OAR/prediction70.npy', np.array(torch.sigmoid(outputs.cpu().detach()).numpy().squeeze()[i]))
    #     if config.output_ch == 1:
    #         outputs = torch.sigmoid(outputs)
    #
    #     acc += get_accuracy(outputs, gts) * imgs.size(0)
    #     SE += get_sensitivity(outputs, gts) * imgs.size(0)
    #     SP += get_specificity(outputs, gts) * imgs.size(0)
    #     PC += get_precision(outputs, gts) * imgs.size(0)
    #     F1 += get_F1(outputs, gts) * imgs.size(0)
    #     JS += get_JS(outputs, gts) * imgs.size(0)
    #     DC += get_DC(outputs, gts) * imgs.size(0)
    #     length += imgs.size(0)
    #
    # acc = acc / length
    # SE = SE / length
    # SP = SP / length
    # PC = PC / length
    # F1 = F1 / length
    # JS = JS / length
    # DC = DC / length
    # score = JS + DC
    # print('[Validation] Acc: %.4f, SE: %.4f, SP: %.4f, PC: %.4f, F1: %.4f, JS: %.4f, DC: %.4f'
    #       % (acc, SE, SP, PC, F1, JS, DC))

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    # model hyper-parameters

    # training hyper-parameters
    parser.add_argument('--num_epochs', type=int, default=100)
    parser.add_argument('--num_epochs_decay', type=int, default=70)
    parser.add_argument('--batch_size', type=int, default=1)
    parser.add_argument('--num_workers', type=int, default=2)
    parser.add_argument('--lr', type=float, default=0.0002)
    parser.add_argument('--beta1', type=float, default=0.5)  # momentum1 in Adam
    parser.add_argument('--beta2', type=float, default=0.999)  # momentum2 in Adam
    parser.add_argument('--augmentation_prob', type=float, default=0.4)

    parser.add_argument('--log_step', type=int, default=2)
    parser.add_argument('--val_step', type=int, default=2)

    # misc
    data_root = '/mnt/HDD/datasets/competitions/aug/'
    save_root = '/mnt/HDD/datasets/competitions/vnet/'
    parser.add_argument('--model_type', type=str, default='VNet', help='VNet/')
    parser.add_argument('--model_path', type=str, default=save_root + 'models_for_cls')
    parser.add_argument('--train_path', type=str, default=data_root)
    parser.add_argument('--valid_path', type=str, default=data_root)
    parser.add_argument('--test_path', type=str, default=data_root)
    parser.add_argument('--result_path', type=str, default=save_root + 'result_for_cls/')
    #  VNet-400-0.0001000-200-0.5000-vv-fold1-ce+dice.pkl
    #  VNet-100-0.0001000-50-0.5000-vv-fold1-1-ce+dice-then-gdice+ce-1.pkl
    # /mnt/HDD/datasets/competitions/candidate/vnet/VNet-400-0.0001000-100-0.5000-vv-fold2-1-ce+dice.pkl
    parser.add_argument('--net_path', type=str, default='/mnt/HDD/datasets/competitions/vnet/models_for_cls/VNet-60-0.0001000-25-0.5000-vv-fold5-d19-ce+dice-then-gdice+ce.pkl')

    parser.add_argument('--cuda_idx', type =int, default=1)

    config = parser.parse_args()

    # TEST_ON = 'right'
    # IS_TRAIN = True
    FOLD = 5

    for TEST_ON in ['raw', 'left', 'right']:
        for IS_TRAIN in [False, True]:
            predict(config,TEST_ON, IS_TRAIN, FOLD)


def open_file(x, a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0) :
    f0 = open(x, mode = 'r').readlines()
    for i in f0 :
        if "PISTOL" in i :
            a1 += 1
        elif "SMG" in i :
            a2 += 1
        elif "SHOTGUN" in i :
            a3 += 1
        elif "AR" in i :
            a4 += 1
        elif "SNIPER" in i :
            a5 += 1
        elif "EOF" == i :
            break
    print(a1, a2, a3, a4, a5)

open_file(str(input()))


import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
from functions import *
import time
def connect_user(userName):
    db.reference().child("usersConnected").child(userName.capitalize()).update({
        'Connected': True
    })
    print(f"Welcome {userName.capitalize()}, you are now connected to the server!\n")

def disconnect_user(userName):
    db.reference().child("usersConnected").child(userName.capitalize()).update({
        'Connected': False
    })
    print(f"\nDisconnection done with success! Good Bye {userName.capitalize()}!")

    #We need to inform bob that the public key in the server is a former one. The new one is being created by Alice
    if userName.capitalize() == "Alice":
        db.reference().child("PublicKey").update({
            "Status": "Old"
        })
    elif userName.capitalize() == "Bob":
        db.reference().child("EncryptedMessage").update({
            'Status': 'Old'
        })
def connexion_server():
    global connexionSuccessfull
    try:
        credit = firebase_admin.credentials.Certificate("elgamalproject-firebase-adminsdk-9mr1b-00c87e9b0d.json")
        default_app = firebase_admin.initialize_app(credit,{
            'databaseURL': "https://elgamalproject.firebaseio.com/"
        })
        print("\n\nConnection with the server estanlished!\n")

    except:
        print("An error occured unable to access the server...")
        exit(-1)

def retrieve_connected_users():
    """
    This function permits to retrieve all the people connected to our server
    :return: a list of all people connected
    """
    data = db.reference().child("usersConnected").get()
    usersConnected = [personne for personne in data if data[personne]['Connected'] == True]
    return usersConnected

def receiving_encrypted_message(privateValue,prime):
    print("\n-----------------------------")
    print(" RECEIVING ENCRYPTED MESSAGE  ")
    print("-----------------------------\n")

    print("Waiting for Bob messages...")

    encryptedMessage = ""
    decryptedMessage = ""
    fullDecryptedMessage = ""
    while True:
        if db.reference().child("EncryptedMessage").get()["Status"] == "InProgress":
            break

    #We start to receive information
    print("We start receiving Bob's message. Deciphering in progress")

    while True:
        if db.reference().child("EncryptedMessage").get()["Status"] == "Finished":
            break

        encryptedMessage = db.reference().child("EncryptedMessage").get()["Message"]
        confirmation_reception_message(False)

        decryptedMessage = decipher_encrypted_message(encryptedMessage,
                                                      db.reference().child("EncryptedMessage").get()["Gamma"],
                                                    privateValue,prime)

        fullDecryptedMessage += f" {decryptedMessage}"
        confirmation_reception_message(True)

    #We redesign the encrypted message
    fullDecryptedMessage += "."
    return fullDecryptedMessage[1:]

def sending_public_key(prime,generator,privateKey):
    index = 0
    while True:
        index += 1
        try:
            db.reference().child("PublicKey").update({
                'Generator': generator,
                'Prime': prime,
                'LastPart': (generator**privateKey) % prime,
                'Status': 'New'
            })
            print("Public key sent to the server.")
            break
        except:
            if index < 5:
                print("A problem occured. We are trying to send the public key again.")
            else:
                print("Too many attempts have been effectuated. We quit the program")
                exit(1)

def receiving_public_key():
    time.sleep(1)
    print("\n------------------------")
    print(" RECEIVING PUBLIC KEY   ")
    print("------------------------\n")

    #Waiting for the public key to be sent to the server
    publicKey = db.reference().child("PublicKey").get()
    if publicKey["Status"] != "New":
        print("Alice didn't send the public key to the server for the moment. Be patient...")
        while True:
            publicKey = db.reference().child("PublicKey").get()
            if publicKey['Status'] == "New":
                break
    print("Alice sent the public key to the server.")
    time.sleep(1)
    print(f"Generator: {publicKey['Generator']}")
    time.sleep(1)
    print(f"Prime: {publicKey['Prime']}")
    time.sleep(1)
    print(f"LastPart: {publicKey['LastPart']}")
    time.sleep(1)
    return publicKey

def confirmation_reception_message(etat):
    db.reference().child("EncryptedMessage").update({
        'Received': etat
    })

def sending_encrypted_message(message,publicKey):

    messageSplitted = message.split(' ')

    k = choice([x for x in range(1,publicKey["Prime"]-1)])
    gamma = (publicKey["Generator"]**k) % publicKey["Prime"]

    time.sleep(1)
    print("\nWe are now sending this message to Alice!")

    db.reference().child("EncryptedMessage").update({'Status':'InProgress'})
    db.reference().child("EncryptedMessage").update({'Gamma': gamma })

    for word in messageSplitted:
        db.reference().child("EncryptedMessage").update({'Message': transform_message(word,publicKey,k)})

        while(db.reference().child("EncryptedMessage").get()["Received"] != True):
            pass

    db.reference().child("EncryptedMessage").update({'Status':'Finished'})
    print("All the message has been sent.")


# Copyright (c) 2019 hippo91 <guillaume.peillex@gmail.com>

# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER

"""Astroid hooks for numpy.core.umath module."""

import astroid

def numpy_core_umath_transform():
    ufunc_optional_keyword_arguments = (
        """out=None, where=True, casting='same_kind', order='K', """
        """dtype=None, subok=True"""
    )
    return astroid.parse(
        """
    class FakeUfunc:
        def __init__(self):
            self.__doc__ = str()
            self.__name__ = str()
            self.nin = 0
            self.nout = 0
            self.nargs = 0
            self.ntypes = 0
            self.types = None
            self.identity = None
            self.signature = None

        @classmethod
        def reduce(cls, a, axis=None, dtype=None, out=None):
            return numpy.ndarray([0, 0])

        @classmethod
        def accumulate(cls, array, axis=None, dtype=None, out=None):
            return numpy.ndarray([0, 0])

        @classmethod
        def reduceat(cls, a, indices, axis=None, dtype=None, out=None):
            return numpy.ndarray([0, 0])

        @classmethod
        def outer(cls, A, B, **kwargs):
            return numpy.ndarray([0, 0])

        @classmethod
        def at(cls, a, indices, b=None):
            return numpy.ndarray([0, 0])

    class FakeUfuncOneArg(FakeUfunc):
        def __call__(self, x, {opt_args:s}):
            return numpy.ndarray([0, 0])

    class FakeUfuncOneArgBis(FakeUfunc):
        def __call__(self, x, {opt_args:s}):
            return numpy.ndarray([0, 0]), numpy.ndarray([0, 0])

    class FakeUfuncTwoArgs(FakeUfunc):
        def __call__(self, x1, x2, {opt_args:s}):
            return numpy.ndarray([0, 0])

    # Constants
    e = 2.718281828459045
    euler_gamma = 0.5772156649015329

    # One arg functions with optional kwargs
    arccos = FakeUfuncOneArg()
    arccosh = FakeUfuncOneArg()
    arcsin = FakeUfuncOneArg()
    arcsinh = FakeUfuncOneArg()
    arctan = FakeUfuncOneArg()
    arctanh = FakeUfuncOneArg()
    cbrt = FakeUfuncOneArg()
    conj = FakeUfuncOneArg()
    conjugate = FakeUfuncOneArg()
    cosh = FakeUfuncOneArg()
    deg2rad = FakeUfuncOneArg()
    exp2 = FakeUfuncOneArg()
    expm1 = FakeUfuncOneArg()
    fabs = FakeUfuncOneArg()
    frexp = FakeUfuncOneArgBis()
    isfinite = FakeUfuncOneArg()
    isinf = FakeUfuncOneArg()
    log = FakeUfuncOneArg()
    log1p = FakeUfuncOneArg()
    log2 = FakeUfuncOneArg()
    logical_not = FakeUfuncOneArg()
    modf = FakeUfuncOneArgBis()
    negative = FakeUfuncOneArg()
    positive = FakeUfuncOneArg()
    rad2deg = FakeUfuncOneArg()
    reciprocal = FakeUfuncOneArg()
    rint = FakeUfuncOneArg()
    sign = FakeUfuncOneArg()
    signbit = FakeUfuncOneArg()
    sinh = FakeUfuncOneArg()
    spacing = FakeUfuncOneArg()
    square = FakeUfuncOneArg()
    tan = FakeUfuncOneArg()
    tanh = FakeUfuncOneArg()
    trunc = FakeUfuncOneArg()

    # Two args functions with optional kwargs
    bitwise_and = FakeUfuncTwoArgs()
    bitwise_or = FakeUfuncTwoArgs()
    bitwise_xor = FakeUfuncTwoArgs()
    copysign = FakeUfuncTwoArgs()
    divide = FakeUfuncTwoArgs()
    divmod = FakeUfuncTwoArgs()
    equal = FakeUfuncTwoArgs()
    float_power = FakeUfuncTwoArgs()
    floor_divide = FakeUfuncTwoArgs()
    fmax = FakeUfuncTwoArgs()
    fmin = FakeUfuncTwoArgs()
    fmod = FakeUfuncTwoArgs()
    greater = FakeUfuncTwoArgs()
    gcd = FakeUfuncTwoArgs()
    hypot = FakeUfuncTwoArgs()
    heaviside = FakeUfuncTwoArgs()
    lcm = FakeUfuncTwoArgs()
    ldexp = FakeUfuncTwoArgs()
    left_shift = FakeUfuncTwoArgs()
    less = FakeUfuncTwoArgs()
    logaddexp = FakeUfuncTwoArgs()
    logaddexp2 = FakeUfuncTwoArgs()
    logical_and = FakeUfuncTwoArgs()
    logical_or = FakeUfuncTwoArgs()
    logical_xor = FakeUfuncTwoArgs()
    maximum = FakeUfuncTwoArgs()
    minimum = FakeUfuncTwoArgs()
    nextafter = FakeUfuncTwoArgs()
    not_equal = FakeUfuncTwoArgs()
    power = FakeUfuncTwoArgs()
    remainder = FakeUfuncTwoArgs()
    right_shift = FakeUfuncTwoArgs()
    subtract = FakeUfuncTwoArgs()
    true_divide = FakeUfuncTwoArgs()
    """.format(
            opt_args=ufunc_optional_keyword_arguments
        )
    )

astroid.register_module_extender(
    astroid.MANAGER, "numpy.core.umath", numpy_core_umath_transform
)


__author__ = 'Jim'
import requests
from RiotAPI import RiotAPI
from GetCords import *
from ApiKeys import apikey

def Name2ID(summName):
    api = RiotAPI(apikey)
    r = api.get_summoner_by_name(summName)
    return (r[summName.lower().replace(' ', '')]['id'])

user = input("Please enter summoner name: ")
userid = Name2ID(user)
api = 'https://na.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/NA1/{}?api_key={}'.format(userid,apikey)
count1=0
count2=0

while True:
    status = requests.get(api)
    if status.status_code == 200:
        print("In game")
        clickSpectate()
        while True:
            status2 = requests.get(api)
            if status2.status_code == 404:
                print("Game Finished, waiting 3 Mins")
                time.sleep(190)
                clickContinue()
                openFriends()
                print("No longer in game")

                break
            else:
                timer2 = range(10,0,-1)
                count1 += 1
                for timee in timer2:
                    print("Currently in Game. Checked {} time(s), will check again in {} seconds.".format(count1,timee), end = '\r')
                    time.sleep(1)
                    continue
    else:
        timer = range(10,0,-1)
        count2 += 1
        for timee in timer:
            print("Currently Not Game. Checked {} time(s), will check again in {} seconds.".format(count2,timee), end = '\r')
            time.sleep(1)
            continue
        continue


# -*- coding: utf-8 -*-
import scrapy
import json
import time
from datetime import datetime
from bs4 import BeautifulSoup
from ..items import EtfingredientItem
from etfingredient import settings
import pymongo
from pymongo import MongoClient
class EtfingredientSpider(scrapy.Spider):
    name = 'etfingredient'
    def __init__(self):    #連線資料庫，資料庫相關設定值放在settings.py
        self.client = MongoClient(settings.MONGO_STRING)
        self.db = self.client['TickerPool']
        self.tickerpool = self.db['twse']
    def start_requests(self):
        tickers = self.tickerpool.find({"ticker_type":"ETF"})
        for ticker in tickers :
            ticker = ticker['ticker']
            url = 'https://www.cnyes.com/twstock/Etfingredient/'+ticker+'.html'
            yield scrapy.Request(url = url, meta={'ticker':ticker},callback = self.parse)
    def parse(self, response):
        data = response.body
        soup = BeautifulSoup(data,'lxml')
        tables = soup.findAll('table')
        item = EtfingredientItem()
        item['ticker'] = response.meta['ticker']
        item['last_crawl_time'] = datetime.now().strftime("%Y-%m-%d")
        ingredient = []
        for table in tables:
            for tr in table.findAll('tr'):
                if(tr.find('a')):
                    name = tr.find('a').text.strip()
                    ratio = tr.select_one('td:nth-of-type(2)').text.strip()
                    ticker = tr.find('a').get("href").split("/")[3].replace('.htm','')
                    ingredient.append({"name":name,"ratio":ratio,"ticker":ticker})
        item['ingredient'] = ingredient
        yield item


import os
import sys
import random

import os.path
from os import path

from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *

from dotsShared      import common, paths

import dotsSideCar as sideCar

bkgZ = -99.0

### ---------------------- dotsBkgItem ---------------------
''' dotsBkgItem: handles adding, setting, copying and saving background
    items.  Includes BkgItem and InitBkg classes. '''
### --------------------------------------------------------
class BkgItem(QGraphicsPixmapItem):

    def __init__(self, imgFile, parent, mirror=False):
        super().__init__()

        self.canvas  = parent
        self.sideCar = self.canvas.sideCar
        self.fileName = imgFile

        img = QImage(imgFile)

        self.viewW = common["viewW"]
        self.viewH = common["viewH"]

        self.imgFile = img.scaled(
            self.viewW,
            self.viewH,
            Qt.KeepAspectRatio|
            Qt.SmoothTransformation)

        self.key = ""

        self.type = 'bkg'
        self.flopped = mirror

        self.rotation = 0
        self.scale = 1.0

        self.setZValue(0)
        self.tag = ''
        self.isBackgroundSet = False

        self.width = self.imgFile.width()
        self.height = self.imgFile.height()

        self.x = (self.viewW - self.width)/2
        self.y = (self.viewH - self.height)/2

        self.setPos(self.x, self.y)
        self.setMirrored(mirror)

        self.setPixmap(QPixmap.fromImage(self.imgFile))
        self.setFlag(QGraphicsPixmapItem.ItemIsMovable, True)

### --------------------------------------------------------
    def mousePressEvent(self, e):
        if e.button() == Qt.RightButton:
            self.ungrabMouse()
        if self.canvas.key == 'del':       # delete it
            self.canvas.scene.removeItem(self)
            self.canvas.disableSliders()
            self.canvas.buttons.btnBkgFiles.setEnabled(True)
        elif self.canvas.key == 'bak':    # send to back
            self.setZValue(self.sideCar.lastZval('bkg')-1)
        elif self.canvas.key == 'shift':
            if self.flopped == False:
                self.flopped = True
            else:
                self.flopped = False
            self.setMirrored(self.flopped)

    def setMirrored(self, mirror):
        sideCar.mirrorSet(self, mirror)

    def centerBkg(self):
        self.updateWidthHeight()
        op = QPointF(self.width/2, self.height/2)
        self.setTransformOriginPoint(op)

    def setBkgRotate(self, value):
        self.centerBkg()
        if value < 0:
            value = 0
        self.rotation = value
        self.setRotation(self.rotation)

    def setBkgScale(self, value):
        self.centerBkg()
        self.scale = value
        self.setScale(self.scale)

    def setBkgOpacity(self, value):
        self.centerBkg()
        self.opacity = value
        self.setOpacity(self.opacity)

    def updateWidthHeight(self):
        sideCar.widthHeightSet(self)

### --------------------------------------------------------
class InitBkg(QWidget):

    def __init__(self, parent):
        super().__init__()

        self.canvas  = parent
        self.sliders = self.canvas.sliders
        self.buttons = self.canvas.buttons
        self.initMap = self.canvas.initMap

        self.sliders.signal[str, int].connect(self.mapKeys)

### --------------------------------------------------------
    @pyqtSlot(str, int)
    def mapKeys(self, key, val):
        self.key = key
        if key == 'rotate':
            self.bkg.setBkgRotate(val)
        elif key == 'scale':
            self.bkg.setBkgScale(val/100.0)
        elif key == 'opacity':
            self.bkg.setBkgOpacity(val/100.0)
        elif key == 'lock':
            self.bkg.setFlag(QGraphicsPixmapItem.ItemIsMovable, False)
        self.bkg.update()

    def bkgfiles(self):
        Q = QFileDialog()
        file, _ = Q.getOpenFileName(self,
            "Choose an image file to open", paths["bkgPath"],
            "Images Files(*.bmp *.jpg *.png)")
        if file:
            self.addBkg(file)

    def addBkg(self, file, flopped=False): ## also used by saveBkg
        if self.initMap.mapSet:
            self.initMap.removeMap()
        self.bkg = BkgItem(file, self.canvas)
        self.canvas.scene.addItem(self.bkg)
        if file.endswith('-bkg.jpg'):
            self.bkg.setScale(1.003)   ## for white space???
            self.bkg.centerBkg()
            self.lockBkg()
        else:
            self.buttons.btnBkgFiles.setEnabled(False)
            self.enableSave(True)
            sideCar.setCursor()

    def saveBkg(self):
        if not self.bkg.isBackgroundSet:
            self.canvas.MsgBox("Set to Background inorder to save", 3)
        else:
            file = os.path.basename(self.bkg.fileName)
            file = file[0: file.index('.')]
            file = file[:15] + "-bkg.jpg"
            ## if it's not already a bkg file and the new file doesn't exist
            if not self.bkg.fileName.lower().endswith("-bkg.jpg") and not \
                path.exists(paths["bkgPath"]+ file):
                self.bkg.fileName = paths["bkgPath"] + file
                flopped = self.bkg.flopped
                pix = self.canvas.view.grab(QRect(QPoint(1,1), QSize()))
                pix.save(paths["bkgPath"] + file,
                    format='jpg',
                    quality=100)
                self.canvas.MsgBox("Saved as " + file, 3)
                self.canvas.clear() ## replace current background with "-bkg.jpg" copy
                self.addBkg(self.bkg.fileName, flopped)
            else:
                self.canvas.MsgBox("Already saved as background jpg")
            self.buttons.btnBkgFiles.setEnabled(True)
            self.enableSave(False)

    def setBkg(self):  ## from set background button
        if self.bkg.isBackgroundSet == False and self.bkg.key != 'lock':
            self.lockBkg()
        else:
            self.enableSave(False)
            self.buttons.btnBkgFiles.setEnabled(True)
            self.canvas.MsgBox("Already set to background")

    def lockBkg(self):
        self.sliders.enableSliders(False)
        self.mapKeys("lock", 0)   ## 0 is a place holder
        if self.hasBackGround():  ## reset zvals of existing bkgs to by -1
            ## seems to work best in descending order
            for itm in self.canvas.scene.items():
                if itm.type == 'bkg' and itm.zValue() <= bkgZ:
                    itm.setZValue(self.canvas.sideCar.lastZval('bkg')-1)
        self.bkg.setZValue(bkgZ)   ## the one showing
        self.bkg.isBackgroundSet = True

        self.buttons.btnBkgFiles.setEnabled(True)
        self.buttons.btnSetBkg.setEnabled(False)
        self.buttons.btnSave.setEnabled(True)

        txt = os.path.basename(self.bkg.fileName)
        self.canvas.MsgBox(txt + " " +  "set to background")

    def snapShot(self):
        if self.hasBackGround() or self.canvas.scene.items():
            self.canvas.unSelect()  ## turn off any select borders
            if self.initMap.mapSet:
                self.initMap.removeMap()
            if self.canvas.sideCar.openPlayFile == '':
                snap = "dots_" + sideCar.snapTag() + ".jpg"
            else:
                snap = os.path.basename(self.canvas.sideCar.openPlayFile)
                snap = snap[:-5] + ".jpg"
            if snap[:4] != "dots":  ## always ask unless
                Q = QFileDialog()
                f = Q.getSaveFileName(self, paths["snapShot"],
                    paths["snapShot"] + snap)
                if not f[0]:
                    return
                elif not f[0].lower().endswith('.jpg'):
                    self.canvas.MsgBox("Wrong file extention - use '.jpg'")
                    return
                snap = os.path.basename(f[0])
            pix = self.canvas.view.grab(QRect(QPoint(0,0), QSize()))
            pix.save(paths["snapShot"] + snap,
                format='jpg',
                quality=100)
            self.canvas.MsgBox("Saved as " + snap, 3)

    def enableSave(self, bool):
        self.sliders.enableSliders(bool)
        self.buttons.btnSetBkg.setEnabled(bool)
        self.buttons.btnSave.setEnabled(bool)

    def hasBackGround(self):
        for pix in self.canvas.scene.items(Qt.AscendingOrder):
            if pix.type == 'bkg':
                return True
            elif pix.zValue() > bkgZ:
                return False

### --------------------- dotsBkgItem ----------------------


prime_num = []
m, n = map(int, input().strip().split(' '))
check = [False] * (n + 1)

for i in range(2, n+1):
    if (check[i] == False):
        if i >= m:
            prime_num.append(i)
        j = i * i
        while j < n+1:
            check[j] = True
            j += i

print(*prime_num, sep='\n')


import logging
import numpy as np
import rasterio
import os

from copy import deepcopy
from osgeo import gdal, gdalconst
from rasterio.transform import Affine

from lib_utils_io import create_darray_2d

logging.getLogger('rasterio').setLevel(logging.WARNING)

# -------------------------------------------------------------------------------------
# Method to translate file in tiff format
def translate_file_type(filename_src, filename_dest, fileinfo={}, src_type='.h5', dest_type='GTiff'):

    if (src_type == '.h5') and (filename_src.endswith(src_type)):

        file_suffix = fileinfo['prefix']
        file_tree = fileinfo['tree']

        fileroot_src = file_suffix + filename_src + file_tree

        gdal.Translate(filename_dest, fileroot_src,
                       outputSRS="+proj=cea +lon_0=0 +lat_ts=30 +ellps=WGS84 +units=m",
                       format=dest_type, outputBounds=[-17367530.45, 7314540.76, 17367530.45, -7314540.76],
                       noData=-9999)

    else:
        logging.error(' ===> Translate options are not available!')
        raise NotImplementedError('Translate options not implemented yet')
# -------------------------------------------------------------------------------------

# -------------------------------------------------------------------------------------
# Method to read tiff file
def reproject_file_tiff(file_name_in, file_name_out,
                        file_wide_out, file_high_out, file_geotrans_out, file_proj_out):

    if isinstance(file_geotrans_out, Affine):
        file_geotrans_out = file_geotrans_out.to_gdal()

    dset_tiff_out = gdal.GetDriverByName('GTiff').Create(
        file_name_out, file_wide_out, file_high_out, 1, gdalconst.GDT_Float32)
    dset_tiff_out.SetGeoTransform(file_geotrans_out)
    dset_tiff_out.SetProjection(file_proj_out)

    dset_tiff_in = gdal.Open(file_name_in, gdalconst.GA_ReadOnly)
    dset_proj_in = dset_tiff_in.GetProjection()
    dset_geotrans_in = dset_tiff_in.GetGeoTransform()
    dset_data_in = dset_tiff_in.ReadAsArray()
    dset_band_in = dset_tiff_in.GetRasterBand(1)

    # Reproject from input file to output file set with out information
    gdal.ReprojectImage(dset_tiff_in, dset_tiff_out, dset_proj_in, file_proj_out,
                        gdalconst.GRA_NearestNeighbour)
    return dset_tiff_out
# -------------------------------------------------------------------------------------

# -------------------------------------------------------------------------------------
# Method to read geographical file
def read_file_geo(file_name):

    if os.path.exists(file_name):
        if file_name.endswith('tif') or file_name.endswith('.tiff'):

            dset = rasterio.open(file_name)
            bounds = dset.bounds
            res = dset.res
            transform = dset.transform
            data = dset.read()
            values = data[0, :, :]
            proj = dset.crs.wkt
            geotrans = dset.transform

            decimal_round = 7

            center_right = bounds.right - (res[0] / 2)
            center_left = bounds.left + (res[0] / 2)
            center_top = bounds.top - (res[1] / 2)
            center_bottom = bounds.bottom + (res[1] / 2)

            lon = np.arange(center_left, center_right + np.abs(res[0] / 2), np.abs(res[0]), float)
            lat = np.arange(center_bottom, center_top + np.abs(res[0] / 2), np.abs(res[1]), float)
            lons, lats = np.meshgrid(lon, lat)

            min_lon_round = round(np.min(lons), decimal_round)
            max_lon_round = round(np.max(lons), decimal_round)
            min_lat_round = round(np.min(lats), decimal_round)
            max_lat_round = round(np.max(lats), decimal_round)

            center_right_round = round(center_right, decimal_round)
            center_left_round = round(center_left, decimal_round)
            center_bottom_round = round(center_bottom, decimal_round)
            center_top_round = round(center_top, decimal_round)

            assert min_lon_round == center_left_round
            assert max_lon_round == center_right_round
            assert min_lat_round == center_bottom_round
            assert max_lat_round == center_top_round

            lats = np.flipud(lats)

            mask_values = np.where(values != 255, 1, 0)
            mask_idx = np.where(mask_values.ravel() == 0)

            dims = values.shape
            values_tmp = np.float32(deepcopy(values.ravel()))
            values_tmp[mask_idx] = np.nan
            values_masked = np.reshape(values_tmp, [dims[0], dims[1]])

            high = dims[0]
            wide = dims[1]

            # handle_file = gdal.Open(file_name)
            # da_proj = handle_file.GetProjection()
            # da_geotrans = handle_file.GetGeoTransform()
            # da_wide = handle_file.RasterXSize   # 2 dims
            # da_high = handle_file.RasterYSize   # 1 dims

            da_frame = create_darray_2d(values_masked, lons, lats, coord_name_x='west_east', coord_name_y='south_north',
                                        dim_name_x='west_east', dim_name_y='south_north', name='geo_values')

            da_mask = create_darray_2d(mask_values, lons, lats, coord_name_x='west_east', coord_name_y='south_north',
                                       dim_name_x='west_east', dim_name_y='south_north', name='geo_mask')

        else:

            logging.error(' ===> Geographical file ' + file_name + ' format unknown')
            raise NotImplementedError('File type reader not implemented yet')
    else:
        logging.error(' ===> Geographical file ' + file_name + ' not found')
        raise IOError('Geographical file location or name is wrong')

    return da_frame, da_mask, wide, high, proj, geotrans, mask_idx
# -------------------------------------------------------------------------------------


""" define the linechart dataset tag  """
from __future__ import absolute_import, unicode_literals
import json

from django import template
from saef.models import DatasetProfileHistory

register = template.Library()

@register.inclusion_tag('dataset/linechart_dataset.html')
def linechart_dataset(ds_id = -1, amount = 5, profile_name="row count"):
    if(ds_id == -1):
        return None

    data= []
    labels = []
    profile_history = DatasetProfileHistory.objects.filter(dataset_id = ds_id).order_by('-create_timestamp')[:amount]
    for row in profile_history:
        batch_time = row.create_timestamp.strftime('%Y-%m-%d %H:%M:%S')
        profile = json.loads(row.profile_json)
        profile_value = profile[profile_name]
        labels.append(str(batch_time))
        data.append(profile_value)
    labels.reverse()
    data.reverse()
    return {'data': data, 'labels': labels}


import sys
import os
sys.path.append(os.environ['EFS_PIP_PATH'])  # nopep8 # noqa
import json
import torch
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, AutoConfig

def encode(tokenizer, question, context):
    encoded = tokenizer.encode_plus(question, context)
    return encoded["input_ids"], encoded["attention_mask"]

def decode(tokenizer, token):
    answer_tokens = tokenizer.convert_ids_to_tokens(
        token, skip_special_tokens=True)
    return tokenizer.convert_tokens_to_string(answer_tokens)

def serverless_pipeline(model_path='./model/files'):
  tokenizer = AutoTokenizer.from_pretrained(model_path)
  model = AutoModelForQuestionAnswering.from_pretrained(model_path)
  def predict(question, context):
          input_ids, attention_mask = encode(tokenizer,question, context)
          start_scores, end_scores = model(torch.tensor(
              [input_ids]), attention_mask=torch.tensor([attention_mask]))
          ans_tokens = input_ids[torch.argmax(
              start_scores): torch.argmax(end_scores)+1]
          answer = decode(tokenizer,ans_tokens)
          return answer
  return predict

question_answering_pipeline = serverless_pipeline()

def handler(event, context):
    try:
        print(event['body'])
        # extract body

        body = json.loads(event['body'])
        # init pipeline
        # predict the answer
        answer = question_answering_pipeline(question=body['question'], context=body['context'])
        print(answer)
        #return
        return {
            "statusCode": 200,
            "headers": {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*',
                "Access-Control-Allow-Credentials": True

            },
            "body": json.dumps({'answer': answer})
        }
    except Exception as e:
        print(repr(e))
        return {
            "statusCode": 500,
            "headers": {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*',
                "Access-Control-Allow-Credentials": True
            },
            "body": json.dumps({"error": repr(e)})
        }


import tensorflow as tf
import tensorflow.contrib.slim as slim

def AEnet(image):
    with slim.arg_scope([slim.conv2d, slim.conv2d_transpose],
                        activation_fn=tf.nn.relu,
                        weights_initializer=tf.truncated_normal_initializer(0.0, 0.01),
                        weights_regularizer=slim.l2_regularizer(0.0)):
        with slim.arg_scope([slim.conv2d, slim.conv2d_transpose],
                            padding="SAME"):
            image = image / 127.5 - 1.0

            net = slim.conv2d(image, 64, [7, 7], stride=1, scope="conv1")  # 128 * 128 * 64

            net = slim.conv2d(net, 128, [3, 3], stride=2, scope="conv2")  # 64 * 64 * 128
            net = slim.conv2d(net, 256, [3, 3], stride=2, scope="conv3")  # 32 * 32 * 256

            net = slim.conv2d(net, 512, [3, 3], stride=2, scope="conv4")  # 16 * 16 * 512
            net = slim.conv2d(net, 1024, [16, 16], stride=1, scope="full1")

            net = slim.conv2d_transpose(net, 512, [16, 16], stride=1, scope="full2")

            #net = tf.reshape(net, [-1, 16 * 16 * 512])
            #net = slim.fully_connected(net, 2048, scope="full1")  # 2048
            #net = slim.fully_connected(net, 16 * 16 * 512, scope="full2")  # value(16 * 16 * 512)
            #net = tf.reshape(net, [-1, 16, 16, 512], name="reshape")  # 16 * 16 * 512
            net = slim.conv2d_transpose(net, 256, [3, 3], stride=2, scope="deconv1")  # 32 * 32 * 256
            net = slim.conv2d_transpose(net, 128, [3, 3], stride=2, scope="deconv2")  # 64 * 64 * 128

            net = slim.conv2d_transpose(net, 64, [3, 3], stride=2, scope="deconv3")  # 128 * 128 * 64

            net = slim.conv2d_transpose(net, 3, [7, 7], stride=1, scope="deconv4")
            tmp_tanh = net

            net = tf.tanh(net)

            net = net * 127.5 + 127.5

    return net, tmp_tanh


def checkio(str):
    if str:
        brackets = [ ('(',')'), ('[',']'), ('{','}')]
        # define fake constants - python does not support the concept of constants
        kStart = 0
        kEnd = 1

        # internal stack used to push and pop brakets in the input string
        stack = []

        for char in str:
            for bracketPair in brackets:
                if char == bracketPair[kStart]:
                    stack.append(char)

              #  if char == bracketPair[kEnd] and len(stack) == 0 and stack.pop() != bracketPair[kStart]:
                elif char == bracketPair[kEnd] and (len(stack) == 0 or stack.pop() != bracketPair[kStart]):
                    return False
        print (stack.pop() )
        print (bracketPair[kEnd])
        print (stack)
        if len(stack) == 0:
            return True
    return False
print (checkio("(((1+(1+1))))]"))

'''
    stack = []
    stackr = []
    pushChars, popChars = "<({[", ">)}]"
    for c in str :
        if c in pushChars :
            stack.append(c)
        if c in popChars:
            stackr.append(c)

    print (stack)
    stackr = stackr.reverse()
    print (stackr)
    if stack == stackr.reverse():

        return True
    else :

        return False

if __name__ == '__main__':
    assert checkio("((5+3)*2+1)") == True, "Simple"
    assert checkio("{[(3+1)+2]+}") == True, "Different types"
    assert checkio("(3+{1-1)}") == False, ") is alone inside {}"
    assert checkio("[1+1]+(2*2)-{3/3}") == True, "Different operators"
    assert checkio("(({[(((1)-2)+3)-3]/3}-3)") == False, "One is redundant"
    assert checkio("2+3") == True, "No brackets, no problem"
'''


import json
import logging
import os
import re
import threading

logger = logging.getLogger(__name__)

class FileWorker(threading.Thread):

    def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=True):
        super().__init__(group=group, target=target, name=name, daemon=daemon)
        self.args = args
        self.kwargs = kwargs
        self.stop = False

        self.properties_per_file = self.kwargs.pop("properties_per_file")
        self.file_format = self.kwargs.get("file_format", "feeds_1.json")

    def gen_file(self, feeds):
        f_name = self.file_format
        while os.path.exists(f_name):
            num = re.match(r".+_([\d]+).+", f_name).group(1)
            f_name = f_name.replace(num, str(int(num) + 1))

        logger.info(f"Creating new file: {f_name}.")
        with open(f_name, "a", encoding='utf8') as f:
            json.dump(feeds, f, ensure_ascii=False)

    def run(self):
        logger.info("FileWorker is waiting for enough jobs to dump to file..")
        feed = self.args[0]
        while not self.stop:
            while len(feed) > self.properties_per_file:
                if isinstance(feed, dict):
                    keys = list(feed.keys())[:self.properties_per_file]
                    dump = {k: feed.pop(k) for k in keys}
                elif isinstance(feed, list):
                    keys = feed[:self.properties_per_file]
                    dump = [feed.pop(0) for k in keys]
                # when gets to the number of page that the user wants, dump current feed to file
                self.gen_file(dump)
        if feed:
            # dump all feeds that left. properly less than "self.properties_per_file"
            self.gen_file(feed)


def how_send_for_how(npeople, friends):
    for person in range(1, npeople + 1):
        yield friends[person]

if __name__ == "__main__":
    npeople = int(input())
    people = list(map(int, input().rstrip().split()))
    friends = {people[i]:i + 1 for i in range(len(people))}

    senders = how_send_for_how(npeople, friends)
    print(' '.join(map(str, senders)))


# -*- coding: utf-8 -*-
"""
Created on Sat Dec 16 05:34:34 2017

@author: francisco
"""
import random
import numpy as np
from os import listdir
from os.path import isfile, join
from numpy import genfromtxt

class MicroarrayDatasets():
    def __init__(self,debug=False):
        self.dsname = 'Microarray'
        self.path = "datasets/bio/debug" if debug else "datasets/bio/"
        self.filesname = [f for f in listdir(self.path) if isfile(join(self.path, f))]
    def files(self):
        for filename in self.filesname:
            yield join(self.path, filename),filename
    def datasets(self):
        for file,filename in self.files():
            data = genfromtxt(file)
            X,y = data[:,:-1],data[:,-1]
            yield X,y,filename

from sklearn.metrics import f1_score
from sklearn.model_selection import RepeatedStratifiedKFold

class Experiment():
    def __init__(self,datasets=MicroarrayDatasets(),resdir="results",debug=False):
        self.ds = datasets
        self.resdir = resdir
        self.debug = debug
    def run(self, basename, clf, n_splits=10, n_repeats=10, random_state=None):
        for X,y,filename in self.ds.datasets():
            print(basename+' '+filename)
            resfilename = join(self.resdir, self.ds.dsname+'_'+filename+'.res_'+basename)
            file = open(resfilename, "a") if not self.debug else open(resfilename, "w")
            performances = []
            rskf = RepeatedStratifiedKFold(n_splits,n_repeats,random_state)
            for fold, idxs in enumerate(rskf.split(X, y)):
                if(fold >= file.tell()/10):
                    tr,te = idxs
                    clf.fit(X[tr],y[tr])
                    y_pred = clf.predict(X[te])
                    f1s = f1_score(y[te], y_pred, average='macro')
                    file.write('{:.7f}'.format(f1s)+'\n')
            file.close()

exp = Experiment(MicroarrayDatasets(True),debug=True)

from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
clfs ={'lr': LogisticRegression()}#,'rf': RandomForestClassifier()}

for cn, clf in clfs.items():
    exp.run(cn, clf, random_state=42, n_splits=3, n_repeats=1)


# encoding=utf-8

def parse_float_isl(value):
    try:
        value = value.replace(',', '.')
        return float(value)
    except:
        return float('0')

#
# VIS
#
def convert_ad2email(obj):
    if str(obj).upper().startswith('AD\\'):
        return str(obj)[3:] + '@vis.is'
    else:
        return obj

#
# String, unicode, etc
#
ISL_CHARS = u'áéíóúýþðæöÁÉÍÓÚÝÞÐÆÖ'.split('\n')
ISL_CHARS_REPLACE_SAFE = u'aeiouythdaeoAEIOUYThDAeO'.split('\n')

def isl_enska(obj):
    ret = u""
    for c in obj:
        if c in ISL_CHARS:
            c = ISL_CHARS_REPLACE_SAFE[ISL_CHARS.index(c)]
        ret = ret + c
    return ret

def _insert_substring_at_positions(superstring, substring, positions):
    insertion_count = 0
    for position in sorted(positions):
        position = position + insertion_count
        superstring = superstring[:position] + substring + superstring[position:]
    return superstring

LOCAL_PHONE_NUMBER_SEPARATION_POSITIONS = [3]
def get_localized_phone_number(phone_number, fail_silently=True, separator=' ',
    area_code='354', min_length=7, max_length=7, separate_at_positions=None):
    """
    Given a string containing 10 digits, return it as two groups of the
    last 7 digits (area code removed), separated by a space before the
    last 4 digits.

    If the number of digits after removing non-digit characters is not
    exactly 10, `ValueError` will be raised.  This is unless `fail_silently`
    is specified, the string with non-digits removed is returned unformatted,
    but no exceptions are raised.

    >>> get_localized_phone_number("+354 5 88 55 22")
    '588 5522'

    Because Iceland is the center of the universe, its values are the
    default to localize by, but `area_code`, `max_length` and `min_length`
    can be modified to accept anything on those formats.

    `separate_at_positions` is a list of 1-based positions in the list
    after which a `separator` should be written.
    """
    separate_at_positions = separate_at_positions or LOCAL_PHONE_NUMBER_SEPARATION_POSITIONS
    min_length = len(area_code) + min_length
    max_length = len(area_code) + max_length

    phone_number = filter(lambda char: char.isdigit(), phone_number)  # remove non-digits
    if min_length <= len(phone_number) <= max_length and phone_number.startswith(area_code):
        return _insert_substring_at_positions(phone_number, separator, separate_at_positions)
    else:
        if fail_silently:
            return phone_number
        else:
            raise ValueError("Can't localize unknown phone number '{phone_number}'.".format(
                phone_number=phone_number))


# 实现每间隔5秒，保存一张摄像机截图
import cv2
import numpy as np

cap = cv2.VideoCapture(0)
index = 0
while True:
	index += 1
	ret, frame = cap.read()
	cv2.imshow('Camera', frame)
	if index % 100 == 0:
		img_index = index // 100
		img_name = str(img_index) + '.jpg'
		cv2.imwrite(img_name, frame)
		print(img_name + ' saved.')
	# FPS设置为20
	if cv2.waitKey(50)&0xFF == ord('q'):
		break
cap.release()
cv2.destroyAllWindows()

01_打开图片和视频/03_保存图片.py