#! /usr/bin/python3
# Autor: Pablo Baeyens
# Uso: ./main.py

import cv2 as cv
import numpy as np
import math
import collections
import random
import auxFunc

#########################
# PARÁMETROS AJUSTABLES #
#########################

# Carga y visualización de imágenes
PATH = "imagenes/" # Carpeta de imágenes
COLOR, GRIS = cv.IMREAD_COLOR, cv.IMREAD_GRAYSCALE

# Carga de datos de k medias
KMEANS_FILENAME = "kmeanscenters2000.pkl"
np.seterr(divide='ignore', invalid='ignore') # evitar errores por norma cero
_, _,  kmeans = auxFunc.loadDictionary(KMEANS_FILENAME)
KMEANS_DICT = kmeans/np.linalg.norm(kmeans, axis = 1).reshape((kmeans.shape[0],1))

# Carga de datos de parches
PATCHES_FILENAME = "descriptorsAndpatches2000.pkl"
PT_DESC, PATCHES = auxFunc.loadAux(PATCHES_FILENAME, True)

# Objetos SIFT y SURF con parámetros elegidos
SIFT = cv.xfeatures2d.SIFT_create()

########################
# FUNCIONES AUXILIARES #
########################

def espera():
  """Función para introducir una espera"""
  input("[Enter para continuar]")

# Del trabajo 0

def leeimagen(filename, flagColor):
  """Lee una imagen y la muestra (tanto en grises como en color)
    - filename: nombre del fichero
    - flagColor: flag que indica si tiene color o no"""

  return cv.imread(filename, flagColor)

N= 0
def pintaI(im, titulo = "Imagen"):
  """Visualiza una matriz de números reales cualquiera
     - im: Imagen a visualizar"""
  global N
  cv.imwrite(titulo + str(N) + ".png", im)
  N += 1
  #cv.imshow(titulo, im)
  #cv.waitKey(0)
  #cv.destroyAllWindows()

def isBW(im):
  """Indica si una imagen está en blanco y negro"""
  return len(im.shape) == 2

def juntaMI(vim, rows):
  """Junta imágenes en varias filas
  PRECONDICIÓN: len(vim) tiene que ser divisible por rows"""
  N = math.ceil(len(vim)/rows)
  output = []

  for i in range(rows): # Crea cada fila
    rim = vim[N*i: N*(i+1)]
    altura = max(im.shape[0] for im in rim)

    for i,im in enumerate(rim):
      if isBW(im): # Pasar a color
        rim[i] = cv.cvtColor(rim[i], cv.COLOR_GRAY2BGR)

        if im.shape[0] < altura: # Redimensionar imágenes
          borde = int((altura - rim[i].shape[0])/2)
          rim[i] = cv.copyMakeBorder(rim[i], borde, borde + (altura - rim[i].shape[0]) % 2,
                                     0, 0, cv.BORDER_CONSTANT, value = (0,0,0))
    output.append(cv.hconcat(rim))

    # Redimensiona filas
    anchura = max(im.shape[1] for im in output)
    for j,im in enumerate(output):
      if im.shape[1] < anchura: # Redimensionar imágenes
        borde = anchura - output[i].shape[1]
        output[i] = cv.copyMakeBorder(output[i], 0,0, 0, borde, cv.BORDER_CONSTANT, value = (0,0,0))
  return cv.vconcat(output)

def muestraMI(vim, titulo = "Imágenes", rows = 1):
  """Visualiza varias imágenes a la vez
  - vim: Secuencia de imágenes"""

  imMulti = juntaMI(vim, rows)
  pintaI(imMulti, titulo)

def pintaMI(*parejas):
  """Representa varias imágenes con sus títulos en una ventana
  - parejas: lista de pares (imagen, título)
  """

  vim, titulos = zip(*parejas)
  muestraMI(list(vim), titulo = " | ".join(titulos))

######################################
# FUNCIONES DEL TRABAJO 2 AUXILIARES #
######################################

def getDescriptors(im, mask = None):
  """Obten descriptores normalizados SIFT de una imagen.
  Argumentos posicionales:
  - im: Imagen
  Argumentos opcionales:
  - mask: Máscara"""
  ps, ds = SIFT.detectAndCompute(im,mask)
  normalized_ds = ds/np.linalg.norm(ds, axis = 1).reshape((ds.shape[0],1))
  return ps, normalized_ds

def ap2A_LA2NN(d_im1, d_im2, ratio = 0.8):
  """Obtiene correspondencias entre descriptores.
  Usa el método 'Lowe-Average-2NN'.
  Argumentos posicionales:
  - d_im1, d_im2: Descriptores de cada imagen
  Argumentos opcionales:
  - ratio: El ratio usado para descartar correspondencias ambiguas. Por defecto 0.8
  Devuelve: Correspondencias"""

  # Declara el matcher
  matcher = cv.FlannBasedMatcher_create()

  # Obten las dos mejores correspondencias de cada punto
  matches = matcher.knnMatch(d_im1, d_im2, k = 2)

  # Toma las correspondencias que no sean ambiguas de acuerdo al test dado por Lowe
  clear_matches = []
  for best, second in matches:
    if best.distance/second.distance < ratio:
      clear_matches.append(best)

  return clear_matches

##############
# EJERCICIOS #
##############

## Ejercicio 1

def ap1Fixed(im1, im2, points):
  """Muestra correspondencias entre imágenes dada una región de la primera
  Argumentos posicionales:
  - im1, im2: Imágenes a comparar,
  - points: Puntos que describen un polígono convexo en im1 que tiene el objeto de interés
  """

  mask = cv.cvtColor(np.zeros(im1.shape, np.uint8), cv.COLOR_BGR2GRAY)
  cv.fillConvexPoly(mask, points, (256, 256, 256))

  p1, d1  = getDescriptors(im1, mask)
  p2, d2  = getDescriptors(im2)

  matches = ap2A_LA2NN(d1, d2)
  pintaMI((im1, "Imagen original"), (mask, "Máscara"))
  pintaI(cv.drawMatches(im1, p1, im2, p2,
           matches, None, flags = cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS))

def ap1Interactive(im1, im2):
  """Selecciona interactivamente región de una imagen
  y pinta sus correspondencias con otra imagen.
  Argumentos posicionales:
  - im1: Imagen de la que seleccionar interactivamente una región,
  - im2: Imagen en la que encontrar objeto de la región de im1.
  """

  print("Extrae la región de la corbata")
  points = np.array(auxFunc.extractRegion(im1))
  print("Puntos elegidos: ", points)
  ap1Fixed(im1, im2, points)

def ejemplo1(parejas):
  """Ejemplo ejercicio 1
  Argumentos posicionales:
  - parejas: Lista de tripletas im1, im2, points para ejemplo
  """
  for im1, im2, ps in parejas:
    ap1Fixed(im1, im2, ps)

def ejemplo1Interactivo(parejas):
  """Ejemplo ejercicio 1 (interactivo)
  Argumentos posicionales:
  - parejas: Lista de parejas im1, im2 para ejemplo interactivo.
  """
  for im1, im2 in parejas:
    ap1Interactive(im1, im2)

## Ejercicio 2

def similitud(bag1, bag2):
  """Calcula producto escalar normalizado de vectores bolsa de palabras.
  Argumentos posicionales:
  - bag1, bag2: Bolsas de palabras normalizadas
  Devuelve:
  - Producto escalar normalizado"""
  return np.dot(bag1, bag2)

def closestMatch(d):
  """Halla el centroide más cercano a un descriptor."""
  return np.argmin(np.linalg.norm(KMEANS_DICT-d))

def ap2A(ims):
  """Implementa modelo de índice invertido + bolsa de palabras
  Argumentos posicionales:
  - ims: Imágenes
  Devuelve:
  - Índice invertido y bolsa de palabras"""

  inv_index = [[] for _ in range(len(KMEANS_DICT))]
  bags = []
  matcher = cv.BFMatcher_create(crossCheck = False)

  for n, im in enumerate(ims):
    _, ds = getDescriptors(im)
    matches = matcher.match(ds, KMEANS_DICT)

    # Cuenta matches
    bag_dict = collections.Counter()
    for match in matches:
      bag_dict[match.trainIdx] += 1

    # Guarda en histograma e índice invertido
    bag = np.zeros(len(KMEANS_DICT))
    for word, cnt in bag_dict.items():
      bag[word] = cnt
      inv_index[word].append(n)

    # Normalizado
    bags.append(bag/np.linalg.norm(bag))
  return inv_index, bags

def ap2B(im_index, bolsas, ims):
  """Halla 5 imágenes más semejantes a una dado modelo
  de índice invertido + bolsa de palabras.
  Argumentos posicionales:
  - im: Índice de imagen para hallar semejantes,
  - bolsas: vector de bolsas de palabras,
  - ims: Lista de imágenes

  Devuelve:
  - Lista de 5 imágenes más semejantes
  """
  matches = sorted(range(len(ims)),
                   key = lambda i: - similitud(bolsas[i], bolsas[im_index]))

  closest = []
  for i in matches[:6]:
    closest.append(ims[i])
  return closest

def ejemplo2(ims, seleccionadas):
  """Ejemplo del ejercicio 2.
  Argumentos posicionales:
  - ims: Lista de imágenes"""

  print("Creando modelo de bolsa de palabras + índice invertido...", end='', flush=True)
  inv_index, bags = ap2A(ims)
  print(" Creado.")

  for n in seleccionadas:
    muestraMI(ap2B(n, bags, ims),
              titulo = "Imágenes más similares a imagen nº {n}".format(n = n),
              rows = 2)

## Ejercicio 3

def ap3(word):
  """Visualiza las regiones imagen de los 10 parches más cercanos a la palabra
  Argumentos posicionales:
  - word: El índice de la palabra visual"""
  matcher = cv.BFMatcher_create(crossCheck = False)
  matches = matcher.knnMatch(np.array([KMEANS_DICT[word]]), PT_DESC, k = 10)[0]

  chosen = []
  for m in matches:
    chosen.append(cv.resize(cv.cvtColor(PATCHES[m.trainIdx], cv.COLOR_RGB2GRAY), (96,96)))

  muestraMI(chosen, titulo = "Parches asociados a palabra Nº {n}".format(n = word), rows = 2)

def ejemplo3(words):
  """Ejemplo del ejercicio 3
  Argumentos posicionales:
  - words: Iterable de índices de palabras visuales elegidas"""
  for word in words:
    ap3(word)

def main():
  """Llama de forma secuencial a la función de cada apartado."""
  print("Cargando imágenes...",end='', flush=True)
  friends = []
  N = 441
  for n in range(N):
     friends.append(leeimagen(PATH + "{n}.png".format(n = n), COLOR))
  friends = np.array(friends)
  print(" Cargadas.")

  print("Ejemplo ejercicio 1 (descomentar código para versión con detectRegion):")
  ejemplo1([(friends[0], friends[2], np.array([[180,262],[242,256],[248,334],[184,329],[180,262]])),
            (friends[24], friends[25], np.array([[203,330],[349,332],[339,476],[217,480],[203,330]]))])

  #print("Ejemplo ejercicio 1 (interactivo)")
  #ejemplo1Interactivo([(friends[0], friends[2]), (friends[24], friends[25])])

  print("Ejemplo ejercicio 2 (en visualizador de imágenes):")
  ejemplo2(friends, [19, 100, 345])

  print("Ejemplo ejercicio 3 (en visualizador de imágenes):")
  ejemplo3([32, 206, 154])

