#import os
#import random
import discord

client = discord.Client()

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):

	async def test():
	  await	message.channel.send("Blackjack Test Starting")

	if message.content.startswith('//test'):
		await test()
		#await message.channel.send("Blackjack Test Starting")

'''
#Blackjack Game
#-------------------------------------------------------------------------------------------------------------------#

		deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

		async	def deal(deck):
					hand = []
					for i in range(1):
						random.shuffle(deck)
						card = deck.pop()
						if card == 11:card = "J"
						if card == 12:card = "Q"
						if card == 13:card = "K"
						if card == 14:card = "A"
						hand.append(card)
					return hand

		async	def total(hand):
					total = 0
					for card in hand:
						if card == "J" or card == "Q" or card == "K": total+= 10
						elif card == "A":
								if total >= 1: total+= 1;
								else: total+= 11
						else: total+= card
					return total

		async	def hit(hand):
				card = deck.pop()
				if card == 11:card = "J"
				if card == 12:card = "Q"
				if card == 13:card = "K"
				if card == 14:card = "A"
				hand.append(card)
				return hand

		async	def clear():
				if os.name == 'nt':
					os.system('CLS')
				if os.name == 'posix':
					os.system('clear')

		async	def print_results(dealer_hand, player_hand):
				await clear()
				print("The dealer has a/n " + str(dealer_hand) + " for a total of " + str(total(dealer_hand)))
				print("You have a " + str(player_hand) + " for a total of " + str(total(player_hand)))

		async	def blackjack(dealer_hand, player_hand):
				if total(player_hand) == 21:
					print_results(dealer_hand, player_hand)
					print("Congratulations! You got a Blackjack!\n")
				elif total(dealer_hand) == 21:
					print_results(dealer_hand, player_hand)
					print("Sorry, you lose. The dealer got a blackjack.\n")

		async	def score(dealer_hand, player_hand):
				if total(player_hand) == 21:
					print_results(dealer_hand, player_hand)
					print("Congratulations! You got a Blackjack!\n")
				elif total(dealer_hand) == 21:
					print_results(dealer_hand, player_hand)
					print("Sorry, you lose. The dealer got a blackjack.\n")
				elif total(player_hand) > 21:
					print_results(dealer_hand, player_hand)
					print("Sorry. You busted. You lose.\n")
				elif total(dealer_hand) > 21:
					print_results(dealer_hand, player_hand)
					print("Dealer busts. You win!\n")
				elif total(player_hand) < total(dealer_hand):
					print_results(dealer_hand, player_hand)
					print("Sorry. Your score isn't higher than the dealers. You lose.\n")
				elif total(player_hand) > total(dealer_hand):
					print_results(dealer_hand, player_hand)
					print("Congratulations. Your score is higher than the dealers. You win\n")

		async	def Dealers_choice(player_hand, dealer_hand):
				if 22 > total(player_hand):
					choice = input("Do you want to [H]it, [S]tand, or [Q]uit: ").lower()
				else:
					choice = "s"
					await clear()
					#Dealers choice
				if choice == "h":
					hit(player_hand)
					while total(dealer_hand) < 17:
						await hit(dealer_hand)
					await score(dealer_hand, player_hand)
				elif choice == "s":
					while total(dealer_hand) < 17:
						await hit(dealer_hand)
					await score(dealer_hand, player_hand)
				elif choice == "q":
					print("Bye!")
					exit()

		async def game():
				choice = 0
				await clear()
				message.channel.send("Welcome to Blackjack")
				dealer_hand = deal(deck)
				player_hand = deal(deck)
				while choice != "q":
					print("The dealer is showing a " + str(dealer_hand[0]))
					print("You have a " + str(player_hand) + " for a total of " + str(total(player_hand)))
					await blackjack(dealer_hand, player_hand)

					await Dealers_choice(player_hand, dealer_hand)
		await game()

#-------------------------------------------------------------------------------------------------------------------#
'''

client.run('TOKEN')


# -*- coding: utf-8 -*-
"""
Created on Wed Mar 18 17:45:01 2020

@author: Theis Kevin
"""

import numpy as np

import csv

import os

#  0 => Non/Faux/False 1 => Oui/Vrai/True

# Change Current Working Directory

path = "C:\\Users\\Theis Kevin\\Documents\\MASTER Sciences de la Mer\\Stage M2\\projets\\david_shanafelt3.0"
os.chdir(path)

file = open("D_TL1_pertuB0.csv","r") # fichier des parametres / Parameters File

reader = csv.reader(file,delimiter = ";", skipinitialspace=True)
header = next(reader) # une fois donc premiere ligne de reader sont les headers
dictio = []
for row in reader:
    dictio.append(dict(zip(header, map(float, row))))

# dict() => creation dictionnaire / zip => permet associer un element d'une liste a une autre
# map permet d'appliquer une fonction ici float sur un element iterable ici les lignes

###############################################################################

#Création des matrices / vecteurs utilisée

# Différente valeurs de a (taux de predation) et b (biomass conversion efficiency)
# Value of a (attack rate) and b ()

def vect_a_b(n,par_1):
    a = 0 * np.ones(4)
    b = 0 * np.ones(4)
    for i in range(4):
        if i == 0 :
            a[i] = par_1["a2"]
            b[i] = par_1["b2"]

        elif i == 1:
            a[i] = par_1["a3"]
            b[i] = par_1["b3"]

        elif i == 2:
            a[i] = par_1["a4"]
            b[i] = par_1["b4"]

        else:
            a[i] = par_1["a5"]
            b[i] = par_1["b5"]
    return a[:n] , b[:n]

def vect_gi(B1,n,par_1):
    gi = 0 * np.ones(n) # creation du vecteur de croissance/mort independante de la densité
    for i in range (n):
        if i == 0:
            gi[i] =  par_1["a2"] * B1
        else:
            gi[i] = 0
    return gi

def vecteur_para(n,par_1) :
    ratio_1CN = 0 * np.ones(n) # vecteur de 1/C:N
    ratio_1CN_no_convers = 0 * np.ones(n)
    for i in range (n):
        if i == 0:
            ratio_1CN[i] = 1/par_1["alpha2"]
            ratio_1CN_no_convers[i] = 1/par_1["alpha2"]

        elif i == 1 :
            ratio_1CN[i] = 1 / par_1["alpha3"]
            ratio_1CN_no_convers[i] = 1/((par_1["alpha3"]*par_1["alpha2"]*(1-par_1["b3"]))/(par_1["alpha3"]-par_1["alpha2"]*par_1["b3"]))

        else:
            ratio_1CN[i] = 1 / par_1["alpha3"]
            ratio_1CN_no_convers[i] = 1 / par_1["alpha3"]
    return ratio_1CN , ratio_1CN_no_convers

def matrix_para(n,par_1):
    predation = np.diag(vect_a_b(n,par_1)[1][1:]*vect_a_b(n,par_1)[0][1:]*np.ones(n-1),k = -1) # matrice de conversion de la proie // prey conversion matrix
    prey = np.diag(vect_a_b(n,par_1)[0][1:]*np.ones(n-1), k = 1) # matrice de mort // Death matrix
    no_conversion = np.diag((1-vect_a_b(n,par_1)[1][1:])*vect_a_b(n,par_1)[0][1:]*np.ones(n-1),k = -1)
    return predation , prey, no_conversion

####  Fonction de recyclage // Recycling function

def recyclage(B,n,par_1):     # un verteur de taille n doit rentrer et attention B1 = N ICI

    recy_mort = par_1["lam"]*vecteur_para(n,par_1)[0] * (par_1["m"] + par_1["D"] * B) * B
    recy_noconvers = par_1["lam"] * vecteur_para(n,par_1)[1] * np.dot(matrix_para(n,par_1)[2],B) * B

    recy_mort_tot = sum(recy_mort)
    recy_noconvers_tot = sum(recy_noconvers)
    recy_tot = recy_mort_tot + recy_noconvers_tot
    recyclage_par_sp = []
    recyclage_sp_mort =[]
    recyclage_sp_noconvers =[]
    for i in range(len(B)):
        recyclage_par_sp.append(recy_mort[i]+recy_noconvers[i])
        recyclage_sp_mort.append(recy_mort[i])
        recyclage_sp_noconvers.append(recy_noconvers[i])

    recy_tot = np.array([recy_tot])
    recy_mort_tot = np.array([recy_mort_tot])
    recy_noconvers_tot = np.array([recy_noconvers_tot])
    W = np.concatenate([recy_tot,recy_mort_tot,recy_noconvers_tot,recyclage_par_sp,recyclage_sp_mort,recyclage_sp_noconvers])
    return W

# Création du systeme // creation of the system ###

def syst(X,t,n,par_1):
    B1 = X[0]
    B = X[1:]
    dN = par_1["I"] - par_1["l"] * B1 - par_1["a2"] / par_1["alpha2"] * B1 * B[0] + recyclage(B,n,par_1)[0]
    Y = B * (vect_gi(B1,n,par_1) + np.dot(matrix_para(n,par_1)[0],B) - np.dot(matrix_para(n,par_1)[1],B) - par_1["m"] - par_1["D"] * B)
    dN = np.array([dN])
    W = np.concatenate([dN,Y])
    return W

def flux(X,t,n,par_1):
    B1 = X[0]
    B = X[1:]
    # Flux pour B1 // Flow for B1
    flux_I = par_1["I"]
    flux_mortB1 = par_1["l"]* B1
    flux_huntB1 = par_1["a2"]/par_1["alpha2"]*B1*B[0]
    flux_recy = recyclage(B,n,par_1)[3:(3+n)]

    # Flux pour autre Bi (=! B1) // Flow for Others
    flux_mortB = B * (par_1["m"] + par_1["D"]*B)

    flux_prelB2 = par_1["a2"]*B1*B[0]
    flux_prelB = B * np.dot(matrix_para(n,par_1)[0],B)
    flux_prelB = flux_prelB[1:]
    flux_huntB = B * np.dot(matrix_para(n,par_1)[1],B)
    flux_huntB = flux_huntB[:-1]

    flux_I = np.array([flux_I])
    flux_mortB1 = np.array([flux_mortB1])
    flux_huntB1 = np.array([flux_huntB1])
    flux_prelB2 = np.array([flux_prelB2])
    flow = np.concatenate([flux_I,flux_mortB1,flux_huntB1,flux_recy,flux_mortB,flux_prelB2,flux_prelB,flux_huntB])
    return flow

########### EQUILIBRE // EQUILIBRIUM  #################
### Recherche des equilibres // Search for equilibrium  ##########

## Tri Diagonal Matrix Algorithm(a.k.a Thomas algorithm) solver
def TDMAsolver(a, b, c, d):
    '''
    TDMA solver, a b c d can be NumPy array type or Python list type.
    refer to http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
    and to http://www.cfd-online.com/Wiki/Tridiagonal_matrix_algorithm_-_TDMA_(Thomas_algorithm)
    '''
    nf = len(d) # number of equations
    ac, bc, cc, dc = map(np.array, (a, b, c, d)) # copy arrays
    for it in range(1, nf):
        mc = ac[it-1]/bc[it-1]
        bc[it] = bc[it] - mc*cc[it-1]
        dc[it] = dc[it] - mc*dc[it-1]

    xc = bc
    xc[-1] = dc[-1]/bc[-1]

    for il in range(nf-2, -1, -1):
        xc[il] = (dc[il]-cc[il]*xc[il+1])/bc[il]

    return xc

# Création du vecteur d'equation // Creation of the equation vector

def fun (X,n,par_1):
    B1 = X[0]
    B = X[1:]
    dN = par_1["I"] - par_1["l"] * B1 - par_1["a2"]/ par_1["alpha2"] * B1 * B[0] + recyclage(B,n,par_1)[0]
    Y = B * (vect_gi(B1,n,par_1) + np.dot(matrix_para(n,par_1)[0],B) - np.dot(matrix_para(n,par_1)[1],B) - par_1["m"] - par_1["D"] * B)
    dN = np.array([dN])
    W = np.concatenate([dN,Y])
    return W

