from flask import Flask
from dash import Dash, callback, html, dcc, Input, Output, callback_context
import dash_bootstrap_components as dbc
import plotly.express as px
import pandas as pd
from waitress import serve

# server = Flask(__name__)
app = Dash(
    __name__,
    # server=server,
    external_stylesheets=[dbc.themes.BOOTSTRAP],
    suppress_callback_exceptions=True,
    # serve_locally=False,
    # requests_pathname_prefix=HOME_PATH
    )

server = app.server

sliders = [
	dbc.Row([
		dbc.Col(dbc.FormText('Heptane (€/unit)', style={'color':'black'}), width=3),
		dbc.Col(dbc.FormText(id='heptane_unit', children=.4341, style={'width':'100%', 'color':'black'}), width=9),
	]),
	dbc.Row([
		dbc.Col(dbc.FormText('Gas-oil (€/unit)', style={'color':'black'}), width=3),
		dbc.Col(dbc.FormText(id='gas_unit', children=1.281, style={'width':'100%', 'color':'black'}), width=9),
	]),
	dbc.Row([
		dbc.Col(dbc.FormText('Electricity (€/unit)', style={'color':'black'}), width=3),
		dbc.Col(dbc.FormText(id='electricity_unit', children=.179, style={'width':'100%', 'color':'black'}), width=9),
	]),
	dbc.Row([
		dbc.Col(dbc.FormText('Treatments + Staff (€/unit)', style={'color':'black'}), width=3),
		dbc.Col(dcc.Input(id='treat_staff_input', value=2.8739, style={'width':'100%'}), width=2),
		dbc.Col(dcc.Slider(0, 5, .01, value=2.8739, id='treat_staff', marks=None, tooltip={"placement": "bottom", "always_visible": True}),width=7)
	]),
	dbc.Row([
		dbc.Col(dbc.FormText('Amortization (€/unit)*', style={'color':'black'}), width=3),
		dbc.Col(dcc.Input(id='amort_input', value=.2, style={'width':'100%'}), width=2),
		dbc.Col(dcc.Slider(0, 2, .01, value=.2, id='amort', marks=None, tooltip={"placement": "bottom", "always_visible": True}),width=7)
	]),
	dbc.Row(dbc.FormText('* The smaller the amortization, the longer the contract needs to go on in order to obtain ROI', style={'color':'black'})),
	dbc.Row(dbc.FormText(children='4 Years of amortization', id="years_amort", style={'color':'black', 'font-weight':'bold'})),
]

# Contenedor de la aplicacion
container = dbc.Container([
    dbc.Row([
		dbc.Col(html.Img(src='assets/logo.png', style={'width': '80px'}), width={"size": 2, "offset": 3}, style={'text-align':'right'}),
		dbc.Col(html.H1('Remoin Ingeniería'), width=7, style={'text-align':'left', 'display': 'flex', 'align-items': 'center'}),
		],justify="center"),
	dbc.Row([
		dbc.Col(dcc.Graph(id="graph"), width=7),
		dbc.Col([
			dbc.Row(html.H5('Market price:')),
			dbc.Row([
				dbc.Col(dbc.FormText('Heptane (€/L)', style={'color':'black', 'width':'100%'}), width=4),
				dbc.Col(dcc.Input(id='heptane_market', value=1.497), width=1)
			]),
			dbc.Row([
				dbc.Col(dbc.FormText('Gas-oil (€/L)', style={'color':'black', 'width':'100%'}), width=4),
				dbc.Col(dcc.Input(id='gas_market', value=1.25), width=1)
			]),
			dbc.Row([
				dbc.Col(dbc.FormText('Electricity (€/kW)', style={'color':'black', 'width':'100%'}), width=4),
				dbc.Col(dcc.Input(id='electricity_market', value=.179), width=1)
			]),
		], width=5, align="center"),
	]),
	html.Div(sliders),

	], style={'margin-top':'20px', 'margin-bottom':'40px'})

app.layout = container

# Actualiza la figura
@callback(
    Output("graph", "figure"),
	Output("years_amort", "children"),
	Input("heptane_market", "value"),
	Input("gas_market", "value"),
	Input("electricity_market", "value"),
	Input("treat_staff", "value"),
	Input("amort", "value"),

	)
def update_bar_chart(gas, heptane, electricity, treat_staff, amort):

	heptane_20 = 0.24
	gas_20 = 0.72
	stuff_20 = 2.68
	amo_20 = 0.72
	ele_20 = 0.104
	price_20 = 4.9104
	earn_20 = 0.4464

	heptane_22 = 0.4341
	gas_22 = 1.281
	stuff_22 = 2.8739
	amo_22 = 0.233
	ele_22 = 0.179
	price_22 = 5.501177
	earn_22 = 0.500107

	costeSimulado = float(gas)*1.34+float(heptane)*.29+float(electricity)*1+treat_staff+amort

	df = {
		'year': ['2020']*3 + ['2022']*3 + ['Simulator']*3,
		'category':['Price\n(paid by Bridgestone)','Cost\n(to Remoin)','Earning\n(to Remoin)']*3,
		'money': [
			# precio, coste, ganancias (1 fila*año)
			price_20, heptane_20+gas_20+amo_20+stuff_20+ele_20,earn_20,
			price_22, heptane_22+gas_22+amo_22+stuff_22+ele_22,earn_22,
			costeSimulado+costeSimulado*.1,costeSimulado,costeSimulado*.1
			# costeSimulado+costeSimulado*.091,costeSimulado,costeSimulado*.091
		]
	}
	df = pd.DataFrame(df)

	fig = px.bar(df, x="year", y="money",
				color="category", barmode="group", text_auto=True)
	fig.update_traces(textfont_size=12, texttemplate='%{y:.3f}', textangle=0, textposition="outside", cliponaxis=False)
	fig.update_layout(
		yaxis_title="€ per unit",
		legend_title_text='',
		# title_pad_t=0,
		title_text='Evolution of costs',
		title_font_color="black",
		title_x=0.5,
		template='simple_white'
		)
	fig.update_layout(legend=dict(
		orientation="h",
		yanchor="bottom",
		y=1.02,
		xanchor="right",
		x=1
	))

	fig.update_layout(
		margin=dict(l=0, r=40, t=120, b=70),
	)

	return fig, f'{round(0.72*7/amort, 2)} Years of amortization'

# Actualiza las unidades por rueda en funcion del precio mercado
@callback(
    Output("heptane_unit", "children"),
	Input("heptane_market", "value"))
def update_heptane_units(heptane_market):
	return round(float(heptane_market)*.29, 4)

# Actualiza las unidades por rueda en funcion del precio mercado
@callback(
    Output("gas_unit", "children"),
	Input("gas_market", "value"))
def update_gas_units(gas_market):
	return round(float(gas_market)*1.34, 4)

# Actualiza las unidades por rueda en funcion del precio mercado
@callback(
    Output("electricity_unit", "children"),
	Input("electricity_market", "value"))
def update_electricity_units(electricity_market):
	return round(float(electricity_market)*1, 4)

# Actualiza los sliders y los input
@callback(
    Output("heptane", "value"),
	Output("heptane_input", "value"),
	Input("heptane", "value"),
	Input("heptane_input", "value"))
def update_inputs(slider_value, input_value):
	ctx = callback_context
	trigger_id = ctx.triggered[0]["prop_id"].split(".")[0]
	value = input_value if trigger_id == "heptane_input" else slider_value
	return float(value), float(value)

# Actualiza los sliders y los input
@callback(
    Output("treat_staff", "value"),
	Output("treat_staff_input", "value"),
	Input("treat_staff", "value"),
	Input("treat_staff_input", "value"))
def update_inputs(slider_value, input_value):
	ctx = callback_context
	trigger_id = ctx.triggered[0]["prop_id"].split(".")[0]
	value = input_value if trigger_id == "treat_staff_input" else slider_value
	return float(value), float(value)

# Actualiza los sliders y los input
@callback(
    Output("amort", "value"),
	Output("amort_input", "value"),
	Input("amort", "value"),
	Input("amort_input", "value"))
def update_inputs(slider_value, input_value):
	ctx = callback_context
	trigger_id = ctx.triggered[0]["prop_id"].split(".")[0]
	value = input_value if trigger_id == "amort_input" else slider_value
	return float(value), float(value)

if __name__ == '__main__':
    app.run_server(debug=False)
	# serve(app.server, host='0.0.0.0', port=8050)


from pyspark.sql import SparkSession
from pyspark.ml.feature import Tokenizer

bucket = 'deb-gcp-bucket-pius'
#staging_bucket = 'useranalytics-pipeline-bucket-staging'
key = 'Bronze/movie_review.csv'
local_key = '../data/movie_review.csv'

# Create a Spark session
spark = SparkSession.builder.appName("ProcessMovieReview").getOrCreate()

#datax = spark.read.csv(f'gs://{bucket}/{key}', inferSchema=True, header=True, sep=',')
datax = spark.read.csv(local_key, inferSchema=True, header=True, sep=',')

# Create a DataFrame
df = spark.createDataFrame(datax, ["cid", "review_str"])

# Instantiate the Tokenizer
tokenizer = Tokenizer(inputCol="review_str", outputCol="review_token")

# Tokenize the 'review_str' column and add the result to a new column 'review_token'
tokenized_df = tokenizer.transform(df)

# Show the result
tokenized_df.select("cid", "review_token").show(truncate=False)

# Stop the Spark session
spark.stop()


from flask import Flask
from flask import render_template
import Panel
import State
from pygooglechart import Chart
from pygooglechart import SimpleLineChart
from pygooglechart import Axis
import os

app = Flask(__name__)
panel1 = Panel.panel_production('Jim', 2, 2, 4, 1)
panel2 = Panel.panel_production('Jim2', 2, 2, 4, 2)
panels = [panel1, panel2]
myPanel = "jim"
CA = State.stateReg('California',15,5)

#initialize sample vars
panel1.setOCV(1)
panel1.setSCI(1)

def makeChart():
    # Set the vertical range from 0 to 100
    max_y = 100

    # Chart size of 200x125 pixels and specifying the range for the Y axis
    chart = SimpleLineChart(400, 250, y_range=[0, max_y])

    # Add the chart data
    # Aggregate data into array before adding to chart
    data = [
        32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37,
        37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62, 62, 60, 55,
        55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32
    ]
    #for i in data:
    #    chart.add_data(i)
    chart.add_data(data)

    # Set the line colour to blue
    chart.set_colours(['0000FF'])

    # Set the vertical stripes
    chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2)

    # Set the horizontal dotted lines
    chart.set_grid(0, 25, 5, 5)

    # The Y axis labels contains 0 to 100 skipping every 25, but remove the
    # first number because it's obvious and gets in the way of the first X
    # label.
    left_axis = range(0, max_y + 1, 25)
    left_axis[0] = ''
    chart.set_axis_labels(Axis.LEFT, left_axis)

    # X axis labels
    chart.set_axis_labels(Axis.BOTTOM, \
        ['9 am', '6 pm', '12 am'])

    return chart

@app.route('/')
#@app.route('/hello/<name>')
def hello_world(name='test'):
    panel_OCV = panel1.getOCV()
    panel_SCI = panel1.getSCI()
    panelWatt = panel_OCV*panel_SCI
    elecSell = CA.geteCost()
    tonsCoal = 15
    chart = makeChart()
    display = chart.get_url()
    return render_template('template.html', name=name, power=panelWatt,
                           sellAt=elecSell, tonsCoal = tonsCoal, chart = display,
                           panelsOwned = panels)

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)


"""Script used to test the /api/v1/system/dns endpoint."""
import e2e_test_framework