if __name__ == "__main__":
  main()


#
#   2019 Fabian Jankowski
#   Match pulsars.
#

import argparse
import logging
import sys

import numpy as np
from astropy.coordinates import SkyCoord
import astropy.units as u

from psrmatch.matcher import Matcher
from psrmatch.version import __version__

# astropy generates members dynamically, pylint therefore fails
# disable the corresponding pylint test for now
# pylint: disable=E1101

def parse_args():
    # treat negative arguments
    for i, arg in enumerate(sys.argv):
        if (arg[0] == "-") and arg[1].isdigit():
            sys.argv[i] = " " + arg

    parser = argparse.ArgumentParser(description="Find matching pulsars.")

    parser.add_argument(
        "ra", type=str, help="Right ascension in ICRS frame and in hh:mm:ss notation."
    )

    parser.add_argument(
        "dec", type=str, help="Declination in ICRS frame and in hh:mm:ss notation."
    )

    parser.add_argument("dm", type=float, help="Dispersion measure.")

    parser.add_argument("--version", action="version", version=__version__)

    return parser.parse_args()

def setup_logging(level=logging.INFO):
    """
    Setup the logging configuration.
    """

    log = logging.getLogger("psrmatch")

    log.setLevel(logging.DEBUG)
    log.propagate = False

    # log to console
    console = logging.StreamHandler()
    console.setLevel(level)
    fmt = (
        "%(asctime)s, %(processName)s, %(name)s, %(module)s, %(levelname)s: %(message)s"
    )
    console_formatter = logging.Formatter(fmt)
    console.setFormatter(console_formatter)
    log.addHandler(console)

#
# MAIN
#

def main():
    args = parse_args()
    setup_logging()

    source = SkyCoord(ra=args.ra, dec=args.dec, frame="icrs", unit=(u.hourangle, u.deg))

    print("Source: {0}".format(source.to_string("hmsdms")))
    print("DM: {0}".format(args.dm))

    m = Matcher()
    m.load_catalogue("psrcat")
    m.create_search_tree()

    m.find_matches(source, args.dm)

if __name__ == "__main__":
    main()


import pandas as pd

def fillna_with_zeros(csvfile):
	data 	=	pd.DataFrame.from_csv(csvfile,header=0)
	data['Expected_result'].fillna(0.0,inplace=True)
	data['Expected_list'].fillna('[0]',inplace=True)
	data.to_csv(csvfile,sep=",")
	return

fillna_with_zeros('MATRIX_GENERATOR/TEST_MATRIX/students_work/2/37/3/matrix_old.csv')
#fillna_with_zeros('MATRIX_GENERATOR/TEST_MATRIX/employees/8/56/2/matrix.csv')


#user input
exam_hour = int(input())
exam_min = int(input())
arrival_hour = int(input())
arrival_min = int(input())

early = "Early"
on_time = "On time"
late = "Late"
output = ""

#logic
#We turn hours into minutes
exam_time = exam_hour * 60 + exam_min
arrival_time = arrival_hour * 60 + arrival_min
time_diff = exam_time - arrival_time

#We check for the first simple conditions/output - late, early or on time
if time_diff > 30:
    print("Early")
elif time_diff < 0:
    print("Late")
else:
    print("On time")
#We define 2 variables for the output, as we turn the minutes into a positive number
hours = 0
minutes = abs(time_diff)
#We turn back muntes over and equal to 60 to hours and the rest remain as minutes
if minutes >= 60:
    hours = minutes // 60
    minutes %= 60
#here are the variables for the first part of the second output:
if hours > 0:
    output += str(hours) + ":" + ("0" + str(minutes) if minutes < 10 else str(minutes)) + " hours"
#the output if hours (minutes // 60) are < 1
else :
    output += str(minutes) + " minutes"
#Here we add the second part of the second output
if time_diff > 0:
    output += " before the start"
else:
    output += " after the start"
#output
print(output)

Python Basics/03.PB-Python-Conditional-Statements-Advanced/Conditional Statements Advanced - Exercise/08. On Time for the exam - 2.py


#######################################################
# Script with unit tests for CYMDISTToFMU
#
# TSNouidui@lbl.gov                          2016-09-06
#######################################################
import unittest
import os
import sys
import platform
import subprocess
import shutil
from datetime import datetime

# Appending parser_path to the system path os required to be able
# to find the CYMDISTToFMU model from this directory
script_path = os.path.dirname(os.path.realpath(__file__))
parser_path = os.path.abspath(os.path.join(script_path, '..', 'parser'))
sys.path.append(parser_path)

import CYMDISTToFMU as cymdist

XSD_SCHEMA = 'CYMDISTModelDescription.xsd'
NEEDSEXECUTIONTOOL = 'needsExecutionTool'
MODELDESCRIPTION = 'modelDescription.xml'
CYMDISTModelicaTemplate_MO = 'CYMDISTModelicaTemplate.mo'
CYMDISTModelicaTemplate_Dymola_MOS = 'CYMDISTModelicaTemplate_Dymola.mos'
CYMDISTModelicaTemplate_JModelica_MOS = 'CYMDISTModelicaTemplate_JModelica.py'
CYMDISTModelicaTemplate_OpenModelica_MOS = 'CYMDISTModelicaTemplate_OpenModelica.mos'
XML_MODELDESCRIPTION = 'CYMDISTModelDescription.xml'
# Get the path to the templates files
script_path = os.path.dirname(os.path.realpath(__file__))
utilities_path = os.path.join(script_path, '..', 'parser', 'utilities')
PYTHON_SCRIPT_PATH = os.path.join(utilities_path, 'cymdist_wrapper.py')
MO_TEMPLATE_PATH = os.path.join(utilities_path, CYMDISTModelicaTemplate_MO)
MOS_TEMPLATE_PATH_DYMOLA = os.path.join(
    utilities_path, CYMDISTModelicaTemplate_Dymola_MOS)
MOS_TEMPLATE_PATH_JMODELICA = os.path.join(
    utilities_path, CYMDISTModelicaTemplate_JModelica_MOS)
MOS_TEMPLATE_PATH_OPENMODELICA = os.path.join(
    utilities_path, CYMDISTModelicaTemplate_OpenModelica_MOS)
XSD_FILE_PATH = os.path.join(utilities_path, XSD_SCHEMA)
XML_INPUT_FILE = os.path.join(utilities_path, XML_MODELDESCRIPTION)
CYMDISTToFMU_LIB_PATH = os.path.join(
    script_path, '..', 'parser', 'libraries', 'modelica')
python_scripts_path = [PYTHON_SCRIPT_PATH]

if(platform.system().lower() == 'windows'):
    python_scripts_path = [item.replace('\\', '\\\\') for item in [
        PYTHON_SCRIPT_PATH]]

CYMDIST_T = cymdist.CYMDISTToFMU('con_path',
                                       XML_INPUT_FILE,
                                       CYMDISTToFMU_LIB_PATH,
                                       MO_TEMPLATE_PATH,
                                       MOS_TEMPLATE_PATH_DYMOLA,
                                       XSD_FILE_PATH,
                                       '34',
                                       python_scripts_path,
                                       '2.0',
                                       'me',
                                       'dymola',
                                       None,
                                       None,
                                       'false')

