import bottle
from objects import *

import types, inspect, functools
from bottle import request, response

from site_engine.plugins.template import *

from site_engine import bem
import bem_blocks

class MyBaseTemplate(TemplatePlugin):

	bem_context = injected_dependency(bem.Context)
	base_layout = bem.block_dependency(bem_context, bem_blocks.BaseLayout)
	quote_display = bem.block_dependency(bem_context, bem_blocks.QuoteDisplay)

	class plugin_instance_mixin_cls(Object):
		title = precalculated_attr(lambda:TitleZone())
		content = precalculated_attr(lambda:TemplateZone())
		js_head = precalculated_attr(lambda:JSZone())
		css_head = precalculated_attr(lambda:CSSZone())

class IndexTemplate(TemplatePlugin):
	filename = 'index.html'

	bem_context = injected_dependency(bem.Context)
	index_page = bem.block_dependency(bem_context, bem_blocks.IndexPage)
	shortener_form = bem.block_dependency(bem_context, bem_blocks.ShortenerForm)

class AppBase(bottle.Bottle):
	def _init(self):
		super()._init()
		self._init_routes()

	def _init_routes(self):
		pass

from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.sql import func
from site_engine.plugins.sqlalchemy import ModelsMixin, DbPlugin

from random import choice, randrange, shuffle

class QuoteProvider(ModelsMixin, bottle.ComplexPlugin):

	def _models(self):

		class Quote:
			__tablename__ = 'quote'
			quote_id = Column(Integer, primary_key=True)
			quote = Column(String, nullable=False)
			quote_author = Column(String)

		return locals()

	@calculated_attr
	def plugin_instance_cls(self):

		class QuoteProviderInstance(Object):

			db_session = bottle.plugin_dependency(self.db_plugin)

			def add_quote(self_inst, quote, quote_author=None):
				quote = self.Quote(quote=quote, quote_author=quote_author)
				self_inst.db_session.add(quote)
				return quote

			def get_random(self_inst):
				db_session = self_inst.db_session
				Quote = self.Quote
				n = db_session.query(func.count(Quote.quote_id)).scalar()
				return db_session.query(Quote).offset(randrange(n)).limit(1).first()

			def get_random_sequence(self_inst):
				ids = [x[0] for x in self_inst.db_session.query(self.Quote.quote_id)]
				shuffle(ids)
				return ids

			def get(self_inst, _id):
				return self_inst.db_session.query(self.Quote).filter_by(quote_id=_id).first()

		return QuoteProviderInstance

class VisitLogger(ModelsMixin, bottle.ComplexPlugin):

	def _models(self):

		class Visit:
			__tablename__ = 'visit'
			visit_id = Column(Integer, primary_key=True)
			path = Column(String, nullable=False)
			query_string = Column(String)
			remote_addr = Column(String)
			user_agent = Column(String)
			referrer = Column(String)
			timestamp = Column(DateTime, default=func.now())
			extra = Column(Integer)
			sid = Column(String)

		return locals()

	@calculated_attr
	def plugin_instance_cls(self):

		class VisitLoggerInstance(Object):

			db_session = bottle.plugin_dependency(self.db_plugin)

			def log(self_inst, request=None, **kwargs):
				if request is None:
					request = bottle.request

				visit = self.Visit(\
						path=request.path, query_string=request.query_string,\
						remote_addr=request.remote_addr,\
						user_agent=request.get_header('User-Agent'),\
						referrer=request.get_header('Referer'))

				for k, v in kwargs.items():
					setattr(visit, k, v)

				self_inst.db_session.add(visit)

		return VisitLoggerInstance

class UrlShortener(ModelsMixin, bottle.ComplexPlugin):

	@precalculated_attr
	def alphabet():
		return ''.join(map(lambda x:chr(x) if type(x) is int else x,\
				list(range(ord('a'), ord('z')+1)) +\
				list(range(ord('A'), ord('Z')+1)) +\
				list(range(ord('0'), ord('9')+1)) +\
				list('')))

	min_id_length = 3
	max_id_length = 9

	def _models(self):

		class Url:
			__tablename__ = 'url'
			url_id = Column(String(self.max_id_length), primary_key=True)
			url = Column(String)
			visits = Column(Integer, default=0)

		return locals()

	def _random_char(self):
		return choice(self.alphabet)

	@calculated_attr
	def plugin_instance_cls(self):

		class UrlShortenerInstance(Object):

			db_session = bottle.plugin_dependency(self.db_plugin)

			def new_id(self_inst, url):
				db_session = self_inst.db_session
				Url = self.Url

				l = ''.join(map(lambda f:f(), [self._random_char] * self.min_id_length))
				while len(l) <= self.max_id_length:
					if db_session.query(Url).filter_by(url_id=l).count():
						continue

					db_session.add(Url(url=url, url_id=l))
					return l

				return None

			def get_url(self_inst, url_id):
				db_session = self_inst.db_session
				Url = self.Url

				return db_session.query(Url).filter_by(url_id=url_id).one_or_none()

		return UrlShortenerInstance

import re
from uuid import uuid4

class MainApp(AppBase):

	_significant_ = True

	@precalculated_attr
	def secret_seed():
		return uuid4().bytes

	def secret_hash(self, data):
		from hashlib import sha256 as hasher
		return hasher(self.secret_seed + data).digest()

	def sid(self):
		return uuid4().hex

	json_format = bottle.JSONPlugin()

	url_re = precalculated_attr(lambda:re.compile(r'^https?://.*'))

	url_shortener = predefined_dependency(Description(UrlShortener))
	quote_provider = predefined_dependency(Description(QuoteProvider))
	visit_logger = predefined_dependency(Description(VisitLogger))

	db_plugin = predefined_dependency(Description(DbPlugin, engine=create_engine('sqlite:////tmp/shortener.db', echo=True)))

	bem_context = predefined_dependency(bem.Context)

	base_template = associated_dependency(MyBaseTemplate, filename='base.html')
	index_template = associated_dependency(IndexTemplate, base_template=base_template)

	styles_template = TemplatePlugin(filename='styles.css')

	def _init_routes(self):

		@self.get('/')
		def index(*, tpl:self.index_template):
			return tpl.render(dict())

		@self.post('/new_url')
		@self.json_format
		def new_url(*, url_short:self.url_shortener):
			params = request.params
			url = params.get('url', '')

			if not self.url_re.match(url):
				return ('error', 'bad url')

			url_id = url_short.new_id(url)
			if url_id:
				return ('ok', dict(url_id=url_id))
			else:
				return ('retry', 'too long')

		@self.get('/<url_id:re:[%s]+>' % self.url_shortener.alphabet)
		def url_page(url_id, *, url_short:self.url_shortener, vl:self.visit_logger):
			url = url_short.get_url(url_id)
			if url is None:
				#~ response.status = 404
				raise(bottle.HTTPError(404, "Cannot resolve this URL."))

			url.visits += 1
			vl.log()

			response.set_cookie('go', {'url': url.url, 'url_id': url.url_id}, secret=self.secret_seed)
			return bottle.redirect(self.get_url('go', url=url.url))
			#~ return 'Redirecting to: {0}'.format(url.url)

		@self.get('/go', name='go')
		def go_to(*, vl:self.visit_logger):
			params = request.query
			url = params.get('url', '')
			if not self.url_re.match(url):
				raise(bottle.HTTPError(400, "Bad request."))

			url_verify = request.get_cookie('go', secret=self.secret_seed)
			response.delete_cookie('go')
			if url_verify and url_verify['url'] == url:
				vl.log(extra=1)
				#~ return 'Redirecting to: {0} (verified)'.format(url)
			else:
				vl.log(extra=2)
				#~ return 'Redirecting to: {0} (unverified)'.format(url)
			return bottle.redirect(url)

		@self.post('/!QuoteDisplay')
		@self.json_format
		def quote_display(*, qp:self.quote_provider):
			params = request.params
			qid = params.get('id')

			q = None
			seq = None
			if qid:
				q = qp.get(qid)
			else:
				seq = qp.get_random_sequence()
				print('seq', seq)
				if seq:
					q = qp.get(seq[0])

			if q:
				d = dict(quote=q.quote, quote_author=q.quote_author)
				if seq:
					d['sequence'] = seq
				return d
			else:
				return dict()

		@self.get('/css/styles.css')
		def styles_css(*, tpl:self.styles_template):
			bottle.response.content_type = 'text/css'
			#~ return tpl.render(dict(manager=self.style_manager))
			return bottle.template('styles.css', template_settings=dict(syntax='/*% %*/ @% %= =%'))

		@self.get('/js/bem_init.js')
		def bem_defs_js():
			bottle.response.content_type = 'text/javascript'
			bottle.response.set_header('Cache-Control', 'max-age=30')
			return bottle.template('bem_defs.tpl.js', template_settings=dict(syntax='/*% %*/ //% /*{ }*/'))

		import os.path
		@self.get('/<dir_part:re:img|css|js|font-awesome>/<path:path>')
		def static(dir_part, path):
			return bottle.static_file(os.path.join(dir_part, path), root='static')

r = registry(Description(MainApp))
application = r.lookup(MainApp)

#~ import pprint
#~ pprint.pprint(r._predefined)

if __name__ == '__main__':
	from bottle import run
	run(app=application, host='0.0.0.0', port=8080, debug=True, reloader=True)


import sys, re, pylab, math, matplotlib, json

#evPerAu = 27.211  # Energy.

def read_vector(fp):
    xs = []
    regexp_float = r"[+-]?(\d+\.)?\d+([deE][+-]?\d+)?"
    for line in fp:
        m = re.search(r"\d+\s+(?P<x>%s)" % regexp_float, line)
        if m:
            xs.append(float(m.group("x")))
    return xs