class APIE2ETestSystemDNS(e2e_test_framework.APIE2ETest):
    """Class used to test the /api/v1/system/dns endpoint."""
    uri = "/api/v1/system/dns"
    get_tests = [{"name": "Read system DNS servers"}]
    put_tests = [
        {
            "name": "Update system DNS servers",
            "payload": {
                "dnsserver": ["8.8.4.4", "1.1.1.1", "8.8.8.8"],
                "dnslocalhost": False,
                "dnsallowoverride": False
            },
            "resp_time": 10    # Allow a few seconds for DNS services to be reloaded
        },
    ]

APIE2ETestSystemDNS()


from McComplex.functions.parser_logger import l
try:
    from modeller import *
    from modeller.scripts import complete_pdb
    from modeller.optimizers import conjugate_gradients, molecular_dynamics, actions
    modeller_ok = True
except Exception:
    modeller_ok = False
    l.warn("Something is wrong with Modeller. The program will not have optimiaztion available. Check that it is installed and in your PYHONPATH.  If the problem is persistent, go to the Modeller website for more info")

from operator import itemgetter

def Optimizemodel(pdb_file):
	"""
	It creates a PDB file with the optimized model from the input pdb,
	with its energies and restraint contributions. Also it will create pdbs on
	every step of the Molecular Dynamics optimization. The energy is returned as
	the total value	of Modeller's objective function, molpdf.
	It also shows the topt 10 contributors to the molpdf before and after optimization
	"""
	# Setting up
	env = environ()
	env.io.atom_files_directory = ['../atom_files']
	env.edat.dynamic_sphere = True

	env.libs.topology.read(file='$(LIB)/top_heav.lib')
	env.libs.parameters.read(file='$(LIB)/par.lib')

	code, ext = pdb_file.split('.')

	# Complete the pdb and make a model
	mdl = complete_pdb(env, pdb_file)
	mdl.write(file=code+'.ini')

	# Select all atoms from the model, make restraints and save them in a file
	atmsel = selection(mdl)
	mdl.restraints.make(atmsel, restraint_type='stereo', spline_on_site=False)
	mdl.restraints.write(file=code+'.rsr')

	# Check the energy before optimization and save it in a var
	initial_mpdf = atmsel.energy()

	# Create optimizer objects
	cg = conjugate_gradients(output='REPORT')
	md = molecular_dynamics(output='REPORT')

	# Open a file to get basic stats on each optimization
	stats_file = open(code+'_opt.stats', 'w')

	# Run MD. Write out a PDB structure every 10 steps during the run.
	# Write stats every 10 steps
	md.optimize(atmsel, temperature=300, max_iterations=50,
            actions=[actions.write_structure(10, code+'.MD%04d.pdb'),
                     actions.trace(10, stats_file)])

	# Run CG, and write stats every 5 steps
	cg.optimize(atmsel, max_iterations=50,
            actions=[actions.trace(5, stats_file)])

	# Final energy
	final_mpdf = atmsel.energy()

	# Assess DOPE
	atmsel.assess_dope(output='ENERGY_PROFILE NO_REPORT', file='TvLDH.profile',
              normalize_profile=True, smoothing_window=15)

	# Print the energies and the contributions
	initial_cont_all = dict(initial_mpdf[1])
	top_init_conts = dict(sorted(initial_cont_all.items(), key = itemgetter(1), reverse = True)[:5])

	l.info("\n\nThe initial energy of " + code + " is " + str(initial_mpdf[0]))
	print("\n\nThe initial energy of " + code + " is " + str(initial_mpdf[0]))
	print("The top 10 initial contributions the restraints are:\n")
	for keys, values in top_init_conts.items():
		print(keys, ":", values)

	final_cont_all = dict(final_mpdf[1])
	top_final_conts = dict(sorted(final_cont_all.items(), key = itemgetter(1), reverse = True)[:5])

	l.info("\n\nThe final energy of " + code + " is " + str(final_mpdf[0]))
	print("\n\nThe final energy of " + code + " is " + str(final_mpdf[0]))
	print("Final contributions the restraints are:\n")
	for keys, values in top_final_conts.items():
		print(keys, ":", values)

	mdl.write(file=code+'_optimized.pdb')


class Solution:
    def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
        maxArea = 0
        rows = len(grid)
        cols = len(grid[0])
        visited = [[False for _ in range(cols)]for _ in range(rows)]

        def check(r, c):
            if r<0 or c<0 or r>=rows or c>=cols or visited[r][c]==True or grid[r][c]==0:
                return 0
            visited[r][c] = True
            area = 1
            area += check(r-1, c)
            area += check(r+1, c)
            area += check(r, c-1)
            area += check(r, c+1)
            return area

        for i in range(rows):
            for j in range(cols):
                if grid[i][j]==1 and visited[i][j]==False:
                    currArea = check(i, j)
                    maxArea = max(maxArea, currArea)
        return maxArea


from itertools import islice
import pandas as pd
import json
import re

import contexto
import decisao

hreg = re.compile(r'[hH][aA][rR][mM][oO]?[nN]?')
ireg = re.compile(r'[iI][nN][Aa][tT]?[iI]?[vV]?')
n4 = re.compile(r'^\d{3}\.\d{1,3}\.\d{1,3}\.\d{1,4}$')
n3 = re.compile(r'^\d{3}\.\d{1,3}\.\d{1,3}$')
n2 = re.compile(r'^\d{3}\.\d{1,3}$')
n1 = re.compile(r'^\d{3}$')

brancos = re.compile(r'\r\n|\n|\r|[ \u202F\u00A0]+$|^[ \u202F\u00A0]+')
norm_brancos = re.compile(r'(\r\n|\n|\r|[ \u202F\u00A0])+')
sepExtra = re.compile(r'#$|^#')

# Calcula e normaliza o estado da classe
def calcEstado(e):
    global hreg, ireg
    if e.strip() == '':
        return 'A'
    elif hreg.search(e):
        return 'H'
    elif ireg.search(e):
        return 'I'
    else:
        return 'Erro'
# --------------------------------------------------
#
# Calcula o nível da classe
def calcNivel(cod):
    res = 0
    if n4.fullmatch(cod):
        res = 4
    elif n3.fullmatch(cod):
        res = 3
    elif n2.fullmatch(cod):
        res = 2
    elif n1.fullmatch(cod):
        res = 1
    return res
# --------------------------------------------------
#
# Processa as notas de aplicação
from nanoid import generate

def procNotas(notas, codClasse, chave1=None, chave2=None):
    res = []
    if not chave1:
        chave1 = 'idNota'
    if not chave2:
        chave2 = 'nota'
    notas = brancos.sub('', notas)
    notas = sepExtra.sub('', notas)
    filtradas = notas.split('#')
    for na in filtradas:
        na = re.sub(r'\"', '\\\"', na)
        res.append({
            chave1: chave2 + '_' + codClasse + '_' + generate('1234567890abcdef', 12),
            chave2: na
        })
    return res
# --------------------------------------------------
#
# Calcula um array de booleanos para as N3 com subdivisão
def calcSubdivisoes(df):
    indN3 = {}
    for index, row in df.iterrows():
        if row["Código"]:
            # Código -----
            codigo = re.sub(r'(\r\n|\n|\r|[ \u202F\u00A0])','', str(row["Código"]))
            # Nível -----
            nivel = calcNivel(codigo)
            if nivel == 3:
                indN3[codigo] = False
            elif nivel == 4:
                pai = re.search(r'^(\d{3}\.\d{1,3}\.\d{1,3})\.\d{1,4}$', codigo).group(1)
                indN3[pai] = True
    return indN3

def processSheet(sheet, nome):
    # Carregam-se os catálogos
    # --------------------------------------------------
    ecatalog = open('./files/entCatalog.json')
    tcatalog = open('./files/tipCatalog.json')
    lcatalog = open('./files/legCatalog.json')
    entCatalog = json.load(ecatalog)
    tipCatalog = json.load(tcatalog)
    legCatalog = json.load(lcatalog)

    # Load one worksheet.
    # --------------------------------------------------
    fnome = nome.split("_")[0]
    print("# Migração da Classe  " + fnome + "----------------------")
    ws = sheet
    data = ws.values
    cols = next(data)[0:]
    data = list(data)
    idx = list(range(len(data)))
    data = (islice(r, 0, None) for r in data)
    df = pd.DataFrame(data, index=idx, columns=cols)

    myClasse = []
    ListaErros = []
    ProcHarmonizacao = []
    indN3 = calcSubdivisoes(df)

    for index, row in df.iterrows():
        myReg = {}
        if row["Código"]:
            # Código -----
            myReg["codigo"] = re.sub(r'(\r\n|\n|\r|[ \u202F\u00A0])','', str(row["Código"]))
            # Nível -----
            myReg["nivel"] = calcNivel(myReg["codigo"])
            # Estado -----
            if row["Estado"]:
                myReg["estado"] = calcEstado(row["Estado"])
                if myReg["estado"] == 'H':
                    ProcHarmonizacao.append( myReg["codigo"])
            else:
                myReg["estado"] = 'A'
            # Título -----
            if row["Título"]:
                myReg["titulo"] = brancos.sub('', row["Título"])
            else:
                if myReg["estado"] != 'H':
                    ListaErros.append('Erro::' + myReg['codigo'] + '::classe sem título')
            # Descrição -----
            myReg["descricao"] = norm_brancos.sub(' ', str(row["Descrição"]))
            # Notas de aplicação -----
            if row["Notas de aplicação"]:
                myReg["notasAp"] = procNotas(row["Notas de aplicação"], myReg["codigo"])
            # Exemplos de notas de aplicação -----
            if row["Exemplos de NA"]:
                myReg["exemplosNotasAp"] = procNotas(row["Exemplos de NA"], myReg["codigo"], 'idExemplo', 'exemplo')
            # Notas de exclusão -----
            if row["Notas de exclusão"]:
                myReg["notasEx"] = procNotas(row["Notas de exclusão"], myReg["codigo"])

            # Processamento do Contexto para classes de nível 3
            if myReg["nivel"] == 3:
                contexto.procContexto(row, myReg, ListaErros, entCatalog, tipCatalog, legCatalog)

            # Processamento das Decisões
            if (myReg["nivel"] == 3 and not indN3[myReg['codigo']]) or myReg["nivel"] == 4:
                decisao.procDecisoes(row, myReg, ListaErros, entCatalog, tipCatalog, legCatalog)

            myClasse.append(myReg)

    outFile = open("./files/"+fnome+".json", "w", encoding="utf-8")
    json.dump(myClasse, outFile, indent = 4, ensure_ascii=False)
    print("Classe extraída: ", nome, " :: ", len(myClasse))
    if len(ListaErros) > 0:
        print("Erros: ")
        print('\n'.join(ListaErros))
    if len(ProcHarmonizacao) > 0:
        print("Processos em Harmonização: ")
        print('\n'.join(ProcHarmonizacao))
    outFile.close()
    print("# FIM: Migração da Classe  " + fnome + "-----------------")


import pytest
import selenium.common
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

"""
|-------------------------------------------------------------------------------------------------|
Test functionality of deleting items from list

Prepare browser:
    1. Open chrome
    2. Insert URL
    3. Delete all cookies
    5. Maximize window
    6. Set page timeout to 5 seconds
    7. Find and save input to insert new items
    8. Create objects to handle keyboard and mouse operation
    9. Create 3 new items add it to list
    10. Localize this items and their destroy button
After test execution:
    1. Close browser
|-------------------------------------------------------------------------------------------------|
"""