# Creation de la J estimé // Creation of estimed J (Jacobian)
def jacfct(X,n,par_1):

    matrix_eq = np.diag(np.ones(n),k=0)

    for i in range (n):
        matrix_eq[i,i] = X[i+1] # matrice des equilibres compartiment biotiques // equilibrium matrice of biotic compartment

    A = np.diag(vect_a_b(n,par_1)[1][1:]*vect_a_b(n,par_1)[0][1:]*np.ones(n-1),k = -1) # Matrice d'interaction // Interaction matrix
    A = A + np.diag(-par_1["D"]*np.ones(n),k = 0)
    A = A + np.diag(- vect_a_b(n,par_1)[0][1:]*np.ones(n-1),k = 1)

    matrix_fprimeg = vect_gi(X[0],n,par_1) + np.dot(A,X[1:]) - par_1["m"]
    matrix_fprimeg = np.diag(matrix_fprimeg*np.ones(n),k=0)

    # Création de la matrice Jacobienne où Jij = Bi %*% dfi(B)/dBj

    jac = np.dot(matrix_eq,A) + matrix_fprimeg # exclusivement des compartiments biotiques

    jac_add = np.ones(n)*0
    jac_add[0] =  par_1["a2"] * X[1]
    jac_new = np.zeros((n,n+1))
    jac_new[:,0] = jac_add
    jac_new[:,1:] = jac # J_new est la matrice biotique totale comprenant dF(B)/dN

    jac_new = jac_new.reshape(n*(n+1))
    jac_tt = np.concatenate([J_N(X,n,par_1),jac_new]) # J_N fonction de la matrice jacobienne pour dN/dt
    jac_tt = jac_tt.reshape(n+1,n+1)    # Matrice Jacobienne de mon système totale
    return jac_tt

###### Réponse à la PERTURBATION // Response to perturbation ###############

# Création du la perturbation // Creation of perturbation

def sigma_pert(par_1):# Deja la variance de la perturbation chez Shanafelt
    if par_1["sppertu"] == 0:
        sigma_pertu = 0.01
    else:
        sigma_pertu = 0.0025

    return sigma_pertu

# Création de la jacobienne partie abiotique DONC ICI B1
# Creation of the Jacobian for abiotic part so B1 // B0 in article

def J_N(X,n,par_1):
    B1 = X[0]
    B = X[1:]
    temp = np.ones(n-1)
    for i in range(n-1):
        temp[i] = (1-vect_a_b(n,par_1)[1][i+1])* vect_a_b(n,par_1)[0][i+1] * vecteur_para(n,par_1)[1][i+1]
    no_conversion_2 = np.diag(temp,k = -1)
    no_conversion_2 = no_conversion_2 + np.diag(temp,k = 1) # use dans le np.dot de Y = matrice Jacobienne du non convertie
    gi_JN = 0 * np.ones(n) # creation du vecteur de croissance/mort independante de la densité
    for i in range (n):
        if i == 0:
            gi_JN[i] = - par_1["a2"]/ par_1["alpha2"]* B1
        else:
            gi_JN[i] = 0
    dN = -par_1["l"] - par_1["a2"]/ par_1["alpha2"]* B[0]
    Y = gi_JN  + par_1["lam"]*vecteur_para(n,par_1)[0] * (par_1["m"] + 2*par_1["D"]*B)  + np.dot(no_conversion_2,B) * par_1["lam"]
    dN = np.array([dN])
    W = np.concatenate([dN,Y])
    return W

#### Fonction rapport => Savoir si I/Irecymax doit être constant (1 / Oui) ou pas (0 / Non)
# Ratio function => If I/Irecymax must be constant (1 / yes) or not (0 / no)

def rap_i_irecymax(n,par_1):
    if par_1["rapport"] == 1:
        if n == 1:
            if par_1["D"]==0:
                par_1["I_max"] =  0.01
                par_1["I_min"] =  0.01
            else:
                par_1["I_max"] = par_1["l"]/par_1["a2"]**2*(10*par_1["D"]*par_1["l"]+par_1["a2"]*par_1["m"])
                par_1["I_min"] =  par_1["l"]/par_1["a2"]**2*(10*par_1["D"]*par_1["l"]+par_1["a2"]*par_1["m"])

        elif n==2:
            if par_1["D"]==0:
                par_1["I_max"] = 0.01
                par_1["I_min"] = 0.01
            else:
                par_1["I_max"] = (par_1["l"]*(10*par_1["a3"]**2*par_1["b3"]*par_1["l"]-par_1["a2"]*par_1["a3"]*par_1["m"]+10*par_1["l"]*par_1["D"]**2+par_1["a2"]*par_1["m"]*par_1["D"]))/(par_1["a2"]**2*par_1["D"])
                par_1["I_min"] = (par_1["l"]*(10*par_1["a3"]**2*par_1["b3"]*par_1["l"]-par_1["a2"]*par_1["a3"]*par_1["m"]+10*par_1["l"]*par_1["D"]**2+par_1["a2"]*par_1["m"]*par_1["D"]))/(par_1["a2"]**2*par_1["D"])
    else:
        par_1["I_max"] =  par_1["I_max"]
        par_1["I_min"] = par_1["I_min"]
    return par_1["I_max"],par_1["I_min"]


import logging
from datetime import datetime

import requests
from dateutil.relativedelta import relativedelta

logger = logging.getLogger('exporter')

def get_sensor_metadata(url, port):
    r = requests.get('https://{}:{}/api/v1/sensor'.format(url, port))
    r.raise_for_status()
    return reformat_sensor_metadata(r.json())

def reformat_sensor_metadata(metadata_json):
    sensor_metadata = {}
    for row in metadata_json:
        sensor_metadata[row['sensor_id']] = {'description': row['description'], 'unit': row['unit']}

    return sensor_metadata

def get_station_metadata_for(url, port, station_id=None):
    r = requests.get('https://{}:{}/api/v1/station'.format(url, port))
    r.raise_for_status()

    return reformat_station_metadata(r.json(), station_id)

def reformat_station_metadata(metadata_json, station_id):
    if not station_id:
        return metadata_json

    for row in metadata_json:
        if row['station_id'] == station_id:
            return [row]

    raise ValueError('Station {} is not available on the backend'.format(station_id))

def get_weather_data_for(month, year, station_id, url, port):
    first_timepoint = datetime(day=1, month=month, year=year)
    last_timepoint = first_timepoint + relativedelta(months=1)

    logger.debug('Requesting data from backend {}:{} for station {}'.format(url, port, station_id))
    r = requests.get('https://{}:{}/api/v1/data?first_timepoint={}&last_timepoint={}&stations={}'.format(
        url,
        port,
        first_timepoint,
        last_timepoint,
        station_id
    ))
    logger.debug('Received data for period {} - {}'.format(first_timepoint, last_timepoint))
    r.raise_for_status()
    month_data = r.json()
    return month_data


import numpy as np
import matplotlib.pyplot as plt

matrix = np.random.randint(1, 101, (5, 5))

matrix *= 10

determinant_before = np.linalg.det(matrix)

inverse_matrix = np.linalg.inv(matrix)

determinant_after = np.linalg.det(inverse_matrix)

labels = ['Детерминант до', 'Детерминант после']
values = [determinant_before, determinant_after]

plt.bar(labels, values, color=['blue', 'green'])
plt.ylabel('Значение определителя')
plt.title('Сравнение определителей')
plt.show()


import sys
from collections import Counter

input_file = sys.argv[1] if len(sys.argv) > 1 else "inputs/14.txt"
with open(input_file) as file:
    sequence = file.readline().strip()
    file.readline()
    rules = dict([line.strip().split(" -> ") for line in file.readlines()])

def part_1():
    current_sequence = sequence
    for _ in range(10):
        new_sequence = ""
        for first, second in zip(current_sequence, current_sequence[1:]):
            new_sequence += first + rules[first + second]
        # the final letter was not added so add it to the sequence
        current_sequence = new_sequence + second
    sorted_counts = Counter(current_sequence).most_common()
    return sorted_counts[0][1] - sorted_counts[-1][1]

def part_2():
    pair_counts = Counter()
    for first, second in zip(sequence, sequence[1:]):
        pair_counts[first + second] += 1
    for _ in range(40):
        updated_double_counts = Counter()
        for current, count in pair_counts.items():
            updated_double_counts[current[0] + rules[current]] += count
            updated_double_counts[rules[current] + current[1]] += count
        pair_counts = updated_double_counts
    single_letter_counter = Counter()
    for pattern, count in pair_counts.items():
        single_letter_counter[pattern[0]] += count
    # the final letter was not counted so add 1 to its count
    single_letter_counter[sequence[-1]] += 1
    sorted_counts = single_letter_counter.most_common()
    return sorted_counts[0][1] - sorted_counts[-1][1]

print(f"Part 1: {part_1()}")
print(f"Part 2: {part_2()}")


import numpy as np
from cil.framework import ImageData
from cil.optimisation.operators import LinearOperator

class DiagonalOperator(LinearOperator):

    r"""DiagonalOperator

    Performs an element-wise multiplication, i.e., `Hadamard Product <https://en.wikipedia.org/wiki/Hadamard_product_(matrices)#:~:text=In%20mathematics%2C%20the%20Hadamard%20product,elements%20i%2C%20j%20of%20the>`_
    of a :class:`DataContainer` `x` and :class:`DataContainer` `diagonal`, `d` .

    .. math:: (D\circ x) = \sum_{i,j}^{M,N} D_{i,j} x_{i, j}

    In matrix-vector interpretation, if `D` is a :math:`M\times N` dense matrix and is flattened, we have a :math:`M*N \times M*N` vector.
    A sparse diagonal matrix, i.e., :class:`DigaonalOperator` can be created if we add the vector above to the main diagonal.
    If the :class:`DataContainer` `x` is also flattened, we have a :math:`M*N` vector.
    Now, matrix-vector multiplcation is allowed and results to a :math:`(M*N,1)` vector. After reshaping we recover a :math:`M\times N` :class:`DataContainer`.

    Parameters
    ----------
    diagonal : DataContainer
        DataContainer with the same dimensions as the data to be operated on.
    domain_geometry : ImageGeometry
        Specifies the geometry of the operator domain. If 'None' will use the diagonal geometry directly. default=None .

    """

    def __init__(self, diagonal, domain_geometry=None):

        if domain_geometry is None:
            domain_geometry = diagonal.geometry.copy()

        super(DiagonalOperator, self).__init__(domain_geometry=domain_geometry,
                                    range_geometry=domain_geometry)
        self.diagonal = diagonal

    def direct(self,x,out=None):

        "Returns :math:`D\circ x` "

        if out is None:
            return self.diagonal * x
        else:
            self.diagonal.multiply(x,out=out)

    def adjoint(self,x, out=None):

        "Returns :math:`D\circ x` "

        return self.direct(x, out=out)

    def calculate_norm(self, **kwargs):

        r""" Returns the operator norm of DiagonalOperator which is the :math:`\infty` norm of `diagonal`

        .. math:: \|D\|_{\infty} = \max_{i}\{|D_{i}|\}

        """

        return self.diagonal.abs().max()


from abc import ABC, abstractmethod
import logging as log
import cv2
from openvino.inference_engine import IENetwork, IECore
from timer import timeit

class Model_X(ABC):
    """
    Abstract Class for the Face Detection Model.
    """

    def __init__(self, model_name, device="CPU", threshold=0.5, extensions=None):
        self.model_weights = model_name + ".bin"
        self.model_structure = model_name + ".xml"
        self.device = device
        self.threshold = threshold
        self.extensions = extensions

    @timeit
    def load_model(self):
        """
        This method is for loading the model to the device specified by the user.
        If your model requires any Plugins, this is where you can load them.
        """
        self.network = IENetwork(self.model_structure, self.model_weights)
        self.input_name = next(iter(self.network.inputs))
        self.input_shape = self.network.inputs[self.input_name].shape
        self.output_name = next(iter(self.network.outputs))
        self.output_shape = self.network.outputs[self.output_name].shape

        self.core = IECore()

        if self.extensions and "CPU" in self.device:
            self.core.add_extension(self.extensions, self.device)

        supported_layers = self.core.query_network(
            network=self.network, device_name=self.device
        )
        unsupported_layers = [
            l for l in self.network.layers.keys() if l not in supported_layers
        ]

        if unsupported_layers:
            log.error(f"Unsupported layers found: {unsupported_layers}")
            exit(1)

        self.exec_network = self.core.load_network(self.network, self.device)

    @timeit
    def predict(self, image):
        """
        This abstractmethod is meant for running predictions on the input image.
        """
        outputs = self.exec_network.infer(
            {self.input_name: self.preprocess_input(image)}
        )
        return self.preprocess_output(outputs, image)

    def check_model(self):
        raise NotImplementedError

    def preprocess_input(self, image):
        """
        Before feeding the data into the model for inference,
        you might have to preprocess it. This function is where you can do that.
        """
        ret = cv2.resize(image, (self.input_shape[3], self.input_shape[2]))
        ret = ret.transpose((2, 0, 1))
        ret = ret.reshape(1, *ret.shape)

        return ret

    @abstractmethod
    def preprocess_output(self, outputs):
        """
        Before feeding the output of this model to the next model,
        you might have to preprocess the output. This function is where you can do that.
        """

        pass


import networkx as nx
import matplotlib.pyplot as plt

