import tensorflow as tf
import numpy as np
import datetime
import matplotlib.pyplot as plt
import utils
import tensorflow.keras.backend as K

from tensorflow.keras.layers import (
    Input, Activation, Layer,
    UpSampling2D, Concatenate,
    Add, Conv2D)
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.applications.vgg19 import VGG19
from ops import (
    WaveLetPooling, WaveLetUnPooling, TfReduceSum,
    WhiteningAndColoring, get_predict_function,
    gram_matrix)

try:
    # In case run on google colab
    from google.colab.patches import cv2_imshow
except ImportError:
    from cv2 import imshow as cv2_imshow

VGG_LAYERS = [
    'block1_conv1', 'block1_conv2',
    'block2_conv1', 'block2_conv2',
    'block3_conv1', 'block3_conv2',
    'block3_conv3', 'block3_conv4',
    'block4_conv1',
]

class WCT2:
    def __init__(self, base_dir, rst, lr,
                show_interval=25,
                gram_loss_weight=1.0):
        self.base_dir = base_dir
        self.rst = rst
        self.lr = lr
        self.show_interval = show_interval
        self.img_shape = (self.rst, self.rst, 3)
        img = Input(self.img_shape)
        self.wct = self.build_wct_model()
        # ======= Loss functions ======= #
        recontruct_img = self.wct(img)

        self.trainer = Model(inputs=[img], outputs=[recontruct_img], name="trainer")
        self.trainer.add_loss(gram_loss_weight * self.gram_loss(img, recontruct_img))
        self.trainer.compile(optimizer=Adam(self.lr), loss=["mse"])

        self.init_transfer_sequence()

    def gram_loss(self, img, gen_img):
        feat_gens = self.encoder(gen_img)
        feats = self.encoder(img)

        gram_gen = [gram_matrix(f) for f in feat_gens]
        gram_in = [gram_matrix(f) for f in feats]
        num_style_layers = len(gram_gen)
        loss_list = [
            K.mean(K.square(gram_gen[i] - gram_in[i])) \
                for i in range(num_style_layers)
        ]

        gram_loss = TfReduceSum()(loss_list) / num_style_layers
        return gram_loss

    def conv_block(self, x, filters, kernel_size,
                    activation='relu', name=""):

        x = Conv2D(filters, kernel_size=kernel_size, strides=1,
                    padding='same', activation=activation, name=name)(x)
        return x

    def copy_layer(self, x, kernel_size, model, layer, name):
        """
        Need to copy layer for unique name
        """
        origin_layer = model.get_layer(layer)
        filters = origin_layer.output_shape[-1]
        return self.conv_block(x, filters, kernel_size,
                               name=origin_layer.name + name)

    def build_wct_model(self):
        img = Input(self.img_shape, name='in_img')
        kernel_size = 3
        skips = []

        vgg_model = VGG19(include_top=False,
                         weights='imagenet',
                         input_tensor=Input(self.img_shape),
                         input_shape=self.img_shape)

        vgg_model.trainable = False
        for layer in vgg_model.layers:
            layer.trainable = False

        self.encoder = Model(inputs=vgg_model.inputs,
                            outputs=[vgg_model.get_layer('block{}_conv1'.format(i)).get_output_at(0) \
                                    for i in {1, 2, 3, 4}],
                            name='encoder')

        # ======= Encoder ======= #
        id_ = 0
        x = self.copy_layer(img, kernel_size, vgg_model,
                            VGG_LAYERS[0], name='_encode')
        for layer in VGG_LAYERS[1:]:
            x = self.copy_layer(x, kernel_size, vgg_model, layer, name='_encode')
            if layer in {'block1_conv2', 'block2_conv2', 'block3_conv4'}:
                to_append = [x]
                x, lh, hl, hh= WaveLetPooling('wave_let_pooling_{}'.format(id_))(x)
                to_append += [lh, hl, hh]
                skips.append(to_append)
                id_ += 1

        # ======= Decoder ======= #
        skip_id = 2
        for layer in VGG_LAYERS[::-1][:-1]:
            filters = vgg_model.get_layer(layer).output_shape[-1]
            name = layer + "_decode"
            if layer in {'block4_conv1', 'block3_conv1', 'block2_conv1'}:
                x = self.conv_block(x, filters // 2, kernel_size, name=name)
                original, lh, hl, hh = skips[skip_id]
                x = WaveLetUnPooling('wave_let_unpooling_{}'.format(skip_id))([x, lh, hl, hh, original])
                skip_id -= 1
            else:
                x = self.conv_block(x, filters, kernel_size, name=name)

        out = self.conv_block(x, 3, kernel_size, 'linear', name="output")

        wct = Model(inputs=img, outputs=out, name='wct')

        for layer in wct.layers:
            # dont train waveletpooling layers
            if "_encode" in layer.name:
                name = layer.name.replace("_encode", "")
                layer.set_weights(vgg_model.get_layer(name).get_weights())
                layer.trainable=False

        return wct

    @staticmethod
    def init_hist():
        return {
            "loss": [],
            "val_loss": []
        }

    def train(self, data_gen, epochs):
        history = self.init_hist()
        print("Train on {} samples".format(len(data_gen.x)))

        for e in range(epochs):
            start_time = datetime.datetime.now()
            print("Train epochs {}/{} - ".format(e + 1, epochs), end="")

            batch_loss = self.init_hist()
            for content_img in data_gen.next_batch():
                loss = self.trainer.train_on_batch([content_img], [content_img])
                batch_loss['loss'].append(loss)

            # evaluate
            # batch_loss['val_loss'] =

            mean_loss = np.mean(np.array(batch_loss['loss']))
            mean_val_loss = 0#np.mean(np.array(batch_loss['val_loss']))

            history['loss'].append(mean_loss)
            history['val_loss'].append(mean_val_loss)

            print("Loss: {}, Val Loss: {} - {}".format(
                mean_loss, mean_val_loss,
                datetime.datetime.now() - start_time
            ))

            if e % self.show_interval == 0:
                self.save_weight()
                idx = np.random.randint(0, data_gen.max_size - 1)
                img = data_gen.x[idx:idx+1]
                gen_img = self.wct.predict(img)
                data_gen.show_imgs(np.concatenate([img, gen_img]))

        self.history = history

    def plot_history(self):
        plt.plot(self.history['loss'], label='train loss')
        plt.plot(self.history['val_loss'], label='val loss')
        plt.ylabel('loss')
        plt.xlabel('epoch')
        plt.title('Segmentation model')
        plt.legend()
        plt.show()

    def save_weight(self):
        try:
            self.wct.save_weights(self.base_dir + '/wct2.h5')
        except Exception as e:
            print("Save model failed, {}".format(str(e)))

    def load_weight(self):
        try:
            self.wct.load_weights(self.base_dir + '/wct2.h5')
        except Exception as e:
            print("Could not load model, {}".format(str(e)))

    def transfer(self, content_img, style_img, alpha=1.0):
        # ===== Encode ===== #
        # step 1.
        content_feat, style_feat = self.en_1([content_img]), self.en_1([style_img])
        content_feat = WhiteningAndColoring(alpha)([content_feat, style_feat])
        # step 2.
        content_feat, c_skips_1 = self.pool_1([content_feat])
        style_feat, s_skips_1 = self.pool_1([style_feat])
        c_skips_1 = [WhiteningAndColoring(alpha)([c_skips_1[i], s_skips_1[i]]) for i in range(4)]
        content_feat = WhiteningAndColoring(alpha)([content_feat, style_feat])
        # step 3.
        content_feat, c_skips_2 = self.pool_2([content_feat])
        style_feat, s_skips_2 = self.pool_2([style_feat])
        c_skips_2 = [WhiteningAndColoring(alpha)([c_skips_2[i], s_skips_2[i]]) for i in range(4)]
        content_feat = WhiteningAndColoring(alpha)([content_feat, style_feat])
        # step 4.
        content_feat, c_skips_3 = self.pool_3([content_feat])
        style_feat, s_skips_3 = self.pool_3([style_feat])
        c_skips_3 = [WhiteningAndColoring(alpha)([c_skips_3[i], s_skips_3[i]]) for i in range(4)]
        content_feat = WhiteningAndColoring(alpha)([content_feat, style_feat])

        # ===== Decode ===== #
        # step 1.
        content_feat, style_feat = self.de_1([content_feat]), self.de_1([style_feat])
        content_feat = WhiteningAndColoring(alpha)([content_feat, style_feat])
        # step 2.
        content_feat = self.unpool_1([content_feat] + c_skips_3)
        style_feat = self.unpool_1([style_feat] + s_skips_3)
        content_feat = WhiteningAndColoring(alpha)([content_feat, style_feat])
        content_feat = self.de_2([content_feat])
        style_feat = self.de_2([style_feat])
        # step 3.
        content_feat = self.unpool_2([content_feat] + c_skips_2)
        style_feat = self.unpool_2([style_feat] + s_skips_2)
        content_feat = WhiteningAndColoring(alpha)([content_feat, style_feat])
        content_feat = self.de_3([content_feat])
        style_feat = self.de_3([style_feat])

        content_feat = self.unpool_3([content_feat] + c_skips_1)
        style_feat = self.unpool_3([style_feat] + s_skips_1)

        content_feat = WhiteningAndColoring(alpha)([content_feat, style_feat])

        content_feat = self.final([content_feat])

        return content_feat.numpy()

    def init_transfer_sequence(self):
        # ===== encoder layers ===== #
        self.en_1 = get_predict_function(self.wct, ['in_img', 'block1_conv1_encode'], name='en_1')

        self.pool_1 = get_predict_function(
            self.wct,
            ['block1_conv2_encode', 'wave_let_pooling_0', 'block2_conv1_encode'],
            name='pool_1')

        self.pool_2 = get_predict_function(
            self.wct,
            ['block2_conv2_encode', 'wave_let_pooling_1', 'block3_conv1_encode'],
            name='pool_2')

        self.pool_3 = get_predict_function(
            self.wct,
            [
                'block3_conv2_encode', 'block3_conv3_encode',
                'block3_conv4_encode',
                'wave_let_pooling_2', 'block4_conv1_encode'
            ],
            name='pool_3')

        # ===== decoder layers ===== #
        self.de_1 = get_predict_function(self.wct, ['block4_conv1_decode'], name='de_1')

        self.unpool_1 = get_predict_function(
            self.wct,
            [
                'wave_let_unpooling_2', 'block3_conv4_decode',
                'block3_conv3_decode', 'block3_conv2_decode'
            ],
            name='unpool_1')

        self.de_2 = get_predict_function(self.wct, ['block3_conv1_decode'], name='de_2')

        self.unpool_2 = get_predict_function(
            self.wct,
            ['wave_let_unpooling_1', 'block2_conv2_decode'],
            name='unpool_2')

        self.de_3 = get_predict_function(self.wct, ['block2_conv1_decode'], name='de_3')

        self.unpool_3 = get_predict_function(
            self.wct,
            ['wave_let_unpooling_0', 'block1_conv2_decode'],
            name='unpool_3')

        self.final = get_predict_function(self.wct, ['output'], name='final')

    def generate(self, content_imgs, style_imgs):
        return self.wct.predict([content_imgs, style_imgs])

    def show_sample(self, content_img, style_img,
                    concate=True, denorm=True, deprocess=True):
        gen_img = self.generate(content_img, style_img)

        if concate:
            return utils.show_images(np.concatenate([content_img, style_img, gen_img]), denorm, deprocess)

        if denorm:
            content_img = utils.de_norm(content_img)
            style_img = utils.de_norm(style_img)
            gen_img = utils.de_norm(gen_img)
        if deprocess:
            content_img = utils.deprocess(content_img)
            style_img = utils.deprocess(style_img)
            gen_img = utils.deprocess(gen_img)

        cv2_imshow(content_img[0])
        cv2_imshow(style_img[0])
        cv2_imshow(gen_img[0])


#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

"""
This is the main file in this application.
It works as controller for cli and gui.
"""
from argparse import RawTextHelpFormatter, \
    Namespace, ArgumentParser
import sys
import os
from app.cli import Client

def main():
    """
    The main method that is called in
    begining of program
    """
    platform = sys.platform
    kwargs = {"platform": platform, "compression": arguments.compression,
              "encryption": arguments.encryption}
    if arguments.restore:
        kwargs["restore"] = True
    else:
        kwargs["restore"] = False

    if arguments.path:

        kwargs["path"] = arguments.path

    else:
        if platform == "linux":
            kwargs["path"] = "/home/"
        elif "win" in platform:
            kwargs["path"] = "C:"

    if arguments.output:
        if os.path.isdir(arguments.output):
            kwargs["output"] = arguments.output
        else:
            raise NotADirectoryError("Target output path is not directory")

    if arguments.graphics:
        # Starts graphics session.
        os.system("python gui.py")
    else:

        client = Client(kwargs)
        if kwargs["restore"]:
            client.split_backup()
            client.restore()
        else:
            client.get_tree()
            client.backup()

ARGS_PARSER = ArgumentParser(description="Backup client application",
                             formatter_class=RawTextHelpFormatter)
ARGS_PARSER.add_argument('-g', '--graphics', dest='graphics',
                         action='store_true', help='Enables GUI')
ARGS_PARSER.add_argument('--no-graphics', dest='graphics',
                         action='store_false', help='Disables GUI')
ARGS_PARSER.add_argument('-p', '--path', action='store',
                         help='Path to directory for backup',
                         default="./")
ARGS_PARSER.add_argument('-o', '--output', action='store',
                         help='Path where the backup will be stored',
                         default="./")
ARGS_PARSER.add_argument('-r', '--restore', action='store_true',
                         help='Restores backup from target path to output',
                         default=False)
ARGS_PARSER.add_argument('-c', '--compression', action='store_true',
                         help="Compress files using gzip",
                         default=False)
ARGS_PARSER.add_argument('-e', '--encryption', action='store_true',
                         help="Encrypt files in backup",
                         default=False)
arguments: Namespace = ARGS_PARSER.parse_args()

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt as error:
        print(error)


import requests
import time
import json

no_of_days=40
system_name="xyz.mycomp.network"
end_time = int(time.time())
no_of_days_in_secs = no_of_days * 24 * 60 * 60
start_time = end_time - no_of_days_in_secs
url = "https://xyz.mno.com/api/1.0/myapi/jobruns/list"

params = {"size": 8921}

payload = {
    "operator": "and",
    "filters": [
        {
            "operator": "ge",
            "field": "created_date",
            "value": start_time
        },
        {
            "operator": "le",
            "field": "created_date",
            "value": end_time
        },
        {
            "operator": "eq",
            "field": "system_name",
            "value": system_name
        },
        {
            "operator": "in",
            "field": "status",
            "value": [
                "FAILED",
                "PASSED"
            ]
        }
    ]
}

with requests.post(url, data=json.dumps(payload), params=params, verify=False, stream=True) as resp:
    data=""
    for chunk in resp.iter_content(chunk_size=4096, decode_unicode=False):  # 4K at a time
        chunk = chunk.decode('utf-8')
        data += chunk
    print(data)

print(type(data))
jobs_ran=json.loads(data)
print(jobs_ran)


data = list(map(int, input().split()))
input_number = int(input())

for i in range(1, len(data)):

    # The same as problem # 08, but in reverse. '>' in the while loop, instead of '<'
    # "while data[i] < data[i - 1] and i > 0:"  ---> original line
    while data[i] > data[i - 1] and i > 0:
        data[i], data[i - 1] = data[i - 1], data[i]
        i -= 1

# print the first N items (input_number)
for item in range(0, input_number):
    print(data[item], end=' ')

Python Fundamentals – June 2018/02a. Exercise/00. LISTS/10. Largest N Elements.py


# Fancy Pants Adventures (PS3 / Android / X360)
# Texture Viewer
# Noesis script by DKDave, 2023

# Note: some .tex files seem to have incorrect dimensions stored in the file and don't display correctly
# XBox360 textures use the same texture types, but are tiled, so change the x360 = 0 to x360 = 1 if you want to extract those

from inc_noesis import *

def registerNoesisTypes():
	handle = noesis.register("Fancy Pants Adventures Textures (PS3/Android/X360)",".lvl;.bgd;.tex")
	noesis.setHandlerTypeCheck(handle, CheckType)
	noesis.setHandlerLoadModel(handle, bcLoadModel)
	return 1

# Check file type

def CheckType(data):
	bs = NoeBitStream(data)
	return 1

def bcLoadModel(data, mdlList):
	bs = NoeBitStream(data)

	check = bs.readUInt()

	if check == 0x52535243:
		bs = NoeBitStream(data, NOE_BIGENDIAN)

	x360 = 0										# 0 = for PS3/Android, 1 = XBox360

	tex_list = []

	offset = 0
	file_end = len(data)
	tex_count = 0

	while True:
		if offset == file_end:
			break

		bs.seek(offset + 4)
		type = bs.readBytes(4).decode("ascii")
		bs.readUInt()
		data_size = bs.readUInt() + 0x20

		if type == "TXET" or type == "TEXT":
			bs.seek(offset + 0x20)
			tex_fmt = bs.readUInt()
			width = bs.readUInt()
			height = bs.readUInt()
			bs.seek(offset + 0x44)
			tex_data_size = bs.readUInt()
			bs.seek(offset + 0x60)
			raw_image = bs.readBytes(tex_data_size)

			if tex_fmt == 0x14:

				if x360 == 1:
					raw_image = rapi.swapEndianArray(raw_image, 2)
					raw_image = rapi.imageUntile360DXT(raw_image, width, height, 16)

				raw_image = rapi.imageDecodeDXT(raw_image, width, height, noesis.FOURCC_BC3)
				raw_image = rapi.imageFlipRGBA32(raw_image, width, height, 0, 1)
				tex = NoeTexture("Texture_" + str(tex_count), width, height, raw_image, noesis.NOESISTEX_RGBA32)
				tex_list.append(tex)

			elif tex_fmt == 0x15:
				raw_image = rapi.imageDecodeRaw(raw_image, width, height, "a4b4g4r4")
				raw_image = rapi.imageFlipRGBA32(raw_image, width, height, 0, 1)
				tex = NoeTexture("Texture_" + str(tex_count), width, height, raw_image, noesis.NOESISTEX_RGBA32)
				tex_list.append(tex)

			elif tex_fmt == 6:

				if x360 == 1:
					raw_image = rapi.imageUntile360Raw(raw_image, width, height, 4)

				raw_image = rapi.imageFlipRGBA32(raw_image, width, height, 0, 1)
				tex = NoeTexture("Texture_" + str(tex_count), width, height, raw_image, noesis.NOESISTEX_RGBA32)
				tex_list.append(tex)
			else:
				print("Unknown texture format: ", tex_fmt)

			tex_count += 1

		offset += data_size

	try:
		mdl = rapi.rpgConstructModel()
	except:
		mdl = NoeModel()

	mdl.setModelMaterials(NoeModelMaterials(tex_list, []))
	mdlList.append(mdl)

	return 1


'''
Python script to search all nested jsons for a sting.

Input: Folder with all of the jsons you want to search inside it, including jsons inside other folders. The script will only find json files and not other files.

Output: No actual output except the console which will tell you in which files it found a match.

Obviously you need python installed to run these scripts (Google that if you need to install it...). You might also need some dependencies, I can't remember what comes with python so try it and see if it says you need to install any.

Written by Mighty_Condor
2023-01-26
'''
import json
import os
import shutil

stringToFind = "5e81ebcd8e146c7080625e15"

def main():

    print("\n\nThis script searches all nested jsons for a string match and prints which files have the string in the console.\n\n")
    print("WARNING: I highly recommend you make a copy of the inputs you are trying to use BEFORE running this script on them! Don't be an idiot, just do it.\n\n")

    input("Go do that if you haven't already, then press any key to continue.\n\n")

    directory = getDirectory("Drag and drop or enter the path to the folder with all of json files inside it: ")

    jsonPaths = getAllJsonFiles(directory)
    jsonFullPaths = [directory + x for x in jsonPaths]

    for path in jsonFullPaths:

        #print(path)

        try: jsonFile = open(path, encoding='utf-8')
        except: error("There was an error loading " + path, 1)

        jsonString = jsonFile.read()
        jsonFile.close()

        if ( jsonString.find(stringToFind) != -1): print("MATCH: ", path)

def error(errorString, fatal) :

    print("\n\n\nERROR: " + errorString, "\n\n\n")
    if (fatal):
        input("Press any key to exit")
        exit()
    else:
        input("Press any key to continue, or exit manually by closing the window or CTRL+C now.")

def getDirectory(prompt, requiredPaths=None):

    directory = input(prompt)

    try:
        directory = os.path.normpath(directory)
        directory = directory.replace('"', '')
    except: error("There was an error fixing the directory path", 1)

    print("\n\nDirectory: ", directory, "\n\n")

    try: os.path.exists(directory)
    except: error("Directory does not exist at that path.", 1)
    print("Directory found!\n\n")

    if requiredPaths != None:
        os.chdir(directory)

        dirList = os.listdir(directory)

        print("Found directory contents:\n\n")
        print(dirList, "\n\n\nChecking requirements now...\n\n")

        errorFlag = 0
        for output in requiredPaths:
            if ( output == "itemIDs" ):
                if not os.path.exists(directory + requiredPaths["itemIDs"]):
                    print("Missing input file or folder " + requiredPaths["itemIDs"] + " for output " + output)
                    errorFlag = 1
            elif ( output == "clothingIDs" ):
                if not os.path.exists(directory + requiredPaths["clothingIDs"]):
                    print("Missing input file or folder " + requiredPaths["clothingIDs"] + " for output " + output)
                    errorFlag = 1
            elif ( enable[output] ):
                for path in requiredPaths[output]:
                    if not os.path.exists(directory + path):
                        print("Missing input file or folder " + path + " for output " + output)
                        errorFlag = 1

        if (errorFlag): error("Please fix the required input files or change the enabled outputs and run the script again", 1)
        else: print("All required files found!\n\n")

    return directory

def getAllJsonFiles(directory):

    jsonPaths = []
    tempFiles = []

    jsonPaths = ["\\" + f for f in os.listdir(directory) if (os.path.isfile(directory + "\\" + f) and f.endswith(".json"))]
    foldersList = ["\\" + f for f in os.listdir(directory) if os.path.isdir(directory + "\\" + f)]

    if foldersList:
        for folder in foldersList:
            tempFiles = getAllJsonFiles(directory + folder)
            tempFiles = ["\\" + folder + f for f in tempFiles]
            jsonPaths.extend(tempFiles)

    return jsonPaths

if __name__ == '__main__': main()


'''
Script to test model functions

Author: Gissella Gonzalez
Date  : August 28, 2023
'''
# import libraries
import os
import logging
import pandas as pd
import numpy as np
import pytest
from sklearn.model_selection import train_test_split
from ml.model import train_model, inference, save, load, sliced_model_metrics
from ml.data import process_data

model_path = 'model/'
out_path = 'out/'
model_name = 'model_sample.pkl'
encoder_name = 'encoder_sample.pkl'
lb_name = 'lb_sample.pkl'

cat_features = [
    "workclass",
    "education",
    "marital-status",
    "occupation",
    "relationship",
    "race",
    "sex",
    "native-country",
]

@pytest.fixture(scope="session")
def data():
    '''
    fixture - get dataframe
    '''
    path = 'data/census.csv'
    df = pd.read_csv(path, skipinitialspace=True)
    return df

@pytest.fixture(scope="session")
def get_model():
    '''
    fixture - get model
    '''
    model = load(model_path + model_name)
    return model

@pytest.fixture(scope="session")
def data_split(data):
    '''
    fixture - split data to test and train datasets
    '''
    train, test = train_test_split(data, test_size=0.20)
    return train, test

@pytest.fixture(scope="module")
def get_train_data(data_split):
    '''
    Return trained data
    '''
    # train, test = train_test_split(data, test_size=0.20)
    X_train, y_train, encoder, lb = process_data(
        data_split[0], cat_features, label="salary", training=True
    )

    return X_train, y_train, encoder, lb

@pytest.fixture(scope="module")
def get_test_data(data_split, get_train_data):
    '''
    Return tested data
    '''
    X_test, y_test, encoder, lb = process_data(data_split[1], categorical_features=cat_features,
                label="salary", training=False, encoder=get_train_data[2], lb=get_train_data[3])

    return X_test, y_test

def test_save(get_train_data):
    '''
    test to see if function is saving models correctly
    '''
    try:
        model_sample = train_model(get_train_data[0], get_train_data[1])
        save(model_sample, model_path + model_name)
        assert os.path.isfile(model_path + model_name)
    except AssertionError as err:
        logging.error(
            "ERROR: Testing test_save - model has not been saved in model folder")
        raise err

    try:
        save(get_train_data[2], model_path + encoder_name)
        assert os.path.isfile(model_path + encoder_name)
    except AssertionError as err:
        logging.error(
            "ERROR: Testing test_save - encoder has not been saved in model folder")
        raise err

    try:
        save(get_train_data[3], model_path + lb_name)
        assert os.path.isfile(model_path + lb_name)
    except AssertionError as err:
        logging.error(
            "ERROR: Testing test_save - lb has not been saved in model folder")
        raise err

def test_load():
    '''
    test loading encoder and lb is working as expected
    '''
    try:
        if os.path.isfile(model_path + model_name):
            assert load(model_path + model_name)
    except AssertionError as err:
        logging.error(
            "ERROR: Testing test_load - model has not loaded properly")
        raise err

def test_inference(get_model, get_test_data):
    '''
    test prediction's type
    '''
    try:
        preds = inference(get_model, get_test_data[0])
        assert isinstance(preds, np.ndarray)
    except AssertionError as err:
        logging.error(
            "ERROR: Testing test_inference - preds is not a np.array")
        raise err

    # test size of predictions
    try:
        assert len(preds) == get_test_data[0].shape[0]
    except AssertionError as err:
        logging.error(
            "ERROR: Testing test_inference - preds is not the same size as test data")
        raise err

def test_sliced_model_metrics(data_split, get_test_data, get_model, data):
    '''
    test to check if function calculates metrics for each class in each feature
    number of features = len(cat_feaures)
    number of unique classes in each feature

    '''
    num_classes = len(data['relationship'].unique())
    try:
        assert sliced_model_metrics(
            data_split[1],
            get_test_data[0],
            get_test_data[1],
            'relationship',
            get_model) == num_classes
    except AssertionError as err:
        logging.error(
            "ERROR: Testing test_sliced_model_metrics - did not run exact lenght of cat_features")
        raise err

# def test_compute_model_metrics(y, preds):
def test_compute_model_metrics():
    """
    test to see if slice_output file is not a blank file
    """
    try:
        assert os.stat(out_path + 'slice_output.txt').st_size != 0
    except AssertionError as err:
        logging.error("ERROR: Testing test_compute_model_metrics - there is not "
                    + "a file named slice_output.txt in out folder")
        raise err

# delete sample files
def test_remove_files():
    os.remove(model_path + model_name)
    os.remove(model_path + encoder_name)
    os.remove(model_path + lb_name)


import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter.ttk import *

class SaveSpotDialog():
    def __init__(self, view,drawing,spot, is_new_spot):
        self.view = view
        self.drawing=drawing
        self.spotCaptureWindow = Toplevel(self.view.master)
        self.spotCaptureWindow.title("Capture Spot Number")
        self.spotCaptureWindow.minsize(300, 100)
        self.spotCaptureWindow.lift()
        self.spotCaptureWindow.grab_set()
        self.spotCaptureWindow.focus_set()

        self.is_new_spot = is_new_spot
        self.spot = spot

        self.currentSpotNumber = tk.StringVar()
        self.note_text = tk.StringVar()
        self.cl_texture_text = tk.StringVar()

        if self.is_new_spot:
            self.currentSpotNumber.set('')
            self.note_text.set('')
            self.cl_texture_text.set('')

        else:
            self.currentSpotNumber.set(spot.group_tag.replace('spot_',''))
            self.note_text.set(spot.notes)
            self.cl_texture_text.set(spot.cl_texture)

        self.spotCaptureLabel = Label(self.spotCaptureWindow, text='Spot ID')
        self.spotCaptureLabel.grid(column=0, row=0, padx=2, pady=2, sticky='EW')

        self.currentSpotTextBox = Entry(self.spotCaptureWindow, width=20, textvariable=self.currentSpotNumber)
        self.currentSpotTextBox.grid(column=1, row=0, padx=2, pady=2, sticky='W')
        self.currentSpotTextBox.focus()

        self.CL_texture_label = Label(self.spotCaptureWindow, text='CL texture')
        self.CL_texture_label.grid(column=0, row=1, padx=2, pady=2, sticky='W')
        self.CL_texture_combobox = Combobox (self.spotCaptureWindow, width=20, textvariable=self.cl_texture_text, values=['homogenous',
                                                                                'oscillitory',
                                                                                'patchy',
                                                                                'sector',
                                                                                'other'])

        self.CL_texture_combobox.grid(column=1, row=1, padx=2, pady=2, sticky='W')

        # Added to address reviewer comments: Freeform notes:
        self.notes_label = Label(self.spotCaptureWindow, text='Comments:', width=50)
        self.notes_label.grid(column=0, row=2, padx=2, pady=2, sticky='W')

        self.notes_textbox = Entry(self.spotCaptureWindow, textvariable=self.note_text)
        self.notes_textbox.grid(column=1, row=2, padx=2, pady=2, sticky='W')

        self.saveSpotNo = Button(self.spotCaptureWindow, text='Save', command=self.save_spot, width=10)
        self.spotCaptureWindow.bind('<Return>', lambda e: self.save_spot())
        self.saveSpotNo.grid(column=1, row=3, padx=2, pady=2, sticky='W')

        self.spotCaptureWindow.protocol("WM_DELETE_WINDOW", self.on_closing)

    def save_spot(self):
        self.spot.cl_texture = self.CL_texture_combobox.get()
        self.spot.notes = self.notes_textbox.get().strip()
        if self.currentSpotNumber.get().strip() == '':
            messagebox.showinfo("Error", "No spot ID provided.")
            self.spotCaptureWindow.lift()
            return

        try:
            if self.is_new_spot:
                self.spot.group_tag = self.currentSpotNumber.get().strip()
                self.view.model.add_new_spot(self.spot)
                self.is_new_spot = False
                self.spotCaptureWindow.destroy()
            else:
                userText = self.currentSpotNumber.get()
                self.view.model.update_spot_id(self.spot, userText)
                self.spotCaptureWindow.destroy()
        except Exception as e:
            messagebox.showinfo("Error", str(e))
            return None

    def on_closing(self):
        self.spotCaptureWindow.destroy()
        self.view.model.DeleteObject(self.spot.group_tag)
        self.view.drawing.myCanvas.delete(self.spot.group_tag)


# usr/bin/python3.10

from collections import defaultdict
import numpy as np

class Graph:
    def __init__(self):
        self.graph = defaultdict(list[int])
        self.adj_matrix = None

    def add_edges(self, nodes: list or tuple[tuple or list[int]], is_directed: bool=False) -> defaultdict[int, list[int]]:
        """
        Creates an edge between two specified nodes.
        Conventially, nodes = list[tuple(int: a, int: b)].
        In directed graph, tuple/s contains 'from a', and "to b" nodes respectively.
        The default is an undirected graph.
        """

        if type(nodes) not in [list, tuple]:
            raise TypeError("nodes type must be a list of tuples that contains a pair of integer 'list[tuple(int, int)]'")

        if len(nodes) < 1:
            raise ValueError("nodes cannot be an empty list")

        if is_directed:
            for node in nodes:
                if type(node) not in [tuple, list]:
                    raise TypeError("all the list elements must be a tuple or list that contains a pair of integers")
                if len(node) != 2:
                    raise ValueError("tuple must contains exactly 2 integers; 'from' node and 'to' node")

                if node[1] not in self.graph[node[0]]:
                    if not self.graph[node[0]]:
                        self.graph[node[0]] = []
                    self.graph[node[0]].append(node[1])

                if not self.graph[node[1]]:
                    self.graph[node[1]] = []

        else:
            for node in nodes:
                if type(node) not in [tuple, list]:
                    raise TypeError("all the list elements must be a tuple or list that contains a pair of integers")
                if len(node) != 2:
                    raise ValueError("tuple must contains exactly 2 integers; 'from' node and 'to' node")

                if node[1] not in self.graph[node[0]]:
                    self.graph[node[0]].append(node[1])
                if node[0] not in self.graph[node[1]]:
                    self.graph[node[1]].append(node[0])

        return self.graph

    def create_adjacency_matrix(self, graph: dict = False) -> np.ndarray:
        """
        Creates an adjacency matrix of a given graph.
        """
        if graph:
            self.graph = graph

        self.adj_matrix = np.zeros((len(self.graph), len(self.graph)))

        for i in sorted(self.graph.keys()):
            for j in self.graph[i]:
                self.adj_matrix[i][j] += 1
        return self.adj_matrix


from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    # path('/', include('paginainicial.url')),
    path('admin/', admin.site.urls),
    path('auth/', include('usuarios.urls')),
    # include serve para apontar para determinado app
    path('divulgar/', include('divulgar.urls')),
]


