import numpy as np
import matplotlib.pyplot as plt
import random
import torch
import torch.nn.functional as FNT
import torch.nn as nn

import random
import math
# (x,y,category)
points= []
N = 30    # number of points per class
K = 3     # number of classes
for i in range(N):
   r = i / N
   for k in range(K):
      t = ( i * 4 / N) + (k * 4) + random.uniform(0,0.2)
      points.append( [ ( r*math.sin(t), r*math.cos(t) ) , k ] )

#  outils d'affichage -  NE PAS TOUCHER

def DessineFond():
    iS = ComputeCatPerPixel()
    levels = [-1, 0, 1, 2]
    c1 = ('r', 'g', 'b')
    plt.contourf(XXXX, YYYY, iS, levels, colors = c1)

def DessinePoints():
    c2 = ('darkred','darkgreen','lightblue')
    for point in points:
        coord = point[0]
        cat   = point[1]
        plt.scatter(coord[0], coord[1] ,  s=50, c=c2[cat],  marker='o')

XXXX , YYYY = np.meshgrid(np.arange(-1, 1, 0.01), np.arange(-1, 1, 0.01))

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

def error_d(score, id_cat, d) :
    #Err = torch.FloatTensor([0,0,0])
    Err = 0
    for i in range(len(score)) :
        Err += torch.max(torch.FloatTensor([0,0,0]), score[i] - score[i,id_cat[i]] + d)
    return Err

# On se propose de travailler avec 2 couches de neurones :
# Input => Linear => Relu => Linear => Scores

neurones = 1000 #10 / 30 / 100 / 300 / 500 / 1000
layer1 = torch.nn.Linear(2,neurones)
activation = torch.nn.functional.relu
layer2 = torch.nn.Linear(neurones,3)

class Net(nn.Module) :
    def __init__(self):
        super().__init__()
        self.couche1 = layer1
        self.activ = activation
        self.couche2 = layer2

    def forward(self,x):
        x1 = self.couche1(x)
        x2 = self.activ(x1)
        x3 = self.couche2(x2)
        return x3

model = Net()
def ComputeCatPerPixel():
    XY = torch.stack((torch.FloatTensor(XXXX), torch.FloatTensor(YYYY)), 2)
    score = model(XY)
    CCCC = score.argmax(axis= 2)
    return CCCC

d = 0.5
input = torch.FloatTensor([i[0] for i in points] )
id_cat = torch.LongTensor([j[1] for j in points] )
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

for iteration in range(100):
    optimizer.zero_grad()
    score = model(input)
    err = error_d(score, id_cat, d)
    sum_error = torch.sum(err)
    sum_error.backward()
    optimizer.step()

    if iteration%1==0 :
      DessineFond()
      DessinePoints()

      plt.title(str(iteration))
      plt.show(block=False)
      plt.pause(0.5)  # pause avec duree en secondes

AI in Games/02 Ex/_02 Ex 7 Appr formes V2.py


from flask import Flask
import time
import os
import signal

app = Flask(__name__)

ready = True
alive = True

@app.route('/')
def hello():
    if (alive and ready):
        time.sleep(5)
        return "OK\n"
    elif (alive):
        time.sleep(15)
        return "OK\n"
    else:
        while True:
            x = 12345678 * 8765432

@app.route('/hang')
def hang():
    global alive
    alive = False
    return "Will hang\n"

@app.route('/kill')
def kill():
    os._exit(1)

@app.route('/setReady')
def ready():
    global ready
    ready = True
    return "Ready\n"

@app.route('/setNotReady')
def notready():
    global ready
    ready = False
    return "Not ready\n"

@app.route('/health')
def health():
    if (alive):
        return "OK\n"
    else:
        while True:
            x = 12345678 * 87654321

@app.route('/readiness')
def readiness():
    if (ready):
        return "OK\n"
    else:
        return "Not ready\n", 503

if __name__ == '__main__':
    app.run(host='0.0.0.0')


import argparse

def parseargs():  # handle user arguments
    parser = argparse.ArgumentParser(description='isoQTL main script.')
    parser.add_argument('--gene_info', required=True, help='File with gene info from simulation script. Required.')
    parser.add_argument('--wilks', help='Wilks-Bartlett results file.')
    parser.add_argument('--ftest', help='F-test results file.')
    parser.add_argument('--qtltools_gene', help='Path to qtltools gene-level results.')
    parser.add_argument('--qtltools_grpbest', help='Path to qtltools grpbest gene-level results.')
    parser.add_argument('--qtltools_grppca1', help='Path to qtltools grppca1 gene-level results.')
    parser.add_argument('--qtltools_grpmean', help='Path to qtltools grpmean gene-level results.')
    parser.add_argument('--fisher_perm', help='Fisher permutation results file.')
    parser.add_argument('--min_perm', help='Min permutation results file.')
    parser.add_argument('--cauchy_perm', help='Cauchy permutation results file.')
    parser.add_argument('--fisher_qtltools_iso', help='Fisher on QTLtools results file.')
    parser.add_argument('--min_qtltools_iso', help='Min-pval on QTLtools results file.')
    parser.add_argument('--cauchy_qtltools_iso', help='Cauchy on QTLtools results file.')
    parser.add_argument('--threshold', default=0.05, type=float,
                        help='P-value threshold for calling an eGene. Default: 0.05.')
    parser.add_argument('--use_perm', action='store_true',
                        help='Use permuted p-value instead of nominal p-value, if available.')
    args = parser.parse_args()
    return args

def read_gene_info(gene_info):
    egenes, null_genes = {}, {}
    with(open(gene_info, 'r')) as infile:
        infile.readline()  # skip header line
        for line in infile:
            splits = line.strip().split('\t')
            gene, h2cis = splits[0], float(splits[2])
            if h2cis == 0.0:
                null_genes[gene] = True
            else:
                egenes[gene] = True
    return egenes, null_genes

def read_isoqtl(isoqtl_res, use_perm, threshold):
    isoqtl_egenes = {}
    with(open(isoqtl_res, 'r')) as infile:
        infile.readline()  # skip header
        for line in infile:
            if use_perm:
                gene = line.strip().split(' ')[0]
                isoqtl_egenes[gene] = True
            else:
                splits = line.strip().split('\t')
                gene = splits[0]
                pval = float(splits[3])
                if pval < threshold:
                    isoqtl_egenes[gene] = True
    return isoqtl_egenes

def read_qtltools_gene(qtltools_gene_res, use_perm, threshold):
    qtltools_gene_egenes = {}
    with(open(qtltools_gene_res, 'r')) as infile:
        for line in infile:
            splits = line.strip().split(' ')
            gene = splits[0]
            if use_perm:
                qtltools_gene_egenes[gene] = True
                # pval = float(splits[-1])
            else:
                pval = float(splits[11])
                if pval < threshold:
                    qtltools_gene_egenes[gene] = True
    return qtltools_gene_egenes

def read_combined_pvals(combined_gene_res, use_perm, threshold):
    combined_egenes = {}
    with(open(combined_gene_res, 'r')) as infile:
        infile.readline()  # skip header
        for line in infile:
            if use_perm:
                gene = line.strip().split(' ')[0]
                combined_egenes[gene] = True
            else:
                splits = line.strip().split('\t')
                gene = splits[0]
                pval = float(splits[1])
                if pval < threshold:
                    combined_egenes[gene] = True
    return combined_egenes

def calc_tp_fp_tn_fn(true_egenes, null_genes, pred_egenes):
    tp, fp, tn, fn = 0.0, 0.0, 0.0, 0.0
    for gene in pred_egenes:
        if gene in true_egenes:
            tp += 1.0
        else:
            fp += 1.0
    fn = len(true_egenes) - tp
    tn = len(null_genes) - fp
    return tp, fp, tn, fn

def calc_metrics_and_print(method_name, tp, fp, tn, fn):
    if tp + fp == 0.0:
        precision = 'N/A'
    else:
        precision = tp / (tp + fp)
    if tp + fn == 0.0:
        recall = 'N/A'
    else:
        recall = tp / (tp + fn)
    if precision == 'N/A' or recall == 'N/A' or precision + recall == 0.0:
        f1_score = 'N/A'
    else:
        f1_score = 2 * precision * recall / (precision + recall)

    print('Results for ' + str(method_name) + ':\n')
    print('Precision: ' + str(precision))
    print('Recall: ' + str(recall))
    print('F1 Score: ' + str(f1_score) + '\n')
    str_vals = ', '.join([str(i) for i in [tp, fp, tn, fn]])
    print('TP FP TN FN: ' + str_vals + '\n')