class Tester(unittest.TestCase):
    '''
    Class that runs all regression tests.

    '''

    def find_executable(self, tool):

        '''
        Function for checking if Dymola, JModelica, or OpenModelica is installed.

        '''

        if tool == 'jmodelica' and platform.system().lower() == "windows":
            tool = 'pylab'
        if tool == 'jmodelica' and platform.system().lower() == "linux":
            tool = 'jm_python.sh'

        if tool == 'openmodelica' and platform.system().lower() == "windows":
            tool = 'omc'

        cmd = "where" if platform.system() == "Windows" else "which"
        try:
            return subprocess.call([cmd, tool])
        except:
            print ("No executable for tool={!s}".format(tool))
            return 1

    def run_cymdist (self, tool):

        '''
        Function for running FMUs exported from Dymola, JModelica, and OpenModelica with PyFMI.

        '''

        try:
            from pyfmi import load_fmu
        except BaseException:
            print ('PyFMI not installed. Test will not be be run.')
            return
        if (tool=='openmodelica' and platform.system().lower() == 'linux'):
                print ('tool={!s} is not supported on Linux'.format(tool))
                return

        else:
        # Export FMUs which are needed to run the cases.
            if tool == 'openmodelica':
                modPat = 'OPENMODELICALIBRARY'
                mosT = MOS_TEMPLATE_PATH_OPENMODELICA
            elif tool == 'dymola':
                modPat = 'MODELICAPATH'
                mosT = MOS_TEMPLATE_PATH_DYMOLA
            elif tool == 'jmodelica':
                # Unset environment variable
                if (os.environ.get('MODELICAPATH') is not None):
                    del os.environ['MODELICAPATH']
                modPat = None
                mosT = MOS_TEMPLATE_PATH_JMODELICA
            for version in ['2']:
                if (tool == 'openmodelica' or tool == 'jmodelica'):
                    version = str(float(version))
                for api in ['me']:
                    if (tool == 'openmodelica' and version == '1.0' and api == 'cs'):
                        print (
                            'tool={!s} with FMI version={!s} and FMI API={!s} is not supported.'.format(
                                tool, version, api))
                        continue
                    for cs_xml in ['true']:
                        if (version == '1'):
                            continue
                        CYMDIST_Test = cymdist.CYMDISTToFMU(
                            'con_path',
                            XML_INPUT_FILE,
                            CYMDISTToFMU_LIB_PATH,
                            MO_TEMPLATE_PATH,
                            mosT,
                            XSD_FILE_PATH,
                            '34',
                            python_scripts_path,
                            version,
                            api,
                            tool,
                            None,
                            modPat,
                            cs_xml)

                        print (
                            'Export the cymdist with tool={!s}, FMI version={!s}, FMI API={!s}'.format(
                                tool, version, api))
                        start = datetime.now()
                        # CYMDIST_Test.print_mo()
                        # CYMDIST_Test.generate_fmu()
                        # CYMDIST_Test.clean_temporary()
                        # CYMDIST_Test.rewrite_fmu()
                        end = datetime.now()
                        print(
                            'Export CYMDIST as an FMU in {!s} seconds.'.format(
                                (end - start).total_seconds()))

                        fmu_path = os.path.join(
                        script_path, '..', 'fmus', tool, platform.system().lower())
                        print(
                            'Copy CYMDIST.fmu to {!s}.'.format(fmu_path))
                        shutil.copy2('CYMDIST.fmu', fmu_path)

        fmu_path = os.path.join(
                script_path, '..', 'fmus', tool, platform.system().lower(), 'CYMDIST.fmu')
        # Parameters which will be arguments of the function
        start_time = 0.0
        stop_time = 0.1

        print ('Starting the simulation with {!s}'.format(tool))
        start = datetime.now()

        cymdist_input_valref = []
        cymdist_output_valref = []

        sim_mod = load_fmu(fmu_path, log_level=7)
        sim_mod.setup_experiment(
            start_time=start_time, stop_time=stop_time)

        cymdist_con_val_str=os.path.abspath('Z:\\thierry\\proj\\cyder_repo\\NO_SHARING\\CYMDIST\\config.json')
        if sys.version_info.major>2:
            cymdist_con_val_str=bytes(cymdist_con_val_str,'utf-8')
        cymdist_con_val_ref=sim_mod.get_variable_valueref('_configurationFileName')

        # Define the inputs
        cymdist_input_names = ['VMAG_A', 'VMAG_B', 'VMAG_C', 'VANG_A', 'VANG_B', 'VANG_C']
        cymdist_input_values = [2520, 2520, 2520, 0, -120, 120]
        cymdist_output_names = ['IA', 'IAngleA', 'IB', 'IAngleB', 'IC', 'IAngleC']

        # Get the value references of cymdist inputs
        for elem in cymdist_input_names:
            cymdist_input_valref.append(
                sim_mod.get_variable_valueref(elem))

        # Get the value references of cymdist outputs
        for elem in cymdist_output_names:
            cymdist_output_valref.append(
                sim_mod.get_variable_valueref(elem))

        # Set the flag to save the results
        sim_mod.set('_saveToFile', 0)

        if not (tool=="jmodelica"):
            sim_mod.set_string([cymdist_con_val_ref], [cymdist_con_val_str])

        # Initialize the FMUs
        sim_mod.initialize()

        # Call event update prior to entering continuous mode.
        sim_mod.event_update()

        # Enter continuous time mode
        sim_mod.enter_continuous_time_mode()

        sim_mod.set_real(cymdist_input_valref, cymdist_input_values)

        end = datetime.now()

        print(
            'Ran a single CYMDIST simulation with {!s} FMU={!s} in {!s} seconds.'.format(
                tool, fmu_path, (end - start).total_seconds()))
        if not (tool=='openmodelica'):
            #print(sim_mod.get_real(cymdist_output_valref))
            # PyFMI fails to get the output of an OpenModelica FMU
            if(abs(sum(sim_mod.get_real(cymdist_output_valref))-618.57)>1e-3):
                raise ValueError('Values are not matching.')
        # Terminate FMUs
        sim_mod.terminate()

    def test_check_duplicates(self):
        '''
        Test the function check_duplicates().

        '''

        # Array does not contain duplicates variables.
        cymdist.check_duplicates(['x1', 'x2', 'x3', 'x4'])

        # Array contain duplicates variables.
        with self.assertRaises(AssertionError):
            cymdist.check_duplicates(['x1', 'x1', 'x3', 'x4'])

    def test_sanitize_name(self):
        '''
        Test the function sanitize_name().

        '''

        # Testing name conversions.
        name = cymdist.sanitize_name('test+name')
        self.assertEqual(name, 'test_name', 'Names are not matching.')

        name = cymdist.sanitize_name('0test+*.name')
        self.assertEqual(name, 'f_0test___name', 'Names are not matching.')

    def test_xml_validator(self):
        '''
        Test the function xml_validator().

        '''

        # Testing validation of xml file
        CYMDIST_T.xml_validator()

    def test_xml_parser(self):
        '''
        Test the function xml_validator().

        '''

        # Testing validation of xml file
        CYMDIST_T.xml_parser()

    def test_print_mo(self):
        '''
        Test the function print_mo().

        '''

        # Testing function to print Modelica model.
        CYMDIST_T.print_mo()

    def test_cymdist_to_fmu(self):
        '''
        Test the export of an FMU with various options.

        '''

        for tool in  ['dymola', 'jmodelica', 'openmodelica']:
            retVal=self.find_executable(tool)
            if ((retVal is not None) and retVal!=1):
                print("======tool={!s} was found. Unit Test will be run".format(tool))
            else:
                continue

            if (platform.system().lower() == 'linux' and tool == 'openmodelica'):
                print ('tool={!s} is not supported on Linux.'.format(tool))
                continue
            if tool == 'openmodelica':
                modPat = 'OPENMODELICALIBRARY'
                mosT = MOS_TEMPLATE_PATH_OPENMODELICA
            elif tool == 'dymola':
                modPat = 'MODELICAPATH'
                mosT = MOS_TEMPLATE_PATH_DYMOLA
            elif tool == 'jmodelica':
                if (os.environ.get('MODELICAPATH') is not None):
                    del os.environ['MODELICAPATH']
                modPat = None
                mosT = MOS_TEMPLATE_PATH_JMODELICA
            for version in ['1', '2']:
                if (tool == 'openmodelica' or tool == 'jmodelica'):
                    version = str(float(version))
                for api in ['me', 'cs']:
                    if (tool == 'openmodelica' and version == '1.0' and api == 'cs'):
                        print (
                            'tool={!s} with FMI version={!s} and FMI API={!s} is not supported.'.format(
                                tool, version, api))
                        continue
                    for cs_xml in ['true']:
                        if (version == '1'):
                            continue
                        CYMDIST_Test = cymdist.CYMDISTToFMU(
                            'con_path',
                            XML_INPUT_FILE,
                            CYMDISTToFMU_LIB_PATH,
                            MO_TEMPLATE_PATH,
                            mosT,
                            XSD_FILE_PATH,
                            '34',
                            python_scripts_path,
                            version,
                            api,
                            tool,
                            None,
                            modPat,
                            cs_xml)

                        print (
                            'Export the cymdist with tool={!s}, FMI version={!s}, FMI API={!s}'.format(
                                tool, version, api))
                        start = datetime.now()
                        CYMDIST_Test.print_mo()
                        CYMDIST_Test.generate_fmu()
                        CYMDIST_Test.clean_temporary()
                        CYMDIST_Test.rewrite_fmu()
                        end = datetime.now()
                        print(
                            'Export CYMDIST as an FMU in {!s} seconds.'.format(
                                (end - start).total_seconds()))

    def test_updates_fmu(self):
        '''
        Test the export and updates of FMUs.

        '''

        for tool in  ['dymola', 'jmodelica', 'openmodelica']:
            retVal=self.find_executable(tool)
            if ((retVal is not None) and retVal!=1):
                print("======tool={!s} was found. Unit Test will be run".format(tool))
            else:
                continue

            if (platform.system().lower() == 'linux' and tool == 'openmodelica'):
                print ('tool={!s} is not supported on Linux.'.format(tool))
                continue
            if tool == 'openmodelica':
                modPat = 'OPENMODELICALIBRARY'
                mosT = MOS_TEMPLATE_PATH_OPENMODELICA
            elif tool == 'dymola':
                modPat = 'MODELICAPATH'
                mosT = MOS_TEMPLATE_PATH_DYMOLA
            elif tool == 'jmodelica':
                if (os.environ.get('MODELICAPATH') is not None):
                    del os.environ['MODELICAPATH']
                modPat = None
                mosT = MOS_TEMPLATE_PATH_JMODELICA
            for version in ['2']:
                if (tool == 'openmodelica' or tool == 'jmodelica'):
                    version = str(float(version))
                for api in ['me']:
                    if (tool == 'openmodelica' and version == '1.0' and api == 'cs'):
                        print (
                            'tool={!s} with FMI version={!s} and FMI API={!s} is not supported.'.format(
                                tool, version, api))
                        continue
                    for cs_xml in ['true']:
                        if (version == '1'):
                            continue
                        CYMDIST_Test = cymdist.CYMDISTToFMU(
                            'con_path',
                            XML_INPUT_FILE,
                            CYMDISTToFMU_LIB_PATH,
                            MO_TEMPLATE_PATH,
                            mosT,
                            XSD_FILE_PATH,
                            '34',
                            python_scripts_path,
                            version,
                            api,
                            tool,
                            None,
                            modPat,
                            cs_xml)

                        print (
                            'Export the cymdist with tool={!s}, FMI version={!s}, FMI API={!s}'.format(
                                tool, version, api))
                        start = datetime.now()
                        CYMDIST_Test.print_mo()
                        CYMDIST_Test.generate_fmu()
                        CYMDIST_Test.clean_temporary()
                        CYMDIST_Test.rewrite_fmu()
                        end = datetime.now()
                        print(
                            'Export CYMDIST as an FMU in {!s} seconds.'.format(
                                (end - start).total_seconds()))
                        fmu_path = os.path.join(
                        script_path, '..', 'fmus', tool, platform.system().lower())
                        print(
                            'Copy CYMDIST.fmu to {!s}.'.format(fmu_path))
                        shutil.copy2('CYMDIST.fmu', fmu_path)

    def test_run_cymdist_all(self):
        '''
        Test the execution of one CYMDIST FMU.

        '''

        # Export FMUs which are needed to run the cases.
        for tool in  ['dymola', 'jmodelica', 'openmodelica']:
            retVal=self.find_executable(tool)
            if ((retVal is not None) and retVal!=1):
                print("======tool={!s} was found. Unit Test will be run.".format(tool))
                print('=======The unit test will be run for tool={!s}.'.format(tool))
                self.run_cymdist (tool)
            else:
                continue

    def test_run_cymdist_dymola(self):
        '''
        Test the execution of one CYMDIST FMU.

        '''

        retVal=self.find_executable('dymola')
        if ((retVal is not None) and retVal!=1):
            print("======tool=dymola was found. Unit Test will be run.")
            print('=======The unit test will be run for Dymola.')
            print('=======Make sure that Dymola is on the System Path otherwise the simulation will fail.')
            # Run the cases
            self.run_cymdist ('dymola')
        else:
            return

    def test_run_cymdist_jmodelica(self):
        '''
        Test the execution of one CYMDIST FMU.

        '''

        retVal=self.find_executable('jmodelica')
        if ((retVal is not None) and retVal!=1):
            print("======tool=jmodelica was found. Unit Test will be run.")
            print('=======The unit test will be run for JModelica.')
            print('=======Make sure that JModelica is on the System Path otherwise the simulation will fail.')
            # Run the cases
            self.run_cymdist ('jmodelica')
        else:
            return

    def test_run_cymdist_openmodelica(self):
        '''
        Test the execution of one CYMDIST FMU.

        '''

        retVal=self.find_executable('openmodelica')
        if ((retVal is not None) and retVal!=1):
            print("======tool=openmodelica was found. Unit Test will be run.")
            print('=======The unit test will be run for OpenModelica.')
            print('=======Make sure that OpenModelica is on the System Path otherwise the simulation will fail.')
            # Run the cases
            self.run_cymdist ('openmodelica')
        else:
            return

if __name__ == "__main__":
        # Check command line options
    if (platform.system().lower() in ['windows']):
        unittest.main()
    else:
        print('=========CYMDISTToFMU is only supported Windows.')