import numpy
import os
import matplotlib.pyplot as pp
import scipy.io as sio
from pandas import read_csv
import pandas as pd
import sys
from tqdm import tqdm

def combineMatArrays(split):
  combined = None
  for element in split[0][0]:
    combined = element[0][0] if combined is None else numpy.concatenate([combined, element[0][0]])
  return combined

path = sys.argv[1]

data = read_csv(path)

density = 0.005
x = []
y = []
y2 = []
x3 = []
y3 =[]
if len(sys.argv)>2:
  threshold = float(sys.argv[2])
  print("data_id\t\ttime_diff")

timestamps = pd.read_csv("Synkope_timestamps.csv")
best_score = 0
best_threshold = 0
max_values = data.max()
for threshold in tqdm(numpy.arange(density,1-density, density)):
  if len(sys.argv)>2:
    threshold = float(sys.argv[2])
  TP = 0;
  FP = 0;
  TN = 0;
  FN = 0;
  time_diff_list = []
  for c in data:
    real_negative = 'Nosynkope' in c
    max_value = max_values[c]
    if not real_negative:
      for iter in range(len(data[c])):
        if data[c][iter]>=threshold:
          data_id = c[-8:-4]
          matfile = sio.loadmat('./Mat/Synkope/'+str(data_id)+'.mat')
          time_data = combineMatArrays(matfile['BeatToBeat']['Time'])
          time_diff = time_data[iter + 800] - timestamps[timestamps['data_id'] == data_id]['seconds']
          try:
            if len(sys.argv)>2:
              print(str(data_id)+"\t\t"+str(time_diff.item()))
            time_diff_list.append(time_diff.item())
          except:
            pass
          break

    if real_negative:
      if max_value>=threshold:#classified as synkope
        FP += 1
      else:
        TN += 1
    else:
      if max_value>=threshold:
        TP += 1
      else:
        FN += 1

  e = 0.00000001
  accuracy = (TP + TN) / (TP + TN + FP + FN + e)
  errorRate = 1 - accuracy
  sensitivity = TP / (TP + FN + e)
  specificity = TN / (TN + FP + e)
  PPV = TP / (TP + FP + e)
  NPV = FP / (FP + FN + e)
  f1 = 2*sensitivity*PPV/(sensitivity+PPV+e)

  avg_diff = numpy.mean(time_diff_list)
  med_diff = numpy.median(time_diff_list)
  if len(sys.argv)>2:
    print("AVERAGE\t\t"+str(avg_diff))
    print("MEDIAN\t\t"+str(med_diff))
  else:
    y2.append([avg_diff, med_diff])
    x3.append(1-specificity)
    y3.append(sensitivity)

  x.append(threshold)
  y.append([accuracy, f1, sensitivity])
  if f1 > best_score:
    best_score = f1
    best_threshold = threshold
    log = "\n"
    log += "TP = "+str(TP)+"\tFP = "+str(FP)+"\nFN = "+str(FN)+"\tTN = "+str(TN)+"\n"
    log += "errorRate = "+str(errorRate)+"\n"
    log += "sensitivity = "+str(sensitivity)+"\n"
    log += "specificity = "+str(specificity)+"\n"
    log += "PPV = "+str(PPV)+"\n"
    log += "NPV = "+str(NPV)+"\n"
    log += "\n"
    log += "accuracy = "+str(accuracy)+"\n"
    log += "F1 = "+str(f1)+"\n"

  if len(sys.argv)>2:
    break

