#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 22 19:07:57 2019

@author: fatimamh
"""
import os
from os import path
import sys
import time
from datetime import datetime
import math
import random
import numpy as np
import pandas as pd

import torch
import torch.nn as nn
from torch import optim
from torch.autograd import Variable

from lib.utils.loader_utils import get_data
from lib.utils.file_utils import get_files
from lib.utils.model_utils import get_time
from lib.utils.model_utils import save_checkpoint
from lib.utils.model_utils import load_checkpoint
from lib.utils.file_utils import print_writer
from lib.utils.file_utils import plot_writer
from lib.utils.file_utils import build_row

from lib.utils.plot_utils import showPlot

'''----------------------------------------------------------------
'''

def train(device, model, data_loader, enc_optimizer, dec_optimizer, criterion, clip):
    ''' Training loop for the model to train.
    Args:
        model: A Seq2Seq model instance.
        iterator: A DataIterator to read the data.
        optimizer: Optimizer for the model.
        criterion: loss criterion.
        clip: gradient clip value.
    Returns:
        epoch_loss: Average loss of the epoch.
    '''

    # set model into train mode
    model.train()
    # define epoch loss
    epoch_loss = 0
    batch_num = 1
    total_batch = len(data_loader)
    for input_tensor, target_tensor in data_loader:
        batch_size = input_tensor.size()[0]
        if batch_size == 1: continue
        print('\t---Train Batch: {}/{}---'.format(batch_num, total_batch))
        input_tensor, target_tensor = input_tensor.to(device), target_tensor.to(device)
        '''------------------------------------------------------------
        3: Clear old gradients from the last step
        ------------------------------------------------------------'''
        enc_optimizer.zero_grad()
        dec_optimizer.zero_grad()
        #loss, predictions, attentions = model(input_tensor, target_tensor, criterion, teacher_forcing=True)
        loss, predictions = model(input_tensor, target_tensor, criterion, teacher_forcing=True)
        #print('predictins: {}'.format(predictions.shape))
        #print('target_tensor: {}'.format(target_tensor.shape))
        '''------------------------------------------------------------
        13: Compute the derivative of the loss w.r.t. the parameters
            (or anything requiring gradients) using backpropagation.
        ------------------------------------------------------------'''
        #del predictions
        loss.backward()
        # clip the gradients
        torch.nn.utils.clip_grad_norm_(model.parameters(), clip)
        '''------------------------------------------------------------
        14: Update optimizer to take a step based on the gradients
            of the parameters.
        ------------------------------------------------------------'''
        enc_optimizer.step()
        dec_optimizer.step()
        epoch_loss += loss.item()
        batch_num+=1

    epoch_loss /= len(data_loader)
    return epoch_loss

'''----------------------------------------------------------------
'''
def evaluate(device, model, data_loader, criterion):
    ''' Evaluation loop for the model to evaluate.
    Args:
        model: A Seq2Seq model instance.
        iterator: A DataIterator to read the data.
        criterion: loss criterion.
    Returns:
        epoch_loss: Average loss of the epoch.
    '''
	#set model in evaluation mode
    #  some layers have different behavior during train/and evaluation (like BatchNorm, Dropout) so setting it matters.
    model.eval()
	#declare loss
    epoch_loss = 0
    # we don't need to update the model parameters. only forward pass.
    with torch.no_grad():
        batch_num = 1
        total_batch = len(data_loader)

        for input_tensor, target_tensor in data_loader:
            batch_size = input_tensor.size()[0]
            if batch_size == 1: continue
            print('\t---Eval Batch: {}/{}---'.format(batch_num, total_batch))
            input_tensor, target_tensor = input_tensor.to(device), target_tensor.to(device)
            #loss, predictions, attentions = model(input_tensor, target_tensor, criterion, teacher_forcing=False)
            loss, predictions = model(input_tensor, target_tensor, criterion, teacher_forcing=False)

            epoch_loss += loss.item()
            batch_num +=1

    epoch_loss /= len(data_loader)
    return epoch_loss

'''----------------------------------------------------------------
'''

def train_model(device, config, model, criterion, enc_optimizer, dec_optimizer):

    '''-----------------------------------------------
    Step 1: Get from config
    -----------------------------------------------'''
    s_epoch     = None
    e_epoch     = config["epochs"]
    clip        = config["grad_clip"]
    print_every = config["print_every"]
    plot_every  = config["plot_every"]
    print_file  = config["print_file"]
    plot_file   = config["plot_file"]

    '''-----------------------------------------------
    Step 2: Get data_loaders
    -----------------------------------------------'''
    train_loader = get_data(config, "train")
    val_loader   = get_data(config, "val")

    '''-----------------------------------------------
    Step 3: From scratch or resume
    -----------------------------------------------'''
    # TO DO how to make it in a function
    bm_file     = os.path.join(config["check_point_folder"], config["best_model_file"])
    ckpt_file   = os.path.join(config["check_point_folder"], config["check_point_file"])
    #model_file = "best-model.pth.tar"

    if path.exists(ckpt_file):
        checkpoint 		= load_checkpoint(ckpt_file)
        s_epoch 		= checkpoint["epoch"]
        best_valid_loss = checkpoint["loss"]

        model.load_state_dict(checkpoint["model_state"])
        enc_optimizer.load_state_dict(checkpoint["enc_optimizer"])
        dec_optimizer.load_state_dict(checkpoint["dec_optimizer"])

    elif path.exists(bm_file):
        checkpoint 		= load_checkpoint(bm_file)
        s_epoch 		= checkpoint["epoch"]
        best_valid_loss = checkpoint["loss"]

        model.load_state_dict(checkpoint["model_state"])
        enc_optimizer.load_state_dict(checkpoint["enc_optimizer"])
        dec_optimizer.load_state_dict(checkpoint["dec_optimizer"])

    else: # from scratch
        s_epoch = 1
        best_valid_loss = float('inf')

    plot_train_loss_total = 0
    plot_valid_loss_total = 0
    '''-----------------------------------------------
    Step 2: Get data_loaders
    -----------------------------------------------'''

    # Training and evluation using train and val sets
    for epoch in range(s_epoch, e_epoch+1):
        print('--------Epoch:{} starts--------\n'.format(epoch))
        train_loss = 0
        valid_loss = 0
        '''------------------------------------------------------------
        5: Get batches from loader and pass to train
        ------------------------------------------------------------'''
        start_time = datetime.now()
        train_loss = train(device, model, train_loader, enc_optimizer, dec_optimizer, criterion, clip)
        valid_loss = evaluate(device, model, val_loader, criterion)
        end_time = datetime.now()
        time_diff = get_time(start_time, end_time)

        plot_train_loss_total += train_loss
        plot_valid_loss_total += valid_loss

        print('Epoch: {}/{} ==> {:.0f}% | Time: {}'.format(epoch, e_epoch, epoch/e_epoch*100, time_diff))

        is_best = bool(valid_loss < best_valid_loss)
        best_valid_loss = (min(valid_loss, best_valid_loss))

        print_row = build_row(epoch, train_loss, valid_loss, best_valid_loss, time_diff)
        print_writer(print_file, print_row)

        # Save checkpoint if is a new best
        if is_best:
            save_checkpoint({'epoch': epoch,
	    				 	 'model_state': model.state_dict(),
	    				 	 'enc_optimizer': enc_optimizer.state_dict(),
                             'dec_optimizer': dec_optimizer.state_dict(),
	    				 	 'loss': best_valid_loss}, bm_file, True)

        if epoch%5 == 0:
            save_checkpoint({'epoch': epoch,
	    					 'model_state': model.state_dict(),
                             'enc_optimizer': enc_optimizer.state_dict(),
                             'dec_optimizer': dec_optimizer.state_dict(),
	    				     'loss': best_valid_loss}, ckpt_file, False)

        if epoch % plot_every == 0:

            plot_train_loss_avg = plot_train_loss_total/plot_every
            plot_valid_loss_avg = plot_valid_loss_total/plot_every

            plot_row = build_row(epoch, plot_train_loss_avg, plot_valid_loss_avg)
            plot_writer(plot_file, plot_row)

            plot_train_loss_total = 0
            plot_valid_loss_total = 0

    showPlot(config["out_folder"], plot_file)

    return True


#
# @lc app=leetcode.cn id=47 lang=python3
#
# [47] 全排列 II
#

# @lc code=start
from typing import List, Set

class Solution:
    def search(self) -> List[List[int]]:
        ret: List[List[int]] = []
        isLeast = len(self.indexTaken) + 1 == len(self.nums)
        # to prevent duplication
        valueTaken = set()
        for i, x in enumerate(self.nums):
            if (i in self.indexTaken) or (x in valueTaken):
                continue
            if isLeast:
                return [[x]]
            valueTaken.add(x)
            self.indexTaken.add(i)
            rest = self.search()
            self.indexTaken.remove(i)
            ret.extend(map(lambda list: [x, *list], rest))
        return ret

    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        self.nums = nums
        self.indexTaken: Set[int] = set()
        return self.search()
# @lc code=end


import json
import leveldb
from settings import LEVELDB_PATH
from pysyncobj import SyncObj, SyncObjConf, replicated

ldb = leveldb.LevelDB(LEVELDB_PATH)

class KVStorage(SyncObj):
    def __init__(self, selfAddress, partnerAddrs, dumpFile=None):
        self.ldb = ldb
        conf = SyncObjConf(
            fullDumpFile=dumpFile,
        )
        super(KVStorage, self).__init__(selfAddress, partnerAddrs, conf)

    def get(self, key):
        try:
            if not isinstance(key, bytes):
                key = key.encode()
            value = self.ldb.Get(key)
            value = json.loads(value.decode())
        except KeyError:
            value = ""
        return value

    # @replicated
    def set(self, key, value):
        if isinstance(key, bytes):
            key = key.decode()
        if isinstance(value, bytes):
            value = value.decode()
        value = json.dumps(value)
        self.ldb.Put(key.encode(), value.encode())

    @replicated
    def pop(self, key):
        self.ldb.Delete(key)


import click
from gpt_translator.gpt_translator import GPTTranslator
from gpt_translator import file_utils
from gpt_translator import __version__
import logging
import os

# set logging level
logging.basicConfig(level=logging.INFO)

@click.group()
def cli():
    pass

@click.command()
@click.option('-f', '--from-file', help='Source file for translation.', required=True)
@click.option('-p', '--prompt', help='Prompt for translation.', required=True)
@click.option('-d', '--working-dir', default='output', help="Working directory. Default is 'output'")
@click.option('-m', '--max-tokens', default=1024, help='Max tokens per paragraph. Default is 1024')
@click.option('-i', '--idxs', default=None, multiple=True, type=int, help='Translate specific paragraphs by index. Default is None')
@click.option('--failure-sleep', default=10, help='Failure sleep time. Default is 10 seconds')
@click.option('--temperature', default=0.7, help='Temperature. Default is 0.7')
@click.option('--presence-penalty', default=0.1, help='Presence penalty. Default is 0.1')
@click.option('--top-p', default=0.99, help='Top P. Default is 0.99')
@click.option('--model', default='gpt-3.5-turbo', help='Model to use. Default is gpt-3.5-turbo')
@click.option('--separator', default=False, help='When exporting text, include part separator. Default is False')
def translate(from_file, prompt, idxs, working_dir, failure_sleep, temperature, presence_penalty, top_p, max_tokens, model, separator):

    translator = GPTTranslator(
        from_file=from_file,
        prompt=prompt,
        working_dir=working_dir,
        failure_sleep=failure_sleep,
        temperature=temperature,
        presence_penalty=presence_penalty,
        top_p=top_p,
        max_tokens=max_tokens,
        model=model,
        part_separator=separator,
    )

    if idxs:
        translator.translate_idxs(idxs)
    else:
        translator.translate()

cli.add_command(translate)

@click.command()
@click.option('-d', '--working-dir', default='output', help='Working directory. Default is output')
def cleanup(working_dir):
    if os.path.exists(working_dir):
        file_utils.cleanup(working_dir)

cli.add_command(cleanup)

@click.command()
def version():
    print(__version__)
    exit(0)

cli.add_command(version)

if __name__ == '__main__':
    cli()


from firmware_patcher import FirmwarePatcher

import e2_formats as fmt

from pathlib import Path
import logging

def main():
    logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
    be = HacktribeAppBackend()
    be.welcome_msg()

# ADD - groove edit, fx edit, pattern edit, etc...
class HacktribeAppBackend:
    def __init__(self):
        logging.debug('Initialising Backend')

        self.fw_patcher = FirmwarePatcher()

    def welcome_msg(self):
        logging.debug('Display welcome message')
        logging.info('Hacktribe Editor\n')
        logging.info('https://github.com/bangcorrupt/hacktribe\n')

if __name__ == "__main__":
    main()


import os
from flask import Flask, request, abort, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from custom_validator import valid_response
from flask_cors.decorator import cross_origin
import random

from models import setup_db, Question, Category

QUESTIONS_PER_PAGE = 10

def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__)
    setup_db(app)
    CORS(app)
    #========================================#
    #                                        #
    #  CORS (Cross Origin Resource Sharing)- #
    #  settings                       #      #
    #                                        #
    #========================================#

    @app.after_request
    def after_request(response):
        response.headers.add('Access-Control-Allow-Origin', '*')
        response.headers.add('Access-Control-Allow-Mehotds',
                             'GET, POST, PATCH, DELETE')
        response.headers.add('Content-Type', 'application/json')

        return response
    #========================================#
    #                                        #
    #  HELPERS - paginating function         #
    #  and filter_questions function         #
    #                                        #
    #========================================#

    def paginate_questions(request, selection):
        page = request.args.get('page', 1, type=int)
        start = (page - 1) * QUESTIONS_PER_PAGE
        end = start + QUESTIONS_PER_PAGE
        questions = [questions.format() for questions in selection]
        current_questions = questions[start:end]

        return current_questions

    def filter_questions(previous_questions, questions):
        return [question for question in questions if question['id'] not in previous_questions]

    def get_filtered_questions(previous_questions):
        questions = Question.query.all()
        questions = [question.format()
                     for question in questions]
        questions = filter_questions(
            previous_questions, questions)
        return questions
    #========================================#
    #                                        #
    #   [GET] /categories - retrive all      #
    #   categories                           #
    #                                        #
    #========================================#

    @app.route('/categories', methods=['GET'])
    @cross_origin()
    def get_all_catgories():
        categories = Category.query.all()
        formatted_categories = {}
        for category in categories:
            formatted_categories[str(category.format()['id'])] = category.format()[
                'type']

        return jsonify({
            "success": True,
            "categories": formatted_categories,
            "total_categories": len(categories)
        })
    #========================================#
    #                                        #
    #  [GET] /categories/<category_id> - get #
    #  get category by id                    #
    #                                        #
    #  [DELETE] /categories/<category_id> -  #
    #  delete category                       #
    #                                        #
    #  [PATCH] - categories/<category_id> -  #
    #  update category                       #
    #                                        #
    #========================================#

    @app.route('/categories/<int:category_id>', methods=['GET', 'PATCH', 'DELETE'])
    @cross_origin()
    def get_or_delete_or_update_category_by_id(category_id):
        if request.method == 'GET':
            category = Category.query.get_or_404(category_id)
            return jsonify({
                "category_id": category.id,
                "category_type": category.type,
                "total_categories": len(Category.query.all()),
                "success": True
            })
        if request.method == 'DELETE':
            category = Category.query.get_or_404(category_id)
            try:
                category.delete()
                return jsonify({
                    "deleted_category_id": category.id,
                    "success": True
                })
            except:
                abort(422)

        if request.method == 'PATCH':
            category = Category.query.get_or_404(category_id)
            data = request.get_json()
            try:
                if 'type' in data:
                    if len(data['type']) > 3:
                        category.type = data['type']
                    else:
                        abort(400)
                category.update()
                return jsonify({
                    "success": True,
                    "updated_category_id": category.id
                })
            except:
                abort(400)
    #========================================#
    #                                        #
    #   [POST] /categories - create new      #
    #   category                             #
    #                                        #
    #========================================#

    @app.route('/categories', methods=['POST'])
    @cross_origin()
    def add_new_category():
        data = request.get_json()
        if not data:
            abort(400)
        new_category = data.get('type')
        print(data)
        if not new_category or len(new_category) < 3:
            abort(400)
        try:
            category = Category(type=new_category)
            category.insert()
            return jsonify({
                "created_category_id": category.id,
                "success": True
            })
        except:
            abort(400)
    #========================================#
    #                                        #
    #   [GET]                                #
    #   /categories/<category_id>/questions  #
    #   - get questions by category          #
    #                                        #
    #========================================#

    @app.route('/categories/<int:category_id>/questions', methods=['GET'])
    @cross_origin()
    def get_questions_by_category(category_id):
        current_category = Category.query.get_or_404(category_id)
        current_questions = paginate_questions(
            request, current_category.questions)
        return jsonify({
            "questions": current_questions,
            "total_questions": len(Question.query.all()),
            "current_category": current_category.type,
            "success": True
        })
    #========================================#
    #                                        #
    #  [GET] /questions - get all questions  #
    #                                        #
    #========================================#

    @app.route('/questions', methods=['GET'])
    @cross_origin()
    def get_all_questions():
        selection = Question.query.all()
        current_questions = paginate_questions(request, selection)
        page = request.args.get('page', 1, type=int)
        if page != 1 and len(current_questions) == 0:
            abort(404)
        categories = Category.query.all()
        formatted_categories = {}
        for category in categories:
            formatted_categories[str(category.format()['id'])] = category.format()[
                'type']
        return jsonify({
            "questions": current_questions,
            "total_questions": len(selection),
            "categories": formatted_categories,
            "success": True
        })
    #========================================#
    #                                        #
    #   [POST] /questions - create new       #
    #   question                             #
    #                                        #
    #========================================#

    @app.route('/questions', methods=['POST'])
    @cross_origin()
    def create_new_question():
        data = request.get_json()
        if not data:
            abort(400)
        new_question = data.get('question')
        new_answer = data.get('answer')
        parent_category = data.get('category')
        new_difficulty = data.get('difficulty')
        search_term = data.get('searchTerm')
        try:
            if search_term:
                questions = Question.query.filter(
                    Question.question.ilike(f'%{search_term}%'))
                print(questions)
                current_questions = paginate_questions(request, questions)
                return jsonify({
                    "questions": current_questions,
                    "total_questions": len(Question.query.all())
                })

            else:
                category_id = int(parent_category)
                category_parent = Category.query.get(category_id)
                if len(new_question) <= 5 or category_parent is None:
                    abort(400)
                create_question = Question(question=new_question, answer=new_answer,
                                           category=category_parent, difficulty=new_difficulty)
                create_question.insert()
                return jsonify({
                    "success": True,
                    "created": create_question.id
                })
        except:
            abort(400)
    #========================================#
    #                                        #
    #   [GET] /questions/<question_id> - get #
    #   question by id                       #
    #                                        #
    #   [DELETE] - questions/<question_id> - #
    #   delete question                      #
    #                                        #
    #   [PATCH] - questions/<question_id> -  #
    #   update question                      #
    #                                        #
    #========================================#

    @app.route('/questions/<int:question_id>', methods=['GET', 'DELETE', 'PATCH'])
    @cross_origin()
    def get_or_delete_question(question_id):
        if request.method == 'GET':
            question = Question.query.get_or_404(question_id)
            return jsonify({
                "question_id": question.id,
                "question": question.question,
                "answer": question.answer,
                "difficulty": question.difficulty,
                "category_id": question.category.format()["id"],
                "category": question.category.format()["type"],
                "total_questions": len(Question.query.all()),
                "success": True
            })

        if request.method == 'PATCH':
            question = Question.query.get_or_404(question_id)
            data = request.get_json()

            try:
                if valid_response(data, 'question'):
                    question.question = data['question']
                if valid_response(data, 'answer'):
                    question.answer = data['answer']
                if valid_response(data, 'difficulty'):
                    question.difficulty = int(data['difficulty'])
                if valid_response(data, 'category', Category):
                    try:
                        category = Category.query.get(int(data['category']))
                        if category:
                            question.category = category
                        else:
                            abort(400)
                    except:
                        abort(400)
                question.update()
                return jsonify({
                    'success': True,
                    'updated_question_id': question.id
                })
            except:
                abort(400)

        if request.method == 'DELETE':
            question = Question.query.get_or_404(question_id)

            if question:
                question.delete()
                return jsonify({
                    "deleted_question_id": question.id,
                    "success": True
                })
            else:
                abort(422)
    #======================================#
    #                                      #
    #   [POST] /quizzes - play and enjoy   #
    #   with trivia quiz game              #
    #                                      #
    #======================================#

    @app.route('/quizzes', methods=['POST'])
    @cross_origin()
    def play_trivia_quizz_game():
        data = request.get_json()
        if data:
            previous_questions = data.get('previous_questions', [])
            if previous_questions:
                for question_id in previous_questions:
                    if not str(question_id).isdigit():
                        abort(400)
            quiz_category = data.get('quiz_category', {})
            if quiz_category:
                if 'id' in quiz_category:
                    if not str(quiz_category['id']).isdigit():
                        abort(400)
                else:
                    abort(400)
            if not quiz_category or int(quiz_category['id']) == 0:
                questions = Question.query.all()
                questions = [question.format()
                             for question in questions]
                questions = filter_questions(
                    previous_questions, questions)
            else:
                category = Category.query.get(int(quiz_category['id']))
                if category and category != None:
                    questions = Question.query.filter(
                        Question.category == category).all()
                    questions = [question.format()
                                 for question in questions]
                    questions = filter_questions(
                        previous_questions, questions)
                else:
                    abort(400)
            if len(questions) > 0:
                return jsonify({
                    "previous_questions": previous_questions,
                    "quiz_category": quiz_category,
                    "question": questions[random.randrange(0, len(questions))],
                    "success": True
                })
            else:
                return jsonify({
                    "previous_questions": previous_questions,
                    "question": None,
                    "success": True
                })

        else:
            abort(400)
    #====================#
    #                    #
    #   ErrorHandlers    #
    #                    #
    #====================#
    #                    #
    #   [400] - bad      #
    #   request          #
    #                    #
    #====================#

    @app.errorhandler(400)
    def bad_request(e):
        return jsonify({
            'success': False,
            'error': 400,
            'message': 'bad request'
        }), 400
    #====================#
    #                    #
    #   [404] - resource #
    #   not found        #
    #                    #
    #====================#

    @app.errorhandler(404)
    def resource_not_found(e):
        return jsonify({
            'success': False,
            'error': 404,
            'message': 'resource not found'
        }), 404
    #====================#
    #                    #
    #   [405] - method   #
    #   not allowed      #
    #                    #
    #====================#

    @app.errorhandler(405)
    def method_not_allowed(e):
        return jsonify({
            'success': False,
            'error': 405,
            'message': 'method now allowed'
        }), 405
    #====================#
    #                    #
    #   [422] - unproces #
    #   -sable entity    #
    #                    #
    #====================#

    @app.errorhandler(422)
    def unprocessable_entity(e):
        return jsonify({
            'success': False,
            'error': 422,
            'message': 'unprocessable entity'
        }), 422

    #====================#
    #                    #
    #   [500] - internal #
    #   server error     #
    #                    #
    #====================#

    @app.errorhandler(500)
    def internal_server_error(e):
        return jsonify({
            'success': False,
            'error': 500,
            'message': 'internal server error'
        }), 500

    return app


"""empty message