def dfs(graph, start):
    visited = set()  # Conjunto para almacenar los nodos visitados
    stack = [start]  # Pila para almacenar los nodos por visitar

    while stack:
        node = stack.pop()  # Extraer el último nodo agregado a la pila

        if node not in visited:
            visited.add(node)
            print(node)  # Imprimir el nodo visitado

            # Agregar los vecinos no visitados a la pila
            neighbors = graph[node]
            stack.extend([neighbor for neighbor in neighbors if neighbor not in visited])

def draw_graph(graph):
    G = nx.Graph()
    for node, neighbors in graph.items():
        for neighbor in neighbors:
            G.add_edge(node, neighbor)

    pos = nx.spring_layout(G)
    nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=500, edge_color='gray', width=1, alpha=0.7)
    plt.show()

# Grafo de ejemplo
graph = {
    'A': ['B', 'C'],
    'B': ['A', 'D', 'E'],
    'C': ['A', 'F'],
    'D': ['B'],
    'E': ['B', 'F'],
    'F': ['C', 'E']
}

# Realizar la búsqueda en profundidad desde el nodo 'A'
dfs(graph, 'A')

# Generar el gráfico del grafo
draw_graph(graph)


"""
Written by:
Dr. Eamon K. Conway
Geospatial Development Center (GDC)
Kostas Research Institute for Homeland Security
Northeastern University

Contact:
e.conway@northeastern.edu

Date:
9/19/2022

DARPA Critical Mineral Challenge 2022

Purpose:
Considers all keywords and fuses nearby keywords together

Args:
keywords,
bboxes
centers

Out:
keywords,
bboxes,
centers

"""

import math
import numpy as np
from tqdm import tqdm
import time

def main(keywords,bboxes,centers,clue_x,clue_y):

            if(abs(int(clue_x))<100):
                clue_x = str(clue_x)[1:3]
            else:
                clue_x = str(clue_x)[1:4]

            clue_y = str(clue_y)[0:2]

            new_keywords = []
            new_bboxes = []
            new_centers=[]
            append = []
            tz = time.time()
            done=False
            success = False
            while done==False:
                # merge keywords if close together
                x = []
                for i in tqdm(range(len(keywords))):
                    tnow = (time.time() - tz)
                    app=True
                    contin = True
                    if(keywords[i]!=''):
                        if(clue_x in keywords[i] or clue_y in keywords[i]):
                            print(keywords[i])
                            top_left_1 = [bboxes[i][0][0],bboxes[i][0][1]]
                            top_right_1 = [bboxes[i][1][0],bboxes[i][0][1]]
                            bot_right_1 = [bboxes[i][1][0],bboxes[i][1][1]]
                            bot_left_1 = [bboxes[i][0][0],bboxes[i][1][1]]
                            tnow = (time.time() - tz)
                            if(tnow > 100):
                                break
                                done=True
                            loop_done = False
                            while loop_done == False:
                                for j in range(len(keywords)):
                                    if(keywords[j]!=''):
                                        if(('scale' in keywords[i]) or ('scale' in keywords[j])):
                                            tol=35
                                        else:
                                            tol=35
                                        top_left_2 = [bboxes[j][0][0],bboxes[j][0][1]]
                                        top_right_2 = [bboxes[j][1][0],bboxes[j][0][1]]
                                        bot_right_2 = [bboxes[j][1][0],bboxes[j][1][1]]
                                        bot_left_2 = [bboxes[j][0][0],bboxes[j][1][1]]
                                        # is box 2 close to box 1 on left side of 1
                                        if(math.isclose(bot_right_2[0],bot_left_1[0],abs_tol=tol) and \
                                          math.isclose(bot_right_2[1],bot_left_1[1],abs_tol=tol) and i!=j and \
clue_x not in keywords[j] and clue_y not in keywords[j]):
                                            app = True
                                            for en in x:
                                                if([j,i] in x):
                                                    app=False
                                            #print('1 ',x,[j,i],app,keywords[j],keywords[i])
                                            if(app==True):
                                                contin=False
                                                new_word = keywords[j]+keywords[i]
                                                #print('nw = ',new_word)
                                                new_keywords.append(new_word)
                                                new_bboxes.append(bboxes[i])
                                                new_centers.append(centers[i])
                                                x.append([j,i])
                                            loop_done=True
                                        # is box 2 close to box 1 on right side of 1
                                        if(math.isclose(bot_right_1[0],bot_left_2[0],abs_tol=tol) and \
                                          math.isclose(bot_right_1[1],bot_left_2[1],abs_tol=tol) and i!=j and \
clue_x not in keywords[j] and clue_y not in keywords[j]):
                                            app = True
                                            for en in x:
                                                if([i,j] in x):
                                                    app=False
                                            #print('2 ',x,[i,j],app,keywords[i],keywords[j])
                                            if(app==True):
                                                contin = False
                                                new_word = keywords[i]+keywords[j]
                                                new_keywords.append(new_word)
                                                new_bboxes.append(bboxes[i])
                                                new_centers.append(centers[i])
                                                x.append([i,j])
                                            loop_done=True
                                    if(j==len(keywords)-1):
                                        loop_done=True
                        if(contin == True):
                                for en in x:
                                    for yn in en:
                                        if(i == yn):
                                            app=False
                                if(app==True):
                                    new_keywords.append(keywords[i])
                                    new_bboxes.append(bboxes[i])
                                    new_centers.append(centers[i])
                if(tnow<=100):
                    success = True
                    done=True
                    print('MergeKeys Successful, Time =  ',(time.time() - tz))
                else:
                    break
                    print('MergeKeys Not Successful, Time =  ',(time.time() - tz))
            if(success==True):
                keywords = new_keywords
                bboxes = new_bboxes
                centers = new_centers

            return keywords,bboxes,centers

if __name__=="__main__":

    fname = 'GEO_0050.txt'
    with open('/scratch/e.conway/DARPA_MAPS/TrainingKeys/'+fname,'r') as f:
        data = f.readlines()
    tot_numbers = []
    for x in data:
        tot_numbers.append(x.split('\n')[0])

    with open('/scratch/e.conway/DARPA_MAPS/TrainingCenters/'+fname,'r') as f:
        data = f.readlines()
    tot_centers = []
    for x in data:
        tot_centers.append([np.float64(x.split(' ')[0]),np.float64(x.split(' ')[1].split('\n')[0])])

    with open('/scratch/e.conway/DARPA_MAPS/TrainingBBoxes/'+fname,'r') as f:
        data = f.readlines()
    tot_bboxes = []
    for x in data:
        tot_bboxes.append([[np.float64(x.split(' ')[0]),np.float64(x.split(' ')[1])],\
                           [np.float64(x.split(' ')[2]),np.float64(x.split(' ')[3].split('\n')[0])]])

    clues = np.genfromtxt('/scratch/e.conway/DARPA_MAPS/CluesTesting/'+fname.split('.txt')[0]+'_clue.csv',delimiter=',')
    clue_x = clues[0]
    clue_y = clues[1]

    keywords,bboxes,centers = main(tot_numbers,tot_bboxes,tot_centers,clue_x,clue_y)

    print('New Keys = ',keywords)
    """
    tot_numbers = ['105','030']
    print('Old Keys = ',tot_numbers)
    tot_num_centers = [[100,505],[120,505]]
    tot_num_boxes = [[[100,500],[110,510]],[[110,510],[120,520]]]
    clue_x = -105.3
    clue_y = 30
    keywords,bboxes,centers = main(tot_numbers,tot_num_boxes,tot_num_centers,clue_x,clue_y)
    print('New Keys = ',keywords)
    """


import nltk
import pickle
from nltk.tokenize import word_tokenize
from nltk.util import ngrams

def process_file(file):

    with open(file, 'r', encoding='utf-8') as f:
        raw_text = f.read()
    f.close()

    unigrams = word_tokenize(raw_text)
    bigrams = list(ngrams(unigrams, 2))

    #dictionaries
    unigram_dict = {t:unigrams.count(t) for t in set(unigrams)}
    bigram_dict = {b:bigrams.count(b) for b in set(bigrams)}

    #tuple
    return bigram_dict, unigram_dict

#-----Main-----
print('processing English..')
eng_bigrams, eng_unigrams = process_file('LangId.train.English')
print('processing Italian..')
ita_bigrams, ita_unigrams = process_file('LangId.train.Italian')
print('processing French..')
fre_bigrams, fre_unigrams = process_file('LangId.train.French')

print('pickling..')
pickle.dump(eng_bigrams, open('eng_bigrams.p', 'wb'))
pickle.dump(eng_unigrams, open('eng_unigrams.p', 'wb'))
pickle.dump(ita_bigrams, open('ita_bigrams.p', 'wb'))
pickle.dump(ita_unigrams, open('ita_unigrams.p', 'wb'))
pickle.dump(fre_bigrams, open('fre_bigrams.p', 'wb'))
pickle.dump(fre_unigrams, open('fre_unigrams.p', 'wb'))
print('done')


# -*- coding: utf-8 -*-
"""
Created on Thu Mar 19 22:45:26 2020

@author: Davide
"""

import numpy as np
import matplotlib.pyplot as plt
import scipy.special as sp
import scipy.misc as spm

#%% Definition of fundamental parameters
x_max = 7 # boundary of the mesh are -x_max, +x_max-h
# definition of a vector with the number of steps for each mesh
N_mesh = 10**2*np.array([1, 4, 8, 10, 50, 80, 100, 400, 700, 1000])#[1, 4, 7, 10, 50, 80, 100, 400, 700, 1000]

E_max = 8 #maximum energy
E_step= 0.09 #step in energy
E=0 #starting energy
acc = 10**(-10) # accuracy in energy

eig_0 = np.zeros((len(N_mesh), 3)) #initialize a vector where we write the eigenvalues for l=0
eig_1 = np.zeros((len(N_mesh), 3)) #initialize a vector where we write the eigenvalues for l=0
eig_2 = np.zeros((len(N_mesh), 3)) #initialize a vector where we write the eigenvalues for l=0
y_eig_0 = np.zeros((len(N_mesh), 3, np.max(N_mesh))) #initialize a vector where we write the eigenfunctions with l=0
y_eig_1 = np.zeros((len(N_mesh), 3, np.max(N_mesh))) #initialize a vector where we write the eigenfunctions with l=1
y_eig_2 = np.zeros((len(N_mesh), 3, np.max(N_mesh))) #initialize a vector where we write the eigenfunctions with l=2

#%% Definition of useful functions

# Function that define the k^2 parameter in numerov for 3D ho
def K2(energy,position,spin):
    return 2*energy-position**2- spin*(spin+1)/position**2

# Numerov algorithm, give as output a vector
def numerov(energy,spin,position,step):
    y_p = np.ones(len(position))
    K2_E = K2(energy,position,spin) #evaluate the k^2 parameter
    i=2
    y_p[0] =(h)**(spin+1) #initial point, at infinity y=0
    y_p[1] = (2*h)**(spin+1) #second point, correct derivative to be set with normalization
    while i<len(position):
        y_p[i]= (2*y_p[i-1]*(1-5/12*step**2 *K2_E[i-1])-y_p[i-2]*(1+step**2 /12*K2_E[i-2]))/(1+step**2/12*K2_E[i])
        i += 1

    return y_p

#%% Main body
for i in range(len(N_mesh)):
    print(i, "out of", len(N_mesh))
    N = N_mesh[i]                           # define the number of steps in this mesh
    h = x_max/N                          # define the step
    x= (np.array(range(N)))*h + h          #define the mesh

    for ell in range(3):
        E = 0                                   # reset energy at zero
        found_E = 0                             # reset found_E
        y_0 = numerov(E,ell,x,h)                  # calculate the function on initial energy
        control_0 = np.sign(y_0[N-1])           # where does it diverge at +infinity
        while E<E_max:                          # find eigenvalues by requiring y(+infinity)=0
            y = numerov(E,ell,x,h)                # compute y
            control_1 = np.sign(y[N-1])         # where does it diverge at +infinity

            if control_0 != control_1 : #if the sign changes then y(+inifinity)=0 has just passed

                #initialize variables for tangent method
                E_0 = E-E_step
                y_0 = numerov(E-E_step,ell,x,h)
                E_1 = E
                y_1 = y
                delta = 1;
                #tangent method
                while delta > acc:

                    E_2 = E_0 - y_0[N-1]*(E_1-E_0)/(y_1[N-1]-y_0[N-1])

                    y_2 = numerov(E_2,ell,x,h)

                    control_2 = np.sign(y_2[N-1])

                    if control_2 == control_1:
                        E_1 = E_2;
                        y_1=y_2
                        delta = E_1-E_0
                    else:
                        E_0 = E_2
                        y_0 =y_2
                        delta = E_1-E_0

                #compute eigenfunction
                y_new = numerov(np.mean([E_0, E_1]),ell,x,h)
                norm = (np.dot(y_new[1:(N-1)],y_new[1:(N-1)]) + (y_new[0]**2 +y_new[N-1]**2)/2)*h
                y_new =  y_new/np.sqrt(norm)
                #copy eigenfunction and eigenvalue
                if ell==0:
                    y_eig_0[i, found_E, :] = np.resize(y_new,(1,np.max(N_mesh)))
                    eig_0[i, found_E] = np.mean([E_0, E_1])
                if ell==1:
                    y_eig_1[i, found_E, :] = np.resize(y_new,(1,np.max(N_mesh)))
                    eig_1[i, found_E] = np.mean([E_0, E_1])
                if ell==2:
                    y_eig_2[i, found_E, :] = np.resize(y_new,(1,np.max(N_mesh)))
                    eig_2[i, found_E] = np.mean([E_0, E_1])

                #increase the counter found_E
                found_E += 1
                if found_E>2:
                    E = 10

                #plot the eigenfunction
                plt.plot(x,y_new/x)

            #increase energy and new sign to be checked
            E += E_step
            y_0 = y
            control_0 = np.sign(y_0[N-1])