print(log)
print("Best threshold: "+str(best_threshold))
print()
if len(sys.argv)<=2:
  pp.ylim(top=1.01)
  pp.gca().set_autoscale_on(False)
  pp.plot(x, y)
  pp.ylabel("Score")
  pp.gca().legend(("Accuracy", "F1", "Sensitivity"))
  pp.xlabel('Threshold')
  pp.show()

  pp.plot(x, y2)
  pp.title("Reaction time is the time difference\nbetween model's and manual presyncope detection")
  pp.ylabel("Reaction time [s]")
  pp.gca().legend(("Average", "Median"))
  pp.xlabel('Threshold')
  pp.show()

  pp.gca().set_autoscale_on(False)
  pp.scatter(x3, y3)
  pp.title("ROC")
  pp.ylabel("True positive rate")
  pp.xlabel('False positive rate')
  pp.show()


from pickle import PUT
from manim import *
from manim_slides import Slide

class SchoolPresent(Slide):
    def construct(self):
        def trans(p: np.ndarray) -> np.ndarray:
            return np.array([
                p[0]*p[0]-p[1]*p[1],
                p[1]*p[0]+p[1]*p[0],
                p[2]
            ])

        tex = Tex('Funkcje zespolone')
        self.play(Write(tex),run_time=2)
        self.next_slide()
        self.play(FadeOut(tex))
        self.wait(0.5)
        przyp=MathTex("i = \\sqrt{-1}")
        self.play(FadeIn(przyp,shift=DOWN))
        self.next_slide()
        equation = Tex("z" , " = " , "a" , " + ", "b", "i")
        equation[0].set_color(YELLOW)
        equation[2].set_color(RED)
        equation[4].set_color(BLUE)
        #self.add(equation)
        self.play(Transform(przyp,equation),run_time=2)
        self.remove(przyp)
        self.next_slide()

        equation2 = MathTex("z", "=" ,"r","(sin(",r"\theta",  ") + ", "cos(",r"\theta",")i)")
        equation2[0].set_color(YELLOW)
        equation2[2].set_color(RED)
        equation2[4].set_color(BLUE)
        equation2[7].set_color(BLUE)
        exp=MathTex("r",r"=\sqrt{a^{2}+b^{2}}").next_to(equation2,DOWN)
        exp[0].set_color(RED)
        exp2=MathTex(r"\theta",r"= tan^{-1}(\frac{b}{a})").next_to(exp,DOWN)
        exp2[0].set_color(BLUE)
        self.play(Transform(equation,equation2),FadeIn(exp),FadeIn(exp2))
        self.remove(equation)

        self.next_slide()
        equation3 = MathTex("z",r"=re^{i\theta}")
        equation3[0].set_color(YELLOW)
        equation3[1][1:2].set_color(RED)
        equation3[1][4:5].set_color(BLUE)
        self.play(Transform(equation2,equation3),FadeOut(exp),FadeOut(exp2))
        self.remove(equation2)
        self.next_slide()
        op2=MathTex(r"z_{1}z_{2}")
        op1=MathTex(r"z_{1} + z_{2}").next_to(op2,UP)
        op3=MathTex(r"\frac{z_{1}}{z_{2}}").next_to(op2,DOWN)

        opp2=Tex("(","a"," + ","b","i)"," * (","c"," + ","d","i)"," = ","a","c"," + ","a","d","i + ","b","c","i - ","b","d")
        opp1=Tex("(","a"," + ","b","i)"," + (","c"," + ","d","i)"," = ","a"," + ","c"," + (","b","+","d",")i").next_to(opp2,UP)
        opp3=MathTex(r"\frac{a + bi}{c + di}",r"=\frac{ac + bd}{c^{2} + d^{2}}",r" + (\frac{bc - ad}{c^{2} + d^{2}})i").next_to(opp2,DOWN)
        opp1[1].set_color(RED)
        opp1[11].set_color(RED)
        opp1[3].set_color(BLUE)
        opp1[15].set_color(BLUE)
        opp1[6].set_color(ORANGE)
        opp1[8].set_color(PURPLE)
        opp1[17].set_color(PURPLE)
        opp1[13].set_color(ORANGE)
        opp2[1].set_color(RED)
        opp2[11].set_color(RED)
        opp2[3].set_color(BLUE)
        opp2[17].set_color(BLUE)
        opp2[6].set_color(ORANGE)
        opp2[8].set_color(PURPLE)
        opp2[15].set_color(PURPLE)
        opp2[12].set_color(ORANGE)
        opp2[14].set_color(RED)
        opp2[20].set_color(BLUE)
        opp2[21].set_color(PURPLE)
        opp2[18].set_color(ORANGE)
        opp3[0][:1].set_color(RED)
        opp3[0][2:3].set_color(BLUE)
        opp3[0][5:6].set_color(ORANGE)
        opp3[0][7:8].set_color(PURPLE)
        opp3[1][1:2].set_color(RED)
        opp3[1][2:3].set_color(ORANGE)
        opp3[1][4:5].set_color(BLUE)
        opp3[1][5:6].set_color(PURPLE)
        opp3[1][7:8].set_color(BLUE)
        opp3[1][10:11].set_color(PURPLE)
        opp3[2][2:3].set_color(BLUE)
        opp3[2][3:4].set_color(ORANGE)
        opp3[2][5:6].set_color(RED)
        opp3[2][6:7].set_color(PURPLE)
        opp3[2][8:9].set_color(BLUE)
        opp3[2][11:12].set_color(PURPLE)

        aop2=MathTex(r"\theta = \theta_{1} + \theta_{2}", r", r = r_{1}r_{2}")
        aop3=MathTex(r"\theta = \theta_{1} - \theta_{2}", r", r = \frac{r_{1}}{r_{2}}").next_to(aop2,DOWN)

        self.play(Unwrite(equation3))
        self.wait(0.2)
        self.play(Write(op1),Write(op2),Write(op3))
        self.next_slide()
        self.play(Transform(op1,opp1),Transform(op2,opp2),Transform(op3,opp3))
        self.remove(op1,op2,op3)
        self.next_slide()
        self.play(Unwrite(opp1),Transform(opp2,aop2),Transform(opp3,aop3))
        self.add(aop2,aop3)
        self.remove(opp1,opp2,opp3)
        self.play(aop2.animate.shift(UP))
        self.next_slide()
        grid = ComplexPlane().add_coordinates()
        track_real=ValueTracker(1)
        track_img=ValueTracker(1)
        d1 = Dot(color=YELLOW)
        #sec22 = MathTex(str(track_real.get_value()), "+" ,str(track_img.get_value()),"i").add_updater(lambda m: m.next_to(d1, UP))
        sec22=Tex("1 + i").to_corner(UP+LEFT)
        d1.add_updater(
            lambda x: x.set_x(track_real.get_value()))
        d1.add_updater(
            lambda y: y.set_y(track_img.get_value()))

        self.play(
                FadeOut(aop3),
                Transform(aop2,sec22),
                Create(grid, run_time=3, lag_ratio=0.1),
                Create(d1),
        )
        self.remove(aop2,aop3)
        self.add(sec22)
        self.remove(equation3)
        self.next_slide()
        sec23=Tex("1 + 3i").to_corner(UP+LEFT)
        self.play(track_img.animate.set_value(3),Transform(sec22,sec23))
        self.wait(1)
        sec24=Tex("-1 - i").to_corner(UP+LEFT)
        self.remove(sec22)
        self.play(track_img.animate.set_value(-1),track_real.animate.set_value(-1),Transform(sec23,sec24))
        self.next_slide()
        arrow=Line(ORIGIN,[track_real.get_value(),track_img.get_value(),0],buff=0)
        rep=Line(ORIGIN,RIGHT)
        ang=Angle(arrow,rep,other_angle=True)
        sec25=MathTex(r"\theta=225^{\circ}",", r=\\sqrt{2}").to_corner(UP+LEFT)
        self.remove(sec23)
        self.play(FadeIn(arrow),FadeIn(ang),Transform(sec24,sec25))
        self.remove(sec24)
        self.next_slide()
        sec26=MathTex(r"f(z) = z^{2}").to_corner(UP+LEFT)
        self.play(Uncreate(ang),Uncreate(arrow),Uncreate(d1),Transform(sec25,sec26))
        self.remove(sec25)
        self.next_slide()
        grad=ComplexPlane()
        grad.prepare_for_nonlinear_transform()
        self.play(Create(grad))
        self.play(ApplyPointwiseFunction(trans,grad))
        self.next_slide()

        vfield=ArrowVectorField(trans)
        #vfield.set_color_by_gradient(RED,YELLOW,GREEN,BLUE,PURPLE)
        self.play(*[GrowArrow(vec) for vec in vfield])
        self.add(vfield)
        self.next_slide()
        stol=StreamLines(trans,stroke_width=3,max_anchors_per_line=30)
        self.play(Uncreate(vfield),FadeOut(grad))
        self.add(stol)
        self.remove(vfield,grad)
        stol.start_animation(warm_up=True,flow_speed=1.5)
        self.wait(20)

        self.next_slide()