from tests.sketcher import Sketcher, bead
from bead_cli.web.sketch import Sketch

def test_new_version_marks_older_superseded():
    sketcher = Sketcher()
    sketcher.define('a1 b1 c1')
    sketcher.compile(
        """
        a1 -> b1
        a1 -:survivor:-> c1
        """
    )

    sketch = sketcher.sketch
    orig_beads = sketch.beads
    sketch = Sketch(
        beads=orig_beads,
        edges=tuple(e for e in sketch.edges if e.label == 'survivor')
    )

    sketch = sketch.drop_deleted_inputs()
    assert orig_beads != sketch.beads
    assert len(sketch.edges) == 1
    assert len(bead(sketch, 'a1').inputs) == 0
    assert len(bead(sketch, 'b1').inputs) == 0
    assert len(bead(sketch, 'c1').inputs) == 1
    assert bead(sketch, 'c1').inputs[0].name == 'survivor'


# This class will handel all the storage activities. Bascially it will help in storing
# all the mouse and keyborad actions.

from os.path import exists
import json

class StorageController:

    def __init__(self, file):
        if exists(file):
            print("Storage file named {} already exists. Truncating its contents..".format(file))
            open(file, "w+").close()
        self.storage= open(file,"a+")
        self.data = []
    # store function will store movement to storage file
    def store(self, movement):
        self.data.append(movement)

    #close storage file
    def close(self):
        # Store all the recordings to json file
        json.dump(self.data, self.storage, indent=4)
        self.storage.close()

    def __del__(self):
        self.storage.close()

if __name__ == '__main__':
    storage= StorageController("storage")
    movement= {'source':'Mouse',
    'action':'Pressed',
    'detail':str((3,4))}
    storage.store(movement)


from django.urls import path
from . import views

urlpatterns = [
    # Drink URLs
    path('', views.drink_list, name='drink_list'),
    path('detail/<int:pk>/', views.drink_detail, name='drink_detail'),
    path('create/', views.drink_create, name='drink_create'),
    path('update/<int:pk>/', views.drink_update, name='drink_update'),
    path('delete/<int:pk>/', views.drink_delete, name='drink_delete'),
    path('add_stock/', views.add_stock, name='add_stock'),
    path('reduce_stock/', views.reduce_stock, name='reduce_stock'),

     # # Sale URLs
    path('sale_list/', views.sale_list, name='sale_list'),
    path('sale_create/', views.sale_create, name='sale_create'),
    path('<int:pk>/update/', views.sale_update, name='sale_update'),
    path('<int:pk>/delete/', views.sale_delete, name='sale_delete'),
    # # Sale Report URL
    path('sale_report/', views.sale_report, name='sale_report'),
    # debt URLs
    path('debt_list', views.debt_list, name='debt_list'),
    path('debts/<str:status>/', views.debt_list, name='debt_list'),
    path('clear-debt/<int:pk>/', views.clear_debt, name='clear_debt'),
    path('<int:pk>/debt_delete/', views.debt_delete, name='debt_delete'),

]


#encoding=utf-8
#py3.2

import os
from struct import pack, unpack
from pdb import set_trace as int3
from array import array

#class QuickIntDict
#    def __init__(self, max_key):
#        self.arrKey = array('i', )

class HcbParser:
    mnemonics = [
        0, 'InitStack', 'Call', 'CallSys', 'Ret', 'Ret1', 'Jmp', 'Jz',
        'Push0', 'Push1', 'PushI32', 'PushI16', 'PushI8', 'PushFloat', 'PushStr', 'PushGlobal',
        'PushStack', 'OP11', 'OP12', 'PushTop', 'PushTemp', 'PopGlobal', 'CpyStack', 'OP17',
        'OP18', 'Neg', 'Add', 'Sub', 'Mul', 'Div', 'Mod', 'Test',
        'CondAnd', 'CondOr', 'SetE', 'SetNE', 'SetG', 'SetLE', 'SetL', 'SetGE'
    ]

    def __init__(self, hcb):
        self.hcb = hcb
        self.text = []
        self.sysfunc = []
        self.func = []
        self.codeBlockLen = 0
        self.funcNameTable = {}
        self.codepage = '932'
        self.scan_flags = bytearray(len(hcb))

        self.txtTable = {}
        self.enCp = '932'
        self.entry_point = 0
        self.game_title = ''

    def readstr(self):
        return self.hcb.read(self.hcb.readu8()).rstrip(b'\0').decode(self.codepage)

    inst_len = [
        0, 2, 4, 2, 0, 0, 4, 4,
        0, 0, 4, 2, 1, 4, 0, 2,
        1, 2, 1, 0, 0, 2, 1, 2,
        1, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0
    ]

    def preProcess(self):
        self.codeBlockLen = self.hcb.readu32()
        self.hcb.seek(self.codeBlockLen)
        self.entry_point = self.hcb.readu32()
        self.hcb.seek(6, 1)
        self.game_title = self.readstr()
        sysfunc_count = self.hcb.readu16()
        for i in range(sysfunc_count):
            self.hcb.seek(1, 1)
            self.sysfunc.append(self.readstr())

    def scanFuncRecursive(self, address):
        self.hcb.seek(address)
        if self.scan_flags[self.hcb.tell()] == 1:
            return
        while 1:
            cur = self.hcb.tell()
            if cur >= self.codeBlockLen:
                break
            self.scan_flags[cur] = 1
            inst = self.hcb.readu8()
            if inst > 0x27 or inst == 0:
                int3()
            if inst == 0xe:
                self.readstr()
            elif inst > 7 or inst < 2 or inst == 3:
                self.hcb.seek(self.inst_len[inst], 1)
            elif inst == 2:
                to_addr = self.hcb.readu32()
                self.func.append(to_addr)
                self.scanFuncRecursive(to_addr)
            elif inst == 4 or inst == 5:
                return
            elif inst == 7:
                to_addr = self.hcb.readu32()
                self.scanFuncRecursive(to_addr)
            elif inst == 6:
                to_addr = self.hcb.readu32()
                self.scanFuncRecursive(to_addr)
                return

    def scanFunc2(self):
        self.hcb.seek(4)
        cur = self.hcb.tell()
        j = 0
        while cur < self.codeBlockLen:
            inst = self.hcb.readu8()
            if inst > 0x27 or inst == 0:
                print('%x' % self.hcb.tell())
                int3()
            if inst == 1:
                addr = self.hcb.tell()-1
                if addr not in self.func:
                    self.func.append(addr)
                self.hcb.seek(2, 1)
            elif inst == 2:
                addr = self.hcb.readu32()
                if addr not in self.func:
                    self.func.append(addr)
            elif inst == 0xe:
                self.readstr()
            else:
                self.hcb.seek(self.inst_len[inst], 1)
            cur = self.hcb.tell()
            if cur >= j*0x50000:
                print('%x/%x' % (cur, self.codeBlockLen))
                j += 1
        self.func.append(self.codeBlockLen)
        self.func.sort()

    def SetFuncNameTable(self, tbl, code):
        self.funcNameTable = dict(tbl)
        self.codepage = code

    def Parse(self):
        self.preProcess()
        print('Scanning Funcs...')
        self.scanFunc2()
        print('Parsing the script...')
        self.hcb.seek(4)
        cur = self.hcb.tell()
        func_it = 0
        j = 0
        while cur < self.codeBlockLen:
            if cur == self.func[func_it]:
                self.text.append('')
                if cur in self.funcNameTable:
                    self.text.append('Func %s(%08X)' %
                                     (self.funcNameTable[cur], cur))
                else:
                    self.text.append('Func #%04d(%08X)' % (func_it, cur))
                func_it += 1
            curline = '%08X:  ' % cur
            inst = self.hcb.readu8()
            if inst == 3:
                num = self.hcb.readu16()
                if num >= len(self.sysfunc):
                    int3()
                curline += self.mnemonics[inst]+' '+self.sysfunc[num]
            elif inst == 2:
                addr = self.hcb.readu32()
                if addr in self.funcNameTable:
                    curline += self.mnemonics[inst] + \
                        ' '+self.funcNameTable[addr]
                else:
                    curline += self.mnemonics[inst] + \
                        ' '+str(self.func.index(addr))
            elif inst == 0xe:
                curline += (self.mnemonics[inst]+' '+self.readstr())
            else:
                ilen = self.inst_len[inst]
                if ilen == 0:
                    opcode = ''
                elif ilen == 1:
                    opcode = ' %X' % self.hcb.readu8()
                elif ilen == 2:
                    opcode = ' %X' % self.hcb.readu16()
                elif ilen == 4:
                    opcode = ' %X' % self.hcb.readu32()
                curline += (self.mnemonics[inst]+opcode)
            self.text.append(curline)
            cur = self.hcb.tell()
            if cur >= j*0x50000:
                print('%x/%x' % (cur, self.codeBlockLen))
                j += 1
        return '\r\n'.join(self.text)

    def isFa(self, s):
        for c in s:
            if ord(c) >= 0x80:
                return True
        return False

    def ParseTxt(self, exBlocks, textStart):
        self.preProcess()
        print('Scanning Funcs...')
        self.scanFunc2()
        print('Parsing the script...')
        self.hcb.seek(4)
        idx = []
        cur = self.hcb.tell()
        func_it = 0
        j = 0
        print('Parsing 0000...')
        while 1:
            if cur == self.func[func_it]:
                for left, right in exBlocks:
                    if func_it == left:
                        func_it = right
                        cur = self.func[func_it]
                        self.hcb.seek(cur)
                if func_it == textStart:
                    break
                func_it += 1
            inst = self.hcb.readu8()
            if inst == 0xe:
                ss = self.readstr()
                if self.isFa(ss):
                    self.text.append(ss)
                    idx.append(pack('I', cur+1))
            else:
                self.hcb.seek(self.inst_len[inst], 1)
            cur = self.hcb.tell()
        fs = open('0000.txt', 'wb')
        fs.write('\r\n'.join(self.text).encode('U16'))
        fs.close()
        fs = open('0000.idx', 'wb')
        fs.write(b''.join(idx))
        fs.close()

        bopen = 0
        txt = []
        idx = []
        while cur < self.codeBlockLen:
            if cur == self.func[func_it]:
                print('Parsing %04d' % func_it)
                if bopen:
                    tfile.write('\r\n'.join(txt).encode('u16'))
                    idxfile.write(b''.join(idx))
                    tfile.close()
                    idxfile.close()
                txt = []
                idx = []
                tfile = open('%04d' % func_it+'.txt', 'wb')
                idxfile = open('%04d' % func_it+'.idx', 'wb')
                func_it += 1
                bopen = 1
            inst = self.hcb.readu8()
            if inst == 0xe:
                txt.append(self.readstr())
                idx.append(pack('I', cur+1))
            elif inst == 2:
                addr = self.hcb.readu32()
                if addr in self.funcNameTable:
                    txt.append('【'+self.funcNameTable[addr]+'】')
                    idx.append(pack('I', cur+1))
            else:
                self.hcb.seek(self.inst_len[inst], 1)
            cur = self.hcb.tell()
        tfile.write('\r\n'.join(txt).encode('u16'))
        idxfile.write(b''.join(idx))
        tfile.close()
        idxfile.close()

    def readAText(self, idxName, txtName):
        stm = open(idxName, 'rb').read()
        idxCnt = int(len(stm)/4)
        ls = open(txtName, 'rb').read().decode('u16').split('\r\n')
        if (idxCnt != len(ls) and idxCnt != len(ls)-1) or\
           (idxCnt == len(ls)-1 and ls[-1] != ''):
           raise Exception("txt lines not match")
        i = 0
        while i < idxCnt:
            idx, = unpack('I', stm[i*4:i*4+4])
            if idx in self.txtTable:
                raise Exception('duplicated idx:%x' % idx)
            self.txtTable[idx] = ls[i]
            i += 1

    def readTexts(self, txtFolder):
        for f in os.listdir(txtFolder):
            if f.endswith('.idx'):
                txtName = os.path.join(txtFolder, f.replace('.idx', '.txt'))
                idxName = os.path.join(txtFolder, f)
                try:
                    self.readAText(idxName, txtName)
                except Exception as e:
                    print('file %s excep: %s' % (txtName, str(e)))
                    return False
        return True

    def patchTxt(self, txtFolder, newName):
        self.preProcess()

        print('reading texts...')
        if not self.readTexts(txtFolder):
            return

        offTable = {}
        relocTable = []

        ns = bytearray(b'\0\0\0\0')
        cur = 4
        self.hcb.seek(cur)
        print('packing texts...')
        while cur < self.codeBlockLen:
            offTable[cur] = len(ns)
            inst = self.hcb.readu8()
            ns.append(inst)
            if inst == 2 or inst == 6 or inst == 7:
                # call
                addr = self.hcb.readu32()
                relocTable.append((len(ns), addr))
                ns += b'\0\0\0\0'
            elif inst == 0xe:
                if cur + 1 in self.txtTable:
                    s = self.txtTable[cur +
                                      1].replace('\\n', '\n').encode(self.enCp)
                    ns.append(len(s)+1)
                    ns += s
                    ns.append(0)
                    self.readstr()
                else:
                    slen = self.hcb.readu8()
                    ns.append(slen)
                    ns += self.hcb.read(slen)
            else:
                ilen = self.inst_len[inst]
                if ilen != 0:
                    ns += self.hcb.read(ilen)

            cur = self.hcb.tell()

        ns[0:4] = pack('I', len(ns))

        print('relocating...')
        for pos, addr in relocTable:
            ns[pos:pos+4] = pack('I', offTable[addr])
        ns += pack('I', offTable[self.hcb.readu32()])
        ns += self.hcb.read()

        print('writing file...')
        fs = open(newName, 'wb')
        fs.write(ns)
        fs.close()

        print('complete.')