#%% plot the relative difference between the computed and exact eigenvalue.
plt.figure()
ax = plt.gca()
ax.grid(True)
ax.set_xscale("Log")
ax.set_yscale("Log")
#plot with l=0
colors=["#58e201ff", "#01bd17ff", "#007a0e"]
for i in range(eig_0.shape[1]):
    t, = plt.loglog(N_mesh, np.abs(eig_0[:,i]-(2*i+1.5))/(2*i+1.5), marker = ".", markersize = 6, color=colors[i])
    t.set_label("n="+str(2*i)+"; l=0")
#plot with l=1
colors=["#21b2b4ff", "#0055ffff", "#00007fff"]
for i in range(eig_1.shape[1]):
    t, = plt.loglog(N_mesh, np.abs(eig_1[:,i]-((2*i+1)+1.5))/((2*i+1)+1.5), marker = "x", markersize = 6, color=colors[i])
    t.set_label("n="+str(2*i+1)+"; l=1")
#plot with l=2
colors=["#ff5500ff", "#ff0000", "#aa0000ff"]
for i in range(eig_2.shape[1]):
    t, = plt.loglog(N_mesh, np.abs(eig_2[:,i]-(2*(i+1)+1.5))/(2*(i+1)+1.5), marker = "*", markersize = 6, color=colors[i])
    t.set_label("n="+str(2*(i+1))+"; l=2")
ax.legend()
plt.errorbar(N_mesh, np.abs(eig_0[:,1]-(3+0.5))/(3+0.5), yerr=acc/2/(3+0.5)*np.ones(len(N_mesh)), fmt = "none", ecolor = "green")
plt.xlabel("Number of point in the mesh")
plt.ylabel("Relative difference between eigenvalues")
plt.show()

#%% plot of the wavefunctions with N = N_mesh[7] and l=0
N = N_mesh[7]
h = x_max/N
x= (np.array(range(N)))*h + h
plt.figure()
ax = plt.gca()
ax.grid(True)
colors=["#58e201ff", "#01bd17ff", "#007a0e"]
for j in range(3): #loop on the energies
    t,= plt.plot(x, y_eig_0[7,j,:N_mesh[7]]/x, color=colors[j])
    t.set_label("n="+str(2*j)+"; l=0")
ax.legend()
plt.xlabel("Position")
plt.ylabel("Wavefunction amplitude")
plt.show()

#%% plot of the wavefunctions with N = N_mesh[7] and l=1
N = N_mesh[7]
h = x_max/N
x= (np.array(range(N)))*h + h
plt.figure()
ax = plt.gca()
ax.grid(True)
colors=["#21b2b4ff", "#0055ffff", "#00007fff"]
for j in range(3): #loop on the energies
    t,= plt.plot(x, y_eig_1[7,j,:N_mesh[7]]/x, color=colors[j])
    t.set_label("n="+str(2*j+1)+"; l=1")
ax.legend()
plt.xlabel("Position")
plt.ylabel("Wavefunction amplitude")
plt.show()

#%% plot of the wavefunctions with N = N_mesh[7] and l=2
N = N_mesh[7]
h = x_max/N
x= (np.array(range(N)))*h + h
plt.figure()
ax = plt.gca()
ax.grid(True)
colors=["#ff5500ff", "#ff0000", "#aa0000ff"]
for j in range(3): #loop on the energies
    t,= plt.plot(x, y_eig_2[7,j,:N_mesh[7]]/x, color=colors[j])
    t.set_label("n="+str(2*(j+1))+"; l=2")
ax.legend()
plt.xlabel("Position")
plt.ylabel("Wavefunction amplitude")
plt.show()

#%% Plot of the eigenvalue with N = N_mesh[7]
plt.figure()
ax = plt.gca()
ax.grid(True)
t, = plt.plot(2*np.array(range(3)), eig_0[7,:], marker = "*", markersize = 10, color="#01bd17ff")
t.set_label("Eigenvalues for l=0")
t, = plt.plot(1+2*np.array(range(3)), eig_1[7,:], marker = "+", markersize = 10, color="#0055ffff")
t.set_label("Eigenvalues for l=1")
t, = plt.plot(2*np.array(range(3))+2, eig_2[7,:], marker = ".", markersize = 6, color="#ff0000")
t.set_label("Eigenvalues for l=2")
ax.legend()
plt.xlabel("Quantum number n")
plt.ylabel("Energy")
plt.show()

#%% plot the relative difference between the computed eigenfunction and the exact one
y_exact_0 = np.zeros((3,N_mesh[7]))
y_exact_1 = np.zeros((3,N_mesh[7]))
y_exact_2 = np.zeros((3,N_mesh[7]))
pi = np.arctan(1)*4
y_exact_0[0,:] = np.exp(-1/2*x**2)*sp.eval_genlaguerre(0,1/2,x**2)*np.sqrt( 1/np.sqrt(4*pi)*2**(0+2*0+3)*sp.factorial(0)/(2**0)/sp.factorial2(2*0+2*0+1) )
y_exact_0[1,:] = np.exp(-1/2*x**2)*sp.eval_genlaguerre(1,1/2,x**2)*np.sqrt( 1/np.sqrt(4*pi)*2**(1+2*0+3)*sp.factorial(1)/(2**0)/sp.factorial2(2*1+2*0+1) )
y_exact_0[2,:] = np.exp(-1/2*x**2)*sp.eval_genlaguerre(2,1/2,x**2)*np.sqrt( 1/np.sqrt(4*pi)*2**(2+2*0+3)*sp.factorial(2)/(2**0)/sp.factorial2(2*2+2*0+1) )
y_exact_1[0,:] = x**1*np.exp(-1/2*x**2)*sp.eval_genlaguerre(0,3/2,x**2)*np.sqrt( 1/np.sqrt(4*pi)*2**(0+2*1+3)*sp.factorial(0)/(2**1)/sp.factorial2(2*0+2*1+1) )
y_exact_1[1,:] = x**1*np.exp(-1/2*x**2)*sp.eval_genlaguerre(1,3/2,x**2)*np.sqrt( 1/np.sqrt(4*pi)*2**(1+2*1+3)*sp.factorial(1)/(2**1)/sp.factorial2(2*1+2*1+1) )
y_exact_1[2,:] = x**1*np.exp(-1/2*x**2)*sp.eval_genlaguerre(2,3/2,x**2)*np.sqrt( 1/np.sqrt(4*pi)*2**(2+2*1+3)*sp.factorial(2)/(2**1)/sp.factorial2(2*2+2*1+1) )
y_exact_2[0,:] = x**2*np.exp(-1/2*x**2)*sp.eval_genlaguerre(0,5/2,x**2)*np.sqrt( 1/np.sqrt(4*pi)*2**(0+2*2+3)*sp.factorial(0)/(2**2)/sp.factorial2(2*0+2*2+1) )
y_exact_2[1,:] = x**2*np.exp(-1/2*x**2)*sp.eval_genlaguerre(1,5/2,x**2)*np.sqrt( 1/np.sqrt(4*pi)*2**(1+2*2+3)*sp.factorial(1)/(2**2)/sp.factorial2(2*1+2*2+1) )
y_exact_2[2,:] = x**2*np.exp(-1/2*x**2)*sp.eval_genlaguerre(2,5/2,x**2)*np.sqrt( 1/np.sqrt(4*pi)*2**(2+2*2+3)*sp.factorial(2)/(2**2)/sp.factorial2(2*2+2*2+1) )
plt.figure()
ax = plt.gca()
ax.grid(True)
#plot with l=0
colors=["#58e201ff", "#01bd17ff", "#007a0e"]
for j in range(3): #loop on the energies
    t,= plt.semilogy(x, abs((y_eig_0[7,j,:N_mesh[7]]/x-y_exact_0[j,:])/y_exact_0[j,:]), color=colors[j])#y_eig[6,j,:N_mesh[6]+1]-
    t.set_label("n="+str(2*j)+"; l=0")
#plot with l=1
colors=["#21b2b4ff", "#0055ffff", "#00007fff"]
for j in range(3): #loop on the energies
    t,= plt.semilogy(x, abs((y_eig_1[7,j,:N_mesh[7]]/x-y_exact_1[j,:])/y_exact_1[j,:]), color=colors[j])#y_eig[6,j,:N_mesh[6]+1]-
    t.set_label("n="+str(2*j+1)+"; l=1")
#plot with l=2
colors=["#ff5500ff", "#ff0000", "#aa0000ff"]
for j in range(3): #loop on the energies
    t,= plt.semilogy(x, abs(((y_eig_2[7,j,:N_mesh[7]]/x-y_exact_2[j,:]))/y_exact_2[j,:]), color=colors[j])#y_eig[6,j,:N_mesh[6]+1]-
    t.set_label("n="+str(2*(j+1))+"; l=2")
    ax.legend()
plt.xlabel("Position")
plt.ylabel("Wavefunction amplitude")
plt.show()

#%% plot the difference between the computed eigenfunction and the exact one
plt.figure()
ax = plt.gca()
ax.grid(True)
#plot with l=0
colors=["#58e201ff", "#01bd17ff", "#007a0e"]
for j in range(3): #loop on the energies
    t,= plt.semilogy(x, abs((y_eig_0[7,j,:N_mesh[7]]/x-y_exact_0[j,:])), color=colors[j])#y_eig[6,j,:N_mesh[6]+1]-
    t.set_label("n="+str(2*j)+"; l=0")
#plot with l=1
colors=["#21b2b4ff", "#0055ffff", "#00007fff"]
for j in range(3): #loop on the energies
    t,= plt.semilogy(x, abs((y_eig_1[7,j,:N_mesh[7]]/x-y_exact_1[j,:])), color=colors[j])#y_eig[6,j,:N_mesh[6]+1]-
    t.set_label("n="+str(2*j+1)+"; l=1")
#plot with l=2
colors=["#ff5500ff", "#ff0000", "#aa0000ff"]
for j in range(3): #loop on the energies
    t,= plt.semilogy(x, abs(((y_eig_2[7,j,:N_mesh[7]]/x-y_exact_2[j,:]))), color=colors[j])#y_eig[6,j,:N_mesh[6]+1]-
    t.set_label("n="+str(2*(j+1))+"; l=2")
    ax.legend()
plt.xlabel("Position")
plt.ylabel("Wavefunction amplitude")
plt.show()


import argparse
import model
import json
import os

import tensorflow as tf
from tensorflow.contrib.learn.python.learn import learn_runner

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--traindata',
      help='Training data can have wildcards',
      required=True
  )
  parser.add_argument(
      '--evaldata',
      help='Training data can have wildcards',
      required=True
  )
  parser.add_argument(
      '--job-dir',
      help='this model ignores this field, but it is required by gcloud',
      default='./junk'
  )
  parser.add_argument(
      '--output_dir',
      help='Output directory',
      required=True
  )
  parser.add_argument(
      '--num_training_epochs',
      help='Number of passes through training dataset',
      type=int,
      default=10
  )

  # for hyper-parameter tuning
  parser.add_argument(
      '--batch_size',
      help='Number of examples to compute gradient on',
      type=int,
      default=100
  )
  parser.add_argument(
      '--nbuckets',
      help='Number of bins into which to discretize lats and lons',
      type=int,
      default=5
  )
  parser.add_argument(
      '--hidden_units',
      help='Architecture of DNN part of wide-and-deep network',
      default='64,64,64,16,4'
  )
  parser.add_argument(
      '--learning_rate',
      help='Controls size of step in gradient descent.',
      type=float,
      default=0.0606
  )

  # parse args
  args = parser.parse_args()
  arguments = args.__dict__

  # unused args provided by service
  arguments.pop('job-dir', None)
  arguments.pop('job_dir', None)
  output_dir = arguments.pop('output_dir')

  # when hp-tuning, we need to use different output directories for different runs
  output_dir = os.path.join(
      output_dir,
      json.loads(
          os.environ.get('TF_CONFIG', '{}')
      ).get('task', {}).get('trial', '')
  )

  # run
  tf.logging.set_verbosity(tf.logging.INFO)
  learn_runner.run(model.make_experiment_fn(**arguments), output_dir)