@pytest.fixture
def openAndPrepareBrowser():
    global driver, todo, keyboard_action, mouse_action, entry1, entry2, entry3, delete_button1, delete_button2, delete_button3
    driver = webdriver.Chrome()
    driver.get("https://todomvc.com/examples/vue/")
    driver.delete_all_cookies()
    driver.maximize_window()
    driver.set_page_load_timeout(5)
    todo = driver.find_element(By.XPATH, "//input[@class='new-todo']")

    mouse_action = ActionChains(driver)
    keyboard_action = ActionChains(driver)

    todo.send_keys("test1")
    keyboard_action.send_keys(Keys.ENTER).perform()
    todo.clear()
    todo.send_keys("test2")
    keyboard_action.send_keys(Keys.ENTER).perform()
    todo.clear()
    todo.send_keys("test3")
    keyboard_action.send_keys(Keys.ENTER).perform()
    todo.clear()

    entry1 = driver.find_element(By.XPATH, "//label[text()='test1']")
    entry2 = driver.find_element(By.XPATH, "//label[text()='test2']")
    entry3 = driver.find_element(By.XPATH, "//label[text()='test3']")

    delete_button1 = driver.find_element(By.XPATH, "//label[text()='test1']/following-sibling::button")
    delete_button2 = driver.find_element(By.XPATH, "//label[text()='test2']/following-sibling::button")
    delete_button3 = driver.find_element(By.XPATH, "//label[text()='test3']/following-sibling::button")

    mouse_action = ActionChains(driver)

    yield
    driver.close()

"""
|-------------------------------------------------------------------------------------------------|
Test deleting all added items to list
Steps:
    1. Delete all items
    2. Try to check it they exist
    3. If not - test passed
|-------------------------------------------------------------------------------------------------|
"""

@pytest.mark.DeleteAll
def test_Delete_All_ToDos(openAndPrepareBrowser):
    mouse_action.move_to_element(entry1).perform()
    delete_button1.click()

    mouse_action.move_to_element(entry2).perform()
    delete_button2.click()

    mouse_action.move_to_element(entry3).perform()
    delete_button3.click()

    driver.get_screenshot_as_file("screenshots\\test_Delete_All_ToDos.png")

    try:
        entry1
    except selenium.common.exceptions.NoSuchElementException:
        assert True

    try:
        entry2
    except selenium.common.exceptions.NoSuchElementException:
        assert True

    try:
        entry3
    except selenium.common.exceptions.NoSuchElementException:
        assert True

"""
|-------------------------------------------------------------------------------------------------|
Test deleting an item located between two other items
Steps:
    1. Delete item
    2. Try to check it it exist
    3. Check if another two items still exists
|-------------------------------------------------------------------------------------------------|
"""

@pytest.mark.DeleteChoosen
def test_Delete_From_Between(openAndPrepareBrowser):
    mouse_action.move_to_element(entry2).perform()
    delete_button2.click()

    driver.get_screenshot_as_file("screenshots\\test_Delete_From_Between.png")

    try:
        entry2
    except selenium.common.exceptions.NoSuchElementException:
        assert True

    assert entry1
    assert entry3

"""
|-------------------------------------------------------------------------------------------------|
Test deleting first item
Steps:
    1. Delete first item
    2. Try to check it it exist
    3. Check if another two items exist
|-------------------------------------------------------------------------------------------------|
"""

@pytest.mark.DeleteChoosen
def test_Delete_First_One(openAndPrepareBrowser):
    mouse_action.move_to_element(entry1).perform()
    delete_button1.click()

    driver.get_screenshot_as_file("screenshots\\test_Delete_First_One.png")

    try:
        entry1
    except selenium.common.exceptions.NoSuchElementException:
        assert True

    assert entry2
    assert entry3

"""
|-------------------------------------------------------------------------------------------------|
Test deleting last item
Steps:
    1. Delete last item
    2. Try to check it it exist
    3. Check if another two items exist
|-------------------------------------------------------------------------------------------------|
"""

@pytest.mark.DeleteChoosen
def test_Delete_Last_One(openAndPrepareBrowser):
    mouse_action.move_to_element(entry3).perform()
    delete_button3.click()

    driver.get_screenshot_as_file("screenshots\\test_Delete_Last_One.png")

    try:
        entry3
    except selenium.common.exceptions.NoSuchElementException:
        assert True

    assert entry1
    assert entry2


# Author: Kimin Lee (pokaxpoka@gmail.com), Changho Hwang (ch.hwang128@gmail.com)
# GitHub: https://github.com/chhwang/cmcl
# ==============================================================================
import tensorflow as tf

def feature_sharing(features):
    """Feature sharing operation.
    Args:
      features: List of hidden features from models.
    """
    nmodel = len(features)
    with tf.variable_scope('feature_sharing'):
        shape = features[0].get_shape()
        output = [0.]*nmodel
        for from_idx in range(nmodel):
            for to_idx in range(nmodel):
                if from_idx == to_idx:
                    # don't drop hidden features within a model.
                    mask = 1.
                else:
                    # randomly drop features to share with another model.
                    mask = tf.floor(0.7 + tf.random_uniform(shape))
                output[to_idx] += mask * features[from_idx]
        return output


import torch
import torchvision.datasets
from sklearn.neighbors import KNeighborsClassifier
from torchvision import models
from utils.Dataset_config import *
from utils.Dataset import *

knn_model = None
pt_resnet50_model_cut = None

'''
rose
1) Dataset usage example and forward into resnet50 model in "get_random_samples_resnet50_forward(nBatches : int = 1)"
    a) the example above also includes combining the results of different forward iterations
2) for knn module you might need to read documentation, can be found in sklearn documentation

'''

def init_Knn_model(
        train_dataset,  # iterable, dim0 = X, dim1 = y
        n_neighbors: int
):
    global knn_model
    knn_model = KNeighborsClassifier(n_neighbors=n_neighbors)
    knn_model.fit(train_dataset[0], train_dataset[1])

def knn_predict(X):
    global knn_model
    assert knn_model is not None  # must init first
    x = X.detach()
    print(x.shape)
    return knn_model.predict(x )

def knn_validate(X_test,y_test, use_print=False):
    global knn_model
    assert knn_model is not None  # must init first
    y_pred = knn_model.predict(X_test)
    miss_classifications = y_test - y_pred
    accu = 1 - torch.count_nonzero(miss_classifications) / len(y_test)
    if use_print:
        print(f"knn miss classified {torch.count_nonzero(miss_classifications)} out of {len(y_test)}")
        print(f"Accuracy {accu} ")
    return accu

def knn_sanity_check():
    forw,y = get_random_samples_resnet50_forward(2)
    to_knn_train_fit = [forw[32:],y[32:]] # must stack , or change innnir implementation of init_Knn_model
    X_test = forw[:32]
    y_test = y[:32]

    init_Knn_model(dataset=to_knn_train_fit,n_neighbors=1)
    print("validate on same trainset, with k=0 should have 100% accuracy")
    knn_validate(to_knn_train_fit[0],to_knn_train_fit[1],use_print=True)
    print("now test set ... ")
    knn_validate(X_test,y_test,use_print=True)

def init_pre_trained_resnet50_model():
    global pt_resnet50_model_cut
    resnet50 = models.resnet50(pretrained=True)
    resnet50.eval()
    pt_resnet50_model_cut = torch.nn.Sequential(*(list(resnet50.children())[:-1]))  # strips off last linear layer

def get_random_samples_resnet50_forward(nBatches : int = 1):
    global pt_resnet50_model_cut
    if pt_resnet50_model_cut is None:
        init_pre_trained_resnet50_model()

    result_tensor = torch.zeros(size=(nBatches*BATCH_SIZE, 2048))
    tags_tensor = torch.zeros(nBatches*BATCH_SIZE)
    with torch.no_grad():
        for i, Xy in zip(range(nBatches),camelyon17_dl):
            X, y = Xy[0], Xy[1]  # X,y shape[0] == BATCH_SIZE
            result_tensor[i*BATCH_SIZE: (i+1) * BATCH_SIZE] = pt_resnet50_model_cut.forward(X).squeeze()
            tags_tensor[i*BATCH_SIZE: (i+1) * BATCH_SIZE] = y
    return result_tensor, tags_tensor


from firecore_torch.metrics import Accuracy, Average
import firecore
import typed_args as ta
from pathlib import Path

from typing import Dict, List
from torch import nn

import torch
from icecream import ic
from firecore_torch.helpers.dist_utils import init_process_group
from torch import Tensor
from firecore_torch.runners import Trainer
from firecore_torch import helpers
import logging
from torch.utils.tensorboard import SummaryWriter
from firecore_torch.helpers.arguments import Args

logger = logging.getLogger(__name__)

def get_backend(device_type: str):
    return {
        'cuda': 'nccl',
        'cpu': 'gloo',
    }[device_type]

@firecore.main_fn
def main():
    # import tracemalloc

    # tracemalloc.start()

    # firecore.logging.init(level='INFO')

    args = Args.parse_args()

    args.work_dir.mkdir(parents=True)

    firecore.logging.init(
        filename=str(args.work_dir / 'run.log'),
        level=logging.INFO,
    )

    device = args.device
    backend = get_backend(device.type)

    init_process_group(backend)

    ic(args)

    cfg = firecore.config.from_file(str(args.config), jpathdir='.')

    ic(cfg)

    base_model: nn.Module = firecore.resolve(cfg['model'])
    model = helpers.make_dist_model(base_model, device)
    ic(base_model)
    criterion: nn.Module = firecore.resolve(cfg['criterion'])
    criterion.to(device)
    ic(criterion)
    params = firecore.resolve(cfg['params'], model=base_model)
    ic(params)
    optimizer = firecore.resolve(cfg['optimizer'], params=params)
    ic(optimizer)
    lr_scheduler = firecore.resolve(cfg['lr_scheduler'], optimizer=optimizer)
    ic(lr_scheduler)

    max_epochs: int = cfg['base']['max_epochs']

    summary_writer = SummaryWriter(
        log_dir=str(args.work_dir/'tf_logs')
    )

    plans: List[Dict] = cfg['plans']

    shared = dict(
        base_model=base_model,
        model=model,
        criterion=criterion,
        optimizer=optimizer,
        lr_scheduler=lr_scheduler,
        device=device,
        max_epochs=max_epochs,
        summary_writer=summary_writer,
        work_dir=args.work_dir,
    )

    workflows: Dict[str, Trainer] = {}
    for plan in plans:
        key = plan['key']
        workflows[key] = firecore.resolve(
            cfg[key]
        )(**shared, stage=key)

    for epoch in range(1, 2 + 1):
        for plan in plans:
            if epoch % plan['interval'] == 0:
                workflow = workflows[plan['key']]
                workflow.step(epoch)


class Error (Exception): pass

import os
import re
import tarfile
import pyfastaq
import json
import subprocess
import sys
from ariba import common, card_record, vfdb_parser, megares_data_finder, megares_zip_parser

allowed_ref_dbs = {
    'argannot',
    'card',
    'megares',
    'plasmidfinder',
    'resfinder',
    'srst2_argannot',
    'vfdb_core',
    'vfdb_full',
    'virulencefinder',
    'ncbi',#added by schultzm
}

argannot_ref = '"ARG-ANNOT, a new bioinformatic tool to discover antibiotic resistance genes in bacterial genomes",\nGupta et al 2014, PMID: 24145532\n'