import os
import json
import time
import random

from Logger import Logger
from Simulation import Simulation

def main(k):
    logger = Logger()
    for n in range(100, 10001, 100):
        start_n_time = time.time()
        if n % 1000 == 0:
            print(f"{n / 100}% done")
        n_data = []
        simulation = Simulation(n)
        simulation.run(k)
        n_data.append({"n": n, "answers": simulation.answers, "timeit": time.time() - start_n_time})
        logger.log(n, json.dumps(n_data))
        print(f"n={n} took {int(time.time() - start_n_time)} seconds")

if __name__ == "__main__":
    main(1)


import logging
import os

#--- Configuration parameters ---

loc_folder = ''  # I/O folder can be set here (Ex:loc_folder = 'C:/Users/USER/PycharmProjects/ProjectA'//). An empty string will look for resources in the root folder.
crypto = ['BTC/TUSD']  # Market pair chosen for trading
target_name = "Return"  # name of the target column
interval = '1s'  # Sample or Candle size of the imported dataset
future_points = 20  # Once the dataset is resampled this value defines how far in the future are we trying to predict (Ex: If resample size = 10 seconds and future_points = 18, the program is predicting at 180 seconds into the future)
resample_co = 15  # Resample size expressed as a number of intervals
time_steps = 30 * future_points  # "future_points" will be used as a base to calculate the sequence length of the LSTM RNN
sequences = 20  # This value represents the number of sequences that are being used in order to train the LSTM.
dense_units = 1  # Dense layer units, this should stay at 1.
dataset_name = 'candles.csv'  # Name of the data set that will be shared and updated constantly by the modeler and trader tools
hyper_loops = 100  # Number of random hyperparameter combinations to try for each combination of indicators that the tuner uses.
comb_loops = 50  # Number of random indicator combinations to try for each combination of hyperparameters that the tuner uses.
desc = 'A'  # Add a description to the output files of the modeler in case we need to save different models
validations = 5  # When modeler_mode is set to 'parallel' the model will calculate the accuracy N times with the same hyperparameters and set the score based on this average in order to get a more consistent value by train/test the model at different times.
#arr_list = ['so_data', 'ema_data', 'rsi_data']  # Once the best indicators have been found by the tuner and in order to use that combination in the modeler is necessary to update this list accordingly with that combination first.
model_reset = 14400  # Time expressed in seconds after which the modeler is reset to find a new model in order to keep the model updated.
modeler_mode = 'solo'  # Set if the modeler will work by itself in "solo" mode or in "parallel" mode with the trader to keep the model updated.
test_size = 0.3  # Fraction of the dataset that will be reserved for testing during modeling
asset_precision = 5
threshold_time = 21600  # A dataset will be built and constantly updated from the websocket seconds-candle stream of prices. This value sets a limit expressed in seconds on the length of this dataset
best_thres = 0  # The trader will use this value to filter weak predictions below a certain absolute value defined by this variable so that orders are only executed when signals are strong enough.
verbose = 0  # Level of description while executing model methods.

# Hyperparameter Tuner Configuration
'''This tool can be set in three different ways depending on the "hyper_mode" value:
hyper_mode = 0 --> Search for the best hyperparameters randomly in a number of combinations of indicators defined by comb_loops.
hyper_mode = 1 --> Search for the best hyperparameters randomly only in the combination of indicators defined in arr_list.
hyper_mode = 2 --> Search for the best hyperparameters randomly on all combinations of indicators.
hyper_mode = 3 --> Uses the default hyperparameter values on all random combinations of indicators.
'''
hyper_mode = 1  # The Hyperparameter finder can try different hyperparameters on all the indicators at the same time or try different Hyperparamenter on all combinations of

# Indicator list for the tuner, modeler, and trader:
arr_list = ['so', 'pvo', 'ema', 'rsi', 'srsi', 'cci', 'psar', 'vwap']  # Once the best indicators have been found by the tuner and in order to use that combination in the modeler is necessary to update this list accordingly with that combination first.
#arr_list = ['srsi', 'cci']
# No indicatorsLayers: 10 Epochs: 90 Batch Size: 15 Dropout Rate: 0.2 Learning Rate: 1e-06 Input Act. Func: tanh Output Act. Func: tanh

# Default hyperparameters for the Tuner and Modeler (If hyper_mode = 2 the tuner will use these):
layers = 64
dropout = 0.2
learning_rate = 0.000001
act_input = 'tanh'
act_output = 'tanh'
epochs = 120
batch_size = 10

# Logger config

file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'Logger.log')
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%m-%d %H:%M', filename=file, filemode='a')
console = logging.StreamHandler()
logging.getLogger('').addHandler(console)
logger = logging.getLogger('Log')

# Files config
model_name = f'model-{desc}.h5'
scalers_name = f'scalers-{desc}.pkl'


import pytest

@pytest.fixture(scope="module")
def neox_handle(launcher):
    with launcher(
        "stabilityai/stablelm-tuned-alpha-3b", num_shard=1, use_flash_attention=False
    ) as handle:
        yield handle

@pytest.fixture(scope="module")
async def neox(neox_handle):
    await neox_handle.health(300)
    return neox_handle.client

@pytest.mark.skip
@pytest.mark.asyncio
async def test_neox(neox, response_snapshot):
    response = await neox.generate(
        "<|USER|>What's your mood today?<|ASSISTANT|>",
        max_new_tokens=10,
        decoder_input_details=True,
    )

    assert response.details.generated_tokens == 10
    assert response == response_snapshot

@pytest.mark.skip
@pytest.mark.asyncio
async def test_neox_load(neox, generate_load, response_snapshot):
    responses = await generate_load(
        neox,
        "<|USER|>What's your mood today?<|ASSISTANT|>",
        max_new_tokens=10,
        n=4,
    )

    generated_texts = [r.generated_text for r in responses]

    assert len(generated_texts) == 4
    assert generated_texts, all(
        [text == generated_texts[0] for text in generated_texts]
    )

    assert responses == response_snapshot


import random

from car import Car

STARTING_MOVE_DISTANCE = 100
MOVE_INCREMENT = 10

class CarManager:

    def __init__(self):
        self.list = []
        for n in range(0, 4):
            for i in range(0, 4):
                car = Car((
                    (i * 150 + random.random() * 75) * (-1 ** random.randint(1, 2)),
                    (n + 1) * 80 - 150), MOVE_INCREMENT)
                self.list.append(car)

    def move(self):
        for n in self.list:
            n.forward(MOVE_INCREMENT)

    def collide(self, player):
        for n in self.list:
            if n.collide(player):
                return True
        return False


def all_equal(a):
    num = a[0]
    for x in a:
        if x != num:
            return False
    return True

def main():
    x = [1, 1, 2]
    print(all_equal(x))

if __name__ == "__main__":
    main()


with open("day10/input.txt") as file_in:
    lines = []
    for line in file_in:
        lines.append(line.replace("\n", "").split(" "))
xVal = 1
iterNum = 1
dict = {iterNum: xVal}
for line in lines:
    if line == ["noop"]:
        iterNum += 1
    elif line[0] == "addx":
            iterNum += 2
            xVal += int(line[1])
    dict[iterNum] = xVal

points = []

for position in range(240):
    cycle = position + 1
    curVal = dict[cycle] if cycle in dict else dict[cycle-1] if cycle-1 in dict else dict[cycle-2] if cycle-2 in dict else 0
    if -1 <= position % 40 - curVal <= 1:
        points.append(position)

# draw board
for y in range(6):
    for x in range(40):

        # check by row, y*40 gets row becasue each row is 40 long and therefore we can reduce to the x variable
        if x + y * 40 in points:
            # if the value is in drawn then print a ■ otherwise print a space
            print("█", end="")
        else:
            print(" ", end="")
    print()


import numpy as np
from matplotlib import pyplot as plt

#simulate data
ys = [np.random.uniform(0, 10, 100) for i in range(5)]	#boxplot takes list as input

#start with a frameless plot (extra room on the left)
plt.subplot2grid((10,10), (0,0), 9, 10, frameon=False)

#label axes
plt.ylabel("magnitude", fontsize=14)

#define offsets
xs = range(len(ys))