from AMMMGlobals import AMMMException

class ValidateConfig(object):
    # Validate config attributes read from a DAT file.

    @staticmethod
    def validate(data):
        # Validate that mandatory input parameters were found
        paramList = ['instancesDirectory', 'fileNamePrefix', 'fileNameExtension', 'numInstances',
                      'n', 'm']
        for paramName in paramList:
            if paramName not in data.__dict__:
                raise AMMMException('Parameter(%s) has not been not specified in Configuration' % str(paramName))

        instancesDirectory = data.instancesDirectory
        if len(instancesDirectory) == 0: raise AMMMException('Value for instancesDirectory is empty')

        fileNamePrefix = data.fileNamePrefix
        if len(fileNamePrefix) == 0: raise AMMMException('Value for fileNamePrefix is empty')

        fileNameExtension = data.fileNameExtension
        if len(fileNameExtension) == 0: raise AMMMException('Value for fileNameExtension is empty')

        numInstances = data.numInstances
        if not isinstance(numInstances, int) or (numInstances <= 0):
            raise AMMMException('numInstances(%s) has to be a positive integer value.' % str(numInstances))

        n = data.n
        if not isinstance(n, int) or (n <= 0):
            raise AMMMException('n(%s), number of codes, has to be a positive integer value.' % str(n))

        m = data.m
        if not isinstance(m, int) or (m <= 0):
            raise AMMMException('m(%s), length of the codes, has to be a positive integer value.' % str(m))

        if pow(2, m) < n:
            raise AMMMException('m to small to generate codes.')


from matrix import Matrix
from gauss import gauss

def function(vector: Matrix) -> Matrix:
    x, y = vector[0][0], vector[0][1]

    return  Matrix(matrix=[
        [x * x - y * y + 3 * y],
        [x * x + 3 * x * y + 2 * y * y + 2 * x + 4 * y]
    ])

def jacobian(vector: Matrix) -> Matrix:
    x, y = vector[0][0], vector[0][1]

    return Matrix(matrix=[
        [2 * x, 2 * x + 3 * y + 2],
        [-2 * y + 3, 3 * x + 4 * y + 4]
    ])

def calculate_diff(x_1: Matrix, x_2: Matrix) -> float:

    x = x_1 - x_2
    result = 0

    for elem in x[0]:
        result += elem ** 2

    result = result ** 1/2

    return result

def newton_method(x_0: Matrix, tolerance: float = 1e-6) -> list:
    x_prev = x_0
    x_next = Matrix(1, x_0.n)
    iteration = 0
    while True:
        iteration += 1
        F = function(x_prev)
        DF = jacobian(x_prev)
        right_sight = [x[0] * (-1) for x in F()]

        DF.add_column(right_sight)
        z = gauss(2, DF)

        x_next = x_prev + z

        if calculate_diff(x_next, x_prev) < tolerance:
            break

        print(f'Iteration: {iteration}, current solution: {x_next}')
        x_prev = x_next

    print('Solution')
    return x_next

print(newton_method(Matrix(matrix=[[1.5, -0.5]])))


import random
import os
import subprocess
import sys

def split_data_set(image_dir, label):
    f_val = open("cooldrink_test.txt", 'a')
    f_train = open("cooldrink_train.txt", 'a')

    files = os.listdir(image_dir)
    data_size = len(files)

    ind = 0
    data_test_size = int(0.1 * data_size)
    test_array = random.sample(range(data_size), k=data_test_size)

    for f in os.listdir(image_dir):
        if(f.split(".")[1] == "jpg"):
            ind += 1

            if ind in test_array:
                f_val.write(image_dir+'/'+f+' '+ label +'\n')
            else:
                f_train.write(image_dir+'/'+f+' '+ label +'\n')

classes = ['appy', 'appyfizz', 'fanta', 'sprite']
for cl in classes:
    split_data_set('training images/' + cl, cl)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Auction',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('auction_start_date', models.DateTimeField(auto_now_add=True)),
                ('auction_end_date', models.DateTimeField()),
                ('opening_bid', models.DecimalField(default=1.0, max_digits=7, decimal_places=2)),
                ('price', models.DecimalField(default=0.0, max_digits=7, decimal_places=2)),
                ('increment', models.DecimalField(default=1.0, max_digits=7, decimal_places=2)),
                ('auction_ended', models.BooleanField(default=False)),
            ],
        ),
        migrations.CreateModel(
            name='Bid',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('bid_amount', models.DecimalField(default=1.0, max_digits=7, decimal_places=2)),
                ('auction', models.ForeignKey(to='auction.Auction')),
                ('bidder', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'ordering': ['bid_amount'],
            },
        ),
        migrations.CreateModel(
            name='Item',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=255)),
                ('seller', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.AddField(
            model_name='auction',
            name='item',
            field=models.ForeignKey(to='auction.Item'),
        ),
    ]


"""
The evaluator.py module contains functions
that return calculated data for an aircraft.
Plotting aircraft components is also possible.
"""

import os.path
import concurrent.futures
import matplotlib.pyplot as plt

from . import drag, inertia, lift, mass

def analyze(aircraft):
    """Analyze a single aircraft."""
    results = {
        'Lift': lift.get_lift_total(aircraft),
        'Drag': drag.get_drag_total(aircraft),
        'Mass': mass.get_mass_total(aircraft),
        'Centroid': inertia.get_centroid(aircraft)
    }
    aircraft.results = results
    return aircraft.name, results

def analyze_all(population):
    """Analyze all aircraft in a given population."""
    # for aircraft in population.aircrafts:
    #     print(analyze(aircraft))
    with concurrent.futures.ProcessPoolExecutor() as executor:
        results = executor.map(analyze, population.aircrafts)
        for result in results:
            print(result)
    return None

    # def analysis(self, V_x, V_z):
    #     """Perform all analysis calculations and store in class instance."""

    #     self.drag = self.get_drag(10)
    #     self.lift_rectangular = self.get_lift_rectangular(13.7)
    #     self.lift_elliptical = self.get_lift_elliptical(15)
    #     self.lift_total = self.get_lift_total()
    #     self.mass_dist = self.get_mass_distribution(self.mass_total)
    #     self.centroid = self.get_centroid()
    #     self.I_['x'] = self.get_inertia_terms()[0]
    #     self.I_['z'] = self.get_inertia_terms()[1]
    #     self.I_['xz'] = self.get_inertia_terms()[2]
    #     spar_dx = self.get_dx(self.spar)
    #     spar_dz = self.get_dz(self.spar)
    #     self.spar.dP_x = self.get_dP(spar_dx, spar_dz, V_x, 0,
    #                                  self.spar.cap_area)
    #     self.spar.dP_z = self.get_dP(spar_dx, spar_dz, 0, V_z,
    #                                  self.spar.cap_area)
    #     print("yayyyyy")
    #     return None

    # print(f"Analysis results for {aircraft.name}:\n", results)
    # self.results = self.get_lift_total(aircraft)

    # self.drag = self.get_drag(10)
    # self.lift_rectangular = self.get_lift_rectangular(13.7)
    # self.lift_elliptical = self.get_lift_elliptical(15)
    # self.lift_total = self.get_lift_total()
    # self.mass_dist = self.get_mass_distribution(self.mass_total)
    # self.centroid = self.get_centroid()
    # self.I_['x'] = self.get_inertia_terms()[0]
    # self.I_['z'] = self.get_inertia_terms()[1]
    # self.I_['xz'] = self.get_inertia_terms()[2]
    # spar_dx = self.get_dx(self.spar)
    # spar_dz = self.get_dz(self.spar)
    # self.spar.dP_x = self.get_dP(spar_dx, spar_dz, V_x, 0,
    #                              self.spar.cap_area)
    # self.spar.dP_z = self.get_dP(spar_dx, spar_dz, 0, V_z,
    #                              self.spar.cap_area)
    # return None

    def tree_print(self, population):
        """Print the list of subcomponents."""
        name = f"    TREE FOR {[i.name for i in population.aircraft]} IN {self.name}    "
        num_of_dashes = len(name)
        print(num_of_dashes * '-')
        print(name)
        for aircraft in population:
            print(".")
            print(f"`-- {aircraft}")
            print(f"    |--{aircraft.wing}")
            print(f"    |   |-- {aircraft.wing.stringers}")
            for spar in aircraft.wing.spars[:-1]:
                print(f"    |   |-- {spar}")
            print(f"    |   `-- {aircraft.wing.spars[-1]}")
            print(f"    |-- {aircraft.fuselage}")
            print(f"    `-- {aircraft.propulsion}")
        print(num_of_dashes * '-')
        return None

    def tree_save(self,
                  population,
                  save_path='/home/blendux/Projects/Aircraft_Studio/save'):
        """Save the evaluator's tree to a file."""
        for aircraft in population.aircraft:
            file_name = f"{aircraft.name}_tree.txt"
            full_path = os.path.join(save_path, file_name)
            with open(full_path, 'w') as f:
                try:
                    f.write(".\n")
                    f.write(f"`-- {aircraft}\n")
                    f.write(f"    |--{aircraft.wing}\n")
                    for spar in aircraft.wing.spars[:-1]:
                        f.write(f"    |   |-- {spar}\n")
                    f.write(f"    |   `-- {aircraft.wing.spars[-1]}\n")
                    f.write(f"    |-- {aircraft.fuselage}\n")
                    f.write(f"    `-- {aircraft.propulsion}\n")
                    logging.debug(f'Successfully wrote to file {full_path}')

                except IOError:
                    print(
                        f'Unable to write {file_name} to specified directory.',
                        'Was the full path passed to the function?')
        return None

def plot_geom(evaluator):
    """This function plots analysis results over the airfoil's geometry."""
    # Plot chord
    x_chord = [0, evaluator.chord]
    y_chord = [0, 0]
    plt.plot(x_chord, y_chord, linewidth='1')
    # Plot quarter chord
    plt.plot(evaluator.chord / 4,
             0,
             '.',
             color='g',
             markersize=24,
             label='Quarter-chord')
    # Plot airfoil surfaces
    x = [0.98 * x for x in evaluator.airfoil.x]
    y = [0.98 * z for z in evaluator.airfoil.z]
    plt.fill(x, y, color='w', linewidth='1', fill=False)
    x = [1.02 * x for x in evaluator.airfoil.x]
    y = [1.02 * z for z in evaluator.airfoil.z]
    plt.fill(x, y, color='b', linewidth='1', fill=False)

    # Plot spars
    try:
        for _ in range(len(evaluator.spar.x)):
            x = (evaluator.spar.x[_])
            y = (evaluator.spar.z[_])
            plt.plot(x, y, '-', color='b')
    except AttributeError:
        print('No spars to plot.')
    # Plot stringers
    try:
        for _ in range(0, len(evaluator.stringer.x)):
            x = evaluator.stringer.x[_]
            y = evaluator.stringer.z[_]
            plt.plot(x, y, '.', color='y', markersize=12)
    except AttributeError:
        print('No stringers to plot.')

    # Plot centroid
    x = evaluator.centroid[0]
    y = evaluator.centroid[1]
    plt.plot(x, y, '.', color='r', markersize=24, label='centroid')

    # Graph formatting
    plt.xlabel('X axis')
    plt.ylabel('Z axis')

    plot_bound = max(evaluator.airfoil.x)
    plt.xlim(-0.10 * plot_bound, 1.10 * plot_bound)
    plt.ylim(-(1.10 * plot_bound / 2), (1.10 * plot_bound / 2))
    plt.gca().set_aspect('equal', adjustable='box')
    plt.gca().legend()
    plt.grid(axis='both', linestyle=':', linewidth=1)
    plt.show()
    return None

def plot_lift(evaluator):
    x = range(evaluator.semi_span)
    y_1 = evaluator.lift_rectangular
    y_2 = evaluator.lift_elliptical
    y_3 = evaluator.lift_total
    plt.plot(x, y_1, '.', color='b', markersize=4, label='Rectangular lift')
    plt.plot(x, y_2, '.', color='g', markersize=4, label='Elliptical lift')
    plt.plot(x, y_3, '.', color='r', markersize=4, label='Total lift')

    # Graph formatting
    plt.xlabel('Semi-span location')
    plt.ylabel('Lift')

    plt.gca().legend()
    plt.grid(axis='both', linestyle=':', linewidth=1)
    plt.show()
    return None