Revision ID: 23e303bcc82a
Revises: 635f1877200c
Create Date: 2022-06-30 17:48:23.090890

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '23e303bcc82a'
down_revision = '635f1877200c'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('room', sa.Column('capacity', sa.Integer(), nullable=False))
    op.drop_column('room', 'person_num')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('room', sa.Column('person_num', sa.INTEGER(), autoincrement=False, nullable=False))
    op.drop_column('room', 'capacity')
    # ### end Alembic commands ###


import discord
import os
from discord.ext import commands

Client = discord.Client()
bot_prefix = "!"
bot = commands.Bot(command_prefix=bot_prefix)
extensions = [
    "cmd.common",
    "cmd.reminder",
    "cmd.mirror",
    "cmd.wiki",
    "cmd.music"
    "cmd.vote"
]

@bot.event
async def on_ready():
    print("Online!")
    print("Name: {}".format(bot.user.name))
    print("ID: {}".format(bot.user.id))
    await bot.change_presence(game=discord.Game(name='with Arta'))

@bot.event
async def on_message(message):
    if message.author.bot:
        return
    await bot.process_commands(message)

@bot.event
async def on_resume():
    print("Resuming")

def main():
    bot.load_extension('cmd.ArtaBot')
    for ext in extensions:
        try:
            bot.load_extension(ext)
        except Exception as e:
            print("Failed to load Extension : {}\n {}: {}".format(ext, type(e).__name__, e))
    bot.run(os.environ.get('BOT_TOKEN'))

