from PIL import Image

class preprocess:
    def __init__(self,f):
        self.filename=f

        try:
            self.image=Image.open(filename)

        except IOError as e:
            raise e

    def resize(self):
        self.image.show(title="Original")

        #Calculate aspect ratio where image.size[0] is width & image.size[1] is height
        aspect_ratio = self.image.size[0] / self.image.size[1]

        #Compute new height keeping width constant as 512
        new_width = 512
        new_height = int((new_width / aspect_ratio))

        #resize and display
        #image = resizeimage.resize_cover(image, [new_width, new_height], validate = False)
        self.image = self.image.resize((new_width,new_height), Image.ANTIALIAS)
        self.image.show(title="Resized")

    def quadrants(self):
        width=self.image.size[0]
        height=self.image.size[1]
        half_width=width//2
        half_height=height//2

        #image.crop(x1,y1,x2,y2) crops the image taking as input (x1,y1):top left co-ordinates & (x2,y2):bottom right co-ordinates

        #Quadrant 1
        self.image.crop((half_width,0,width,half_height)).show()

        #Quadrant 2
        self.image.crop((0,0,half_width,half_height)).show()

        #Quadrant 3
        self.image.crop((0,half_height,half_width,height)).show()

        #Quadrant 4
        self.image.crop((half_width,half_height,width,height)).show()

    def rgb(self):
        #image.split() returns a tuple comprising of 3 images, one for each r, g & b
        print(self.image.split())

        self.image.split()[0].show()
        self.image.split()[1].show()
        self.image.split()[2].show()
list1 = ['10_left','10_right','13_left','13_right','15_left','15_right','16_left','16_right','17_left' ,'17_right']
i = 0
while i < len(list1):
    list2 = list1[i]+'.jpeg'
    i += 1
    print(list2)
    filename = 'E:\\Project\\Diabetic Retinopathy\\check\\' + list2
    obj = preprocess(filename)
    obj.resize()
    obj.rgb()
    obj.quadrants()


import os
from ..util import get_board

def define( subparsers ):

    parser = subparsers.add_parser( 'list-options',
        help='Displays all the available menu options for the board.' )

    parser.add_argument( 'name',
        help='Board name. Use the list-boards command to list all available boards.' )

    parser.set_defaults( func = cmd )

def cmd( args ):
    """Creates the arduino board.mk makefile"""

    board_data = get_board( args.name )

    menus = board_data.get( 'menu' )
    rows = []
    if menus:
        for key, data in menus.items():
            rows += [ (
                key,
                '{}'.format( board_data.menus.getvalue( key ) ) ) ]

            for choice, choice_data in menus.get( key ).items():
                mark = '*' if choice == data.first_key() else '-'
                rows += [ (
                    '    {} {}'.format( mark, choice ),
                    '  {}'.format( choice_data.value ) ) ]

    max_len = 0
    for row in rows:
        if len( row[0] ) > max_len:
            max_len = len( row[0] )

    for row in rows:
        print( ( '{0: <%s} : {1}' % max_len ).format( *row ) )


#!/usr/bin/env python3
import sys
import urllib.request
from bs4 import BeautifulSoup

if len(sys.argv) != 4:
	print("Usage: "+sys.argv[0]+" <url> <HxW> <ext>")
else:
	url = sys.argv[1]
	res = sys.argv[2]
	ext = sys.argv[3]
	print("Fetching page...")

	user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1797.2 Safari/537.36"

	headers = {"User-Agent":user_agent}

	req = urllib.request.Request(url, None,headers, None, False, None);

	page = urllib.request.urlopen(req).read().decode('utf-8')
	print("Parsing...")
	soup = BeautifulSoup(page)
	tables = soup.select('table.listing')
	if len(tables) < 1:
		print("Cannot find the links.. :\(")
	else:
		print("Got table listing!... Getting links...")
		table = tables[0]
		a = table.find_all('a')
		if len(a) > 0:
			print("Previewing links!")
			for link in a:
				l = "http://www.kissanime.com"+str(link['href'])
				req = urllib.request.Request(l, None, headers, None, False, None)
				p = urllib.request.urlopen(req).read().decode('utf-8')
				s = BeautifulSoup(p)
				div = s.find('div', id='divDownload')
				divs = div.find_all('a')
				for z in divs:
					if res in z:
						print("aria2c -x16 -o'"+str(str(link.string)).strip()+"."+ext+"' '"+str(z['href'])+"'")
		else:
			print("Cannot find any links... :\(")


from src.utils.math import sigmoid
import math as mt
import numpy as np

class LogisticRegression:
    w  = []
    S  = []
    m  = []
    H = []
    iterations_irls = 15
    model_name = 'Regressao Logistica Bayesiana'

    def __init__(self, w0=[], S0=[], m0=[], iterations_irls=15):
        self.w = w0
        self.S = S0
        self.m = m0
        self.iterations_irls = iterations_irls

    def train(self, input, output):

        # initial values
        self.m = np.array([np.zeros(input.shape[1])])
        self.S = np.identity(input.shape[1])
        self.w = np.array([np.ones(input.shape[1])])

        # IRLS algorithm
        for i in range(self.iterations_irls):
            R = sigmoid(np.dot(self.w, input.T))
            R = R * (1 - R)
            R = np.diag(R[0])

            A = np.dot(input.T, R)
            A = np.dot(A, input) + np.linalg.inv(self.S)

            w_t = output - sigmoid(np.dot(input, self.w[0]))
            w_t = np.dot(np.linalg.inv(A), np.dot(input.T, w_t))
            w_t = w_t - np.dot(np.linalg.inv(self.S), self.w.T - self.m.T).T
            self.w = self.w + w_t

        R_hat = sigmoid(np.dot(self.w, input.T))
        R_hat = R_hat * (1 - R_hat)
        R_hat = np.diag(R_hat[0])

        self.H = np.dot(np.dot(input.T, R_hat), input) + np.linalg.inv(self.S)

    def predict(self, input):
        output = []
        for x in input:
            # Probit
            mu_a = np.dot(self.w, x)[0]
            s2   = np.dot(np.dot(x.T, np.linalg.inv(self.H)), x)
            output.append(sigmoid( mu_a * (1 + (mt.pi * s2)/8) ** 0.5))
        return np.array(output)


from datetime import datetime
from numpy import *
from collections import defaultdict
from openpyxl.utils import get_column_letter

set_printoptions(suppress=True)

data = loadtxt("../data/ratings.csv",delimiter=",",skiprows=1)

userID=[int(i) for i in data[:,0]]
movieID=[int(i) for i in data[:,1]]
ratingID=[float(i) for i in data[:,2]]
timestamp=[datetime.fromtimestamp(i).strftime('%Y-%m-%d')
           for i in data[:,3]]
relevantData=zip(movieID,ratingID)
#vse skupne ocene, št. pojavitev
allMovies=defaultdict(list)
for mID,rating in relevantData:
    if(mID not in allMovies):
        allMovies[mID].append([0.0,0])
    allMovies[mID][0][1]+=1
    allMovies[mID][0][0]+=rating
#skupni rating (allMovies[key][0][0])
#št. pojavitev (allMovies[key][0][1])

#težava ker je film ocenjen enkrat ali manjkrat, popravimo tako da vzamemo vse filme ki so bili ocenjeni več kot 15x
trueAvgRatings=defaultdict(float)
for key,value in allMovies.items():
    if (allMovies[key][0][1] != 0 and allMovies[key][0][1]>100):
        trueAvgRatings[key] = allMovies[key][0][1]
    else:
        trueAvgRatings[key] = -1
#top 100 najbolj ogledanih filmov

topTrueFilmi = sorted(trueAvgRatings.items(), key=lambda v: v[1], reverse=True)[:100]

print(topTrueFilmi)

user_data=defaultdict(int)
for i in userID:
    user_data[i]+=1

topUserRatings=sorted(user_data.items(), key=lambda v: v[1], reverse=True)[:100]

print(topUserRatings)
film_matrix=defaultdict(lambda : defaultdict(int))
for key, value in topTrueFilmi:
    for u, rat in topUserRatings:
        film_matrix[key][u]=0

print(film_matrix)
a=1

for mid, uid, rating in zip(movieID,userID,ratingID):
    for key,value in film_matrix.items():
        if mid==key and uid in value:
            film_matrix[key][uid]=rating

print(film_matrix)
from openpyxl import load_workbook
movNames=defaultdict(list)
movieNames= load_workbook(filename="../data/moviesRMK_V1.xlsx")
useNames = movieNames['movies']
useNamesID=[]
useNamesName=[]
for i in range(2,9127):
    wholeColumn=useNames['A'+str(i)].value.split(",")
    useNamesID.append(wholeColumn[0])
    useNamesName.append(wholeColumn[1])

useNamesID=[int(i) for i in useNamesID]
skupajImena=zip(useNamesID,useNamesName)

topVrstica=[]
topVrstica.append("movieID")
for key,value in film_matrix.items():
    for temp in value.items():
        topVrstica.append("u"+str(temp[0]))
    break
print(len(topVrstica))
print(topVrstica)
from openpyxl import *
wb = Workbook()
dest_filename = '.' \
                './data/testing_data_regress.xlsx'

mov_names=[]
for key,val in film_matrix.items():
    temp=zip(useNamesID,useNamesName)
    for fid,mname in temp:
        if key==fid:
            mov_names.append(mname)
            break
print(len(mov_names))
print(mov_names)
ws1 = wb.active
ws1.title = "range names"
for ind,val in enumerate(topVrstica):
    temp = get_column_letter(ind+1)
    ws1[temp+str(1)].value=val

for ind,val in enumerate(mov_names):
    ws1['A' + str(ind+2)].value = val

currentIndex=2
forKeys=1
for key,value in film_matrix.items():
    for ind,val in enumerate(value.items()):
        ind = ind + 1
        temp = get_column_letter(ind + 1)
        ws1[temp + str(currentIndex)].value=val[1]
    currentIndex += 1

wb.save(filename=dest_filename)


# coding: utf-8
# Copyright 2013 The Font Bakery Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# See AUTHORS.txt for the list of Authors and LICENSE.txt for the License.

import argparse, os
import result_suite

from .base import make_suite, run_suite

def run_set(path):
    """ Return tests results for .ttf font in parameter """
    assert os.path.exists(path)
    return run_suite(make_suite(path, 'result'))

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--ttf', default='')
    # parser.add_argument('filename', default='')
    parser.add_argument('unittest_args', nargs='*')

    args = parser.parse_args()
    # #@$%
    s = make_suite(args.ufo)
    run_suite(s)