class RefGenesGetter:
    def __init__(self, ref_db, version=None, debug=False):
        if ref_db not in allowed_ref_dbs:
            raise Error('Error in RefGenesGetter. ref_db must be one of: ' + str(allowed_ref_dbs) + ', but I got "' + ref_db)
        self.ref_db=ref_db
        self.debug = debug
        self.genetic_code = 11
        self.max_download_attempts = 3
        self.sleep_time = 2
        self.version = version
        pyfastaq.sequences.genetic_code = self.genetic_code

    def _get_card_versions(self, tmp_file):
        print('Getting available CARD versions')
        common.download_file('https://card.mcmaster.ca/download', tmp_file, max_attempts=self.max_download_attempts, sleep_time=self.sleep_time, verbose=True)
        p = re.compile(r'''href="(/download/0/.*?broad.*?v([0-9]+\.[0-9]+\.[0-9]+)\.tar\.(gz|bz2))"''')
        versions = {}

        with open(tmp_file) as f:
            for line in f:
                got = p.findall(line)
                for match in got:
                    key = tuple([int(x) for x in match[1].split('.')])
                    versions[key] = 'https://card.mcmaster.ca' + match[0]

        if len(versions) == 0:
            raise Error('Error getting CARD versions. Cannot continue')

        print('Found versions:')

        for key, url in sorted(versions.items()):
            print('.'.join([str(x) for x in key]), url, sep='\t')

        os.unlink(tmp_file)
        return versions

    def _get_from_card(self, outprefix):
        outprefix = os.path.abspath(outprefix)
        tmpdir = outprefix + '.download'
        current_dir = os.getcwd()

        try:
            os.mkdir(tmpdir)
            os.chdir(tmpdir)
        except:
            raise Error('Error mkdir/chdir ' + tmpdir)

        versions = self._get_card_versions('download.html')
        if self.version is not None:
            key = tuple([int(x) for x in self.version.split('.')])
            if key not in versions:
                raise Error('Error! Did not find requested version ' + self.version)
        else:
            key = sorted(list(versions.keys()))[-1]
            self.version = '.'.join([str(x) for x in key])

        print('Getting version', self.version)
        card_tarball_url = versions[key]
        card_tarball = 'card.tar.bz2'
        print('Working in temporary directory', tmpdir)
        print('Downloading data from card:', card_tarball_url, flush=True)
        common.syscall('wget -O ' + card_tarball + ' ' + card_tarball_url, verbose=True)
        print('...finished downloading', flush=True)
        if not tarfile.is_tarfile(card_tarball):
            raise Error('File ' + card_tarball + ' downloaded from ' + card_tarball_url + ' does not look like a valid tar archive. Cannot continue')

        json_file = './card.json'
        with tarfile.open(card_tarball, 'r') as tfile:
            tfile.extract(json_file)

        print('Extracted json data file ', json_file,'. Reading its contents...', sep='')

        final_fasta = outprefix + '.fa'
        final_tsv = outprefix + '.tsv'
        log_file = outprefix + '.log'
        f_out_fa = pyfastaq.utils.open_file_write(final_fasta)
        f_out_tsv = pyfastaq.utils.open_file_write(final_tsv)
        f_out_log = pyfastaq.utils.open_file_write(log_file)

        with open(json_file) as f:
            json_data = json.load(f)

        json_data = {int(x): json_data[x] for x in json_data if not x.startswith('_')}
        print('Found', len(json_data), 'records in the json file. Analysing...', flush=True)

        for gene_key, gene_dict in sorted(json_data.items()):
            crecord = card_record.CardRecord(gene_dict)
            data = crecord.get_data()
            data['ARO_description'] = data['ARO_description'].encode('utf-8')
            fasta_name_prefix = '.'.join([
                card_record.CardRecord._ARO_name_to_fasta_name(data['ARO_name']),
                data['ARO_accession'],
            ])

            for card_key, gi, genbank_id, start, end, dna_seq, protein_seq in data['dna_seqs_and_ids']:
                if dna_seq == '':
                    print('Empty dna sequence', gene_key, data['ARO_id'], data['ARO_accession'], sep='\t', file=f_out_log)
                    continue

                fasta_id = '.'.join([
                    fasta_name_prefix,
                    genbank_id,
                    start + '-' + end,
                    card_key
                ])
                fasta = pyfastaq.sequences.Fasta(fasta_id, dna_seq)

                if gi != 'NA':
                    gene_tuple = fasta.make_into_gene()
                    if gene_tuple is None:
                        print('Could not make gene from sequence', fasta.id, sep='\t', file=f_out_log)
                        continue
                    else:
                        translated =  gene_tuple[0].translate()
                        if gene_tuple[0][:3] in pyfastaq.genetic_codes.starts[self.genetic_code]:
                            translated.seq = 'M' + translated.seq[1:]

                        if translated.seq[:-1] != protein_seq:
                            print('Translation of inferred gene dna sequence does not match protein sequence', fasta.id, sep='\t', file=f_out_log)
                            continue

                print(fasta, file=f_out_fa)

                if gi == 'NA':
                    gene_or_not = '0'
                    variant_only = '0'
                elif len(data['snps']) == 0:
                    gene_or_not = '1'
                    variant_only = '0'
                else:
                    gene_or_not = '1'
                    variant_only = '1'

                print(fasta.id, gene_or_not, variant_only, '.', '.', data['ARO_name'], sep='\t', file=f_out_tsv)

                if len(data['snps']) == 0 and data['ARO_description'] != '':
                    print(fasta.id, gene_or_not, variant_only, '.', '.', data['ARO_description'], sep='\t', file=f_out_tsv)
                else:
                    for snp in data['snps']:
                        if data['ARO_description'] != '':
                            print(fasta.id, gene_or_not, variant_only, snp, '.', data['ARO_description'], sep='\t', file=f_out_tsv)

        pyfastaq.utils.close(f_out_fa)
        pyfastaq.utils.close(f_out_tsv)
        pyfastaq.utils.close(f_out_log)
        os.chdir(current_dir)
        if not self.debug:
            common.rmtree(tmpdir)

        print('Extracted data and written ARIBA input files\n')
        print('Finished. Final files are:', final_fasta, final_tsv, sep='\n\t', end='\n\n')
        print('You can use them with ARIBA like this:')
        print('ariba prepareref -f', final_fasta, '-m', final_tsv, 'output_directory\n')
        print('If you use this downloaded data, please cite:')
        print('"CARD 2020: antibiotic resistome surveillance with the comprehensive antibiotic resistance database", Alcock et al 2020, PMID: 31665441')
        print('and in your methods say that version', self.version, 'of the database was used')

    @classmethod
    def _get_genetic_epi_database_from_bitbucket(cls, db_name, outdir, git_commit=None):
        assert db_name in {'plasmidfinder', 'resfinder', 'virulencefinder'}
        cmd = 'git clone ' + 'https://bitbucket.org/genomicepidemiology/' + db_name + '_db.git ' + outdir
        common.syscall(cmd)

        if git_commit is not None:
            common.syscall('cd ' + outdir + ' && git checkout ' + git_commit)

        print('Using this git commit for ' + db_name + ' database:')
        subprocess.check_call('cd ' + outdir + ' && git log -n 1', shell=True)

    def _get_from_resfinder(self, outprefix):
        outprefix = os.path.abspath(outprefix)
        final_fasta = outprefix + '.fa'
        final_tsv = outprefix + '.tsv'
        tmpdir = outprefix + '.tmp.download'
        current_dir = os.getcwd()

        if self.version =='old':
            try:
                os.mkdir(tmpdir)
                os.chdir(tmpdir)
            except:
                raise Error('Error mkdir/chdir ' + tmpdir)

            zipfile = 'resfinder.zip'
            cmd = 'curl -X POST --data "folder=resfinder&filename=resfinder.zip" -o ' + zipfile + ' https://cge.cbs.dtu.dk/cge/download_data.php'
            print('Downloading data with:', cmd, sep='\n')
            common.syscall(cmd)
            common.syscall('unzip ' + zipfile)
        else:
            RefGenesGetter._get_genetic_epi_database_from_bitbucket('resfinder', tmpdir, git_commit=self.version)
            os.chdir(tmpdir)

        print('Combining downloaded fasta files...')
        fout_fa = pyfastaq.utils.open_file_write(final_fasta)
        fout_tsv = pyfastaq.utils.open_file_write(final_tsv)
        used_names = {}

        for filename in os.listdir():
            if filename.endswith('.fsa'):
                print('   ', filename)
                fix_file = os.path.join(tmpdir, filename + '.fix.fsa')
                RefGenesGetter._fix_virulencefinder_fasta_file(os.path.join(tmpdir, filename), fix_file)
                file_reader = pyfastaq.sequences.file_reader(fix_file)
                for seq in file_reader:
                    try:
                        prefix, suffix = seq.id.split('_', maxsplit=1)
                        description = 'Original name: ' + seq.id
                        seq.id = prefix + '.' + suffix
                    except:
                        description = '.'

                    # names are not unique across the files
                    if seq.id in used_names:
                        used_names[seq.id] += 1
                        seq.id += '_' + str(used_names[seq.id])
                    else:
                        used_names[seq.id] = 1

                    print(seq, file=fout_fa)
                    print(seq.id, '1', '0', '.', '.', description, sep='\t', file=fout_tsv)

        pyfastaq.utils.close(fout_fa)
        pyfastaq.utils.close(fout_tsv)
        print('\nFinished combining files\n')
        os.chdir(current_dir)
        if not self.debug:
            common.rmtree(tmpdir)
        print('Finished. Final files are:', final_fasta, final_tsv, sep='\n\t', end='\n\n')
        print('You can use them with ARIBA like this:')
        print('ariba prepareref -f', final_fasta, '-m', final_tsv, 'output_directory\n')
        print('If you use this downloaded data, please cite:')
        print('"Identification of acquired antimicrobial resistance genes", Zankari et al 2012, PMID: 22782487\n')

    def _get_from_argannot(self, outprefix):
        outprefix = os.path.abspath(outprefix)
        tmpdir = outprefix + '.tmp.download'
        current_dir = os.getcwd()

        try:
            os.mkdir(tmpdir)
            os.chdir(tmpdir)
        except:
            raise Error('Error mkdir/chdir ' + tmpdir)

        downloadfile = 'arg-annot-database_doc'
        common.download_file('https://www.mediterranee-infection.com/wp-content/uploads/2019/09/ARG-ANNOT_NT_V6_July2019.txt', downloadfile, max_attempts=self.max_download_attempts, sleep_time=self.sleep_time, verbose=True)
        os.chdir(current_dir)
        print('Extracted files.')

        genes_file = os.path.join(tmpdir, downloadfile)
        final_fasta = outprefix + '.fa'
        final_tsv = outprefix + '.tsv'

        seq_reader = pyfastaq.sequences.file_reader(genes_file)
        f_out_tsv = pyfastaq.utils.open_file_write(final_tsv)
        f_out_fa = pyfastaq.utils.open_file_write(final_fasta)

        for seq in seq_reader:
            original_id = seq.id
            seq.id = re.sub(r'\((.*)\)', r'\1.', seq.id.split()[0])
            print(seq, file=f_out_fa)
            print(seq.id, '1', '0', '.', '.', 'Original name: ' + original_id, sep='\t', file=f_out_tsv)

        pyfastaq.utils.close(f_out_tsv)
        pyfastaq.utils.close(f_out_fa)
        if not self.debug:
            common.rmtree(tmpdir)

        print('Finished. Final files are:', final_fasta, final_tsv, sep='\n\t', end='\n\n')
        print('You can use them with ARIBA like this:')
        print('ariba prepareref -f', final_fasta, '-m', final_tsv, 'output_directory\n')
        print('If you use this downloaded data, please cite:')
        print(argannot_ref)

    def _get_from_megares(self, outprefix):
        data_finder = megares_data_finder.MegaresDataFinder(version=self.version)
        download_url = data_finder.run()
        zip_parser = megares_zip_parser.MegaresZipParser(download_url, outprefix)
        zip_parser.run()
        final_fasta = outprefix + '.fa'
        final_tsv = outprefix + '.tsv'
        print('Finished. Final files are:', final_fasta, final_tsv, sep='\n\t', end='\n\n')
        print('You can use them with ARIBA like this:')
        print('ariba prepareref -f', final_fasta, '-m', final_tsv, 'output_directory\n')
        print('If you use this downloaded data, please cite:')
        print('"MEGARes: an antimicrobial database for high throughput sequencing", Lakin et al 2016, PMID: PMC5210519\n')

    def _get_from_plasmidfinder(self, outprefix):
        outprefix = os.path.abspath(outprefix)
        final_fasta = outprefix + '.fa'
        final_tsv = outprefix + '.tsv'
        tmpdir = outprefix + '.tmp.download'
        current_dir = os.getcwd()

        if self.version == 'old':
            try:
                os.mkdir(tmpdir)
                os.chdir(tmpdir)
            except:
                raise Error('Error mkdir/chdir ' + tmpdir)

            zipfile = 'plasmidfinder.zip'
            cmd = 'curl -X POST --data "folder=plasmidfinder&filename=plasmidfinder.zip" -o ' + zipfile + ' https://cge.cbs.dtu.dk/cge/download_data.php'
            print('Downloading data with:', cmd, sep='\n')
            common.syscall(cmd)
            common.syscall('unzip ' + zipfile)
        else:
            RefGenesGetter._get_genetic_epi_database_from_bitbucket('plasmidfinder', tmpdir, git_commit=self.version)
            os.chdir(tmpdir)

        print('Combining downloaded fasta files...')
        fout_fa = pyfastaq.utils.open_file_write(final_fasta)
        fout_tsv = pyfastaq.utils.open_file_write(final_tsv)
        name_count = {}

        for filename in os.listdir(tmpdir):
            if filename.endswith('.fsa'):
                print('   ', filename)
                file_reader = pyfastaq.sequences.file_reader(os.path.join(tmpdir, filename))
                for seq in file_reader:
                    original_id = seq.id
                    seq.id = seq.id.replace('_', '.', 1)
                    if seq.id in name_count:
                        name_count[seq.id] += 1
                        seq.id = seq.id + '.' + str(name_count[seq.id])
                    else:
                        name_count[seq.id] = 1

                    print(seq, file=fout_fa)
                    print(seq.id, '0', '0', '.', '.', 'Original name was ' + original_id, sep='\t', file=fout_tsv)

        pyfastaq.utils.close(fout_fa)
        pyfastaq.utils.close(fout_tsv)
        print('\nFinished combining files\n')
        os.chdir(current_dir)
        if not self.debug:
            common.rmtree(tmpdir)
        print('Finished. Final files are:', final_fasta, final_tsv, sep='\n\t', end='\n\n')
        print('You can use them with ARIBA like this:')
        print('ariba prepareref -f', final_fasta, '-m', final_tsv, 'output_directory\n')
        print('If you use this downloaded data, please cite:')
        print('"PlasmidFinder and pMLST: in silico detection and typing of plasmids", Carattoli et al 2014, PMID: 24777092\n')

    def _get_from_srst2_argannot(self, outprefix):
        if self.version is None:
            self.version = 'r2'
        if self.version not in {'r1', 'r2'}:
            raise Error('srst2_argannot version must be r1 or r2. Got this: ' + self.version)

        version_string = '.r1' if self.version == 'r1' else '_r2'
        srst2_url = 'https://raw.githubusercontent.com/katholt/srst2/master/data/ARGannot' + version_string + '.fasta'
        srst2_fa = outprefix + '.original.fa'
        command = 'wget -O ' + srst2_fa + ' ' + srst2_url
        common.syscall(command, verbose=True)

        final_fasta = outprefix + '.fa'
        final_tsv = outprefix + '.tsv'

        f_out_fa = pyfastaq.utils.open_file_write(final_fasta)
        f_out_meta = pyfastaq.utils.open_file_write(final_tsv)
        seq_reader = pyfastaq.sequences.file_reader(srst2_fa)

        for seq in seq_reader:
            original_id = seq.id
            name, extra = seq.id.split()
            cluster_id, cluster_name, allele_name, allele_id = name.split('__')
            seq.id = cluster_name + '.' + name
            print(seq, file=f_out_fa)
            print(seq.id, 1, 0, '.', '.', 'Original name: ' + original_id, sep='\t', file=f_out_meta)

        pyfastaq.utils.close(f_out_fa)
        pyfastaq.utils.close(f_out_meta)
        if not self.debug:
            os.unlink(srst2_fa)

        print('Finished downloading and converting data. Final files are:', final_fasta, final_tsv, sep='\n\t', end='\n\n')
        print('You can use them with ARIBA like this:')
        print('ariba prepareref -f', final_fasta, '-m', final_tsv, 'output_directory\n')
        print('If you use this downloaded data, please cite:')
        print('"SRST2: Rapid genomic surveillance for public health and hospital microbiology labs",\nInouye et al 2014, Genome Medicine, PMID: 25422674\n')
        print(argannot_ref)
        # Use to also output the version of SRST2 here, but the r2 version of their
        # fasta file was made after SRST2 release 0.2.0. At the time of writing this,
        # 0.2.0 is the latest release, ie r2 isn't in an SRST2 release.

    def _get_from_vfdb_core(self, outprefix):
        self._get_from_vfdb_common(outprefix, 'VFDB_setA_nt.fas.gz','core')

    def _get_from_vfdb_full(self, outprefix):
         self._get_from_vfdb_common(outprefix, 'VFDB_setB_nt.fas.gz','full')

    def _get_from_vfdb_common(self, outprefix, filename, info_text):
        outprefix = os.path.abspath(outprefix)
        tmpdir = outprefix + '.tmp.download'

        try:
            os.mkdir(tmpdir)
        except:
            raise Error('Error mkdir ' + tmpdir)

        zipfile = os.path.join(tmpdir, filename)
        common.download_file('http://www.mgc.ac.cn/VFs/Down/' + filename, zipfile, max_attempts=self.max_download_attempts, sleep_time=self.sleep_time, verbose=True)
        print('Extracting files ... ', end='', flush=True)
        vparser = vfdb_parser.VfdbParser(zipfile, outprefix)
        vparser.run()
        if not self.debug:
            common.rmtree(tmpdir)
        print('done')
        final_fasta = outprefix + '.fa'
        final_tsv = outprefix + '.tsv'

        print('Extracted core DNA sequence dataset and metadata. Final files are:', final_fasta, final_tsv, sep='\n\t', end='\n\n')
        print('You can use them with ARIBA like this:')
        print('ariba prepareref -f', final_fasta, '-m', final_tsv, 'output_directory\n')
        print('If you use this downloaded data, please cite:')
        print('"VFDB 2016: hierarchical and refined dataset for big data analysis-10 years on",\nChen LH et al 2016, Nucleic Acids Res. 44(Database issue):D694-D697. PMID: 26578559\n')

    @classmethod
    def _fix_virulencefinder_fasta_file(cls, infile, outfile):
        '''Some line breaks are missing in the FASTA files from
        virulence finder. Which means there are lines like this:
        AAGATCCAATAACTGAAGATGTTGAACAAACAATTCATAATATTTATGGTCAATATGCTATTTTCGTTGA
        AGGTGTTGCGCATTTACCTGGACATCTCTCTCCATTATTAAAAAAATTACTACTTAAATCTTTATAA>coa:1:BA000018.3
        ATGAAAAAGCAAATAATTTCGCTAGGCGCATTAGCAGTTGCATCTAGCTTATTTACATGGGATAACAAAG
        and therefore the sequences are messed up when we parse them. Also
        one has a > at the end, then the seq name on the next line.
        This function fixes the file by adding line breaks'''
        with open(infile) as f_in, open(outfile, 'w') as f_out:
            for line in f_in:
                if line.startswith('>') or '>' not in line:
                    print(line, end='', file=f_out)
                elif line.endswith('>\n'):
                    print('WARNING: found line with ">" at the end! Fixing. Line:' + line.rstrip() + ' in file ' + infile, file=sys.stderr)
                    print(line.rstrip('>\n'), file=f_out)
                    print('>', end='', file=f_out)
                else:
                    print('WARNING: found line with ">" not at the start! Fixing. Line:' + line.rstrip() + ' in file ' + infile, file=sys.stderr)
                    line1, line2 = line.split('>')
                    print(line1, file=f_out)
                    print('>', line2, sep='', end='', file=f_out)

    def _get_from_virulencefinder(self, outprefix):
        outprefix = os.path.abspath(outprefix)
        final_fasta = outprefix + '.fa'
        final_tsv = outprefix + '.tsv'
        tmpdir = outprefix + '.tmp.download'
        current_dir = os.getcwd()

        if self.version == 'old':
            try:
                os.mkdir(tmpdir)
                os.chdir(tmpdir)
            except:
                raise Error('Error mkdir/chdir ' + tmpdir)

            zipfile = 'virulencefinder.zip'
            cmd = 'curl -X POST --data "folder=virulencefinder&filename=virulencefinder.zip" -o ' + zipfile + ' https://cge.cbs.dtu.dk/cge/download_data.php'
            print('Downloading data with:', cmd, sep='\n')
            common.syscall(cmd)
            common.syscall('unzip ' + zipfile)
        else:
            RefGenesGetter._get_genetic_epi_database_from_bitbucket('virulencefinder', tmpdir, git_commit=self.version)
            os.chdir(tmpdir)

        print('Combining downloaded fasta files...')
        fout_fa = pyfastaq.utils.open_file_write(final_fasta)
        fout_tsv = pyfastaq.utils.open_file_write(final_tsv)
        name_count = {}

        for filename in os.listdir(tmpdir):
            if filename.endswith('.fsa'):
                print('   ', filename)
                fix_file = os.path.join(tmpdir, filename + '.fix.fsa')
                RefGenesGetter._fix_virulencefinder_fasta_file(os.path.join(tmpdir, filename), fix_file)
                file_reader = pyfastaq.sequences.file_reader(fix_file)
                for seq in file_reader:
                    original_id = seq.id
                    seq.id = seq.id.replace('_', '.', 1)
                    seq.id = seq.id.replace(' ', '_')
                    if seq.id in name_count:
                        name_count[seq.id] += 1
                        seq.id = seq.id + '.' + str(name_count[seq.id])
                    else:
                        name_count[seq.id] = 1
                    print(seq, file=fout_fa)
                    print(seq.id, '0', '0', '.', '.', 'Original name was ' + original_id, sep='\t', file=fout_tsv)

        pyfastaq.utils.close(fout_fa)
        pyfastaq.utils.close(fout_tsv)
        print('\nFinished combining files\n')
        os.chdir(current_dir)
        if not self.debug:
            common.rmtree(tmpdir)
        print('Finished. Final files are:', final_fasta, final_tsv, sep='\n\t', end='\n\n')
        print('You can use them with ARIBA like this:')
        print('ariba prepareref -f', final_fasta, '-m', final_tsv, 'output_directory\n')
        print('If you use this downloaded data, please cite:')
        print('"Real-time whole-genome sequencing for routine typing, surveillance, and outbreak detection of verotoxigenic Escherichia coli", Joensen al 2014, PMID: 24574290\n')

    def _get_from_ncbi(self, outprefix, test=None): ## author github:schultzm
        """
        Download the NCBI-curated Bacterial Antimicrobial Resistance Reference Gene Database.
        Uses BioPython to do the data collection and extraction.
        Author: schultzm (github) May 31, 2019.

        >>> from Bio import Entrez
        >>> import getpass
        >>> import socket
        >>> BIOPROJECT = "PRJNA313047"
        >>> RETMAX = 100
        >>> import getpass
        >>> import socket
        >>> Entrez.email = getpass.getuser()+'@'+socket.getfqdn()
        >>> search_results = Entrez.read(Entrez.esearch(db="nucleotide",
        ...                                             term=BIOPROJECT,
        ...                                             retmax=RETMAX,
        ...                                             usehistory="y",
        ...                                             idtype="acc"))
        >>> webenv = search_results["WebEnv"]
        >>> query_key = search_results["QueryKey"]
        >>> target_accn = "    NG_061627.1"
        >>> records = Entrez.efetch(db="nucleotide",
        ...                         rettype="gbwithparts",
        ...                         retmode="text",
        ...                         retstart=0,
        ...                         retmax=RETMAX,
        ...                         webenv=webenv,
        ...                         query_key=query_key,
        ...                         idtype="acc")
        >>> from Bio.Alphabet import generic_dna
        >>> from Bio import SeqIO
        >>> from Bio.Seq import Seq
        >>> from Bio.SeqRecord import SeqRecord
        >>> for gb_record in SeqIO.parse(records, "genbank"):
        ...     if gb_record.id == 'NG_061627.1':
        ...         gb_record
        SeqRecord(seq=Seq('TAATCCTTGGAAACCTTAGAAATTGATGGAGGATCTTAACAAGATCCTGACATA...GGC', IUPACAmbiguousDNA()), id='NG_061627.1', name='NG_061627', description='Klebsiella pneumoniae SCKLB88 mcr-8 gene for phosphoethanolamine--lipid A transferase MCR-8.2, complete CDS', dbxrefs=['BioProject:PRJNA313047'])

        """

        outprefix = os.path.abspath(outprefix)
        final_fasta = outprefix + '.fa'
        final_tsv = outprefix + '.tsv'

        # Download the database as genbank using Bio.Entrez
        from Bio import Entrez
        import getpass
        import socket
        import sys
        BIOPROJECT = "PRJNA313047" ## https://www.ncbi.nlm.nih.gov/bioproject/?term=PRJNA313047
        RETMAX=100000000
        Entrez.email = getpass.getuser()+'@'+socket.getfqdn()
        # See section 9.15  Using the history and WebEnv in
        # http://biopython.org/DIST/docs/tutorial/Tutorial.html#sec:entrez-webenv
        search_results = Entrez.read(Entrez.esearch(db="nucleotide",
                                                    term=BIOPROJECT,
                                                    retmax=RETMAX,
                                                    usehistory="y",
                                                    idtype="acc"))
        acc_list = search_results["IdList"]
        webenv = search_results["WebEnv"]
        query_key = search_results["QueryKey"]
        if test:
            return acc_list
            #up to here
        if len(acc_list) > 0:
            print(f"E-fetching {len(acc_list)} genbank records from BioProject {BIOPROJECT} and writing to.  This may take a while.", file=sys.stderr)
            #pull out the records as fasta from the genbank
            from Bio import SeqIO
            from Bio.Seq import Seq
            from Bio.SeqRecord import SeqRecord

            print(f"Parsing genbank records.")
            with open(final_fasta, "w") as f_out_fa, \
                 open(final_tsv, "w") as f_out_tsv:
                for idx, record_id in enumerate(acc_list):
                    print(f"'{record_id}'")
                    if record_id.startswith('AP'):
                        print('skipping')
                        continue
                    record = Entrez.efetch(db="nucleotide",
                                                rettype="gbwithparts", retmode="text",
                                                id=record_id)
                    gb_record = SeqIO.read(record, "genbank")

                    n=0
                    record_new=[]
                    for index, feature in enumerate(gb_record.features):
                        if feature.type == 'CDS':
                            n+=1
                            gb_feature = gb_record.features[index]
                            id = None
                            try:
                                id = gb_feature.qualifiers["allele"]
                            except:
                                try:
                                    try:
                                        id = gb_feature.qualifiers["gene"]
                                    except:
                                        id = gb_feature.qualifiers["locus_tag"]
                                except KeyError:
                                    print(f"gb_feature.qualifer not found", file=sys.stderr)
                            accession = gb_record.id
                            seq_out = Seq(str(gb_feature.extract(gb_record.seq)))
                            record_new.append(SeqRecord(seq_out,
                                         id=f"{id[0]}.{accession}",
                                         description=""))
                    if len(record_new) == 1:
                        print(f"Processing record {idx+1} of {len(acc_list)} (accession {accession})", file=sys.stderr)
                        f_out_fa.write(f"{record_new[0].format('fasta').rstrip()}\n")
                        f_out_tsv.write(f"{id[0]}.{accession}\t1\t0\t.\t.\t{gb_feature.qualifiers['product'][0]}\n")
                    if idx == len(acc_list)-1:
                        print('Finished. Final files are:', final_fasta, final_tsv, sep='\n\t', end='\n\n')
                        print('You can use them with ARIBA like this:')
                        print('ariba prepareref -f', final_fasta, '-m', final_tsv, 'output_directory\n')

        else:
            print(f"Nothing to do. Exiting.")
    def run(self, outprefix):
        exec('self._get_from_' + self.ref_db + '(outprefix)')