xmin = 1	#boxplot indexing starts at 1
xmax = len(ys)
x_range = xmax - xmin
x_start = xmin - x_range/10.	#larger offset for boxplot
x_end = xmax + x_range/10.

ymin = min([min(y) for y in ys])
ymax = max([max(y) for y in ys])
y_range = ymax - ymin
y_start = ymin - y_range/25.
y_end = ymax + y_range/25.

#plot data
plt.boxplot(ys, notch=True, patch_artist=True, labels=("a", "b", "c", "d", "e"), medianprops=dict(linestyle="none"))	#boxplot has built-in support for labels, unlike barplot

#define axes with offsets
plt.axis([x_start, x_end, y_start, y_end], frameon=False)

#plot axes (black with line width of 4)
plt.axvline(x=x_start, color="k", lw=4)
plt.axhline(y=y_start, color="k", lw=4)

#plot ticks
plt.tick_params(direction="out", top=False, right=False, length=12, width=3, pad=5, labelsize=12)

plt.savefig("box")
plt.show()


import os
import copy
import shutil

import pytest

import payu

from test.common import cd
from test.common import tmpdir, ctrldir, labdir, expt_archive_dir
from test.common import config as config_orig
from test.common import write_config
from test.common import make_all_files, make_random_file, write_metadata
from test.common import make_expt_archive_dir

verbose = True

# Global config
config = copy.deepcopy(config_orig)

# Enable metadata
config.pop('metadata')
pytestmark = pytest.mark.filterwarnings(
    "ignore::payu.git_utils.PayuGitWarning")

def setup_module(module):
    """
    Put any test-wide setup code in here, e.g. creating test files
    """
    if verbose:
        print("setup_module      module:%s" % module.__name__)

    # Should be taken care of by teardown, in case remnants lying around
    try:
        shutil.rmtree(tmpdir)
    except FileNotFoundError:
        pass

    try:
        tmpdir.mkdir()
        labdir.mkdir()
        ctrldir.mkdir()
        make_all_files()
        write_metadata()
    except Exception as e:
        print(e)

    # Create 5 restarts and outputs
    for dir_type in ['restart', 'output']:
        for i in range(5):
            path = make_expt_archive_dir(type=dir_type, index=i)
            make_random_file(os.path.join(path, f'test-{dir_type}00{i}-file'))

def teardown_module(module):
    """
    Put any test-wide teardown code in here, e.g. removing test outputs
    """
    if verbose:
        print("teardown_module   module:%s" % module.__name__)

    try:
        shutil.rmtree(tmpdir)
        print('removing tmp')
    except Exception as e:
        print(e)

def setup_sync(additional_config, add_envt_vars=None):
    """Given additional configuration and envt_vars, return initialised
    class used to build/run rsync commands"""
    # Set experiment config
    test_config = copy.deepcopy(config)
    test_config.update(additional_config)
    write_config(test_config)

    # Set up Experiment
    with cd(ctrldir):
        lab = payu.laboratory.Laboratory(lab_path=str(labdir))
        experiment = payu.experiment.Experiment(lab, reproduce=False)

    # Set enviroment vars
    if add_envt_vars is not None:
        for var, value in add_envt_vars.items():
            os.environ[var] = value

    return payu.sync.SyncToRemoteArchive(experiment)

def assert_expected_archive_paths(source_paths,
                                  expected_dirs,
                                  expected_protected_dirs):
    """Check given source archive source paths that it includes
    the expected dirs to sync"""
    dirs, protected_dirs = [], []
    for source_path in source_paths:
        path = source_path.path
        assert os.path.dirname(path) == str(expt_archive_dir)

        dir = os.path.basename(path)
        if source_path.protected:
            protected_dirs.append(dir)
        else:
            dirs.append(dir)

    assert dirs == expected_dirs
    assert protected_dirs == expected_protected_dirs

@pytest.mark.parametrize(
    "envt_vars, expected_outputs, expected_protected_outputs",
    [
        (
            {},
            ['output000', 'output001', 'output002', 'output003'], ['output004']
        ),
        (
            {'PAYU_SYNC_IGNORE_LAST': 'True'},
            ['output000', 'output001', 'output002', 'output003'], []
        ),
    ])
def test_add_outputs_to_sync(envt_vars, expected_outputs,
                             expected_protected_outputs):
    sync = setup_sync(additional_config={}, add_envt_vars=envt_vars)

    # Test function
    sync.add_outputs_to_sync()

    # Assert expected outputs and protected outputs are added
    assert_expected_archive_paths(sync.source_paths,
                                  expected_outputs,
                                  expected_protected_outputs)

    # Tidy up test - Remove any added enviroment variables
    for envt_var in envt_vars.keys():
        del os.environ[envt_var]

@pytest.mark.parametrize(
    "add_config, envt_vars, expected_restarts, expected_protected_restarts",
    [
        (
            {
                "sync": {
                    'restarts': True
                },
                "restart_freq": 5
            }, {},
            [], ['restart000']
        ),
        (
            {
                "sync": {
                    'restarts': True
                },
                "restart_freq": 2
            }, {},
            ['restart000', 'restart002'], ['restart004']
        ),
        (
            {
                "sync": {
                    "restarts": True
                },
                "restart_freq": 2
            }, {'PAYU_SYNC_IGNORE_LAST': 'True'},
            ['restart000', 'restart002'], []
        ),
        (
            {"restart_freq": 3}, {'PAYU_SYNC_RESTARTS': 'True'},
            ['restart000', 'restart001', 'restart002'],
            ['restart003', 'restart004']
        ),
    ])
def test_restarts_to_sync(add_config, envt_vars,
                          expected_restarts, expected_protected_restarts):
    sync = setup_sync(add_config, envt_vars)

    # Test function
    sync.add_restarts_to_sync()

    # Assert expected restarts and protected restarts are added
    assert_expected_archive_paths(sync.source_paths,
                                  expected_restarts,
                                  expected_protected_restarts)

    # Tidy up test - Remove any added enviroment variables
    for envt_var in envt_vars.keys():
        del os.environ[envt_var]

def test_set_destination_path():
    additional_config = {
        "sync": {
            "url": "test.domain",
            "user": "test-usr",
            "path": "remote/path",
        }}
    sync = setup_sync(additional_config=additional_config)

    # Test destination_path
    sync.set_destination_path()
    assert sync.destination_path == "test-usr@test.domain:remote/path"

    # Test value error raised when path is not set
    sync = setup_sync(additional_config={})
    with pytest.raises(ValueError):
        sync.set_destination_path()

@pytest.mark.parametrize(
    "add_config, expected_excludes",
    [
        (
            {
                "sync": {
                    "exclude": ["iceh.????-??-??.nc", "*-DEPRECATED"]
                },
                "collate": {
                    "enable": True
                }
            }, ("--exclude iceh.????-??-??.nc --exclude *-DEPRECATED"
                " --exclude *.nc.*")
        ),
        (
            {
                "sync": {
                    "exclude_uncollated": False
                },
                "collate": {
                    "enable": True
                }
            }, ""
        ),
        (
            {
                "sync": {
                    "exclude": "*-DEPRECATED"
                },
                "collate": {
                    "enable": False
                }
            }, "--exclude *-DEPRECATED"
        )
    ])
def test_set_excludes_flags(add_config, expected_excludes):
    sync = setup_sync(additional_config=add_config)

    # Test setting excludes
    sync.set_excludes_flags()
    assert sync.excludes == expected_excludes

def test_sync():
    # Add some logs
    pbs_logs_path = os.path.join(expt_archive_dir, 'pbs_logs')
    os.makedirs(pbs_logs_path)
    log_filename = 'test_s.e1234'
    test_log_content = 'Test log file content'
    with open(os.path.join(pbs_logs_path, log_filename), 'w') as f:
        f.write(test_log_content)

    # Add nested directories to output000
    nested_output_dirs = os.path.join('output000', 'submodel', 'test_sub-dir')
    nested_output_path = os.path.join(expt_archive_dir, nested_output_dirs)
    os.makedirs(nested_output_path)

    # Add empty uncollated file
    uncollated_file = os.path.join(nested_output_dirs, 'test0.res.nc.0000')
    with open(os.path.join(expt_archive_dir, uncollated_file), 'w'):
        pass

    # Add empty collated file
    collated_file = os.path.join(nested_output_dirs, 'test1.res.nc')
    with open(os.path.join(expt_archive_dir, collated_file), 'w'):
        pass

    # Remote archive path
    remote_archive = tmpdir / 'remote'

    additional_config = {
        "sync": {
            "path": str(remote_archive),
            "runlog": False
        }
    }
    sync = setup_sync(additional_config)

    # Function to test
    sync.run()

    expected_dirs_synced = {'output000', 'output001', 'output002',
                            'output003', 'output004',
                            'pbs_logs', 'metadata.yaml'}

    # Test output is moved to remote dir
    assert set(os.listdir(remote_archive)) == expected_dirs_synced

    # Test inner log files are copied
    remote_log_path = os.path.join(remote_archive, 'pbs_logs', log_filename)
    assert os.path.exists(remote_log_path)

    with open(remote_log_path, 'r') as f:
        assert test_log_content == f.read()

    # Check nested output dirs are synced
    assert os.path.exists(os.path.join(remote_archive, nested_output_dirs))

    # Check that uncollated files are not synced by default
    assert not os.path.exists(os.path.join(remote_archive, uncollated_file))
    assert os.path.exists(os.path.join(remote_archive, collated_file))

    # Check synced file still exist locally
    local_archive_dirs = os.listdir(expt_archive_dir)
    for dir in expected_dirs_synced:
        assert dir in local_archive_dirs

    # Test sync with remove synced files locally flag
    additional_config['sync']['remove_local_files'] = True
    sync = setup_sync(additional_config)
    sync.run()

    # Check synced files are removed from local archive
    # Except for the protected paths (last output in this case)
    for output in ['output000', 'output001', 'output002', 'output003']:
        file_path = os.path.join(expt_archive_dir, dir, f'test-{output}-file')
        assert not os.path.exists(file_path)

    last_output_path = os.path.join(expt_archive_dir, 'output004')
    last_output_file = os.path.join(last_output_path, f'test-output004-file')
    assert os.path.exists(last_output_file)

    # Test sync with remove synced dirs flag as well
    additional_config['sync']['remove_local_dirs'] = True
    sync = setup_sync(additional_config)
    sync.run()

    # Assert synced output dirs removed (except for the last output)
    local_archive_dirs = os.listdir(expt_archive_dir)
    for output in ['output000', 'output001', 'output002', 'output003']:
        assert output not in local_archive_dirs
    assert 'output004' in local_archive_dirs


# pylint: disable=missing-function-docstring       # not a requirement for functions that are tests
# pylint: disable=redefined-outer-name             # need this to be able to use fixtures

import pytest
import requests_mock
from click.testing import CliRunner

from fairtally import __version__
from fairtally.cli import cli