if __name__ == '__main__':
    args = parseargs()
    true_egenes, null_genes = read_gene_info(args.gene_info)

    if args.wilks is not None:
        wilks_egenes = read_isoqtl(args.wilks, args.use_perm, args.threshold)
        tp, fp, tn, fn = calc_tp_fp_tn_fn(true_egenes, null_genes, wilks_egenes)
        calc_metrics_and_print('Wilks-Bartlett', tp, fp, tn, fn)

    if args.qtltools_gene is not None:
        qtltools_gene_egenes = read_qtltools_gene(args.qtltools_gene, args.use_perm, args.threshold)
        tp, fp, tn, fn = calc_tp_fp_tn_fn(true_egenes, null_genes, qtltools_gene_egenes)
        calc_metrics_and_print('QTLtools_gene', tp, fp, tn, fn)

    if args.qtltools_grpbest is not None:
        qtltools_grpbest_egenes = read_qtltools_gene(args.qtltools_grpbest, args.use_perm, args.threshold)
        tp, fp, tn, fn = calc_tp_fp_tn_fn(true_egenes, null_genes, qtltools_grpbest_egenes)
        calc_metrics_and_print('QTLtools_grpbest', tp, fp, tn, fn)

    if args.qtltools_grppca1 is not None:
        qtltools_grppca1_egenes = read_qtltools_gene(args.qtltools_grppca1, args.use_perm, args.threshold)
        tp, fp, tn, fn = calc_tp_fp_tn_fn(true_egenes, null_genes, qtltools_grppca1_egenes)
        calc_metrics_and_print('QTLtools_grppca1', tp, fp, tn, fn)

    if args.qtltools_grpmean is not None:
        qtltools_grpmean_egenes = read_qtltools_gene(args.qtltools_grpmean, args.use_perm, args.threshold)
        tp, fp, tn, fn = calc_tp_fp_tn_fn(true_egenes, null_genes, qtltools_grpmean_egenes)
        calc_metrics_and_print('QTLtools_grpmean', tp, fp, tn, fn)

    if args.ftest is not None:
        ftest_egenes = read_isoqtl(args.ftest, args.use_perm, args.threshold)
        tp, fp, tn, fn = calc_tp_fp_tn_fn(true_egenes, null_genes, ftest_egenes)
        calc_metrics_and_print('F-test', tp, fp, tn, fn)

    if args.fisher_perm is not None:
        fisher_perm_egenes = read_isoqtl(args.fisher_perm, args.use_perm, args.threshold)
        tp, fp, tn, fn = calc_tp_fp_tn_fn(true_egenes, null_genes, fisher_perm_egenes)
        calc_metrics_and_print('fisher_perm', tp, fp, tn, fn)

    if args.min_perm is not None:
        min_perm_egenes = read_isoqtl(args.min_perm, args.use_perm, args.threshold)
        tp, fp, tn, fn = calc_tp_fp_tn_fn(true_egenes, null_genes, min_perm_egenes)
        calc_metrics_and_print('min_perm', tp, fp, tn, fn)

    if args.cauchy_perm is not None:
        cauchy_perm_egenes = read_isoqtl(args.cauchy_perm, args.use_perm, args.threshold)
        tp, fp, tn, fn = calc_tp_fp_tn_fn(true_egenes, null_genes, cauchy_perm_egenes)
        calc_metrics_and_print('cauchy_perm', tp, fp, tn, fn)

    if args.fisher_qtltools_iso is not None:
        fisher_qtltools_iso_egenes = read_combined_pvals(args.fisher_qtltools_iso, args.use_perm, args.threshold)
        tp, fp, tn, fn = calc_tp_fp_tn_fn(true_egenes, null_genes, fisher_qtltools_iso_egenes)
        calc_metrics_and_print('fisher_qtltools_iso', tp, fp, tn, fn)

    if args.min_qtltools_iso is not None:
        min_qtltools_iso_egenes = read_combined_pvals(args.min_qtltools_iso, args.use_perm, args.threshold)
        tp, fp, tn, fn = calc_tp_fp_tn_fn(true_egenes, null_genes, min_qtltools_iso_egenes)
        calc_metrics_and_print('min_qtltools_iso', tp, fp, tn, fn)

    if args.cauchy_qtltools_iso is not None:
        cauchy_qtltools_iso_egenes = read_combined_pvals(args.cauchy_qtltools_iso, args.use_perm, args.threshold)
        tp, fp, tn, fn = calc_tp_fp_tn_fn(true_egenes, null_genes, cauchy_qtltools_iso_egenes)
        calc_metrics_and_print('cauchy_qtltools_iso', tp, fp, tn, fn)


from RPGFiles.lookup import ITEM
from text_formatting import idchat, bold, smartCapitalize
from math import ceil
from os.path import isfile

class Item:

    def __init__(self, name):
        ITEM[name] = self
        self.name = name
        self.typeof = ''
        self.is2handed = False
        self.isshield = False
        self.isarmor = False
        self.ischarm = False
        self.isconsumable = False
        self.energyuse = 400
        self.numofuses = 1
        self.curelife = 0
        self.curefocus = 0
        self.curemana = 0
        self.curestatus = -1     # -1 = no cure, 0 =  cure all, 1 = cure poison, 2 = cure toxic, 3 = cure blind, 4 = cure panic
        self.maxstacksize = 1
        self.statusinflict = 0   # -1 = random status, 0 = no status, 1+ look at status list
        self.statuschance = 0
        self.price = 0
        self.battleitem = False
        self.worlditem = False
        self.description = ''
        self.usedtext = ''
        self.damagereceivetext = 'Damage Recieve text missing {player} {damage}'
        self.damagedealtext = 'Damage Deal text missing {player} {damage}'
        self.critreceivetext = 'Crit Recieve text missing {player} {damage}'
        self.critdealtext = 'Crit Deal text missing {player} {damage}'
        self.nodamagedealtext = 'No Damage Deal Text Missing'
        self.nodamagereceivetext = 'No Damage Recieve Text Missing'
        self.buff = {'maxlife': 0,
                     'maxmana': 0,
                     'maxfocus': 0,
                     'power': 0,
                     'finesse': 0,
                     'will': 0,
                     'guard': 0,
                     'magic': 0,
                     'speed': 0,
                     'luck': 0,
                     'critchance': 0,
                     }
        self.buffelementalatk = {'Fire': 0,
                                 'Elec': 0,
                                 'Water': 0,
                                 'Earth': 0,
                                 'Psyche': 0,
                                 }
        self.buffelementaldef = {'Fire': 0,
                                 'Elec': 0,
                                 'Water': 0,
                                 'Earth': 0,
                                 'Psyche': 0,
                                 }

    async def useItem(self, player):
        for item in player.inventory:
            if item.name == self.name:
                player.inventory.remove(item)
                break
        await idchat(player.id, ("You use %s!" % (bold(self.name))))
        await idchat(player.id, ("%s!" % (self.usedtext)))
        player.life = min(player.life + ceil(player.get('maxlife') * (self.curelife / 100)), player.get('maxlife'))
        player.focus = min(player.focus + ceil(player.get('maxfocus') * (self.curefocus / 100)), player.get('maxfocus'))
        player.mana = min(player.mana + ceil(player.get('maxmana') * (self.curemana / 100)), player.get('maxmana'))

    async def useItemBattle(self, enc, player, target, players, escape):
        if self.battleitem is False:
            await idchat(self.id, "You can't use %s in this way." % (bold(self.name)))
        else:
            for p in players:
                if p.name == player.name:
                    await idchat(player.id, ("You use %s!" % (bold(self.name))))
                    await idchat(player.id, "%s" % (self.usedtext))
                else:
                    await idchat(player.id, "%s used %s!" % (player.name, bold(self.name)))
            player.energy -= self.energyuse
            player.life = min(player.life + ceil(player.get('maxlife') * (self.curelife / 100)), player.get('maxlife'))
            player.focus = min(player.focus + ceil(player.get('maxfocus') * (self.curefocus / 100)), player.get('maxfocus'))
            player.mana = min(player.mana + ceil(player.get('maxmana') * (self.curemana / 100)), player.get('maxmana'))
            if self.curestatus == 0:
                await player.clearStatusEffects()
            elif self.curestatus != -1:
                i = 0
                for status in player.status:
                    i += 1
                    if i == self.curestatus:
                        player.status[status] = False
            for stat in player.statchange:
                player.statchange[stat] += self.buff[stat]
            await enc.evaluateStatus(target, self.statusinflict, self.statuschance)
            player.inventory.remove(self)
            if self.name == 'Flask of Shadows':
                await idchat(self.id, "You escape!")
                player.runaway += 1
                player.lastmessage = ''
                return True

    def getImage(self):
        image = 'RPGFiles/Art/Icons/Item/%s.png' % smartCapitalize(self.name)
        if isfile(image):
            return image
        else:
            return 'RPGFiles/Art/Icons/NOPIC.png'