import re
if __package__:
    from scripts.intent import *
    from scripts.answer_dictionary import DEFAULT,INIT_GREETINGS,SHOW_LIST
else:
    from intent import *
    from answer_dictionary import DEFAULT,INIT_GREETINGS,SHOW_LIST

input_intents=['keyword','source','domain']
sort_intents=['relevant','popular']
KEYWORD_NER=True

import spacy
nlp=spacy.load('en_core_web_md')

def match_date(message):
    connections=['\.','/','-']
    l=['([0-9]{4})','([0-9]{1,2})','([0-9]{1,2})']
    pattern_string=['('+conn.join(l)+')' for conn in connections]
    pattern_string='|'.join(pattern_string)
    date_pattern=re.compile(pattern_string)
    return date_pattern.search(message)

def match_num(message):
    pattern=re.compile('[1-9]')
    return pattern.match(message)

def interpret(message,last_state):
    words=message.split()
    if 'news' in words:
        return 'read_news',None
    date=match_date(message)
    if date:
        if last_state==INPUT:
            return 'date',date.group()
        elif last_state==GIVEN:
            return 'date_sort',date.group()
        else:
            return 'none',None
    if last_state==INPUT:
        if KEYWORD_NER:
            intent='keyword'
            doc=nlp(message)
            if doc.ents:
                addition=doc.ents[-1]
                return intent,addition
        else:
            for input_intent in input_intents:
                if input_intent in words:
                    index=words.index(input_intent)
                    addition=words[index+1]
                    return input_intent,addition
    for sort_intent in sort_intents:
        if sort_intent in words:
            return sort_intent,None
    num=match_num(message)
    if last_state==GIVEN and num:
        return 'choose',int(num.group(0))
    return 'none',None

def respond(message,state,search_sequence,policy=policy_rules):
    intent,addition=interpret(message,state)
    print('get intent:'+intent+' state is:'+str(state))
    if (state,intent) in policy:
        new_state,answer=policy[(state,intent)]
        if new_state in GIVEN_STATES:
            assert hasattr(answer,'__call__')
            answer=answer(intent,addition)
            assert type(answer)==list
            #print(answer)
            answer=format_list(answer)
            #print(answer)
            search_sequence=intent,addition
        if new_state in SORTED_STATES:
            assert hasattr(answer,'__call__')
            (search_intent,search_addition)=search_sequence
            answer=answer(search_intent,search_addition,intent,addition)
            assert type(answer)==list
            answer=format_list(answer)
        if new_state==GIVEN_CHOOSE:
            assert hasattr(answer,'__call__')
            (search_intent,search_addition)=search_sequence
            answer=answer(search_intent,search_addition,return_type='url',num=addition)
        if (new_state,None) in policy:
            new_state,_=policy[new_state,None]
        return answer,new_state,search_sequence
    else:
        return DEFAULT[0],state,search_sequence

def format_list(title_list):
    return SHOW_LIST[0].format('
'.join(['"'+title+'"' for title in title_list]))

def start():
    state=0
    answer=INIT_GREETINGS[0]
    search_sequence=('','')
    return answer,state,search_sequence

def bot_template(answer):
    if type(answer)==str:
        print('bot:{0}'.format(answer))
    else:
        print('bot:{0}'.format(''))

if __name__=='__main__':
    from generate import read,sort
    TEST_TYPE=1
    user_template='user:{0}'
    bot_template='bot:{0}'
    answer,state,search_sequence=start()
    if TEST_TYPE==0:
        message=''
        while(message!='\n'):
            print(bot_template.format(answer))
            message=input('user:')
            answer,state,search_sequence=respond(message,state,search_sequence)
    if TEST_TYPE==1:
        messages=['hi, I want to read some news','keyword France']
        print(bot_template.format(answer))
        for message in messages:
            print(user_template.format(message))
            answer,state,search_sequence=respond(message,state,search_sequence)
            print(bot_template.format(answer))


#реализация хеширования цепочками
def make_hash(text):
    x = 263
    p = 10**9 + 7
    hf = 0
    for i in range(len(text)):
        hf += (ord(text[i])*x**i) % p
    hf = hf % p
    return hf

m = int(input())
table = []
for i in range(m):
    table.append([])

n = int(input())
recs = []
for i in range(n):
    com = input().split()

    if com[0] == 'add':
        hf = make_hash(com[1]) % m
        if len(table[hf]) != 0:
            if com[1] not in table[hf]:
                table[hf].insert(0, com[1])
        else:
            table[hf].append(com[1])

    elif com[0] == 'del':
        hf = make_hash(com[1]) % m
        if len(table[hf]) != 0:
            if com[1] in table[hf]:
                table[hf].remove(com[1])

    elif com[0] == 'find':
        hf = make_hash(com[1]) % m
        if len(table[hf]) != 0:
            if com[1] in table[hf]:
                recs.append('yes')
            else:
                recs.append('no')
        else:
            recs.append('no')

    elif com[0] == 'check':
        if len(table[int(com[1])]) != 0:
            recs.append(' '.join(table[int(com[1])]))
        else:
            recs.append('')

for i in recs:
    print(i)


from Crypto.Cipher import AES
import base64
BS = AES.block_size

def pad(s):
    return s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
#定义 padding 即 填充 为PKCS7

def unpad(s):
    return s[0:-ord(s[-1])]

class prpcrypt():
    def __init__(self, key):
        self.key = key
        self.mode = AES.MODE_CBC
    # AES的加密模式为CBC
    def encrypt(self, text):
        text = pad(text)
        cryptor = AES.new(self.key, self.mode, self.key)
        #第二个self.key 为 IV 即偏移量
        x = len(text) % 8
        if x != 0:
            text = text + '\0' * (8 - x)  # 不满16，32，64位补0
        print(text)
        self.ciphertext = cryptor.encrypt(text)
        return base64.standard_b64encode(self.ciphertext).decode("utf-8")

    def decrypt(self, text):
        cryptor = AES.new(self.key, self.mode, self.key)
        de_text = base64.standard_b64decode(text)
        plain_text = cryptor.decrypt(de_text)
        st = str(plain_text.decode("utf-8")).rstrip('\0')
        out = unpad(st)
        return out

pc = prpcrypt('ningbozhihuirend')  # 自己设定的密钥
e = pc.encrypt("hello")  # 加密内容
d = pc.decrypt(e)
print("加密后%s,解密后%s" % (e, d))


from django.db.models import Model, ManyToManyField, ForeignKey, CASCADE, CharField, BooleanField, DateTimeField#, DateField, TimeField
from django.conf import settings
from gallery.models import Gallery
#from medium.models import Medium
AUTH_USER_MODEL=settings.AUTH_USER_MODEL

class GoodsExchange(Model):
	#launcher=ForeignKey(AUTH_USER_MODEL, related_name='launcher_goodsexchange', on_delete=CASCADE)
	gallery=ForeignKey(Gallery, related_name='gallery_goodsexchange', on_delete=CASCADE)
	ge2ge=ForeignKey('self', related_name='goodsexchange_goodsexchange', null=True, on_delete=CASCADE)
	coordinate=CharField(max_length=30, null=True)
	datetime=DateTimeField(null=True)
	isDealt=BooleanField(default=False)
	timestamp=DateTimeField(auto_now=True)
	class Meta:
		db_table='goodsexchange'


from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns

from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from webservices import views
from rest_framework.documentation import include_docs_urls

GET_SAMPLE_INGESTION = url(
    regex=r'^sample-ingestion/(?P<name_and_gene_collection>[A-za-z0-9\-_]+)/$',
    view=views.SampleIngestionViewSet.as_view({'get': 'retrieve', 'delete': 'destroy'}),
    name='get-sample-ingestion'
)

LIST_CREATE_SAMPLE_INGESTION = url(
    regex=r'^sample-ingestion/$',
    view=views.SampleIngestionViewSet.as_view({'get': 'list', 'post': 'create'}),
    name='list-sample-ingestion'
)

LIST_GENE_COVERAGE = url(
    regex=r'^gene-coverage/$',
    view=views.GeneCoverageView.as_view({'post': 'list'}),
    name='gene-coverage-list'
)

GET_GENE_COVERAGE = url(
    regex=r'^gene-coverage/(?P<sample_name>[A-za-z0-9\-_]+)/(?P<gene_collection>[A-za-z0-9\-_]+)/(?P<gene>[A-za-z0-9\-_]+)/$',
    view=views.GeneCoverageView.as_view({'get': 'retrieve'}),
    name='gene-coverage-detail'
)

LIST_SAMPLE_METRICS = url(
    regex=r'^sample-metrics/$',
    view=views.SampleMetricsView.as_view({'post': 'list'}),
    name='sample-metrics-list'
)

GET_SAMPLE_METRICS = url(
    regex=r'^sample-metrics/(?P<sample_name>[A-za-z0-9\-_]+)/(?P<gene_collection>[A-za-z0-9\-_]+)$',
    view=views.SampleMetricsView.as_view({'get': 'retrieve'}),
    name='sample-metrics-detail'
)

schema_view = get_schema_view(
   openapi.Info(
      title="Calypso",
      default_version='v1',
      description="Calypso is a service to store and analyse coverage metrics from whole genome sequences",
   ),
   public=True,
)

urls = [LIST_CREATE_SAMPLE_INGESTION,
        GET_SAMPLE_INGESTION,
        GET_GENE_COVERAGE,
        LIST_GENE_COVERAGE,
        GET_SAMPLE_METRICS,
        LIST_SAMPLE_METRICS,
        url(r'^docs/$', schema_view.with_ui('swagger', cache_timeout=None), name='schema-swagger-ui')
        ]

urlpatterns = format_suffix_patterns(urls)


""""

This file is part of the suite of provided scripts to assist in summarising and presenting the training and validation statistics
from the granular-tags tool.

Run summarise.py with relevant command line arguments for generation of summary documents in the same directory as the logs.

"""
import os

from certifi import where

import extractor
import matplotlib.pyplot as plt

import argparse
from datetime import timedelta, date
# import shutil

import pandas as pd

parser = argparse.ArgumentParser(description='Summarise the training runs of a dataset')
parser.add_argument('--dataset', type=str, help='Input the dataset you want to summarise')
parser.add_argument('--save_dir', help='Save where?', required=True)
parser.add_argument('--log_dir', help='BiT output where?', required=True)

args = parser.parse_args()
highest_validation_accuracy = 0.00
highest_validation_path = ''

final_validation_by_hyperparam = []

for folder in os.listdir(args.log_dir):
    if args.dataset in folder:
        full_path = f'{args.log_dir}{folder}'
        #print(full_path)

        training, validation, header = extractor.get_info_from_log(f'{full_path}/train.log')

        print(f'saving some csvs in {folder}')
        pd.DataFrame.to_csv(training,os.pjoin(folder,'training.csv'))
        pd.DataFrame.to_csv(validation,os.pjoin(folder,'validation.csv'))

        modeltype, base_lr, batch, batch_split, examples_per_class = extractor.make_summary_txt(full_path,validation,folder,args,header)

        this_run_info = f'{modeltype}_{base_lr}_{batch}_{batch_split}_{examples_per_class}'

        plt.figure()
        extractor.plot_multi(training[['Training loss', 'Learning rate']],figsize=(10,5))
        plt.tight_layout()
        plt.savefig(f"{full_path}/training_curves_{folder}_{this_run_info}.png")
        plt.close()

        plt.figure()
        extractor.plot_multi(validation['Validation loss', 'Precision', 'Recall','Accuracy','Specificity','Balanced Accuracy','F1','Dataset Cardinality', 'Label density','Naive Accuracy','Hamming loss','Adjusted accuracy','Exact matches'],figsize=(20,10))
        plt.tight_layout()
        plt.savefig(f"{full_path}/validation_curves_{folder}_{this_run_info}.png")
        plt.close()

        new_dir_path = f'{full_path[:39]}_{this_run_info}'
        os.rename(full_path,new_dir_path)

        if validation.empty != True:
            this_run_final_f1 = validation['F1'][-1]
            if this_run_final_f1 > highest_validation_f1:
                highest_validation_f1 = this_run_final_f1
                highest_validation_path = new_dir_path

            final_validation_by_hyperparam.append({'Final validation f1':pd.to_numeric(this_run_final_f1, errors='coerce'),'Batch size':pd.to_numeric(batch[6:], errors='coerce'),'Batch splitting divisor':pd.to_numeric(batch_split[12:]),'Examples per class':pd.to_numeric(examples_per_class[19:], errors='coerce'),'Base learning rate':pd.to_numeric(base_lr[8:], errors='coerce')})

        # elif validation.empty:
        #     if args.delete_old:
        #         print("HOW DID I GET HERE?????")
        #         continue

print(f'Highest validation accuracy for dataset {args.dataset} acheived with full_path = {highest_validation_path} with {highest_validation_accuracy}%.')

#what I want: end val accuracies by param (base_lr, examples_per_class, batch_size)
final_validation_by_hyperparam = pd.DataFrame.from_records(final_validation_by_hyperparam)
print(final_validation_by_hyperparam)

final_validation_by_hyperparam = final_validation_by_hyperparam.groupby(['Batch size','Batch splitting divisor','Examples per class','Base learning rate'],dropna=False).mean().reset_index()

# where_duplicated_runs = final_validation_by_hyperparam.duplicated(subset=['Batch size','Batch splitting divisor','Examples per class','Base learning rate'],keep=False)

print(final_validation_by_hyperparam)
# print(where_duplicated_runs)

dataframe_save_path = f'{args.save_dir}/{args.dataset}.csv'

pd.DataFrame.to_csv(final_validation_by_hyperparam,dataframe_save_path)


from collections import deque

secondary_dict = {"orange": ["red", "yellow"], "purple": ["red", "blue"], "green": ["yellow", "blue"]}
main_colors = ["red", "yellow", "blue"]
secondary_colors = ["orange", "purple", "green"]
matched_main = []
str_list = deque([ch for ch in input().split(" ")])

while len(str_list) > 0:
    current_str = ''
    reversed_current_str = ''

    if len(str_list) > 1:
        part_one = str_list.popleft()
        part_two = str_list.pop()
        current_str = part_one + part_two
        reversed_current_str = part_two + part_one

        if current_str in main_colors:
            matched_main.append(current_str)
            continue
        elif current_str in secondary_colors:
            matched_main.append(current_str)
            continue
        elif reversed_current_str in main_colors:
            matched_main.append(reversed_current_str)
        elif reversed_current_str in secondary_colors:
            matched_main.append(reversed_current_str)
        else:
            current_one = part_one[:-1]
            current_two = part_two[:-1]

            if len(str_list) > 1:
                index = len(str_list) // 2

                if current_two != "":
                    str_list.insert(index, current_two)

                if current_one != "":
                    str_list.insert(index, current_one)

            elif len(str_list) <= 1:

                if current_two != "":
                    str_list.appendleft(current_two)

                if current_one != "":
                    str_list.appendleft(current_one)

    else:
        part_one = str_list.popleft()
        current_str = part_one

        if current_str in main_colors:
            matched_main.append(current_str)
            continue
        elif current_str in secondary_colors:
            matched_main.append(current_str)
            continue
        else:
            current_one = part_one[:-1]

            if current_one != "":
                str_list.append(current_one)

final_list = []

for ch in matched_main:
    count = 0

    if ch in secondary_colors:
        for key in secondary_dict[ch]:
            if key in matched_main:
                count += 1
    else:
        final_list.append(ch)

    if count == 2:
        final_list.append(ch)

print(final_list)


import sys

from django.conf import settings
from django.conf.urls import *
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from mezzanine.core.views import direct_to_template

admin.autodiscover()

# Add the urlpatterns for any custom Django applications here.
# You can also change the ``home`` view to add your own functionality
# to the project's homepage.

#urlpatterns = staticfiles_urlpatterns()

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = patterns('',
        url(r'^__debug__/', include(debug_toolbar.urls)),
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
        url(r'^assets/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.STATIC_ROOT,
        }),
    )