if __name__ == '__main__':
    main()


# for parallel to work, picloud must be installed:
# > sudo pip install cloud
# > picloud setup
# Make sure you get a picloud key from tech support!

import multiprocessing
import numpy
import functools
import tqdm
import time

import inspect

def _escapable_child(f, *args):
	# help prevent any forking issues with seeding the RNG
	numpy.random.seed()

	try:
		return f(*args)

	except KeyboardInterrupt:
		return

def aync_run(f_args, pool_size = None, process = True):
	if pool_size == None:
		try:
			pool_size = multiprocessing.cpu_count()

		except NotImplementedError:
			pool_size = 1

	if process:
		# processes are not subject to the GIL
		pool = multiprocessing.Pool(processes = pool_size)

	else:
		# threads are
		pool = multiprocessing.pool.ThreadPool(processes = pool_size)

	try:
		async_results = []
		for f, args in f_args:
			partial_f = functools.partial(_escapable_child, f)
			async_result = pool.apply_async(partial_f, args)
			async_results.append(async_result)

		pool.close()

	except KeyboardInterrupt:
		pool.terminate()
		raise

	except Exception:
		pool.terminate()
		raise

	finally:
		pool.join()

	return [async_result.get() for async_result in async_results]

def lmap(f, lst, pool_size = None, process = True, progress_bar = False):
	"""Parallelize a map using local processes or threads.	Works well if the function we are offloading to is a C function, like an expensive pandas or numpy function.

	   f should be a read-only function -- i.e. it doesn't manipulate local data"""

	if pool_size == None:
		try:
			pool_size = multiprocessing.cpu_count()

		except NotImplementedError:
			pool_size = 1

	if process:
		# processes are not subject to the GIL
		pool = multiprocessing.Pool(processes = pool_size)

	else:
		# threads are
		pool = multiprocessing.pool.ThreadPool(processes = pool_size)

	try:
		partial_f = functools.partial(_escapable_child, f)
		if progress_bar:
			l = list(tqdm.tqdm(pool.starmap(partial_f, lst), total=len(lst)))
		else:
			l = pool.starmap(partial_f, lst)
		pool.close()

	except KeyboardInterrupt:
		pool.terminate()
		raise

	except Exception:
		pool.terminate()
		raise

	finally:
		pool.join()

	return l


from django.urls import path
from . import views

urlpatterns = [
    path("", views.login_view, name="login"),
    path("register", views.register, name="register"),
    path("logout", views.logout_view, name="logout"),
    path("status/<int:status>", views.success_status, name="status"),
    path("home/<str:user>", views.home_view, name="home"),
    path("<str:user>/share", views.share_property, name="share"),
    # path("<str:user>/saveshare", views.saveSharing, name="saveSharing"),
    path("home/<str:plot_no>/myproperties", views.my_properties, name="myproperty"),
    path("home/<str:plot_no>/shareProperty", views.SharedProperty_view, name="sharedproperty"),
    path("home/<str:plot_no>/agreement", views.agreement_view, name="agreement_view"),
    path("/make_agreement", views.make_agreement, name="make_agreement"),
    path("home/<str:plot_no>/saledeed", views.saledeed_view, name="saledeed_view"),
    path("bank", views.bank, name="bank"),
]


# -*- coding: utf-8 -*-
import os
import copy
import odl
import torch
import numpy as np

from math import ceil
from tqdm import tqdm
from warnings import warn

from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from torch.optim.lr_scheduler import CyclicLR, OneCycleLR

from dival import LearnedReconstructor
from dival.measure import PSNR

torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