from peewee import *
import json
import string
import random

db = SqliteDatabase('data/database.db')

class Tunnel(Model):
    name = CharField(unique=True)  # subdomain
    hostname = CharField()
    localaddr = CharField()
    remoteport = CharField()
    proto = CharField()
    auth = CharField()
    starttime = IntegerField()

    class Meta:
        database = db

class Auth(Model):
    token = CharField()

    class Meta:
        database = db

    @staticmethod
    def token_gen(len):
        str = string.ascii_letters + string.digits
        keylist = [random.choice(str) for i in range(len)]
        return "".join(keylist)

def tunnel_to_dict(row):
    d = {}
    d['id'] = row.id
    d['name'] = row.name
    d['hostname'] = row.hostname
    d['localaddr'] = row.localaddr
    d['remoteport'] = row.remoteport
    d['proto'] = row.proto
    d['auth'] = row.auth
    d['starttime'] = row.starttime
    return d

def database_init():
    try:
        Tunnel.create_table()
    except OperationalError:
        print("tunnel table already exists!")
    try:
        Auth.create_table()
        random_token = Auth.token_gen(32)
        Auth.create(token=random_token)
        print("auth token: " + random_token)
    except OperationalError:
        print("auth table already exists!")


"""Module containing the datatype of the output domain that the learning algorithms return."""
from collections import defaultdict
from typing import Set, List, Dict