"""Script for applying Mapping Extension algorithm on BERTMap results
"""

# append the paths
from bertmap.map.onto_map import OntoMapping
import os

main_dir = os.getcwd().split("BERTMap")[0] + "BERTMap"
# os.environ["TOKENIZERS_PARALLELISM"] = "false"  # disable huggingface tokenizer paralellism
import sys

sys.path.append(main_dir)

# import essentials
import argparse
import json
from shutil import copy2
from pathlib import Path
import pandas as pd
import subprocess
from subprocess import TimeoutExpired

# import bertmap
from bertmap import na_vals
from bertmap.utils import set_seed, banner
from bertmap.onto import OntoBox
from bertmap.extend import BERTClassifierExtend

def mapping_repair(
    config,
    candidate_limit,
    set_type_to_repair,
    repair_threshold,
):

    # configurations
    global task_dir, exp_dir, map_dir, repaired_set_type, src_ob, tgt_ob
    task_dir = config["data"]["task_dir"]
    repaired_set_type = set_type_to_repair

    src_ob = OntoBox.from_saved(task_dir + "/src.onto")
    tgt_ob = OntoBox.from_saved(task_dir + "/tgt.onto")

    fine_tune_params = config["fine-tune"]
    learn = fine_tune_params["learning"]
    # assert learn == "us" or learn == "ss"
    include_ids = fine_tune_params["include_ids"]
    banner(f"repairing fine-tuned models of {learn} settings", sym="#")
    exp_dir = (
        task_dir + f"/fine-tune.exp/{learn}.exp"
        if not include_ids
        else task_dir + f"/fine-tune.exp/{learn}.ids.exp"
    )

    map_dir = f"{exp_dir}/map.{candidate_limit}"

    # apply mapping repair
    file_to_repair = f"{map_dir}/extended/{repaired_set_type}.{candidate_limit}.tsv"
    file_to_save_repaired = f"{map_dir}/repaired/{repaired_set_type}.{candidate_limit}.tsv"
    if os.path.exists(file_to_save_repaired):
        print(f"skip map repair for candidate limit {candidate_limit} as existed ...")
    else:
        Path(f"{map_dir}/repaired/").mkdir(parents=True, exist_ok=True)
        formatted_file_path = repair_formatting(file_to_repair, repair_threshold=repair_threshold)

        src_onto_path = task_dir + "/src.onto"
        for file in os.listdir(src_onto_path):
            if file.endswith(".owl"):
                src_onto_path += f"/{file}"
                break

        tgt_onto_path = task_dir + "/tgt.onto"
        for file in os.listdir(tgt_onto_path):
            if file.endswith(".owl"):
                tgt_onto_path += f"/{file}"
                break

        # apply java commands of LogMap DEBUGGER
        repair_command = (
            f"java -jar {main_dir}/repair/logmap-matcher-4.0.jar DEBUGGER "
            + f"file:{src_onto_path} file:{tgt_onto_path} TXT {formatted_file_path} {map_dir}/repaired false true"
        )
        repair_process = subprocess.Popen(repair_command.split(" "))
        try:
            _, _ = repair_process.communicate(timeout=120)
        except TimeoutExpired:
            repair_process.kill()
            _, _ = repair_process.communicate()
        eval_formatting(f"{map_dir}/repaired/mappings_repaired_with_LogMap.tsv", candidate_limit)

    # configure reference mappings and mappings to be ignored
    print(f"evaluate the repaired mappings with threshold: {repair_threshold}")
    pred = f"{map_dir}/repaired/{repaired_set_type}.{candidate_limit}.tsv"
    ref = f"{task_dir}/refs/maps.ref.full.tsv"
    train_maps_df = pd.read_csv(
        f"{task_dir}/refs/maps.ref.ss.train.tsv",
        sep="\t",
        na_values=na_vals,
        keep_default_na=False,
    )
    val_maps_df = pd.read_csv(
        f"{task_dir}/refs/maps.ref.ss.val.tsv",
        sep="\t",
        na_values=na_vals,
        keep_default_na=False,
    )

    ref_ignored = (
        f"{task_dir}/refs/maps.ignored.tsv"
        if config["corpora"]["ignored_mappings_file"]
        else None
    )
    if ref_ignored:
        ref_ignored = pd.read_csv(
            ref_ignored, sep="\t", na_values=na_vals, keep_default_na=False
        )
    else:
        # init mappings to be ignored if there is no pre-defined one
        ref_ignored = pd.DataFrame(columns=["Entity1", "Entity2", "Value"])
    # train + val (30%) should be ignored for semi-supervised setting
    ref_ignored_ss = (
        ref_ignored.append(val_maps_df).append(train_maps_df).reset_index(drop=True)
    )
    # only val (10%) should be ignored for unsupervised setting
    ref_ignored_us = ref_ignored.append(val_maps_df).reset_index(drop=True)
    # results on 70% testing mappings
    result_ss = OntoMapping.evaluate(pred, ref, ref_ignored_ss, threshold=repair_threshold, prefix=repaired_set_type)
    result_ss.to_csv(f"{map_dir}/repaired/results.test.ss.{candidate_limit}.csv")
    # results on 90% testing mappings
    if "us" in learn:
        result_us = OntoMapping.evaluate(pred, ref, ref_ignored_us, threshold=repair_threshold, prefix=repaired_set_type)
        result_us.to_csv(f"{map_dir}/repaired/results.test.us.{candidate_limit}.csv")