class BaseLearnedReconstructor(LearnedReconstructor):
    HYPER_PARAMS = {
        'epochs': {
            'default': 20,
            'retrain': True
        },
        'batch_size': {
            'default': 64,
            'retrain': True
        },
        'lr': {
            'default': 0.01,
            'retrain': True
        },
        'normalize_by_opnorm': {
            'default': False,
            'retrain': True
        }
    }

    def __init__(self, ray_trafo, epochs=None, batch_size=None, lr=None,
                 normalize_by_opnorm=None,
                 num_data_loader_workers=8, use_cuda=True, show_pbar=True,
                 fbp_impl='astra_cuda', hyper_params=None, log_dir=None,
                 log_num_validation_samples=0,
                 save_best_learned_params_path=None,
                 **kwargs):
        """
        Parameters
        ----------
        ray_trafo : :class:`odl.tomo.RayTransform`
            Ray transform from which the FBP operator is constructed.
        scales : int, optional
            Number of scales in the U-Net (a hyper parameter).
        epochs : int, optional
            Number of epochs to train (a hyper parameter).
        batch_size : int, optional
            Batch size (a hyper parameter).
        lr : float, optional
            Base learning rate (a hyper parameter).
        normalize_by_opnorm : bool, optional
            Whether to normalize :attr:`ray_trafo` by its operator norm.
        num_data_loader_workers : int, optional
            Number of parallel workers to use for loading data.
        use_cuda : bool, optional
            Whether to use cuda for the U-Net.
        show_pbar : bool, optional
            Whether to show tqdm progress bars during the epochs.
        fbp_impl : str, optional
            The backend implementation passed to
            :class:`odl.tomo.RayTransform` in case no `ray_trafo` is specified.
            Then ``dataset.get_ray_trafo(impl=fbp_impl)`` is used to get the
            ray transform and FBP operator.
        log_dir : str, optional
            Tensorboard log directory (name of sub-directory in utils/logs).
            If `None`, no logs are written.
        log_num_valiation_samples : int, optional
            Number of validation images to store in tensorboard logs.
            This option only takes effect if ``log_dir is not None``.
        save_best_learned_params_path : str, optional
            Save best model weights during training under the specified path by
            calling :meth:`save_learned_params`.
        """
        super().__init__(reco_space=ray_trafo.domain,
                         observation_space=ray_trafo.range,
                         hyper_params=hyper_params, **kwargs)
        self.ray_trafo = ray_trafo
        self.num_data_loader_workers = num_data_loader_workers
        self.use_cuda = use_cuda
        self.show_pbar = show_pbar
        self.fbp_impl = fbp_impl
        self.log_dir = log_dir
        self.log_num_validation_samples = log_num_validation_samples
        self.save_best_learned_params_path = save_best_learned_params_path
        self.model = None

        if epochs is not None:
            self.epochs = epochs
            if kwargs.get('hyper_params', {}).get('epochs') is not None:
                warn("hyper parameter 'epochs' overridden by constructor "
                     "argument")

        if batch_size is not None:
            self.batch_size = batch_size
            if kwargs.get('hyper_params', {}).get('batch_size') is not None:
                warn("hyper parameter 'batch_size' overridden by constructor "
                     "argument")

        if lr is not None:
            self.lr = lr
            if kwargs.get('hyper_params', {}).get('lr') is not None:
                warn("hyper parameter 'lr' overridden by constructor argument")

        if normalize_by_opnorm is not None:
            self.normalize_by_opnorm = normalize_by_opnorm
            if (kwargs.get('hyper_params', {}).get('normalize_by_opnorm')
                    is not None):
                warn("hyper parameter 'normalize_by_opnorm' overridden by "
                     "constructor argument")

        if self.normalize_by_opnorm:
            self.opnorm = odl.power_method_opnorm(self.ray_trafo)
            self.ray_trafo = (1./self.opnorm) * self.ray_trafo

        self.device = (torch.device('cuda:0')
                       if self.use_cuda and torch.cuda.is_available() else
                       torch.device('cpu'))

    def eval(self, test_data):
        self.model.eval()

        running_psnr = 0.0
        with tqdm(test_data, desc='test ',
                  disable=not self.show_pbar) as pbar:
            for obs, gt in pbar:
                rec = self.reconstruct(obs)
                running_psnr += PSNR(rec, gt)

        return running_psnr / len(test_data)

    def train(self, dataset):
        torch.random.manual_seed(1)
        # create PyTorch datasets
        dataset_train = dataset.create_torch_dataset(
            part='train', reshape=((1,) + dataset.space[0].shape,
                                   (1,) + dataset.space[1].shape))

        dataset_validation = dataset.create_torch_dataset(
            part='validation', reshape=((1,) + dataset.space[0].shape,
                                        (1,) + dataset.space[1].shape))

        # reset model before training
        self.init_model()

        criterion = torch.nn.MSELoss()
        self.init_optimizer(dataset_train=dataset_train)

        # create PyTorch dataloaders
        data_loaders = {'train': DataLoader(
            dataset_train, batch_size=self.batch_size,
            num_workers=self.num_data_loader_workers, shuffle=True,
            pin_memory=True),
            'validation': DataLoader(
                dataset_validation, batch_size=self.batch_size,
                num_workers=self.num_data_loader_workers,
                shuffle=True, pin_memory=True)}

        dataset_sizes = {'train': len(dataset_train),
                         'validation': len(dataset_validation)}

        self.init_scheduler(dataset_train=dataset_train)
        if self.scheduler is not None:
            schedule_every_batch = isinstance(
                self.scheduler, (CyclicLR, OneCycleLR))

        best_model_wts = copy.deepcopy(self.model.state_dict())
        best_psnr = 0

        if self.log_dir is not None:
            writer = SummaryWriter(
                log_dir=os.path.join(BASE_DIR, 'utils/logs', self.log_dir),
                max_queue=0)
            validation_samples = dataset.get_data_pairs(
                'validation', self.log_num_validation_samples)

        self.model.to(self.device)
        self.model.train()

        for epoch in range(self.epochs):
            # Each epoch has a training and validation phase
            for phase in ['train', 'validation']:
                if phase == 'train':
                    self.model.train()  # Set model to training mode
                else:
                    self.model.eval()  # Set model to evaluate mode

                running_psnr = 0.0
                running_loss = 0.0
                running_size = 0
                with tqdm(data_loaders[phase],
                          desc='epoch {:d}'.format(epoch + 1),
                          disable=not self.show_pbar) as pbar:
                    for inputs, labels in pbar:
                        if self.normalize_by_opnorm:
                            inputs = (1./self.opnorm) * inputs
                        inputs = inputs.to(self.device)
                        labels = labels.to(self.device)

                        # zero the parameter gradients
                        self.optimizer.zero_grad()

                        # forward
                        # track gradients only if in train phase
                        with torch.set_grad_enabled(phase == 'train'):
                            outputs = self.model(inputs)
                            loss = criterion(outputs, labels)

                            # backward + optimize only if in training phase
                            if phase == 'train':
                                loss.backward()
                                torch.nn.utils.clip_grad_norm_(
                                    self.model.parameters(), max_norm=1)
                                self.optimizer.step()
                                if (self.scheduler is not None and
                                        schedule_every_batch):
                                    self.scheduler.step()
                                # lrs.append(self.scheduler.get_lr())
                                # losses.append(loss.item())

                        for i in range(outputs.shape[0]):
                            labels_ = labels[i, 0].detach().cpu().numpy()
                            outputs_ = outputs[i, 0].detach().cpu().numpy()
                            running_psnr += PSNR(outputs_, labels_)

                        # statistics
                        running_loss += loss.item() * outputs.shape[0]
                        running_size += outputs.shape[0]

                        pbar.set_postfix({'phase': phase,
                                          'loss': running_loss/running_size,
                                          'psnr': running_psnr/running_size})
                        if self.log_dir is not None and phase == 'train':
                            step = (epoch * ceil(dataset_sizes['train']
                                                 / self.batch_size)
                                    + ceil(running_size / self.batch_size))
                            writer.add_scalar('loss/{}'.format(phase),
                                              torch.tensor(running_loss/running_size), step)
                            writer.add_scalar('psnr/{}'.format(phase),
                                              torch.tensor(running_psnr/running_size), step)

                    if self.scheduler is not None and not schedule_every_batch:
                        self.scheduler.step()

                    epoch_loss = running_loss / dataset_sizes[phase]
                    epoch_psnr = running_psnr / dataset_sizes[phase]

                    if self.log_dir is not None and phase == 'validation':
                        step = (epoch+1) * ceil(dataset_sizes['train']
                                                / self.batch_size)
                        writer.add_scalar('loss/{}'.format(phase),
                                          epoch_loss, step)
                        writer.add_scalar('psnr/{}'.format(phase),
                                          epoch_psnr, step)

                    # deep copy the model (if it is the best one seen so far)
                    if phase == 'validation' and epoch_psnr > best_psnr:
                        best_psnr = epoch_psnr
                        best_model_wts = copy.deepcopy(self.model.state_dict())
                        if self.save_best_learned_params_path is not None:
                            self.save_learned_params(
                                self.save_best_learned_params_path)

            # import matplotlib.pyplot as plt
            # plt.plot(lrs)
            # plt.show()
            # plt.plot(lrs, losses)
            # plt.show()

                if (phase == 'validation' and self.log_dir is not None and
                        self.log_num_validation_samples > 0):
                    with torch.no_grad():
                        val_images = []
                        for (y, x) in validation_samples:
                            y = torch.from_numpy(
                                np.asarray(y))[None, None].to(self.device)
                            x = torch.from_numpy(
                                np.asarray(x))[None, None].to(self.device)
                            reco = self.model(y)
                            reco -= torch.min(reco)
                            reco /= torch.max(reco)
                            val_images += [reco, x]
                        writer.add_images(
                            'validation_samples', torch.cat(val_images),
                            (epoch + 1) * (ceil(dataset_sizes['train'] /
                                                self.batch_size)),
                            dataformats='NCWH')

        print('Best val psnr: {:4f}'.format(best_psnr))
        self.model.load_state_dict(best_model_wts)

    def init_model(self):
        """
        Initialize :attr:`model`.
        Called in :meth:`train` at the beginning.
        """
        raise NotImplementedError

    def init_optimizer(self, dataset_train):
        """
        Initialize the optimizer.
        Called in :meth:`train`, after calling :meth:`init_model` and before
        calling :meth:`init_scheduler`.

        Parameters
        ----------
        dataset_train : :class:`torch.utils.data.Dataset`
            The training (torch) dataset constructed in :meth:`train'.
        """
        self.optimizer = torch.optim.Adam(self.model.parameters(), lr=self.lr)

    def init_scheduler(self, dataset_train):
        """
        Initialize the learning rate scheduler.
        Called in :meth:`train`, after calling :meth:`init_optimizer`.

        Parameters
        ----------
        dataset_train : :class:`torch.utils.data.Dataset`
            The training (torch) dataset constructed in :meth:`train'.
        """
        self.scheduler = torch.optim.lr_scheduler.OneCycleLR(
            self.optimizer, max_lr=self.lr,
            steps_per_epoch=ceil(len(dataset_train) / self.batch_size),
            epochs=self.epochs)

    def _reconstruct(self, observation):
        self.model.eval()
        with torch.set_grad_enabled(False):
            obs_tensor = torch.from_numpy(
                np.asarray(observation)[None, None])
            if self.normalize_by_opnorm:
                obs_tensor = obs_tensor / self.opnorm
            obs_tensor = obs_tensor.to(self.device)
            reco_tensor = self.model(obs_tensor)
            reconstruction = reco_tensor.cpu().detach().numpy()[0, 0]
        return self.reco_space.element(reconstruction)

    def save_learned_params(self, path):
        path = path if path.endswith('.pt') else path + '.pt'
        torch.save(self.model.state_dict(), path)

    def load_learned_params(self, path, force_parallel=False):
        path = path if path.endswith('.pt') else path + '.pt'
        self.init_model()
        map_location = ('cuda:0' if self.use_cuda and torch.cuda.is_available()
                        else 'cpu')
        state_dict = torch.load(path, map_location=map_location)

        # backwards-compatibility with non-data_parallel weights
        data_parallel = list(state_dict.keys())[0].startswith('module.')
        if force_parallel and not data_parallel:
            state_dict = {('module.' + k): v for k, v in state_dict.items()}
        self.model.load_state_dict(state_dict)


import sqlite3

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

# file name of the userinfo
filename = 'usersinfo.sqlite3'

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

def run():

    '''
    Initialize the sqlfile.
    '''

    conn = sqlite3.connect(filename)
    cur = conn.cursor()
    cur.execute('CREATE TABLE IF NOT EXISTS Favs (id TEXT, fav1lati REAL,'
        ' fav1lon REAL, fav2lati REAL, fav2lon REAL)')
    conn.commit()
    conn.close()

#-------------------------------------------------------------------------------

def get(user, option=None):

    '''
    Return the user info according to the option.
    '''

    if user is None:
        raise ValueError("user can't be none")
        return None

    conn = sqlite3.connect(filename)
    cur = conn.cursor()

    if option is None:
        cur.execute('SELECT * FROM Favs WHERE id = ?', (user,))
        locations = cur.fetchone()
        conn.close()
        return locations
    else:
        location = [None, None]
        latS = option + 'lati'
        lonS = option + 'lon'
        sqllat = 'SELECT {lat} FROM Favs WHERE id = ?'.format(lat=latS, lon=lonS)
        sqllon = 'SELECT {lon} FROM Favs WHERE id = ?'.format(lat=latS, lon=lonS)

        cur.execute(sqllat, (user,))
        col = cur.fetchone()
        if col is not None:
            location[0] = col[0]

        cur.execute(sqllon, (user,))
        col = cur.fetchone()
        if col is not None:
            location[1] = col[0]

        conn.close
        return location

#---------------------------------------------------------------------------------

def edit(user, option, lat, lon):

    '''
    Edit the user info to lat, lon in the option columns
    '''

    conn = sqlite3.connect(filename)
    cur = conn.cursor()

    latS = option + 'lati'
    lonS = option + 'lon'

    sqllat = 'SELECT {lat} FROM Favs WHERE id = ?'.format(lat=latS, lon=lonS)
    sqllon = 'SELECT {lon} FROM Favs WHERE id = ?'.format(lat=latS, lon=lonS)
    cur.execute(sqllat, (user,))
    collat = cur.fetchone()
    cur.execute(sqllon, (user,))
    collon = cur.fetchone()

    if collat is None or collon is None:
        cur.execute('INSERT INTO Favs (id, {lat}, {lon}) VALUES ( ?, ?, ?)'.format(lat=latS, lon=lonS), (user, lat, lon))
        conn.commit()
    else:
        cur.execute('UPDATE Favs SET {lat} = ? WHERE id = ?'.format(lat=latS), (lat, user))
        cur.execute('UPDATE Favs SET {lon} = ? WHERE id = ?'.format(lon=lonS), (lon, user))
        conn.commit()

    cur.execute('SELECT * FROM Favs')
    for row in cur :
        print(row)

    conn.close()
    return ["END", None]

#-------------------------------------------------------------------------

if __name__ == "__main__":

    '''
    For testing
    '''

    user = input("Enter the user")
    run()
    print(str(get(user)))
    edit(user, 'fav2', 40, 75)
    print(str(get(user)))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# File : find_alpha.py
# Description : Find best value for alpha.
# Auteur : Nils Schaetti <nils.schaetti@unine.ch>
# Date : 01.02.2017 17:59:05
# Lieu : Neuchâtel, Suisse
#
# This file is part of the PAN18 author profiling challenge code.
# The PAN18 author profiling challenge code is a set of free software:
# you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Foobar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
#