if __name__=="__main__":
    #eigenvalues_path = sys.argv[1]
    #ipratios_path = sys.argv[2]
    #title = sys.argv[3]

    json_path = sys.argv[1]
    title = sys.argv[2]

    with open(json_path) as fp:
        data = json.load(fp)
    eigenvalues = data['condition']['eigenvalues']
    ipratios = data['condition']['eigenstate_ipratio']

    #with open(eigenvalues_path, "r") as fp:
    #    eigenvalues = read_vector(fp)
    #with open(ipratios_path, "r") as fp:
    #    ipratios = read_vector(fp)
    #homo_index = 1266  #len(ipratios) / 2 - 1
    #log_pratios = map(lambda x: -math.log10(x), ipratios)
    pratios = map(lambda x: 1.0 / x, ipratios)
    marker_style = matplotlib.markers.MarkerStyle(marker=None, fillstyle=u'full')
    pylab.xlabel("Eigenvalue [a.u.]")
    #pylab.ylabel("Log10 (Participation Ratio)")
    pylab.ylabel("Participation Ratio")
    #pylab.scatter(eigenvalues, log_pratios, facecolors='none', edgecolors='black', linewidth=0.5, label="Eigenstate")
    pylab.scatter(eigenvalues, pratios, facecolors='none', edgecolors='black', linewidth=0.5, label="Eigenstate")
    #pylab.scatter(eigenvalues[homo_index], log_pratios[homo_index], facecolors='none', edgecolors='b', linewidth=1.5, label="HOMO")
    #i = 1266
    #pylab.scatter(eigenvalues[i], pratios[i], facecolors='none', edgecolors='b', linewidth=1.5, label=str(i+1))
    #i = 1255
    #pylab.scatter(eigenvalues[i], pratios[i], facecolors='none', edgecolors='r', linewidth=1.5, label=str(i+1))
    pylab.legend(loc="upper left")
    pylab.savefig(title + "_pr.png")


def won(board, t):
    # 가로줄
    for row in board:
        if row == [t, t, t]:
            return True

    # 세로줄
    for col in range(3):
        if [board[row][col] for row in range(3)] == [t, t, t]:
            return True

    # 대각선
    if [board[0][0], board[1][1], board[2][2]] == [t, t, t]:
        return True
    if [board[2][0], board[1][1], board[0][2]] == [t, t, t]:
        return True
    return False

def solution(board):
    board = [list(row) for row in board]

    o_count, x_count = 0, 0
    for row in board:
        for c in row:
            if c == 'O':
                o_count += 1
            if c == 'X':
                x_count += 1

    if not (o_count == x_count or o_count == x_count + 1):
        return 0

    if won(board, 'O') and won(board, 'X'):
        return 0

    if won(board, 'O') and o_count != x_count + 1:
        return 0

    if won(board, 'X') and o_count != x_count:
        return 0

    return 1

프로그래머스/unrated/160585. 혼자서 하는 틱택토/혼자서 하는 틱택토.py

혼자서 하는 틱택토.py