def repair_formatting(map_file_tsv, repair_threshold):
    map_dict = BERTClassifierExtend.read_mappings_to_dict(map_file_tsv, threshold=repair_threshold)
    lines = []
    for m in map_dict.keys():
        src_iri, tgt_iri = m.split("\t")
        src_iri = src_ob.onto_text.expand_entity_iri(src_iri)
        tgt_iri = tgt_ob.onto_text.expand_entity_iri(tgt_iri)
        value = map_dict[m]
        lines.append(f"{src_iri}|{tgt_iri}|=|{value}|CLS\n")
    formatted_file = map_file_tsv.replace(".tsv", "-logmap_format.txt")
    with open(formatted_file, "w") as f:
        f.writelines(lines)
    return formatted_file

def eval_formatting(repaired_map_file_tsv, candidate_limit):
    repaired_df = pd.read_csv(
        repaired_map_file_tsv,
        sep="\t",
        names=["Entity1", "Entity2", "Value"],
        na_values=na_vals,
        keep_default_na=False,
    )
    repaired_df["Entity1"] = repaired_df["Entity1"].apply(
        lambda iri: src_ob.onto_text.abbr_entity_iri(iri)
    )
    repaired_df["Entity2"] = repaired_df["Entity2"].apply(
        lambda iri: tgt_ob.onto_text.abbr_entity_iri(iri)
    )
    repaired_df.to_csv(
        repaired_map_file_tsv.replace(
            "mappings_repaired_with_LogMap.tsv", f"{repaired_set_type}.{candidate_limit}.tsv"
        ),
        index=False,
        sep="\t",
    )

if __name__ == "__main__":

    set_seed(888)

    # parse configuration file and specify mode
    parser = argparse.ArgumentParser(description="run bertmap system")
    parser.add_argument(
        "-c", "--config", type=str, help="configuration file for bertmap system", required=True
    )
    parser.add_argument(
        "-t",
        "--threshold",
        type=float,
        help="threshold for mapping repair (suggested value is the best threshold from validation result)",
        default=0.999,
    )
    parser.add_argument(
        "-e",
        "--extended",
        type=str,
        choices={"src", "tgt", "combined"},
        required=True,
        help="the mapping set type to be extended",
    )
    args = parser.parse_args()

    banner("load configurations", sym="#")
    print(f"configuration-file: {args.config}")
    with open(args.config, "r") as f:
        config_json = json.load(f)
    for stage, stage_config in config_json.items():
        print(f"{stage} params:")
        for param, value in stage_config.items():
            print(f"\t{param}: {value}")
    Path(config_json["data"]["task_dir"] + "/configs").mkdir(parents=True, exist_ok=True)
    config_file = config_json["data"]["task_dir"] + "/configs/" + args.config.split("/")[-1]
    if os.path.exists(config_file):
        print("config file already existed, use the existed one ...")
    else:
        copy2(args.config, config_file)

    for limit in config_json["map"]["candidate_limits"]:
        print(f"current candidate limit: {limit}")
        mapping_repair(
            config=config_json,
            candidate_limit=limit,
            set_type_to_repair=args.extended,
            repair_threshold=args.threshold,
        )


#Given a directed, acyclic graph of N nodes.
#Find all possible paths from node 0 to node N-1, and return them in any order.

def allPathsSourceTarget(graph):
        output = []
        print('graph ' + str(graph[0]))
        dfs(graph, len(graph), output, 0, [0])
        print(output)

def dfs(graph, n, output, index, path):
    if index == n or len(graph[index]) == 0:
        output.append(path)
        #print(output)
        return
    # consider one row at a time and do DFS
    for edge in graph[index]:
        dfs(graph, n, output, edge, path+[edge])

graph = [[1,2], [3], [3], []]
allPathsSourceTarget(graph)


__author__ = 'diego'

from braviz.readAndFilter import tabular_data
from braviz.readAndFilter import log_db

import logging
import sqlite3

def verify_db_completeness(database_file=None):
    """
    Verifies that all tables exist in the braviz data base.
    If any are missing they are constructed.

    Args:
        database_file (str) : Path of the database file that should be ckecked, if None, the database defined
            by the configuration file will be used.
    """
    if database_file is None:
        conn = tabular_data.get_connection()
    else:
        conn = sqlite3.connect(database_file)

    # tabular data
    from braviz.readAndFilter import tabular_data_db_creation
    if not _check_tables(conn, ("variables", "subjects", "var_descriptions", "var_values", "nom_meta", "ratio_meta")):
        tabular_data_db_creation.create_data_base(conn=conn)

    # user db
    from braviz.readAndFilter import user_data_db_creation

    if not _check_tables(conn, ("applications", "scenarios", "vars_scenarios", "subj_samples", "subj_comments",)):
        user_data_db_creation.create_tables(conn)
    user_data_db_creation.update_current_applications(conn)

    # bundles db
    if not _check_tables(conn, ("fiber_bundles",)):
        from braviz.readAndFilter import bundles_db_creation
        bundles_db_creation.create_bundles_table(conn)
        bundles_db_creation.add_named_bundes_to_table(conn)
    _check_named_tracts(conn)

    # geom db
    if not _check_tables(conn, ("geom_rois", "geom_spheres", "geom_lines",)):
        from braviz.readAndFilter import geometry_db_creation
        geometry_db_creation.create_geom_rois_tables(conn)
        geometry_db_creation.create_lines_table(conn)
        geometry_db_creation.create_spheres_table(conn)

def _check_table(conn, table_name):
    q = "SELECT count(*) FROM sqlite_master WHERE type='table' and name = ? "
    cur = conn.execute(q, (table_name,))
    res = cur.fetchone()[0]
    return res > 0

def _check_named_tracts(conn):
    import braviz.readAndFilter
    r = braviz.readAndFilter.BravizAutoReader()
    named_tracts = r.get("FIBERS", None, index=True)
    existing_tracts = {b[0] for b in conn.execute(
        "SELECT bundle_name FROM fiber_bundles").fetchall()}
    for n in named_tracts:
        if not n in existing_tracts:
            q = """INSERT INTO fiber_bundles (bundle_name, bundle_type, bundle_data) VALUES (?, 0, ?) """
            with conn:
                conn.execute(q, (n, n))

def _check_tables(conn, tables):
    import logging
    for t in tables:
        if not _check_table(conn, t):
            logging.warning("Table %s not found" % t)
            return False
    return True

def verify_log_db(database_file=None):
    """
    Verifies that all log tables exist in the braviz log data base.
    If any are missing they are created.

    Args:
        database_file (str) : Path of the database file that should be ckecked, if None, the database defined
            by the configuration file will be used.
    """
    if database_file is None:
        conn = log_db.get_log_connection()
    else:
        conn = sqlite3.connect(database_file)
    # log db
    if not _check_tables(conn, ("sessions","events","annotations")):
        from braviz.readAndFilter import log_db_creation
        log_db_creation.create_tables(conn)

if __name__ == "__main__":
    from braviz.utilities import configure_logger_from_conf
    configure_logger_from_conf("check_db_integrity")
    verify_db_completeness()
    verify_log_db()


#!/usr/bin/env python

#Extract longest transcript of a gene from GFF using GFF parser
#Author: Hui Liu
#lhui2010@gmail.com

#pip install bcbio-gff

from BCBio import GFF
from Bio import SeqIO
from Bio import SeqFeature
from Bio.SeqRecord import SeqRecord
import sys
import re
import os
#import pprint

import argparse

infile=""

parser = argparse.ArgumentParser(description='Extract longest transcripts from GFF')
parser.add_argument('in_file', nargs='?',
                    help='GFF3 format file')
args = parser.parse_args()

if (len(sys.argv) < 2):
    parser.parse_args(['--help'])

#examiner = GFFExaminer()
#in_handle = open(in_file)
#pprint.pprint(examiner.available_limits(in_handle))
#in_handle.close()

len_dict = dict()
id_dict = dict()

#in_file = "test2.gff3"
#in_file = sys.argv[1]
#print in_file
#exit(0)
in_handle = open(args.in_file)
for rec in GFF.parse(in_handle):
    for feat in rec.features:
        gene_name = feat.id
#        print type(feat.sub_features)
#        print len(feat.sub_features)
#        continue
        if(feat.type != "gene"):
            continue

        if(len(feat.sub_features) > 0):

            for sub_index in range(len(feat.sub_features)):

                length=0

                for sub_index2 in range(len(feat.sub_features[sub_index].sub_features)):

                    if(feat.sub_features[sub_index].sub_features[sub_index2].type == 'CDS'):

                        length += feat.sub_features[sub_index].sub_features[sub_index2].location.end.position - feat.sub_features[sub_index].sub_features[sub_index2].location.start.position
                if ( not(len_dict.__contains__(gene_name)) or length > len_dict[gene_name]):

                    len_dict[gene_name] = length

                    id_dict[gene_name] = feat.sub_features[sub_index].id
in_handle.close()

with open(args.in_file + '.longest_gene', 'w') as fh:
    for gene in list(id_dict):
        fh.write(id_dict[gene] + "\n")
        #fh.write(id_dict[gene], "\t", gene)