from pddl_plus_parser.models import SignatureType, Predicate, PDDLType, PDDLConstant, PDDLFunction, Domain, \
    ConditionalEffect, CompoundPrecondition, \
    UniversalEffect, NumericalExpressionTree

DISJUNCTIVE_PRECONDITIONS_REQ = ":disjunctive-preconditions"
NEGATIVE_PRECONDITIONS_REQ = ":negative-preconditions"
EQUALITY_REQ = ":equality"
UNIVERSAL_PRECONDITIONS_REQ = ":universal-preconditions"
CONDITIONAL_EFFECTS_REQ = ":conditional-effects"
ADDED_LEARNING_REQUIREMENTS = [DISJUNCTIVE_PRECONDITIONS_REQ, NEGATIVE_PRECONDITIONS_REQ, EQUALITY_REQ,
                               UNIVERSAL_PRECONDITIONS_REQ]

class LearnerAction:
    """Class representing an action that the learning algorithm outputs."""

    name: str
    signature: SignatureType
    preconditions: CompoundPrecondition
    discrete_effects: Set[Predicate]
    conditional_effects: Set[ConditionalEffect]
    universal_effects: Set[UniversalEffect]
    numeric_effects: Set[NumericalExpressionTree]

    def __init__(self, name: str, signature: SignatureType):
        self.name = name
        self.signature = signature
        self.preconditions = CompoundPrecondition()
        self.discrete_effects = set()
        self.conditional_effects = set()
        self.universal_effects = set()
        self.numeric_effects = set()

    def __str__(self):
        signature_str_items = []
        for parameter_name, parameter_type in self.signature.items():
            signature_str_items.append(f"{parameter_name} - {str(parameter_type)}")

        signature_str = " ".join(signature_str_items)
        return f"({self.name} {signature_str})"

    @property
    def parameter_names(self) -> List[str]:
        return list(self.signature.keys())

    @property
    def preconditions_str_set(self) -> Set[str]:
        """Returns the set of the string representations of the preconditions."""
        precondition_str_set = set()
        for _, precondition in self.preconditions:
            if isinstance(precondition, Predicate):
                precondition_str_set.add(precondition.untyped_representation)

        return precondition_str_set

    def _signature_to_pddl(self) -> str:
        """Converts the action's signature to the PDDL format.

        :return: the PDDL format of the signature.
        """
        signature_str_items = []
        for parameter_name, parameter_type in self.signature.items():
            signature_str_items.append(f"{parameter_name} - {str(parameter_type)}")

        signature_str = " ".join(signature_str_items)
        return f"({signature_str})"

    def _effects_to_pddl(self) -> str:
        """Converts the effects to the needed PDDL format.

        :return: the PDDL format of the effects.
        """
        simple_effects = "\n\t\t".join([effect.untyped_representation for effect in self.discrete_effects])

        conditional_effects = "\n\t\t"
        conditional_effects += "\t\t\n".join([str(conditional_effect) for conditional_effect
                                              in self.conditional_effects])

        universal_effects = "\n\t\t"
        universal_effects += "\t\t\n".join([str(universal_effect) for universal_effect
                                            in self.universal_effects])

        if len(self.numeric_effects) > 0:
            numeric_effects = "\t\t\n".join([effect.to_pddl() for effect in self.numeric_effects])
            return f"(and {simple_effects}\n" \
                   f"\t\t{conditional_effects}\n" \
                   f"\t\t{universal_effects}\n" \
                   f"{numeric_effects})"

        return f"(and {simple_effects} {conditional_effects} {universal_effects})"

    def to_pddl(self) -> str:
        """Returns the PDDL string representation of the action.

        :return: the PDDL string representing the action.
        """
        action_string = f"(:action {self.name}\n" \
                        f"\t:parameters {self._signature_to_pddl()}\n" \
                        f"\t:precondition {str(self.preconditions)}\n" \
                        f"\t:effect {self._effects_to_pddl()})"
        formatted_string = "\n".join([line for line in action_string.split("\n") if line.strip()])
        return f"{formatted_string}\n"