# Imports
import os
import torch
import dataset
import numpy as np
from torch.autograd import Variable
from tools import settings, functions

################################################
# MAIN
################################################

# Argument parser
args = functions.argument_parser_execution()

# Image augmentation and normalization
image_transform = functions.image_transformer('val')

# For each alpha value
for alpha in np.arange(0.9, 1.1, 0.1):
    print(u"Alpha {}".format(alpha))
    # Counters
    total_images_success = 0.0
    total_tweets_success = 0.0
    total_both_success = 0.0
    total = 0.0
    total_diff = 0.0

    # For each language
    for lang in ['en', 'es', 'ar']:
        # Load models and voc
        image_model, tweet_model, tweet_voc = functions.load_models(
            model_type=args.image_model,
            n_gram=args.n_gram,
            lang=lang,
            cuda=args.cuda
        )

        # Counters
        images_success = 0.0
        tweets_success = 0.0
        both_success = 0.0
        count = 0
        diff = 0

        # Make to output directory
        output_dir = os.path.join(args.output_dir, lang)
        if not os.path.exists(output_dir):
            os.mkdir(output_dir)
        # end

        # Transformer
        text_transform = functions.tweet_transformer(lang, args.n_gram, voc=tweet_voc)

        # Author profiling data set
        profiling_dataset = dataset.AuthorProfilingDataset(root=args.input_dataset, download=False, lang=lang,
                                                           text_transform=text_transform, image_transform=image_transform,
                                                           train=False, filter_robot=True)
        pan18loader = torch.utils.data.DataLoader(profiling_dataset, batch_size=1, shuffle=True)

        # For the data set
        for data in pan18loader:
            # Images, tweets and labels
            tweets, images, labels = data

            # Variable and CUDA
            images, tweets, labels = Variable(images), Variable(tweets), Variable(labels)
            if args.cuda:
                images, tweets, labels = images.cuda(), tweets.cuda(), labels.cuda()
            # end if

            # Tweets batch size
            tweets_batch_size = tweets.size(1)

            # Remove images and tweet dimension
            tweets = tweets.view(-1, settings.min_length)
            images = images.view(-1, 3, settings.image_size, settings.image_size)

            # Compute prob. for tweets and images
            images_probs = image_model(images)
            tweets_probs = tweet_model(tweets)

            # Resize to images and tweet dimension and transpose
            tweets_probs = tweets_probs.view(-1, tweets_batch_size, 2)
            images_probs = images_probs.view(-1, 10, 2)

            # Tweet and image prob
            tweets_prob = torch.mean(tweets_probs, 1)
            image_prob = torch.mean(images_probs, 1)

            # Take the max as predicted
            _, images_prediction = torch.max(torch.mean(images_probs, 1), 1)
            _, tweets_prediction = torch.max(torch.mean(tweets_probs, 1), 1)

            # Both prediction
            _, both_prediction = torch.max(
                (torch.mean(images_probs, 1) * alpha + torch.mean(tweets_probs, 1) * (1.0 - alpha)) / 2.0,
                1
            )

            # Both prob
            both_prob = (torch.mean(images_probs, 1) * alpha + torch.mean(tweets_probs, 1) * (1.0 - alpha)) / 2.0

            # labels
            if int(labels) == 0:
                prob_diff = float(both_prob[0, 0] - both_prob[0, 1])
            else:
                prob_diff = float(both_prob[0, 1] - both_prob[0, 0])
            # end if

            # Add to correctly classified profiles
            images_success += int((images_prediction == labels).sum())
            tweets_success += int((tweets_prediction == labels).sum())
            both_success += int((both_prediction == labels).sum())
            diff += prob_diff

            # Add to total
            count += labels.size(0)
        # end for

        # Total success
        total_images_success += images_success
        total_tweets_success += tweets_success
        total_both_success += both_success
        total_diff += diff
        total += count

        # Accuracies
        images_accuracy = images_success / count * 100.0
        tweets_accuracy = tweets_success / count * 100.0
        diff_accuracy = diff / count
        both_accuracy = both_success / count * 100.0

        # Print language accuracy
        print(u"\tLang {}, Images accuracy {}, Tweets accuracy {}, Both accuracy {}, Diff {}".format(
            lang,
            images_accuracy,
            tweets_accuracy,
            both_accuracy,
            diff_accuracy)
        )
    # end for

    # Accuracies
    total_images_accuracy = total_images_success / total * 100.0
    total_tweets_accuracy = total_tweets_success / total * 100.0
    total_diff_accuracy = total_diff / total
    total_both_accuracy = total_both_success / total * 100.0

    # Print total accuracy
    print(u"Alpha {}, Images accuracy {}, Tweets accuracy {}, Both accuracy {}, Diff {}".format(
        alpha,
        total_images_accuracy,
        total_tweets_accuracy,
        total_both_accuracy,
        total_diff_accuracy)
    )
# end for


from PyQt6.QtCore import QSize
from PyQt6.QtWidgets import (
    QApplication,
    QMainWindow,
    QVBoxLayout,
    QWidget,
)
from widgets import *
import json
import os

basedir = os.path.dirname(__file__)
datafile = os.path.join(basedir, "data", "data.json")

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Task Tracker")
        self.setFixedWidth(500)
        self.setWindowOpacity(0.8)

        #load data from json file
        self.load()
        self.model.layoutChanged.connect(self.save)

        #save buttun
        hlayout = QHBoxLayout()
        self.catwidget = CategoryWidget(self.model, self.categories, self.colors)
        refresh = RoundedButton("Save", "gray")
        refresh.clicked.connect(self.model.save)
        hlayout.addWidget(refresh)

        #add category
        addCategory = RoundedButton("Add Category", 'green')
        addCategory.clicked.connect(self.catwidget.addCategoryWindow)
        addCategory.clicked.connect(self.model.save)
        hlayout.addWidget(addCategory)

        layout = QVBoxLayout()
        layout.addWidget(TaskListWidget(self.model))
        layout.addLayout(hlayout)
        layout.addWidget(self.catwidget)

        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def load(self):
        try:
            with open (datafile, 'r') as f:
                data = json.load(f)
                self.categories = data['categories']
                self.colors = data['colors']
                tasks = data['tasks']
                for t in tasks:
                    t[1] = datetime.strptime(t[1][:10], "%Y-%m-%d")
                self.model = TaskModel(tasks=tasks, cols=['', 'Due Date', 'Category', 'Description', 'Color'])
        except:
            self.model = TaskModel(tasks=[], cols=['', 'Due Date', 'Category', 'Description', 'Color'])
            self.categories = []
            self.colors = []

    def save(self):
        with open(datafile, 'w') as f:
            jsondata = {'tasks': self.model.tasks, 'categories': self.catwidget.categories, 'colors': self.catwidget.colors}
            json.dump(jsondata, f, default=str)

app = QApplication([])
# Create a Qt widget, which will be our window.
window = MainWindow()
window.show()  # IMPORTANT!!!!! Windows are hidden by default.

# Start the event loop.
app.exec()


from dataclasses import dataclass
from accelerate import Accelerator
from trainer.accelerators.base_accelerator import BaseAcceleratorConfig, BaseAccelerator

@dataclass
class DebugAcceleratorConfig(BaseAcceleratorConfig):
    _target_: str = "trainer.accelerators.debug_accelerator.DebugAccelerator"

class DebugAccelerator(BaseAccelerator):
    def __init__(self, cfg: DebugAcceleratorConfig):
        super().__init__(cfg)
        self.accelerator = Accelerator(
            gradient_accumulation_steps=cfg.gradient_accumulation_steps,
            mixed_precision=cfg.mixed_precision,
            log_with=cfg.log_with,
            project_dir=cfg.output_dir,
            dynamo_backend=cfg.dynamo_backend,
        )
        self.post_init()


import pydicom as dicom
import matplotlib.pylab as plt
import json

image_path = './x.dcm'
output_name = "fichero"

def write_dicom_json(image_path, output_name):
    ds=dicom.dcmread(image_path)

    diccionario = {'Columns': ds.Columns, 'Rows': ds.Rows }

    with open(output_name+'.json', 'w') as json_file:
     json.dump(diccionario, json_file) #Escribir lo que hay en diccionario en el fichero

write_dicom_json(image_path, output_name)


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

import datetime as dt
import json
import os

import jsonschema

from rally import api
from rally import consts
from rally import exceptions
from rally.task.processing import charts

OLD_TASK_RESULT_SCHEMA = {
    "type": "object",
    "$schema": consts.JSON_SCHEMA,
    "properties": {
        "key": {
            "type": "object",
            "properties": {
                "kw": {
                    "type": "object"
                },
                "name": {
                    "type": "string"
                },
                "pos": {
                    "type": "integer"
                },
            },
            "required": ["kw", "name", "pos"]
        },
        "sla": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "criterion": {
                        "type": "string"
                    },
                    "detail": {
                        "type": "string"
                    },
                    "success": {
                        "type": "boolean"
                    }
                }
            }
        },
        "hooks": {"type": "array"},
        "result": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "atomic_actions": {
                        "type": "object"
                    },
                    "duration": {
                        "type": "number"
                    },
                    "error": {
                        "type": "array"
                    },
                    "idle_duration": {
                        "type": "number"
                    },
                    "output": {"type": "object"}
                },
                "required": ["atomic_actions", "duration", "error",
                             "idle_duration"]
            },
            "minItems": 0
        },
        "load_duration": {
            "type": "number",
        },
        "full_duration": {
            "type": "number",
        },
        "created_at": {
            "type": "string"
        }
    },
    "required": ["key", "sla", "result", "load_duration", "full_duration"],
    "additionalProperties": False
}

class FailedToLoadResults(exceptions.RallyException):
    error_code = 225
    msg_fmt = "ERROR: Invalid task result format in %(source)s\n\n\t%(msg)s"

def _update_atomic_actions(atomic_actions, started_at):
    """Convert atomic actions in old format to latest one."""
    new = []
    for name, duration in atomic_actions.items():
        finished_at = started_at + duration
        new.append({
            "name": name,
            "started_at": started_at,
            "finished_at": finished_at,
            "children": []}
        )
        started_at = finished_at
    return new