os.system("select_ID_from_GFF.pl {0}.longest_gene  {0} > {0}.longest".format(args.in_file))


from musica import *
from feeder import *
from arvoreAVL import *

arvore = Arvore()
while True:
    print("1 - Inserir Música")
    print("2 - Buscar Música pelo ID")
    print("3 - Listar Músicas pelo ano")
    print("4 - Imprimir as músicas em ordem alfabética")
    print("5 - Exibir a altura da árvore")
    print("6 - Exibir a árvore")
    print("7 - Sair do programa")
    opcao = input("Informe o que opção desejada: ")
    if opcao == '1':
        print("Entre com as informações a respeito da música:")
        nome = str(input("Nome(s): "))
        album = str(input("album: "))
        ano = int(input("Ano: "))
        idMusica = int(input("IdMusica: "))
        musica = Musica(nome, album, ano, idMusica)
        teste = arvore.inserir(musica)
        if teste:
            print("Musica adicionada com sucesso: {}".format(musica.nome))
        else:
            print("Já existe uma música com o ID informado")

    elif opcao == '2':
        valor_idMusica = int(input("Informe o ID da música que deseja consultar: "))
        teste = encontraMusica(arvore, valor_idMusica)
        if teste == None:
            print("Nenhuma música foi encontrada para o ID informado\n")
        elif valor_idMusica == teste.idMusica:
            print("Musica encontrada!")
            print(f'Música: {teste}\n')

    elif opcao == '3':
        ano_Musica = int(input("Informe o ano da música que deseja consultar: "))
        musicas = encontra_ano(ano_Musica, arvore)
        if len(musicas) != 0:
            print("Músicas em ordem alfabética:")
            for x in musicas:
                print(f'{x}')
        else:
            print("Não foram encontradas músicas para esse ano")
        lista  = []

    elif opcao == '4':
        print("Músicas em ordem alfabética: ")
        musicas = ordenarMusicas(arvore)
        for x in musicas:
            print(f'{x}\n')
        musicasOrd  = []

    elif opcao == '5':
        altura = arvore.altura
        print(f'Altura da árvore: {altura}\n')
    elif opcao == '6':
        print(imprimirArvore(arvore))

    elif opcao == '7':
        break
    elif opcao == '8':
        for musica in array_Musicas:
            arvore.inserir(musica)
    else:
        print("Opção inválida, informar um valor entre os listados")


from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

# train data만 사용하여 학습
iris = load_iris()
dt_clf = DecisionTreeClassifier()
train_data = iris.data
train_label = iris.target
dt_clf.fit(train_data, train_label)

pred = dt_clf.predict(train_data)
print(f'예측 정확도 #1 : {accuracy_score(train_label, pred):.4f}')

# train, test data로 나눠서 학습
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=121)

dt_clf.fit(X_train, y_train)
pred = dt_clf.predict(X_test)
print(f'예측 정확도 #2 : {accuracy_score(y_test, pred):.4f}')


from youtube_dl import YoutubeDL
from urllib.request import urlopen
from bs4 import BeautifulSoup
import pyexcel
url = "https://www.apple.com/itunes/charts/songs/"
html_content = urlopen(url).read().decode("utf8")
soup = BeautifulSoup(html_content, "html.parser")
listdiv = soup.find_all("div", "section-content")
li_list = listdiv[1].find_all("li")
chart = []
for li in li_list:
    song = {}
    pos = li.strong.string
    name = li.h3.string
    artist = li.h4.string
    song["Rank"] = pos
    song["Name"] = name
    song["Artist"] = artist
    chart.append(song)
pyexcel.save_as(records = chart, dest_file_name = "iTunesTopSongs.xlsx")
options = {
    "default_search" : "ytsearch",
    "max_downloads" : 2
    }
dl = YoutubeDL(options)
download_list = []
for i in chart:
    download_list.append(i["Name"] + i["Artist"])
dl.download(download_list)
# print(download_list, sep = "\n")


"""Test for Hashlookup"""

import copy
import mock

from flask import current_app

from timesketch.lib.analyzers.contrib import hashlookup_analyzer
from timesketch.lib.testlib import BaseTest
from timesketch.lib.testlib import MockDataStore

SHA256_HASH = "ac7233de5daa4ab262e2e751028f56a7e9d5b9e724624c1d55e8b070d8c3cd09"
SHA256_N_HASH = "bc7233de5daa4ab262e2e751028f56a7e9d5b9e724624c1d55e8b070d8c3cd09"
MATCHING_HASH = {"sha256_hash": SHA256_HASH}
NO_MATCHING_HASH = {"sha256_hash": SHA256_N_HASH}

class TestHashlookup(BaseTest):
    """Tests the functionality of the analyzer."""

    def setUp(self):
        super().setUp()
        current_app.config["HASHLOOKUP_URL"] = "https://test.com/"

    @mock.patch("timesketch.lib.analyzers.interface.OpenSearchDataStore", MockDataStore)
    @mock.patch("requests.get")
    def test_hash_match(self, mock_requests_get):
        """Test match"""
        analyzer = hashlookup_analyzer.HashlookupAnalyzer("test_index", 1)
        analyzer.hashlookup_url = "https://test.com/"
        analyzer.datastore.client = mock.Mock()
        mock_requests_get.return_value.status_code = 200
        mock_requests_get.return_value.json.return_value = {"FileName": "test.txt"}

        event = copy.deepcopy(MockDataStore.event_dict)
        event["_source"].update(MATCHING_HASH)
        analyzer.datastore.import_event("test_index", event["_source"], "0")

        message = analyzer.run()
        self.assertEqual(
            message,
            ("Hashlookup Matches: 1"),
        )
        url = f"https://test.com/sha256/{SHA256_HASH}"
        mock_requests_get.assert_called_with(url)

    @mock.patch("timesketch.lib.analyzers.interface.OpenSearchDataStore", MockDataStore)
    @mock.patch("requests.get")
    def test_hash_nomatch(self, mock_requests_get):
        """Test no match"""
        analyzer = hashlookup_analyzer.HashlookupAnalyzer("test_index", 1)
        analyzer.hashlookup_url = "https://test.com/"
        analyzer.datastore.client = mock.Mock()
        mock_requests_get.return_value.status_code = 404
        mock_requests_get.return_value.json.return_value = []

        event = copy.deepcopy(MockDataStore.event_dict)
        event["_source"].update(NO_MATCHING_HASH)
        analyzer.datastore.import_event("test_index", event["_source"], "0")

        message = analyzer.run()
        self.assertEqual(
            message,
            ("Hashlookup Matches: 0"),
        )
        url = f"https://test.com/sha256/{SHA256_N_HASH}"
        mock_requests_get.assert_called_with(url)


from django.db import models
from . import constants
from datetime import datetime
from coreapp.base import BaseModel
from utility.utils.model_utils import get_code
class Company(BaseModel):
    name = models.CharField(max_length=255)
    address = models.CharField(max_length=255)
    phone = models.CharField(max_length=10)
    email = models.EmailField()
    website = models.URLField()
    notes = models.TextField()

    def __str__(self):
        return self.name

class Employee(BaseModel):
    first_name = models.CharField(max_length=40)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()
    phone = models.CharField(max_length = 20)
    company = models.ForeignKey(Company, on_delete=models.CASCADE)

    @property
    def full_name(self):
        return f'{self.first_name} {self.last_name}'

    def __str__(self):
        return f'{self.first_name} {self.last_name}'

class Device(BaseModel):
    name = models.CharField(max_length=255)
    serial_number = models.CharField(max_length=20, blank=True, null=True)
    device_type = models.SmallIntegerField( choices=constants.DEVICE_TYPE_CHOICES.choices)
    company = models.ForeignKey(Company, on_delete=models.CASCADE)
    notes = models.TextField()

    def save(self, *args, **kwargs):
        if not self.id:
            self.serial_number = get_code(Device,"SN",10)
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name

class Deligate(BaseModel):
    company = models.ForeignKey(Company, on_delete=models.CASCADE)
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    device = models.ManyToManyField(Device)
    checked_out = models.DateTimeField()
    returned = models.DateTimeField()
    device_status = models.SmallIntegerField( choices=constants.DEVICE_TYPE_CHOICES.choices)
    deligate_status = models.SmallIntegerField( choices=constants.DELIGATE_STATUS.choices)

    def save(self, *args, **kwargs):
        if self.returned is None:
            self.returned = datetime.now()
        if self.checked_out is None:
            self.checked_out = datetime.now()
        if self.deligate_status == constants.DELIGATE_STATUS.RETURNED:
            self.returned = datetime.now()
        if self.device_status == constants.DELIGATE_STATUS.CHECKED_OUT:
            self.checked_out = datetime.now()
        super().save(*args, **kwargs)

    def __str__(self):
        return f'{self.company.name}'

class DeviceHistory(BaseModel):
    device = models.ForeignKey(Device, on_delete=models.CASCADE)
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    date = models.DateTimeField(auto_now_add=True )
    status = models.SmallIntegerField( choices=constants.DELIGATE_STATUS.choices)
    device_condition = models.SmallIntegerField( choices=constants.DEVICE_CONDITION.choices)
    def __str__(self):
        return f'{self.device.name}'


# Welcome to the world as defined by JJE.
# This determines where the player is in the world and the enemies and items

import random
import enemies
import npc

class MapTile:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def intro_text(self):
        raise NotImplementedError("Create a subclass!!")

    def modify_player(self, player):
        pass

class StartTile(MapTile):
    def intro_text(self):
       return """You are locked in an abandoned mansion.
       There are a few paths to travel.
       Can you survive?"""

class BoringTile(MapTile):
    def intro_text(self):
        return """
        Not much is happening here.
        """

class EnemyTile(MapTile):
    def __init__(self, x, y):
        r = random.random()
        if r < 0.35:
            self.enemy = enemies.Lich()
            self.alive_text = "A deadly Lich lurks in the darkness." \
                              "Watch out!"
            self.dead_text = "The Lich lies lifeless in front of you"

        if r < 0.40:
            self.enemy = enemies.Mummy()
            self.alive_text = "You hear a strange noise in the distance." \
                              "A huge mummy comes after you."
            self.dead_text = "The mummies cloth lies in a pile on the ground."

        if r < 0.50:
            self.enemy = enemies.ScaryClown()
            self.alive_text = "A Scary Clown jumps out the corner "\
                              "It's in front of you!"
            self.dead_text = "The corpse of a dead clown "\
                             "rots on the ground."
        elif r < 0.80:
            self.enemy = enemies.ViciousDog()
            self.alive_text = "A vicious dog is blocking your path!"
            self.dead_text = "A dead dog reminds you your alive."

        else:
            self.enemy = enemies.JumboRat()
            self.alive_text = "You've awaken a Jumbo Rat!"
            self.dead_text = "The rat scattered in fear."

        super(). __init__(x,y)

    def intro_text(self):
        text = self.alive_text if self.enemy.is_alive() else self.dead_text
        return text

    def modify_player(self, player):
        if self.enemy.is_alive():
            player.hp = player.hp - self.enemy.damage
            print("Enemy does {} damage. You have {} HP remaining!".
                  format(self.enemy.damage, player.hp))

class FindPenniesTile(MapTile):
    def __init__(self, x, y):
        self.pennies = random.randint(1, 50)
        self.pennies_claimed = False
        super().__init__(x, y)

    def modify_player(self, player):
        if not self.pennies_claimed:
            self.pennies_claimed = True
            player.pennies = player.pennies + self.pennies
            print("+{} pennies added.".format(self.pennies))

    def intro_text(self):
        if self.pennies_claimed:
            return """
            Another unremarkable part of the mansion. You must press onwards.
            """
        else:
            return """
            Someone dropped some pennies. You pick it up.
            """