class LearnerDomain:
    """Class representing the domain that is to be learned by the action model learning algorithm."""

    name: str
    requirements: List[str]
    types: Dict[str, PDDLType]
    constants: Dict[str, PDDLConstant]
    predicates: Dict[str, Predicate]
    functions: Dict[str, PDDLFunction]
    actions: Dict[str, LearnerAction]

    # processes: Dict[str, Action] - TBD
    # events: Dict[str, Action] - TBD

    def __init__(self, domain: Domain):
        self.name = domain.name
        self.requirements = domain.requirements
        self.types = domain.types
        self.constants = domain.constants
        self.predicates = domain.predicates
        self.functions = domain.functions
        self.actions = {}
        for action_name, action_object in domain.actions.items():
            self.actions[action_name] = LearnerAction(name=action_name, signature=action_object.signature)

    def __str__(self):
        return (
                "< Domain definition: %s\n Requirements: %s\n Predicates: %s\n Functions: %s\n Actions: %s\n "
                "Constants: %s >"
                % (
                    self.name,
                    [req for req in self.requirements],
                    [str(p) for p in self.predicates.values()],
                    [str(f) for f in self.functions.values()],
                    [str(a) for a in self.actions.values()],
                    [str(c) for c in self.constants],
                )
        )

    def _complete_missing_requirements(self) -> None:
        """Completes the requirements of the domain from the needed requirements of the learning algorithm."""
        for requirement in ADDED_LEARNING_REQUIREMENTS:
            if requirement not in self.requirements:
                self.requirements.append(requirement)

    def _types_to_pddl(self) -> str:
        """Converts the types to a PDDL string.

        :return: the PDDL string representing the types.
        """
        parent_child_map = defaultdict(list)
        for type_name, type_obj in self.types.items():
            if type_name == "object":
                continue

            parent_child_map[type_obj.parent.name].append(type_name)

        types_strs = []
        for parent_type, children_types in parent_child_map.items():
            types_strs.append(f"\t{' '.join(children_types)} - {parent_type}")

        return "\n".join(types_strs)

    def _constants_to_pddl(self) -> str:
        """Converts the constants to a PDDL string.

        :return: the PDDL string representing the constants.
        """
        same_type_constant = defaultdict(list)
        for const_name, constant in self.constants.items():
            if const_name == "object":
                continue

            same_type_constant[constant.type.name].append(const_name)

        types_strs = []
        for constant_type_name, constant_objects in same_type_constant.items():
            types_strs.append(f"\t{' '.join(constant_objects)} - {constant_type_name}")

        return "\n".join(types_strs)

    def _functions_to_pddl(self) -> str:
        """Converts the functions to PDDL format.

        :return: the PDDL format of the functions.
        """
        return "\n\t".join([str(f) for f in self.functions.values()])

    def to_pddl(self) -> str:
        """Converts the domain into a PDDL string format.

        :return: the PDDL string representing the domain.
        """
        self._complete_missing_requirements()
        predicates = "\n\t".join([str(p) for p in self.predicates.values()])
        predicates_str = f"(:predicates {predicates}\n)\n\n" if len(self.predicates) > 0 else ""
        types_str = f"(:types {self._types_to_pddl()}\n)\n\n" if len(self.types) > 0 else ""
        actions = "\n".join(action.to_pddl() for action in self.actions.values())
        constants = f"(:constants {self._constants_to_pddl()}\n)\n\n" if len(self.constants) > 0 else ""
        functions = f"(:functions {self._functions_to_pddl()}\n)\n\n" if len(self.functions) > 0 else ""
        return f"(define (domain {self.name})\n" \
               f"(:requirements {' '.join(self.requirements)})\n" \
               f"{types_str}" \
               f"{constants}" \
               f"{predicates_str}" \
               f"{functions}" \
               f"{actions}\n)"


#!/usr/bin/env python
# --------------------------------------------------------
# --------------------------------------------------------

"""Test an object detection network on an image database."""

import matplotlib
# matplotlib.use('Agg')
import matplotlib.pyplot as plt

import _init_paths
from core.test import test_net
from core.config import cfg, cfg_from_file, cfg_from_list, set_global_cfg
from datasets.factory import get_repo_imdb
import caffe
import argparse
import pprint
import time, os, sys

def parse_args():
    """
    Parse input arguments
    """
    parser = argparse.ArgumentParser(description='Test an Object Detection network')
    parser.add_argument('--gpu', dest='gpu_id', help='GPU id to use',
                        default=0, type=int)
    parser.add_argument('--def', dest='prototxt',
                        help='prototxt file defining the network',
                        default=None, type=str)
    parser.add_argument('--net', dest='caffemodel',
                        help='model to test',
                        default=None, type=str)
    parser.add_argument('--cfg', dest='cfg_file',
                        help='optional config file', default=None, type=str)
    parser.add_argument('--wait', dest='wait',
                        help='wait until net file exists',
                        default=True, type=bool)
    parser.add_argument('--imdb', dest='imdb_name',
                        help='dataset to test',
                        default='voc_2007_test', type=str)
    parser.add_argument('--comp', dest='comp_mode', help='competition mode',
                        action='store_true')
    parser.add_argument('--set', dest='set_cfgs',
                        help='set config keys', default=None,
                        nargs=argparse.REMAINDER)
    parser.add_argument('--vis', dest='vis', help='visualize detections',
                        action='store_true')
    parser.add_argument('--num_dets', dest='max_per_image',
                        help='max number of detections per image',
                        default=100, type=int)
    parser.add_argument('--rotate', dest='rotate',
                        help='how much should we rotate each image?',
                        default=0, type=int)
    # params for model to which active learning is applied
    parser.add_argument('--al_def', dest='al_def',
                        help='model prototxt to which active learning is applied',
                        default=None, type=str)
    parser.add_argument('--al_net', dest='al_net',
                        help='model weights to which active learning is applied',
                        default=None, type=str)

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = parse_args()

    print('Called with args:')
    print(args)

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)
    set_global_cfg("TEST")

    cfg.GPU_ID = args.gpu_id
    cfg.ROTATE_IMAGE = args.rotate

    print('Using config:')
    pprint.pprint(cfg)

    while not os.path.exists(args.caffemodel) and args.wait:
        print('Waiting for {} to exist...'.format(args.caffemodel))
        time.sleep(10)

    caffe.set_mode_gpu()
    caffe.set_device(args.gpu_id)
    net = caffe.Net(args.prototxt, args.caffemodel, caffe.TEST)
    net.name = os.path.splitext(os.path.basename(args.caffemodel))[0]

    print(args.imdb_name)
    imdb = get_repo_imdb(args.imdb_name)
    imdb.competition_mode(args.comp_mode)
    if not cfg.TEST.OBJ_DET.HAS_RPN and cfg.TASK == 'object_detection':
        imdb.set_proposal_method(cfg.TEST.PROPOSAL_METHOD)

    al_net = None
    if args.al_net is not None and args.al_def is not None:
        al_net = caffe.Net(args.al_def,caffe.TEST, weights=args.al_net)
        al_net.name = "al_"+os.path.splitext(os.path.basename(args.al_def))[0]

    test_net(net, imdb, max_per_image=args.max_per_image, vis=args.vis, al_net=al_net)

'''
argparse.ArgumentParser
Input: (description='Test an Object Detection network'), Output: parser

caffe.Net
input: (args.prototxt, args.caffemodel, caffe.TEST), Output: net

os.path.splitext
input: (os.path.basename(args.caffemodel)), output: net.name

get_repo_imdb
input: (args.imdb_name), output: imdb
'''


# -*- coding: UTF-8 -*-

import os

from django import forms
from wheatley.models import Location, Photo, Tweet, Settings

class LocationForm(forms.ModelForm):
	class Meta:
		model = Location
		fields = ['title', 'latitude', 'longitude', 'elevation']

class PhotoForm(forms.ModelForm):
	SIZE = (
			(u'640x480', u'640x480'),
			(u'567x480', u'567x480'),
			(u'720x576', u'720x567'),
			(u'1920x1080', u'1920x1080'),
			(u'1920x1200', u'1920x1200'),
	)
	size = forms.CharField(max_length=255, widget=forms.Select(choices=SIZE))

	# ugly fuck :P
	def get_raspistill(self):
		settings = Settings.objects.latest('created_at')
		raspistill = settings.get_raspistill()

		cmd = "%s --width %s --height %s --quality %s" % (raspistill,
				self.data['width'], self.data['height'],
				self.data['quality'])
		cmd = "%s --imxfx %s --metering %s --exposure %s --awb %s" % (cmd,
				self.data['imxfx'], self.data['metering'],
				self.data['exposure'], self.data['awb'])
		cmd = "%s --sharpness %s --contrast %s --brightness %s" % (cmd,
				self.data['sharpness'], self.data['contrast'],
				self.data['brightness'])
		cmd = "%s --saturation %s --rotation %s" % (cmd,
				self.data['saturation'], self.data['rotation'])

		if 'hflip' in self.data.keys():
			cmd = "%s --hflip" % (cmd)
		if 'vflip' in self.data.keys():
			cmd = "%s --vflip" % (cmd)

		cmd = "%s --output %s" % (cmd, os.path.join(settings.media_path, self.data['filename']))

		return cmd

	class Meta:
		model = Photo
		fields = ['filename', 'width', 'height', 'quality', 'imxfx',
		'metering', 'exposure', 'awb', 'sharpness', 'contrast', 'brightness',
		'saturation', 'rotation', 'hflip', 'vflip', 'size',]

class TweetForm(forms.ModelForm):
	class Meta:
		model = Tweet
		fields = ['status', 'hashtag']
		widgets = {
				'status': forms.Textarea(attrs={'cols': 80, 'rows': 3}),
		}

# vim: set ft=python ts=4 sw=4 :


"""

"""
from order_app.models import BuyOrder, SellOrder, OrderBookState
from datetime import datetime

def populate_order_table(self) -> None:
    """"""
    state, b_created = OrderBookState.objects.get_or_create(
        lookup_time=self.lookup_time)

    for order_type, order_set in (
            ("sell", self.sell_orders), ("buy", self.buy_orders)
    ):
        for row in order_set:
            date_as_int = float(
                "{}.{}".format(row["date"][:10], row["date"][10:])
            )
            date = datetime.fromtimestamp(date_as_int)
            if order_type == "sell":
                use_model = SellOrder
            else:
                use_model = BuyOrder
            obj, created = use_model.objects.get_or_create(
                amount=row["amount"],
                date=date,
                label=row["label"],
                order_id=row["orderId"],
                price=row["price"],
                total=row["total"])
            if not created:
                print("Cannot create Order coz already created")
            if order_type == "sell":
                state.sell_orders.add(obj)
            else:
                state.buy_orders.add(obj)

if __name__ == '__main__':
    for row in rows:
        row.poulate_order_table()


import os
import sys
import re
import tempfile
import platform
import tarfile
import json

from cerbero.build.cookbook import CookBook
from cerbero.packages.packagesstore import PackagesStore
from cerbero.packages.package import SDKPackage
from cerbero.utils import messages as m
from cerbero.utils import shell, parse_file

from cerbero.packages import PackageType
from cerbero.packages.disttarball import DistTarball
from hacks.utils import MD5
from cerbero.errors import FatalError

from cerbero.bootstrap.build_tools import BuildTools as Build_Tools

class Package(object):

    def __init__(self,config ):
        self.config = config
        self.store  = PackagesStore(config)
        self.cookbook = CookBook(config)
        self.packages_ = None

    def _recipes(self,package):
        '''
        return the recipes (name:version) dict included in the package (not include in deps packages)

        '''

        all = package.recipes_dependencies()

        rdeps=[]
        for pkg in self.store.get_package_deps( package.name):
            rdeps.extend( pkg.recipes_dependencies())
        rnames = list(set(all).difference(set(rdeps)))
        recipes ={}
        for name in rnames:
            recipes[name] = self.cookbook.get_recipe(name).version
        return recipes

    def get_recipes(self,package):
        '''
        return the recipes (name:version) dict included in the package (not include in deps packages)

        '''

        if isinstance (package,SDKPackage):
            recipes={}
            for p in self.store.get_package_deps(package):
                recipes.update( self._recipes( p) )
            return recipes

        return self._recipes(package)

    def get_packages_deps(self,package):
        '''
        return the packages of the SDK/package dependent.

        '''

        dependencies = []

        if isinstance (package,SDKPackage):
            if hasattr(package, 'dependencies'):
                dependencies = package.dependencies
            return []
        else:
            dependencies = [p.name for p in self.store.get_package_deps(package.name, False)]

        deps = {}
        for name in dependencies:
            version = self.store.get_package(name).version
            deps[name] = version
        return deps

    def _pkg(self,package):

        t = DistTarball(self.config,package,self.store)
        tarball={}

        for ptype in [PackageType.DEVEL,PackageType.RUNTIME]:
            TNAME={PackageType.DEVEL:'devel',PackageType.RUNTIME:'runtime'}
            tarball[TNAME[ptype]]={
                'filename':t._get_name(ptype)
            }
        profile = {
            'name':package.name,
            'version':package.version,
            'platform':self.config.target_platform,
            'arch':self.config.target_arch,
            'recipes': self.get_recipes(package),
            'dependencies': self.get_packages_deps(package),

            #'deps': [p.name for p in self.store.get_package_deps(package.name, True)],
            #'direct.deps': [p.name for p in self.store.get_package_deps(package.name, False)],
            'tarball': tarball
        }

        if self.config.build_type == 'debug':
            profile['debug'] = True
        return profile

    def get(self,name):
        if self.packages_ is None:
            self.packages()
        return self.packages_[name]

    def packages(self):
        if self.packages_ is None:
            self.packages_ = {}
            for pkg in self.store.get_packages_list():
                #if isinstance (pkg,SDKPackage):
                #    continue
                self.packages_[pkg.name] = self._pkg( pkg )

        return self.packages_