def _update_old_results(tasks_results, path):
    """Converts tasks results in old format to latest one."""
    task = {"version": 2,
            "title": "Task loaded from a file.",
            "description": "Auto-ported from task format V1.",
            "uuid": "n/a",
            "env_name": "n/a",
            "env_uuid": "n/a",
            "tags": [],
            "status": consts.TaskStatus.FINISHED,
            "subtasks": []}

    start_time = None

    for result in tasks_results:
        try:
            jsonschema.validate(
                result, OLD_TASK_RESULT_SCHEMA)
        except jsonschema.ValidationError as e:
            raise FailedToLoadResults(source=path,
                                      msg=str(e))

        iter_count = 0
        failed_iter_count = 0
        min_duration = None
        max_duration = None

        for itr in result["result"]:
            if start_time is None or itr["timestamp"] < start_time:
                start_time = itr["timestamp"]
            # NOTE(chenhb): back compatible for atomic_actions
            itr["atomic_actions"] = _update_atomic_actions(
                itr["atomic_actions"], started_at=itr["timestamp"])

            iter_count += 1
            if itr.get("error"):
                failed_iter_count += 1

            duration = itr.get("duration", 0)

            if max_duration is None or duration > max_duration:
                max_duration = duration

            if min_duration is None or min_duration > duration:
                min_duration = duration

        durations_stat = charts.MainStatsTable(
            {"total_iteration_count": iter_count})

        for itr in result["result"]:
            durations_stat.add_iteration(itr)

        created_at = dt.datetime.strptime(result["created_at"],
                                          "%Y-%d-%mT%H:%M:%S")
        updated_at = created_at + dt.timedelta(
            seconds=result["full_duration"])
        created_at = created_at.strftime(consts.TimeFormat.ISO8601)
        updated_at = updated_at.strftime(consts.TimeFormat.ISO8601)
        pass_sla = all(s.get("success") for s in result["sla"])
        runner_type = result["key"]["kw"]["runner"].pop("type")
        for h in result["hooks"]:
            trigger = h["config"]["trigger"]
            h["config"] = {
                "description": h["config"].get("description"),
                "action": (h["config"]["name"], h["config"]["args"]),
                "trigger": (trigger["name"], trigger["args"])}
        workload = {"uuid": "n/a",
                    "name": result["key"]["name"],
                    "position": result["key"]["pos"],
                    "description": result["key"].get("description",
                                                     ""),
                    "full_duration": result["full_duration"],
                    "load_duration": result["load_duration"],
                    "total_iteration_count": iter_count,
                    "failed_iteration_count": failed_iter_count,
                    "min_duration": min_duration,
                    "max_duration": max_duration,
                    "start_time": start_time,
                    "created_at": created_at,
                    "updated_at": updated_at,
                    "args": result["key"]["kw"]["args"],
                    "runner_type": runner_type,
                    "runner": result["key"]["kw"]["runner"],
                    "hooks": result["hooks"],
                    "sla": result["key"]["kw"]["sla"],
                    "sla_results": {"sla": result["sla"]},
                    "pass_sla": pass_sla,
                    "contexts": result["key"]["kw"]["context"],
                    "contexts_results": [],
                    "data": sorted(result["result"],
                                   key=lambda x: x["timestamp"]),
                    "statistics": {
                        "durations": durations_stat.to_dict()},
                    }
        task["subtasks"].append(
            {"title": "A SubTask",
             "description": "",
             "workloads": [workload]})
    return [task]

def _update_new_results(tasks_results):
    for task_result in tasks_results["tasks"]:
        try:
            jsonschema.validate(task_result, api._Task.TASK_SCHEMA)
        except jsonschema.ValidationError as e:
            raise exceptions.RallyException(
                "ERROR: Invalid task result format\n\n\t%s" % str(e)) from None
        task_result.setdefault("env_name", "n/a")
        task_result.setdefault("env_uuid", "n/a")
        for subtask in task_result["subtasks"]:
            for workload in subtask["workloads"]:
                workload.setdefault("contexts_results", [])
                workload["runner_type"], workload["runner"] = list(
                    workload["runner"].items())[0]
                workload["name"], workload["args"] = list(
                    workload.pop("scenario").items())[0]

    return tasks_results["tasks"]

def load(path):
    with open(os.path.expanduser(path)) as f:
        raw_tasks_results = f.read()

    try:
        tasks_results = json.loads(raw_tasks_results)
    except ValueError:
        raise FailedToLoadResults(
            source=path, msg="error while loading JSON.") from None

    if isinstance(tasks_results, list):
        return _update_old_results(tasks_results, path)
    elif isinstance(tasks_results, dict) and "tasks" in tasks_results:
        return _update_new_results(tasks_results)
    else:
        raise FailedToLoadResults(
            source=path, msg="Wrong format")


import json
import math
import unittest

from embedded_jubatus import Weight
from jubatus.common import Datum
from jubatus.weight.types import Feature

CONFIG = {
    "converter": {
        "num_filter_types": {},
        "num_filter_rules": [],
        "string_filter_types": {},
        "string_filter_rules": [],
        "num_types": {},
        "num_rules": [
            {"key": "n0", "type": "num"},
            {"key": "n1", "type": "log"},
            {"key": "n2", "type": "str"}
        ],
        "string_types": {},
        "string_rules": [
            {"key": "t0", "type": "space",
             "sample_weight": "bin", "global_weight": "bin"},
            {"key": "t1", "type": "space",
             "sample_weight": "tf", "global_weight": "idf1"}
        ]
    }
}

class TestWeight(unittest.TestCase):
    def test(self):
        w = Weight(CONFIG)
        d = Datum({'n0': 1, 'n1': 2, 'n2': 3, 't0': 'hello world'})
        for r in [w.update(d), w.calc_weight(d)]:
            self.assertEqual(5, len(r))
            for x in r:
                self.assertTrue(isinstance(x, Feature))
            m = dict([(x.key, x.value) for x in r])
            self.assertEqual(5, len(m))
            self.assertEqual(1.0, m['n0@num'])
            self.assertAlmostEqual(math.log(2), m['n1@log'])
            self.assertEqual(1.0, m['n2@str$3'])

        w.update(Datum({'t1': 'hello world'}))
        w.update(Datum({'t1': 'foo bar'}))
        w.update(Datum({'t1': 'hello'}))
        d = Datum({'t1': 'foo bar hello world hoge'})
        r0 = dict([(x.key, x.value) for x in w.calc_weight(d)])

        model = w.save_bytes()
        w = Weight(CONFIG)
        w.load_bytes(model)
        self.assertEqual(CONFIG, json.loads(w.get_config()))
        r1 = dict([(x.key, x.value) for x in w.calc_weight(d)])
        self.assertEqual(r0, r1)

        st = w.get_status()
        self.assertTrue(isinstance(st, dict))
        self.assertEqual(len(st), 1)
        self.assertEqual(list(st.keys())[0], 'embedded')
        self.assertTrue(isinstance(st['embedded'], dict))


from typing import (
    Awaitable,
    Callable,
    Optional,
    TYPE_CHECKING,
)
import functools
from tornado.web import HTTPError

if TYPE_CHECKING:
    from cylc.uiserver.handlers import CylcAppHandler

def authenticated(
    method: Callable[..., Optional[Awaitable[None]]]
) -> Callable[..., Optional[Awaitable[None]]]:
    """
    A decorator based on the original by tornado.web.authenticated.

    This decorator, different than the original, does not forward
    the user to the log-in form. If the user is not present, it
    fails immediately.

    This is necessary, as when establishing a WebSockets connection,
    the client may not be able to handle HTTP redirects.

    Args:
        method (Callable): The method to be decorated
    Returns:
        Callable: decorated method
    """
    @functools.wraps(method)
    def wrapper(  # type: ignore
            self: 'CylcAppHandler', *args, **kwargs
    ) -> Optional[Awaitable[None]]:
        if not self.current_user:
            self.log.debug('Unauthenticated WebSocket request!')
            raise HTTPError(403)

        return method(self, *args, **kwargs)

    return wrapper


import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from scipy.stats import norm, kstest
import math

data = pd.read_excel('statistics\penis_size_values.xlsx')
loc, scale = norm.fit(data)
n = norm(loc=loc, scale=scale)
print(n)

plt.hist(data, bins=50)

mu = 13.78
variance = 1
sigma = math.sqrt(variance)
x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
plt.plot(x, 25*norm.pdf(x, mu, sigma))
plt.title("Penis length averaged by country \n(we can conclude that this is not normally distributed)")
plt.xlabel("Centimeters")
plt.ylabel("Number of Countries")
plt.show()

print(kstest(data, n.cdf))


import io

import pynmea2
import serial

ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=5.0)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))

while 1:
    try:
        line = sio.readline()
        value = line.__str__()
        print(value)
        file1 = open("gps_data.txt","a")
        file1.write(value)
        file1.close()
    except serial.SerialException as e:
        print('Device error:{}'.format(e))
        continue
    except UnicodeDecodeError as e:
        continue


import copy
import logging
import numpy as np
from itertools import product
from pprint import pprint
from typing import Callable

import pytest
from sklearn.tree import DecisionTreeRegressor
from sklearn.base import clone
import sklearn.tree
# from sklearn.tree._tree import Tree
from sklearn.utils._testing import assert_allclose
from sklearn.utils.validation import check_symmetric, check_random_state

from bipartite_learn.tree._bipartite_classes import (
    BipartiteDecisionTreeRegressor,
    BipartiteDecisionTreeClassifier,
    _normalize_weights,
)
from bipartite_learn.tree import _bipartite_classes
from bipartite_learn.melter import row_cartesian_product

from .utils.make_examples import make_interaction_blobs, make_interaction_regression, make_interaction_data
from .utils.test_utils import assert_equal_dicts, stopwatch, parse_args, gen_mock_data, melt_2d_data

# from sklearn.tree._tree import DTYPE_t, DOUBLE_t
DTYPE_t, DOUBLE_t = np.float32, np.float64

logging.basicConfig(level=logging.DEBUG)
logging.getLogger("matplotlib").setLevel(logging.CRITICAL)

# Default test params
DEF_PARAMS = dict(
    n_samples=(51, 60),
    n_features=(10, 9),
    # noise=2,
    noise=0.1,
    # noise=0.0,  # makes it unable to reach 1 sample per leaf
)

@pytest.fixture(params=range(10))
def random_state(request):
    return request.param

@pytest.fixture(params=[(37, 61)])
def n_samples(request):
    return request.param

@pytest.fixture(params=[(10, 9)])
def n_features(request):
    return request.param

@pytest.fixture(params=["squared_error_gso", "friedman_gso"])
def gso_criterion(request):
    return request.param

def get_leaves(tree: sklearn.tree._tree.Tree):
    # the Tree object can be accessed from tree estimators with estimator.tree_
    ch_left = tree.children_left
    ch_right = tree.children_right
    leaf_mask = ch_right == ch_left  # == -1 == sklearn.tree._tree.TREE_LEAF

    return dict(
        value=tree.value[leaf_mask],
        impurity=tree.impurity[leaf_mask],
        n_node_samples=tree.n_node_samples[leaf_mask],
        weighted_n_node_samples=tree.weighted_n_node_samples[leaf_mask],
    )

def assert_equal_leaves(
    tree1, tree2, verbose=False, ignore=None, rtol=1e-7, atol=1e-8,
):
    n_leaves1 = tree1.n_leaves
    n_leaves2 = tree2.n_leaves
    assert n_leaves1 == n_leaves2, (
        f'Number of leaves differ: {n_leaves1} != {n_leaves2}.'
    )
    leaves1 = get_leaves(tree1)
    leaves2 = get_leaves(tree2)

    for leaves in (leaves1, leaves2):
        # It is normal for the last leaves to differ in their order.
        keys_to_order = [
            leaves['impurity'].reshape(-1),
            leaves['weighted_n_node_samples'].reshape(-1),
        ]
        if ignore is None or 'value' not in ignore:
            if leaves1['value'].shape != leaves2['value'].shape:
                raise ValueError(
                    'Diferent leaf value shapes, you are probably working with '
                    'mixed classification-regression trees. To ignore leaf '
                    'values include "value" in the ignore parameter.'
                )
            keys_to_order.append(leaves['value'].reshape(-1))

        order = np.lexsort(np.vstack(keys_to_order))

        for k, v in leaves.items():
            leaves[k] = v[order]
            # leaves[k] = np.sort(leaves[k].flatten())
            # leaves[k] = np.sort(np.unique(leaves[k].flatten()))

    assert_equal_dicts(leaves1, leaves2, ignore=ignore, rtol=rtol, atol=atol)