# Runtime: 88 ms, faster than 100.00% of Python online submissions for Sum of Nodes with Even-Valued Grandparent.
# Memory Usage: 19.4 MB, less than 100.00% of Python online submissions for Sum of Nodes with Even-Valued Grandparent.

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def sumEvenGrandparent(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """

        self.ans = 0

        def f(root, family):

            if family[0] and family[0] % 2 == 0:
                self.ans += root.val

            if root.left:
                f(root.left, (family[1], root.val))

            if root.right:
                f(root.right, (family[1], root.val))

        f(root, (None, None))

        return self.ans


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

'''
https://leetcode.com/problems/find-duplicate-file-in-system/description/

Input:
["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"]
Output:
[["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]
'''

class Solution:
    def findDuplicate(self, paths):
        '''
        :type paths: List[str]
        :rtype: List[List[str]]
        '''
        path_dict = {}
        for val in paths:
            # 'root/a 1.txt(abcd) 2.txt(efgh)' -> ['root/a', '1.txt(abcd)', '2.txt(efgh)']
            # 第一项为目录项，后面为文件名和内容
            tmp = val.split(' ')
            # 对每一项文件内容进行拆分
            for s in tmp[1:]:
                # '1.txt(abcd)' -> ['1.txt', 'abcd)']
                split_s = s.split('(')
                # ['1.txt', 'abcd)'] -> 'abcd'
                split_val = split_s[1].split(')')[0]
                # 通过字典保存，键为内容，值为目录+文件名
                if split_val not in path_dict:
                    path_dict[split_val] = [tmp[0] + '/' + split_s[0]]
                else:
                    path_dict[split_val].append(tmp[0] + '/' + split_s[0])

        duplicated_list = []
        # 遍历，如果存在重复内容，则存入list
        for lst in list(path_dict.values()):
            if len(lst) != 1:
                duplicated_list.append(lst)

        return duplicated_list


import pandas as pd
import os
import re
import math
import time
from algorithms.feature_extraction.feature_extraction import normalize_var, simulate_missing_data
from algoritmos_felipe.DamageDetection import *
from algorithms.data_imputation.DataImputation import MeanImputation, KNNImputation, InterpolationImputation
import scipy

def get_average_results(algorithm, missing_data_percentage, num_iterations, imputation_method=None):
    results = {
        'UCL': 0,
        'error_type_1': 0,
        'error_type_2': 0,
        'true_positives': 0,
        'true_negatives': 0,
    }

    invalid_iterations = 0

    for i in range(num_iterations):
        all_data = get_extracted_data(missing_data_percentage, i if missing_data_percentage != 0 else 0,
                                      imputation_method)

        learn_data = all_data[0:158, :]

        try:
            algorithm.train_and_test(learn_data, all_data, 197)

            results['UCL'] += algorithm.UCL
            results['error_type_1'] += algorithm.err[0]
            results['error_type_2'] += algorithm.err[1]
            results['true_positives'] += len([x for x in algorithm.class_states if x == 1])
            results['true_negatives'] += len([x for x in algorithm.class_states if x == 2])
        except Exception as e:
            invalid_iterations += 1
            print(e)

    print('Invalid iterations: {}'.format(invalid_iterations))
    return {key: value / (num_iterations - invalid_iterations) for key, value in results.items()}

def get_extracted_data(missing_data_percentage, iteration_number, imputation_method=None):
    if imputation_method:
        filename = '/home/alessandro/Documentos/Programming/Projects/TCC1/algorithms/features/{}/' \
                   'Features_Originais_Hora_12_Sensor_5_MDP_{}_{}.csv'.format(imputation_method,
                                                                              missing_data_percentage, iteration_number)
    else:
        filename = '/home/alessandro/Documentos/Programming/Projects/TCC1/algorithms/features/' \
                   'Features_Originais_Hora_12_Sensor_5_MDP_{}_{}.csv'.format(missing_data_percentage, iteration_number)

    data = np.genfromtxt(filename, delimiter=',')

    # Pegando apenas as frequências
    data = data[:, [0, 1]]
    return data

def get_results_imputation_knn(missing_data_percentage, imputation_method):
    canal = 1

    files_directory = '/home/alessandro/FELIPE/Z24_bridge/Z24Date/'

    # Pegar todos os arquivos registrados às 12h
    files = [f for f in os.listdir(files_directory) if re.match(r'.*_12\.mat', f)]
    files.sort()

    for i in range(num_iterations):
        full_database_with_missing_data = []
        full_database = []

        for j, filename in enumerate(files):
            print('#{} | {} | Extracting {} ...'.format(i + 1, j + 1, filename))

            temp = scipy.io.loadmat(files_directory + filename)

            # Obtém apenas a matriz desta observação com os 8 canais.
            dataset_orig = temp['data']

            # Obtém as leituras do canal de interesse.
            dataset_orig = dataset_orig[:, canal]

            # Normalização. (Let's hope this won't break the script).
            dataset_orig = normalize_var(dataset_orig, -1, 1)

            full_database.append(dataset_orig)

            dataset_missing_data = simulate_missing_data(dataset_orig, missing_data_percentage)

            full_database_with_missing_data.append(dataset_missing_data)

        full_database_data_imputed = KNNImputation.impute_data(full_database_with_missing_data)

        results = {
            'sum_squared_error': 0,
            'number_missing_values': 0
        }

        for row_i, row_missing_data in enumerate(full_database_with_missing_data):
            for col_i, col_missing_data in enumerate(row_missing_data):
                if np.isnan(col_missing_data):
                    imputed_value = full_database_data_imputed[row_i][col_i]
                    squared_error = pow((imputed_value - full_database[row_i][col_i]), 2)

                    results['sum_squared_error'] += squared_error
                    results['number_missing_values'] += 1

    mean_squared_error = results['sum_squared_error'] / results['number_missing_values']

    return {
        'mean_squared_error': mean_squared_error,
        'root_mean_squared_error': math.sqrt(mean_squared_error),
    }

list_missing_data_percentage = [60, 70, 80, 90]
# list_missing_data_percentage = [5]

results = {
    'imputation_method': [],
    'missing_data_percentage': [],
    'mean_squared_error': [],
    'root_mean_squared_error': [],
}

# imputation_strategy = KNNImputation
# imputation_strategies = ['Mean_Imputation', 'Interpolation_Imputation', 'KNN_Imputation_N_3', 'KNN_Imputation_N_7']
# imputation_strategies = [MeanImputation, InterpolationImputation, KNNImputation]
imputation_strategies = [KNNImputation]

for missing_data_percentage in list_missing_data_percentage:
    num_iterations = 10

    for imputation_strategy in imputation_strategies:
        print(">> {}% missing data | {} iterations | {} <<\n".format(missing_data_percentage, num_iterations, imputation_strategy.description))

        results_estimation = get_results_imputation_knn(missing_data_percentage, imputation_strategy)

        results['imputation_method'].append(imputation_strategy.description)
        results['missing_data_percentage'].append(missing_data_percentage)
        results['mean_squared_error'].append(results_estimation['mean_squared_error'])
        results['root_mean_squared_error'].append(results_estimation['root_mean_squared_error'])

df = pd.DataFrame(results)

print(df)

results_filename = '/home/alessandro/Documentos/Programming/Projects/TCC1/results/imputation_statistical_measures.csv'

df.to_csv(results_filename, mode='a', header=False, index=False)


import json
from random import random
from time import sleep

from util.utils import HttpUtils

class Crawler:
    # 评论数至少大于 N
    people_threshold = 1000

    @classmethod
    def start(cls):
        cls.crawl_book()
        cls.sort_book()

    @classmethod
    def crawl_book(cls):

        tag_source_url = "https://book.douban.com/tag/"
        soup_obj = HttpUtils.get(tag_source_url)

        tags = HttpUtils.get_contents(soup_obj, "div.article tr td a")

        tags = ['小说', '外国文学', '文学', '中国文学', '经典', '日本文学', '古典文学', '王小波', '当代文学',
                '钱钟书', '外国名著', '推理', '绘本', '青春', '东野圭吾', '科幻', '言情', '悬疑',
                '奇幻', '韩寒', '推理小说', '阿加莎·克里斯蒂', '科幻小说', '魔幻', '历史', '心理学', '哲学', '传记', '文化', '社会学', '艺术', '设计', '社会',
                '政治', '建筑', '宗教', '电影',
                '政治学', '数学', '中国历史', '回忆录', '思想', '国学', '人物传记', '人文', '音乐', '艺术史', '绘画', '戏剧', '西方哲学', '二战', '军事', '佛教',
                '近代史', '考古', '自由主义', '美术', '爱情', '旅行', '成长', '生活', '心理', '励志', '摄影', '教育', '游记', '灵修',
                '健康', '情感', '两性', '人际关系', '手工', '养生', '家居', '自助游', '经济学', '管理', '经济', '商业', '金融', '投资', '营销', '理财',
                '创业', '广告', '股票', '企业史', '策划', '科普', '互联网', '编程', '科学', '交互设计', '用户体验', '算法', '科技', 'web', 'UE', '交互',
                '通信', 'UCD', '神经网络', '程序']
        print(tags)

        book_shelf = dict()
        for tag in tags:
            for page in range(0, 10):
                url = "https://book.douban.com/tag/%s?start=%d&type=T" % (tag, page * 20)
                soup_obj = HttpUtils.get(url)

                if soup_obj is None:
                    print("blocked?")
                    break

                print(tag, page)
                books_obj = soup_obj.select("#subject_list ul > li")

                if len(books_obj) == 0:
                    break

                for book_obj in books_obj:
                    try:
                        title = HttpUtils.get_attr(book_obj, "h2 a", "title")
                        rating = float(HttpUtils.get_content(book_obj, "span.rating_nums"))
                        people = int(
                            HttpUtils.get_content(book_obj, "span.pl").strip().replace("人评价", "").replace("(",
                                                                                "").replace(
                                ")",
                                ""))

                        if people > cls.people_threshold:
                            if title in book_shelf:
                                book_shelf[title].tag.append(tag)
                            else:
                                book_shelf[title] = Book(title, rating, people, [tag])
                    except Exception as e:
                        pass

                # 为了应对时间窗口内单 ip 访问数量限制，只是停顿一下
                sleep(random() * 0.5 + 0.5)

        books = list(book_shelf.values())

        with open("douban_book_raw.txt", "w") as fp:
            fp.write(json.dumps(books, default=Book.convert))

    @classmethod
    def sort_book(cls):
        with open("douban_book_raw.txt", "r") as fp:
            line = fp.read()

        books = json.loads(line, object_hook=Book.revert)

        books.sort(key=lambda x: (5 ** x.rating) * (x.people ** 0.7), reverse=True)

        with open("douban_book_rating.txt", "w") as fp:
            for book in books:
                fp.write("%s\t%s\t%s\t%s\n" % (book.title, book.rating, book.people, book.tag))

class Book:
    title = ""
    rating = 0
    people = 0
    tag = []

    def __init__(self, title, rating, people, tag):
        self.title = title
        self.rating = rating
        self.people = people
        self.tag = tag

    def convert(std):
        return {'title': std.title,
                'rating': std.rating,
                'people': std.people,
                'tag': std.tag
                }

    def revert(std):
        return Book(std['title'], std['rating'], std['people'], std['tag'])

if __name__ == "__main__":
    Crawler.start()


#!/usr/bin/python
""" Script to play open an FPLC data file and plot UV280
    Usage: $ ./fplcplot1 [file]
	 Note: input files must be tab delimited """

import fileinput
# Initialize arrays
ml = []
mau = []

# collect switches to True onces file headers are skipped
collect = False
i = 0

# Begin reading file and input data into arrays after headers
for line in fileinput.input():
	rawline = line.split() # Splits string line into a list
	i += 1
	if collect and (len(rawline) > 0):
		ml.append(float(rawline[0]))
		mau.append(float(rawline[1]))
	elif (len(rawline) > 0) and line.startswith('ml'):
		collect = True
	else:
		if i == 5:
			collect = True

#Plotting the data
import matplotlib.pyplot as plt
ylimit = max(mau)*1.1
plt.xlim(min(ml), max(ml))
plt.ylim(min(mau),ylimit)
plt.xlabel('mL')
plt.ylabel('mAu')
#plt.title('FPLC Trace')
plt.plot(ml, mau)
plt.grid(True)
plt.show()
#plt.savefig('fplcplot.png')


from unittest import TestCase
from faker import Faker
import pandas as pd
import numpy as np

from server.data_processors import PlayerDataAggregator
from server.data_processors.player_data_aggregator import REQUIRED_COLS, STATS_COLS

FAKE = Faker()
N_ROWS = 10

class TestPlayerDataAggregator(TestCase):
    def setUp(self):
        self.index_cols = ["team", "year", "round_number"]
        self.transformer = PlayerDataAggregator(
            index_cols=self.index_cols, aggregations=["sum", "std"]
        )

        teams = [FAKE.company(), FAKE.company()]
        self.data_frame = pd.DataFrame(
            {
                **{
                    "team": sorted(teams * int(N_ROWS / 2)),
                    "oppo_team": list(reversed(sorted(teams * int(N_ROWS / 2)))),
                    "year": [2014] * N_ROWS,
                    "round_number": [12] * N_ROWS,
                    "score": np.random.randint(50, 150, N_ROWS),
                    "oppo_score": np.random.randint(50, 150, N_ROWS),
                    "at_home": ([1] * 5) + ([0] * 5),
                    "player_id": [n for n in range(N_ROWS)],
                    "player_name": [FAKE.name() for _ in range(N_ROWS)],
                },
                **{
                    stats_col: np.random.randint(0, 20, N_ROWS)
                    for stats_col in STATS_COLS
                },
            }
        )

    def test_transform(self):
        valid_data_frame = self.data_frame

        with self.subTest(data_frame=valid_data_frame):
            transformed_df = self.transformer.transform(valid_data_frame)

            self.assertIsInstance(transformed_df, pd.DataFrame)

            # We drop player_id & player_name, but add new stats cols for each aggregation
            expected_col_count = len(valid_data_frame.columns) - 2 + len(STATS_COLS)
            self.assertEqual(expected_col_count, len(transformed_df.columns))

            # Match data should remain unchanged (requires a little extra manipulation,
            # because I can't be bothred to make the score data realistic)
            for idx, value in (
                enumerate(valid_data_frame.groupby("team")["score"].mean().astype(int))
            ):
                self.assertEqual(value, transformed_df["score"].iloc[idx])

            for idx, value in (
                enumerate(valid_data_frame.groupby("team")["oppo_score"].mean().astype(int))
            ):
                self.assertEqual(value, transformed_df["oppo_score"].iloc[idx])

            # Player data should be aggregated, but same sum
            self.assertEqual(
                valid_data_frame["rolling_prev_match_kicks"].sum(),
                transformed_df["rolling_prev_match_kicks_sum"].sum(),
            )

        for required_col in REQUIRED_COLS + self.index_cols:
            invalid_data_frame = self.data_frame.drop(required_col, axis=1)

            with self.subTest(data_frame=invalid_data_frame):
                with self.assertRaises(ValueError):
                    self.transformer.transform(invalid_data_frame)


#!/usr/bin/env python3
from pathlib import Path

import perf

from cnn.utils.bench_factory import BenchmarkFactory

GRAPHS_DIR = (Path(__file__).parent / "models/").absolute()
FROZEN_NETS = [
    'dense_opt', 'dense_quant', 'conv_opt', 'conv_quant', 'medium_opt',
    'medium_quant'
]

def main(name, runner):
    bench_suite = BenchmarkFactory((GRAPHS_DIR / (name + ".pb")).as_posix(),
                                   runner)
    bench_suite.bench()

def add_cmdline_args(cmd, args):
    if args.net_name:
        cmd.append(args.net_name)

if __name__ == "__main__":
    runner = perf.Runner(add_cmdline_args=add_cmdline_args)
    runner.argparser.add_argument("net_name", nargs='?', choices=FROZEN_NETS)
    args = runner.parse_args()

    main(args.net_name, runner)


import os
from flask import Flask, render_template, request, redirect, session
from dotenv import load_dotenv

load_dotenv()

app = Flask(__name__)
app.secret_key = os.getenv('SECEST_KEY', 'for dev')

@app.route('/')
def visit():
    if 'visited' in session:
        session['visited'] += 1
    else:
        session['visited'] = 1
    return render_template('index.html', counter=session['visited'])

@app.route('/detroy_session')
def destroy_session():
    session.clear()
    return redirect('/')

@app.route('/count', methods=['GET'])
def show_count():
    if 'visited' in session:
        session['visited'] += 2
    else:
        session['visited'] = 1
    return render_template('count.html', counter=session['visited'])

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


"""
A small test to ensure the Raspberry Pi can actually connect
to the web socket without timing out
"""

import socketio
from time import time
from threading import Thread
from config import SERVER_URL

timing = {
    'start': 0,
    'end': 0
}
DEFAULT_TIMEOUT = 30

socket = socketio.Client()

def init_socket():
    socket.connect(SERVER_URL)
    socket.emit('Pi connected')

@socket.on('Pi connect success')
def connect_success(data):
    timing['end'] = time()
    print(
        'Success: got response in %.2f seconds.'
        % (timing['end'] - timing['start'])
    )

if __name__ == '__main__':
    thread = Thread(target=init_socket)
    thread.start()
    print('Testing connection...')

    timing['start'] = time()
    # End the socket connection after the default timeout if there
    # is no response from the server
    timeout = timing['start'] + DEFAULT_TIMEOUT

    # Make the program wait until we get a response
    while timing['end'] - timing['start'] <= 0:
        try:
            if timeout - time() <= 0:
                print('Connection to socket timed out, disconnecting...')
                socket.disconnect()
                exit()
        except KeyboardInterrupt:
            print('Disconnecting...')
            break

    socket.disconnect()


'''pspnet_unets5os16_hrf'''
import copy
from .base_cfg import SEGMENTOR_CFG
from .._base_ import DATASET_CFG_HRF_256x256, DATALOADER_CFG_BS16

# deepcopy
SEGMENTOR_CFG = copy.deepcopy(SEGMENTOR_CFG)
# modify dataset config
SEGMENTOR_CFG['dataset'] = DATASET_CFG_HRF_256x256.copy()
# modify dataloader config
SEGMENTOR_CFG['dataloader'] = DATALOADER_CFG_BS16.copy()
# modify scheduler config
SEGMENTOR_CFG['scheduler']['max_epochs'] = 1
# modify other segmentor configs
SEGMENTOR_CFG['num_classes'] = 2
SEGMENTOR_CFG['backbone'] = {
    'type': 'UNet', 'structure_type': 'unets5os16', 'pretrained': False, 'selected_indices': (3, 4),
}
SEGMENTOR_CFG['head'] = {
    'in_channels': 64, 'feats_channels': 16, 'pool_scales': [1, 2, 3, 6], 'dropout': 0.1,
}
SEGMENTOR_CFG['auxiliary'] = {
    'in_channels': 128, 'out_channels': 64, 'dropout': 0.1,
}
SEGMENTOR_CFG['inference'] = {
    'mode': 'slide',
    'opts': {'cropsize': (256, 256), 'stride': (170, 170)},
    'tricks': {
        'multiscale': [1], 'flip': False, 'use_probs_before_resize': True
    },
    'metric_list': ['dice', 'mdice'],
}
SEGMENTOR_CFG['work_dir'] = 'pspnet_unets5os16_hrf'
SEGMENTOR_CFG['logfilepath'] = 'pspnet_unets5os16_hrf/pspnet_unets5os16_hrf.log'
SEGMENTOR_CFG['resultsavepath'] = 'pspnet_unets5os16_hrf/pspnet_unets5os16_hrf_results.pkl'


#
# Module which deals with pickling of objects.
#
# multiprocessing/reduction.py
#
# Copyright (c) 2006-2008, R Oudkerk
# Licensed to PSF under a Contributor Agreement.
#
import os
import sys
import socket
from .reduction import register

# Windows
if sys.version_info[:2] < (3, 3):
    import _subprocess as _winapi

    from _multiprocessing import win32
    _winapi.OpenProcess = win32.OpenProcess
else:
    import _winapi

class DupHandle(object):
    def __init__(self, handle, access, pid=None):
        # duplicate handle for process with given pid
        if pid is None:
            pid = os.getpid()
        proc = _winapi.OpenProcess(_winapi.PROCESS_DUP_HANDLE, False, pid)
        try:
            self._handle = _winapi.DuplicateHandle(
                _winapi.GetCurrentProcess(),
                handle, proc, access, False, 0)
        finally:
            _winapi.CloseHandle(proc)
        self._access = access
        self._pid = pid

    def detach(self):
        # retrieve handle from process which currently owns it
        if self._pid == os.getpid():
            return self._handle
        proc = _winapi.OpenProcess(_winapi.PROCESS_DUP_HANDLE, False,
                                   self._pid)
        try:
            return _winapi.DuplicateHandle(
                proc, self._handle, _winapi.GetCurrentProcess(),
                self._access, False, _winapi.DUPLICATE_CLOSE_SOURCE)
        finally:
            _winapi.CloseHandle(proc)

if sys.version_info >= (3, 3):
    from multiprocessing.connection import Connection, PipeConnection
else:
    from _multiprocessing import Connection, PipeConnection

# make Connection pickable
def reduce_connection(conn):
    rh = DupHandle(conn.fileno(), _winapi.DUPLICATE_SAME_ACCESS)
    return rebuild_connection, (rh, conn.readable, conn.writable)

def rebuild_connection(reduced_handle, readable, writable):
    handle = reduced_handle.detach()
    return Connection(handle, readable=readable, writable=writable)

register(Connection, reduce_connection)

def reduce_pipe_connection(conn):
    access = ((_winapi.FILE_GENERIC_READ if conn.readable else 0) |
              (_winapi.FILE_GENERIC_WRITE if conn.writable else 0))
    dh = DupHandle(conn.fileno(), access)
    return rebuild_pipe_connection, (dh, conn.readable, conn.writable)

def rebuild_pipe_connection(dh, readable, writable):
    from .connection import PipeConnection
    handle = dh.detach()
    return PipeConnection(handle, readable, writable)

register(PipeConnection, reduce_pipe_connection)

# make sockets pickable
def _reduce_socket(s):
    from multiprocessing.resource_sharer import DupSocket
    return _rebuild_socket, (DupSocket(s),)

def _rebuild_socket(ds):
    return ds.detach()

register(socket.socket, _reduce_socket)


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D

# Program for problem 19 to calculate the trajectory of predator and prey populations
# Benjamin Klimko, PHYS 416, Spring 2018

def rk4(x,t,tau,derivsRK):
	##  Runge-Kutta integrator (4th order)
	## Input arguments -
	##   x = current value of dependent variable
	##   t = independent variable (usually time)
	##   tau = step size (usually timestep)
	##   derivsRK = right hand side of the ODE; derivsRK is the
	##             name of the function which returns dx/dt
	##             Calling format derivsRK(x,t).
	## Output arguments -
	##   xout = new value of x after a step of size tau
	half_tau = 0.5*tau
	F1 = derivsRK(x,t)
	t_half = t + half_tau
	xtemp = x + half_tau*F1
	F2 = derivsRK(xtemp,t_half)
	xtemp = x + half_tau*F2
	F3 = derivsRK(xtemp,t_half)
	t_full = t + tau
	xtemp = x + tau*F3
	F4 = derivsRK(xtemp,t_full)
	xout = x + tau/6.*(F1 + F4 + 2.*(F2+F3))
	return xout

def rka(x,t,tau,err,derivsRK):
	## Adaptive Runge-Kutta routine
	## Inputs
	##   x          Current value of the dependent variable
	##   t          Independent variable (usually time)
	##   tau        Step size (usually time step)
	##   err        Desired fractional local truncation error
	##   derivsRK   Right hand side of the ODE; derivsRK is the
	##              name of the function which returns dx/dt
	##              Calling format derivsRK(x,t).
	## Outputs
	##   xSmall     New value of the dependent variable
	##   t          New value of the independent variable
	##   tau        Suggested step size for next call to rka
	##* Set initial variables
	tSave = t;  xSave = x    # Save initial values
	safe1 = .9;  safe2 = 4.  # Safety factors
	eps = np.spacing(1) # smallest value
	##* Loop over maximum number of attempts to satisfy error bound
	maxTry = 100
	for iTry in range(1,maxTry):
		##* Take the two small time steps
		half_tau = 0.5 * tau
		xTemp = rk4(xSave,tSave,half_tau,derivsRK)
		t = tSave + half_tau
		xSmall = rk4(xTemp,t,half_tau,derivsRK)
		##* Take the single big time step
		t = tSave + tau
		xBig = rk4(xSave,tSave,tau,derivsRK)
		##* Compute the estimated truncation error
		scale = err * (np.abs(xSmall) + np.abs(xBig))/2.
		xDiff = xSmall - xBig
		errorRatio = np.max( [np.abs(xDiff)/(scale + eps)] )
		#print safe1,tau,errorRatio
		##* Estimate news tau value (including safety factors)
		tau_old = tau
		tau = safe1*tau_old*errorRatio**(-0.20)
		tau = np.max([tau,tau_old/safe2])
		tau = np.min([tau,safe2*tau_old])
		##* If error is acceptable, return computed values
		if errorRatio < 1 :
			xSmall = xSmall ## +  (xDiff)/15
			return xSmall, t, tau
	##* Issue error message if error bound never satisfied
	print ('ERROR: Adaptive Runge-Kutta routine failed')
	return

def dblpendrk(s,t):
	#  Returns right-hand side of Lotka-Volterra model ODEs
	#  Inputs
	#    s      State vector [theta1, theta2, dtheta1, dtheta2]
	#    t      Time (not used)
	#  Output
	#    deriv  Derivatives [dtheta1/dt, dtheta2/dt, d^2theta1/dt^2 d^2theta2/dt^2]

	alpha = (m1+m2)*L1
	beta = m2*L2*np.cos(s[0]-s[1])
	a = -(m1+m2)*g*np.sin(s[0]) - m2*L2*s[3]**2 *np.sin(s[0]-s[1])
	delta = m2*L1*np.cos(s[0]-s[1])
	ep = m2*L2
	b = -m2*g*np.sin(s[1]) + m2*L1*s[2]**2 * np.sin(s[0]-s[1])
	left = np.array([[alpha, beta], [delta, ep]])
	right = np.array([a, b])
	#* Return the derivatives [d^2theta1/dt^2 d^2theta2/dt^2]
	# dbldot = np.linalg.inv(left).dot(right)
	dbldot = np.linalg.solve(left, right)
	deriv = np.array([s[2], s[3], dbldot[0], dbldot[1]])

	return deriv

# set initial conditions and constants
g = 9.81 # gravity
m1 = 1
m2 = 1
L1 = 0.1
L2 = 0.1

#initial angles
theta1 = np.pi/4
theta2 = np.pi/2
th1dot = np.pi
th2dot = np.pi/12

# state vector: theta1, theta2, theta1_dot, theta2_dot
state = np.array([theta1, theta2, th1dot, th2dot])

tau = 1       # Initial guess for the timestep
err = 1.e-5  # Error tolerance
time = 0

nstep = 500

# loop through each step
for istep in range(0, nstep):
	if istep == 0: # initialize data vectors
		th1_plot = np.array([state[0]])
		th2_plot = np.array([state[1]])
		tplot = np.array([time])
		potential = np.array([-m1*L1*g*np.cos(state[0]) - m2*g*(L1*np.cos(state[0])+L2*np.cos(state[1]))])
		kinetic = np.array([(.5*(m1+m2)*L1**2*state[2]**2) + (.5*m2*L2**2*state[3]**2) + (m2*L1*L2*state[2]*state[3]*np.cos(state[0]-state[1]))])

	else: # append to data vectors
		th1_plot = np.append(th1_plot, state[0])
		th2_plot = np.append(th2_plot, state[1])
		tplot = np.append(tplot, time)
		potential = np.append(potential, -m1*L1*g*np.cos(state[0]) - m2*g*(L1*np.cos(state[0])+L2*np.cos(state[1])))
		kinetic = np.append(kinetic, (.5*(m1+m2)*L1**2*state[2]**2) + (.5*m2*L2**2*state[3]**2) +\
		 (m2*L1*L2*state[2]*state[3]*np.cos(state[0]-state[1])))

	[state, time, tau] = rka(state, time, tau, err, dblpendrk) # use adaptive Runge-Kutta to update the equations

# compute values for plotting

x1 = L1*np.sin(th1_plot)
y1 = -L1*np.cos(th1_plot)
x2 = x1 + L2*np.sin(th2_plot)
y2 = y1 - L2*np.cos(th2_plot)

#animate
plt.figure()
ax = plt.gca()
plt.title('Double Pendulum')

for i in range(0, len(x1)):
	plt.plot(x1[i], y1[i], 'ro')
	plt.plot(x2[i], y2[i], 'ko')
	ax.set_xlim(-.3, .3)
	ax.set_ylim(-.3, .3)
	l1 = Line2D([x1[i], x2[i]], [y1[i], y2[i]])
	l2 = Line2D([0, x1[i]], [0, y1[i]])
	ax.add_line(l1)
	ax.add_line(l2)
	plt.draw()
	plt.pause(0.01)
	# l1.remove()
	# l2.remove()
	if(i==len(x1)-1):
		#save plot out
		plt.savefig('c3bq19_3.png', bbox_inches='tight')
		# temp=input('Hit return to end:')
	else:
		# plt.clf()
		l1.remove()
		l2.remove()

totalE = potential + kinetic
plt.figure(2)
plt.plot(tplot, potential, label='potential energy')
plt.plot(tplot, kinetic, label='kinetic energy')
plt.plot(tplot, totalE, label='total energy')
plt.title('Double Pendulum Energy Diagram')
plt.legend()
plt.show()
# plt.savefig('c3bq19_1e.png', bbox_inches='tight')

temp=input('Hit return to end:')


from flask import Flask, Response
import json
import time

waitPeriod = 30

time.sleep(220)

app = Flask(__name__)

@app.route("/")
def hello():
    with open('/home/pi/Desktop/telegramnovo/relejson.json') as filejson:
        jsons = json.load(filejson)

    il1a = jsons['IL1-A']
    il1b = jsons['IL1-B']
    il1c = jsons['IL1-C']
    ioa = jsons['Io-A']
    u12kv = jsons['U12-kv']
    u23kv = jsons['U23-kv']
    u31kv = jsons['U31-kv']
    fhz = jsons['f-Hz']
    skva = jsons['S-kVA']
    pkw = jsons['P-kW']
    qkvar = jsons['Q-kVAr']
    pf = jsons['PF']
    ngseqkv = jsons['NgSeq-kV']
    psseqkv = jsons['PsSeq-kV']
    zroseqkv = jsons['ZroSeq-kV']
    eventos  = jsons['EVENTOS']

    JSOND = "{" + '"' + "IL1A" + '"' + " : " +str(float(il1a))+","+'"'+"IL1B"+'"'+" : "+str(float(il1b))+","+'"'+"IL1C"+'"'+" : "+str(float(il1c))+","+'"'+"IoA"+'"'+" : "+str(float(ioa))+","+'"'+"U12kv"+'"'+" : "+str(float(u12kv))+","+'"'+"U23kv"+'"'+" : "+str(float(u23kv))+","+'"'+"U31kv"+'"'+" : "+str(float(u31kv))+","+'"'+"fHz"+'"'+" : "+str(float(fhz))+","+'"'+"SkVA"+'"'+" : "+str(float(skva))+","+'"'+"PkW"+'"'+" : "+str(float(pkw))+","+'"'+"QkVAr"+'"'+" : "+str(float(qkvar))+","+'"'+"PF"+'"'+" : "+str(float(pf))+","+'"'+"NgSeqkV"+'"'+" : "+str(float(ngseqkv))+","+'"'+"PsSeqkV"+'"'+" : "+str(float(psseqkv))+","+'"'+"ZroSeqkV"+'"'+" : "+str(float(zroseqkv))+","+'"'+"EVENTOS"+'"'+" : "+'"'+str(eventos)+'"'+"}"

    return Response(JSOND, mimetype='application/json')

if __name__ == "__main__":
    app.run(host="10.5.145.37 ", port=int("10052"))


import os
from itertools import permutations
from menace import Board
from latex import preamb, postamb

positions = [[Board()], [], [], []]

for o1, x1 in permutations(range(9), 2):
    board = Board()
    board[o1] = 1
    board[x1] = 2
    if board.is_max() and not board.in_set(positions[1]):
        positions[1].append(board)

for o1, x1, o2, x2 in permutations(range(9), 4):
    board = Board()
    board[o1] = 1
    board[x1] = 2
    board[o2] = 1
    board[x2] = 2
    if board.is_max() and not board.in_set(positions[2]):
        positions[2].append(board)

for o1, x1, o2, x2, o3, x3 in permutations(range(9), 6):
    board = Board()
    board[o1] = 1
    board[x1] = 2
    board[o2] = 1
    board[x2] = 2
    board[o3] = 1
    board[x3] = 2
    if board.has_winner():
        continue
    if board.is_max() and not board.in_set(positions[3]):
        positions[3].append(board)

assert len(positions[0]) == 1
assert len(positions[1]) == 12
assert sum([len(p) for p in positions]) == 304

for i, boards in enumerate(positions):
    latex = preamb
    for j, board in enumerate(boards):
        latex += board.as_latex()
        latex += "\n"
        if (j + 1) % 5 == 0:
            latex += "\n\\noindent"
    latex += postamb
    print(latex)
    with open(f"output/boxes{i}.tex", "w") as f:
        f.write(latex)
    assert os.system(
        f"pdflatex -output-directory output output/boxes{i}.tex") == 0


import time
import numpy as np
from skimage import io

start = time.clock()
# 加粗白線的方式 mask上設定如果周圍有兩到四個個白點，就設定中間白點
# 濾橫波

def Filter_img (L):
    kernel = (np.array([[1, 2, 1],
                        [0, 0, 0],
                        [-1, -2, -1]]))
    height, width = L.shape
    filtered = np.zeros_like(L)
    for y in range(height):
        for x in range(width):
            weighted_pixel_sum = 0
            for ky in range(-1, 2):
                for kx in range(-1, 2):
                    pixel = 0
                    pixel_y = y + ky
                    pixel_x = x + kx
                    if (pixel_y >= 0) and (pixel_y < height) and (pixel_x >= 0) and (pixel_x < width):
                        pixel = L[pixel_y, pixel_x]
                    weight = kernel[ky + 1, kx + 1]
                    weighted_pixel_sum += pixel * weight
            filtered[y, x] = abs(weighted_pixel_sum)

    Range = filtered.max() - filtered.min()
    filtered = (filtered - filtered.min()) / Range
    return filtered

L = io.imread("bOX.bmp",as_grey=True)
filtered = Filter_img(L)
io.imsave('filtered1.jpg', filtered)

L = io.imread("bOX2.bmp", as_grey=True)
filtered = Filter_img(L)
io.imsave('filtered2.jpg', filtered)

time_used = (time.clock() - start)
print("Time used:",time_used)


#!/usr/bin/python

import numpy as np
import os,fnmatch

data_dir = os.listdir("./wt")

pattern = "*weight*.npy"

file_names = []

for file in data_dir:
	if (fnmatch.fnmatch(file,pattern)):
		print(file[:-4])
		file_names.append(file)

# exit()
text_file = open("mc_weights.c","w")

params = ""

for file in file_names:
	data = np.load("./wt/" + file)

	params += "#define " + str(file[:-4]).upper() +"_ROWS " + str(data.shape[0]) + "\n"
	params += "#define " + str(file[:-4]).upper() +"_COLS " + str(data.shape[1]) + "\n"

	params += "\n"

	params += "#define "+ str(file[:-4]).upper() + " "
	print(file)
	print(data.shape)
	for rows in range(data.shape[0]):
		for cols in range(data.shape[1]):
			params += str(data[rows][cols])+" ,"

	params = params[:-1]

	params += "\n\n"

text_file.write(params)
text_file.close()


import sys
from PyQt5 import QtWidgets, QtCore

class FileBrowser(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 480
        self.file_name = ""
        self.init_ui()

    def init_ui(self):
        point = QtCore.QRect(self.left, self.top, self.width, self.height)
        self.setGeometry(QtCore.QRect(point.center(), point.size()))
        self.setFixedSize(self.width, self.height)
        self.open_file_name_dialog()

    def open_file_name_dialog(self):
        options = QtWidgets.QFileDialog.Options()
        options |= QtWidgets.QFileDialog.DontUseNativeDialog
        self.file_name, _ = QtWidgets.QFileDialog.getOpenFileName(
            self,
            "QFileDialog.getOpenFileName()",
            "../Algorithm/",
            "All Files (*);;",
            options=options
        )

    def get_filename(self):
        return self.file_name


'''
as_completed()方法
对多个任务，不会按顺序返回每个任务，而是先完成的任务先返回
并且不需要等待所有任务都完成,任务完成后就返回了
'''
import asyncio

async def phase(i):
    print('in phase {}'.format(i))
    await asyncio.sleep(0.5 - (0.1 * i))
    print('done with phase {}'.format(i))
    return 'phase {} result'.format(i)

async def main(num_phases):
    print('starting main')
    phases = [phase(i) for i in range(num_phases)]
    print('waitingh for phases to complete')
    results = []
    # results = await asyncio.gather(*phases)
    for next_to_complete in asyncio.as_completed(phases):
        answer = await next_to_complete
        print('received answer {!r}'.format(answer))
        results.append(answer)
    print('results: {!r}'.format(results))
    return results

loop = asyncio.get_event_loop()
loop.run_until_complete(main(3))


from ..models import SeqAttModel
from ...credentials import azure_account_name, azure_account_key
from azure.storage.blob import BlockBlobService
import tensorflow as tf

def initialize_func(sess, logger, FLAGS):
  '''
  Initialize main function to predict using model.
  '''

  tf.set_random_seed(1)

  ##############################
  ### Log hyperparameters.
  param_desc = FLAGS.model_name + ":   "
  for flag, val in FLAGS.__dict__['__flags'].items():
    param_desc += flag + ": " + str(val) + "; "
  logger.debug("Parameters " + param_desc)
  ##############################

  ##############################
  ### Instantiate model.
  model = SeqAttModel(sess, FLAGS, logger,
                      model_name=FLAGS.model_name)
  logger.debug('Model instantiated.')
  ##############################

  ##############################
  ### Download model
  block_blob_service = BlockBlobService(
      account_name=azure_account_name,
      account_key=azure_account_key
  )

  for suffix in [".meta", ".index", ".data-00000-of-00001"]:
    filename = FLAGS.model_name + "-" + str(FLAGS.iter_num) + suffix
    logger.debug("Loading save file: " + filename)
    assert(filename in [x.name for x in
                        block_blob_service.list_blobs("models")])
    block_blob_service.get_blob_to_path(
        "models",
        filename,
        FLAGS.checkpoints_dir + filename
    )
  ##############################

  ##############################
  ### Build model
  model.initialize()
  logger.debug('Model initialized.')
  model.restore(FLAGS.checkpoints_dir + FLAGS.model_name +
                "-" + FLAGS.iter_num)
  logger.debug('Model restored.')
  ##############################

  def function(query):
    result = model.predict([query])[0]
    logger.debug('Model predicted: ' + str(result))
    return result

  return function


# title: constrained-subsequence-sum
# detail: https://leetcode.com/submissions/detail/383266043/
# datetime: Wed Aug 19 23:59:30 2020
# runtime: 544 ms
# memory: 27 MB

class Solution:
    def constrainedSubsetSum(self, nums: List[int], k: int) -> int:
        n = len(nums)
        q = collections.deque([(nums[0], 0)])
        result = nums[0]
        for i in range(1, n):
            while q and q[0][1] < i - k:
                q.popleft()
            a = q[0][0]
            m = nums[i] + (a if a > 0 else 0)
            while q and m >= q[-1][0]:
                q.pop()
            q.append((m, i))
            result = max(result, m)
        return result


# Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
#
# Note that the row index starts from 0.
#
#
# In Pascal's triangle, each number is the sum of the two numbers directly above it.
#
# Example:
#
# Input: 3
# Output: [1,3,3,1]
# Follow up:
#
# Could you optimize your algorithm to use only O(k) extra space?

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        ans = [1]
        for i in range(rowIndex):
            ans = [x+y for x, y in zip([0]+ans, ans+[0])]
        return ans


#coding:utf-8
import xadmin
from sblog.models import Author,Tag,Blog
from django.conf import settings
from photologue.models import Gallery,Photo,PhotoEffect,PhotoSize,Watermark
from xadmin import views
from xadmin.layout import Col, Fieldset

from .models import Blog,Author,Tag,AccessRecord
from .forms import GalleryAdminForm,PhotoAdminForm,BlogForm
from threadedcomments.models import ThreadedComment

MULTISITE = getattr(settings, 'PHOTOLOGUE_MULTISITE', False)
# Register your models here.
class BaesSetting(object):
    enable_themes = True
    use_bootswatch = True

class GlobalSetting(object):
    site_title = u"喵黑小熊"
    site_footer = u"我的！窝哒！还是我大！"
    menu_style = 'accordion'
    global_search_models = [Blog,Gallery,ThreadedComment]
    global_models_icon = {
        Blog:'fa fa-x fa-laptop', Gallery:'fa fa-x fa-camera' ,
        ThreadedComment:'fa fa-comment',
    }

    apps_label_title = {
        'auth':u"权限管理",
        'threadedcomments':u'评论',
        'reversion':u'版本控制',
    }

    def get_site_menu(self):
        return (
            {'title':u'博客','perm':self.get_model_perm(Blog,'change'),'icon':'fa fa-x fa-laptop','menus':(
                {'title':u'文章','url':self.get_model_url(Blog,'changelist'),'icon':'fa fa-pencil-square-o'},
                {'title':u'作者','url':self.get_model_url(Author,'changelist'),'icon':'fa fa-user'},
                {'title':u'标签','url':self.get_model_url(Tag,'changelist'),'icon':'fa fa-tag'},
            )},
            {'title':u'相册&照片','perm':self.get_model_perm(Gallery,'change'),'icon':'fa fa-x fa-camera','menus':(
                {'title':u'画廊','url':self.get_model_url(Gallery,'changelist'),'icon':'fa fa-x fa-book'},
                {'title':u'照片','url':self.get_model_url(Photo,'changelist'),'icon':'fa fa-x fa-camera'},
                {'title':u'照片尺寸','url':self.get_model_url(PhotoSize,'changelist'),'icon':'fa fa-x fa-scissors'},
                {'title':u'照片效果','url':self.get_model_url(PhotoEffect,'changelist'),'icon':'fa fa-x fa-adjust'},
                {'title':u'水印','url':self.get_model_url(Watermark,'changelist'),'icon':'fa fa-x fa-moon-o'},
            )},
            {'title':u'系统日志','perm':self.get_model_perm(AccessRecord,'change'),'icon':'fa fa-x fa-desktop','menus':(
                {'title':u'访问记录','url':self.get_model_url(AccessRecord,'changelist')},
            )},
        )

class AuthorAdmin(object):
    """docstring for AuthorAdmin"""
    list_display = ('name','email','website')
    search_fields = ('name',)
    reversion_enable = True

class BlogAdmin(object):
    """docstring for BlogAdmin"""
    list_display = ('caption','id','tags_names','author','publish_time')
    list_filter = ('publish_time','author')
    date_hierarchy = 'publish_time'
    ordering = ('-publish_time',)
    filter_horizontal = ('tags',)
    form = BlogForm
    reversion_enable = True

class TagAdmin(object):
    list_display = ('tag_name','create_time')
    reversion_enable = True

class GalleryAdmin(object):
    list_display = ('title', 'date_added', 'photo_count', 'is_public')
    list_filter = ['date_added', 'is_public']
    if MULTISITE:
        list_filter.append('sites')
    date_hierarchy = 'date_added'
    prepopulated_fields = {'slug': ('title',)}
    form = GalleryAdminForm
    reversion_enable = True

class PhotoAdmin(object):
    list_display = ('title', 'date_taken', 'date_added',
                    'is_public', 'view_count', 'admin_thumbnail')
    list_filter = ['date_added', 'is_public']
    if MULTISITE:
        list_filter.append('sites')
    search_fields = ['title', 'slug', 'caption']
    list_per_page = 10
    prepopulated_fields = {'slug': ('title',)}
    readonly_fields = ('date_taken',)
    form = PhotoAdminForm
    reversion_enable = True

    if MULTISITE:
        filter_horizontal = ['sites']
    if MULTISITE:
        actions = ['add_photos_to_current_site', 'remove_photos_from_current_site']

class PhotoSizeAdmin(object):
    list_display = ('name', 'width', 'height', 'crop', 'pre_cache', 'effect', 'increment_count')
    fieldsets = (
        (None, {
            'fields': ('name', 'width', 'height', 'quality')
        }),
        ('Options', {
            'fields': ('upscale', 'crop', 'pre_cache', 'increment_count')
        }),
        ('Enhancements', {
            'fields': ('effect', 'watermark',)
        }),
    )
    reversion_enable = True

class PhotoEffectAdmin(object):
    list_display = ('name', 'description', 'color', 'brightness',
                    'contrast', 'sharpness', 'filters', 'admin_sample')
    fieldsets = (
        (None, {
            'fields': ('name', 'description')
        }),
        ('Adjustments', {
            'fields': ('color', 'brightness', 'contrast', 'sharpness')
        }),
        ('Filters', {
            'fields': ('filters',)
        }),
        ('Reflection', {
            'fields': ('reflection_size', 'reflection_strength', 'background_color')
        }),
        ('Transpose', {
            'fields': ('transpose_method',)
        }),
    )
    reversion_enable = True

class WatermarkAdmin(object):
    list_display = ('name', 'opacity', 'style')
    reversion_enable = True

class ThreadedCommentsAdmin(object):
    fieldsets = (
        (None,
           {'fields': ('content_type', 'object_pk', 'site')}
        ),
        (('Content'),
           {'fields': ('user', 'user_name', 'user_email', 'user_url', 'title', 'comment')}
        ),
        (('Hierarchy'),
           {'fields': ('parent',)}
        ),
        (('Metadata'),
           {'fields': ('submit_date', 'ip_address', 'is_public', 'is_removed')}
        ),
    )

    list_display = ('name', 'title', 'content_type', 'object_pk', 'parent',
                    'ip_address', 'submit_date', 'is_public', 'is_removed')
    search_fields = ('title', 'comment', 'user__username', 'user_name',
                     'user_email', 'user_url', 'ip_address')
    raw_id_fields = ("parent",)
    reversion_enable = True

class AccessRecordAdmin(object):
    def avg_count(self, instance):
        return int(instance.view_count / instance.user_count)

    avg_count.short_description = "Avg Count"
    avg_count.allow_tags = True
    avg_count.is_column = True

    list_display = ('date', 'user_count', 'view_count', 'avg_count')
    list_display_links = ('date',)

    list_filter = ['date', 'user_count', 'view_count']
    actions = None
    aggregate_fields = {"user_count": "sum", 'view_count': "sum"}

    refresh_times = (3, 5, 10)
    data_charts = {
        "user_count": {'title': u"User Report", "x-field": "date", "y-field": ("user_count", "view_count"),
                       "order": ('date',)},
        "avg_count": {'title': u"Avg Report", "x-field": "date", "y-field": ('avg_count',), "order": ('date',)},
        "per_month": {'title': u"Monthly Users", "x-field": "_chart_month", "y-field": ("user_count",),
                      "option": {
                          "series": {"bars": {"align": "center", "barWidth": 0.8, 'show': True}},
                          "xaxis": {"aggregate": "sum", "mode": "categories"},
                      },
                      },
    }

    def _chart_month(self, obj):
        return obj.date.strftime("%B")

xadmin.site.register(views.BaseAdminView,BaesSetting)
xadmin.site.register(views.CommAdminView,GlobalSetting)
xadmin.site.register(Author,AuthorAdmin)
xadmin.site.register(Blog,BlogAdmin)
xadmin.site.register(Tag,TagAdmin)

xadmin.site.register(Gallery,GalleryAdmin)
xadmin.site.register(Photo,PhotoAdmin)
xadmin.site.register(PhotoSize,PhotoSizeAdmin)
xadmin.site.register(PhotoEffect,PhotoEffectAdmin)
xadmin.site.register(Watermark,WatermarkAdmin)
xadmin.site.register(ThreadedComment,ThreadedCommentsAdmin)
xadmin.site.register(AccessRecord, AccessRecordAdmin)


valor=0
soma=0
teste=False
antes=0
valor=int(input("Digite um número inteiro:"))
while(valor>0):
    antes=(valor%10)
    soma= soma + (valor%10)
    valor=valor//10
    if((valor%10)==antes):teste=True
if(teste): print("sim")
else: print("não")


'''
This script is heavily inspired by the `run_glue.py` script provided by the HuggingFace team here:
https://github.com/huggingface/transformers/blob/master/examples/run_glue.py
'''
import sys

sys.path.append('../reformer_pytorch')

import argparse
import glob
import json
import logging
import os
import random
from datetime import datetime
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset
from torch.utils.data.distributed import DistributedSampler
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm, trange
from reformer_pytorch import Reformer
from reformer_lm_pytorch import ReformerLM

from transformers import BertTokenizer, AdamW, get_linear_schedule_with_warmup
from transformers.data.metrics import glue_compute_metrics as compute_metrics
from transformers.data.processors.glue import glue_convert_examples_to_features as convert_examples_to_features
from transformers.data.processors.glue import glue_output_modes as output_modes
from transformers.data.processors.glue import glue_processors as processors

class FCSoftmax(nn.Module):
    def __init__(self, input_dim, output_dim):
        super(FCSoftmax, self).__init__()
        self.fc = nn.Linear(input_dim, output_dim)
        self.softmax = nn.Softmax()

    def forward(self, x):
        x = self.fc(x)
        x = self.softmax(x)
        return x

class TrainerGLUE(object):

    def __init__(self,
                 model,
                 tokenizer,
                 max_seq_len=8192,
                 task=None,
                 output_mode=None,
                 seed=42,
                 model_name_or_path=None,
                 per_gpu_train_batch_size=8,
                 per_gpu_eval_batch_size=8,
                 output_dir='./output_data',
                 data_dir='../data/glue',
                 fp16=False,
                 fp16_opt_level=0,
                 num_tokens = None,
                 output_dim = 2):
        self.device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
        self.seed = seed
        self.model_name_or_path = model_name_or_path

        self.fc_layer = FCSoftmax(input_dim = num_tokens, output_dim = output_dim).to(self.device)
        self.model = nn.Sequential(model, self.fc_layer).to(self.device)

        self.tokenizer = tokenizer
        self.max_seq_len = max_seq_len
        self.n_gpu = torch.cuda.device_count() if torch.cuda.is_available() else 0

        self.per_gpu_train_batch_size = per_gpu_train_batch_size
        self.train_batch_size = per_gpu_train_batch_size * max(1, self.n_gpu)

        self.per_gpu_eval_batch_size = per_gpu_eval_batch_size
        self.eval_batch_size = self.per_gpu_eval_batch_size * max(1, self.n_gpu)

        self.fp16 = fp16
        self.fp16_opt_level = fp16_opt_level

        self.data_dir = str(data_dir) + '/' + str(task)
        self.output_dir = output_dir

        self.task = task
        self.output_mode = output_modes[self.task]

        self.loss_fn = nn.CrossEntropyLoss()
        self.writer = SummaryWriter()

    def set_seed(self):
        random.seed(self.seed)
        np.random.seed(self.seed)
        torch.manual_seed(self.seed)
        if self.n_gpu > 0:
            torch.cuda.manual_seed_all(self.seed)

    def train(self,
              train_dataset,
              task_name,
              num_train_epochs=3.0,
              weight_decay=0.0,
              learning_rate=5e-5,
              adam_epsilon=1e-8,
              warmup_steps=0,
              max_grad_norm=1.0,
              max_steps=-1,
              gradient_accumulation_steps=1,
              logging_steps=1000,
              save_steps=10000,
              ):

        train_sampler = RandomSampler(train_dataset)
        train_dataloader = DataLoader(train_dataset, batch_size=self.train_batch_size, sampler=train_sampler)

        if max_steps > 0:
            t_total = max_steps
            num_train_epochs = max_steps // (len(train_dataloader) // gradient_accumulation_steps) + 1
        else:
            t_total = len(train_dataloader) // gradient_accumulation_steps * num_train_epochs

        no_decay = ['bias', 'LayerNorm.weight']
        optimizer_grouped_parameters = [
            {
                'params': [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay)],
                'weight_decay': weight_decay
            },
            {
                'params': [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay)],
                'weight_decay': 0.0
            }
        ]

        optimizer = AdamW(optimizer_grouped_parameters, lr=learning_rate, eps=adam_epsilon)
        scheduler = get_linear_schedule_with_warmup(
            optimizer,
            num_warmup_steps=warmup_steps,
            num_training_steps=t_total
        )

        # checking if another optimizer/scheduler exists
        if os.path.isfile('optimizer.pt') and os.path.isfile('scheduler.pt'):
            # if so, load them
            optimizer.load_state_dict(torch.load('optimizer.pt'))
            scheduler.load_state_dict(torch.load('scheduler.pt'))

        if self.fp16:
            try:
                from apex import amp
            except ImportError:
                raise ImportError("Please install apex from https://www.github.com/nvidia/apex to use fp16 training.")
            self.model, optimizer = amp.initialize(self.model, optimizer, opt_level=self.fp16_opt_level)

        if self.n_gpu > 1:
            self.model = torch.nn.DataParallel(self.model)

        # Training

        global_step = 0
        epochs_trained = 0
        steps_trained_in_current_epoch = 0

        # check to see if we are continuing from a checkpoint
        if self.model_name_or_path is not None:
            if os.path.exists(self.model_name_or_path):
                global_step = int(self.model_name_or_path.split('-')[-1].split('/')[0])
                epochs_trained = global_step // (len(train_dataloader) // gradient_accumulation_steps)
                steps_trained_in_current_epoch = global_step % (len(train_dataloader) // gradient_accumulation_steps)

        tr_loss, logging_loss = 0.0, 0.0
        self.model.zero_grad()
        train_iterator = trange(
            epochs_trained, int(num_train_epochs), desc='Epoch', disable=False
        )
        self.set_seed()

        epoch_num = 1

        for _ in train_iterator:
            epoch_iterator = tqdm(train_dataloader, desc='Iteration', disable=False)

            for step, batch in enumerate(epoch_iterator):

                # skip any steps already trained on if picking up from a checkpoint
                if steps_trained_in_current_epoch > 0:
                    steps_trained_in_current_epoch -= 1
                    continue

                self.model.train()
                batch = tuple(t.to(self.device) for t in batch)

                input_ids = batch[0]
                attention_mask = batch[1]
                token_type_ids = batch[2]
                labels = torch.tensor(batch[3], dtype=torch.long, device = self.device)

                # probably only need to pass the input_ids, after we
                # apply the masking ourselves
                outputs = self.model(input_ids)
                outputs = torch.argmax(outputs, dim=-1)

                loss = self.loss_fn(outputs.float(), labels.long())
                loss.requires_grad = True

                if self.n_gpu > 1:
                    loss = loss.mean()
                if gradient_accumulation_steps > 1:
                    loss = loss / gradient_accumulation_steps

                if self.fp16:
                    with amp.scale_loss(loss, optimizer) as scaled_loss:
                        scaled_loss.backward()
                else:
                    loss.backward()

                tr_loss += loss.item()

                epoch_iterator.set_description('Iteration %d: [loss = %f]' % (step, loss.item()))
                train_iterator.set_description('Epoch %d: [avg_loss = %f]' % (epoch_num, tr_loss / float(step + 1)))

                # tensorboard logging
                self.writer.add_scalar('loss/train', loss.item(), global_step)

                if (step + 1) % gradient_accumulation_steps == 0:
                    if self.fp16:
                        torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer), max_grad_norm)
                    else:
                        torch.nn.utils.clip_grad_norm_(self.model.parameters(), max_grad_norm)

                    optimizer.step()
                    scheduler.step()
                    self.model.zero_grad()
                    global_step += 1

                    if logging_steps > 0 and global_step % logging_steps == 0:
                        logs = {}

                        results = self.evaluate(task_name)
                        for key, value in results.items():
                            eval_key = f'eval_{key}'
                            logs[eval_key] = value
                            self.writer.add_scalar(str(key) + '/eval', value, global_step)

                        loss_scalar = (tr_loss - logging_loss) / logging_steps
                        learning_rate_scalar = scheduler.get_lr()[0]
                        logs['learning_rate'] = learning_rate_scalar
                        logs['loss'] = loss_scalar
                        logging_loss = tr_loss

                        for key, value in logs.items():
                            # add logging
                            pass
                        print(json.dumps({**logs, **{"step": global_step}}))

                if save_steps > 0 and global_step % save_steps == 0:
                    output_dir = os.path.join(self.output_dir, f'checkpoint-{global_step}')
                    if not os.path.exists(output_dir):
                        os.makedirs(output_dir)
                    model_to_save = (
                        self.model.module if hasattr(self.model, 'module') else self.model
                    )
                    torch.save(model_to_save, f'{output_dir}.pt')
                    torch.save(optimizer.state_dict(), os.path.join(output_dir, 'optimizer.pt'))
                    torch.save(scheduler.state_dict(), os.path.join(output_dir, 'scheduler.pt'))

                if 0 < max_steps < global_step:
                    epoch_iterator.close()
                    break

            epoch_num += 1

            if 0 < max_steps < global_step:
                train_iterator.close()
                break

        return global_step, tr_loss / global_step

    def evaluate(self, task_name):
        # Loop to handle MNLI double evaluation (matched, mis-matched)
        eval_task_names = ("mnli", "mnli-mm") if task_name == "mnli" else (task_name,)
        eval_outputs_dirs = (self.output_dir, self.output_dir + '-MM') if task_name == 'mnli' else (self.output_dir,)

        results = {}
        for eval_task, eval_output_dir in zip(eval_task_names, eval_outputs_dirs):
            eval_dataset = self.load_and_cache_examples(eval_task, evaluate=True)
            if not os.path.exists(eval_output_dir):
                os.makedirs(eval_output_dir)
            eval_sampler = SequentialSampler(eval_dataset)
            eval_dataloader = DataLoader(eval_dataset, sampler=eval_sampler, batch_size=self.eval_batch_size)

            if self.n_gpu > 1:
                self.model = torch.nn.DataParallel(self.model)

            eval_loss = 0.0
            nb_eval_steps = 0
            preds = None
            out_label_ids = None

            for batch in tqdm(eval_dataloader, desc='Evaluating'):
                self.model.eval()
                batch = tuple(t.to(self.device) for t in batch)

                with torch.no_grad():
                    input_ids = batch[0]
                    attention_mask = batch[1]
                    token_type_ids = batch[2]
                    labels = batch[3]

                    outputs = model(input_ids)
                    outputs = torch.argmax(outputs, dim=-1).float()
                    tmp_eval_loss = self.loss_fn(outputs, labels.long())
                    eval_loss += tmp_eval_loss.mean().item()

                nb_eval_steps += 1
                if preds is None:
                    preds = torch.argmax(outputs, dim=-1).detach().cpu().numpy()
                    out_label_ids = labels.detach().cpu().numpy()
                else:
                    preds = np.append(preds, torch.argmax(outputs, dim=-1).detach().cpu().numpy())
                    out_label_ids = np.append(out_label_ids, labels.detach().cpu().numpy())

            eval_loss = eval_loss / nb_eval_steps
            if self.output_mode == 'classification':
                #preds = np.argmax(preds, axis=1)
                pass
            elif self.output_mode == 'regression':
                preds = np.squeeze(preds)
            result = compute_metrics(eval_task, preds, out_label_ids)
            results.update(result)

            output_eval_file = os.path.join(eval_output_dir, 'eval_results.txt')
            with open(output_eval_file, 'w') as writer:
                for key in sorted(result.keys()):
                    writer.write(f'{key} = {result[key]}')
        return results

    def load_and_cache_examples(self, task, evaluate=False):

        processor = processors[task]()
        output_mode = output_modes[task]
        cached_features_file = os.path.join(
            self.data_dir,
            "cached_{}_{}_{}".format(
                "dev" if evaluate else "train",
                # list(filter(None, self.model_name_or_path.split("/"))).pop(),
                str(self.max_seq_len),
                str(task),
            )
        )
        if os.path.exists(cached_features_file):
            features = torch.load(cached_features_file)
        else:
            label_list = processor.get_labels()
            examples = (
                processor.get_dev_examples(self.data_dir) if evaluate else processor.get_train_examples(self.data_dir)
            )

            features = convert_examples_to_features(
                examples,
                tokenizer,
                label_list=label_list,
                max_length=self.max_seq_len,
                output_mode=self.output_mode,
                pad_on_left=False,
                pad_token=self.tokenizer.convert_tokens_to_ids([self.tokenizer.pad_token])[0],
                pad_token_segment_id=0,
            )
            torch.save(features, cached_features_file)

        # converting to tensors and building datasets
        all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)
        all_attention_mask = torch.tensor([f.attention_mask for f in features], dtype=torch.long)
        all_token_type_ids = torch.tensor([f.token_type_ids for f in features], dtype=torch.long)

        if self.output_mode == 'classification':
            all_labels = torch.tensor([f.label for f in features], dtype=torch.long)
        elif self.output_mode == 'regression':
            all_labels = torch.tensor([f.label for f in features], dtype=torch.float)
        else:
            all_labels = None

        dataset = TensorDataset(
            all_input_ids,
            all_attention_mask,
            all_token_type_ids,
            all_labels
        )
        return dataset

## -- main --

parser = argparse.ArgumentParser(description='Main GLUE trainer')
parser.add_argument('--tasks', default = None)

parser.add_argument('--gpu', action='store_true')

parser.add_argument('--max_seq_len', type=int, default = 512)
parser.add_argument('--n_hashes', type=int)
parser.add_argument('--causal', action='store_true')
parser.add_argument('--tied_connections', action='store_true')
parser.add_argument('--kmeans', action='store_true')
parser.add_argument('--full_attention', action='store_true')

parser.add_argument('--ckpt_dir', type=str, default = "")

parser.add_argument('--num_train_epochs', type=int, default = 10)
parser.add_argument('--num_eval_steps', type=int, default = 20)
args = parser.parse_args()

device = torch.device("cuda" if torch.cuda.is_available() and args.gpu else "cpu")

tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
tokenizer.max_len = args.max_seq_len

model = ReformerLM(
    num_tokens      = tokenizer.vocab_size,
    dim             = 512,
    depth           = 6,
    heads           = 1,
    n_hashes        = args.n_hashes,
    max_seq_len     = args.max_seq_len,
    causal          = args.causal,
    recurrence      = args.tied_connections,
    k_means_hashing = args.kmeans,
    use_full_attn   = args.full_attention
).to(device)

if args.ckpt_dir is not "":
    assert os.path.isdir(args.ckpt_dir)
    try:
        logging.info(f'{datetime.now()} | Continuing from checkpoint...')
        model.load_state_dict(torch.load(f'{ckpt_dir}/model_state_dict.pt', map_location=device))
        print('Model loaded!')
    except Exception as e:
        logging.info(f'{datetime.now()} | No checkpoint was found | {e}')

# training on glue tasks
for key in list(args.tasks.split(',')) if args.tasks is not None else processors.keys():
    task_name = key.lower()
    processor = processors[task_name]()
    output_mode = output_modes[task_name]
    label_list = processor.get_labels()
    num_labels = len(label_list)

    print("Training task " + task_name + " with output mode " + output_mode)

    trainer = TrainerGLUE(
        model=model,
        max_seq_len=args.max_seq_len,
        tokenizer=tokenizer,
        task=key,
        per_gpu_train_batch_size=32,
        per_gpu_eval_batch_size=8,
        num_tokens = tokenizer.vocab_size,
        output_dim = num_labels
    )

    train_dataset = trainer.load_and_cache_examples(task_name, tokenizer)
    print('Dataset loaded')
    trainer.train(train_dataset, task_name=task_name, logging_steps=args.num_eval_steps, num_train_epochs=args.num_train_epochs)
    print(f'Trained: {task_name}')


from sklearn.externals import joblib
from keras.models import load_model
from tools import proc_text,cal_bow
from keras.preprocessing import sequence
import numpy as np

def NB_predict(string):
    nb_model = joblib.load('NaiveBayes_2000.pkl')
    bow = cal_bow(string)
    answer = nb_model.predict([bow])
    return answer
    # print('贝叶斯分类器对字符串：{0} 的预测标签为 {1}'.format(string, answer))

def CNN_predict(string):
    model = load_model('text_cnn_finally.h5')
    tokenizer = joblib.load('tokenizer.pkl')
    pro = proc_text(string)
    seq = tokenizer.texts_to_sequences([pro])
    vec = sequence.pad_sequences(seq, maxlen=100)
    answer = model.predict(vec)
    label = (1 if (answer[0] > 0.5) else 0)
    return label

def LSTM_predict(string):
    model = load_model('lstm_first.h5')
    tokenizer = joblib.load('tokenizer.pkl')
    pro = proc_text(string)
    seq = tokenizer.texts_to_sequences([pro])
    vec = sequence.pad_sequences(seq, maxlen=100)
    answer = model.predict(vec)
    label = (1 if (answer[0] > 0.5) else 0)
    return label


def busca_preco(loja):
    if loja == 'aliexpress':
        for serial in gera_lista(loja):
            d.get(f'https://www.aliexpress.com/item/{serial}.html')
            price = d.find_element_by_class_name('product-price-value')
            price = price.text
            name = d.find_element_by_class_name('product-title-text')
            name = name.text
            print(name, price)
            salvar_banco(loja, serial, name, price, data_atual)
    elif loja == 'amazon':
        for serial in gera_lista(loja):
            d.get(f'https://www.amazon.com.br//dp/{serial}/')
            price = d.find_element_by_id('price_inside_buybox')
            price = price.text
            name = d.find_element_by_id('productTitle')
            name = name.text
            print(name, price)
            salvar_banco(loja, serial, name, price, data_atual)


# python 3
import requests

''' This script gets information from Ensembl api, it was used to get genomic start-end coordinates
    and also to get dna sequence. Ensembl ID used were obtained first from Uniprot
'''

direction = '/homes/dtg30/Desktop/group_proj/venv/src/Group-project/csv_tables/kinases/'
file_csv = 'kinase_ensembl.csv'
file_gene_sequence = 'kinase_gene_information.csv'

INFILE = open(direction + file_csv, 'r')
INFILE.readline()
#uniprot_id|ensembl_gene_id|ensembl_transcript_id|ensembl_translation_id\n
OUTFILE_GENE = open(direction + file_gene_sequence, 'w')

OUTFILE_GENE.write('uniprot|ensembl_id|genome_starts|genome_ends|genome_sequence\n')

separator = '|'
ensembl_id_list  = []

for line in INFILE :
    line = line.strip().split('|')
    ensembl_id = line[1]
    if ensembl_id not in ensembl_id_list:
        ddict = {}
        ddict[line[0]] = line[1]
        ensembl_id_list.append(ddict)

for entry in ensembl_id_list:
    uniprot = [*entry][0]
    ensembl_id = entry[uniprot]
    print(ensembl_id)
    server = "https://rest.ensembl.org/sequence/id/{}".format(ensembl_id)
    fasta = requests.get(server, headers={ "Content-Type" : "text/x-fasta"})
    header = fasta.text.split('\n')[0]
    genome_starts = header.split(':')[3]
    genome_ends = header.split(':')[4]
    genome_sequence_request = requests.get(server+ext, headers={ "Content-Type" : "text/plain"})
    genome_sequence = genome_sequence_request.text.rstrip()
    text_out_list = [uniprot,ensembl_id, genome_starts, genome_ends, genome_sequence]
    text_output = '|'.join(text_out_list) + '\n'
    OUTFILE_GENE.write(text_output)

OUTFILE_GENE.close()