class BuildTools(object):

    def __init__(self, config):
        self.config = config
        self._profile = None

    def _get(self):

        tools = Build_Tools(self.config)
        store  = PackagesStore(self.config)
        cookbook = CookBook(self.config)

        names  = tools.BUILD_TOOLS
        names += tools.PLAT_BUILD_TOOLS.get(self.config.platform, [])
        recipes ={}
        path = os.path.join(self.config.packages_dir,'custom.py')
        d={}
        parse_file(path,d )
        version = d['GStreamer'].version

        for name in names:
            recipes[name] = cookbook.get_recipe(name).version

        pkgname ='build-tools'

        tarball = '%s-%s-%s-%s'%( pkgname,
            self.config.platform,
            self.config.arch,
            version
        )

        return {
            'name':pkgname,
            'version': version,
            'prefix':self.config.build_tools_prefix,
            'deps':[],
            'platform':self.config.platform,
            'arch':self.config.arch,

            'tarball':{
                'runtime':{ 'filename':tarball +'.tar.bz2'}
            },
            'recipes': recipes
        }

    def get(self):
        if self._profile is None:
            self._profile = self._get()
        return self._profile


#! python3
# cadastro_imobiliario/model.py

class Imovel:
    def __init__(self, logradouro, cep, bairro, cidade, id):
        self.logradouro = logradouro
        self.cep = cep
        self.bairro = bairro
        self.cidade = cidade
        self.id = id

    def resumo(self):
        mapa = {
            "logradouro": self.logradouro,
            "cep": self.cep,
            "bairro": self.bairro,
            "cidade": self.cidade,
            "id": self.id,
        }
        return mapa

class Individuo:
    def __init__(self, nome, data_de_nascimento, id):
        self.nome = nome
        self.data_de_nascimento = data_de_nascimento
        self.id = id

    def resumo(self):
        mapa = {
            "nome": self.nome,
            "data_de_nascimento": self.data_de_nascimento,
            "id": self.id,
        }
        return mapa

class Aluguel:
    def __init__(self, id_imovel, id_proprietario, id):
        self.id_imovel = id_imovel
        self.id_proprietario = id_proprietario
        self.id = id

    def resumo(self):
        mapa = {
            "id imovel": self.id_imovel,
            "id proprietario": self.id_proprietario,
            "id": self.id,
        }
        return mapa

class Contrato:
    def __init__(self, id_aluguel, id_inquilino, id):
        self.id_aluguel = id_aluguel
        self.id_inquilino = id_inquilino
        self.id = id

    def resumo(self):
        mapa = {
            "id aluguel": self.id_aluguel,
            "id inquilino": self.id_inquilino,
            "id": self.id
        }
        return mapa


#!/usr/bin/python

import sys

class user:
    old = ''
    current = ''
    new = ''
    home = ''
    uid = ''   #User ID Number goes here.
    if not isinstance(uid, int):
        try:
            uid = int(uid)
        except:
            print >> sys.stderr, "uid provided is a non-number. Quitting."
            exit(1)

def get_values():
    import pwd
    pwdall = pwd.getpwall()
    uidpw=[]
    retval = 0
    for x in pwdall:
        if user.old in x:
            uidpw = x
    if not uidpw:
        uidpw= [ '','','','','','']
    (user.current,user.home) = (uidpw[0],uidpw[5])

def set_values():
    import subprocess
    import shlex
    from subprocess import call,PIPE
    retval = 0
    command = '/usr/sbin/usermod -l %s -d /home/%s -m %s'
    command = command % (
        user.new,
        user.new,
        user.current
    )
    command = shlex.split(command)
    if not user.current == user.new:
        retval = call(command,stdout=PIPE,stderr=PIPE)
    return retval

def main():
    get_values()
    retval = set_values()
    return retval

if __name__ == "__main__":
    sys.exit(main())


#!/usr/bin/env python3

import numpy as np
import pytest
from sklearn.linear_model import LinearRegression

import matplotlib.pyplot as plt

class LinearRegr:
    """ Linear Regression model from scratch.
    y = w_0 + w_1*x_1 + ... + w_m*x_m
    Attributes:
        self.weight (np.array): weights vector. weight = (w_0, w_1, ..., w_m)
    """
    weight = np.array([])

    def fit(self, x: np.array, y: np.array, learning_rate: float = 0.002, max_iter: int = 100000):
        """ Find weight vector minimizing quadratic loss function <self._cost> using <self._gradient_descent> method.
        Args:
            x (np.array[n,m]): n m-dimensional samples of parameters
            y (np.array[n]): n results.
            learning_rate (float). precision of each step. Defaults to 0.002
            max_iter (int). Number of steps. Defaults to 10**5.
        Returns:
            self.
        """
        w, b = self._gradient_descent(x, y, np.zeros(x.shape[1]), 0, learning_rate=learning_rate, max_iter=max_iter)
        self.weight = np.append(np.array([b]), w)
        return self

    def predict(self, x: np.array):
        """ Find weight vector minimizing quadratic loss function <self._cost> using <self._gradient_descent> method.
        Args:
            x (np.array[n,m]): n m-dimensional samples of parameters
        Returns:
            y (np.array[n]): n results. Calculated using self.weight
        """
        return x.dot(self.weight[1:]) + self.weight[0]

    @staticmethod
    def _cost(x, y, weight: np.array, bias: float):
        """ Return cost of model with given parameter"""
        cost = np.sum((((x.dot(weight) + bias) - y) ** 2))
        return cost

    @staticmethod
    def _gradient_descent(x: np.array, y: np.array, weight: np.array, bias: float, learning_rate: float,
                          max_iter: int):
        """ Calculate minimum of loss function using gradient descent algorithm.
        Args:
            x (np.array[n,m]): n m-dimenstional samples of parameters
            y (np.array[n]): n results.
            weight (np.array[n]). Initial weight.
            bias (float). Initial bias.
            learning_rate (float). precision of each step.
            max_iter (int). Number of steps.
        Returns:
            weight (np.array[n]). Calculated weight
            bias (float). Calculated bias.
        """
        for _ in range(max_iter):
            z = x.dot(weight) + bias
            loss = z - y

            weight_gradient = x.T.dot(loss)
            bias_gradient = np.sum(loss)

            weight = weight - learning_rate * weight_gradient
            bias = bias - learning_rate * bias_gradient

        return weight, bias

def test_RegressionInOneDim():
    X = np.array([1, 3, 2, 5]).reshape((4, 1))
    Y = np.array([2, 5, 3, 8])
    a = np.array([1, 2, 10]).reshape((3, 1))
    expected = LinearRegression().fit(X, Y).predict(a)
    actual = LinearRegr().fit(X, Y).predict(a)
    assert list(actual) == pytest.approx(list(expected))

def test_RegressionInThreeDim():
    X = np.array([1, 2, 3, 5, 4, 5, 4, 3, 3, 3, 2, 5]).reshape((4, 3))
    Y = np.array([2, 5, 3, 8])
    a = np.array([1, 0, 0, 0, 1, 0, 0, 0, 1, 2, 5, 7, -2, 0, 3]).reshape((5, 3))
    expected = LinearRegression().fit(X, Y).predict(a)
    actual = LinearRegr().fit(X, Y).predict(a)
    assert list(actual) == pytest.approx(list(expected))


# day18

def evaluate(x):
    # evaluate each parentheses
    while '(' in x:
        start_ind = x.index('(')
        end_ind   = get_closing_paran(x, start_ind)

        simplified_x = []
        y = evaluate(x[start_ind + 1: end_ind])

        simplified_x += x[:start_ind] + [y] + x[end_ind + 1:]

        return(evaluate(simplified_x))

    # no parentheses left
    # evaluate each '+'
    while '+' in x:
        ind = x.index('+')

        simplified_x = []
        y = int(x[ind-1]) + int(x[ind+1])

        simplified_x += x[:ind-1] + [str(y)] + x[ind+2:]
        return(evaluate(simplified_x))

    # only '*' left
    value = int(x[0])
    ind = 1
    while ind < len(x):
        if x[ind] == '*':
            value *= int(x[ind + 1])

        ind += 2

    return str(value)

def get_closing_paran(x, i):
    num_open   = 1
    num_closed = 0

    while num_open > num_closed:
        open_i  = len(x) + 1
        close_i = len(x) + 1

        # check for inner parentheses
        if '(' in x[i+1:]:
            open_i  = x.index('(', i+1)
        close_i =  x.index(')', i+1)

        if open_i < close_i:
            num_open   += 1
            i = open_i
        else:
            num_closed += 1
            i = close_i

    return close_i

total = 0

f = open('day18.txt', 'r')
for x in f:
    x = x.replace('(', ' ( ')
    x = x.replace(')', ' ) ')
    x = x.split()

    total += int(evaluate(x))

print(total)


a, b, c = [int(el) for el in input().split()]

cond2 = (a ** 2 + b ** 2 + c ** 2
         == 2 * max(a, b, c) ** 2)

if cond2:
    print("YES")
else:
    print("NO")


import pandas as pd
from boostaroota import BoostARoota
import urllib

#################
#Madelon Dataset
train_url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/madelon/MADELON/madelon_train.data'
# download the file
raw_data = urllib.request.urlopen(train_url)
train = pd.read_csv(raw_data, delim_whitespace=True, header=None)
train.columns = ["Var"+str(x) for x in range(len(train.columns))]
labels_url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/madelon/MADELON/madelon_train.labels'
raw_data = urllib.request.urlopen(labels_url)
labels = pd.read_csv(raw_data, delimiter=",", header=None)
labels.columns = ["Y"]

########################################################################################################################
#
#  Test that BoostARoota is working
#
########################################################################################################################
br = BoostARoota(metric='logloss')

br.fit(train,labels)
len(train.columns)
len(br.keep_vars_)
new_train = br.transform(train)
new_train2 = br.fit_transform(train,labels)

#Dimension Reduction
print("Original training set has " + str(train.shape) + " dimensions. \n" +\
"BoostARoota with .fit() and .transform() reduces to " + str(new_train.shape) + " dimensions. \n" +\
"BoostARoota with .fit_transform() reduces to " + str(new_train2.shape) + " dimensions.\n" +\
"The two methods may give a slightly different dimensions because of random variation as it is being refit")

########################################################################################################################
#
#  Test that its working with any classifier
#
########################################################################################################################
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier()

from sklearn.ensemble import ExtraTreesClassifier
clf = ExtraTreesClassifier()
br = BoostARoota(clf=clf)
new_train = br.fit_transform(train, labels)