def checkItems():
    items = []
    for item in ITEM:
        image = 'RPGFiles/Art/Icons/Item/%s.png' % smartCapitalize(item)
        if not isfile(image):
            items.append(item)
    return items


from gevent import monkey; monkey.patch_all()
import gevent
import copy
import contextlib
import time
from .helpers import cast_to_args, cast_to_kwargs, format_time
from .log_config import LOG_DICT, TASK_DICT
import traceback

class TaskNotDefined(Exception):
    def __init__(self, task_name):
        super().__init__("Task {}: execution task not defined!!".format(task_name))

class TaskError(Exception):
    def __init__(self, task_name):
        super().__init__("Task {} failed when executing!!".format(task_name))

class TaskNotRunError(Exception):
    def __init__(self, task_name):
        super().__init__("Task {} run method must be called before running "
                         "the check_result method!!".format(task_name))

class TaskAlreadyRunException(Exception):
    def __init__(self, task_name):
        super().__init__("Task {} has already run. If you want to run the task twice,"
                         "create another one with the same characteristics!!"
                         .format(task_name))

class ResultFunctionError(Exception):
    def __init__(self, task_name):
        super().__init__("Result function for task {} failed"
                         " when executing!!".format(task_name))

class RunFailException(Exception):
    def __init__(self, task_name):
        super().__init__("The task {} result was unexpected!!".format(task_name))

@contextlib.contextmanager
def task_trace(log_dict, task_name):
    # What would be best than creating non idempotent functions in order to
    # build a library aimed to testing stateful systems
    t = time.time()
    task_dict = copy.deepcopy(TASK_DICT)
    log_dict["tasks"][task_name] = task_dict
    task_dict["status"]["task"] = "SUCCESS"
    try:
        yield
    except Exception as e:
        task_dict["status"]["task"] = "ERROR"
        raise e
    finally:
        task_dict["execution_time"] = format_time(t)

@contextlib.contextmanager
def verification_trace(log_dict, task_name):
    t = time.time()
    # I have the dict already saved in the log dictionary
    task_dict = log_dict["tasks"][task_name]
    task_dict["status"]["verification"] = "SUCCESS"
    try:
        yield
    except RunFailException as e:
        task_dict["status"]["verification"] = "FAILED"
        raise e
    except Exception as e:
        task_dict["status"]["verification"] = "ERROR"
        raise e
    finally:
        task_dict["verification_time"] = format_time(t)

@contextlib.contextmanager
def flow_trace(log_dict):
    t = time.time()
    log_dict["status"] = "SUCCESS"
    try:
        yield
    except RunFailException as e:
        log_dict["status"] = "FAILED"
    except Exception:
        log_dict["status"] = "ERROR"
        traceback.print_exc()
    finally:
        log_dict["total_elapsed_time"] = format_time(t)

class TaskResult(object):
    # This class is just a place holder to be used when specifying the
    # check_result functions for a Task class. If you want to use the task result
    # as a variable input, you can set this place holder
    pass

class Task(object):

    def __init__(self, name, task_function=None, task_args=None, task_kwargs=None,
                 result_function=None, result_args=None, result_kwargs=None):
        """
        Define a task to be executed. You must define a task_function when
        instantiating or using the add_execution_task function. Further,
        a checking function can be defined to be executed after the task has finished
        :param name: <str> The task name
        :param task_function: <function> The task execution main function
        :param task_args: <list> If your task uses positional arguments, pass them
            here as a list
        :param task_kwargs: <dict> If your task uses keyword arguments, pass them
            here as a dictionary
        :param result_function: <function> The result function. You can set a function
            to be executed after the task runs. You can use this function to check
            if the task did what it was supposed to do. Must return a boolean value
        :param result_args: <dict> If your result_function uses positional arguments,
            pass them here as a dictionary
        :param result_kwargs: <dict> If your result_function uses keyword arguments,
            pass them here as a dictionary
        """
        self.name = name

        self.__task_function = task_function
        self.__task_args = cast_to_args(task_args)
        self.__task_kwargs = cast_to_kwargs(task_kwargs)

        self.__result_function = result_function
        self.__result_args = cast_to_args(result_args)
        self.__result_kwargs = cast_to_kwargs(result_kwargs)

        self.__result = None
        self.__task_run = None

    @property
    def result(self):
        if self.__result is None:
            return TaskResult()
        else:
            return self.__result

    @result.setter
    def result(self, function_result):
        self.__result = function_result

    def run(self):
        """
        Run the main task
        :return:
        """
        if not self.__task_function:
            raise TaskNotDefined(self.name)
        if self.__task_run:
            raise TaskAlreadyRunException(self.name)
        try:
            self.__result = self.__task_function(*self.__task_args,
                                                 **self.__task_kwargs)
            self.__task_run = True
        except Exception:
            raise TaskError(self.name)

    def check_result(self):
        """
        If a verification result function was given. Execute it and success
        if this one return a positive boolean value
        :return:
        """
        if not self.__result_function:
            # If there is not a defined a result function. It is assumed the task
            # has completed successfully
            return True
        if self.__result is None:
            # If there isn't a result, the task was not run, and the result verification
            # cannot be called yet
            raise TaskNotRunError(self.name)
        try:
            mutated_args = [self.__result if isinstance(a, TaskResult) else a
                            for a in self.__result_args]
            fail_or_success = self.__result_function(*mutated_args,
                                                     **self.__result_kwargs)
        except Exception:
            raise ResultFunctionError(self.name)

        if not isinstance(fail_or_success, bool):
            raise TypeError("The expected Type for the task {} result function"
                            " must be boolean".format(self.name))
        return fail_or_success

    def add_execution_task(self, task_function, args=None, kwargs=None):
        """
        Add a execution task
        :param task_function: <function>
        :param args: The positional arguments of the function
        :param kwargs: The keyword arguments of the function
        :return:
        """
        self.__task_function = task_function
        self.__task_args = cast_to_args(args)
        self.__task_kwargs = cast_to_kwargs(kwargs)

    def add_result_function(self, result_function, args=None, kwargs=None):
        """
        Add a result function to be exectude after the task. The return value
        must be a boolean
        :param result_function: <function>
        :param args: The positional arguments of the function
        :param kwargs: The keyword arguments of the function
        :return: <bool>
        """
        self.__result_function = result_function
        self.__result_args = cast_to_args(args)
        self.__result_kwargs = cast_to_kwargs(kwargs)

    def copy(self):
        return Task(
            self.name, task_function=self.__task_function, task_args=self.__task_args,
            task_kwargs=self.__task_kwargs, result_function=self.__result_function,
            result_args=self.__result_args, result_kwargs=self.__result_kwargs
        )

class FlowPath(object):

    def __init__(self, task_path):
        """
        This is the flow the tasks will follow. A logging will be collected when
        calling the run function
        :param task_path: <list>.<Task>
        """
        self.path = cast_to_args(task_path)

        self.log = copy.deepcopy(LOG_DICT)

    def add_task(self, task):
        """
        Add a task to the task list. The task will be appended at the bottom of the
        execution path
        :param task: <Task>
        :return:
        """
        self.path.append(task)

    def __check_result(self, task):
        try:
            fail_or_success = task.check_result()
        except Exception as e:
            raise e

        if not isinstance(fail_or_success, bool):
            raise TypeError("The expected Type for the task {} result function"
                            " must be boolean".format(task.name))
        return fail_or_success

    def __run_task(self, task):
        # First, run the task
        with task_trace(self.log, task.name):
            try:
                task.run()
            except Exception:
                raise TaskError(task.name)
        # Then, check the result function returns a True value
        with verification_trace(self.log, task.name):
            expected_result = self.__check_result(task)
            if expected_result:
                return
            else:
                raise RunFailException(task.name)

    def __compute_log_aggregations(self):
        total_exec_time = total_verif_time = 0
        for t, values in self.log.get("tasks").items():
            task_exec_time = values.get("execution_time")
            if task_exec_time:
                total_exec_time += task_exec_time
            task_verif_time = values.get("task_verif_time")
            if task_verif_time:
                total_verif_time += task_verif_time
        self.log["total_execution_time"] = total_exec_time
        self.log["total_verification_time"] = total_verif_time

    def run(self):
        """
        Run the Flow Path following the order given in the path list
        :return: <dict> Returns the execution log
        """
        self.log["path"] = [t.name for t in self.path]
        with flow_trace(self.log):
            for task in self.path:
                self.__run_task(task)
                self.log["executed_path"].append(task.name)

        self.__compute_log_aggregations()
        return self.log

    def copy(self):
        tasks_copy = [t.copy() for t in self.path]
        return FlowPath(tasks_copy)