class TraderTile(MapTile):
    def __init__(self, x, y):
        self.trader = npc.Trader()
        super().__init__(x, y)

    def trade(self, buyer, seller):
        for i, item in enumerate(seller.inventory, 1):
            print("{}. {} - {} Pennies".format(i, item.name, item.value))
        while True:
            user_input = input("Choose an item or press Q to exit: ")
            if user_input in ['Q', 'q']:
                return
            else:
                try:
                    choice = int(user_input)
                    to_swap = seller.inventory[choice -1]
                    self.swap(seller, buyer, to_swap)
                except ValueError:
                    print("Invalid choice!")

    def swap(self, seller, buyer, item):
        if item.value > buyer.pennies:
            print("That's to expensive!")
            return
        seller.inventory.remove(item)
        buyer.inventory.append(item)
        seller.pennies = seller.pennies + item.value
        buyer.pennies = buyer.pennies - item.value
        print("Trade complete!")

    def check_if_trade(self, player):
        while True:
            print("Would you like to (B)uy, (S)ell, or (Q)uit?")
            user_input = input()
            if user_input in['Q', 'q']:
                return
            elif user_input in['B', 'b']:
                print("Here's whats available to buy: ")
                self.trade(buyer=player, seller=self.trader)
            elif user_input in['S', 's']:
                print("Here's whats available to sell: ")
                self.trade(buyer=self.trader, seller=player)
            else:
                print("Invalid choice!")

    def intro_text(self):
        return """
        A creature waits in the corner,
        he looks willing to trade.
        """

class VictoryTile(MapTile):
    def modify_player(self, player):
        player.victory = True

    def intro_text(self):
        return """
        Escape is in your grasp!
        You Survived!!!!
        """

world_dsl = """
|EN|EN|VT|EN|EN|
|EN|  |  |  |EN|
|EN|FG|EN|  |TT|
|TT|  |ST|FG|EN|
|FG|  |EN|  |FG|
"""

world_map = []

def tile_at(x,y):
    if x < 0 or y < 0:
        return None
    try:
        return world_map[y][x]
    except IndexError:
        return None

start_tile_location = None

def is_dsl_valid(dsl):
    if dsl.count("|ST|") != 1:
        return False
    if dsl.count("|VT|") == 0:
        return False
    lines = dsl.splitlines()
    lines =[l for l in lines if l]
    pipe_counts = [line.count("|") for line in lines]
    for count in pipe_counts:
        if count != pipe_counts[0]:
            return False

    return True

def parse_world_dsl():
    if not is_dsl_valid(world_dsl):
        raise SyntaxError("DSL is invalid!")

    dsl_lines = world_dsl.splitlines()
    dsl_lines = [x for x in dsl_lines if x]

    for y, dsl_row in enumerate(dsl_lines):
        row = []
        dsl_cells = dsl_row.split("|")
        dsl_cells = [c for c in dsl_cells if c]
        for x, dsl_cell in enumerate(dsl_cells):
            tile_type = tile_type_dict[dsl_cell]
            if tile_type == StartTile:
                global start_tile_location
                start_tile_location = x, y
            row.append(tile_type(x, y) if tile_type else None)

        world_map.append(row)

tile_type_dict = {
    "VT": VictoryTile,
    "EN": EnemyTile,
    "ST": StartTile,
    "FG": FindPenniesTile,
    "TT": TraderTile,
    "  ": None}


import math
import sys

from methods import secant, fixed_point_iteration, fixed_point_iteration_system

f1 = lambda x: 3 * x ** 3 + 4.5 * x ** 2 - 2.3 * x - 3
f2 = lambda x: 2.1 * math.exp(x) + x ** 2 - 4.2 * x - 3.5
f3 = lambda x: 7 / x - 2.1 * x ** 2 + 3 * x + 2

f4 = lambda x, y: 0.1 * x ** 2 + x + 0.2 * y ** 2 - 0.3
f5 = lambda x, y: 0.2 * x ** 2 + y - 0.1 * x * y - 0.7

f1_der = lambda x: 9 * x ** 2 + 9 * x - 2.3
f2_der = lambda x: 2.1 * math.exp(x) + 2 * x - 4.2
f3_der = lambda x: -7 / x ** 2 - 2.1 * 2 * x + 3

f4_x = lambda x, y: 0.3 - 0.1 * x ** 2 - 0.2 * y ** 2
f5_y = lambda x, y: 0.7 - 0.2 * x ** 2 - 0.1 * x * y

f4_der_x = lambda x: -0.2 * x
f5_der_x = lambda x, y: -0.4 * x + 0.1 * y

f4_der_y = lambda y: -0.4 * y
f5_der_y = lambda x: 0.1 * x

functions = {1: f1, 2: f2, 3: f3}
functions_der = {1: f1_der, 2: f2_der, 3: f3_der}
systems = {1: [f4_x, f5_y]}
methods = {1: secant, 2: fixed_point_iteration}
methods_for_systems = {1: fixed_point_iteration_system}

def main():
    args = sys.argv[1:]
    if len(args) != 0:
        input_filepath = args[0]
        input_file = open(input_filepath, "r")
        process_file_input(input_file)
    else:
        process_console_input()

def process_file_input(file):
    func_or_system = int(file.readline())
    if func_or_system == 1:  # Function
        function_num = int(file.readline())
        function = functions[function_num]
        function_der = functions_der[function_num]
        method = methods[int(file.readline())]
        if method is secant:
            x1 = float(file.readline())
            x2 = float(file.readline())
        elif method is fixed_point_iteration:
            start_point = float(file.readline())
    elif func_or_system == 2:  # System
        system = systems[int(file.readline())]
        method = methods_for_systems[int(file.readline())]
        if method is fixed_point_iteration_system:
            x = float(file.readline())
            y = float(file.readline())

    accuracy = float(file.readline())

    if method is secant:
        secant(function, x1, x2, accuracy)
    elif method is fixed_point_iteration:
        fixed_point_iteration(function, function_der, start_point, accuracy)
    elif method is fixed_point_iteration_system:
        fixed_point_iteration_system(system[0], f4_der_x, f4_der_y, system[1], f5_der_x, f5_der_y, x, y, accuracy)

def process_console_input():
    print("1) Functions")
    print("2) Systems")
    print("Enter 1 or 2:")
    func_or_system = int(input())

    if func_or_system == 1:  # Function
        print("1) 3x^3+4.5x^2-2.3x-3=0")
        print("2) 2.1e^x+x^2-4.2x-3.5=0")
        print("3) 7/x-2.1x^2+3x+2=0")
        print("Choose function: ")
        function_num = int(input())
        function = functions[function_num]
        function_der = functions_der[function_num]

        print("1) Secant")
        print("2) Fixed Point Iteration")
        print("Choose method: ")
        method = methods[int(input())]

        if method is secant:
            print("Enter point x1: ")
            x1 = float(input())
            print("Enter point x2: ")
            x2 = float(input())
        elif method is fixed_point_iteration:
            print("Enter start point: ")
            start_point = float(input())

    elif func_or_system == 2:  # System
        print("1) 0.1x^2+x+0.2y^2-0.3=0\n   0.2x^2+y-0.1xy-0.7=0")
        system = systems[int(input())]
        print("1) Fixed Point Iteration")
        print("Choose method: ")
        method = methods_for_systems[int(input())]
        if method is fixed_point_iteration_system:
            print("Enter x: ")
            x = float(input())
            print("Enter y: ")
            y = float(input())

    print("Enter accuracy: ")
    accuracy = float(input())

    if method is secant:
        secant(function, x1, x2, accuracy)
    elif method is fixed_point_iteration:
        fixed_point_iteration(function, function_der, start_point, accuracy)
    elif method is fixed_point_iteration_system:
        # f1, f1_der_x, f1_der_y, f2, f2_der_x, f2_der_y, x, y, e
        # system[0], , , system[1], , x, y, accuracy
        fixed_point_iteration_system(system[0], f4_der_x, f4_der_y, system[1], f5_der_x, f5_der_y, x, y, accuracy)

main()


# for app

from flask import Flask, render_template
from .models import DB, User

# app.py should just have app configuration and routing logic

def create_app():
    """ Create and configure an instance of the flask app"""
    app = Flask(__name__)

    # add config for database
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sqlite3.db'

    # have database know about the  app
    DB.init_app(app)

    @app.route('/')
    @app.route('/home')
    def home():
        users = User.query.all()
        return render_template('home.html', title='Home', users=users)

    @app.route('/about')
    def about():
        return '
About Page

Just trying to compare tweet behaviours!
'

    @app.route('/reset')
    def reset():
        DB.drop_all()
        DB.create_all()
        return 'Reset database!'
    return app

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


import sys
n = int(sys.stdin.readline().strip())
line = sys.stdin.readline().strip()
Adrian_s = "ABC"
Bruno_s = "BABC"
Goran_s = "CCAABB"
d = {"Bruno":0, "Adrian":0, "Goran":0}
for i in range(0,len(line)):
    if line[i] == Bruno_s[i % len(Bruno_s)]:
        d["Bruno"] += 1
    if line[i] == Adrian_s[i % len(Adrian_s)]:
        d["Adrian"] += 1
    if line[i] == Goran_s[i % len(Goran_s)]:
        d["Goran"] += 1

best_student = []
best_score = -1
for key in d:
    if d[key] > best_score:
        best_score = d[key]
        best_student = [key]
    elif d[key] == best_score:
        best_student.append(key)

print(best_score)
for student in sorted(best_student):
    print(student)


import sys
import logging
import joblib
from PIL import Image
import numpy as np
import  cv2
from keras.applications.vgg16 import preprocess_input
import json
from flask import Flask, request, Response

application = Flask(__name__)
logger = logging.getLogger("beer_project_logger")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))

@application.route('/beer_guess', methods=['POST'])
def guess_beer():
    response = None
    try:
        logger.info("Receiving Image")
        image = request.files['image']
        label = compute_predictions(image)

        logger.info("Generating cupom messages")
        cupom_messages = generate_cumpom_messages(label)
        cupons_description = cupom_messages[0]
        cupons_tag = cupom_messages[1]

        response = Response(content_type='application/json', status = 200, response = json.dumps({"Label": label, "Message": cupons_description, "Tags": cupons_tag}))
    except BaseException as error:
        error_content = {"Error": str(error)}
        logger.error("Error", json.dumps(error_content))
        response = Response(content_type='application/json', status = 500, response = json.dumps(error_content))
    return response

def preprocess_image(image):
    logger.info("Preprocessing image")
    image_processed = Image.open(image)
    image_processed = np.array(image_processed)
    image_processed = cv2.resize(src= image_processed, dsize = (224, 224))
    image_processed = image_processed.reshape((1, image_processed.shape[0], image_processed.shape[1], image_processed.shape[2]))
    image_processed = preprocess_input(image_processed)
    return image_processed

def get_dict_labels():
    mapping_labels =  np.array(['cerveja_lata', 'cerveja_garrafa', 'cerveja_longneck'])
    classes_dict = {}
    for i,classe in enumerate(mapping_labels):
        classes_dict[i] = classe
    return classes_dict

def decode_predictions_custom(pred):
    logger.info("Decoding predictions")
    items = np.argsort(pred[0])[-5:][::-1]
    classes_dict = get_dict_labels()
    label = list(map(lambda x: classes_dict[x], items))[0]
    return label

def compute_predictions(image):
    logger.info("Model load")
    model = joblib.load('model_beers_novo.pickle')
    image_processed =  preprocess_image(image)
    predictions = model.predict(image_processed)
    final_label = decode_predictions_custom(predictions)
    return final_label

def generate_cumpom_messages(beer):
    cupom_list = ['10% na primeira compra', '5% na compra da sua cerveja',
    'Na compra da primeira, leve a segunda com 10% de desconto']
    tags = ["CERVEJA10", "CERVEJA5", "LEVE1PAGUEMENOS"]

    number_of_cupoms = np.random.randint(1,4)
    cupom_chosed = list(np.random.choice(cupom_list, number_of_cupoms, replace=False))
    cupom_str = "Cupons: "
    tags_str = ""

    for i in range(number_of_cupoms):
        cupom_str = f"{cupom_str} {cupom_chosed[i]}, "
        tag_index = cupom_list.index(cupom_chosed[i])
        tags_str = f"{tags_str}{tags[tag_index]}, "
    return [cupom_str, tags_str]