@pytest.fixture
def mocked_internet():
    with requests_mock.Mocker() as mocker:
        repo = "https://github.com/fair-software/repo1"
        raw = "https://raw.githubusercontent.com/fair-software/repo1"
        api = "https://api.github.com/repos/fair-software/repo1"
        mocker.get(api, status_code=200, json={"default_branch": "master"})
        mocker.get(api + "/license", status_code=200)
        mocker.get(raw + "/master/.howfairis.yml", status_code=404)
        mocker.get(raw + "/master/.zenodo.json", status_code=404)
        mocker.get(raw + "/master/CITATION.cff", status_code=404)
        mocker.get(raw + "/master/CITATION", status_code=404)
        mocker.get(raw + "/master/codemeta.json", status_code=404)
        mocker.get(raw + "/master/README.rst", status_code=404)
        mocker.get(raw + "/master/README.md", status_code=404)
        mocker.get(raw + "/master/this/path/does-not-exist/.howfairis.yml", status_code=404)
        mocker.get(raw + "/master/this/path/does-not-exist/.zenodo.json", status_code=404)
        mocker.get(raw + "/master/this/path/does-not-exist/CITATION.cff", status_code=404)
        mocker.get(raw + "/master/this/path/does-not-exist/CITATION", status_code=404)
        mocker.get(raw + "/master/this/path/does-not-exist/codemeta.json", status_code=404)
        mocker.get(raw + "/master/this/path/does-not-exist/README.md", status_code=404)
        mocker.get(raw + "/master/this/path/does-not-exist/README.rst", status_code=404)
        mocker.get(repo, status_code=200, text="<html>GitHub page for repo1</html>")
        return mocker

@pytest.fixture
def cli_runner():
    return CliRunner(mix_stderr=False)

@pytest.fixture
def invoke_cli(mocked_internet, cli_runner):
    def _invoker(args, **kwargs):
        with mocked_internet:
            return cli_runner.invoke(cli, args, catch_exceptions=False, **kwargs)

    return _invoker

def test_help(invoke_cli):
    result = invoke_cli("--help")
    assert "Usage: fairtally" in result.stdout

def test_version(invoke_cli):
    result = invoke_cli("--version")
    assert __version__ in result.stdout

def test_no_url(invoke_cli):
    result = invoke_cli("")

    assert "No URLs provided, aborting." in result.stderr
    assert result.exit_code == 1

def read_file(filename):
    with open(filename, "r") as fid:
        return fid.read()

def test_single_url_no_options(invoke_cli, cli_runner: CliRunner):
    with cli_runner.isolated_filesystem():
        url = "https://github.com/fair-software/repo1"
        result = invoke_cli(url)

        assert "fairtally progress" in result.stderr
        assert "Completed fairtally on 1 URLs and written report to tally.html" in result.stderr
        assert result.exit_code == 0
        assert url in read_file("tally.html")

def test_input_from_file_to_stdout(invoke_cli, tmp_path):
    my_input_file = tmp_path / "urls.txt"
    my_input_file.write_text("https://github.com/fair-software/repo1\n")

    result = invoke_cli(["--input-file", str(my_input_file), "--output-file", "-"])

    assert "fairtally progress" in result.stderr
    assert "Completed fairtally on 1 URLs and written report to -" in result.stderr
    assert result.exit_code == 0
    assert "https://github.com/fair-software/repo1" in result.stdout

def test_input_from_stdin_to_stdout(invoke_cli):
    stdin = "https://github.com/fair-software/repo1\n"

    result = invoke_cli(["--input-file", "-", "--output-file", "-"], input=stdin)

    assert "fairtally progress" in result.stderr
    assert "Completed fairtally on 1 URLs and written report to -" in result.stderr
    assert result.exit_code == 0
    assert "https://github.com/fair-software/repo1" in result.stdout

def test_json_format(invoke_cli, cli_runner):
    with cli_runner.isolated_filesystem():
        url = "https://github.com/fair-software/repo1"
        result = invoke_cli([url, "--format", "json"])

        assert "fairtally progress" in result.stderr
        assert "Completed fairtally on 1 URLs and written report to tally.json" in result.stderr
        assert result.exit_code == 0
        assert url in read_file("tally.json")

def test_json_format_to_stdout(invoke_cli):
    url = "https://github.com/fair-software/repo1"
    result = invoke_cli([url, "--format", "json", "--output-file", "-"])

    assert "fairtally progress" in result.stderr
    assert "Completed fairtally on 1 URLs and written report to -" in result.stderr
    assert result.exit_code == 0
    assert url in result.stdout


import logging
import numpy as np
from math import sqrt, degrees
from scipy.spatial import distance

from src.utils.constants import EDGE_PAIRS_FOR_ANGLES, VERTICES_TO_IGNORE, IMPORTANT_FEATURES

logging.basicConfig(level=logging.INFO)

def get_sin_and_cos(a, b):
    c = sqrt(a ** 2 + b ** 2)
    cos = a / c
    sin = b / c
    return sin, cos

def rotate_pose(landmarks, rotation_matrix):
    rotated_vertices = []
    for frame_vertex in landmarks:
        point = np.array(frame_vertex)
        rotated = np.matmul(rotation_matrix, point)
        rotated_vertices.append((rotated[0], rotated[1], rotated[2]))
    return rotated_vertices

def stop_rotation_around_x(frame_vertices, reference_joints):
    point_1 = frame_vertices[reference_joints[0]]
    arr = np.array([frame_vertices[5], frame_vertices[13], frame_vertices[17]])
    point_2 = arr.sum(axis=0)

    a = point_2[1] - point_1[1]
    b = point_2[2] - point_1[2]

    sin, cos = get_sin_and_cos(a, b)
    rotation_matrix_x = np.array([[1, 0, 0],
                                  [0, cos, sin],
                                  [0, -sin, cos]])

    rotated_vertices = rotate_pose(frame_vertices, rotation_matrix_x)
    return rotated_vertices, np.arcsin(sin)

def stop_rotation_around_y(frame_vertices, reference_joints):
    point_1 = frame_vertices[reference_joints[0]]

    arr = np.array([frame_vertices[5], frame_vertices[13], frame_vertices[17]])
    point_2 = arr.sum(axis=0)

    a = point_2[0] - point_1[0]
    b = point_2[2] - point_1[2]

    sin, cos = get_sin_and_cos(a, b)
    rotation_matrix_y = np.array([[cos, 0, sin],
                                  [0, 1, 0],
                                  [-sin, 0, cos]])
    rotated_vertices = rotate_pose(frame_vertices, rotation_matrix_y)
    return rotated_vertices, np.arcsin(sin)

def stop_rotation_around_z(frame_vertices, reference_joints):
    point_1 = frame_vertices[reference_joints[0]]
    point_2 = frame_vertices[reference_joints[1]]

    a = point_2[0] - point_1[0]
    b = point_2[1] - point_1[1]

    sin, cos = get_sin_and_cos(a, b)

    rotation_matrix_z = np.array([[cos, sin, 0],
                                  [-sin, cos, 0],
                                  [0, 0, 1]])

    rotation_matrix_z_2 = np.array([[0, -1, 0],
                                    [1, 0, 0],
                                    [0, 0, 1]])

    rotated_vertices = []
    for frame_vertex in frame_vertices:
        point = np.array(frame_vertex)
        rotated = np.matmul(rotation_matrix_z, point)
        rotated = np.matmul(rotation_matrix_z_2, rotated)
        rotated_vertices.append((rotated[0], rotated[1], rotated[2]))
    return rotated_vertices, np.arcsin(sin)

def scale_vertices(frame_vertices, scale_factor_v=1):
    # frame_vertices = [(point[0] * 1000, point[1] * 1000, point[2] * 1000) for point in frame_vertices]

    wrist_joint = frame_vertices[0]
    middle_base_joint = frame_vertices[9]
    base_limb_length = distance.euclidean(wrist_joint, middle_base_joint)
    scale_ratio_v = scale_factor_v / base_limb_length
    base_vector_v = [middle_base_joint[i] - wrist_joint[i] for i in range(0, 3)]
    unit_vector_v = _unit_vector(base_vector_v)

    base_limb_length2 = distance.euclidean(frame_vertices[17], frame_vertices[5])
    scale_ratio_h = 0.7 / base_limb_length2
    unit_vector_h = _unit_vector([frame_vertices[17][i] - frame_vertices[5][i] for i in range(0, 3)])

    processed = []
    for land_mark in frame_vertices:
        processed.append([land_mark[0] * scale_ratio_h * unit_vector_h[0],
                          land_mark[1] * scale_ratio_v * unit_vector_v[1],
                          land_mark[2] * (scale_ratio_v + scale_ratio_h) / 2])
    # for land_mark in frame_vertices:
    #     processed.append([land_mark[0] * scale_ratio_v,
    #                       land_mark[1] * scale_ratio_v,
    #                       land_mark[2] * scale_ratio_v])
    return processed, None

def normalize_movement(frame_vertices, angles=None):
    origin_coordinates = frame_vertices[0]
    relocated_vertices = []
    for frame_vertex in frame_vertices:
        relocated_vertices.append((frame_vertex[0] - origin_coordinates[0], frame_vertex[1] - origin_coordinates[1],
                                   frame_vertex[2] - origin_coordinates[2]))
    return relocated_vertices, angles

def pre_process(land_marks):
    steps = [
        normalize_movement,
        stop_rotation_around_x,
        stop_rotation_around_z,
        stop_rotation_around_y,
        scale_vertices
    ]
    args_for_steps = [
        (),
        ([0, 9],),
        ([0, 9],),
        ([5, 17],),
        ()
    ]

    processed = land_marks
    rotations = []
    for idx, step in enumerate(steps):
        processed, rotation = step(processed, *args_for_steps[idx])
        if rotation: rotations.append(degrees(rotation))
    return tuple(processed), rotations

def get_angle(v1, v2):
    # TODO: Check rotations implementation
    # Check rotation reference lines
    # Check how angles>90
    def unit_vector(vector):
        """ Returns the unit vector of the vector.  """
        return vector / np.linalg.norm(vector)

    def angle_between(v1, v2):
        v1_u = unit_vector(v1)
        v2_u = unit_vector(v2)
        return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))

    angle = angle_between(v1, v2)
    return degrees(np.pi - angle)

def flatten_points(land_marks: list):
    flatten_coordinates = []
    for point in land_marks:
        for coordinate in point:
            flatten_coordinates.append(coordinate)
    return flatten_coordinates

def un_flatten_points(flatten_coordinates: list):
    landmark_points = []
    for i in range(0, len(flatten_coordinates), 3):
        landmark_points.append(flatten_coordinates[i:i + 3])
    return landmark_points

def _unit_vector(vector):
    """ Returns the unit vector of the vector.  """
    return vector / np.linalg.norm(vector)

def pre_process_single_frame(land_mark):
    if not land_mark: return
    current_vertices, angles = pre_process(land_mark)
    return current_vertices, angles