class ConcurrentFlows(object):
    """
    If you want to run concurrent flows, you can instantiate this class
    with a dictionary of FlowPath instances
    """
    def __init__(self, flow_dict=None):
        self.flow_list = {} if flow_dict is None else flow_dict

        self.__logs = None

    @property
    def logs(self):
        return self.__logs

    @logs.setter
    def logs(self, log_list):
        self.__logs = {ld[0]: ld[1].value for ld in log_list}

    def add_flow(self, flow_id, flow):
        """
        Add a flow path, or a list of flow paths
        :param flow_id: <str>. A flow path identifier
        :param flow: <list>.<FlowPath> or <FlowPath>
        :return:
        """
        if isinstance(flow, list):
            self.flow_list.extend(flow)
        else:
            self.flow_list.append(flow)

    def __run_flows_at_once(self):
        """
        Run all the flow path at the same time.
        :return:
        """
        id_list, path_jobs = [], []
        for fid, flow_path in self.flow_list.items():
            id_list.append(fid)
            path_jobs.append(gevent.spawn(flow_path.run))
        gevent.joinall(path_jobs)
        self.logs = zip(id_list, path_jobs)

    def run(self, options=None):
        """
        Run all the specified flows according to the passed options. The default
        running behaviour is to run all the flow Paths at once.
        :param options:
        :return:
        """
        if not options:
            self.__run_flows_at_once()
        return self.logs


# -*- coding: utf-8 -*-
from . import db
from app.exceptions import ValidationError
from datetime import datetime

class Particle(db.Model):
    __tablename__ = 'particles'
    id = db.Column(db.Integer, primary_key=True)
    author_id = db.Column(db.BigInteger)
    googleUserImage = db.Column(db.Text)
    context = db.Column(db.Text, nullable=False)
    parsed_context = db.Column(db.Text)
    x = db.Column(db.Float, nullable=False)
    y = db.Column(db.Float, nullable=False)
    anonymous = db.Column(db.Boolean)
    created_at = db.Column(db.DateTime, index=True,
                           default=datetime.utcnow)

    comments = db.relationship('Comment', backref='particle', lazy='dynamic')
    likes = db.relationship('Like', backref='particle', lazy='dynamic')

    def __init__(self, author_id, googleUserImage, context, parsed_context, x, y, anonymous=False):
        self.author_id = author_id
        self.googleUserImage = googleUserImage
        self.context = context
        self.parsed_context = parsed_context
        self.x = x
        self.y = y
        self.anonymous = anonymous

    def __repr__(self):
        return '<Particle [%r](%r):%r>' % (self.created_at, self.author_id, self.context)

    def to_json(self):  # json 출력 루틴
        json_particle = {
            'id': self.id,
            'author': self.author_id,
            'googleUserImage': self.googleUserImage,
            'context': self.context,
            'created_at': self.created_at,
            'x': self.x,
            'y': self.y,
            'anonymous': self.anonymous,
            'likes': [like.user_id for like in self.likes],
            'likes_count': self.likes.count(),
        }
        return json_particle

    @staticmethod
    def from_json(json_particle):  # json 입력 루틴
        author_id = json_particle.get('author_id')
        googleUserImage = json_particle.get('googleUserImage')
        context = json_particle.get('context')
        x = json_particle.get('x')
        y = json_particle.get('y')
        anonymous = json_particle.get('anonymous')

        if author_id is None or author_id == '':
            raise ValidationError('particle does not have information')
        parsed_context = removeEscapeChar(context).lower()

        particle = Particle(author_id=author_id, googleUserImage=googleUserImage, context=context,
                            parsed_context=parsed_context, x=x, y=y, anonymous=anonymous)
        return particle

class Like(db.Model):
    __tablename__ = 'likes'
    id = db.Column(db.Integer, primary_key=True)
    particle_id = db.Column(db.Integer, db.ForeignKey(
        'particles.id'), nullable=False)
    user_id = db.Column(db.BigInteger, nullable=False)

    def __init__(self, user_id):
        self.user_id = user_id

    @staticmethod
    def from_json(json_like):
        user_id = json_like.get('userID')
        if user_id is None or user_id == '':
            raise ValidationError('Like does not have information')
        like = Like(user_id=user_id)
        return like

class Comment(db.Model):
    __tablename__ = 'comments'
    id = db.Column(db.Integer, primary_key=True)
    context = db.Column(db.Text)
    parsed_context = db.Column(db.Text)
    created_at = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    author_id = db.Column(db.Integer)
    googleUserImage = db.Column(db.Text)
    particle_id = db.Column(db.Integer, db.ForeignKey(
        'particles.id'), nullable=False)
    parent_id = db.Column(db.Integer, db.ForeignKey('comments.id'))
    comments = db.relationship('Comment', lazy='dynamic')

    def __init__(self, context, parsed_context, particle_id=None):
        self.context = context
        self.parsed_context = parsed_context
        self.particle_id = particle_id

    def __repr__(self):
        return '<Comment #%r[%r]: %r>' % (self.particle_id, self.author_id, self.context)

    @staticmethod
    def from_json(json_comment):  # json 입력 루틴
        author_id = json_comment.get('author_id')
        googleUserImage = json_comment.get('googleUserImage')
        context = json_comment.get('context')

        if (author_id is None or author_id == '') and \
                (context is None or context == ''):
                    raise ValidationError('comment does not have a context')
        parsed_context = removeEscapeChar(context).lower()

        return Comment(author_id=author_id, googleUserImage=googleUserImage, context=context, parsed_context=parsed_context)

    def to_json(self):  # json 출력 루틴
        json_comment = {
            'id': self.id,
            'author': self.author_id,
            'googleUserImage': self.googleUserImage,
            'context': self.context,
            'created_at': self.created_at,
            'particle_id': self.particle_id,
            'parent_id': self.parent_id,
            'count_reply': self.comments.count()
        }
        return json_comment

def removeEscapeChar(context):
    import re
    str = re.sub("(<([^>]+)>)", "", context)
    str = str.replace(' ', "").replace('<', "<").replace('>', ">")\
        .replace('&', "&").replace('"', '"')
    return str


COUNTRIES = {
    "RU": "Россия",
    "BR": "Беларусь",
    "CN": "Китай",
}

CITIES_RU = {
    "SNTPTRSBRG": "Санкт-Петербург",
    "MSCW": "Москва",
    "NVSBRSK": "Новосибирск",
    "KZN": "Казань",
    "MRMNSK": "Мурманск",
}

CITIES_BR = {
    "MNSK": "Минск",
    "GML": "Гомель",
    "BRST": "Брест",
}

CITIES_CH = {
    "GNDZH": "Гуньджоу",
    "PKN": "Пекин",
    "SHNGH": "Шанхай",
}


# #!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn.datasets as data

import hdbscan

sns.set_context('poster')
sns.set_style('white')
sns.set_color_codes()
plot_kwds = {'alpha' : 0.5, 's' : 80, 'linewidths':0}

moons, _ = data.make_moons(n_samples=50, noise=0.05)
# blobs, _ = data.make_blobs(n_samples=50, centers=[(-0.75,2.25), (1.0, 2.0)], cluster_std=0.25)
test_data = np.vstack([moons])#, blobs])

# print(test_data.T)

plt.scatter(test_data.T[0], test_data.T[1], color='b', **plot_kwds)
plt.show()

clusterer = hdbscan.HDBSCAN(min_cluster_size=5, gen_min_span_tree=True)
clusterer.fit(test_data)
#
# print(clusterer.labels_)
# print(clusterer.probabilities_)
# print(clusterer.cluster_persistence_)

plt.figure()
clusterer.minimum_spanning_tree_.plot(edge_cmap='viridis',
                                      edge_alpha=0.6,
                                      node_size=80,
                                      edge_linewidth=2)

plt.figure()
clusterer.single_linkage_tree_.plot(cmap='viridis', colorbar=True)

plt.figure()
clusterer.condensed_tree_.plot(select_clusters=True, selection_palette=sns.color_palette())
# plt.show()


# to get individual jpegs for bruker videos on sherlock

## this should make new directories and run ffmpeg

import os
import subprocess
import sys

#variables to change
#date = '20210716'    #date folder
dates = ['20210716', '20210714', '20210709', '20210702', '20210607']

def main():
  #main path
  for date in dates:
    video_path = '/oak/stanford/groups/trc/data/Ashley2/bruker videos/' + str(date) +'/'   #should end in /

    #find fly folders
    for fly_dir in os.listdir(video_path):
      if 'fly' in fly_dir:
          fly_video_path = os.path.join(video_path, fly_dir)
          video_name = os.listdir(fly_video_path)
          print(fly_video_path)
          print(video_name)
          if len(video_name) == 0 or len(video_name) > 1:
              raise Exception("ERROR: THERE ARE TOO MANY ITEMS IN FOLDER or TOO FEW", video_name)
              break
          if '.avi' not in video_name[0]:
              raise Exception ("ERROR: this file is not avi", video_name[0])
              break
          fly_video = os.path.join(fly_video_path, str(video_name[0]))

          # make jpeg path
          jpeg_path = os.path.join(video_path, 'analysis/', fly_dir)
          make_dirs(jpeg_path)
          print(jpeg_path)

          ## run ffmpeg

          final_jpeg_path_v1 = os.path.join(jpeg_path, 'V01frame_%07d.jpg')
          cmd = f'ffmpeg -i REPLACEPATH REPLACEPATHJPEG -hide_banner -loglevel error'  #have to do it this way because I have spaces in the filepath
          split_version = cmd.split(" ")
          split_version[2] = fly_video #replaces REPLACEPATH
          split_version[3] = final_jpeg_path_v1 #replaces REPLACEPATHJPEG
          subprocess.call(split_version)

          print(f"{fly_dir} DONE from {date}")