if __name__ == '__main__':
     application.run(host='0.0.0.0', port=8080)


from django.shortcuts import redirect, render
from itertools import product
from django.shortcuts import render
from methods import Details_context
from .models import *
from django.core.paginator import Paginator
from cart.models import *
from .forms import *
# Create your views here.

def all_reviews(request, id):
    review_list=Reviews.objects.filter(product_id=id).reverse()
    context={
        "review":review_list,
        "rat_list":rating_(id),
        'cart_item':len(Cart.objects.filter(customer_id=request.user.id)),
        'wishlist_item':len(Wishlist.objects.filter(customer_id=request.user.id))
    }
    context["product"]=Product.objects.get(id=id)
    return render(request, "review.html", context )

def rating_(id):
    if Reviews.objects.filter(product_id=id).count()==0:
        rat_list=[0,0,0,0,0]
        return rat_list
    else:
        total=Reviews.objects.filter(product_id=id).count()
        rat_list=[]
        a=0
        for i in range(1,6):
            rat=Reviews.objects.filter(product_id=id,rating=i).count()
            a+=rat*i
            value=rat*100/total
            value=round(value,2)
            rat_list.append(value)
        rat_list.append(str(a/total)[:3])

        return rat_list

def detail(request, item_id):
    cat=Product.objects.get(id=item_id).category_id
    context=Details_context(item_id)
    context["product"]=Product.objects.get(id=item_id)
    context["image"]=Other_image.objects.filter(product_id=item_id)
    context["list"]=Product.objects.filter(category=cat)
    context["ram_list"]=RAM.objects.filter(product_id=item_id)
    context["color"]=RAM.objects.filter(product_id=item_id)
    context['cart_item']=len(Cart.objects.filter(customer_id=request.user.id))
    context['wishlist_item']=len(Wishlist.objects.filter(customer_id=request.user.id))

    if request.method=="POST":
        massage=request.POST["massage"]
        rating=request.POST['RadioOptions']
        name=request.POST['name']
        email=request.POST['email']
        review=Reviews.objects.create(review=massage, rating=rating, name=name,email=email,product_id=item_id)
        review.save()
        add_rating_to_product(item_id)
    return render(request, "detail.html", context)

def helpfull(request,review_id):
    review=Reviews.objects.get(id=review_id)
    review.helpfull+=1
    review.save()
    product_id=Reviews.object.get(id=review_id)
    redirect(f"/products/{product_id.product__id}")

def add_rating_to_product(id):
    rev=Reviews.objects.filter(product_id=id).count()
    a=0
    for i in range(1,6):
        rat=Reviews.objects.filter(product_id=id,rating=i).count()
        a+=rat*i
    rat=str(a/rev)[:3]

    item=Product.objects.get(id=id)
    item.review_count=rev
    item.rating_count=rat
    item.save()

def shop(request):
    product=Product.objects.all()
    context={
    "product":product,
    'cart_item':len(Cart.objects.filter(customer_id=request.user.id)),
    'wishlist_item':len(Wishlist.objects.filter(customer_id=request.user.id))
    }

    return render(request, "shop.html", context)

def category(request,itemCategory ):
    product_set=Product.objects.filter(category__id=itemCategory)
    context={
        "product":product_set,
        'cart_item':len(Cart.objects.filter(customer_id=request.user.id)),
        'wishlist_item':len(Wishlist.objects.filter(customer_id=request.user.id)),
        "path":f"Product: {Category_group.objects.get(sub_category=itemCategory).title}/ {Sub_category.objects.get(id=itemCategory).title}"
    }
    if request.GET:
        if request.GET['filter_by']:
            filter_type=request.GET['filter_by']
            if filter_type=="low_to_high":
                product_set=Product.objects.filter(category__id=itemCategory).order_by('price')
            elif filter_type=="high_to_low":
                product_set=Product.objects.filter(category__id=itemCategory).order_by('-price')
            elif filter_type=="rating":
                product_set=Product.objects.filter(category__id=itemCategory).order_by('-rating_count')
            else:
                product_set=Product.objects.filter(category__id=itemCategory)
            context["product"]=product_set
    return render(request, "shop.html",context)

def Group_category(request,category_sub):
    product_set=Product.objects.filter(category__group__id=category_sub)
    context={
        "product":product_set,
        'cart_item':len(Cart.objects.filter(customer_id=request.user.id)),
        'wishlist_item':len(Wishlist.objects.filter(customer_id=request.user.id)),
        "path":f"Product: {Category_group.objects.get(id=category_sub).title}/ "
    }
    return render(request, "shop.html",context)


import pandas as pd
import numpy as np
import logging, math

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Create Heros' Synergy and Counter scores to return top 5 choices
def create_features():
    """
    Use preprocessed data to compute each hero's Synergy and Counter scores against all other heroes
    """
    pre_data = pd.read_csv('./pre_data/s8_champ_win_data.csv')

    logger.info("This data et has %d matches", len(pre_data))
    logger.info("Computing hero synergy, counter, self_win_rate...")

    generate_maps(pre_data)

def generate_maps(data):
    """
    Compute synergy and counter dict and transform to matrix, then save to csv file
    """
    counter = dict()
    synergy = dict()
    self_win = dict()

    # each hero has:
    # total wins number against other 137 heroes
    # total matches number against other 137 heroes
    # total wining rate against other 137 heroes

    counter['wins'] = np.zeros((138, 138))
    counter['matches'] = np.zeros((138, 138))
    counter['winrate'] = np.zeros((138, 138))

    synergy['wins'] = np.zeros((138, 138))
    synergy['matches'] = np.zeros((138, 138))
    synergy['winrate'] = np.zeros((138, 138))

    self_win['wins'] = np.zeros(138)
    self_win['matches'] = np.zeros(138)
    self_win['winrate'] = np.zeros(138)

    for instance in data.values:
        build_dict(synergy, counter, self_win, instance)

    compute_wining_rates(synergy, counter, self_win, 138)
    sy_mat, ct_mat, s_win_mat = build_matrix(synergy, counter, self_win, 138)

    np.savetxt('./fea_data/heroes_synergies_readable.csv', sy_mat)
    np.savetxt('./fea_data/heroes_counters_readable.csv', ct_mat)
    np.savetxt('./fea_data/heroes_self_win_rate_readable.csv', s_win_mat)
    np.save('./fea_data/heroes_synergies', sy_mat)
    np.save('./fea_data/heroes_counters', ct_mat)
    np.save('./fea_data/heroes_self_win_rate', s_win_mat)

    logger.info("Synergy and Counter map created successfully!")

def build_dict(synergy, counter, self_win, match):
    """
    fulfill synergy and counter dict with each historical match record
    """
    b_win = match[276]

    b_team = []
    r_team = []

    # go through each match record and find blue team heroes and red team heroes
    for i in range(len(match)-1):
        if match[i] == 1:
            if i % 2 == 1:
                r_team.append(math.floor(i/2))
            else:
                b_team.append(int(i/2))

    # skip dirty data that does not match a 5 vs 5 situation
    if len(r_team) == 5 and len(b_team) == 5:
        # go through a 5 vs 5 regular match of each hero team up/against each hero
        for i in range(5):
            # for self win
            self_win['matches'][b_team[i]] += 1
            self_win['matches'][r_team[i]] += 1

            if b_win == 1:
                self_win['wins'][b_team[i]] += 1
            else:
                self_win['wins'][r_team[i]] += 1

            for j in range(5):
                b_hero_i = b_team[i]
                b_hero_j = b_team[j]
                r_hero_i = r_team[i]
                r_hero_j = r_team[j]

                # for synergy
                if i != j:
                    synergy['matches'][r_hero_i, r_hero_j] += 1
                    synergy['matches'][b_hero_i, b_hero_j] += 1

                    if b_win == 1:
                        synergy['wins'][b_hero_i, r_hero_j] += 1
                    else:
                        synergy['wins'][r_hero_i, b_hero_j] += 1

                # for counter
                counter['matches'][r_hero_i, b_hero_j] += 1
                counter['matches'][b_hero_i, r_hero_j] += 1

                if b_win == 1:
                    counter['wins'][b_hero_i, r_hero_j] += 1
                else:
                    counter['wins'][r_hero_i, b_hero_j] += 1

def compute_wining_rates(synergy, counter, self_win, heroes_num):
    """
    Loop through synergy and counter dict and compute winning rate
    """
    for i in range(heroes_num):
        self_win['winrate'][i] = self_win['wins'][i] / self_win['matches'][i] if self_win['matches'][i] != 0.0 else 0.0
        for j in range(heroes_num):
            if i != j:
                if synergy['matches'][i, j] != 0:
                    synergy['winrate'][i, j] = synergy['wins'][i, j] / float(synergy['matches'][i, j])

                if counter['matches'][i, j] != 0:
                    counter['winrate'][i, j] = counter['wins'][i, j] / float(counter['matches'][i, j])

def build_matrix(synergy, counter, self_win, heroes_num):
    """
    Loop through all combination of heroes to build score matrix for synergy and counter
    """
    sy_mat = np.zeros((heroes_num, heroes_num))
    ct_mat = np.zeros((heroes_num, heroes_num))
    s_win_mat = np.zeros(heroes_num)

    for i in range(heroes_num):
        s_win_mat[i] = self_win['winrate'][i]
        for j in range(heroes_num):
            if i != j:
                if synergy['matches'][i, j] > 0:
                    sy_mat[i, j] = synergy['winrate'][i, j]/(self_win['winrate'][i]+self_win['winrate'][j])
                else:
                    sy_mat[i, j] = 0

                if counter['matches'][i, j] > 0:
                    ct_mat[i, j] = counter['winrate'][i, j]/(self_win['winrate'][i]+1-self_win['winrate'][j])
                else:
                    ct_mat[i, j] = 0

    return sy_mat, ct_mat, s_win_mat

if __name__ == '__main__':
    create_features()


def solution(genres, plays):
    answer = []
    Dict = {}
    Dict2 = {}
    SetG = list(set(genres))
    maxGen = ['none' for _ in SetG]
    for each in SetG:
        Dict[each] = 0
        Dict2[each] = [[], []]
    for i in range(len(genres)):
        Dict[genres[i]] += plays[i]
        Dict2[genres[i]][0].append(plays[i])
        Dict2[genres[i]][1].append(i)
    valL = sorted(Dict.values(), reverse = True)
    for k, v in Dict.items():
        i = valL.index(v)
        maxGen[i]=k
    for each in maxGen:
        tempL = Dict2[each]
        if len(tempL[1]) >= 2:
            SortGenVal = sorted(tempL[0], reverse = True)
            if SortGenVal[0] == SortGenVal[1]:
                practNum = SortGenVal.count(max(SortGenVal))
                if practNum == 2:
                    ind = tempL[0].index(SortGenVal[0])
                    saving = tempL
                    tempL[0].remove(SortGenVal[0])
                    tempL[1].remove(tempL[1][ind])
                    ind2 = tempL[0].index(SortGenVal[1])
                    if saving[1][ind] < saving[1][ind2]:
                        answer.append(saving[1][ind])
                        answer.append(saving[1][ind2])
                    elif saving[1][ind] > saving[1][ind2]:
                        answer.append(saving[1][ind2])
                        answer.append(saving[1][ind])
                else:
                    indexL = []
                    for e in SortGenVal[0:practNum-1]:
                        ind = tempL[0].index(e)
                        tempL[0].remove(e)
                        indexL.append(tempL[1][ind])
                        tempL[1].remove(tempL[1][ind])
                    sortL = sorted(indexL)
                    answer.append(sortL[0])
                    answer.append(sortL[1])
            else:
                ind = tempL[0].index(SortGenVal[0])
                answer.append(tempL[1][ind])
                ind2 = tempL[0].index(SortGenVal[1])
                answer.append(tempL[1][ind2])
        else:
            answer.append(tempL[1][0])
    return answer

print(solution(["classic", "pop", "classic", "pop", "classic", "classic"], [400, 600, 150, 600, 500, 500]))