else:
    urlpatterns = []

urlpatterns += patterns("",

    # Change the admin prefix here to use an alternate URL for the
    # admin interface, which would be marginally more secure.
    ("^admin/", include(admin.site.urls)),
    ("^", include("invites.urls")),
    ("^", include("captcha.urls")),
    ("^", include("mezzanine.urls")),
    url("^$", direct_to_template, {"template": "index.html"}, name="home"),

)

# Adds ``STATIC_URL`` to the context of error pages, so that error
# pages can use JS, CSS and images.
handler404 = "mezzanine.core.views.page_not_found"
handler500 = "mezzanine.core.views.server_error"


# python 3.10
from collections import defaultdict
import heapq

def solution(n, paths, gates, summits):
    """
    Args:
        n : XX산의 지점 수
        paths : 각 등산로의 정보를 담은 2차원 정수 배열
        gates : 출입구들의 번호가 담긴 정수 배열
        summits : 산봉우리들의 번호가 담긴 정수 배열

    Returns:
        intensity가 최소가 되는 등산코스에 포함된 산봉우리 번호와 intensity의 최솟값을 차례대로 담긴 정수 배열
    """
    summits = set(summits) ## 중요!! 해시 테이블로 탐색 시간 감소
    answer = []
    adj = defaultdict(list)

    # 인접리스트로 그래프 구축
    for start, end, weight in paths:
        adj[start].append((end, weight))
        adj[end].append((start, weight))

    # 모든 지점으로 가는 최소 intensity
    dp = [10000001] * (n+1)

    def dijkstra(start):
        dp[start] = 0
        hq = [(0, start)]

        while hq:
            # 큐에서 꺼낸다
            intens, cur = heapq.heappop(hq) # 현 위치까지 오는 최소 intensity, 현재 위치

            # intensity가 기존보다 클 경우 패스
            if intens > dp[cur]:
                continue

            # 연결된 곳 순회
            for t, w in adj[cur]:
                nextIntens = max(intens, w)
                # 갈 수 있는가
                if dp[t] > nextIntens:
                    dp[t] = nextIntens
                    # 산봉우리가 아닌 경우에 이동
                    if t not in summits:
                        # 큐에 넣는다
                        heapq.heappush(hq, (dp[t], t))

    for gate in gates:
        dijkstra(gate)

    answer = [0, 10000001]
    for summit in sorted(summits): ## 번호가 낮은 순서대로 먼저 체크
        if dp[summit] < answer[1]:
            answer = [summit, dp[summit]]

    return answer

n = 7
paths = [[1, 3, 10], [1, 4, 20], [2, 3, 4], [2, 4, 6], [3, 5, 20], [4, 5, 6]]
gates = [1, 2]
summits = [5]

print(solution(n, paths, gates, summits))


# 확률 10^30 배 올리기 - loop strategy(루프 전략)
import random