## functions ##
def make_dirs (file_path):
    """this will check if a filepath exists and if it doesn't it will create one"""
    if os.path.exists(file_path):
        print('jpeg directory already exists', file_path)
    else:
        os.makedirs(file_path)
        print('file path created: ', file_path)

if __name__=='__main__':
  main()


import numpy as np
import os

def ArrayToBinary(theArray, binaryFilePath, attributeName='value', yOffSet=0):
    """
    Use Numpy tricks to write a numpy array in binary format with indices

    input: Numpy 2D array
    output: Numpy 2D array in binary format
    """
    import numpy as np
    # print("Writing out file: %s" % (binaryFilePath))
    col, row = theArray.shape

    with open(binaryFilePath, 'w') as fileout:
        #Oneliner that creates the column index. Pull out [y for y in range(col)] to see how it works
        column_index = np.array(np.repeat([y for y in np.arange(0+yOffSet, col+yOffSet) ], row), dtype=np.dtype('int64'))
        #print(column_index)
        #Oneliner that creates the row index. Pull out the nested loop first: [x for x in range(row)]
        #Then pull the full list comprehension: [[x for x in range(row)] for i in range(col)]
        row_index = np.array(np.concatenate([[x for x in range(row)] for i in range(col)]), dtype=np.dtype('int64'))

        #Oneliner for writing out the file
        #Add this to make it a csv tofile(binaryFilePath), "," and modify the open statement to 'w'
        np.core.records.fromarrays([column_index, row_index, theArray.ravel()], dtype=[('y','int64'),('x','int64'),(attributeName,theArray.dtype)]).ravel().tofile(binaryFilePath, ",")

    del column_index, row_index, theArray

row = range(100)
grid = [row for r in row]
array = np.array(grid)
print(array.shape)
binaryPartitionPath = r"c:\work\test.csv"
if os.path.exists(binaryPartitionPath): os.remove(binaryPartitionPath)

binaryFiles = []
for offset, a in enumerate(np.array_split(array, 10)):
    print(offset, offset * a.shape[0])
    binaryPartitionPath = r"c:\work\test_%s.csv" % (offset)
    if os.path.exists(binaryPartitionPath): os.remove(binaryPartitionPath)
    ArrayToBinary(a, binaryPartitionPath, 'mask', offset * a.shape[0])
    #break
    binaryFiles.append(binaryPartitionPath)

with open(r"c:\work\test.csv", "w") as fo:
 for bFile in binaryFiles:
      with open(bFile,'r') as fi: fo.write(fi.read())


from xlsxwriter.workbook import Workbook
import sqlite3 as sql
import Config

con = sql.connect(Config.Database["databaseName"])

# This function creates all necessary tables needed in the SQLite database.
def createTables():
    cur = con.cursor()
    cur.executescript(Config.Database["createTablesQuery"])
    print("Created all tables!")
    con.commit()

# This function drops all non-system tables in the SQLite database. BE CAREFUL USING THIS FUNCTION!!!

def dropAllTables():
    cur = con.cursor()
    cur.execute(
        "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite%'")
    for row in cur.fetchall():
        query = "DROP TABLE IF EXISTS " + row[0]
        print(query)
        cur.execute(query)
        con.commit()

# USE AT YOUR OWN RISK, DELETES ALL ROWS FROM ALL TABLES

def cleanTables():
    cur = con.cursor()
    cur.execute("SELECT name FROM sqlite_master WHERE type='table'")
    for row in cur:
        con.cursor().execute("DELETE FROM " + row[0])
        con.commit()

def tableExists(tableName):
    cur = con.cursor()
    cur.execute(
        "SELECT COUNT(1) FROM sqlite_master WHERE type='table' AND name=?;", (tableName,))
    if(cur.fetchone()[0] < 1):
        return False
    return True

def selectAllFromTable(tableName):
    cur = con.cursor()
    cur.execute("SELECT * FROM " + tableName)
    return cur

def selectAllFromTableHTML(query):
    return queryToHTML(query, None)

def queryToHTML(query, tupleValues):
    cur = executeQuery(query, tupleValues)
    resultHTML = Config.HTML["header"]
    resultHTML += Config.HTML["selectTable"]
    columnNames = []
    for column in cur.description:
        resultHTML += "" + column[0] + " 	 \n"
    resultHTML += "

 \n"
    for row in cur:
        resultHTML += "
 \n"
        for column in row:
            resultHTML += "" + str(column) + " 	 \n"
        resultHTML += "
 \n"
    resultHTML += "

"
    return resultHTML

def executeQuery(query, tupleValues):
    cur = con.cursor()
    if tupleValues is None:
        cur.execute(query)
    else:
        cur.execute(query, tupleValues)
    con.commit()
    return cur

def getHTMLForm(tableName, methodName):
    cur = con.cursor()
    query = """PRAGMA table_info('{0}')""".format(tableName,)
    cur.execute(query)
    formHTML = Config.HTML["header"]
    formHTML += Config.HTML["insertForm"].format(
        str(tableName) + "/" + str(methodName))
    for row in cur:
        formHTML += """{0}({1}):<input type="text" class="form-control" id="{0}" name="{0}">""".format(
            str(row[1]), str(row[2]))
    formHTML += """<input type="submit" name="form" value="Submit"></form>
"""
    return formHTML

"""
This function takes in a filepath and select SQL query and outputs the result (with columns) to the filepath specified.
"""

def generateExcelFile(path, selectQuery):
    cur = con.cursor()
    cur.execute(selectQuery)
    workbook = Workbook(path)
    sheet = workbook.add_worksheet()
    columnNames = []
    for column in cur.description:
        columnNames.append(column[0])
    listToWrite = cur.fetchall()
    listToWrite.insert(0, tuple(columnNames))
    print(listToWrite)
    for r, row in enumerate(listToWrite):
        for c, col in enumerate(row):
            sheet.write(r, c, col)
    workbook.close()

"""
This is the main database script used to communicate with the SQLite database.
This contains all necessary methods to create the schema for this database.
"""

def main():
    createTables()
    # dropAllTables()
    #generateExcelFile("test.xlsx","SELECT * FROM accounts")

if __name__ == "__main__":
    main()


from picamera import PiCamera
from picamera.array import PiRGBArray
from threading import Thread
from imutils.video import FPS
from imutils import contours
from skimage import measure
from udpsocket import ThreadedUDPSocket
import imutils
import cv2
import time
import numpy as np
import argparse
import math
import pickle
import sys

resolution = (640, 480) # length x height
framerate = 60
exposure = 1000 # time in milliseconds. 10000 is normal exposure.
fps = FPS()

def main(args):
    try:
        stream = VideoStream().start()
        time.sleep(2)
        old_uid = 0
        uid = 0
        key = None
        result = ()

        while not all(result) or key != ord(' '):
            while uid == old_uid:
                frame, uid = stream.read()
            result = initialize(frame)
            old_uid = uid
            key = cv2.waitKey(1) & 0xFF

        cv2.destroyAllWindows()

        x_px_len, y_px_len, top_l = result

        with open('Init_Settings.txt', 'wb') as f:
            pickle.dump(result, f)
        sys.exit()

    except KeyboardInterrupt:
        cv2.destroyAllWindows()
        stream.stop()
        raise SystemExit(0)