# TODO: parameter description.
def compare_trees(
    tree1,
    tree2,
    random_state,
    tree2_is_2d=True,
    supervision=1.0,
    max_gen_depth=None,
    verbose=False,
    rtol=1e-7,
    atol=1e-8,
    **params,
):
    """Fit and compare trees.

    Fit bipartite_learnBipartiteDecisionTreeRegressor on mock data and assert the grown
    tree is identical to the one built by sklearn.DecisionTreeRegressor.

    Parameters
    ----------
        random_state : int
        shape : list-like of int
        n_features : list-like of int
        nrules : int
        min_samples_leaf : int
        transpose_test : bool
        noise : float
    """
    print('Starting with settings:')
    pprint(dict(
        random_state=random_state,
        tree2_is_2d=tree2_is_2d,
        supervision=supervision,
        max_gen_depth=max_gen_depth,
    ) | params)

    with stopwatch():
        # XX, Y, x, y, gen_tree = make_interaction_regression(
        #     return_molten=True,
        #     return_tree=True,
        #     n_samples=params['n_samples'],
        #     n_features=params['n_features'],
        #     noise=params['noise'],
        #     random_state=random_state,
        #     max_depth=max_gen_depth,
        #     n_targets=params.get('n_targets'),
        #     max_target=1000,
        # )
        XX, Y, x, y = make_interaction_blobs(
            return_molten=True,
            n_samples=params['n_samples'],
            n_features=params['n_features'],
            random_state=random_state,
            noise=2.0,
            centers=10,
        )

    # NOTE on ExtraTrees:
    # Even with the same random_state, the way 2d splitter uses this random
    # state will be different (same random state for each axis), thus yielding
    # an ExtraTree2d different from sklearn's ExtraTree.

    with stopwatch(f'Fitting {tree1.__class__.__name__}...'):
        if supervision == 1.0:
            print('* Using supervised data for tree1.')
            tree1.fit(x, y)
        elif supervision == 0.0:
            print('* Using unsupervised data for tree1.')
            tree1.fit(x, x)
        else:
            print('* Using semisupervised data for tree1.')
            # Apply 'supervision' parameter weighting
            Xy = np.hstack((x.copy(), y.copy().reshape(-1, 1)))
            # Xy.shape[1] == n_features + n_outputs = n_features + 1
            Xy[:, -1] *= np.sqrt(supervision * Xy.shape[1])
            Xy[:, :-1] *= np.sqrt((1-supervision) *
                                  Xy.shape[1] / (Xy.shape[1]-1))
            tree1.fit(x, Xy)

    with stopwatch(f'Fitting {tree2.__class__.__name__}...'):
        if tree2_is_2d:
            print('* Using 2D data for tree2.')
            tree2.fit(XX, Y)
        else:
            print('* Using 1D data for tree2.')
            tree2.fit(x, y)

    if verbose:
        print(f'* Tree 1 ({tree1.__class__.__name__}) params:')
        pprint(tree1.get_params())
        print(f'* Tree 2 ({tree2.__class__.__name__}) params:')
        pprint(tree2.get_params())

    assert_equal_leaves(
        tree1.tree_,
        tree2.tree_,
        ignore=['value'] if supervision != 1.0 else None,
        rtol=rtol,
        atol=atol,
    )

@pytest.mark.parametrize('msl', [1, 5, 100])
@pytest.mark.parametrize('max_depth', [None, 5, 10])
@pytest.mark.parametrize(
    'criterion_mono, criterion_bi',
    [
        ('squared_error', 'squared_error_gso'),
        ('friedman_mse', 'friedman_gso'),
    ]
)
def test_simple_tree_1d2d(
    msl,
    random_state,
    max_depth,
    criterion_mono,
    criterion_bi,
    **params,
):
    params = DEF_PARAMS | params
    tree2 = BipartiteDecisionTreeRegressor(
        min_samples_leaf=msl,
        max_depth=max_depth,
        random_state=check_random_state(random_state),
        bipartite_adapter="gmosa",
        criterion=criterion_bi,
    )

    tree1 = DecisionTreeRegressor(
        min_samples_leaf=msl,
        max_depth=max_depth,
        criterion=criterion_mono,
        random_state=check_random_state(random_state),
    )

    return compare_trees(
        tree1=tree1,
        tree2=tree2,
        random_state=random_state,
        **params,
    )

@pytest.mark.parametrize(
    "pred_weight", [None, "uniform", "precomputed", lambda x: x**2])
class TestGMOSymmetry:
    """Test X symmetry constraints of GMO trees.
    """
    params = DEF_PARAMS
    XX, Y = make_interaction_regression(**params)
    XX_sim, Y_sim, = make_interaction_regression(
        **(params | dict(n_features=params['n_samples'])))

    pbct = BipartiteDecisionTreeRegressor(
        bipartite_adapter="gmo",
    )

    def test_square_array_error(self, pred_weight, **params):
        params = DEF_PARAMS | params
        pbct = clone(self.pbct).set_params(prediction_weights=pred_weight)
        with pytest.raises(
            ValueError,
            match=r"array must be 2-dimensional and square.",
        ):
            pbct.fit(self.XX, self.Y)

    def test_non_symmetric_array(self, pred_weight, **params):
        params = DEF_PARAMS | params
        pbct = clone(self.pbct).set_params(prediction_weights=pred_weight)
        with pytest.raises(
            ValueError,
            match=r"Array must be symmetric",
        ):
            pbct.fit(self.XX_sim, self.Y_sim)

    @pytest.mark.parametrize("splitter", ["random", "best"])
    def test_working(self, pred_weight, splitter, **params):
        params = DEF_PARAMS | params
        pbct = clone(self.pbct).set_params(
            prediction_weights=pred_weight,
            splitter=splitter,
        )
        XX_sim = [check_symmetric(Xi, raise_warning=False)
                  for Xi in self.XX_sim]
        pbct.fit(XX_sim, self.Y_sim)
        pbct.predict(np.hstack([XX_sim[0][:3], XX_sim[1][:3]]))

@pytest.mark.parametrize("splitter", ["random", "best"])
@pytest.mark.parametrize("adapter", ["gmosa"])
def test_identity_gso(splitter, adapter, gso_criterion, **params):
    params = DEF_PARAMS | params
    XX, Y, x, y = make_interaction_regression(return_molten=True, **params)
    tree = BipartiteDecisionTreeRegressor(
        min_samples_leaf=1,
        splitter=splitter,
        bipartite_adapter=adapter,
        criterion=gso_criterion,
    )
    assert_allclose(tree.fit(XX, Y).predict(XX).reshape(Y.shape), Y)

@pytest.mark.parametrize(
    "pred_weights", [
        None,
        *_bipartite_classes.PREDICTION_WEIGHTS_OPTIONS-{"raw"},
        lambda x: x ** 3,
    ]
)
@pytest.mark.parametrize("splitter", ["random", "best"])
@pytest.mark.parametrize(
    "criterion", _bipartite_classes.BIPARTITE_CRITERIA["gmo"]["regression"].keys()
)
def test_identity_gmo(pred_weights, splitter, criterion, **params):
    """Tests wether regression trees can grow util isolating every instance.
    """
    params = DEF_PARAMS | params
    params['n_features'] = params['n_samples'] #= (10, 10)

    XX, Y = make_interaction_regression(**params)
    tree = BipartiteDecisionTreeRegressor(
        min_samples_leaf=1,
        bipartite_adapter="gmo",
        prediction_weights=pred_weights,
        splitter=splitter,
        criterion=criterion,
    )
    XX = [check_symmetric(X, raise_warning=False) for X in XX]
    Y *= 1000

    tree.fit(XX, Y)

    wn_samples = tree.tree_.weighted_n_node_samples
    ch_left = tree.tree_.children_left
    ch_right = tree.tree_.children_right
    n_samples_per_leaf = wn_samples[ch_left == ch_right]

    assert_allclose(n_samples_per_leaf, 1.0)
    assert_allclose(tree.predict(XX).reshape(Y.shape), Y)

@pytest.mark.parametrize("splitter", ["random", "best"])
def test_random_state(splitter, n_samples, random_state):
    random_state = check_random_state(random_state)

    XX = [random_state.uniform(size=(n, n)) for n in n_samples]
    XX = [check_symmetric(X, raise_warning=False) for X in XX]
    Y = random_state.choice((0., 1.), size=n_samples)

    tree1 = BipartiteDecisionTreeRegressor(
        min_samples_leaf=1,
        bipartite_adapter="gmosa",
        splitter=splitter,
        criterion="squared_error",
        random_state=copy.deepcopy(random_state),
    ).fit(XX, Y)

    tree2 = BipartiteDecisionTreeRegressor(
        min_samples_leaf=1,
        bipartite_adapter="gmosa",
        splitter=splitter,
        criterion="squared_error",
        random_state=copy.deepcopy(random_state),
    ).fit(XX, Y)

    leaves1 = get_leaves(tree1.tree_)
    leaves2 = get_leaves(tree2.tree_)

    values1 = tree1.tree_.value[..., -1].reshape(-1)
    values2 = tree2.tree_.value[..., -1].reshape(-1)

    assert len(values1) > 10, "Not enough leaves"

    pred1 = tree1.predict(XX)
    pred2 = tree2.predict(XX)

    assert_equal_dicts(
        {
            **leaves1,
            'value': values1,
            'predictions': pred1,
        },
        {
            **leaves2,
            'value': values2,
            'predictions': pred2,
        },
    )

@pytest.mark.parametrize("splitter", ["random", "best"])
def test_gini_mse_identity(splitter, n_samples, random_state):
    random_state_ = check_random_state(random_state)

    XX = [random_state_.uniform(size=(n, n)) for n in n_samples]
    XX = [check_symmetric(X, raise_warning=False) for X in XX]
    Y = random_state_.choice((0., 1.), size=n_samples)

    # NOTE on random_state: passing the integer itself to the trees was the only
    # option to yield consistent results. Even deepcopying the random_state
    # object seems to induce some differences.
    tree_reg = BipartiteDecisionTreeRegressor(
        min_rows_leaf=10,
        min_cols_leaf=10,
        bipartite_adapter="gmosa",
        splitter=splitter,
        criterion="squared_error",
        random_state=random_state,
    ).fit(XX, Y)

    tree_clf = BipartiteDecisionTreeClassifier(
        min_rows_leaf=10,
        min_cols_leaf=10,
        bipartite_adapter="gmosa",
        splitter=splitter,
        criterion="gini",
        random_state=random_state,
    ).fit(XX, Y)

    leaves_reg = get_leaves(tree_reg.tree_)
    leaves_clf = get_leaves(tree_clf.tree_)

    leaves_reg['impurity'] *= 2

    values_reg = tree_reg.tree_.value[..., -1].reshape(-1)
    values_clf = tree_clf.tree_.value[..., -1].flatten()  # copy
    values_clf /= tree_clf.tree_.weighted_n_node_samples

    assert len(leaves_reg['impurity']) > 5, "Not enough leaves"

    pred_reg = tree_reg.predict(XX)
    pred_clf = tree_clf.predict_proba(XX)[..., -1]

    assert_equal_dicts(
        {
            **leaves_reg,
            'value': values_reg,
            'predictions': pred_reg,
        },
        {
            **leaves_clf,
            'value': values_clf,
            'predictions': pred_clf,
        },
    )