def optimized_loop(boxes, numbers):
    count = 0
    pairs = {box: number for box, number in zip(boxes, numbers)}

    for box in boxes:
        choice = box
        for _ in range(len(boxes) // 2):
            choice = pairs[choice]
            if choice == box:
                count += 1
                break
        else:
            break

    return count

prisoner_numbers = 100
trials = 10000
success_count = 0

for _ in range(trials):
    boxes = list(range(1, prisoner_numbers + 1))
    numbers = boxes.copy()
    random.shuffle(numbers)
    result = optimized_loop(boxes, numbers)
    if result == prisoner_numbers:
        success_count += 1

success_rate = success_count / trials

print(f"Success: {success_count}")
print(f"Failure: {trials - success_count}")
print(f"Success Rate: {success_rate}")


import os

class Lang:
    """Lang class"""

    # static list of language codes

    # taken from
    # http://www.loc.gov/standards/iso639-2/php/code_list.php

    #  self.codes[0] =  RFC 4646
    # self.codes[1] = ISO 639-1
    # self.codes[2] = English name

    # Good code list RFC 4646:http://sharpertutorials.com/list-of-culture-codes/
    # Note: Previous versions supported  # self.codes[0] = ISO 639-2

    codes = [
            ["en-CA", "en", "english"],
            ["de-DE", "de", "german"],
            ["fr-FR", "fr", "french"],
            ["cz-CZ", "cz", "czech"],
            ["it-IT", "it", "italian"],
            ["gr-GR", "el", "greek"],
            ["ca-ES", "ca", "catalan"],
            ["es-ES", "es", "spanish"],
            ["fi-FI", "fi", "finnish"],
            ["sv-SE", "sv", "swedish"],
            ["pt-PT", "pt", "portuguese"],
        # to be continued ...
    ]
    defaultCode = "en-CA"

    # static method
    def getCode(langString):

        for lang in Lang.codes:
            if langString.lower() in [code.lower() for code in lang]:
                return lang[0]
        # return None if nothing found
        return None

    getCode = staticmethod(getCode)

    def __init__(self):

        # default
        self.code = self.defaultCode
        self.strings = {}
        self.initStrings()

    def setCode(self, code):
        """ Set chosen language code """

        self.code = Lang.getCode(code)
        if not self.code:
            self.code = self.defaultCode
        return

    def initStrings(self):
        """ Initialize self.strings object according to known codes from
        Lang.py

        It can be used later like::

            self.strings["eng"]["foo"] = "bar"

        """

        for lang in self.codes:
            self.strings[lang[0]] = {}
        return

    def get(self, key):
        """ Will return desired string in selected language """

        if self.strings[self.code].has_key(key):
            return self.strings[self.code][key]

        return key


import logging
from substrates_nrps import *

import os
import utils

def run_nrps_substr_spec_predictions(pksnrpsvars, seq_record, options):
    #Predict NRPS A domain specificities with NRPSPredictor and Minowa et al. method

    # first extract A domains
    pksnrpsvars.nrpsnames, pksnrpsvars.nrpsseqs = extract_nrps_genes(pksnrpsvars.pksnrpscoregenes, pksnrpsvars.domaindict, seq_record, extra_aa=120)
    if len(pksnrpsvars.nrpsnames) > 0:
        run_nrpspredictor(seq_record, pksnrpsvars.nrpsnames, pksnrpsvars.nrpsseqs, options)
        run_minowa_predictor_nrps(pksnrpsvars.pksnrpscoregenes, pksnrpsvars.domaindict, seq_record, options)

def parse_nrps_preds(options, pksnrpsvars):
    pksnrpsvars.minowa_nrps_preds = {}
    pksnrpsvars.minowa_nrps_preds_details = {}
    pksnrpsvars.nrps_svm_preds = {}
    pksnrpsvars.nrps_svm_preds_details = {}
    pksnrpsvars.nrps_code_preds = {}
    pksnrpsvars.nrps_code_preds_details = {}
    substratetransdict2 = {'pipecolate':'pip','fOHOrn':'orn','beta-Lys':'blys','5NhOrn':'orn','OHOrn':'orn','Aad':'Aaa','bOHTyr':'bht'}
    if len(pksnrpsvars.nrpsnames) > 0:
        minowa_a_file = open(options.raw_predictions_outputfolder + os.sep + "ctg" + str(options.record_idx) + "_minowa_nrpspredoutput.txt","r")
        minowa_a_file = minowa_a_file.read()
        minowa_a_file = minowa_a_file.replace("\r","\n")
        parts = minowa_a_file.split("\\\\\n")[1:]
        for i in parts:
            partlines = i.split("\n")
            acc = partlines[0]
            tophit = partlines[2].split("\t")[0]
            if tophit in substratetransdict2.keys():
                tophit = substratetransdict2[tophit]
            pksnrpsvars.minowa_nrps_preds[acc] = tophit.lower()
            pksnrpsvars.minowa_nrps_preds_details[acc] = "Minowa HMM method A-domain
Substrate specificity prediction top hits:
\n" + partlines[1] + "
\n" + partlines[2] + "
\n" + partlines[3] + "
\n" + partlines[4] + "

\n\n"
        nrpspredictorfile1 = open(options.raw_predictions_outputfolder + os.sep + "ctg" + str(options.record_idx) + "_nrpspredictor2_svm.txt","r")
        nrpspredictorfile2 = open(options.raw_predictions_outputfolder + os.sep + "ctg" + str(options.record_idx) + "_nrpspredictor2_codes.txt","r")
        nrpspredictorfile1 = nrpspredictorfile1.read()
        nrpspredictorfile1 = nrpspredictorfile1.replace("\r","\n")
        lines = nrpspredictorfile1.split("\n")[1:-1]
        for k in lines:
            tabs = k.split("\t")
            if tabs[6] != "N/A":
                pksnrpsvars.nrps_svm_preds[tabs[0]] = tabs[6]
            elif tabs[5] != "N/A":
                pksnrpsvars.nrps_svm_preds[tabs[0]] = tabs[5]
            elif tabs[4] != "N/A":
                pksnrpsvars.nrps_svm_preds[tabs[0]] = tabs[4]
            else:
                pksnrpsvars.nrps_svm_preds[tabs[0]] = tabs[3]
            pksnrpsvars.nrps_svm_preds_details[tabs[0]] = " NRPSPredictor2 SVM prediction details:
\n8 Angstrom 34 AA code:
\n" + tabs[1] + "
\nPredicted physicochemical class:
\n" + tabs[3] + "
\nLarge clusters prediction:
\n" + tabs[4] + "
\nSmall clusters prediction:
\n" + tabs[5] + "
\nSingle AA prediction:
\n" + tabs[6] + "

\n\n"
        nrpspredictorfile2 = nrpspredictorfile2.read()
        nrpspredictorfile2 = nrpspredictorfile2.replace("\r","\n")
        lines = nrpspredictorfile2.split("\n")[:-1]
        for k in lines:
            tabs = k.split("\t")
            pksnrpsvars.nrps_code_preds[tabs[0]] = tabs[1]
            pksnrpsvars.nrps_code_preds_details[tabs[0]] = " NRPSPredictor2 Stachelhaus code prediction:
\n" + tabs[1] + "

\n\n"

#not in db yet
# aaa
# mpro
# dhb
# pgly
# aeo
#4mha
#pico
#phg
# dha
# scy
# pip
#adds
#HIV VS 2HIVA????
#bht
#4pPro
#ala-b
#dht
#sal
#tcl
#hyv-d
#iva
#vol
#mal
#mmal
#mxmal
#emal

def calculate_consensus_prediction(pksnrpsvars):
    #Combine substrate specificity predictions into consensus prediction
    pksnrpsvars.consensuspreds = {}
    available_smiles_parts = ['GLY','ALA','VAL','LEU','ILE','MET','PRO','PHE','TRP','SER','THR','ASN','GLN','TYR','CYS','LYS','ARG',
    'HIS','ASP','GLU','MPRO','ORN','PGLY','DAB','BALA','AEO','DHA','PIP','BMT','gly','ala','val','leu','ile','met','pro','phe','trp','ser',
    'thr','asn','gln','tyr','cys','lys','arg','his','asp','glu','aaa','mpro','dhb','2hiva','orn','pgly','dab','bala','aeo','4mha','pico','phg',
    'dha','scy','pip','bmt','adds','aad','abu','hiv','dhpg','bht','3-me-glu','4pPro','ala-b','ala-d','dht','Sal','tcl','lys-b','hpg','hyv-d',
    'iva','vol','mal','mmal','mxmal','emal','nrp','pk','Gly','Ala','Val','Leu','Ile','Met','Pro','Phe','Trp','Ser','Thr','Asn','Gln','Tyr',
    'Cys','Lys','Arg','His','Asp','Glu','Mpro','23Dhb','34Dhb','2Hiva','Orn','Pgly','Dab','Bala','Aeo','4Mha','Pico','Aaa','Dha','Scy','Pip',
    'Bmt','Adds','DHpg','DHB','nrp','pk']

    for feature in pksnrpsvars.pksnrpscoregenes:
        locus = utils.get_gene_id(feature)
        nra = 0
        j = pksnrpsvars.domaindict[locus]
        for k in j:
            if k[0] == "AMP-binding" or k[0] == "A-OX":
                nra +=1
                preds = []
                preds.append(pksnrpsvars.minowa_nrps_preds[locus + "_A" + str(nra)])
                preds.append(pksnrpsvars.nrps_svm_preds[locus + "_A" + str(nra)])
                preds.append(pksnrpsvars.nrps_code_preds[locus + "_A" + str(nra)])
                cpred = "n"
                for l in preds:
                    if preds.count(l) > 1:
                        if l in available_smiles_parts:
                            pksnrpsvars.consensuspreds[locus + "_A" + str(nra)] = l
                        else:
                            pksnrpsvars.consensuspreds[locus + "_A" + str(nra)] = "nrp"
                        cpred = "y"
                if cpred == "n":
                    pksnrpsvars.consensuspreds[locus + "_A" + str(nra)] = "nrp"

def generate_domainnamesdict(pksnrpsvars):
    pksnrpsvars.domainnamesdict = {}
    for feature in pksnrpsvars.pksnrpscoregenes:
        locus = utils.get_gene_id(feature)
        j = pksnrpsvars.domaindict[locus]
        domainnames = []
        for k in j:
            domainnames.append(k[0])
        pksnrpsvars.domainnamesdict[locus] = domainnames


"""
author: LSH9832
"""
from yolox import Detector, draw_bb
import cv2

if __name__ == '__main__':
    detector = Detector(
        model_path="E:/图像检测平台/webyolox-main/settings/test/output/last.pth",
        model_size="s",
        class_path="E:/图像检测平台/webyolox-main/yolox/datasets/classes.txt",
        conf=0.25,
        nms=0.4,
        input_size=640,
        fp16=True
    )

    """
    or create detector as follows
    """
    # from yolox import create_detector_from_settings
    # detector = create_detector_from_settings("./detect_settings.yaml")

    detector.loadModel()

    cap = cv2.VideoCapture(0)
    while cap.isOpened():
        success, image = cap.read()
        if success:
            result = detector.predict(image)

            draw_bb(image, result, detector.get_all_classes())
            cv2.imshow("image", image)
            if cv2.waitKey(1) == 27:    # esc
                cv2.destroyAllWindows()
                break


import twisted.trial.unittest
from twisted.internet.defer import inlineCallbacks

from txdav.common.icommondatastore import NotFoundError

from calendarserver.tools.shell.cmd import CommandsBase
from calendarserver.tools.shell.terminal import ShellProtocol

class TestCommandsBase(twisted.trial.unittest.TestCase):

    def setUp(self):
        self.protocol = ShellProtocol(None, commandsClass=CommandsBase)
        self.commands = self.protocol.commands

    @inlineCallbacks
    def test_getTargetNone(self):
        target = (yield self.commands.getTarget([], wdFallback=True))
        self.assertEquals(target, self.commands.wd)

        target = (yield self.commands.getTarget([]))
        self.assertEquals(target, None)

    def test_getTargetMissing(self):
        return self.assertFailure(self.commands.getTarget(["/foo"]), NotFoundError)

    @inlineCallbacks
    def test_getTargetOne(self):
        target = (yield self.commands.getTarget(["users"]))
        match = (yield self.commands.wd.locate(["users"]))
        self.assertEquals(target, match)

    @inlineCallbacks
    def test_getTargetSome(self):
        target = (yield self.commands.getTarget(["users", "blah"]))
        match = (yield self.commands.wd.locate(["users"]))
        self.assertEquals(target, match)

    def test_commandsNone(self):
        allCommands = self.commands.commands()
        self.assertEquals(sorted(allCommands), [])

        allCommands = self.commands.commands(showHidden=True)
        self.assertEquals(sorted(allCommands), [])

    def test_commandsSome(self):
        protocol = ShellProtocol(None, commandsClass=SomeCommands)
        commands = protocol.commands

        allCommands = commands.commands()

        self.assertEquals(
            sorted(allCommands),
            [
                ("a", commands.cmd_a),
                ("b", commands.cmd_b),
            ]
        )

        allCommands = commands.commands(showHidden=True)

        self.assertEquals(
            sorted(allCommands),
            [
                ("a", commands.cmd_a),
                ("b", commands.cmd_b),
                ("hidden", commands.cmd_hidden),
            ]
        )

    def test_complete(self):
        items = (
            "foo",
            "bar",
            "foobar",
            "baz",
            "quux",
        )

        def c(word):
            return sorted(CommandsBase.complete(word, items))

        self.assertEquals(c(""), sorted(items))
        self.assertEquals(c("f"), ["oo", "oobar"])
        self.assertEquals(c("foo"), ["", "bar"])
        self.assertEquals(c("foobar"), [""])
        self.assertEquals(c("foobars"), [])
        self.assertEquals(c("baz"), [""])
        self.assertEquals(c("q"), ["uux"])
        self.assertEquals(c("xyzzy"), [])

    def test_completeCommands(self):
        protocol = ShellProtocol(None, commandsClass=SomeCommands)
        commands = protocol.commands

        def c(word):
            return sorted(commands.complete_commands(word))

        self.assertEquals(c(""), ["a", "b"])
        self.assertEquals(c("a"), [""])
        self.assertEquals(c("h"), ["idden"])
        self.assertEquals(c("f"), [])

    @inlineCallbacks
    def _test_completeFiles(self, tests):
        protocol = ShellProtocol(None, commandsClass=SomeCommands)
        commands = protocol.commands

        def c(word):
            # One token
            d = commands.complete_files((word,))
            d.addCallback(lambda c: sorted(c))
            return d

        def d(word):
            # Multiple tokens
            d = commands.complete_files(("XYZZY", word))
            d.addCallback(lambda c: sorted(c))
            return d

        def e(word):
            # No tokens
            d = commands.complete_files(())
            d.addCallback(lambda c: sorted(c))
            return d

        for word, completions in tests:
            if word is None:
                self.assertEquals((yield e(word)), completions, "Completing %r" % (word,))
            else:
                self.assertEquals((yield c(word)), completions, "Completing %r" % (word,))
                self.assertEquals((yield d(word)), completions, "Completing %r" % (word,))

    def test_completeFilesLevelOne(self):
        return self._test_completeFiles((
            (None, ["groups/", "locations/", "resources/", "uids/", "users/"]),
            ("", ["groups/", "locations/", "resources/", "uids/", "users/"]),
            ("u", ["ids/", "sers/"]),
            ("g", ["roups/"]),
            ("gr", ["oups/"]),
            ("groups", ["/"]),
        ))

    def test_completeFilesLevelOneSlash(self):
        return self._test_completeFiles((
            ("/", ["groups/", "locations/", "resources/", "uids/", "users/"]),
            ("/u", ["ids/", "sers/"]),
            ("/g", ["roups/"]),
            ("/gr", ["oups/"]),
            ("/groups", ["/"]),
        ))

    def test_completeFilesDirectory(self):
        return self._test_completeFiles((
            ("users/", ["wsanchez", "admin"]),  # FIXME: Look up users
        ))

    test_completeFilesDirectory.todo = "Doesn't work yet"

    def test_completeFilesLevelTwo(self):
        return self._test_completeFiles((
            ("users/w", ["sanchez"]),  # FIXME: Look up users?
        ))

    test_completeFilesLevelTwo.todo = "Doesn't work yet"

class SomeCommands(CommandsBase):

    def cmd_a(self, tokens):
        pass

    def cmd_b(self, tokens):
        pass

    def cmd_hidden(self, tokens):
        pass

    cmd_hidden.hidden = "Hidden"


import tweepy
import time
import pandas as pd
from config import *

class TweetExtractor():

    def __init__(self):

        try:
            self.auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
            self.auth.set_access_token(access_token, access_token_secret)
            self.api = tweepy.API(self.auth, wait_on_rate_limit=True)

        except:
            print('Tweepy Authentication Failed.')

    def extract(self, queries = ['#UPElections2022'], filename: str = '') -> str:

        if filename == '':
            filename = time.strftime('%Y%m%d_%H%M%S')

        filename = 'data/extracted/' + filename + '.pkl'

        frames = []

        for query in queries:
            df = []
            tweetData = tweepy.Cursor(self.api.search_tweets, q=query, count=100)

            for tweet in tweetData.items():
                try:
                    df.append([tweet.created_at, tweet.user.screen_name,
                                    tweet.user.location, tweet.text])

                except:
                    print('Error in Extraction.')
            df = pd.DataFrame(df, columns=['created_at', 'screen_name', 'location', 'text'])
            frames.append(df)

        df = pd.concat(frames)
        df.drop_duplicates(inplace=True, ignore_index=False)
        #df.to_csv(filename, encoding='utf-8', index=False)
        df.to_pickle(filename)

        print('Extraction Complete!')
        return filename


import FileManager
import numpy as np
from PIL import Image as Image

class ImageSquarer:

    def resizeImage(self, file, size_goal):
        image = FileManager.read(file)
        image_shape = np.array(image).shape

        if image_shape[1] > image_shape[0]:
            new_width = int(size_goal)
            new_height = int(image_shape[0] * new_width / image_shape[1])
        else:
            new_height = int(size_goal)
            new_width = int(image_shape[1] * new_height / image_shape[0])

        image_resized = image.resize((new_width, new_height))

        FileManager.close(image)

        return image_resized

    def squareImage(self, file, size_goal):
        resizedImage = self.resizeImage(file, size_goal)

        squareImage = Image.new("RGB", (size_goal, size_goal), "white")
        squareImage.paste(resizedImage, (0, 0))
        resizedImage.close()

        return squareImage

    def saveImage(self, image, destination, mode):
        return FileManager.save(image, destination, mode)

    def deleteImage(self, filepath):
        FileManager.delete(filepath)

#to do:
    # generate square images (save them somewhere or no?)
    # scale down RGB values (divide by 255)
    # reduce dimensionality from 4 to 2
    # store square images as csv files
    # be able to delete old images / csv files at location
    # separate csv file generation from machine learning (for time reduction purposes)

#in the future:
    #make file manager a class object
    #get around using imageSquarer as a mask