class VideoStream:
    def __init__(self, resolution=resolution, framerate=framerate):
        # Initialize the camera
        self.camera = PiCamera()
        self.camera.resolution = resolution
        self.camera.framerate = framerate
        self.camera.shutter_speed = exposure
        self.rawCap = PiRGBArray(self.camera, size=resolution)
        self.stream = self.camera.capture_continuous(self.rawCap,
                                                     format='bgr',
                                                     use_video_port=True)
        # Initialize threading shared variables
        self.frame = None
        self.stopped = False
        self.mtx = np.loadtxt('ORANGE cameraMatrix.txt', delimiter=',', dtype=np.float32)
        self.dist = np.loadtxt('ORANGE cameraDistortion.txt', delimiter=',', dtype=np.float32)

    def start(self):
        # Starts a new thread to read frames from the video stream
        Thread(target=self.update, args=()).start()
        return self

    def update(self):
        # Keep looping infinitely until the thread is stopped
        for uid, frame in enumerate(self.stream):
            start = time.time()
            # Fetch a frame and clear the stream
            newcameramtx, roi = cv2.getOptimalNewCameraMatrix(self.mtx, self.dist, (640, 480), 1, (640, 480))
            # undistort
            mapx,mapy = cv2.initUndistortRectifyMap(self.mtx, self.dist, None, newcameramtx,(640, 480), 5)
            dst = cv2.remap(frame.array, mapx, mapy, cv2.INTER_LINEAR)
            # crop the image
            x,y,w,h = roi
            dst = dst[y:y+h, x:x+w]

            self.frame = (dst, uid)
            self.rawCap.truncate(0)

            # If the user stops the program, close the thread
            if self.stopped == True:
                self.stream.close()
                self.rawCap.close()
                self.camera.close()
                return self

    def read(self):
        # Return the frame most recently read
        return self.frame

    def stop(self):
        # Indicate that the thread should be stopped
        self.stopped = True
        return self

def initialize(frame):
    # Isolate Red data
    im_draw = frame

    _, _, red = cv2.split(frame)

    # convert grayscale image back to BGR format for concatenation
    red_formatted = cv2.cvtColor(red, cv2.COLOR_GRAY2BGR)
    demo_windows1 = np.concatenate((frame, red_formatted), axis=1)

    # threshold the image to reveal light regions in the
    # blurred image
    thresh = cv2.threshold(red, 170, 255, cv2.THRESH_BINARY)[1]
    blurred = cv2.GaussianBlur(thresh, (11, 11), 0)
    thresh = cv2.threshold(blurred, 70, 255, cv2.THRESH_BINARY)[1]

    blurred_formatted = cv2.cvtColor(blurred, cv2.COLOR_GRAY2BGR)
    thresh_formatted = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
    demo_windows2 = np.concatenate((blurred_formatted, thresh_formatted), axis=1)

    # Possibly unnessesary, pylons are very bright.
    # perform a series of erosions and dilations to remove
    # any small blobs of noise from the thresholded image
    #thresh = cv2.erode(thresh, None, iterations=1)
    #thresh = cv2.dilate(thresh, None, iterations=1)

    # perform a connected component analysis on the thresholded image
    labels = measure.label(thresh, connectivity=1, background=0, return_num=True)

    # Save points as individual values for maniplulation
    regions = measure.regionprops(labels[0], cache=True)

    if len(regions) != 2:
        x_axis_len = None
        y_axis_len = None
        top_l = None
        axii = frame
    else:
        top_l = regions[0].centroid
        bot_r = regions[1].centroid

        # Get axis lengths, row is x, col is y
        x_axis_len = bot_r[0] - top_l[0]
        y_axis_len = bot_r[1] - top_l[1]
        #print("X-axis length= {point}".format(point = x_axis_len))
        #print("Y-axis length= {point}".format(point = y_axis_len))

        # Draw axis. Only use for Demo
        axii = cv2.line(im_draw, (int(round(top_l[1])),int(round(top_l[0]))), (int(round(top_l[1])),int(round(top_l[0]+x_axis_len))), (0,255,255), 1)
        axii = cv2.line(axii, (int(round(top_l[1])),int(round(top_l[0]))), (int(round(top_l[1]+y_axis_len)),int(round(top_l[0]))), (0,255,255), 1)

    axii_formatted = cv2.resize(axii, (0, 0), None, 2, 2)
    demo_windows = np.concatenate((demo_windows1, demo_windows2), axis=0)
    demo_windows = np.concatenate((demo_windows, axii_formatted), axis=1)
    demo_windows = cv2.resize(demo_windows, (0, 0), None, 0.5, 0.5)
    cv2.imshow("Boundary", demo_windows)

    return (x_axis_len, y_axis_len, top_l)

if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument("-d", "--demo", type=bool, default=True)
    ap.add_argument("-s", "--scale", type=float, default=0.2753647864651542)
    args = ap.parse_args()
    main(args)


def timer(a, b, c, d):
    slow = ""
    fast = ""
    if c > a:
        slow = "%s:%s" % (c, d)
        fast = "%s:%s" % (a, b)
    elif a > c:
        slow = "%s:%s" % (a, b)
        fast = "%s:%s" % (c, d)
    elif a == c:
        if b > d:
            slow = "%s:%s" % (a, b)
            fast = "%s:%s" % (c, d)
        elif b < d:
            slow = "%s:%s" % (c, d)
            fast = "%s:%s" % (a, b)

    return slow, fast

hour1 = input("첫 번째 시간의 시를 입력하세요.")
minute1 = input("첫 번째 시간의 분을 입력하세요.")
hour2 = input("두 번째 시간의 시를 입력하세요.")
minute2 = input("두 번째 시간의 분을 입력하세요.")
slow, fast = timer(hour1, minute1, hour2, minute2)
print("빠른시간:%s" % fast)
print("느린시간:%s" % slow)


'''
6. Determine the bandwidth when forwarding MPI messages depending on the size of the data.
    1 Create a list object of one int type element
    2 Measure the size L of the object using the sys library
    3 host should send a message to the worker, then the worker should send it back
    4 repeat point (3) N=10 times
    5 measure the time T from sending the first message to receiving the last one at the host
    6 bandwidth is measured by the formula: R=(2*N*L) / T
    7 output the result to the console in the format "object_size (bytes): R (MB/s)
    8 increase the number of sheet elements by 1000
    9 repeat from point (2) until the number of elements is equal to 50 000 elements
'''

'''
6. Определите пропускную способность при пересылке MPI-сообщений в зависимости от размера данных.
    1 Создайте объект списка из одного элемента типа int
    2 Измерьте размер L объекта с помощью библиотеки sys
    3 хост должен отправить сообщение работнику, затем работник должен отправить его обратно
    4 повторите пункт (3) N=10 раз
    5 измерьте время T от отправки первого сообщения до получения последнего на хосте
    6 пропускная способность измеряется по формуле: R=(2*N*L) / T
    7 выведите результат на консоль в формате "object_size (байты): R (МБ/с)
    8 увеличьте количество элементов листа на 1000
    9 повторяйте с точки (2) до тех пор, пока количество элементов не станет равным 50 000 элементов
'''

import time
import sys
from mpi4py import MPI as mpi

def sendRecvMessage(N: int, listSize: int):
    comm = mpi.COMM_WORLD
    rank = comm.Get_rank()
    size = comm.Get_size()

    # 1 Create a list object of one int type element
    # 8 increase the number of sheet elements by 1000
    intList = [1] * listSize

    # 2 Measure the size L of the object using the sys library
    L = sys.getsizeof(intList) # sizeof list in bytes

    message = None

    # 3 host should send a message to the worker, then the worker should send it back
    # 4 repeat point (3) N=10 times
    for i in range(N):

        # start timer before first message sending
        if (i == 0 and rank == 0):
            t_start = time.perf_counter()

        if (rank == 1):
            # first of all to recieve message from root
            message = comm.recv(tag=i, source=0)

            # and then send this message to root
            comm.send(message, dest=0, tag=i+1)
        elif (rank == 0):
            # send message to all workers from host and each worker get this message
            comm.send(intList, dest=1, tag=i)

            # root shoult get message
            message = comm.recv(tag=i+1, source=1)

            # stop timer after last message receiving
            if (i == N - 1):
                t_stop = time.perf_counter()
                T = float(t_stop - t_start)

                # 5 measure the time T from sending the first message to receiving the last one at the host
                print("Time from first message sending to last message receiving: {:0.5f} sec ({:0.5f} mls)".format(T, T*1000))

                # 6 bandwidth is measured by the formula: R=(2*N*L) / T
                # 7 output the result to the console in the format "object_size (bytes): R (MB/s)
                R = (2 * N * L) / T
                print("object_size {} (bytes): R {:0.2f} (MB/s)".format(L, R))

def main():
    if (mpi.COMM_WORLD.Get_size() != 2):
        if (mpi.COMM_WORLD.rank == 0):
            print("Wrong number of threads!")
        return

    # 4 repeat point (3) N=10 times
    N = 10
    # 8 increase the number of sheet elements by 1000
    elementsCount = 1000
    # 9 repeat from point (2) until the number of elements is equal to 50 000 elements
    totalElementsCount = 50000

    iterCount = totalElementsCount // elementsCount + 1

    # for elements list with one element

    if (mpi.COMM_WORLD.rank == 0):
            print("With one element")
    sendRecvMessage(N, 1)
    if (mpi.COMM_WORLD.rank == 0):
            print()

    for i in range(1, iterCount):
        if (mpi.COMM_WORLD.rank == 0):
            print("Iter #{}".format(i))

        sendRecvMessage(N, elementsCount*i)

        if (mpi.COMM_WORLD.rank == 0):
            print()