bl_info = {
    "name": "Run Script in PyConsole",
    "author": "CoDEmanX",
    "version": (1, 0),
    "blender": (2, 67, 0),
    "location": "Python Console > Console > Run Script",
    "description": "Execute the code of a textblock within the python console.",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Development"}

import bpy

def main(self, context):
    text = bpy.data.texts.get(self.text)
    if text is not None:
        text = "exec(compile(" + repr(text) + ".as_string(), '" + text.name + "', 'exec'))"
        bpy.ops.console.clear_line()
        bpy.ops.console.insert(text=text)
        bpy.ops.console.execute()

class CONSOLE_OT_run_script(bpy.types.Operator):
    """Run a text datablock in PyConsole"""
    bl_idname = "console.run_code"
    bl_label = "Run script"

    text = bpy.props.StringProperty()

    @classmethod
    def poll(cls, context):
        return context.area.type == 'CONSOLE'

    def execute(self, context):
        main(self, context)
        return {'FINISHED'}

def get_texts(context):
    l = []
    for area in context.screen.areas:
        if area.type == 'TEXT_EDITOR':
            text = area.spaces[0].text
            if text is not None and text not in l:
                l.append(text)
    return {'visible': [t.name for t in l],
            'invisible': [t.name for t in bpy.data.texts if t not in l]}

class CONSOLE_MT_run_script(bpy.types.Menu):
    bl_label = "Run script"
    bl_idname = "CONSOLE_MT_run_script"

    def draw(self, context):
        layout = self.layout
        texts = get_texts(context)
        visible, invisible = texts['visible'], texts['invisible']

        if not (visible or invisible):
            layout.label("No text blocks!")
        else:
            if invisible:
                for t in invisible:
                    layout.operator(CONSOLE_OT_run_script.bl_idname, text=t).text = t
            if visible and invisible:
                layout.separator()
            if visible:
                for t in visible:
                    layout.operator(CONSOLE_OT_run_script.bl_idname, text=t, icon='VISIBLE_IPO_ON').text = t

def draw_item(self, context):
    layout = self.layout
    layout.menu(CONSOLE_MT_run_script.bl_idname)

def register():
    bpy.utils.register_module(__name__)
    bpy.types.CONSOLE_MT_console.prepend(draw_item)

def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.CONSOLE_MT_console.remove(draw_item)

if __name__ == "__main__":
    register()


#collecting the size of the wikipedia articles of currencies, saving them in excel and saving down the actual article in plain text

import os, requests, bs4, re, openpyxl

#BeautifulSoup gettext function gives text in a messy format, full of special characters. This function is to clean the text.
def cleanText(sourceText):

    t=str(sourceText.encode('utf-8'))

    t=t[2:-1] #remove b and '' characters
    t=re.sub(r"\\n","-", t) #remove \n (new lines) after the conversion
    t=re.sub(r"\\x..","-", t) #remove spec characters after the conversion
    t=re.sub(r"\\'","\'", t) #remove \ characters in front of 's
    t=re.sub(r"--References.*","",t) #remove text that's not part of the article
    t=re.sub(r".*From Wikipedia, the free encyclopedia","",t,count=1) #remove noise from the first part
    return t

wikiLinks={} #initialise dictionary that will store the links to currency wiki pages

wikiMain = requests.get('https://en.wikipedia.org/wiki/Currency')

mainSoup=bs4.BeautifulSoup(wikiMain.text,"html.parser")

rows=mainSoup.find_all('tr') #find all rows of the table

i=0 #initialise row counter
key=0

print("Starting now...\n")

for row in rows:
    i+=1 #increment row counter
    if i<30:
        continue #skip first table on page and header

    cells=row.find_all('td') #find all cells
    j=0 #reset cell counter

    for cell in cells:
        j+=1
        print("row no: "+str(i)+", cell no: "+str(j)+", text: "+cell.text)

        if j==1:
            key=int(cell.text) #1st cell is the transaction rank of the currency
        elif j==2:
            link='https://en.wikipedia.org'+cell.find_all('a')[0].get('href') #get link from the 2nd cell
            wikiLinks[int(key)]=link #add currency rank and link to the dictionary
        else:
            break

    if i>48:
        break

print("Links collected:\n")
print(wikiLinks)

print("Opening excel file...\n")
wb=openpyxl.load_workbook(filename="Currencies Wiki Size.xlsx")#it already exists, copied the list of cities from wikipedia - only the article sizes have to be added
ws=wb.active
print("Excel open\n")

print("Starting text collection and writing into excel...\n")
ws['e1']="Wiki size" #first unused column in the excel

for k in range(1,len(wikiLinks)+1):
    wikicurrency=requests.get(wikiLinks[k]) #get the article
    wikicurrencySoup=bs4.BeautifulSoup(wikicurrency.text,"html.parser")
    wikicurrencyText=wikicurrencySoup.get_text() #get the text from the article
    wikicurrencyTextClean=cleanText(wikicurrencyText) #remove rubbish left after using get_text
    print("Length of "+str(k)+": "+str(len(wikicurrencyTextClean)))
    ws['e'+str(k+1)]=len(wikicurrencyTextClean) #data goes to column H

    currencyName=wikiLinks[k][30:] #getting the name of the currency from the link
    f=open(str(k)+' '+currencyName+'.txt','w') #save the text of the article into a plain text file
    f.write(wikicurrencyTextClean)
    f.close()

print("Done, closing and saving.\n")
wb.save("Currencies Wiki Size.xlsx")
print("All done, bye for now.")


import json as js
import jieba
import numpy as np
import csv
from gensim.models import word2vec
jieba.set_dictionary('dict.txt.big')
model = word2vec.Word2Vec.load("word2vec.model")

query_seg=[]
word_terms_freq = {}
word_to_index = {}
query = []
dictionary_size = 8000
document_num = 100000
threshold_num = 5000
query_path = "./news_data_1/QS_1.csv"
ans_path = "ans.csv"

# standpoint ------------------------------------------------------------------------------------------
query_standpoint = []
website={'udn','ltn','chinatimes','appledaily','tvbs'}
query_standpoint.append({'udn','ltn','tvbs','chinatimes'})	#支持通姦除罪(不明顯
query_standpoint.append({'tvbs','ltn',})	#取消機車待轉(都模糊)
query_standpoint.append({'udn','tvbs','ltn'})	#支持博弈特區(不明顯)
query_standpoint.append({'appledaily','udn','tvbs'})	#支持華航罷工(tvbs，不確定)  #0.04%
query_standpoint.append({'ltn','tvbs','udn','chinatimes','appledaily'})		#支持性交易 全支持???
query_standpoint.append({'chinatimes','udn','appledaily','tvbs'})	#ecfa
query_standpoint.append({'ltn','udn','tvbs','appledaily','chinatimes'})		#證所稅廢除
query_standpoint.append({'ltn','tvbs','appledaily','udn'})	#贊成中油在觀塘興建第三天然氣接收站
query_standpoint.append({'ltn','udn','chinatimes','tvbs','appledaily'})		#陸生納保
query_standpoint.append({'ltn','appledaily','udn','tvbs','chinatimes'})		#服儀解禁
query_standpoint.append({'chinatimes','ltn','appledaily'})		#不支持使用加密貨幣(ltn態度模糊)
query_standpoint.append({'udn','chinatimes','tvbs'})	#不支持學雜費調漲(本項建議增加反對詞的權重)每個媒體都模糊
query_standpoint.append({'ltn','tvbs','appledaily','chinatimes'})		#支持舉債前瞻  0.08%
query_standpoint.append({'chinatimes','ltn','udn','tvbs','appledaily'})		#支持電競列入體育競技
query_standpoint.append({'tvbs','udn','chinatimes','appledaily','lstn'})		#返台鐵東移(ltn模糊)
query_standpoint.append({'tvbs','ltn'})		#支持陳前總統保外就醫(tvbs 模糊) udn
query_standpoint.append({'ltn','appledaily'})		#年改 取消18%
query_standpoint.append({'udn','chinatimes'})		#同意動物實驗(可刪，都不太支持)
query_standpoint.append({'ltn','udn'})		#油價應該凍漲或緩漲
query_standpoint.append({'tvbs','appledaily','ltn'})		#反對旺旺中時併購中嘉
for i in [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,16,17,18]:
    query_standpoint[i] = website
#------------------------------------------------------------------------------------------
def check(word):
    ignore = ['，', '。', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_',
              '-', '+', '=', '[', ']', ',', '.', '<', '>', '/', ';', ':',
              '\'', '\"', '{', '}', '、', '\n','\r', '\t', '\a', '\b', '\f', '\v']
    for i in ignore:
        if(word == i):
            return False
    return True
#------------------------------------------------------------------------------------------

with open('url2content.json') as f:
	texts = js.load(f)
with open('word_to_index.json') as f:
	word_to_index = js.load(f)
with open('word_terms_freq.json') as f:
	word_terms_freq = js.load(f)
with open('index2website.json') as f:
	index2website = js.load(f)
news_bag = np.load("news_bag.npy")
query_bag = np.load("query_bag.npy")
news_vec = np.load("news_vec.npy")
query_vec = np.load("query_vec.npy")
standpoint = np.load("../news_standpoint.npy")
print(word_to_index["服儀"], word_terms_freq["服儀"])

with open(file = query_path, mode = 'r') as csvfile:
    rows = csv.reader(csvfile)
    first = True
    for row in rows:
        if(first == True):
            first = False
            continue
        query.append(row[1])
    csvfile.close()

stand = model.wv["支持"]/np.linalg.norm(model.wv["支持"]) + model.wv["合法"]/np.linalg.norm(model.wv["合法"])
oppose = model.wv["反對"]/np.linalg.norm(model.wv["反對"]) + model.wv["非法"]/np.linalg.norm(model.wv["非法"])
stand = stand/np.linalg.norm(stand)
oppose = oppose/np.linalg.norm(oppose)
# calculating tf-idf -----------------------------------------------------------
tf_idf_score = []
for i in range(query_bag.shape[0]):
    temp = []
    for k in range(document_num):
        if(news_bag[k][0]!=0):        # ignore no word news and below threshold news
            #score = np.dot(query_bag[i][1:], news_bag[k][1:]) / ( np.linalg.norm(query_bag[i][1:])*np.linalg.norm(news_bag[k][1:]) )             # exclude sum of word
            score = np.dot(query_vec[i], news_vec[k]) / ( np.linalg.norm(query_vec[i])*np.linalg.norm(news_vec[k]) )
            if(i == 2):
                if( standpoint[k] >= 0.5 and np.dot(query_bag[i][1:], news_bag[k][1:])>0):
                    temp.append( (score,k+1) )
            elif(index2website[str(k)] in query_standpoint[i] and np.dot(query_bag[i][1:], news_bag[k][1:])>0):
                temp.append( (score,k+1) )
    tf_idf_score.append(temp)
    print(len(temp))

print(word_to_index["支持"], word_to_index["反對"])
#------------------------------------------------------------------------------

# creating answer
q_idx = []
for i in range(1, 10):
    q_idx.append('q_0'+str(i))
for i in range(10, 21):
    q_idx.append('q_'+str(i))

header = []
header.append("Query_Index")
for i in range(1, 10):
    header.append("Rank_00"+str(i))
for i in range(10, 100):
    header.append("Rank_0"+str(i))
for i in range(100, 301):
    header.append("Rank_"+str(i))

ans = []
ans.append(header)
for i in range(len(query)):
    rank = sorted(tf_idf_score[i], reverse=True)
    temp = []
    temp.append(q_idx[i])
    for j in range(300):
        news = rank[j][1]
        if(news<10):
            temp.append("news_00000"+str(news))
        elif(news>=10 and news<100):
            temp.append("news_0000"+str(news))
        elif(news>=100 and news<1000):
            temp.append("news_000"+str(news))
        elif(news>=1000 and news<10000):
            temp.append("news_00"+str(news))
        elif(news>=10000 and news<100000):
            temp.append("news_0"+str(news))
        else:
            temp.append("news_"+str(news))
    ans.append(temp)

peek = np.load("peek.npy", allow_pickle=True)
peek_q = [16, 17, 18, 19, 20]

for i in peek_q:
    temp = [0] * 301
    temp[0] = ans[i][0]
    peek_length = len(peek[i-16])
    isadd = {}
    for j in range(peek_length):
        temp[j+1] = peek[i-16][j]
        isadd[peek[i-16][j]] = 1
    count = peek_length
    wcount = peek_length
    while(count<300):
        n = ans[i][wcount-peek_length+1]
        if(n not in isadd):
            temp[count+1] = n
            count += 1
        wcount += 1
    ans[i] = temp

with open(ans_path, 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    for i in range(len(ans)):
        writer.writerow(ans[i])


from selenium import webdriver
from bs4 import BeautifulSoup
import time
import csv
starturl = 'https://en.wikipedia.org/wiki/List_of_brightest_stars_and_other_record_stars'
browser = webdriver.Chrome('./chromedriver.exe')
browser.get(starturl)
time.sleep(10)
def scrape():
    headers =['V_Mag',"Proper _name","Bayer_designation",'Distance','Spectral class','Mass','Radius','Luminosity']
    planet_data = []
    for i in range(0,439):
        soup = BeautifulSoup(browser.page_source,'html.parser')
        for ultag in soup.find_all('ul',attrs = {'class','exoplanet'}):
            litags = ultag.find_all('li')
            templist = []
            for index,litag in enumerate(litags):
                if index == 0:
                    templist.append(litag.find_all('a')[0].contents[0])
                else:
                    try:
                        templist.append(litag.contents[0])
                    except:
                        templist.append('')
            planet_data.append(templist)
        browser.find_element_by_xpath('//*[@id="mw-content-text"]/div[1]/table/thead/tr/th[2]').click()
    with open('scrapper3.csv','w')as f:
        csvwriter = csv.writer(f)
        csvwriter.writerow(headers)
        csvwriter.writerows(planet_data)
scrape()


from src.data.street_crosswalk_loader import StreetCrosswalkLoader

def test_load_streets(zurich_bellevue):
    bbox = zurich_bellevue
    loader = StreetCrosswalkLoader()
    streets = loader.load_data(bbox)

    assert len(streets) > 50
    assert len(loader.crosswalks) > 16
    for street in streets:
        assert len(street.nodes) == 2


import sys
import sintax_c

lang = False
source = False

for i in sys.argv:
    if 'source' in i:
        source = i.split("=")
        if len(source) > 1:
            source = source[1]
    if 'lang' in i:
        lang = i.split("=")
        if len(lang) > 1:
            lang = lang[1]

if not source:
    print("set source file")
    exit(-1)
if not lang:
    print("set programming language")
    exit(-1)

hFile = open(source)
source = hFile.read()
an = sintax_c.SyntaxClang(source)
print(an.get_number_functions())
for i in an.functions:
    print(i)
    #print(analyzer.functions[i].source)
    print(an.functions[i].variables)


'''
T: create_scoresheets_second_rounds_bool = True
F: new_creation, create_registration_file_bool, create_only_registration_file, create_only_nametags, reading_scrambling_list_from_file, reading_grouping_from_file_bool, only_one_competitor, read_only_registration_file, create_schedule, create_only_schedule, scrambler_signature, use_cubecomps_ids = False
'''
import apis
from lib.api import *
import information_analysis as analysis
import grouping_scrambling_functions as grouping_scrambling
import pdf_file_generation as pdf_files
from modules import *
from constants import EVENT_DICT
from lib.utils import *

def printConsecSheets(parser_args):
    competitors_local = ''
    competitors_api_local = []
    scoresheet_competitor_name_local = ''

    use_cubecomps_ids = True
    if parser_args.cubecomps:
        cubecomps_id = parser_args.cubecomps
    else:
        cubecomps_id = input("Link to previous round: ")
    cubecomps_api, competitors_local, event_round_name, advancing_competitors_next_round, competition_name, competition_name_stripped = apis.get_round_information_from_cubecomps(
        cubecomps_id
    )

    event_2 = event_round_name.split(" - ")[0].replace(" Cube", "")
    event_2 = list(EVENT_DICT.keys())[list(EVENT_DICT.values()).index(event_2)]

    current_round_number = (
        event_round_name.split(" - ")[1]
        .replace("Round", "")
        .replace("Combined", "Round")
        .replace("Final", "")
        .replace("First", "-r1")
        .replace("Second", "-r2")
        .replace("Semi", "-r3")
        .replace(" ", "")[-1:]
    )
    if current_round_number.isdigit():
        round_number = int(current_round_number) + 1
    else:
        print("Please open next round before using script. Script aborted.")
        return

    next_round_name = "{} -{} {}".format(
        event_round_name.split(" - ")[0].replace(" Cube", ""),
        event_round_name.split(" - ")[1]
        .replace("First", "")
        .replace("Second", "")
        .replace("Semi", "")
        .replace("Combined ", " Round"),
        str(round_number),
    )
    event_round_name = next_round_name.replace(" 4", "")

    if not parser_args.use_access_token:
        wca_password, wca_mail = apis.wca_registration(False, parser_args)

    if parser_args.wca_registration:
        wca_info = parser_args.wca_registration
    else:
        wca_info = get_confirmation(
            "Used WCA registration for this competition? (y/n) "
        )
    if wca_info:
        print("Using WCA website information.")
    if parser_args.scrambler_signature:
        scrambler_signature = parser_args.scrambler_signature
    else:
        scrambler_signature = get_confirmation(
            "Add scrambler signature field to scorecards? (y/n)"
        )
    file_name, grouping_file_name = apis.competition_information_fetch(
        wca_info, False, False, False, parser_args
    )

    if not parser_args.use_access_token:
        competition_wcif_file = apis.get_wca_info(
            competition_name, competition_name_stripped, wca_password, wca_mail
        )
    else:
        competition_wcif_file = apis.get_wca_info(
            competition_name, competition_name_stripped, access_token
        )

    ### always part ###

    # Extract data from WCIF file
    wca_json = json.loads(competition_wcif_file)

    # Registration
    competitor_information_wca = analysis.get_registrations_from_wcif(
        wca_json,
        True,
        use_cubecomps_ids,
        competitors_local,
        competitors_api_local,
        False,
        scoresheet_competitor_name_local,
    )

    # Events
    event_ids_wca, group_list, event_info, event_counter_wca, minimal_scramble_set_count, round_counter, event_list_wca = analysis.get_events_from_wcif(
        wca_json, EVENT_DICT
    )

    # Schedule
    full_schedule, competition_days, competition_start_day, timezone_utc_offset, events_per_day = analysis.get_schedule_from_wcif(
        wca_json
    )

    # Evaluate collected information
    if wca_info:
        competitor_information = competitor_information_wca

        wca_ids, registration_list_wca = analysis.prepare_registration_for_competitors(
            competitor_information, event_list_wca, len(event_list_wca)
        )

        if not registration_list_wca:
            print("")
            print(
                "ERROR!! WCA registration not used for this competition. Please select registration file for import. Script aborted."
            )
            return
        registration_list_wca = sorted(
            sorted(registration_list_wca, key=lambda x: x[1]),
            key=lambda x: x[1].split()[-1],
        )

        analysis.column_ids, event_list = grouping_scrambling.update_column_ids(
            event_list_wca, analysis.column_ids
        )
    if group_list:
        print("WCA information sucessfully imported.")
    else:
        print(
            "An error occured while importing the rounds and groups information from the WCIF file, script aborted."
        )
        print(
            'Please make sure to enter all necessary information in the "Manage events" tab on the WCA competition page.'
        )
        return
    if minimal_scramble_set_count == 1:
        continue_script = get_confirmation(
            "It looks like all your events only have one set of scrambles. Do you still want to continue running this script? (y/n)"
        )
        if not continue_script:
            print("")
            print(
                "Please edit the group information in the competition event tab  before running this script again."
            )
            print("Script aborted.")
            return
        else:
            print(
                "Continue script. Please be reminded, that there is a high possibility of not finding any scramblers!"
            )

    ### Get data from csv-export
    # same as for the WCA registration, get competitor information from registration file (if used): name, WCA ID, date of birth, gender, country and events registered for
    if not wca_info:
        print("Open registration file...")
        use_csv_registration_file = True
        if not competitors_api:
            competitors_api_local = competitors_local
        analysis.column_ids, event_list, event_counter, competitor_information, all_data, wca_ids = analysis.get_registration_from_file(
            wca_json,
            file_name,
            False,
            reading_grouping_from_file_bool,
            use_csv_registration_file,
            analysis.column_ids,
            competitor_information_wca,
            competitors_local,
            use_cubecomps_ids,
            competitors_api_local,
        )

        registration_list = sorted(
            sorted(all_data, key=lambda x: x[1]), key=lambda x: x[1].split()[-1]
        )

        if event_counter != event_counter_wca:
            print(
                "ERROR!! Number of events from WCA Website does not match number of events in registration data. Please use correct registration file. Abort script."
            )
            return

    if wca_info:
        registration_list = registration_list_wca

    ### always part over

    print("Creating scoresheets for {} ...".format(event_round_name))
    pdf_files.create_scoresheets_second_rounds(
        competition_name,
        competitor_information,
        advancing_competitors_next_round,
        event_round_name,
        event_info,
        event_2,
        next_round_name,
        scrambler_signature,
    )

    if registration_list:
        result_string = helper.initiate_result_string(registration_list)


import re

class UserPrefix(object):
    def __init__(self, nickname, user_name, host_name):
        self.nickname = nickname
        self.user_name = user_name
        self.host_name = host_name

    def dump(self):
        return '{}!{}@{}'.format(self.nickname, self.user_name, self.host_name)

    @classmethod
    def parse(cls, text):
        return cls(**re.match(r'(?P<nickname>[^!]+)!(?P<user_name>[^@]+)'
                              r'@(?P<host_name>.+)', text).groupdict())

class ServerPrefix(object):
    def __init__(self, server_name):
        self.server_name = server_name

    def dump(self):
        return self.server_name

    @classmethod
    def parse(cls, text):
        return cls(text)

class Message(object):
    def __init__(self, command, args, prefix=None):
        self.command = command
        self.args = args
        self.prefix = prefix

    @classmethod
    def parse(cls, text):
        prefix = None

        if text[0] == ':':
            raw_prefix, text = text[1:].split(' ', 1)
            if '@' not in raw_prefix:
                prefix_cls = ServerPrefix
            else:
                prefix_cls = UserPrefix
            prefix = prefix_cls.parse(raw_prefix)

        head, colon, maybe_last = text.partition(' :')

        command, *args = head.split(' ')

        if colon == ' :':
            args.append(maybe_last)

        return cls(command, args, prefix)

    def dump(self):
        parts = []

        if self.prefix is not None:
            parts.append(':')
            parts.append(self.prefix.dump())
            parts.append(' ')

        parts.append(self.command)

        for i, arg in enumerate(self.args):
            parts.append(' ')
            if i == len(self.args) - 1 and ' ' in arg or not arg:
                parts.append(':')
            parts.append(arg)

        return ''.join(parts)


from limite.tela_aluguel import TelaAluguel
from entidade.aluguel import Aluguel
from daos.dao_aluguel import AluguelDAO

class ControladorAluguel:

  def __init__(self, controlador_sistema, controlador_cliente, controlador_funcionario, controlador_carro):
    self.__controlador_sistema = controlador_sistema
    self.__controlador_cliente = controlador_cliente
    self.__controlador_funcionario = controlador_funcionario
    self.__controlador_carro = controlador_carro
    self.__dao = AluguelDAO()
    self.__tela_aluguel = TelaAluguel()

  def incluir_aluguel(self):
    dados_aluguel, test_none = self.__tela_aluguel.dados_cadastrar()

    placa = dados_aluguel["carro"]
    carro_certo, carro_verificador = self.__controlador_carro.aluguel(placa)

    nome = dados_aluguel["cliente"]
    cliente_certo, cliente_verificador = self.__controlador_cliente.aluguel(nome)

    nome = dados_aluguel["funcionario"]
    funcionario_certo, funcionario_verificador = self.__controlador_funcionario.aluguel(nome)

    identificador = dados_aluguel["data"]
    codigo_certo, codigo_verificador = self.codigo_aluguel(identificador)

    aluguel_verificador = False

    if carro_verificador == 2:
      carro_ja_alugado = True #true = carro nao alugado
      if cliente_verificador and  funcionario_verificador == True and codigo_verificador == 0:
          aluguel = Aluguel(carro_certo, cliente_certo, funcionario_certo, codigo_certo)

          self.__controlador_carro.aluga(carro_certo,aluguel, carro_ja_alugado)
          self.__controlador_cliente.novo(cliente_certo, aluguel)
          self.__controlador_funcionario.novo(funcionario_certo, aluguel)

          self.__dao.add(aluguel)
          aluguel_verificador = True
    if test_none == False:
      self.__tela_aluguel.cadastro(aluguel_verificador)
      if aluguel_verificador == False:
        self.aluguel_verificadores(cliente_verificador, funcionario_verificador, carro_verificador,codigo_verificador)

  def aluguel_verificadores(self, cliente_verificador, funcionario_verificador, carro_verificador, codigo_verificador):
    self.__tela_aluguel.aluguel_erro(cliente_verificador, funcionario_verificador, carro_verificador, codigo_verificador)

  def codigo_aluguel(self, identificador):
    codigo_verificador = 0
    codigo_certo = 0
    for codigo in self.__dao.get_all():
      if codigo.data == identificador:
        codigo_verificador = codigo_verificador + 1
    if codigo_verificador == 0:
      codigo_certo = identificador
    return codigo_certo, codigo_verificador

  def lista_alugueis(self):
    dados_aluguel = []
    for aluguel in self.__dao.get_all():
      dados_aluguel.append({"carro": aluguel.carro.placa, "cliente": aluguel.cliente.nome, "funcionario": aluguel.funcionario.nome, "data": aluguel.data})
    self.__tela_aluguel.mostra_aluguel(dados_aluguel)

  def exclui_aluguel(self):
    codigo, test_none = self.__tela_aluguel.exclui_aluguel()
    verificador = False
    if test_none == False:
      aluguel = self.__dao.get(codigo)
      if type(aluguel) == Aluguel:
        verificador = True
        self.__controlador_carro.aluga(aluguel.carro,aluguel, False)
        self.__controlador_cliente.remove(aluguel.cliente, aluguel)
        self.__controlador_funcionario.remove(aluguel.funcionario, aluguel)
        self.__dao.remove(aluguel.data)

      self.__tela_aluguel.exclui_aluguel_return(verificador)

  def retorna_tela_principal(self):
    self.__controlador_sistema.inicializa_sistema()

  def abre_tela(self):
    lista_opcoes = {1: self.incluir_aluguel, 2: self.lista_alugueis, 3:self.exclui_aluguel, 0: self.retorna_tela_principal }

    continua = True
    while continua:
      lista_opcoes[self.__tela_aluguel.tela_opcoes()]()


import math

from matplotlib.colors import hsv_to_rgb
from matplotlib.widgets import Button
from pyrealtime.input_layers import InputLayer
from pyrealtime.layer import TransformMixin, ThreadLayer
from pyrealtime.layer_manager import LayerManager
from pyrealtime.plot_layer import PlotLayer
import numpy as np

import matplotlib.pyplot as plt
from pyrealtime.utility_layers import BufferLayer, MeanLayer

def gen_dummy_data(counter):
    data = np.random.randint(100, size=(1,))
    return data

TARGET = 50
RED_H = 0
GREEN_H = 0.28

class CirclePlotter(PlotLayer):

    def draw_empty_plot(self, ax):
        h, = ax.plot([], [], '.', markersize=50)
        self.button = Button(plt.axes([0.7, 0.01, 0.1, 0.05]), 'Zero')
        self.button.on_clicked(self.clicked)
        return h,

    def clicked(self, _):
        self.raise_event("click", True)

    def init_fig(self):
        self.ax.set_ylim(0, 100)
        self.series[0].set_data(0, [])
        return self.series

    def update_fig(self, data):
        self.series[0].set_data(0, data)
        hue = self.hue_transfer_function(data)
        color = hsv_to_rgb([hue, 1, 0.75])
        self.series[0].set_color(color)
        return self.series

    @staticmethod
    def hue_transfer_function(data):
        max_error = max((1 - TARGET) ** 2, TARGET ** 2)
        error = (data - TARGET) ** 2
        return RED_H + GREEN_H * math.exp(-3 * error / max_error)

class OffsetLayer(TransformMixin, ThreadLayer):

    def __init__(self, port_in, offset=0, *args, **kwargs):
        super().__init__(port_in, *args, **kwargs)
        self.offset = offset
        self.save_next_flag = False

    def handle_signal(self, signal):
        if signal:
            self.save_next_flag = True

    def transform(self, data):
        if self.save_next_flag:
            self.save_next_flag = False
            self.offset = data
        return data - self.offset

def main():
    raw_data = InputLayer(gen_dummy_data, rate=20, name="dummy input")
    buffer = BufferLayer(raw_data, buffer_size=10)
    offset = OffsetLayer(MeanLayer(buffer), offset=25)
    plotter = CirclePlotter(offset)
    offset.set_signal_in(plotter.get_port('click'))
    LayerManager.run()

if __name__ == "__main__":
    main()


class IntCode:
    def __init__(self, file_name):
        self.intCode_list = IntCode.from_file(file_name)
        self.instruction_ptr = 0

    @classmethod
    def from_file(cls, file_name):
        with open(file_name, "r") as f:
            storage = f.read().strip().split(',')
            return [int(element) for element in storage]

    def decrypt_int_code(self):
        while self.intCode_list[self.instruction_ptr] != 99:
            current_opcode = self.intCode_list[self.instruction_ptr]
            opcode, parameter1, parameter2, parameter3 = self.get_intCode_positions(str(current_opcode))

            if opcode == 1:
                self.intCode_list[parameter3] = self.intCode_list[parameter1] + self.intCode_list[parameter2]
            elif opcode == 2:
                self.intCode_list[parameter3] = self.intCode_list[parameter1] * self.intCode_list[parameter2]
            elif opcode == 3:
                self.intCode_list[parameter3] = parameter1
                self.instruction_ptr -= 2
            elif opcode == 4:
                print(self.intCode_list[parameter1])
                self.instruction_ptr -= 2
            else:
                raise ValueError(f"Unexpected Opcode: {current_opcode} appeared")

            self.instruction_ptr += 4

    def get_intCode_positions(self, opcode: str):
        int_code_positions = [int(opcode[-1])]
        opcode = list(opcode)

        if opcode[-1] == "3":
            input_value = input("Give in the start number: ")
            int_code_positions.extend([int(input_value), None, self.intCode_list[self.instruction_ptr + 1]])
            return int_code_positions

        while len(opcode) < 4:
            opcode.insert(0, '0')

        for index, parameter_mode in enumerate(reversed(opcode[:2]), 1):
            if parameter_mode == "0":
                int_code_positions.append(self.intCode_list[self.instruction_ptr + index])
            elif parameter_mode == "1":
                int_code_positions.append(self.instruction_ptr + index)
            else:
                raise ValueError(f"Wrong parameter: {parameter_mode}")
        int_code_positions.append(self.intCode_list[self.instruction_ptr + 3])  # might be a problem if we have opcode 4
        return int_code_positions

if __name__ == "__main__":
    try:
        code = IntCode("file_with_input.txt")
        code.decrypt_int_code()
    except ValueError as e:
        print(e)


from flask import redirect, request

from flask_admin.contrib.sqla import ModelView
from flask_login import current_user

class FusionTableFlowView(ModelView):
    can_delete = False
    can_create = True
    can_edit = True
    can_export = False
    can_set_page_size = True

    def is_accessible(self):
        if current_user.is_authenticated:
            return current_user.superuser
        return False

    def inaccessible_callback(self, name, **kwargs):
        return redirect("/login?next=%s" % request.url)


'''
### BEGIN NODE INFO
[info]
name = Pulser_729
version = 1.01
description =
instancename = Pulser_729

[startup]
cmdline = %PYTHON% %FILE%
timeout = 20

[shutdown]
message = 987654321
timeout = 20
### END NODE INFO
'''
from labrad.server import LabradServer, setting
from twisted.internet.defer import Deferred, DeferredLock
from twisted.internet.threads import deferToThread
from twisted.internet import reactor
from api import api

class Pulser_729(LabradServer):

    name = 'Pulser_729'

    def initServer(self):
        self.api  = api()
        self.inCommunication = DeferredLock()
        self.initializeBoard()

    def initializeBoard(self):
        connected = self.api.connectOKBoard()
        if not connected:
            raise Exception("Pulser Not Found")

    @setting(0, 'Reset DDS', returns = '')
    def resetDDS(self , c):
        """
        Reset the ram position to 0
        """
        yield self.inCommunication.acquire()
        yield deferToThread(self.api.resetAllDDS)
        self.inCommunication.release()

    @setting(1, "Program DDS", program = '*(is)', returns = '')
    def programDDS(self, c, program):
        """
        Programs the DDS, the input is a tuple of channel numbers and buf objects for the channels
        """
        yield self.inCommunication.acquire()
        yield deferToThread(self._programDDSSequence, program)
        self.inCommunication.release()

    @setting(2, "Reinitialize DDS", returns = '')
    def reinitializeDDS(self, c):
        """
        Reprograms the DDS chip to its initial state
        """
        yield self.inCommunication.acquire()
        yield deferToThread(self.api.initializeDDS)
        self.inCommunication.release()

    def _programDDSSequence(self, program):
        '''takes the parsed dds sequence and programs the board with it'''
        for chan, buf in program:
            self.api.setDDSchannel(chan)
            self.api.programDDS(buf)
        self.api.resetAllDDS()

    def wait(self, seconds, result=None):
        """Returns a deferred that will be fired later"""
        d = Deferred()
        reactor.callLater(seconds, d.callback, result)
        return d

if __name__ == "__main__":
    from labrad import util
    util.runServer( Pulser_729() )


import numpy as np
# import matplotlib as mp
# matplotlib inline
# import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow.contrib.slim as slim
# from tensorflow.examples.tutorials.mnist import input_data
import math
import time
import pickle
import os
#import dataset

# import lasagne

start_time = time.time()

tf.reset_default_graph()

img_size_0 = 16
n_class = 1000

# using downsized imagenet from
# https://patrykchrabaszcz.github.io/Imagenet32/

def unpickle(file):
    with open(file, 'rb') as fo:
        dict = pickle.load(fo)
    return dict

# from mnist
def dense_to_one_hot(labels_dense, num_classes):
  """Convert class labels from scalars to one-hot vectors."""
  num_labels = labels_dense.shape[0]
  index_offset = np.arange(num_labels) * num_classes
  labels_one_hot = np.zeros((num_labels, num_classes))
  labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
  return labels_one_hot

def load_databatch(data_folder, img_size):
    data_file = os.path.join(data_folder, 'train_data_batch_')

    idx = 1
    d = unpickle(data_file + str(idx))
    x = d['data']
    y = d['labels']
    mean_image = d['mean']

    x = x / np.float32(255)
    mean_image = mean_image / np.float32(255)

    # Labels are indexed from 1, shift it so that indexes start at 0
    y = [i - 1 for i in y]
    data_size = x.shape[0]

    x -= mean_image

    img_size2 = img_size * img_size

    x = np.dstack((x[:, :img_size2], x[:, img_size2:2 * img_size2], x[:, 2 * img_size2:]))
    #x = x.reshape((x.shape[0], img_size, img_size, 3)).transpose(0, 3, 1, 2)
    x = x.reshape((x.shape[0], img_size, img_size, 3))

    for idx in range(9):
        d = unpickle(data_file + str(idx+2))
        x1 = d['data']
        y1 = d['labels']
        mean_image1 = d['mean']

        x1 = x1 / np.float32(255)
        mean_image1 = mean_image1 / np.float32(255)

        # Labels are indexed from 1, shift it so that indexes start at 0
        y1 = [i - 1 for i in y1]
        data_size += x1.shape[0]

        x1 -= mean_image1

        img_size2 = img_size * img_size

        x1 = np.dstack((x1[:, :img_size2], x1[:, img_size2:2 * img_size2], x1[:, 2 * img_size2:]))
        # x = x.reshape((x.shape[0], img_size, img_size, 3)).transpose(0, 3, 1, 2)
        x1 = x1.reshape(x1.shape[0], img_size, img_size, 3)

        x = np.concatenate((x, x1), axis=0)
        y = np.concatenate((y, y1), axis=0)

    # create mirrored images
    X_train = x[0:data_size, :, :, :]
    Y_train = y[0:data_size]
    X_train_flip = X_train[:, :, :, ::-1]
    Y_train_flip = Y_train
    X_train = np.concatenate((X_train, X_train_flip), axis=0)
    Y_train = np.concatenate((Y_train, Y_train_flip), axis=0)

    Y_train = dense_to_one_hot(Y_train,n_class)

    # validation data
    data_file = os.path.join(data_folder, 'val_data')

    d = unpickle(data_file)
    x = d['data']
    y = d['labels']
    # mean_image = d['mean']

    x = x / np.float32(255)
    # mean_image = mean_image / np.float32(255)

    # Labels are indexed from 1, shift it so that indexes start at 0
    y = [i - 1 for i in y]
    data_size = x.shape[0]

    # x -= mean_image

    img_size2 = img_size * img_size

    x = np.dstack((x[:, :img_size2], x[:, img_size2:2 * img_size2], x[:, 2 * img_size2:]))
    # x = x.reshape((x.shape[0], img_size, img_size, 3)).transpose(0, 3, 1, 2)
    x = x.reshape((x.shape[0], img_size, img_size, 3))

    X_val = x[0:data_size, :, :, :]
    Y_val = np.array(y[0:data_size])
    Y_val = dense_to_one_hot(Y_val,n_class)

    return dict(
        # X_train=lasagne.utils.floatX(X_train),
        X_train=X_train,
        #Y_train=Y_train.astype('int32'),
        Y_train=Y_train,
        X_val=X_val,
        Y_val=Y_val,
        mean=mean_image)

imagenet = load_databatch('../Imagenet' + str(img_size_0) + '_train/', img_size_0)

#for i in range(9):
#    imagenet1 = load_databatch('Imagenet' + str(img_size_0) + '_train/', i+2, img_size_0)
#    imagenet['X_train'] = np.concatenate((imagenet['X_train'],imagenet1['X_train']), axis=0)
#    imagenet['Y_train'] = np.concatenate((imagenet['Y_train'],imagenet1['Y_train']), axis=0)

def next_batch(num, data, labels):

    '''
    Return a total of `num` random samples and labels.
    '''
    idx = np.arange(0 , len(data))
    np.random.shuffle(idx)
    idx = idx[:num]
    data_shuffle = data[idx]
    labels_shuffle = labels[idx]
    #labels_shuffle = np.asarray(labels_shuffle.values.reshape(len(labels_shuffle), 1))

    return data_shuffle, labels_shuffle

# define the nn
x = tf.placeholder(tf.float32, [None, img_size_0, img_size_0, 3], name="x-in")
true_y = tf.placeholder(tf.float32, [None, n_class], name="y-in")
keep_prob = tf.placeholder("float")

#x_image = tf.reshape(x, [-1, 28, 28, 1])  # reshape
net = slim.conv2d(x, 64, [11, 11], 4, padding='VALID',
                        scope='conv1')
#net = slim.max_pool2d(net, [3, 3], 2, scope='pool1')
net = slim.conv2d(net, 192, [5, 5], scope='conv2')
#net = slim.max_pool2d(net, [3, 3], 2, scope='pool2')
net = slim.conv2d(net, 384, [3, 3], scope='conv3')
net = slim.conv2d(net, 384, [3, 3], scope='conv4')
net = slim.conv2d(net, 256, [3, 3], scope='conv5')
#net = slim.max_pool2d(net, [3, 3], 2, scope='pool5')

# hidden_3 = slim.dropout(hidden_3,keep_prob)    # dropout
# fc 1x
net = slim.fully_connected(slim.flatten(net), 2048, scope='fc1')
net = slim.fully_connected(slim.flatten(net), 2048, scope='fc2')
net = slim.dropout(net, keep_prob, scope='dropout')  # dropout
# out_y = slim.fully_connected(slim.flatten(h_fc1), 10, activation_fn=tf.nn.softmax,scope='fc2')
out_y = slim.fully_connected(slim.flatten(net), n_class, scope='fc3')

with tf.name_scope('loss'):
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=true_y,
                                                            logits=out_y)
cross_entropy = tf.reduce_mean(cross_entropy)

with tf.name_scope('adam_optimizer'):
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

with tf.name_scope('accuracy'):
    correct_prediction = tf.equal(tf.argmax(out_y, 1), tf.argmax(true_y, 1))
    correct_prediction = tf.cast(correct_prediction, tf.float32)
accuracy = tf.reduce_mean(correct_prediction)

batchSize = 2000
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(200001):
    batch = next_batch(batchSize, imagenet['X_train'], imagenet['Y_train'])
    sess.run(train_step, feed_dict={x: batch[0], true_y: batch[1], keep_prob: 0.5})
    # if i % 100 == 0 and i != 0:
    if i % 10 == 0:
        trainAccuracy = sess.run(accuracy, feed_dict={x: batch[0], true_y: batch[1], keep_prob: 1.0})
        print("step %d, training accuracy %g"%(i, trainAccuracy))
        print("time elapsed: {:.2f}s".format(time.time() - start_time))
        #print('%d' % (i))
        #print('%g' % (trainAccuracy))
        if i % 10 == 0:
            testAccuracy = sess.run(accuracy, feed_dict={x: imagenet['X_val'], true_y: imagenet['Y_val'], keep_prob: 1.0})
            print("test accuracy %g" % (testAccuracy))

#testAccuracy = sess.run(accuracy, feed_dict={x: mnist.test.images, true_y: mnist.test.labels, keep_prob: 1.0})
#print("test accuracy %g" % (testAccuracy))

print("time elapsed: {:.2f}s".format(time.time() - start_time))


# -*- coding: utf-8 -*-
from selenium.common.exceptions import MoveTargetOutOfBoundsException
from web_framework.browser.browser import Browser
from web_framework.waitings.custom_waitings import CustomWaitings
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains

class BaseElement:
    """
    Class for base element
    """
    def __init__(self, locator):
        """
        Constructor to initialize web driver, locator and waiting

        :param locator: locator for element
        """
        self.driver = Browser().get_driver()
        self.locator = locator
        self.wait = CustomWaitings()

    def find_element_located(self):
        """
        method to look for element by locator
        """
        return self.wait.fluent_wait().until(EC.presence_of_element_located(self.locator),
                                        message=f"Can't find element by locator {self.locator}")

    def find_elements_located(self):
        """
        method to look for elements by locator
        """
        return self.wait.fluent_wait().until(EC.presence_of_all_elements_located(self.locator),
                                        message=f"Can't find elements by locator {self.locator}")

    def hover(self):
        """
        method to hover element by locator
        """
        target = self.find_element_located()
        y_position = 0
        searching = True
        while searching:
            try:
                ActionChains(self.driver).move_to_element(target).perform()
                searching = False
            except MoveTargetOutOfBoundsException:
                y_position += 500
                self.driver.execute_script('window.scrollTo(0, ' + str(y_position) + ');')


import pytest
import math
import inspect

def list_module():
    print('begin list_module')
    assert(not inspect.isbuiltin(math))
    print("HOOK:", math.sin)
    print("HOOK:", math.sin(1))
    print('end list_module')

list_module()

@pytest.mark.usefixtures("cleandir")
def test_trace_only_no_arg(script_runner):
    ret = script_runner.run("pytracer", "trace",
                            f"--module {__file__}")
    assert ret.success

@pytest.mark.usefixtures("cleandir")
def test_trace_only(script_runner):
    ret = script_runner.run("pytracer", "trace",
                            f"--module {__file__} --test2=1")
    assert ret.success

@pytest.mark.usefixtures("turn_numpy_ufunc_off", "cleandir", "parse")
def test_trace_parse(nsamples, script_runner):
    for _ in range(nsamples):
        ret = script_runner.run("pytracer", "trace",
                                f"--module {__file__}")
        assert ret.success


# https://groups.google.com/forum/#!topic/keras-users/rRdL01zGKi4
#https://github.com/Lasagne/Recipes/blob/master/modelzoo/inception_v3.py

# https://github.com/fchollet/keras/issues/1321#issuecomment-166576492

import numpy as np

import sys,os
import yaml
import json
import h5py

from keras.optimizers import SGD, RMSprop, Adagrad, Adam
from keras.layers.core import Merge
from keras.models import Sequential, Graph
from keras.layers.normalization import BatchNormalization
from keras.layers.core import Activation, Dense, Reshape, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D, AveragePooling2D

from rnd_libs.lib.label_embedding.embedding_io import EmbeddingIO
from rnd_libs.lib.keras.loss_functions import ObjectiveFunctions
from rnd_libs.lib.keras.custom_keras_layers import power_mean_Layer,global_Average_Pooling
from rnd_libs.lib.keras.custom_utils import random_float
from IPython.core.debugger import Tracer
layer_dict = {'Convolution2D': Convolution2D,
							'ZeroPadding2D': ZeroPadding2D,
							'MaxPooling2D':MaxPooling2D,
							'BatchNormalization':BatchNormalization,
							'Activation': Activation}

pooling_dict = {'max':MaxPooling2D,'average':AveragePooling2D}
optimizer_dict = {'sgd':SGD,'rmsprop':RMSprop, 'adam':Adam}

class InceptionV3_finetune():

	def __init__(self):

		self.layer_list = []
		self.config_file = None
		self.io = EmbeddingIO(None)
		self.init = False
		self.cfgs = None
		self.loss_functions = ObjectiveFunctions()

	#def

	def configure(self,config_file):

		self.config_file = config_file
		self.init = False

		if not os.path.exists(self.config_file):
			self.io.print_error('Could not find config file for InceptionV3 {0}'.format(self.config_file))
			self.init = False
			return
		#if

		pfile = open(self.config_file,'r')
		self.cfgs = yaml.load(pfile)
		self.betas=random_float(0,20,self.cfgs['n_channels_partClassifiers'])
		pfile.close()
		#Tracer()()
		self.init = True

	#def

	def add_to_graph(self, *args, **kwargs):

		self.model.add_node(*args, **kwargs)
		self.last_added_node = kwargs['name']

		self.layer_list.append(kwargs['name'])

		return kwargs['name']

	#def

	def add_bn_conv_layer(self, *args, **kwargs):

		layer_name = kwargs['name']
		input_layer = kwargs['input']

		del kwargs['name']
		del kwargs['input']

		if 'padding' in kwargs:
			layer_name = layer_name + '_pad'
			self.add_to_graph(ZeroPadding2D(padding=kwargs['padding']), name=layer_name, input=input_layer)
			input_layer = layer_name
			del kwargs['padding']
		#if

		# CONV with linear activation by default
		layer_name = layer_name + '_conv'
		self.add_to_graph(Convolution2D(*args, border_mode='valid', **kwargs), name=layer_name, input=input_layer)

		# Batch normalization added directly on output of a linear layer
		input_layer = layer_name
		layer_name = layer_name + '_bn'
		_ = self.add_to_graph(BatchNormalization(mode=0, epsilon=0.0001, axis=1), name=layer_name, input=input_layer)

		# Standard normalization
		input_layer = layer_name
		layer_name = layer_name + '_nonlin'
		_ = self.add_to_graph(Activation('relu'), name=layer_name, input=input_layer)

		return layer_name

	#def

	def add_inceptionA(self, input_layer, list_nb_filter, base_name):

		l1_1 = self.add_bn_conv_layer(name=base_name+'_l1_1', input=input_layer, nb_filter=list_nb_filter[0][0], nb_row=1, nb_col=1)

		l2_1 = self.add_bn_conv_layer(name=base_name+'_l2_1', input=input_layer, nb_filter=list_nb_filter[1][0], nb_row=1, nb_col=1)
		l2_2 = self.add_bn_conv_layer(name=base_name+'_l2_2', input=l2_1, nb_filter=list_nb_filter[1][1], nb_row=5, nb_col=5, padding=(2,2))

		l3_1 = self.add_bn_conv_layer(name=base_name+'_l3_1', input=input_layer, nb_filter=list_nb_filter[2][0], nb_row=1, nb_col=1)
		l3_2 = self.add_bn_conv_layer(name=base_name+'_l3_2', input=l3_1, nb_filter=list_nb_filter[2][1], nb_row=3, nb_col=3, padding=(1,1))
		l3_3 = self.add_bn_conv_layer(name=base_name+'_l3_3', input=l3_2, nb_filter=list_nb_filter[2][2], nb_row=3, nb_col=3, padding=(1,1))

		l4_1 = self.add_to_graph(ZeroPadding2D(padding=(1,1)), name=base_name+'_14_1', input=input_layer)
		l4_2 = self.add_to_graph(AveragePooling2D(pool_size=(3,3), strides=(1,1)), name=base_name+'_14_2', input=l4_1)
		l4_3 = self.add_bn_conv_layer(name=base_name+'_l4_3', input=l4_2, nb_filter=list_nb_filter[3][0], nb_row=1, nb_col=1)

		self.add_to_graph(Activation("linear"), name=base_name, inputs=[l1_1, l2_2, l3_3, l4_3], merge_mode="concat", concat_axis=1)

		self.io.print_info('Added Inception-A {0}'.format(base_name))
		# https://github.com/fchollet/keras/issues/391

	def add_inceptionB(self, input_layer, list_nb_filter, base_name):

		l1_1 = self.add_bn_conv_layer(name=base_name+'_l1_1', input=input_layer, nb_filter=list_nb_filter[0][0], nb_row=3, nb_col=3, subsample=(2, 2))

		l2_1 = self.add_bn_conv_layer(name=base_name+'_l2_1', input=input_layer, nb_filter=list_nb_filter[1][0], nb_row=1, nb_col=1)
		l2_2 = self.add_bn_conv_layer(name=base_name+'_l2_2', input=l2_1, nb_filter=list_nb_filter[1][1], nb_row=3, nb_col=3, padding=(1,1))
		l2_3 = self.add_bn_conv_layer(name=base_name+'_l2_3', input=l2_2, nb_filter=list_nb_filter[1][2], nb_row=3, nb_col=3, subsample=(2,2))

		l3_1 = self.add_to_graph(MaxPooling2D(pool_size=(3,3), strides=(2,2)), name=base_name+'_13_1', input=input_layer)

		self.add_to_graph(Activation("linear"), name=base_name, inputs=[l1_1, l2_3, l3_1], merge_mode="concat", concat_axis=1)

		self.io.print_info('Added Inception-B {0}'.format(base_name))
		# https://github.com/fchollet/keras/issues/391

	def add_inceptionC(self, input_layer, list_nb_filter, base_name):

		l1_1 = self.add_bn_conv_layer(name=base_name+'_l1_1', input=input_layer, nb_filter=list_nb_filter[0][0], nb_row=1, nb_col=1)

		l2_1 = self.add_bn_conv_layer(name=base_name+'_l2_1', input=input_layer, nb_filter=list_nb_filter[1][0], nb_row=1, nb_col=1)
		l2_2 = self.add_bn_conv_layer(name=base_name+'_l2_2', input=l2_1, nb_filter=list_nb_filter[1][1], nb_row=1, nb_col=7, padding=(0,3))
		l2_3 = self.add_bn_conv_layer(name=base_name+'_l2_3', input=l2_2, nb_filter=list_nb_filter[1][2], nb_row=7, nb_col=1, padding=(3,0))
		## padding and nb_row might not match with the lasagne weights

		l3_1 = self.add_bn_conv_layer(name=base_name+'_l3_1', input=input_layer, nb_filter=list_nb_filter[2][0], nb_row=1, nb_col=1)
		l3_2 = self.add_bn_conv_layer(name=base_name+'_l3_2', input=l3_1, nb_filter=list_nb_filter[2][1], nb_row=7, nb_col=1, padding=(3,0))
		l3_3 = self.add_bn_conv_layer(name=base_name+'_l3_3', input=l3_2, nb_filter=list_nb_filter[2][2], nb_row=1, nb_col=7, padding=(0,3))
		l3_4 = self.add_bn_conv_layer(name=base_name+'_l3_4', input=l3_3, nb_filter=list_nb_filter[2][3], nb_row=7, nb_col=1, padding=(3,0))
		l3_5 = self.add_bn_conv_layer(name=base_name+'_l3_5', input=l3_4, nb_filter=list_nb_filter[2][4], nb_row=1, nb_col=7, padding=(0,3))

		l4_1 = self.add_to_graph(ZeroPadding2D(padding=(1,1)), name=base_name+'_14_1', input=input_layer)
		l4_2 = self.add_to_graph(AveragePooling2D(pool_size=(3,3), strides=(1,1)), name=base_name+'_14_2', input=l4_1)
		l4_3 = self.add_bn_conv_layer(name=base_name+'_l4_3', input=l4_2, nb_filter=list_nb_filter[3][0], nb_row=1, nb_col=1)

		self.add_to_graph(Activation("linear"), name=base_name, inputs=[l1_1, l2_3, l3_5, l4_3], merge_mode="concat", concat_axis=1)

		self.io.print_info('Added Inception-C {0}'.format(base_name))
		# https://github.com/fchollet/keras/issues/391

	def add_inceptionD(self, input_layer, list_nb_filter, base_name):

		l1_1 = self.add_bn_conv_layer(name=base_name+'_l1_1', input=input_layer, nb_filter=list_nb_filter[0][0], nb_row=1, nb_col=1)
		l1_2 = self.add_bn_conv_layer(name=base_name+'_l1_2', input=l1_1, nb_filter=list_nb_filter[0][1], nb_row=3, nb_col=3, subsample=(2,2))

		l2_1 = self.add_bn_conv_layer(name=base_name+'_l2_1', input=input_layer, nb_filter=list_nb_filter[1][0], nb_row=1, nb_col=1)
		l2_2 = self.add_bn_conv_layer(name=base_name+'_l2_2', input=l2_1, nb_filter=list_nb_filter[1][1], nb_row=1, nb_col=7, padding=(0,3))
		l2_3 = self.add_bn_conv_layer(name=base_name+'_l2_3', input=l2_2, nb_filter=list_nb_filter[1][2], nb_row=7, nb_col=1, padding=(3,0))
		l2_4 = self.add_bn_conv_layer(name=base_name+'_l2_4', input=l2_3, nb_filter=list_nb_filter[1][2], nb_row=3, nb_col=3, subsample=(2,2))

		l3_1 = self.add_to_graph(MaxPooling2D(pool_size=(3,3), strides=(2,2)), name=base_name+'_13_1', input=input_layer)

		self.add_to_graph(Activation("linear"), name=base_name, inputs=[l1_2, l2_4, l3_1], merge_mode="concat", concat_axis=1)

		self.io.print_info('Added Inception-D {0}'.format(base_name))
		# https://github.com/fchollet/keras/issues/391

	def add_inceptionE(self, input_layer, list_nb_filter, base_name, pool_mode):

		l1_1 = self.add_bn_conv_layer(name=base_name+'_l1_1', input=input_layer, nb_filter=list_nb_filter[0][0], nb_row=1, nb_col=1)

		l2_1 = self.add_bn_conv_layer(name=base_name+'_l2_1', input=input_layer, nb_filter=list_nb_filter[1][0], nb_row=1, nb_col=1)
		l2_2a = self.add_bn_conv_layer(name=base_name+'_l2_2a', input=l2_1, nb_filter=list_nb_filter[1][1], nb_row=1, nb_col=3, padding=(0,1))
		l2_2b = self.add_bn_conv_layer(name=base_name+'_l2_2b', input=l2_1, nb_filter=list_nb_filter[1][1], nb_row=3, nb_col=1, padding=(1,0))

		l3_1 = self.add_bn_conv_layer(name=base_name+'_l3_1', input=input_layer, nb_filter=list_nb_filter[2][0], nb_row=1, nb_col=1)
		l3_2 = self.add_bn_conv_layer(name=base_name+'_l3_2', input=l3_1, nb_filter=list_nb_filter[2][1], nb_row=3, nb_col=3, padding=(1,1))
		l3_3a = self.add_bn_conv_layer(name=base_name+'_l3_3a', input=l3_2, nb_filter=list_nb_filter[2][2], nb_row=1, nb_col=3, padding=(0,1))
		l3_3b = self.add_bn_conv_layer(name=base_name+'_l3_3b', input=l3_2, nb_filter=list_nb_filter[2][3], nb_row=3, nb_col=1, padding=(1,0))

		l4_1 = self.add_to_graph(ZeroPadding2D(padding=(1,1)), name=base_name+'_14_1', input=input_layer)
		l4_2 = self.add_to_graph(pooling_dict[pool_mode](pool_size=(3,3), strides=(1,1)), name=base_name+'_14_2', input=l4_1)
		l4_3 = self.add_bn_conv_layer(name=base_name+'_l4_3', input=l4_2, nb_filter=list_nb_filter[3][0], nb_row=1, nb_col=1)

		self.add_to_graph(Activation("linear"), name=base_name, inputs=[l1_1, l2_2a, l2_2b, l3_3a, l3_3b, l4_3], merge_mode="concat", concat_axis=1)

		self.io.print_info('Added Inception-E {0}'.format(base_name))
		# https://github.com/fchollet/keras/issues/391

	def define(self):

		try:

			self.model = Graph()
			#self.model.add_input(name='input',  input_shape=(self.cfgs['n_channels'], self.cfgs['image_height'], self.cfgs['image_width']))
			self.model.add_input(name='input',  input_shape=(self.cfgs['n_channels'], None, None))

			#
			# Part of the network which is defined in the config file should move here
			#

			cgfs_nodes = self.cfgs['nodes']

			for node in cgfs_nodes:

				if not node['type'] == 'Activation':
					self.add_to_graph(layer_dict[node['type']](**node['parameter']), name=node['name'] , input=node['input'])
				else:
					self.add_to_graph(layer_dict[node['type']](node['parameter']['mode']), name=node['name'] , input=node['input'])
				#if

				self.io.print_info('Added {1}:{0}'.format(node['type'],node['name']))

			#for

			self.add_inceptionA(input_layer=self.last_added_node, list_nb_filter=((64,), (48, 64), (64, 96, 96), (32,)), base_name='mixed')
			self.add_inceptionA(input_layer=self.last_added_node, list_nb_filter=((64,), (48, 64), (64, 96, 96), (64,)),  base_name='mixed_1')
			self.add_inceptionA(input_layer=self.last_added_node, list_nb_filter=((64,), (48, 64), (64, 96, 96), (64,)), base_name='mixed_2')

			self.add_inceptionB(input_layer=self.last_added_node, list_nb_filter=((384,), (64, 96, 96)), base_name='mixed_3')

			self.add_inceptionC(input_layer=self.last_added_node, list_nb_filter=((192,), (128, 128, 192), (128, 128, 128, 128, 192), (192,)), base_name='mixed_4')
			self.add_inceptionC(input_layer=self.last_added_node, list_nb_filter=((192,), (160, 160, 192), (160, 160, 160, 160, 192), (192,)), base_name='mixed_5')
			self.add_inceptionC(input_layer=self.last_added_node, list_nb_filter=((192,), (160, 160, 192), (160, 160, 160, 160, 192), (192,)), base_name='mixed_6')
			self.add_inceptionC(input_layer=self.last_added_node, list_nb_filter=((192,), (192, 192, 192), (192, 192, 192, 192, 192), (192,)), base_name='mixed_7')

			self.add_inceptionD(input_layer=self.last_added_node, list_nb_filter=((192, 320), (192, 192, 192, 192)), base_name='mixed_8')

			self.add_inceptionE(input_layer=self.last_added_node, list_nb_filter=((320,), (384, 384, 384), (448, 384, 384, 384), (192,)), pool_mode='average', base_name='mixed_9')
			self.add_inceptionE(input_layer=self.last_added_node, list_nb_filter=((320,), (384, 384, 384), (448, 384, 384, 384), (192,)), pool_mode='max', base_name='mixed_10')

			#self.add_to_graph(AveragePooling2D(pool_size=(5,5)), name='pool3', input=self.last_added_node)
			#self.io.print_info('Added {1}:{0}'.format('AveragePooling',self.last_added_node))

			"""
			self.add_to_graph(Flatten(), name='flatten', input='pool3')
			self.io.print_info('Added {1}:{0}'.format('Flatten',self.last_added_node))

			self.add_to_graph(Dense(self.cfgs['nb_classes']), name='softmax', input='flatten')
			self.io.print_info('Added {1}:{0}'.format('Dense',self.last_added_node))
			"""
			#print 'Adding finetuning layers'---------------------------------------------------------------------------------------

			self.add_to_graph(BatchNormalization(mode=0, epsilon=0.0001, axis=1),name='batchnorm',input=self.last_added_node)
			self.io.print_info('Added {1}:{0}'.format('BatchNormalization before finetuning',self.last_added_node))

			self.add_to_graph(Convolution2D(self.cfgs['n_channels_partClassifiers'],1,1),name='partClassifiers',input=self.last_added_node)
			self.io.print_info('Added {1}:{0}'.format('partClassifiers',self.last_added_node))

			self.add_to_graph(Activation('relu'),name='relu_on_partClassifiers',input=self.last_added_node)
			self.io.print_info('Added {1}:{0}'.format('ReLu on partClassifiers',self.last_added_node))

			if self.cfgs['pooling_AtfineTuning']=='avg':
				self.add_to_graph(global_Average_Pooling(), name='custom_pool', input=self.last_added_node)
				self.io.print_info('Added {1}:{0}'.format('Custom Pooling Layer:'+self.cfgs['pooling_AtfineTuning'],self.last_added_node))
			elif self.cfgs['pooling_AtfineTuning']=='softpool':
				self.add_to_graph(power_mean_Layer(self.betas), name='custom_pool', input=self.last_added_node)   # Learnable
				self.io.print_info('Added {1}:{0}'.format('Custom Pooling Layer:'+self.cfgs['pooling_AtfineTuning'],self.last_added_node))

			self.add_to_graph(Flatten(), name='flatten', input=self.last_added_node)
			self.io.print_info('Added {1}:{0}'.format('Flatten',self.last_added_node))

			self.add_to_graph(Dense(self.cfgs['nb_classes']),name='softmax',input=self.last_added_node) #Learnable
			self.io.print_info('Added {1}:{0}'.format('Softmax-dense weights',self.last_added_node))

			self.add_to_graph(Activation(self.cfgs['activation']), name='prob', input=self.last_added_node)
			self.io.print_info('Added {1}:{0}'.format('Activation',self.last_added_node))

			#Harsimrat original architecture ---------------------------------------------------------------------------------------

			# self.add_to_graph(AveragePooling2D(pool_size=(5,5)), name='pool3', input=self.last_added_node)
			# self.io.print_info('Added {1}:{0}'.format('AveragePooling',self.last_added_node))

			# """
			# self.add_to_graph(Flatten(), name='flatten', input='pool3')
			# self.io.print_info('Added {1}:{0}'.format('Flatten',self.last_added_node))
			# self.add_to_graph(Dense(self.cfgs['nb_classes']), name='softmax', input='flatten')
			# self.io.print_info('Added {1}:{0}'.format('Dense',self.last_added_node))
			# """

			# self.add_to_graph(Convolution2D(self.cfgs['nb_classes'],1,1),name='softmax',input='pool3')

			# self.add_to_graph(Activation(self.cfgs['activation']), name='prob', input='softmax')

			#Adding dense finetunning layers
#Re
			# Output to the Graph
			self.model.add_output(name='output', input='prob')
			#Tracer()()
			if not self.cfgs['model_weights_file'] == 'None':
				#import ipdb; ipdb.set_trace()
				self.init_from_this()
				#import ipdb; ipdb.set_trace()
			#if

		except Exception as err:

			self.io.print_error('Error configuring the model, {0}'.format(err))
			self.init = False
			return

		#try

		self.init = True

	#def

	def init_from_this(self):

		weights_file = self.cfgs['model_weights_file']
		#Tracer()()
		if not weights_file == 'None':
			self.load_weights(weights_file)
			self.io.print_info('Weights Initalized from {0}'.format(weights_file))
		#if

	#def

	def load_weights(self, filepath):

		#Tracer()()
		if filepath.endswith('.npz'):
			pfile = open(filepath,'r')
			graph = np.load(pfile)['graph'].item()

			for node_name,weights in graph.items():

				if node_name in self.cfgs['ignore_while_loading']:
					self.io.print_warning('Ignoring weights from {0}'.format(node_name))
					continue
				#if

				self.io.print_info('Transfering parameters from {0}'.format(node_name))
				self.model.nodes[node_name].set_weights(weights)

			#for

			pfile.close()
		elif filepath.endswith('.npy'):
			pfile = open(filepath,'r')
			graph = np.load(pfile).item()

			for node_name,weights in graph.items():

				if node_name in self.cfgs['ignore_while_loading']:
					self.io.print_warning('Ignoring weights from {0}'.format(node_name))
					continue
				#if

				self.io.print_info('Transfering parameters from {0}'.format(node_name))
				self.model.nodes[node_name].set_weights(weights)

			#for

			pfile.close()
		elif filepath.endswith('.hdf5'):

			self.model.load_weights(filepath)
		else:
			self.io.print_error('Unknown model weights file {}'.format(filepath))
		#if

		self.io.print_info(self.model.nodes['softmax'].get_config())

	#def

	def setup_loss_function(self,w):

		self.loss_functions.set_weights(w)

	#def

	def compile(self,compile_cfgs):

		try:

			#opt = optimizer_dict[compile_cfgs['optimizer']](lr=compile_cfgs['lr'], epsilon=compile_cfgs['epsilon'])
			opt = SGD(lr=compile_cfgs['lr'])
			#model_layer = dict([(ler.name, layer) for layer in self.model.layesrs])

			for layer_name,model_layer in self.model.nodes.items():
				if layer_name not in self.cfgs['trainable_layers']:
					model_layer.trainable=False
				else:
					self.io.print_info('Trainable Layer: {0}'.format(layer_name))

			self.model.compile(loss={'output':self.loss_functions.dict[compile_cfgs['loss']]}, optimizer=opt)

		except Exception as e:
			self.io.print_error('Error configuring the model, {0}'.format(e))
			self.init = False
			return
		#try

		self.init = True

	def get_model(self):
		return self.model

	#def


from classes.random import Random
from classes.genetic_management import GeneticManagement
from classes.symbolic_regression import SymbolicRegression

import numpy as np
import genetic as gp

X= np.array([[1,2,3],[2,3,-1]]).T
y = np.array([[1,0,-1]]).T
pop_size = 2000
fit_threshold = 0.1
tourn_size = 5
p_mutate = 0.08
p_crossover = 0.9
n_gen = 10

est_gp = SymbolicRegression(pop_size, tourn_size, n_gen, p_mutate, p_crossover, fit_threshold)
est_gp.fit(indep_variable = X, truth = y, batch_size = 1)
validate = est_gp.predict(X, y)
print("Fidelity on training data: %f" % np.linalg.norm(validate['prediction(s)'] - y, ord = 1)/ np.linalg.norm(y, ord = 1))


import battlecode as bc
import random
import sys
import traceback

from Controllers import *
from Entities import *

class RunMars:
    """This is how we run Mars"""
    # Initialize controllers
    # Initialize all class variables
    # Only include code that should be initialized once at the beginning of the match
    def __init__(self, gameController):
        self.game_controller = gameController
        self.map_controller = MapController(gameController)
        self.enemy_tracking_controller = EnemyTrackingController(gameController)
        self.strategy_controller = StrategyController(gameController, \
        self.map_controller, self.enemy_tracking_controller)
        self.research_tree_controller = ResearchTreeController(gameController, \
        self.strategy_controller)
        self.build_controller = BuildController(gameController, self.map_controller, \
        self.strategy_controller)
        self.pathfinding_controller = PathfindingController(gameController, self.map_controller)
        self.mission_controller = MissionController(gameController, self.strategy_controller, \
        self.map_controller, self.research_tree_controller)
        self.unit_controller = UnitController(gameController, self.strategy_controller, \
        self.pathfinding_controller, self.mission_controller, self.map_controller, self.research_tree_controller)
        self.targetting_controller = TargettingController(gameController, \
        self.map_controller, self.strategy_controller, self.unit_controller, self.enemy_tracking_controller)

        self.round = gameController.round()

    # Runs once per turn for this planet only
    def Run(self):
        """This  runs on Marce once per turn"""
        self.round = self.game_controller.round()
        if self.round == 1:
            print("First round on Earth.  Initializing map")
            self.map_controller.InitializeMarsMap()
        #print("Do mars turn things here")


from typing import Dict

import numpy as np
from scipy.sparse import csr_matrix
from tqdm import tqdm

from kbgen import MultiType
from ..model_m1 import KBModelM1
from kbgen.load_tensor_tools import load_single_adjacency_matrix, load_types_npz, load_domains, load_ranges, \
    load_type_hierarchy, load_prop_hierarchy, load_types_dict, load_relations_dict, num_adjacency_matrices
from .interfaces import LearnProcess, ResultCollector

class M1LearnProcess(LearnProcess):
    def __init__(self, dense_entity_types: np.ndarray, multitype_index, **kwargs):
        super(M1LearnProcess, self).__init__(**kwargs)
        self.dense_entity_types = dense_entity_types
        self.multitype_index = multitype_index

    def learn_distributions(self, relation_id: int):
        """
        For an in-depth explanation take a loot at the single core implementation in the M1-Model itself.
        :param relation_id: the relation id for which the features are learned
        """
        adjacency_matrix = load_single_adjacency_matrix(self.input_dir, relation_id)
        result = M1Result(relation_id=relation_id)

        # number of edges for this relation
        # used for fact count as well as relation distribution
        result.num_edges = adjacency_matrix.nnz

        domain_distribution = {}
        range_distribution = {}

        subject_ids_row = adjacency_matrix.row
        object_ids_row = adjacency_matrix.col

        # iterate over all non-zero fields of the adjacency matrix
        # iterate over all edges that exist for the current relation
        for index in range(result.num_edges):
            # get subject and object id from the adjacency matrix
            subject_id = subject_ids_row[index]
            object_id = object_ids_row[index]
            # create multi types from the two sets of entity types for each the subject and the object

            multi_type, = self.dense_entity_types[subject_id].nonzero()
            subject_multi_type = self.multitype_index[frozenset(multi_type)]

            multi_type, = self.dense_entity_types[object_id].nonzero()
            object_multi_type = self.multitype_index[frozenset(multi_type)]

            # if the subject's multi type is not known add it to the relation domain distribution and create an
            # empty relation range distribution for that multi type
            if subject_multi_type not in domain_distribution:
                domain_distribution[subject_multi_type] = 0
                range_distribution[subject_multi_type] = {}

            # if the object's multi type is not known add it to the relation range distribution of the subject's
            # multi type
            if object_multi_type not in range_distribution[subject_multi_type]:
                range_distribution[subject_multi_type][object_multi_type] = 0

                # increment the number of occurrences of the subject's multi type in the relation domain distribution
                domain_distribution[subject_multi_type] += 1
                # increment the number of occurrences of the object's multi type in the relation range distribution
                # of the subject's multi type
                range_distribution[subject_multi_type][object_multi_type] += 1

        result.domain_distribution = domain_distribution
        result.range_distribution = range_distribution

        self.result_queue.put(result)

class M1ResultCollector(ResultCollector):
    """
    For an in-depth explanation take a loot at the single core implementation in the M1-Model itself.
    """
    def __init__(self, input_dir: str, multitype_index: Dict[frozenset, int]):
        self.input_dir = input_dir
        self.multitype_index = multitype_index

        # features that will be learned before the multiprocessing
        self.entity_types = None
        self.dense_entity_types = None
        self.domains = None
        self.ranges = None
        self.entity_type_hierarchy = None
        self.object_property_hierarchy = None
        self.entity_type_to_id = None
        self.relation_to_id = None
        self.count_entities = None
        self.count_types = None
        self.count_facts = None
        self.count_relations = None
        self.entity_type_distribution = None

        self.load_data()

        # distributions and features that will be learned in parallel
        self.relation_distribution: Dict[int, int] = {}
        self.relation_domain_distribution: Dict[int, Dict[int, int]] = {}
        self.relation_range_distribution: Dict[int, Dict[int, Dict[int, int]]] = {}
        self.count_facts = 0

    def load_data(self):
        entity_types = load_types_npz(self.input_dir)
        self.dense_entity_types = entity_types.toarray()

        self.domains = load_domains(self.input_dir)
        self.ranges = load_ranges(self.input_dir)

        self.entity_type_hierarchy = load_type_hierarchy(self.input_dir)
        self.object_property_hierarchy = load_prop_hierarchy(self.input_dir)

        self.entity_type_to_id = load_types_dict(self.input_dir)
        self.relation_to_id = load_relations_dict(self.input_dir)

        print("Learning entity type distributions...")
        # the entity type adjacency matrix has the dimensions num_entities x num_entity_types
        self.count_entities = self.dense_entity_types.shape[0]
        self.count_types = self.dense_entity_types.shape[1]

        # number of different relations (object properties)
        self.count_relations = num_adjacency_matrices(self.input_dir)

        self.entity_type_distribution = {}
        for entity_type in tqdm(self.dense_entity_types):
            entity_type_set = MultiType(entity_type.nonzero()[0])

            # add the multi type to the distribution if it is new
            if entity_type_set not in self.entity_type_distribution:
                self.entity_type_distribution[entity_type_set] = 0.0

            self.entity_type_distribution[entity_type_set] += 1

    def handle_result(self, result: 'M1Result'):
        relation_id = result.relation_id
        self.count_facts += result.num_edges
        self.relation_distribution[relation_id] = result.num_edges
        self.relation_domain_distribution[relation_id] = result.domain_distribution
        self.relation_range_distribution[relation_id] = result.range_distribution

    def build_model(self) -> KBModelM1:
        model = KBModelM1(entity_type_hierarchy=self.entity_type_hierarchy,
                          object_property_hierarchy=self.object_property_hierarchy,
                          domains=self.domains,
                          ranges=self.ranges,
                          entity_count=self.count_entities,
                          relation_count=self.count_relations,
                          edge_count=self.count_facts,
                          entity_type_count=self.count_types,
                          entity_type_distribution=self.entity_type_distribution,
                          relation_distribution=self.relation_distribution,
                          relation_domain_distribution=self.relation_domain_distribution,
                          relation_range_distribution=self.relation_range_distribution,
                          relation_to_id=self.relation_to_id,
                          entity_type_to_id=self.entity_type_to_id)
        model.replace_multitype_indices(self.multitype_index)
        return model

class M1Result(object):
    def __init__(self,
                 relation_id: int = None,
                 num_edges: int = None,
                 domain_distribution: Dict[MultiType, int] = None,
                 range_distribution: Dict[MultiType, Dict[MultiType, int]] = None):
        self.relation_id = relation_id
        self.num_edges = num_edges
        self.domain_distribution = domain_distribution
        self.range_distribution = range_distribution


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# date: 2019/4/8 11:32 AM
# author: liteng

import time
from src.tools.log import Logger
from src.control.control import Controller
from tests.sidechain_eth.case import case_recharge
from tests.sidechain_eth.case import test_deploy_contract
from tests.sidechain_eth.case import case_withdraw
from tests.sidechain_eth.case import test_oracle_api_get_blockcount
from tests.sidechain_eth.case import test_oracle_api_sendrechargetx
from tests.sidechain_eth.case import test_money_lean
from tests.sidechain_eth.case import test_oracle_api_get_withdraw_by_height
from tests.sidechain_eth.case import test_oracle_api_get_withdraw_tx_info
from tests.sidechain_eth.case import test_oracle_api_get_exist_txs

config = {
    "ela": {
        "number": 6,
        "crc_number": 2,
        "pre_connect_offset": 2,
        "crc_dpos_height": 300,
        "public_dpos_height": 304,
        "min_cross_fee": 1000
    },
    "side": True,
    "arbiter": {
        "enable": True,
        "number": 2,
        "pow_chain": True,
        "print_level": 0
    },
    "geth": {
        "enable": True,
        "number": 1#只有1生效
    },

    "times": 1
}

def test_content():
    controller = Controller(config)
    controller.ready_for_dpos()

    h1 = controller.params.ela_params.crc_dpos_height
    h2 = controller.params.ela_params.public_dpos_height
    pre_offset = config["ela"]["pre_connect_offset"]
    eth_enable = config["geth"]["enable"]

    global test_case
    current_height = controller.get_current_height()

    if current_height < h1 - 1:
        controller.discrete_mining_blocks(h1 - 1 - current_height)

    height_times = dict()
    height_times[current_height] = 1

    global result
    global before_h1
    before_h1 = True
    recharge_txs = []
    while True:
        current_height = controller.get_current_height()
        times = controller.get_height_times(height_times, current_height)
        Logger.info("current height: {}, times: {}".format(current_height, times))

        if times >= 100:
            result = False
            break
        #
        if eth_enable and current_height > h2 + 1:
            test_case = "cross chain recharge did after H2"
            Logger.info("### Testing {} ###".format(test_case))
            # case_recharge.rechange_by_address(controller.tx_manager, "0x040e18b1bbb5dead8120fec5885fdf56498bcd6a")
            # deployx contract
            Logger.info("Start deploy contract test!")
            test_deploy_contract.run_cases()
            Logger.info("End deploy contract test!")

            #test_money_lean
            # txids = test_money_lean.run_cases(controller.tx_manager)
            # if len(txids) > 0:
            #     recharge_txs.extend(txids)
            # break

            Logger.info("Start recharge test!")
            #test recharge
            for i in range(1):
                txid = case_recharge.run_cases(controller.tx_manager)
                if txid:
                    recharge_txs.append(txid)
            Logger.info("End recharge test!")

            #test withdraw
            Logger.info("Start withdraw test!")
            for i in range(1):
                acc = case_recharge.get_rand_account()
                Logger.info("Create rand account:{}, recharge to this account, use the account do withdrawing".format(acc.address))
                res, txid = case_recharge.run_case_expect_succ(controller.tx_manager, acc)
                if res:
                    controller.discrete_mining_blocks(4)
                    time.sleep(3)
                    case_withdraw.run_cases(acc, 4, 5)
                    recharge_txs.append(txid)
                else:
                    Logger.error("Case withdraw failed at recharge prepare!")
            Logger.info("End withdraw test!")
            controller.discrete_mining_blocks(1)

            time.sleep(3)
            controller.discrete_mining_blocks(1)
            time.sleep(3)
            break;

        controller.discrete_mining_blocks(1)
        time.sleep(1)

    # controller.check_result(test_case, result)
    Logger.info("Test orcale_api:getexistdeposittransactions")
    test_oracle_api_get_exist_txs.run_cases(recharge_txs)
    Logger.info("Test orcale_api:getwithdrawtransactionsbyheight")
    res = test_oracle_api_get_withdraw_by_height.run_cases()
    Logger.info("Test orcale_api:getwithdrawtransaction")
    withdraw_txs = res.setdefault("succ", [])
    test_oracle_api_get_withdraw_tx_info.run_cases(withdraw_txs)
    Logger.info("Test orcale_api:getblockcount")
    test_oracle_api_get_blockcount.run_cases()

    controller.terminate_all_process()

if __name__ == '__main__':
    # times = config["times"]
    # if times > 1:
    #     config["stop"] = True
    #
    # for i in range(config["times"]):
    #     Logger.warn("[main] begin testing {} times".format(i + 1))
    #     time.sleep(2)
    test_content()
    # Logger.warn("[main] end testing {} times".format(i + 1))
    # time.sleep(3)


from django import forms
from .models import Message

class ContactForm(forms.ModelForm):
    """
    Form for users to contact site owner
    """

    class Meta:
        """
        Give form information about model
        """

        # Model that form is assossiated with
        model = Message
        # Fields to be rendered
        fields = '__all__'

    # Override __init__() method
    def __init__(self, *args, **kwargs):
        """
        Add placeholders, remove auto-generated
        labels and set autofocus on first field
        """
        # Call default __init__() method
        super().__init__(*args, **kwargs)
        # Dictionary of placeholders displayed in form fields
        placeholders = {
            'full_name': 'Full Name',
            'email': 'Email Address',
            'message': 'Message (max 500 characters)',
        }

        # full_name autofocus attribute set to true, so cursor starts here
        self.fields['full_name'].widget.attrs['autofocus'] = True
        for field in self.fields:
            # Place a star next to placeholder to indicate field is required
            placeholder = f'{placeholders[field]} *'
            # Set form placeholder attributes to respective
            # values in placeholder dict above
            self.fields[field].widget.attrs['placeholder'] = placeholder
            # Add stripe-style-input CSS class to each field
            self.fields[field].widget.attrs['class'] = 'stripe-style-input'
            # Remove label from each field
            self.fields[field].label = False
            # Add 'border-black rounded-0' classes to match site styles
            for field_name, field in self.fields.items():
                field.widget.attrs['class'] = 'border-black rounded-0'


# -*- coding: UTF-8 -*-
"""
===============================================================
author：sjyttkl
email：695492835@qq.com
date：2018
introduction:
===============================================================
"""
__author__ = "sjyttkl"
import tensorflow as tf

v1 = tf.Variable(tf.constant(value=1.0,shape=[1]),name="v1")
v2 = tf.Variable(tf.constant(value=2.0,shape=[1]),name="v2")
result1 = v1 + v2

saver = tf.train.Saver()
#通过export_meta_graph函数导出Tensorflow计算图的元图，并保存为json格式
saver.export_meta_graph("model/model.ckpt.meta.json",as_text=True)

#model.ckpt问中列表的第一行描述文件的元信息，比如这个文件中存储的变量列表。
#列表下每一行报错了一个变量的片段，变量片段的信息通过SavedSlice ProtocolBuffer定义的。
#SaverSlice类型中保存了变量的名称、单曲片段的信息以及变量的取值。
#Tensorflow提供了tf.train.NewCheckpointReader类来查看model.ckpt文件中保存的变量信息
#tf.train.NewCheckpointReader可以读取checkpoint文件中保存的所有变量
reader = tf.train.NewCheckpointReader("model/model.ckpt")
#获取所有变量的列表，这个是从变量名到变量维度的字典
all_variables = reader.get_variable_to_shape_map()
for variable_name in all_variables:
    print(variable_name,all_variables[variable_name])
#获取名称为v1的变量取值
print("Value for variable v1 is ",reader.get_tensor("Variable_1"))

5.4 持久化原理及其数据格式.py