@pytest.mark.parametrize("min_samples_leaf", [1, 20, 100])
@pytest.mark.parametrize("splitter", ["random", "best"])
@pytest.mark.parametrize(
    "criterion", _bipartite_classes.BIPARTITE_CRITERIA["gmo"]["regression"].keys()
)
def test_leaf_mean_symmetry(min_samples_leaf, splitter, criterion):
    """Tests if Y_leaf.mean(0).mean() == Y_leaf.mean(1).mean()

    Y_leaf.mean(0) and Y_leaf.mean(1) are stored by regression GMO trees as
    output values. This function tests if these values are correct by checking
    the aforementioned equality.
    """
    params = DEF_PARAMS

    XX, Y, x, y = make_interaction_regression(return_molten=True, **params)
    tree = BipartiteDecisionTreeRegressor(
        min_samples_leaf=min_samples_leaf,
        bipartite_adapter="gmo",
        prediction_weights="raw",
        splitter=splitter,
        criterion=criterion,
    )
    tree.fit(XX, Y)
    pred = tree.predict(XX)
    row_pred, col_pred = np.hsplit(pred, [tree._n_rows_fit])
    mean_rows = np.nanmean(row_pred, axis=1)
    mean_cols = np.nanmean(col_pred, axis=1)

    assert not np.isnan(mean_rows).any()
    assert not np.isnan(mean_cols).any()
    assert_allclose(mean_rows, mean_cols)

def test_weight_normalization():
    rng = np.random.default_rng()
    w = rng.random((100, 20))
    zeroed_w = rng.choice(w.shape[0], w.shape[0]//10)
    w[zeroed_w] = 0.

    pred = rng.random((100, 20))
    not_nan = rng.choice((True, False), pred.shape)
    pred[~not_nan] = np.nan

    w = _normalize_weights(w, pred)

    assert_allclose(w.sum(axis=1), 1.)
    assert all(
        1. not in row or set(row.unique()) < {0., 1.}
        for row in w
    )
    assert np.all(w[np.isnan(pred)] == 0.)
    assert_allclose(
        w[zeroed_w],
        (not_nan / not_nan.sum(axis=1, keepdims=True))[zeroed_w]
    )

@pytest.mark.parametrize("mrl,mcl", [(1, 5), (2, 1), (11, 19)])
@pytest.mark.parametrize("criterion", ["squared_error"])
def test_leaf_shape_gmo(random_state, mrl, mcl, criterion, **params):
    params = DEF_PARAMS | params
    # XX, Y, x, y = gen_mock_data(melt=True, **params)
    rng = check_random_state(random_state)
    n_clusters = 10, 10
    Y_unique = np.arange(np.prod(n_clusters), dtype=float).reshape(n_clusters)
    Y = Y_unique.repeat(mrl, axis=0).repeat(mcl, axis=1)
    shape = mrl * n_clusters[0], mcl * n_clusters[1]

    n_features = 50, 50
    XX = [
        rng.random((s, f)).astype(np.float32)
        for s, f in zip(shape, n_features)
    ]
    XX[0][:, 0] = np.sort(XX[0][:, 0])
    XX[1][:, 0] = np.sort(XX[1][:, 0])

    # Shuffle
    id_rows = rng.choice(shape[0], size=shape[0], replace=False)
    id_cols = rng.choice(shape[1], size=shape[1], replace=False)
    XX[0] = XX[0][id_rows]
    XX[1] = XX[1][id_cols]
    Y = Y[np.ix_(id_rows, id_cols)]

    tree = BipartiteDecisionTreeRegressor(
        min_rows_leaf=mrl,
        min_cols_leaf=mcl,
        bipartite_adapter="gmo",
        prediction_weights="raw",
        criterion=criterion,
    )
    tree.fit(XX, Y)
    pred = tree.predict(XX)
    row_pred, col_pred = np.hsplit(pred, [tree._n_rows_fit])
    n_rows = (~np.isnan(row_pred)).sum(axis=1)
    n_cols = (~np.isnan(col_pred)).sum(axis=1)

    assert_allclose(n_rows, mrl)
    assert_allclose(n_cols, mcl)

@pytest.mark.parametrize("mrl,mcl", [(1, 1), (1, 5), (2, 1), (19, 11)])
def test_leaf_shape_gso(mrl, mcl, random_state, gso_criterion, **params):
    params = DEF_PARAMS | params
    # XX, Y, x, y = gen_mock_data(melt=True, **params)
    rng = check_random_state(random_state)
    n_clusters = 10, 10
    Y_unique = np.arange(np.prod(n_clusters), dtype=float).reshape(n_clusters)
    Y = Y_unique.repeat(mrl, axis=0).repeat(mcl, axis=1)
    y_values = np.sort(Y_unique.reshape(-1))

    shape = mrl * n_clusters[0], mcl * n_clusters[1]
    n_features = 50, 50
    XX = [
        rng.random((s, f)).astype(np.float32)
        for s, f in zip(shape, n_features)
    ]
    XX[0][:, 0] = np.sort(XX[0][:, 0])
    XX[1][:, 0] = np.sort(XX[1][:, 0])

    # Shuffle
    id_rows = rng.choice(shape[0], size=shape[0], replace=False)
    id_cols = rng.choice(shape[1], size=shape[1], replace=False)
    XX[0] = XX[0][id_rows]
    XX[1] = XX[1][id_cols]
    Y = Y[np.ix_(id_rows, id_cols)]

    # Control result
    X, y = row_cartesian_product(XX), Y.reshape(-1)
    tree1d = DecisionTreeRegressor()
    tree1d.fit(X, y)

    tree = BipartiteDecisionTreeRegressor(
        min_rows_leaf=mrl,
        min_cols_leaf=mcl,
        bipartite_adapter="gmosa",
        criterion=gso_criterion,
    )
    tree.fit(XX, Y)

    leaves1 = get_leaves(tree1d.tree_)
    leaf_values1d = np.sort(leaves1['value'].reshape(-1))
    assert_allclose(leaves1['n_node_samples'], mrl*mcl)
    assert_allclose(leaf_values1d, y_values)

    assert_equal_leaves(tree1d.tree_, tree.tree_)


import numpy as np
import matplotlib.pyplot as plt

blocks = [
    4,
    8,
    16,
    32,
    64,
    128,
    256,
    512,
    1024,
    2048,
]

direct = [
    15.48,
    17.02,
    24.96,
    50.82,
    157.93,
    601.19,
    2218.42,
    9348.04,
    36895.13,
    154896.50,
]

fft = [
    31.00,
    33.78,
    33.83,
    44.84,
    64.02,
    94.29,
    161.30,
    325.70,
    621.68,
    1029.86,
]

blocks = np.array(blocks)
fft = np.array(fft) * 2
direct = np.array(direct)

logn = np.array(blocks) * np.log10(np.array(blocks))
nsqrd = np.array(blocks) ** 2

fig, ax = plt.subplots(figsize=(5, 3))

ax.plot(blocks, direct, label="Direct")
ax.plot(blocks, fft, label="FFT")
# ax.plot(blocks, logn, label="O(N log N)")
# ax.plot(blocks, nsqrd, label="O(N^2)")

ax.set_xticks([16, 128, 256, 512, 1024])
ax.set_xlim([16, 1024])
ax.set_ylim([0, 40e3])
# ax.set_yscale("log")
ax.grid(c="lightgray")
ax.set_ylabel("Runtime (μs)")
ax.set_xlabel("Filter size")
plt.tight_layout()
plt.legend()
plt.savefig("docs/figures/runtime.pdf")
plt.savefig("docs/figures/runtime.png")


import cv2
import numpy as np

#Pick an image to analyze
img = cv2.imread('cube.jpg')

#Coordinates we need to extract [y,x], y increasing downwards
coordinates = 84,141

#color outputs as [B G R]
pxl = img[coordinates]

#Print pxl, RGB values
print(pxl)

#returns a List of coordinates, read from a txt file
def getCoordinatesArray(filename):
    file = open(filename, "r")
    upList, rightList, fwdList,downList, leftList, backList = [],[],[],[],[],[]

    list = []
    for line in file:
        list.append(line.strip().split('\n'))

    for x in range(0,6):
        upList.append(tuple(list[x]))
    for x in range(6,12):
        rightList.append(tuple(list[x]))
    for x in range(12,18):
        fwdList.append(tuple(list[x]))
    for x in range(18,24):
        downList.append(tuple(list[x]))
    for x in range(24,30):
        leftList.append(tuple(list[x]))
    for x in range(30,36):
        backList.append(tuple(list[x]))

    file.close()
    colorList = []
    colorList.append(upList)
    colorList.append(rightList)
    colorList.append(fwdList)
    colorList.append(downList)
    colorList.append(leftList)
    colorList.append(backList)
    return colorList

#print("R: " + str(pxl[2]) + "\tG: " + str(pxl[1]) + "\tB: " + str(pxl[0]))


from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import path
from flask_login import LoginManager
from .logger_config import setup_logger
db = SQLAlchemy()
DB_NAME = "schmeisinger.db"

logger = setup_logger()

def create_app():
    app = Flask(__name__) # initialize flask
    app.config['SECRET_KEY'] = 'eih7theicieFeipap5heechaethaeT' # remove in production
    app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}'
    db.init_app(app)

    from .views import views # register our blueprint
    from .auth import auth
    from .configure import configure
    app.register_blueprint(views,url_prefix='/')
    app.register_blueprint(auth, url_prefi='/') # '/auth' > means you need to navigate /auth/<name of page>
    app.register_blueprint(configure, url_prefix='/')

    from .models import UserEntity, GameEntity, PenaltyEntity, PenaltyRecordEntity, ParticipantEntity
    create_database(app)

    login_manager = LoginManager()
    login_manager.init_app(app)
    login_manager.login_view = 'auth.login'
    @login_manager.user_loader
    def load_user(id):
        return UserEntity.query.get(int(id)) # query.get is like query_id but looks for primary key by default so no id=id required

    def load_game(id):
        return GameEntity.query.get(int(id))
    return app

def create_database(app):
    # check if db exists if not create
    if not path.exists('website/' + DB_NAME):
        with app.app_context():
            db.create_all()
        logger.info("Created Database!")