def run_pre_process_steps(pose_q, processed_q_1, duplicate_queues=None, for_labelling=False):
    logging.info('Pre-processing...')
    while True:
        try:
            if not pose_q.empty():
                landmarks, frame_no = pose_q.get()
                landmarks, original_angles = build_feature_vector(landmarks)
                processed_q_1.put((landmarks, original_angles))
                if duplicate_queues:
                    for q in duplicate_queues:
                        if for_labelling:
                            q.put(((landmarks, original_angles), frame_no))
                        else:
                            q.put((landmarks, original_angles))
            else:
                pass
        except Exception as e:
            logging.error(e)
            break

def flatten(landmark, decimal_points=4):
    lm_row = []
    for landmark_point in landmark:
        lm_row.extend(np.round(landmark_point, decimal_points))
    return lm_row

def get_angles(landmarks):
    angles = []
    for limb_pair in EDGE_PAIRS_FOR_ANGLES:
        limb2 = [landmarks[limb_pair[1][1]][i] - landmarks[limb_pair[1][0]][i] for i in range(0, 3)]
        limb1 = [landmarks[limb_pair[0][1]][i] - landmarks[limb_pair[0][0]][i] for i in range(0, 3)]
        angle = get_angle(limb2, limb1)
        angles.append((angle - 90) / 90)

    return angles

def select_important_features(flat_co_and_angles):
    selected = []
    for ft in IMPORTANT_FEATURES:
        selected.append(flat_co_and_angles[ft])
    return selected

def build_feature_vector(landmarks, ratio=1):
    preprocessed_landmark, rotations = pre_process(landmarks)
    angles = get_angles(preprocessed_landmark)
    if ratio != 1:
        angles = [angle * ratio for angle in angles]
    preprocessed_landmark = list(preprocessed_landmark)
    for index in sorted(VERTICES_TO_IGNORE, reverse=True):
        del preprocessed_landmark[index]

    flatted = flatten(preprocessed_landmark)
    flatted.extend(angles)
    flatted = select_important_features(flatted)
    return flatted, rotations


# -*- coding: utf-8 -*-
"""
Created on Mon Apr 16 15:13:19 2018

@author: Proprietario
https://github.com/mayoyamasaki/saliency-map
"""

#import sys
from src.saliency_map import SaliencyMap
#from src.utils import OpencvIo
import cv2
# =============================================================================
#
# if __name__ == "__main__":
#     oi = OpencvIo()
#     src = oi.imread(sys.argv[1])
#     sm = SaliencyMap(src)
#     oi.imshow_array([sm.map])
# =============================================================================

class Image_Saliency_2():

    def __init__(self, imgpath):
        self.img = cv2.imread(imgpath)
        self.filename = imgpath

    def runSaliency2(self):

        sm = SaliencyMap(self.filename)

        cv2.imshow('sal22', sm)
        cv2.imshow('orig', self.img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()


import heapq

t = int(input())
for _ in range(t):
    n = int(input())
    L, R = [], []
    N = [[] for j in range(n)]
    P = [[] for j in range(n + 1)]
    for i in range(n):
        k, l, r = map(int, input().split())
        if l - r > 0:
            N[k - 1].append(i)
        else:
            P[k].append(i)
        L.append(l)
        R.append(r)
    ans = sum([min(L[i], R[i]) for i in range(n)])
    S, T = [], []
    for j in range(n):
        for i in N[j]:
            heapq.heappush(S, L[i] - R[i])
        for i in P[n - 1 - j]:
            heapq.heappush(T, R[i] - L[i])
        while len(S) > j + 1:
            heapq.heappop(S)
        while len(T) > j + 1:
            heapq.heappop(T)
    print(ans + sum(S) + sum(T))


"""
Boolean Logic
##########

Boolean logic, put simply, means ways of combining the values of True and False.

Python has many built in operators to comparing values and combining booleans.
We'll briefly cover these below, so we can go onto the awesomeness of conditionals!

http://learnpythonthehardway.org/book/ex27.html
https://docs.python.org/3/library/stdtypes.html
"""

#Just to recap, the simplest forms of booleans are like so
true = True
false = False

#Note how True and False are case sensitive!

#The first operator to cover is equality. This is true if two variables
#(or literals, such as 3.0, or "Text") are equal in value.
#Note, the operator is TWO equal signs.
#One equals sign is assignment to a variable. Two is a check for equality.

#This is True
3 == 3
"Text" == "Text"

#This is False
3 == 2
"Text" == "Banana"

#There's also the opposite of equality.... inequality! The not equal operator
#works the same as equality, but is False is they're equal, and True if they're
#not equal. The exact opposite!

#These are now False
3 != 3
"Text" != "Text"

#These are now all True!
3 != 2
"Text" != "Banana"

#Let's look at some operators for mainly working with numbers! Huzzah, numbers!
#Firstly, greater than and its opposite, less than.
#They check if one number is greater than another or, equivalently, less than
#another number!

#Greater than in action
#These are all True!
3 > 2
1.2 > 1.1

#Equivalently, these are all False
2 > 3
1.1 > 1.2

#Less than works exactly the same, but the operator is the other way around!
#These are now all True!
2 < 3
1.1 < 1.2

#And these are now all False!
3 < 2
1.2 < 1.1

#The way to remember these is simple:
"""The mouth of the crocodile is always towards the bigger number if it's True"""
#or
"""The bigger end of the operator is towards the bigger number"""

#However, these operators are known as "strictly less than", and vice versa.
#As in, these are False
2 < 2
2 > 2

#But there's a solution! Two operators called "Greater than or equal to" and,
#of course, "Less than or equal to"
#These are now True! Huzzah!
2 <= 2
2 >= 2

#The final important operator is "not". It's really simple but really, really
#useful. It just flips the result of whatever it's in front of!
#I.e False becomes True, True becomes False!

#Here's a few examples
#These ALL evaluate to True
not 2 == 3
not 2 > 3
not 5 < 2
not 2 <= 2

#Everything here can also be applied to variables!
true = True
false = False

#False
true == false

#True
true == (not false) #Brackets needed... explained later.
not (true == false)

#Finally, there's two special magical operators that only work on values of True
#and False. These are extremely important for combining the above things into
#Something more useful. They're called "and" and "or"!
#This reference is really useful if you're uncertain how these work:
# http://learnpythonthehardway.org/book/ex27.html

#"and" is True whenever the things on either side of it are True!
#Otherwise, it's False.
#These are all True
True and True
1 == 1 and 2 == 2
1 == 1 and 2 < 3
False and False
1 == 1 and not 2 == 3

#These are all False
True and False
False and True
1 == 1 and 2 == 3
1 <= 2 and 3 <= 2
1 == 1 and not 2 == 2

#The "or" operator is similar to "and", in that it only works on True and False.
#Logical "Or" is True whenever the thing on its left OR the thing on the right
#are True. It's False only if the thing on the left and the thing on the right
#are both False.

#This is all True!
True or False
False or True
True or True
1 == 1 or False
False or 2 < 3
2 < 3 or 2 > 3 #Left one is True, right one is False

#This is all False
False or False
2 > 3 or 3 > 4
False or 2 == 3

#You can also combine "and" and "or" with... anything else.
#To make sure things are worked out in the right order, you can wrap things
#In brackets. Anything in brackets is calculated first, like in maths!
#Try and follow the following examples

#These are all True
(False or True) and True
True and True and True and (True or False)
False or (False or (False or (False or True)))
1 == 1 and (2 == 2 or 2 > 3)

#These are all False
(False and True) or False
False or False or False or False or False and True

#Etc. It's confusing, sometimes you have to spent quite a while thinking about
#how to do logic like this! After a while, it'll be almost second nature :)

"""
Conditionals
##########

Conditional statements are a way of choosing bits of code to run.

How they work: A statement (be it a variable, a function call or an operator) is
used as a condition to decide to which way to go. If the statement is equal to
anything but "False" or "None" then the condition is true.

Again, examples!
"""

#This is the most basic conditional.
#Notice how the layout is similar to a function. The code to be run is
#indented once so it's inside the conditional.
if True:
    print("Hello world!")

#Note, how this next print statement DOESN'T run
if False:
    print("This will never be seen!")

#You can put... well, pretty much anything we've seen above into these!
if 1 < 2:
    print("1 is less than 2!")

if 2 == 2:
    print("2 is equal to 2! Who knew?!")

if 3 < 2:
    print("But 3 isn't less than 2. So we'll never see this :(")

if 2 == 2 and False:
    print("One side of the and is False. We'll never see this! Curses!")

if not True:
    print("Not displayed, because it's not True!")

#It goes on like this... hell, we can have some functions!
if len("This text is 32 characters long!") == 32:
    print("Wow, it actually is 32 characters long!")

#You can also have an "else" block. This executes if the condition is False.
#I.e, If the first block doesn't run, the else block does.

if False:
    print("This won't display as the condition is False.")
else:
    print("This will, because it's after an else!")


N = int(input())
t = [tuple(map(int, input().split())) for _ in range(N)]

def can(x):
    m = sorted((x-h)//s for h, s in t)
    for i, j in enumerate(m):
        if j<0 or i>j:
            return False
    return True

l, r = 0, 10**15

while r-l>1:
    m = (l+r)//2
    if can(m):
        r = m
    else:
        l = m

print(r)


import re;

# 正規表現のパターンに位置するものを画面に出力
words = ["orange", "october", "octpus", "order", "order", "banana", "baby", "busy"]
pattern = r"^oc.*$"
print("ocで始まるパターン=", pattern)
for word in words:
    if re.match(pattern, word):
        print("=", word)
        # = october
        # = octpus

words = ["orange", "october", "octpus", "order", "order", "banana", "baby", "busy"]
pattern = r"^b.*y$"
print("bで始まりyで終わるパターン", pattern)
for word in words:
    if re.match(pattern, word):
        print("=", word)
        # = baby
        # = busy

words = ["hoge.py", "hoge.img", "hoge.png", "huga.py"]
pattern = r"\.py$"
print("Pythonファイルのパターン", pattern)
for word in words:
    if re.search(pattern, word):
        print("=", word)
        # = hoge.py
        # = huga.py

words = ["Java", "PHP", "Perl", "FLASH", "HTML", "CSS", "Ruby", "Python", "GO", "Swift", "VB.NET", "C#", "C++", "Kotlin"]
pattern = r"^....$"
print("任意の4文字のパターン", pattern)
for word in words:
    if re.search(pattern, word):
        print("=", word)
        # = Java
        # = Perl
        # = HTML
        # = Ruby

word = "青巻紙赤巻紙黄巻紙"
print(re.findall(r".+紙", word)) # ['青巻紙赤巻紙黄巻紙']
print(re.findall(r".+?紙", word)) # ['青巻紙', '赤巻紙', '黄巻紙']

zipre = re.compile(r"^[0-9]{3}\-[0-9]{4}")
print(zipre.search("440-0012")) # <_sre.SRE_Match object; span=(0, 8), match='440-0012'>

word = "i like red color."
pattern = r"\w+ (color|colour)"
print(re.search(pattern, word)) # <_sre.SRE_Match object; span=(7, 16), match='red color'>

word = "date: 2017/10/15"
pattern = r"(\d{4})/(\d{1,2})/(\d{1,2})"
group = re.search(pattern, word)
print(group.groups()) # ('2017', '10', '15')