if __name__ == "__main__":
    main()


import argparse
import os
import time

from panoramic_video_generator import PanoramicVideoGenerator

def main(file_path, num_images):
    file_name = os.path.basename(file_path)
    exp_no_ext = file_name.split('.')[0]
    os.system('mkdir dump')
    os.system(('mkdir ' + str(os.path.join('dump', '%s'))) % exp_no_ext)
    os.system(('ffmpeg -i ' + str(os.path.join('videos', '%s ')) + str(os.path.join('dump', '%s', '%s%%03d.jpg'))) % (
        file_path, exp_no_ext, exp_no_ext))

    s = time.time()
    panorama_generator = PanoramicVideoGenerator(os.path.join('dump', '%s') % exp_no_ext,
                                                 exp_no_ext, num_images)
    panorama_generator.align_images(translation_only='boat' in file_path)
    panorama_generator.generate_panoramic_images(9)
    print(' time for %s: %.1f' % (exp_no_ext, time.time() - s))

    panorama_generator.save_panoramas_to_video()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Generate panoramic images from a sequence of images.")
    parser.add_argument("--input_dir", type=str, required=True, default='boat.mp4', help="path to input images")
    parser.add_argument("--num_images", type=int, required=False, default=2100, help="number of images in the sequence")
    args = parser.parse_args()
    main(*vars(args).values())


import numpy as np
import pandas as pd
from sklearn import metrics
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv("phishing.csv")

df.head()

df.shape

df.info

df.columns

df = df.drop(['Index'], axis = 1)

X = df.drop('class', axis = 1)
y = df["class"]

from sklearn.model_selection import train_test_split

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2, random_state = 0)

# K-Nearest Neighbors Classifier model
from sklearn.neighbors import KNeighborsClassifier

# instantiate the model
knn = KNeighborsClassifier(n_neighbors=1)

# fit the model
knn.fit(X_train,y_train)

y_train_knn = knn.predict(X_train)
y_test_knn = knn.predict(X_test)

acc_train_knn = metrics.accuracy_score(y_train,y_train_knn)
acc_test_knn = metrics.accuracy_score(y_test,y_test_knn)
print("K-Nearest Neighbors : Accuracy on training Data: {:.3f}".format(acc_train_knn))
print("K-Nearest Neighbors : Accuracy on test Data: {:.3f}".format(acc_test_knn))
print()
precision_score_train_knn = metrics.precision_score(y_train,y_train_knn)
precision_score_test_knn = metrics.precision_score(y_test,y_test_knn)
print("K-Nearest Neighbors : precision on training Data: {:.3f}".format(precision_score_train_knn))
print("K-Nearest Neighbors : precision on test Data: {:.3f}".format(precision_score_test_knn))

recall_score_train_knn = metrics.recall_score(y_train,y_train_knn)
recall_score_test_knn = metrics.recall_score(y_test,y_test_knn)
print("K-Nearest Neighbors : Recall on training Data: {:.3f}".format(recall_score_train_knn))
print()


from tabulate import tabulate
import time, os

def printCards():
  infoKeys = [{"NAME": name, **values} for name, values in myBeasts.items()]
  print (tabulate(infoKeys, headers = 'keys', tablefmt='fancy_grid'))

myBeasts = {}
keys = ['TYPE', 'SPECIAL MOVE', 'STARTING HP', 'STARTING MP']

while True:
  os.system("clear")
  print ("ðŸ‘¾ PokÃ©Beast - The Non-Copyright Generic Beast Battle Game")
  print ()
  NAME = input ("\033[35mInput your beast\'s name > \033[36m")
  info = {}
  for key in keys:
    info[key] = input(f"\033[35mInput your beast\'s {key.lower()} > \033[36m")
  myBeasts[NAME] = info
  time.sleep(0.5)
  print ()
  printCards()
  time.sleep(0.5)
  print ()
  menu = input ("Again? (y/n) > ")
  if menu == "y":
    continue
  else:
    break


import sys

n = int(input())
min = sys.maxsize
max = -sys.maxsize

for x in range(n):
    num = int(input())

    if num < min:
        min = num

    if num > max:
        max = num

print(f'Max number: {max}')
print(f'Min number: {min}')


import logging
import json
import os
import threading
from time import sleep

from requests.auth import AuthBase

from .client import DockerRegistryClient

DOCKER_CONFIG_FILE = os.path.expanduser('~/.docker/config.json')

def _str_or_list(value):
    if not value:
        return []
    if isinstance(value, str):
        return [value]
    return list(value)

log = logging.getLogger(__name__)

class DockerRegistryQuery(object):
    def __init__(self, client):
        self._client = client
        self._cache = {}
        self._initialized = False
        self._refresh_interval = 300   # 5 minutes
        self.auto_refresh_started = False

    @property
    def client(self):
        return self._client

    def refresh(self):
        log.info("Initializing cache.")
        if self._initialized:
            log.info("Clearing.")
            self._cache.clear()
        repos = self._client.get_catalog().json()['repositories']
        log.info("Found %s repositories.", len(repos))
        for repo in repos:
            log.info("Checking respository '%s'.", repo)
            tags = self._client.get_tags(repo).json()['tags']
            self._cache[repo] = tags
        log.info("Cache init completed.")
        self._initialized = True
        self._start_auto_refresh()

    def _start_auto_refresh(self):
        if self.auto_refresh_started:
            return
        self.auto_refresh_started = True
        self.refresh_thread = threading.Thread(target=self._auto_refresh)
        self.refresh_thread.setDaemon(True)
        self.refresh_thread.start()

    def _auto_refresh(self):
        while True:
            sleep(self._refresh_interval)
            try:
                self.refresh()
            except:
                continue

    def get_repo_names(self):
        """
        Returns a sorted list of available repository names.

        :return: Repository name list.
        :rtype: list[str]
        """
        if not self._initialized:
            self.refresh()
        return self._cache.keys()

    def get_tag_names(self, repos=None):
        """
        Returns a sorted list of available tags, optionally filtered by a set of repository names.

        :param repos: Optional repository name or list names to limit the output.
        :type repos: str | list[str] | tuple[str] | NoneType
        :return: List with tuples of repositories and tags.
        :rtype: list[(str, str)]
        """
        if not self._initialized:
            self.refresh()
        if repos:
            repo_list = _str_or_list(repos)
            return {repo: self._cache[repo] for repo in repo_list}
        else:
            return self._cache

    @property
    def cache(self):
        """
        Returns the cache instance for queries.

        :rtype: Lookup cache for repositories, tags, and digests.
        :rtype: docker_registry_util.cache.ImageDigestCache
        """
        return self._cache

    @property
    def client(self):
        """
        Returns the client instance used for cache updates.

        :return: Docker Registry API client.
        :rtype: docker_registry_util.client.DockerRegistryClient
        """
        return self._client

    def load(self, file):
        """
        Loads a previous state of a cache from a JSON file or file-like object.

        :param file: JSON file.
        """
        self._cache = json.load(file)
        self._initialized = True

    def loads(self, s):
        """
        Loads a previous state of a cache from a JSON string.

        :param s: JSON-formatted string.
        :type s: str
        """
        self._cache = json.loads(s)
        self._initialized = True

    def dump(self, file):
        """
        Saves the current state of the image cache to a file (or file-like object) in JSON format.

        :param file: Output file.
        """
        self._cache.dump(file)

    def dumps(self):
        """
        Returns the current state of the image cache as a string in JSON format.

        :return: str
        """
        return self._cache.dumps()

class HTTPBase64Auth(AuthBase):
    """
    Similar to HTTPBasicAuth, but handles the base64 encoded string directly instead of dividing it into user name
    and password.
    """
    def __init__(self, auth):
        self.auth = auth

    def __eq__(self, other):
        return self.auth == getattr(other, 'auth', None)

    def __ne__(self, other):
        return not self == other

    def __call__(self, r):
        r.headers['Authorization'] = 'Basic {0}'.format(self.auth)
        return r

def _get_auth_config(registry):
    try:
        with open(DOCKER_CONFIG_FILE) as config_file:
            config_data = json.load(config_file)
    except IOError:
        return None
    auth_data = config_data.get('auths')
    if not auth_data:
        return None
    registry_data = auth_data.get(registry)
    if not registry_data:
        return None
    return registry_data.get('auth')

def get_query(registry):
    kwargs = {}
    if os.path.isfile(DOCKER_CONFIG_FILE):
        config_auth = _get_auth_config(registry)
        if config_auth:
            kwargs['auth'] = HTTPBase64Auth(config_auth)
    if registry.startswith('http'):
        base_url = registry
    else:
        base_url = 'http://{0}'.format(registry)
    client = DockerRegistryClient(base_url, **kwargs)
    query = DockerRegistryQuery(client)
    return query


import os
import re

class ResultPostprocNet():

    def __init__(self, hd_dir, model_path):

        '''``self.replacer_id`` used for renaming of ``.out``
        files in order to prevent rewriting in parametrization'''

        self.hd_dir = hd_dir
        self.model_path = model_path
        self.replacer_id = "_seq"

    def out_to_array(self, results_params, progress=None):

        '''Convert strings with data from ``results_params[param]``
        to lists.

        Inputs:

        - ``results_params`` -- dict with values
        (like: "0.3133: [0.12, 0.13, ..., 3.1]")

        - ``progress`` -- ProgressNotebook instance.

        Return:

        - ``results_param_arrays`` -- dict
        (like: results_param_arrays[param][var][time]
                = [0.12, 0.13, ..., 3.1])
        '''

        results_param_arrays = {}

        def gen(result):
            for key in result:
                step_0_0 = result[key].replace('...,', "")
                step_0_1 = re.subn(r'\[\s+', "[", step_0_0)[0]
                step_0 = re.subn(r'\.\s+', ".0,", step_0_1)[0]
                step_1 = re.subn(r'\s+', ",", step_0)[0]
                step_2 = step_1.replace('.]', '.0]')
                step_3 = step_2.replace('[,', '[')
                step_4 = step_3.replace("nan", "None")
                yield((key, eval(step_4)))

        steps = 0
        for param in results_params:
            for var, data in enumerate(results_params[param]):

                if param not in results_param_arrays:
                    results_param_arrays[param] = {}

                try:
                    lines = data.split('\n')
                    result = {}

                    for line in lines:
                        if ':' in line:
                            key, val = line.split(':')
                            result[key] = val[1:]
                        else:
                            val = line
                            result[key] += val

                    result_t = [(float(key), val) for key, val in gen(result)]

                except:
                    results_param_arrays[param][var] = None
                    continue

                results_param_arrays[param][var] = dict(result_t)
                steps += 1
                progress.succ(steps)
                # print(steps)
        times = [key[0] for key in result_t]
        # result_x = np.array([result_t[key] for key in result_t]).T
        return(times, results_param_arrays)

    def remove_out(self):

        '''Clear ``.out`` and ``.mp4`` files from out folder.'''

        out_dir = os.path.join(self.hd_dir, 'problems',
                               self.model_path, "out")
        listdir = os.listdir(out_dir)

        outs = [os.path.join(out_dir, file_name)
                for file_name in listdir
                if '.out' in file_name]

        mp4 = [os.path.join(out_dir, file_name) for file_name in listdir
               if 'mp4' in file_name]

        for out in outs:
            os.remove(out)
            print(os.path.basename(out), "removed")

        for video in mp4:
            os.remove(video)
            print(os.path.basename(video), "removed")

    def extract_out(self, params_idx, data):

        '''extract data from ``.out`` files to
        ``data[params_idx]``'''

        out_dir = os.path.join(self.hd_dir, 'problems',
                               self.model_path, "out")
        listdir = os.listdir(out_dir)

        replacer_id = self.replacer_id

        outs = [os.path.join(out_dir, file_name)
                for file_name in listdir
                if ('.out' in file_name) and (replacer_id not in file_name)]
        # print(outs)

        data[params_idx] = []
        for out in outs:
            param_data = "failure"
            with open(out) as f:
                param_data = f.read()
            data[params_idx].append(param_data)
        # return(data)

    def rename_out(self, params_idx):

        '''rename ``.out`` and ``.mp4`` files in order to
        prevent their revriting.
        (see also ``self.replacer_id`` in ``self.__init__``)'''

        out_dir = os.path.join(self.hd_dir, 'problems',
                               self.model_path, "out")
        listdir = os.listdir(out_dir)

        replacer_id = self.replacer_id
        replacer = replacer_id+str(params_idx)

        outs = [os.path.join(out_dir, file_name)
                for file_name in listdir
                if ('.out' in file_name) and (replacer_id not in file_name)]
        new_outs = [out.replace(".out", replacer+".out")
                    for out in outs]

        mp4 = [os.path.join(out_dir, file_name) for file_name in listdir
               if 'mp4' in file_name and (replacer_id not in file_name)]
        new_mp4 = [file_name.replace(".mp4", replacer+".mp4")
                   for file_name in mp4]

        for num, out in enumerate(outs):
            os.rename(out, new_outs[num])

        for num, file_name in enumerate(mp4):
            os.rename(file_name, new_mp4[num])


import mysql.connector

from collections import namedtuple

# TODO: Write document for this lib

class MySQLWrapper:
    """
    Wrapper lib for mysql connection
    """
    conn = None
    cur = None
    conf = None

    def __init__(self, **kwargs):
        """
        Setup database configuration

        :param kwargs:
        """
        self.conf = kwargs

    def init_app(self, current_app):
        """
        Get database configuration from flask app instance and connect to the mysql database

        :param current_app: <object> Flask app instance
        :return:
        """

        self.conf = dict(self.conf or ())

        self.conf['user'] = current_app.config.get('MYSQL_DATABASE_USERNAME')
        self.conf['password'] = current_app.config.get('MYSQL_DATABASE_PASSWORD')
        self.conf['host'] = current_app.config.get('MYSQL_DATABASE_HOST', '127.0.0.1')
        self.conf['database'] = current_app.config.get('MYSQL_DATABASE_NAME')
        self.conf['port'] = current_app.config.get('MYSQL_DATABASE_PORT', '3306')

        self.connect()

    def connect(self):
        """
        Setup connection and database cursor

        :return:
        """

        self.conn = mysql.connector.connect(**self.conf)
        self.cur = self.conn.cursor()

    def end(self):
        """Close the connection"""
        self.cur.close()
        self.conn.close()

    def _select(self, table=None, fields=(), where=None, order=None, limit=None):
        """
        Form the sql query for execution

        :param table: <string> table name
        :param fields: <list> list of selecting fields e.g. ("field1", "field2")
        :param where: <list> query condition e.g. [ "id > 1 and id < %s", 5 ]
        :param order: <list> returning order [ "id ASC", "name DESC" ]
        :param limit: <limit> number of returning items and offset of the query [5, 10]
        :return: <object> Mysql cursor object
        """
        query = "SELECT %s FROM `%s`" % (",".join(fields), table)

        # handle where query
        if where and len(where) > 0:
            query += " WHERE %s" % where[0]

        # handle order query
        if order and len(order) > 0:
            query += " ORDER BY %s" % ", ".join(order)

        # Handle limit query
        if limit and len(limit) > 0:
            query += " LIMIT %s" % limit[0]

            if len(limit) > 1:
                query += ", %s" % limit[1]

        return self.query(query, where[1] if where and len(where) > 1 else None)

    def query(self, sql, params=None, multi=False):
        """
        Run raw query

        :param sql: <string> raw sql query
        :param params: <tuple> parameters
        :param multi: <boolean> indicator for running multiple queries or not
        :return: <object> Mysql cursor object
        """

        if not multi:
            self.cur.execute(sql, params, multi=multi)
        else:
            for result in self.conn.cmd_query_iter(sql):
                continue

        return self.cur

    def get_one(self, table=None, fields='*', where=None, order=None, limit=(0, 1)):
        """
        Get single result

        :param table: <string> table name
        :param fields: <list> list of selecting fields e.g. ("field1", "field2")
        :param where: <list> query condition e.g. [ "id > 1 and id < %s", 5 ]
        :param order: <list> returning order [ "id ASC", "name DESC" ]
        :param limit: <limit> number of returning items and offset of the query [5, 10]
        :return: <namedtuple> Row object-like with fields are query fields
        """

        cur = self._select(table, fields, where, order, limit)
        result = cur.fetchone()

        row = None
        if result:
            # Create named tuple by using the query fields
            Row = namedtuple("Row", [d[0] for d in cur.description])
            row = Row(*result)

        return row

    def get_all(self, table=None, fields='*', where=None, order=None, limit=None):
        """
        Get all results

        :param table: <string> table name
        :param fields: <list> list of selecting fields e.g. ("field1", "field2")
        :param where: <list> query condition e.g. [ "id > 1 and id < %s", 5 ]
        :param order: <list> returning order [ "id ASC", "name DESC" ]
        :param limit: <limit> number of returning items and offset of the query [5, 10]
        :return: <list> A list of Row object-like with fields are query fields
        """

        cur = self._select(table, fields, where, order, limit)
        result = cur.fetchall()

        rows = None
        if result:
            # Create named tuple by using the query fields
            Row = namedtuple("Row", [d[0] for d in cur.description])
            rows = [Row(*r) for r in result]

        return rows

    def commit(self):
        """Commit a transaction"""
        return self.conn.commit()