import tkinter, threading
from tkinter import ttk
from interface.onglets.onglets_map import OngletsMap
from interface.onglets.onglets_packets import OngletsPackets
from interface.onglets.onglets_personnage import OngletsPersonnage
from interface.onglets.onglets_sorts import OngletsSorts
import time

class MainInterface(threading.Thread):

    def __init__(self):
        threading.Thread(None,self.launch).start()
        while True:
            time.sleep(1)
            if self.ongletsSorts:
                break

    def set_character(self, character):
        self.character = character
        self.ongletsMap.set_character(character)
        self.ongletsSorts.set_character(character)
        self.ongletsPersonnage.set_character(character)
        threading.Thread(None,self.character_statue).start()

    def character_statue(self):
        en_mouvement = tkinter.Label(self.main, bg="red", text = "En mouvement")
        en_mouvement.place(relx=0.05, rely=0.05, relwidth=0.08, relheight=0.04)

        en_recolte = tkinter.Label(self.main, bg="red", text = "En recolte")
        en_recolte.place(relx=0.05, rely=0.10, relwidth=0.08, relheight=0.04)

        en_combat = tkinter.Label(self.main, bg="red", text = "En combat")
        en_combat.place(relx=0.05, rely=0.15, relwidth=0.08, relheight=0.04)
        while True:
            time.sleep(1)
            if self.character.deplacement.ismouving:
                en_mouvement.configure(bg = "Green")
            else:
                en_mouvement.configure(bg = "Red")
            if self.character.isharvest:
                en_recolte.configure(bg = "Green")
            else:
                en_recolte.configure(bg = "red")
            if self.character.isfighting:
                en_combat.configure(bg = "Green")
            else:
                en_combat.configure(bg = "red")

    def launch(self):
        self.main = tkinter.Tk()
        self.main.title("LeafBot")
        self.main.geometry('1200x900')
        self.create_notebook()
        self.main.mainloop()

    def create_notebook(self):
        self.onglets = tkinter.ttk.Notebook(self.main)
        self.onglets.pack()
        self.onglets.place(relx=0.15, rely=0.05, relwidth=0.83, relheight=0.83)

        self.ongletsPackets = OngletsPackets(self.onglets)
        self.ongletsPersonnage = OngletsPersonnage(self.onglets)
        self.ongletsMap = OngletsMap(self.onglets)
        self.ongletsSorts = OngletsSorts(self.onglets)

    def base_start(self,character):
        self.vita = tkinter.Label(self.main, bg="red", text = character.vie_actuelle +" / " + character.vie_max)
        self.vita.pack()
        self.vita.place(relx=0.20, rely=0.90, relwidth=0.08, relheight=0.08)

        self.energie = tkinter.Label(self.main, bg="yellow", text = character.ennergie_actuelle +" / " + character.ennergie_max)
        self.energie.pack()
        self.energie.place(relx=0.40, rely=0.90, relwidth=0.08, relheight=0.08)

        self.xp = tkinter.Label(self.main,bg="deep sky blue", text = character.xp_actuelle +" / " + character.xp_fin)
        self.xp.pack()
        self.xp.place(relx=0.60, rely=0.90, relwidth=0.1, relheight=0.08)

        self.kamas = tkinter.Label(self.main, bg="orange", text = character.kamas)
        self.kamas.pack()
        self.kamas.place(relx=0.80, rely=0.90, relwidth=0.08, relheight=0.08)

if __name__ == "__main__":
    MainInterface()


from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import argodb as argo
import research_tools as research

plt.ion()
plt.close('all')

dirtopo = '/datawork/fsi2/mars/DATA/BATHY/ETOPO2'
topofile = 'etopo2.nc'

dirtopo = '/net/alpha/exports/sciences/data/BATHYMETRIE/BATHYMETRIE'
topofile = 'ETOPO2v2c_f4.nc'
dirtile = '/net/libra/local/tmp/1/herry/tiles'

itile = 50

figsize = (9, 7)

reso = 0.5

argodic = research.read_argo_filter(itile)
minlon, maxlon, minlat, maxlat = argodic['LONMIN_NO_M'], argodic['LONMAX_NO_M'], argodic['LATMIN_NO_M'], argodic['LATMAX_NO_M']

lon = np.arange(reso*np.floor(minlon/reso), reso*np.floor(maxlon/reso)+reso, reso)
lat = np.arange(reso*np.floor(minlat/reso), reso*np.floor(maxlat/reso)+reso, reso)

#lon_deg, lat_deg = define_grid(minlon, maxlon, minlat, maxlat, reso_deg)

with Dataset('%s/%s' % (dirtopo, topofile)) as nc:
    z = nc.variables['z'][:,:]

dl0 = 1/30.  # 1/30deg for etopo
lontopo = np.arange(-180, 180+dl0, dl0)
lattopo = np.arange(-90, 90+dl0, dl0)

def get_idx_of_box(lontopo, lattopo, cell):
    minlon, maxlon, minlat, maxlat = cell
    ilon = [i for i, x in enumerate(lontopo) if (x>=minlon) and (x<=maxlon)]
    jlon = [j for j, x in enumerate(lattopo) if (x>=minlat) and (x<=maxlat)]
    return ilon[0], ilon[-1], jlon[0], jlon[-1]

domain = [minlon, maxlon, minlat, maxlat]
i0, i1, j0, j1 = get_idx_of_box(lontopo, lattopo, domain)

def average_topo_on_box(depth, cell):
    """ average high resolution depth array on cell """
    i0, i1, j0, j1 = get_idx_of_box(lontopo, lattopo, cell)
    return np.mean(depth[j0:j1, i0:i1].ravel())

def box(cell, d=0):
    x1, x2, y1, y2 = cell
    plt.plot([x1-d, x1-d, x2+d, x2+d, x1-d],
           [y1-d, y2+d, y2+d, y1-d, y1-d], 'k')

plt.figure(figsize=figsize)
plt.imshow(z[j0:j1, i0:i1],
           origin='lower', extent=[minlon, maxlon, minlat, maxlat])
plt.axis('tight')
plt.colorbar()

reso = 0.5
lon = np.arange(minlon, maxlon, reso)
lat = np.arange(minlat, maxlat, reso)

nlon = len(lon)
nlat = len(lat)
bathy = np.zeros((nlat, nlon))
for j in range(nlat-1):
    for i in range(nlon-1):
        reso2 = reso*0.5
        gridcell = [lon[i]-reso2, lon[i]+reso2, lat[j]-reso2, lat[j]+reso2]
        box(gridcell)
        get_idx_of_box(lontopo, lattopo, gridcell)
        bathy[j, i] = average_topo_on_box(z, gridcell)

msk = bathy < 0

fig, ax = plt.subplots(2,1)

divider = make_axes_locatable(ax[0])
ax_cb = divider.new_horizontal(size="4%", pad=0.2)
im = ax[0].imshow(bathy,
           origin='lower', interpolation='nearest',
           extent=[minlon, maxlon, minlat, maxlat])
ax[0].set_title('tile #%03i' % itile)
fig.add_axes(ax_cb)
fig.colorbar(im, cax=ax_cb)

divider = make_axes_locatable(ax[1])
ax_cb = divider.new_horizontal(size="4%", pad=0.2)
ax[1].imshow(msk,
           origin='lower', interpolation='nearest',
           extent=[minlon, maxlon, minlat, maxlat])


#import Library
import speech_recognition as sr

# Initialize recognizer class
r = sr.Recognizer()

# Reading Audio file as source
# listening the audio file and store in audio_text variable
# The path should be correct

with sr.AudioFile('Sample.wav') as source:

    audio = r.listen(source)

# Using exception handling in case the api could not be acceessed successfully.
    try:
        # using google speech recognition
        text = r.recognize_google(audio)
        print('Convertint Speech into text successfully!')
        print(text)

    except:
         print('Could not access API, please run it again.')


import argparse
import logging
import random
import sys
import time
from copy import deepcopy

import numpy as np
import torch
from scipy.stats import kendalltau

from datasets.dataloader import get_dataloader
from models.cell_operations import NAS_BENCH_201
from models.supernet import Supernet201
from utils import obtain_accuracy, AverageMeter, set_seed, run_func, time_record

parser = argparse.ArgumentParser("Train 201 Supernet")
# dataset
parser.add_argument("--data_root", type=str, default='./dataset/', help="The path to dataset")
parser.add_argument("--dataset", type=str, default='cifar10', help="Dataset.")
parser.add_argument("--search_space_name", type=str, default='nas-bench-201', help="The search space name.")
parser.add_argument("--num_classes", type=int, default=10, help="Dataset Classes")
# supernet
parser.add_argument("--max_nodes", type=int, default=4, help="The maximum number of nodes.")
parser.add_argument("--channel", type=int, default=16, help="The number of channels.")
parser.add_argument("--num_cells", type=int, default=5, help="The number of cells in one stage.")
# training settings
parser.add_argument("--exp_name", type=str, default='debug_baseline', help='exp_name for saving results')
parser.add_argument("--method", type=str, default='spos', choices=['spos', 'fairnas', 'sumnas'])
parser.add_argument("--lr", type=float, default=0.05, help="Learning rate")
parser.add_argument("--inner_lr", type=float, default=0.05, help="Learning rate")
parser.add_argument("--momentum", type=float, default=0.9, help="Momentum")
parser.add_argument("--wd", type=float, default=2.5e-4, help="Weight decay")
parser.add_argument("--epochs", type=int, default=250, help="Training epochs")
parser.add_argument("--gpu_id", type=int, default=0, help="Training GPU")
parser.add_argument("--train_batch_size", type=int, default=256, help="Train batch size")
parser.add_argument("--valid_batch_size", type=int, default=512, help="Valid batch size")
parser.add_argument("--print_freq", type=int, default=50, help="print frequency when training")
parser.add_argument("--rank_print_freq", type=int, default=100, help="print frequency when ranking")
parser.add_argument("--seed", type=int, default=0, help="manual seed")
parser.add_argument("--debug", default=False, action='store_true', help="for debug")

args = parser.parse_args()
args.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
args.ckpt_path = 'checkpoints/%s.pt' % args.exp_name
args.pred_path = 'results/%s.npy' % args.exp_name
if args.debug:
    args.epochs = 5

# logging config
log_format = '%(asctime)s %(message)s'
logging.basicConfig(stream=sys.stdout, level=logging.INFO,
                    format=log_format, datefmt='%m/%d %I:%M:%S %p')
logging.info(args)
set_seed(args.seed)

def mix_grad(grad_list, weight_list):
    """
    calc weighted average of gradient
    """
    mixed_grad = []
    for g_list in zip(*grad_list):
        g_list = torch.stack([weight_list[i] * g_list[i] for i in range(len(weight_list))])
        mixed_grad.append(torch.sum(g_list, dim=0))
    return mixed_grad

def apply_grad(model, grad):
    """
    assign gradient to model(nn.Module) instance. return the norm of gradient
    """
    for p, g in zip(model.parameters(), grad):
        if p.grad is None:
            p.grad = g
        else:
            p.grad += g

def train(epoch, train_loader, model, criterion, optimizer, inner_optimizer=None):
    train_loss = AverageMeter()
    train_top1 = AverageMeter()
    train_top5 = AverageMeter()

    model.train()
    path_list = []
    num_candidate_ops = 5
    candidate_ops = list(range(5))
    candidate_edges = 6

    for step, (inputs, targets) in enumerate(train_loader):
        inputs = inputs.to(args.device)
        targets = targets.to(args.device)
        if args.method == 'spos':
            # randomly sample an arch
            sampled_arch = [
                random.choice(candidate_ops) for _ in range(candidate_edges)
            ]
            optimizer.zero_grad()
            logits = model(inputs, sampled_arch)
            loss = criterion(logits, targets)
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), 5)
            optimizer.step()

        elif args.method == 'fairnas':
            # shuffle the ops to get sub-models with strict fairness
            for _ in range(candidate_edges):
                random.shuffle(candidate_ops)
                path_list.append(deepcopy(candidate_ops))
            # inner loop
            optimizer.zero_grad()
            for _path_id in range(num_candidate_ops):
                sampled_arch = [_operations[_path_id] for _operations in path_list]
                logits = model(inputs, sampled_arch)
                loss = criterion(logits, targets)
                loss.backward()
                # record training metrics
                prec1, prec5 = obtain_accuracy(logits.data, targets.data, topk=(1, 5))
                train_loss.update(loss.item(), inputs.size(0))
                train_top1.update(prec1.item(), inputs.size(0))
                train_top5.update(prec5.item(), inputs.size(0))
            torch.nn.utils.clip_grad_norm_(model.parameters(), 5)
            optimizer.step()

        elif args.method == 'sumnas':
            # record the supernet weights
            weights_before = deepcopy(model.state_dict())
            grad_list = []

            # shuffle the ops to get sub-models fairly
            for _ in range(candidate_edges):
                random.shuffle(candidate_ops)
                path_list.append(deepcopy(candidate_ops))
            # inner loop
            for _path_id in range(num_candidate_ops):
                sampled_arch = [_operations[_path_id] for _operations in path_list]
                # inner optimization
                for _step in range(args.adaption_steps):
                    inner_optimizer.zero_grad()
                    logits = model(inputs, sampled_arch)
                    loss = criterion(logits, targets)
                    loss.backward()
                    torch.nn.utils.clip_grad_norm_(model.parameters(), 5)
                    inner_optimizer.step()
                    # record training metrics
                    prec1, prec5 = obtain_accuracy(logits.data, targets.data, topk=(1, 5))
                    train_loss.update(loss.item(), inputs.size(0))
                    train_top1.update(prec1.item(), inputs.size(0))
                    train_top5.update(prec5.item(), inputs.size(0))
                # record reptile gradient
                outer_grad = []
                weights_after = deepcopy(model.state_dict())
                for p_0, p_T in zip(weights_before.items(), weights_after.items()):
                    outer_grad.append(-(p_T[1] - p_0[1]).detach())
                grad_list.append(outer_grad)
                model.load_state_dict(weights_before)
            # outer loop
            optimizer.zero_grad()
            weight = torch.ones(len(grad_list)) / len(grad_list)
            grad = mix_grad(grad_list, weight)
            apply_grad(model, grad)
            optimizer.step()

        else:
            raise ValueError('Wrong training method for the supernet: %s' % args.method)
        # record
        prec1, prec5 = obtain_accuracy(logits.data, targets.data, topk=(1, 5))
        train_loss.update(loss.item(), inputs.size(0))
        train_top1.update(prec1.item(), inputs.size(0))
        train_top5.update(prec5.item(), inputs.size(0))

        if step % args.print_freq == 0 or step + 1 == len(train_loader):
            logging.info('[Training] Epoch %03d/%03d, step %03d/%03d, loss: %.3f, top1: %.3f, top5: %.3f'
                         % (epoch, args.epochs, step, len(train_loader), train_loss.avg, train_top1.avg, train_top5.avg))
    return train_loss.avg, train_top1.avg, train_top5.avg

def valid(valid_loader, model, criterion):
    val_loss, val_top1, val_top5 = AverageMeter(), AverageMeter(), AverageMeter()
    model.eval()
    with torch.no_grad():
        for step, (val_inputs, val_targets) in enumerate(valid_loader):
            val_inputs = val_inputs.to(args.device)
            val_targets = val_targets.to(args.device)

            # randomly sample an arch
            candidate_ops = range(5)
            candidate_edges = 6
            sampled_arch = [
                random.choice(candidate_ops) for _ in range(candidate_edges)
            ]

            # prediction
            logits = model(val_inputs, sampled_arch)
            loss = criterion(logits, val_targets)
            # record
            prec1, prec5 = obtain_accuracy(
                logits.data, val_targets.data, topk=(1, 5)
            )
            val_loss.update(loss.item(), val_inputs.size(0))
            val_top1.update(prec1.item(), val_inputs.size(0))
            val_top5.update(prec5.item(), val_inputs.size(0))
    return val_loss.avg, val_top1.avg, val_top5.avg

def valid_specific_path(valid_loader, model, sampled_arch, criterion, device):
    val_loss, val_top1, val_top5 = AverageMeter(), AverageMeter(), AverageMeter()
    model.eval()
    with torch.no_grad():
        for step, (val_inputs, val_targets) in enumerate(valid_loader):
            val_inputs = val_inputs.to(device)
            val_targets = val_targets.to(device)

            # prediction
            logits = model(val_inputs, sampled_arch)
            loss = criterion(logits, val_targets)
            # record
            prec1, prec5 = obtain_accuracy(
                logits.data, val_targets.data, topk=(1, 5)
            )
            val_loss.update(loss.item(), val_inputs.size(0))
            val_top1.update(prec1.item(), val_inputs.size(0))
            val_top5.update(prec5.item(), val_inputs.size(0))
    return val_loss.avg, val_top1.avg, val_top5.avg

def rank_supernet(valid_loader, model, criterion):
    logging.info('---------- Start to rank on NAS-Bench-201 ----------')
    nasbench201 = np.load('./dataset/nasbench201/nasbench201_dict.npy', allow_pickle=True).item()
    if args.debug:
        new_dict = {}
        for i in range(5):
            new_dict[str(i)] = deepcopy(nasbench201[str(i)])
        nasbench201 = deepcopy(new_dict)
    nasbench201_len = len(nasbench201)
    tmp_pred = []
    tmp_target = []
    prediction = {}
    for step, item in enumerate(nasbench201):
        model_id = int(item)
        operation = nasbench201[item]['operation']
        target = nasbench201[item]['cifar10_test']
        val_loss, val_top1, val_top5 = valid_specific_path(valid_loader, model, operation, criterion, args.device)
        tmp_pred.append(val_top1)
        tmp_target.append(target)
        prediction[model_id] = {'id': model_id, 'model_gene': operation, 'pred': val_top1, 'target': target}
        if step % args.rank_print_freq == 0 or (step + 1) == nasbench201_len:
            logging.info("model_id: %d  gene: %s  loss: %.3f  top1: %.3f  target: %.3f"
                         % (model_id, str(operation), val_loss, val_top1, target))
            logging.info("Evaluated: %05d\tWaiting: %05d\tCurrent Kendall's Tau: %.5f" %
                         (len(tmp_pred), nasbench201_len-len(tmp_pred), kendalltau(tmp_pred, tmp_target)[0]))

    # save predictions
    print('\n')
    np.save(args.pred_path, prediction)
    logging.info('Finish ranking and save predictions to : %s' % args.pred_path)
    final_ranking = kendalltau(tmp_pred, tmp_target)[0]
    logging.info("Final_pred: %05d\tFinal_target: %05d\tFinal_Kendall's Tau: %.5f" %
                 (len(tmp_pred), len(tmp_target), final_ranking))

    return final_ranking

def main():
    # time record
    train_start = time.time()

    # dataloader
    train_loader, valid_loader = get_dataloader(args, model=None, dataset=args.dataset)

    # supernet
    model = Supernet201(
        C=args.channel, N=args.num_cells, max_nodes=args.max_nodes,
        num_classes=args.num_classes, search_space=NAS_BENCH_201
    ).to(args.device)

    # training settings
    optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.wd)
    if args.method == 'sumnas':
        args.adaption_steps = 2
        optimizer = torch.optim.SGD(model.parameters(), 1.0, weight_decay=4e-5)
        inner_optimizer = torch.optim.SGD(model.parameters(), 0.05, momentum=0.9)
    else:
        inner_optimizer = None
    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, args.epochs)
    criterion = torch.nn.CrossEntropyLoss()
    best_val_top1 = 0.0

    logging.info('---------- Start to train supernet ----------')
    for epoch in range(args.epochs):
        # train supernet
        train_loss, train_top1, cnn_top5 = train(epoch, train_loader, model, criterion, optimizer, inner_optimizer)
        logging.info(
            "[Epoch: %s/%s] train_loss=%.3f, train_top1=%.3f, train_top5=%.3f" %
            (epoch, args.epochs, train_loss, train_top1, cnn_top5)
        )

        # valid supernet
        val_loss, val_top1, val_top5 = valid(valid_loader, model, criterion)
        logging.info(
            "[Validation], val_loss=%.3f, val_top1=%.3f, val_top5=%.3f, best_top1=%.3f" %
            (val_loss, val_top1, val_top5, best_val_top1)
        )
        if best_val_top1 < val_top1:
            best_val_top1 = val_top1

        # save latest checkpoint
        torch.save(model.state_dict(), args.ckpt_path)
        logging.info('Save latest checkpoint to %s' % args.ckpt_path)

        # scheduler step
        scheduler.step()
        print('\n')

    # time record
    supernet_training_elapse = time_record(train_start, prefix='Supernet training')
    print('\n')

    # load best supernet weights
    latest_pretrained_weights = torch.load(args.ckpt_path)
    model.load_state_dict(latest_pretrained_weights)
    model.eval()

    # ranking supernet
    final_ranking = rank_supernet(valid_loader, model, criterion)

    # write results
    with open('./results/ranking.txt', 'a') as f:
        f.write("EXP: %s \t Seed: %s \t Kendall' Tau: %.6f \t Training_Elapse: %s \n"
                % (args.exp_name, args.seed, final_ranking, supernet_training_elapse))

if __name__ == "__main__":
    run_func(args, main)


from . import Funktioner
import datetime
import MyModules.GUIclasses2 as GUI
import numpy as np
import os
from . import FileOps
#fix 14.04.10, simlk. Changed "centering error", which should make the test more forgiving at small distances - at large distances it has no effect.
# Last edit: 2012-01-09 fixed mtl test. Unified, 2012-04-23: fixed possible None return val in TestStretch
#Rejcection criteria reflects a model variance for meas. of a strectch, should correpond to a variance of half the parameter used in the test.

# Test is really - for two meas:
#par=prec=reject_par/2.....
#|diff|<2*sqrt(var_model(d,par)) - always linear in par.
#No more Centering err/ constant 'avoid zero' term!
#Thus the var-models are artificial close to zero. Instead a global min is defined (0,3 mm for now)!!!!!

GLOBAL_MIN_DEV=0.3 #twice precision on mean

def MTL_var_model_linear(dist,parameter):
	dist=dist/1000.0
	return (dist*parameter)**2

def MTL_var_model(dist,parameter):
	dist=dist/1000.0
	DLIM=0.2 #km
	c_err=0 #divided by two below because 'precision' is (defined to be)  half of 'reject par'
	if dist<DLIM:
		FKLIN=(np.sqrt(DLIM)*parameter-c_err*0.5)/DLIM
		return (FKLIN*dist+c_err*0.5)**2
	else:
		return (parameter**2*dist)

def MGL_var_model(dist,parameter):
	dist=dist/1000.0
	c_err=0.0 #divided by two below because 'precision' is (defined to be)  half of 'reject par'
	return (np.sqrt(dist)*parameter+c_err*0.5)**2   #add a centering err....

class FBreject(object):
	def __init__(self,database,program="MGL",parameter=2.0,unit="ne"):
		if program=="MGL":
			self.var_model=MGL_var_model
		else:
			if unit=="ne":
				self.var_model=MTL_var_model
			else:
				self.var_model=MTL_var_model_linear
		self.unit=unit
		self.parameter=parameter
		self.precision=parameter*0.5 #this is the correpsonding 'precision'
		self.initialized=False
		self.found=False
		self.wasok=False
		self.database=database
		self.initialized=True
	def GetData(self):
		data=""
		for key in list(self.database.keys()):
			s=self.database[key]
			data+="%s->%s: dist: %.2f m\n" %(key[0],key[1],s.dist)
			for i in range(len(s.hdiffs)):
				data+="dh: %.4f m tid: %s j-side: %s\n" %(s.hdiffs[i],s.times[i].isoformat().replace("T",","),s.jpages[i])
		return data
	def GetDatabase(self):
		return self.database
	def TestStretch(self,start,end,hdiff): #returns foundstretch,testresult,#found,msg
		self.found=False
		self.wasok=False
		msg=""
		key_back=(end,start)
		key_forward=(start,end)
		nforward=0
		nback=0
		hdiffs_all=np.empty((0,))
		dists=[]
		if key_back in self.database:
			s_back=self.database[key_back]
			nback+=len(s_back.hdiffs)
			if nback>0:
				dists.append(s_back.dist)
				hdiffs_all=np.append(hdiffs_all,np.array(s_back.hdiffs)*-1.0)
		if key_forward in self.database:
			s_forward=self.database[key_forward]
			nforward+=len(s_forward.hdiffs)
			if nforward>0:
				dists.append(s_forward.dist)
				hdiffs_all=np.append(hdiffs_all,np.array(s_forward.hdiffs))
		msg+="%s->%s er tidligere m\u00E5lt %d gang(e), og %d gang(e) i modsat retning.\n" %(start,end,nforward,nback)
		nall=len(hdiffs_all)
		if len(hdiffs_all)>0:
			d=np.mean(dists)
			norm_d=np.sqrt(d/1e3)
			msg+="Afstand: %.2f m\n" %d
			if len(hdiffs_all)>1:
				raw_mean=np.mean(hdiffs_all)
				raw_std=np.std(hdiffs_all,ddof=1)
				raw_prec=raw_std/np.sqrt(len(hdiffs_all))
				raw_max_diff=hdiffs_all.max()-hdiffs_all.min()
				msg+="hdiff_middel: %.4f m, max-diff: %.2f mm (%.2f ne)\n" %(raw_mean,raw_max_diff*1000,raw_max_diff*1e3/norm_d)
				msg+="std_dev: %.2f mm, std_dev(middel): %.2f mm (%.2f ne)\n" %(raw_std*1000,raw_prec*1000,raw_prec*1e3/norm_d)
			msg+="\nEfter inds\u00E6ttelse af ny m\u00E5ling:\n"
			hdiffs_new=np.append(hdiffs_all,[hdiff])
			new_mean=np.mean(hdiffs_new)
			new_std=np.std(hdiffs_new,ddof=1)
			new_prec=new_std/np.sqrt(len(hdiffs_new))
			new_max_diff=hdiffs_new.max()-hdiffs_new.min()
			msg+="hdiff_middel: %.4f m, max-diff: %.2f mm (%.2f ne)\n" %(new_mean,new_max_diff*1000,new_max_diff*1e3/norm_d)
			msg+="std_dev: %.2f mm, std_dev(middel): %.2f mm (%.2f ne)\n" %(new_std*1000,new_prec*1000,new_prec*1e3/norm_d)
			msg+="\nForkastelsesparameter: %.3f %s." %(self.parameter,self.unit)
			max_dev=self.GetMaxDev(d) #in mm!!
			if len(hdiffs_new)==2:
				msg+=" Vil acceptere |diff|<%.2f mm" %(2*max_dev)
			isok=(new_prec*1e3<=max_dev)
			self.found=True
			self.wasok=isok
			if isok:
				msg+="\nDen samlede standardafvigelse p\u00E5 middel er OK.\n"
			else:
				msg+="\nDen samlede standarafvigelse p\u00E5 middel er IKKE OK\n"
				msg+="Foretag flere m\u00E5linger!\n"
			if len(hdiffs_all)>1 and new_prec>raw_prec: #or something more fancy
				msg+="Den nye m\u00E5ling er tilsyneladende en outlier og kan evt. omm\u00E5les!\n"
				isok=False
			return True,isok,len(hdiffs_all),msg
		else:
			msg="%s->%s er ikke m\u00E5lt tidligere" %(start,end)
			self.found=False
			self.wasok=True
			return True,True,0,msg

	def GetMaxDev(self,dist): #max dev in mm!
		return max(np.sqrt(self.var_model(dist,self.precision)),GLOBAL_MIN_DEV*0.5)

	def InsertStretch(self,start,end,hdiff,dist,dato,tid,jside=""):
		if not self.initialized:
			return True #we havent done anyting
		data=self.database
		try:
			start=start.strip()
			end=end.strip()
			key=(start,end)
			m,h=Funktioner.GetTime(tid)
			day,month,year=Funktioner.GetDate(dato)
			date=datetime.datetime(year,month,day,h,m)
			if key in data:
				data[key].AddStretch(hdiff,dist,date,jside)
			else:
				data[key]=Stretch()
				data[key].AddStretch(hdiff,dist,date,jside)
		except Exception as msg:
			print(repr(msg))
			return False
		else:
			return True
	def OutlierAnalysis(self):
		data=self.database.copy()
		msg=""
		noutliers=0
		nbad=0
		keys=list(data.keys())
		for key_forward in keys:
			l_msg="%s->%s:" %key_forward
			key_back=(key_forward[1],key_forward[0])
			if not key_forward in data: #could happen since we delete stuff below
				continue
			s_forward=data[key_forward]
			hdiffs_all=np.array(s_forward.hdiffs)
			nforward=len(s_forward.hdiffs)
			dists=[s_forward.dist]
			nback=0
			if key_back in data:
				s_back=data[key_back]
				nback=len(s_back.hdiffs)
				if nback>0:
					dists.append(s_back.dist)
					hdiffs_all=np.append(hdiffs_all,np.array(s_back.hdiffs)*-1.0)
			d=np.mean(dists)
			l_msg+=" m\u00E5lt %d gange frem og %d gange tilbage." %(nforward,nback)
			report=False
			if len(hdiffs_all)>1:
				std_dev=np.std(hdiffs_all,ddof=1)
				m=np.mean(hdiffs_all)
				#same test as above#
				prec=std_dev/np.sqrt(len(hdiffs_all))
				max_dev=self.GetMaxDev(d) #in mm
				#print max_dev,prec
				is_ok=(prec*1e3<=max_dev)
				if not is_ok:
					nbad+=1
					report=True
					l_msg+="\nForkastelseskriterie IKKE overholdt."
					l_msg+="\nTilladt fejl p\u00E5 middel: %.2f mm, aktuel fejl: %.2f mm" %(max_dev,prec*1e3)
				if len(hdiffs_all)>2:
					dh=np.fabs(hdiffs_all-m)
					outlier_limit=1.5*std_dev
					if len(hdiffs_all)==3:
						outlier_limit=1.1*std_dev
					I=np.where(np.fabs(dh)>outlier_limit)[0]
					if I.size>0:
						report=True
						l_msg+="\nOutliere:"
						for i in I:
							noutliers+=1
							if i>nforward-1:
								i-=nforward
								s=s_back
							else:
								s=s_forward
							l_msg+="\nHdiff: %.4f m, m\u00E5lt %s, journalside: %s" %(s.hdiffs[i],s.times[i].isoformat().replace("T"," "),s.jpages[i])
							hdiffs_new=np.delete(hdiffs_all,i)
							new_prec=np.std(hdiffs_new,ddof=1)/np.sqrt(len(hdiffs_new))
							l_msg+="\nFejl p\u00E5 middel: %.2f mm, fejl p\u00E5 middel uden denne m\u00E5ling: %.2f mm" %(prec*1e3,new_prec*1e3)
				if report:
					msg+="\n"+"*"*60+"\n"+l_msg

			#Finally delete that entry#
			del data[key_forward]
			if nback>0:
				del data[key_back]
		nprob=noutliers+nbad
		if nprob==0:
			return True,"Ingen problemer fundet"
		lmsg="%*s %d\n" %(-42,"#overtr\u00E6delser af forkastelseskriterie:",nbad)
		lmsg+="%*s %d\n" %(-42,"#outliere:",noutliers)
		return False,lmsg+msg

	def IsInitialized(self):
		return self.initialized
	def GetNumber(self):
		return len(self.database)
	def Disconnect(self):
		pass

def GetPlotData(program="MGL",parameter=2.0,unit="ne"):
	if program=="MGL":
		var_model=MGL_var_model
	else:
		if unit=="ne":
			var_model=MTL_var_model
		else:
			var_model=MTL_var_model_linear
	dists=np.arange(0,1500,10)
	precision=0.5*parameter  #since parameter is 'reject-parameter' and we define precison as half of dat - man :-)
	out=2*np.sqrt([var_model(x,precision) for x in dists])
	return np.column_stack((dists,out))

def GetGlobalMinLine(program="MGL"):
	dists=[0,400.0]
	hs=[GLOBAL_MIN_DEV,GLOBAL_MIN_DEV]
	return np.column_stack((dists,hs))

class Stretch(object):
	def __init__(self):
		self.hdiffs=[]
		self.dist=0
		self.times=[]
		self.jpages=[]
	def AddStretch(self,hdiff,dist,date,jpage=""):
		n=float(len(self.hdiffs))+1
		self.dist=self.dist*(n-1)/n+dist/n
		self.hdiffs.append(hdiff)
		self.times.append(date)
		self.jpages.append(jpage)

def MakeRejectData(resfiles):
	data=dict()
	nstrk=0
	nerrors=0
	for file in resfiles:
		heads=FileOps.Hoveder(file)
		for head in heads:
			try:
				key=(head[0],head[1])
				hdiff=float(head[5])
				dist=float(head[4])
				jside=head[6]
				tid=head[3]
				dato=head[2]
				m,h=Funktioner.GetTime(tid)
				day,month,year=Funktioner.GetDate(dato)
				date=datetime.datetime(year,month,day,h,m)
			except Exception as msg:
				print(repr(msg),head)
				nerrors+=1
			else:
				if key in data:
					data[key].AddStretch(hdiff,dist,date,jside)
				else:
					data[key]=Stretch()
					data[key].AddStretch(hdiff,dist,date,jside)
				nstrk+=1
	return data,nerrors


from django.conf.urls import url

from . import views

app_name = 'api'

urlpatterns = [
    url(r'^device/',views.device,name='api_device'),
    url(r'^light/',views.light,name='api_light'),
    url(r'^temperature/',views.temperature,name='api_temperature'),
    url(r'^humidity/',views.humidity,name='api_humidity'),
    url(r'^dirt_humidity/',views.dirt_humidity,name='api_dirt_humidity'),
    url(r'^fertilization/',views.fertilization,name='api_fertilization'),
    url(r'^water/',views.water,name='api_water'),
    url(r'^schedule/',views.schedule,name='api_schedule'),
    url(r'^user/',views.user,name='api_user'),
    url(r'^.*', views.noSuchApi, name='api_no_such_api'),
]


import numpy as np
import cv2

def compute_perspective_transform(corner_points,width,height,image):
	""" Compute the transformation matrix
    @ corner_points : 4 corner points selected from the image
    @ height, width : size of the image
    """
	# Create an array out of the 4 corner points
	corner_points_array = np.float32(corner_points)
	# Create an array with the parameters (the dimensions) required to build the matrix
	img_params = np.float32([[0,0],[width,0],[0,height],[width,height]])
	# Compute and return the transformation matrix
	matrix = cv2.getPerspectiveTransform(corner_points_array,img_params)
	img_transformed = cv2.warpPerspective(image,matrix,(width,height))
	return matrix,img_transformed

def compute_point_perspective_transformation(matrix,list_downoids):
	""" Apply the perspective transformation to every ground point which have been detected on the main frame.
    @ matrix : the 3x3 matrix
    @ list_downoids : list that contains the points to transform
    return : list containing all the new points
    """
	# Compute the new coordinates of our points
	list_points_to_detect = np.float32(list_downoids).reshape(-1, 1, 2)
	transformed_points = cv2.perspectiveTransform(list_points_to_detect, matrix)
	# Loop over the points and add them to the list that will be returned
	transformed_points_list = list()
	for i in range(0,transformed_points.shape[0]):
		transformed_points_list.append([transformed_points[i][0][0],transformed_points[i][0][1]])
	return transformed_points_list


# encoding: utf-8
"""
    GraphicInterface.py

    Displays the op amp calculator

    Dario Marroquin 18269 (dariomarroquin)
    Pablo Ruiz 18259 (PingMaster99)

    Version 1.0
    Updated March 4, 2020
"""
from tkinter import *
from CalculationsModule import *
import matplotlib.pyplot as plt
import numpy as np

# Constants
TITLE_SIZE = 15

def calculate():
    """
    Performs the op amp calculator calculations
    """
    plt.clf()
    inverter = int(opa.get()) == 1
    point = vin.get()
    try:
        point = float(point)
    except ValueError:
        vin.delete(0, END)
        point = None

    # Needed data
    populate_calculations()
    function, result, real_value = calculate_opamp_function(point, inverter)
    spline_result, spline_print = calculate_opamp_spline(point)
    error = calculate_error(point, result, inverter)
    spline_error = calculate_error(point, spline_result, inverter)

    # Error comparison
    print("Error mínimo cuadrado:", error, "%\nError trazadores cúbicos: ", spline_error, "%\n\nTrazadores:\n",
          spline_print, "\n\n")

    if type(result) is not str:
        str(round(result, 4))

    if type(error) is not str:
        error = str(round(error, 4)) + " %"

    if function[0] > 0:
        a0 = "+ " + str(round(function[0], 4))
    elif function[0] < 0:
        a0 = "- " + str(round(function[0], 4))[1:]
    else:
        a0 = ""
    result_funcion["text"] = f"{round(function[1], 4)} * Vin {a0}"

    result_ev["text"] = result
    result_err["text"] = error

    x_1 = np.linspace(0, 20)
    y_1 = x_1 * real_value
    y_2 = x_1 * function[1] + function[0]

    # Results graph
    plt.plot(x_1, y_1, label="Teórico")
    plt.plot(x_1, y_2, label="Experimental")
    plt.legend()
    plt.title("Función teórica y experimental")
    plt.xlabel("Vin")
    plt.ylabel("Vout")
    plt.show()

"""
    GUI window with grid layout
"""
window = Tk()
window.columnconfigure(0, minsize=100)
window.columnconfigure(1, minsize=100)
window.columnconfigure(2, minsize=100)
window.columnconfigure(3, minsize=100)
window.columnconfigure(4, minsize=100)
window.columnconfigure(5, minsize=100)
window.columnconfigure(6, minsize=100)
window.columnconfigure(7, minsize=50)

window.rowconfigure(0, minsize=30)
window.rowconfigure(1, minsize=30)
window.rowconfigure(2, minsize=30)
window.rowconfigure(3, minsize=30)
window.rowconfigure(4, minsize=30)
window.rowconfigure(5, minsize=30)
window.rowconfigure(6, minsize=30)
window.rowconfigure(7, minsize=30)

"""
    Titles
"""
title = Label(window, text="Calculadora de Op amps", bg="#595358", fg="white")
title.config(font=("Arial", 20))
title.grid(column=0, row=0, columnspan=8, sticky="we")

"""
    Input
"""
vin = Entry(window, font="Arial 20")
vin.grid(row=1, column=4)
vin_title = Label(window, text="Vin", bg="#3891A6", fg="BLACK")
vin_title.config(font=("Arial", TITLE_SIZE))
vin_title.grid(row=1, column=3)

"""
    RadioButton
"""

opa = StringVar(window, True)

# Dictionary to create multiple buttons
radio = {"Opamp Amplificador Inversor": True,
         "Opamp Amplificador no inversor": False,
         }
# Loop is used to create multiple Radiobuttons
# rather than creating each button separately
for (text, value) in radio.items():
    Radiobutton(window, text=text, variable=opa, value=value).grid(columnspan=2, pady=(1, 0))

"""
    Buttons
"""
calculate_button = Button(window, text="Calcular", padx=20, pady=10, command=calculate, bg="#99c24d")
calculate_button.config(font=("Arial", 15))
calculate_button.grid(row=2, column=6)

"""
    Results
"""
result_funcion = Label(window)
result_funcion.grid(row=2, column=4)
rsf_title = Label(window, text="Función", bg="#3891A6", fg="BLACK")
rsf_title.config(font=("Arial", TITLE_SIZE))
rsf_title.grid(row=2, column=3)

result_ev = Label(window)
result_ev.grid(row=3, column=4)
rsev_title = Label(window, text="Vout", bg="#3891A6", fg="BLACK")
rsev_title.config(font=("Arial", TITLE_SIZE))
rsev_title.grid(row=3, column=3)

result_err = Label(window)
result_err.grid(row=4, column=4)
rserr_title = Label(window, text="Error (%)", bg="#3891A6", fg="BLACK")
rserr_title.config(font=("Arial", TITLE_SIZE))
rserr_title.grid(row=4, column=3)

"""
    Circuit picture
"""

photo = PhotoImage(file=r"./OPAMPS.png")
image = Button(window, image=photo, padx=0, pady=0)
image.config(height=200, width=500)
image.grid(row=6, column=1, columnspan=5, pady=(0, 20))

"""
    Window display
"""
window.geometry("980x500")
window.config(bg="#B2CEDE")
window.mainloop()


# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab

""" A Python logging library with super powers """

import sys
import textwrap

from os import getcwd, path as p
from argparse import RawTextHelpFormatter, ArgumentParser
from pickle import dump, load
from io import open
from functools import partial, lru_cache
from signal import signal, SIGINT

import pygogo as gogo

from dateutil.parser import parse as parse_date
from chakula import tail, __version__
from chakula.formatter import PLACEHOLDERS, Formatter

try:
    from redisworks import Root as OldRoot
except ImportError:
    OldRoot = object

DEF_TIME_FMT = '%Y/%m/%d %H:%M:%S'
DEF_INTERVAL = '300s'
CURDIR = p.basename(getcwd())
LOGFILE = '%s.log' % CURDIR
FIELDS = sorted(PLACEHOLDERS)

logger = gogo.Gogo(__name__, monolog=True).logger

examples = r'''
Format specifiers must have one the following forms:
  %%(placeholder)[flags]s
  {placeholder:flags}

Examples:
  %(prog)s <url>
  echo '<url>' | %(prog)s --reverse
  %(prog)s -s pubdate -s title -s author <url1> <url2> <url3>
  %(prog)s --interval 60s --newer "2011/12/20 23:50:12" <url>
  %(prog)s --format '%%(timestamp)-30s %%(title)s\n' <url>
  %(prog)s --format '%%(title)s was written on %%(pubdate)s\n' <url>
  %(prog)s --format '{timestamp:<30} {title} {author}\n' <url>
  %(prog)s --format '{timestamp:<20} {pubdate:^30} {author:>30}\n' <url>
  %(prog)s --time-format '%%Y/%%m/%%d %%H:%%M:%%S' <url>
  %(prog)s --time-format 'Day of the year: %%j Month: %%b' <url>

Useful flags in this context are:
  %%(placeholder)-10s - left align and pad
  %%(placeholder)10s  - right align and pad
  {placeholder:<10}  - left align and pad
  {placeholder:>10}  - right align and pad
  {placeholder:^10}  - center align and pad
'''

available = textwrap.wrap('Available fields: {}'.format(', '.join(FIELDS)))
epilog = [textwrap.dedent(examples)] + available

def timespec(value):
    """Parse the 'timespec' option:

    >>> timespec(1)
    1
    >>> timespec('5m')
    300
    >>> timespec('1h')
    3600
    """

    try:
        return int(value)
    except ValueError:
        multiply = {'s': 1, 'm': 60, 'h': 3600}
        suffix = value[-1]

        msg = 'invalid timespec value {} - hint: 60, 60s, 1m, 1h'

        if suffix in multiply:
            try:
                v = int(value[:-1])
                return v * multiply[suffix]
            except ValueError:
                ValueError(msg.format(value))
        else:
            raise ValueError(msg.format(value))

parser = ArgumentParser(
    description='description: Tails 1 or more rss feeds',
    prog='chakula',
    usage='%(prog)s [options] <url> [<url> ...]',
    formatter_class=RawTextHelpFormatter,
    epilog='\n'.join(epilog),
)

parser.add_argument(
    dest='urls', nargs='*', default=[sys.stdin],
    help='The urls to tail (default: reads from stdin).')

i_help = 'Number of seconds between polling (default: {}).'

parser.add_argument(
    '-i', '--interval', action='store', help=i_help.format(DEF_INTERVAL),
    type=timespec, default=DEF_INTERVAL)

parser.add_argument(
    '-N', '--iterations', action='store', type=int,
    help='Number of times to poll before quiting (default: inf).')

parser.add_argument(
    '-I', '--initial', action='store', type=int,
    help='Number of entries to show (default: all)')

parser.add_argument(
    '-n', '--newer', metavar='DATE', action='store',
    help='Date by which entries should be newer than')

parser.add_argument(
    '-s', '--show', metavar='FIELD', choices=FIELDS, action='append',
    help='Entry field to display (default: title).', default=[])

t_help = "The date/time format (default: 'YYYY/MM/DD HH:MM:SS')."

parser.add_argument(
    '-t', '--time-format', metavar='FORMAT', action='store',
    default=DEF_TIME_FMT, help=t_help)

parser.add_argument(
    '-F', '--format', action='store',
    help='The output format (overrides other format options).')

parser.add_argument(
    '-c', '--cache', action='store',
    help='File path to store feed information across multiple runs.')

parser.add_argument(
    '-r', '--reverse', action='store_true',
    help='Show entries in reverse order.')

parser.add_argument(
    '-f', '--fail', action='store_true', help='Exit on error.')

parser.add_argument(
    '-u', '--unique', action='store_true', help='Skip duplicate entries.')

parser.add_argument(
    '-H', '--heading', action='store_true', help='Show field headings.')

parser.add_argument(
    '-v', '--version', help="Show version and exit.", action='store_true',
    default=False)

parser.add_argument(
    '-V', '--verbose', help='Increase output verbosity.', action='store_true',
    default=False)

class Root(OldRoot):
    def __init__(self, conn, return_object=True, *args, **kwargs):
        super(Root, self).__init__(*args, **kwargs)
        self.red = conn
        self.return_object = return_object
        self.setup()

get_root = lru_cache(maxsize=8)(lambda conn: Root(conn))

def sigint_handler(signal=None, frame=None):
    logger.info('\nquitting...\n')
    sys.exit(0)

def update_cache(path, extra, redis=False):
    if redis:
        root = get_root(path)

        try:
            items = extra.__dict__['_registry'].evaluated_items
        except AttributeError:
            root.extra = extra
        else:
            root.extra = items['root.extra']

        return root.red
    else:
        with open(path, 'wb') as f:
            dump(extra, f)

        return path

def load_extra(path, redis=False):
    if redis:
        root = get_root(path)
        extra = root.extra or {}

        for k, v in extra.items():
            v['updated'] = tuple(v.get('updated') or [])
            v['modified'] = tuple(v.get('modified') or [])
    else:
        try:
            with open(path, 'rb') as f:
                extra = load(f)
        except FileNotFoundError:
            extra = {}

    return extra

def run():
    """CLI runner"""
    args = parser.parse_args()
    kwargs = {'monolog': True, 'verbose': args.verbose}
    logger = gogo.Gogo(__name__, **kwargs).get_logger('run')
    signal(SIGINT, sigint_handler)

    if args.version:
        logger.info('chakula v%s' % __version__)
        exit(0)

    if args.newer:
        newer = parse_date(args.newer).timetuple()
        logger.debug('showing entries newer than %s', newer)
    else:
        newer = None

    if args.format:
        fmt = args.format.replace('\\n', '\n')
        formatter = Formatter(fmt, args.time_format)
    else:
        show = args.show or ['title']
        pargs = (show, args.time_format, args.heading)
        formatter = Formatter.from_fields(*pargs)

    logger.debug('using format: %r', formatter.fmt)
    logger.debug('using time format: %r', formatter.time_fmt)

    info = {
        'seen': set() if args.unique else None, 'newer': newer,
        'reverse': args.reverse, 'iterations': args.iterations,
        'interval': args.interval, 'formatter': formatter,
        'initial': args.initial, 'logger': logger, 'fail': args.fail}

    first = args.urls[0]

    if hasattr(first, 'isatty') and first.isatty():  # called with no args
        # This doesn't work for scripttest though
        parser.print_help()
        sys.exit(0)
    elif hasattr(first, 'read'):  # piped into sdtin
        urls = first.read().splitlines()
    else:
        urls = args.urls

    if args.cache:
        extra = load_extra(args.cache)
        info['tail_handler'] = partial(update_cache, args.cache)
    else:
        extra = {}

    tail(urls, extra=extra, **info)
    sys.exit(0)

if __name__ == '__main__':
    run()


import os
import sys
from lockdoors import main
from lockdoors import sanitize
from lockdoors import shrts
from pathlib import Path
from datetime import datetime
from time import sleep
#VAR
yes = set(['yes', 'y', 'ye', 'Y'])
no = set(['no', 'n', 'nop', 'N'])
cwd = os.getcwd()
null = ""
###Cheatsheets
def revsh():
    shrts.clscprilo()
    print("\033[91mHere is the list of the files :\033[90m")
    print("\033[92m")
    os.system("     find " + shrts.getinstalldir() + "/REVERSE/CHEATSHEETS/ -type f")
    print("\033[90m")
    shrts.okrev()
#Tools
def radar2():
    radar2.title = "Radar 2 : unix-like reverse engineering framework"
    tool_dir = "/REVERSE/Tools/radar2"
    shrts.prilogspc()
    os.system("git clone https://github.com/radare/radare2.git " + shrts.getinstalldir() + tool_dir + null)
    shrts.clscprilo()
    print("\033[92m           Radar2 Downlaoded successfully \033[90m")
    shrts.spc()
    print("\033[92m           Check " + shrts.getinstalldir() + tool_dir +" Folder\033[90m")
    shrts.okrev()
def virustotal():
    virustotal.title = "VirusTotal tools"
    tool_dir = "/REVERSE/Tools/virustotal"
    if os.path.exists('/usr/local/bin/virustotal'):
        shrts.prilogspc()
        os.system("git clone https://github.com/botherder/virustotal.git " + shrts.getinstalldir() + tool_dir + null)
        shrts.prilogspc()
        print("\033[92m           " + virustotal.title + "\033[90m")
        shrts.spc()
        key = sanitize.bash_escape_restrictor(input("\033[92mEnter the Virtustoal Api  ? : \033[90m"))
        outp = sanitize.bash_escape_restrictor(input("\033[92mEnter directory containing files to scan ? : \033[90m"))
        os.system("python2 " + shrts.getinstalldir() + tool_dir + "/vt.py --key "+key+" " +outp)
        shrts.okrev()
    else:
        shrts.prilogspc()
        print("\033[92m           " + virustotal.title + "\033[90m")
        shrts.spc()
        print("\033[91mDownloading ...\033[0m")
        shrts.spc()
        os.system("git clone  https://github.com/botherder/virustotal.git  " + shrts.getinstalldir() + tool_dir + null)
        shrts.prilogspc()
        print("\033[92m           " + virustotal.title + "\033[90m")
        shrts.spc()
        shrts.prilogspc()
        print("\033[91mInstalling ...\033[0m.")
        shrts.spc()
        os.system("""echo "#!/bin/bash" > /usr/local/bin/virustotal""")
        os.system("""echo "#Dev : Sofiane Hamlaoui" >> /usr/local/bin/virustotal""")
        os.system("echo python2 " + shrts.getinstalldir() + tool_dir + "/vt.py >> /usr/local/bin/virustotal")
        os.system("chmod +x /usr/local/bin/virustotal")
        print(("You can now use " + "\033[91m" + virustotal.title + "\033[90m" + " from Lockdoor [\033[92m Lockdoor \033[90m ]" ))
        shrts.okrev()
def miasm():
    miasm.title = "miasm : Reverse engineering framework"
    tool_dir = "/REVERSE/Tools/miasm"
    shrts.prilogspc()
    os.system("git clone https://github.com/cea-sec/miasm.git " + shrts.getinstalldir() + tool_dir + null)
    shrts.prilogspc()
    os.system("cd " +shrts.getinstalldir() + tool_dir + " && python2 setup.py build")
    os.system("cd " +shrts.getinstalldir() + tool_dir + " && python2 setup.py install")
    shrts.spc()
    print("\033[92m           Miasm Downlaoded successfully \033[90m")
    shrts.spc()
    print("\033[92m           Check " + shrts.getinstalldir() + tool_dir +" Folder\033[90m")
    shrts.okrev()
def mirror():
    mirror.title = "mirror : reverses the bytes of a file"
    tool_dir = "/REVERSE/Tools/mirror"
    shrts.prilogspc()
    os.system("git clone https://github.com/guelfoweb/mirror.git " + shrts.getinstalldir() + tool_dir + null)
    shrts.clr()
    shrts.prilogspc()
    print("\033[92m           Mirror Downlaoded successfully \033[90m")
    shrts.spc()
    print("\033[92m           Check " + shrts.getinstalldir() + tool_dir +" Folder\033[90m")
    shrts.okrev()
def Dnspy():
    Dnspy.title = "Dnspy : reverses the bytes of a file"
    tool_dir = "/REVERSE/Tools/Dnspy"
    shrts.prilogspc()
    os.system("git clone https://github.com/0xd4d/dnSpy.git " + shrts.getinstalldir() + tool_dir + null)
    shrts.clr()
    shrts.prilogspc()
    print("\033[92m           Dnspy Downlaoded successfully \033[90m")
    shrts.spc()
    print("\033[92m           Check " + shrts.getinstalldir() + tool_dir +" Folder\033[90m")
    shrts.okrev()
def angrio():
    angrio.title = "angrio : a python framework for analyzing binaries"
    tool_dir = "/REVERSE/Tools/angrio"
    shrts.prilogspc()
    print("\033[92m           Installing \033[90m")
    shrts.spc()
    os.system("pip install angr ")
    shrts.clr()
    shrts.prilogspc()
    print("\033[92m           Dnspy Downlaoded successfully \033[90m")
    shrts.spc()
    print("\033[92m           Check Angr.io docs to learn more about the tool \033[90m")
    print("\033[92m           https://github.com/angr/angr-doc \033[90m")
    shrts.okrev()
def dllrunner():
    dllrunner.title = "Dllrunner : a smart DLL execution script for malware analysis"
    tool_dir = "/REVERSE/Tools/dllrunner"
    shrts.prilogspc()
    os.system("git clone https://github.com/Neo23x0/DLLRunner " + shrts.getinstalldir() + tool_dir + null)
    shrts.clr()
    shrts.prilogspc()
    print("\033[92m           Dllrunner Downlaoded successfully \033[90m")
    shrts.spc()
    print("\033[92m           Check "+ shrts.getinstalldir() + tool_dir + " Folder\033[90m")
    shrts.okrev()
def yara():
    yara.title = "YARA :  a tool to identify and classify malwares "
    tool_dir = "/REVERSE/Tools/yara"
    shrts.prilogspc()
    print("\033[92m           Installing \033[90m")
    shrts.spc()
    os.system("pip install yara-python")
    shrts.clr()
    shrts.prilogspc()
    print("\033[92m           YARA Downlaoded successfully \033[90m")
    shrts.spc()
    print("\033[92m           Check YARA Docs to learn more about the tool\033[90m")
    print("\033[92m           https://yara.readthedocs.io/en/latest/\033[90m")
    shrts.okrev()
#Menu
def menu():
    shrts.clscprilo()
    print("""\033[94m
    [ REVERSE ENGINEERING  ]

        Make A Choice :\033[90m
    \033[91m -[!]----- Tools ------[!]-\033[90m

            \033[93m1)  Radar2
            2)  Virustotal
            3)  Miasm
            4)  Mirror
            5)  Dnspy
            6)  Angrio
            7)  DLLRunner
            8)  Yara\033[90m
    \033[91m-[!]----- Cheatsheets ------[!]-\033[90m

        \033[93m    9) Reverse Engineering Cheatsheets\033[90m
    ------------------------
    \033[94mb)    Back to ROOT MENU
    q)    Leave  Lockdoor\033[94m
       """)
    choice = input("\033[92mLockdoor@ReverseEngineering~# \033[0m")
    os.system('clear')
    if choice == "1":
      radar2()
    elif choice == "2":
      virustotal()
    elif choice == "3":
      miasm()
    elif choice == "4":
      mirror()
    elif choice == "5":
      Dnspy()
    elif choice == "6":
      angrio()
    elif choice == "7":
      dllrunner()
    elif choice == "8":
      yara()
    elif choice == "9":
      revsh()
    elif choice == "b":
      main.menu()
    elif choice == "q":
        shrts.prilogspc()
        now = datetime.now()
        dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
        print("                 \033[91m-[!]- LOCKDOOR IS EXITING -[!]-\033[0m")
        shrts.spc()
        print("                 \033[91m-[!]- EXITING AT " + dt_string + " -[!]-\033[0m")
        sys.exit()
    elif choice == "":
      menu()
    else:
      menu()


# Tim Marder
# SoftDev1 pd06
# K#13 -- Echo Echo Echo
# 2018-09-28

from flask import Flask, render_template, request
app = Flask(__name__) #create instance of class Flask

@app.route("/")       #assign fxn to route
def hello_world():
    return render_template("home.html")

@app.route("/auth", methods = ["GET", "POST"])
def authenticate():
    print(app)
    print(request)
    print(request.args)
    print(request.headers)
    return render_template("auth.html",
                            first = request.form['first'],
                            last = request.form['last'],
                            request = request.method)

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


#!/usr/bin/env python
# coding: utf-8

# In[1]:

import gzip
from collections import defaultdict
import math
import scipy.optimize
import numpy
import string
import random
from sklearn import linear_model
import sklearn

# In[2]:

# This will suppress any warnings, comment out if you'd like to preserve them
import warnings
warnings.filterwarnings("ignore")

# In[3]:

# Check formatting of submissions
def assertFloat(x):
    assert type(float(x)) == float

def assertFloatList(items, N):
    assert len(items) == N
    assert [type(float(x)) for x in items] == [float]*N

# In[4]:

answers = {}

# In[5]:

f = open("spoilers.json.gz", 'r')

# In[6]:

dataset = []
for l in f:
    d = eval(l)
    dataset.append(d)

# In[7]:

f.close()

# In[8]:

# A few utility data structures
reviewsPerUser = defaultdict(list)
reviewsPerItem = defaultdict(list)

for d in dataset:
    u,i = d['user_id'],d['book_id']
    reviewsPerUser[u].append(d)
    reviewsPerItem[i].append(d)

# Sort reviews per user by timestamp
for u in reviewsPerUser:
    reviewsPerUser[u].sort(key=lambda x: x['timestamp'])

# Same for reviews per item
for i in reviewsPerItem:
    reviewsPerItem[i].sort(key=lambda x: x['timestamp'])

# In[9]:

# E.g. reviews for this user are sorted from earliest to most recent
[d['timestamp'] for d in reviewsPerUser['b0d7e561ca59e313b728dc30a5b1862e']]

# In[10]:

### 1

# In[11]:

def MSE(y, ypred):
    return sum([(a-b)**2 for (a,b) in zip(y,ypred)]) / len(y)

# In[12]:

# (a)
y = []
y_pred = []
for u in reviewsPerUser:
    cur = []
    reviews = reviewsPerUser[u]
    for i in range(0, len(reviews) - 1):
        cur.append(reviews[i]['rating'])
    if len(cur) == 0:
        continue
    y_pred.append(sum(cur)/len(cur))
    y.append(reviews[-1]['rating'])
answers['Q1a'] = MSE(y, y_pred)
assertFloat(answers['Q1a'])

# In[13]:

# (b)
y = []
y_pred = []
for u in reviewsPerItem:
    cur = []
    reviews = reviewsPerItem[u]
    for i in range(0, len(reviews) - 1):
        cur.append(reviews[i]['rating'])
    if len(cur) == 0:
        continue
    y_pred.append(sum(cur)/len(cur))
    y.append(reviews[-1]['rating'])
answers['Q1b'] = MSE(y, y_pred)
assertFloat(answers['Q1b'])

# In[14]:

### 2
answers['Q2'] = []

for N in [1,2,3]:
    y = []
    y_pred = []
    for u in reviewsPerUser:
        cur = []
        reviews = reviewsPerUser[u]
        for i in range(0, len(reviews) - 1):
            cur.append(reviews[i]['rating'])
        if len(cur) == 0:
            continue

        if len(cur) < N:
            cur_new = cur

        if len(cur) >= N:
            cur_new = cur[-N:]

        y_pred.append(sum(cur_new)/len(cur_new))
        y.append(reviews[-1]['rating'])

    answers['Q2'].append(MSE(y,y_pred))

# In[15]:

assertFloatList(answers['Q2'], 3)

# In[16]:

answers

# In[17]:

### 3a

# In[18]:

def feature3(N, u): # For a user u and a window size of N

    cur = []
    reviews = reviewsPerUser[u]
    for i in range(0, len(reviews) - 1):
        cur.append(reviews[i]['rating'])

    feat = [1]
    for n in range(1, N + 1):
        feat.append(cur[-n])

    return feat

# In[19]:

answers['Q3a'] = [feature3(2,dataset[0]['user_id']), feature3(3,dataset[0]['user_id'])]

# In[20]:

assert len(answers['Q3a']) == 2
assert len(answers['Q3a'][0]) == 3
assert len(answers['Q3a'][1]) == 4

# In[21]:

### 3b
answers['Q3b'] = []
def feat(N, u):
    feat = [1]
    data = reviewsPerUser[u]
    for d in data[-N-1:-1]:
        feat.append(d['rating'])
    return feat

for N in [1,2,3]:
    X = []
    y = []
    for u,data in reviewsPerUser.items():
        if len(data) <= N:
            continue
        else:
            X.append(feat(N,u))
            y.append(data[-1]['rating'])
    model = sklearn.linear_model.LinearRegression(fit_intercept=False)
    model.fit(X, y)
    y_pred = model.predict(X)
    mse = MSE(y, y_pred)
    answers['Q3b'].append(mse)
assertFloatList(answers['Q3b'], 3)
answers

# In[22]:

### 4a
globalAverage = [d['rating'] for d in dataset]
globalAverage = sum(globalAverage) / len(globalAverage)

def featureMeanValue(N, u): # For a user u and a window size of N
    feat = [1]
    data = reviewsPerUser[u]
    if len(data) < N + 1:
        if len(data) < 2:
            for j in range(N):
                feat.append(globalAverage)
        elif len(data) >= 2:
            rate = [review['rating'] for review in data[:-1]]
            avg = sum(rate)/len(rate)
            for i in range(len(data)-1):
                feat.append(data[-i-2]['rating'])
            for i in range(N-len(data)+1):
                feat.append(avg)
    else:
        for i in range(N):
            feat.append(data[-i-2]['rating'])
    return feat

def featureMissingValue(N, u):
    feat = [1]
    data = reviewsPerUser[u]

    if len(data) < N + 1:
        if len(data) < 2:
            for j in range(N):
                feat.append(1)
                feat.append(0)
        elif len(data) >= 2:
            for i in range(len(data)-1):
                feat.append(0)
                feat.append(data[- i - 2]['rating'])
            for i in range(N + 1-len(data)):
                feat.append(1)
                feat.append(0)
    else:
        for i in range(N):
            feat.append(0)
            feat.append(data[-i-2]['rating'])
    return feat

answers['Q4a'] = [featureMeanValue(10, dataset[0]['user_id']), featureMissingValue(10, dataset[0]['user_id'])]

answers

# In[23]:

answers['Q4b'] = []

for featFunc in [featureMeanValue, featureMissingValue]:
    X = []
    y = []
    for user,rating in reviewsPerUser.items():
        if len(rating) < 1:
            continue
        else:
            X.append(featFunc(10,user))
            y.append(rating[-1]['rating'])

    model = linear_model.LinearRegression()
    model.fit(X,y)
    y_pred = model.predict(X)
    mse = MSE(y, y_pred)
    answers['Q4b'].append(mse)

# In[24]:

answers['Q4b']

# In[25]:

### 5
#(a)
def feature5(sentence):
    feat = [1]
    feat.append(len(sentence))
    feat.append(sentence.count('!')) # Quadratic term
    feat.append(sum(i.isupper() for i in sentence))
    return feat

X = []
y = []

for d in dataset:
    for spoiler,sentence in d['review_sentences']:
        X.append(feature5(sentence))
        y.append(spoiler)

# In[26]:

answers['Q5a'] = X[0]

# In[27]:

###5(b)
mod = sklearn.linear_model.LogisticRegression( class_weight='balanced', C=1)
mod.fit(X,y)
predictions = mod.predict(X)

TP = sum([(p and l) for (p,l) in zip(predictions, y)])
FP = sum([(p and not l) for (p,l) in zip(predictions, y)])
TN = sum([(not p and not l) for (p,l) in zip(predictions, y)])
FN = sum([(not p and l) for (p,l) in zip(predictions, y)])
TPR = TP / (TP + FN)
TNR = TN / (TN + FP)
BER = 1 - 1/2 * (TPR + TNR)
answers['Q5b'] = [TP, TN, FP, FN, BER]

# In[28]:

assert len(answers['Q5a']) == 4
assertFloatList(answers['Q5b'], 5)

# In[29]:

### 6
def feature6(review):
    review = review['review_sentences']
    feat = [1]
    for i in range(0, 5):
        feat.append(review[i][0])
    feat.append(len(review[5][1]))
    feat.append(review[5].count('!')) # Quadratic term
    feat.append(sum(i.isupper() for i in review[5][1]))

    return feat

# In[30]:

y = []
X = []

for d in dataset:
    sentences = d['review_sentences']
    if len(sentences) < 6: continue
    X.append(feature6(d))
    y.append(sentences[5][0])

# In[31]:

answers['Q6a'] = feature6(dataset[0])
answers

# In[32]:

answers['Q6a'] = X[0]
answers

# In[33]:

mod = sklearn.linear_model.LogisticRegression(class_weight='balanced', C = 1)
mod.fit(X,y)
predictions = mod.predict(X)

TP = sum([(p and l) for (p,l) in zip(predictions, y)])
FP = sum([(p and not l) for (p,l) in zip(predictions, y)])
TN = sum([(not p and not l) for (p,l) in zip(predictions, y)])
FN = sum([(not p and l) for (p,l) in zip(predictions, y)])
TPR = TP / (TP + FN)
TNR = TN / (TN + FP)
BER = 1 - 1/2 * (TPR + TNR)

answers['Q6b'] = BER

# In[34]:

assert len(answers['Q6a']) == 9
assertFloat(answers['Q6b'])
answers

# In[35]:

### 7

# In[36]:

# 50/25/25% train/valid/test split
Xtrain, Xvalid, Xtest = X[:len(X)//2], X[len(X)//2:(3*len(X))//4], X[(3*len(X))//4:]
ytrain, yvalid, ytest = y[:len(X)//2], y[len(X)//2:(3*len(X))//4], y[(3*len(X))//4:]

# In[37]:

def pipeline(reg, bers, BER_test):
    mod = linear_model.LogisticRegression(class_weight='balanced', C=reg)

    # 50/25/25% train/valid/test split
    Xtrain, Xvalid, Xtest = X[:len(X)//2], X[len(X)//2:(3*len(X))//4], X[(3*len(X))//4:]
    ytrain, yvalid, ytest = y[:len(X)//2], y[len(X)//2:(3*len(X))//4], y[(3*len(X))//4:]

    mod.fit(Xtrain,ytrain)
    ypredValid = mod.predict(Xvalid)
    ypredTest = mod.predict(Xtest)

    # validation

    TP = sum([(a and b) for (a,b) in zip(yvalid, ypredValid)])
    TN = sum([(not a and not b) for (a,b) in zip(yvalid, ypredValid)])
    FP = sum([(not a and b) for (a,b) in zip(yvalid, ypredValid)])
    FN = sum([(a and not b) for (a,b) in zip(yvalid, ypredValid)])

    TPR = TP / (TP + FN)
    TNR = TN / (TN + FP)

    BER = 1 - 0.5*(TPR + TNR)

    print("C = " + str(reg) + "; validation BER = " + str(BER))
    bers = bers.append(BER)

     # test

    TP = sum([(a and b) for (a,b) in zip(ytest, ypredTest)])
    TN = sum([(not a and not b) for (a,b) in zip(ytest, ypredTest)])
    FP = sum([(not a and b) for (a,b) in zip(ytest, ypredTest)])
    FN = sum([(a and not b) for (a,b) in zip(ytest, ypredTest)])

    TPR = TP / (TP + FN)
    TNR = TN / (TN + FP)

    BER = 1 - 0.5*(TPR + TNR)

    BER_test = BER_test.append(BER)

    return mod

# In[38]:

bers = []
BER_test = []
for c in [0.01, 0.1, 1, 10, 100]:
    pipeline(c, bers, BER_test)
bers
BER_test

# In[39]:

bestC = 0.1
ber = 0.21299572460563176
answers['Q7'] = bers + [bestC] + [ber]
assertFloatList(answers['Q7'], 7)
answers

# In[40]:

### 8
def Jaccard(s1, s2):
    numer = len(s1.intersection(s2))
    denom = len(s1.union(s2))
    if denom == 0:
        return 0
    return numer / denom

# In[41]:

# 75/25% train/test split
dataTrain = dataset[:15000]
dataTest = dataset[15000:]

# In[42]:

# A few utilities

itemAverages = defaultdict(list)
ratingMean = []

for d in dataTrain:
    itemAverages[d['book_id']].append(d['rating'])
    ratingMean.append(d['rating'])

for i in itemAverages:
    itemAverages[i] = sum(itemAverages[i]) / len(itemAverages[i])

ratingMean = sum(ratingMean) / len(ratingMean)

# In[43]:

reviewsPerUser = defaultdict(list)
usersPerItem = defaultdict(set)

for d in dataTrain:
    u,i = d['user_id'], d['book_id']
    reviewsPerUser[u].append(d)
    usersPerItem[i].add(u)

# In[44]:

# From my HW2 solution, welcome to reuse
def predictRating(user,item):
    ratings = []
    similarities = []
    for d in reviewsPerUser[user]:
        i2 = d['book_id']
        if i2 == item: continue
        ratings.append(d['rating'] - itemAverages[i2])
        similarities.append(Jaccard(usersPerItem[item],usersPerItem[i2]))
    if (sum(similarities) > 0):
        weightedRatings = [(x*y) for x,y in zip(ratings,similarities)]
        return itemAverages[item] + sum(weightedRatings) / sum(similarities)
    else:
        # User hasn't rated any similar items
        if item in itemAverages:
            return itemAverages[item]
        else:
            return ratingMean

# In[45]:

predictions = [predictRating(d['user_id'], d['book_id']) for d in dataTest]
labels = [d['rating'] for d in dataTest]

# In[46]:

answers["Q8"] = MSE(predictions, labels)
assertFloat(answers["Q8"])

# In[ ]:

# In[56]:

### 9
item = [d['book_id'] for d in dataTrain]
data0, rating0 =  [], []

for d in dataTest:
    num = item.count(d['book_id'])
    if num == 0:
        data0.append([d['user_id'], d['book_id']])
        rating0.append(d['rating'])

pred0 = [predictRating(u, i) for u, i in data0]

mse0 = MSE(pred0, rating0)
mse0

# In[57]:

data1, rating1 = [],[]

for d in dataTest:
    num = item.count(d['book_id'])

    if 1 <= num <= 5:
        data1.append([d['user_id'], d['book_id']])
        rating1.append(d['rating'])

pred1 = [predictRating(u, i) for u, i in data1]

mse1to5= MSE(pred1, rating1)
mse1to5

# In[58]:

data5, rating5 = [], []

for d in dataTest:
    num = item.count(d['book_id'])

    if num > 5:
        data5.append([d['user_id'], d['book_id']])
        rating5.append(d['rating'])

pred5 = [predictRating(u, i) for u, i in data5]

mse5 = MSE(pred5, rating5)
mse5

# In[ ]:

# In[50]:

answers["Q9"] = [mse0, mse1to5, mse5]
assertFloatList(answers["Q9"], 3)

answers

# In[51]:

### 10

# In[52]:

userAverages = defaultdict(list)

for d in dataTrain:
    userAverages[d['user_id']].append(d['rating'])

for i in userAverages:
    userAverages[i] = sum(userAverages[i]) / len(userAverages[i])

def predictRating(user,item):
    ratings = []
    similarities = []
    for d in reviewsPerUser[user]:
        i2 = d['book_id']
        if i2 == item: continue
        ratings.append(d['rating'] - itemAverages[i2])
        similarities.append(Jaccard(usersPerItem[item],usersPerItem[i2]))
    if (sum(similarities) > 0):
        weightedRatings = [(x*y) for x,y in zip(ratings,similarities)]
        return itemAverages[item] + sum(weightedRatings) / sum(similarities)
    else:
        # User hasn't rated any similar items
        if item in itemAverages:
            return itemAverages[item]
        else:
            # return RatingMean
            if user in userAverages:
                return userAverages[user]
            else:
                return ratingMean

item = [d['book_id'] for d in dataTrain]
data10, rating10 = [], []

for d in dataTest:
    num = item.count(d['book_id'])
    if num == 0:
        data10.append([d['user_id'], d['book_id']])
        rating10.append(d['rating'])

pred10 = [predictRating(u, i) for u, i in data10]

mse10 = MSE(pred10, rating10)
mse10

# In[59]:

answers["Q10"] = ("To improve the prediction function for unseen items, we can modify the predictRating function. Since previously the predictRating only use itemAverages for prediction function, we can add the userAverage to specify the condition and make mse smaller, inside of just categorize data into ratingMean. We can see that the mse become smaller for unseen data.", mse10)
assert type(answers["Q10"][0]) == str
assertFloat(answers["Q10"][1])

# In[60]:

answers

# In[55]:

f = open("answers_midterm.txt", 'w')
f.write(str(answers) + '\n')
f.close()


#!/usr/bin/python
# disk monitor

import logging as l
l.basicConfig(filename='disk_log.txt',filemode='a',level=l.DEBUG,
			  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
			  datefmt='%c')

# modes
# r -> read -> you can only read the file.
# a -> append -> you can only append the contents to the file.
# w -> write -> you can write to the file.
#   -> if you dont have a file a new file will be created.
#   -> if you have a file with data,the file gets truncated to zero.

disk_size = int(raw_input("please enter  your disk size:"))

if disk_size < 60:
	l.info("Your disk looks healthy at {}.".format(disk_size))
elif disk_size < 80:
	l.warning("Buddy!! your disk is getting fat - {}.".format(disk_size))
elif disk_size < 90:
	l.error("Buddy!! you disk is feeling sick - {}.".format(disk_size))
elif disk_size < 99:
	l.critical("Buddy!! you disk is dead - {}.".format(disk_size))


import Gmail_API_Lib
import Track_API_Lib
import Slack_API_Lib
import importlib
import json
import csv
import lovely_logger as log
import datetime
import time

late_checkin_alert_hour = 21
unclean_property_alert_hour = 14
regular_check_interval_minutes = 15
check_checkin_interval_minutes = 15
reload = 1#dummy variable to make the library re-save

#All times are in local time (EST)
late_checkins_time = datetime.datetime.now() - datetime.timedelta(days = 1) #Init
alert_checkins_time = datetime.datetime.now() - datetime.timedelta(days = 1) #Init
check_for_cleans_time = datetime.datetime.now() - datetime.timedelta(days = 1) #Init
set_cleans_time = datetime.datetime.now() - datetime.timedelta(days = 1) #Init
regular_interval_check_time = datetime.datetime.now() - datetime.timedelta(days = 1) #Init
check_checkin_time = datetime.datetime.now() - datetime.timedelta(days = 1) #Init
last_email_subject_read_file_cleaner = 'C:\\Users\\Bailey\\Documents\\Cozi\\Automations\\Track Automations\\email subject logs\\Last_Email_Read_Cleaner.txt'
last_email_subject_read_file_UMC = 'C:\\Users\\Bailey\\Documents\\Cozi\\Automations\\Track Automations\\email subject logs\\Last_Email_Read_UMC.txt' #universal Master Code
log.init('C:\\Users\\Bailey\\Documents\\Cozi\\Automations\\Track Automations\\Daily_Checks_Log')

try:
    while (1):
        today = datetime.datetime.now()
        current_hour = today.hour

        if (check_checkin_time + datetime.timedelta(minutes = check_checkin_interval_minutes) < today): #Updates todays reservations every 15 minutes.
            log.info("Updating todays checkins")
            todays_checkins = Track_API_Lib.get_todays_arrivals_units_and_names()
            check_checkin_time = today

        #General checks at regular intervals
        if (regular_interval_check_time + datetime.timedelta(minutes = regular_check_interval_minutes)) < today: #Check every hour for Universal Master Code
            log.info('Getting messages from Gmail')
            msg_info = Gmail_API_Lib.get_gmail_subjects_and_dates() #E-mail subjects and dates
            log.info('Got messages from Gmail')
            #Alert for Master Code usage
            log.info('Starting UMC Check')
            UMC_check = Gmail_API_Lib.check_universal_master_code(msg_info) #Checks to see if the universal master code. Sends a Slack notification if so.
            log.info('Completed UMC Check')
            log.info('Starting New Checkins check')
            new_checkins = Gmail_API_Lib.check_for_checkins(msg_info, todays_checkins) #Already strips non-PC properties and notifies CS team in Slack
            if (len(new_checkins) > 0):
                new_alerts = Gmail_API_Lib.alert_checkin()
                Track_API_Lib.note_checkins(new_checkins)
            regular_interval_check_time = today

        if (current_hour == 13 or current_hour == late_checkin_alert_hour): #check for late checkins at 12pm and 8pm CST (Check twice to ensure there aren't more than 500 messages in inbox)
            if ((late_checkins_time + datetime.timedelta(hours = 1)) <= today):
                log.info('Checking for late checkins')
                msg_info = Gmail_API_Lib.get_gmail_subjects_and_dates() #E-mail subjects and dates
                log.info('Got messages from Gmail')
                log.info('Getting todays checkins')
                todays_checkins = Track_API_Lib.get_todays_arrivals_units_and_names()
                log.info('Processing missing checkins')
                missing_checkins = Gmail_API_Lib.check_for_checkins(msg_info, todays_checkins) #Already strips non-PC properties
                log.info('Processing missing checkins')
                late_checkins_time = today #subtract an hour to ensure the execution time doesn't keep creeping up over time.
                if (current_hour == late_checkin_alert_hour and missing_checkins != None): #Alert for late checkins at 8pm CST. MUST BE SAME HOUR AS IF STATEMENT ABOVE OR THIS WONT TRIGGER
                    if ((alert_checkins_time + datetime.timedelta(hours = 1)) <= today):
                        log.info('Alerting for late checkins')
                        late_checkins = Gmail_API_Lib.report_late_checkins()
                        log.info('Sending Slack notifications')
                        Slack_API_Lib.send_guest_late_checkin_alert(late_checkins)
                        log.info('Posting notes to reservations in Track')
                        Track_API_Lib.note_late_checkins(late_checkins)
                        alert_checkins_time = today
                        log.info('Completed late checkins')

        if ((current_hour >= 7 and current_hour <= 21) or current_hour == 3): #Check between 7am EST and 8pm EST and again at 3am EST
            if ((check_for_cleans_time + datetime.timedelta(hours = 1)) <= today):  #Checks every hour. Need to keep file updated with properties that have PC locks
                log.info('Checking for cleaned properties')
                #Set cleaned property statuses in Track
                msg_info = Gmail_API_Lib.get_gmail_subjects_and_dates() #E-mail subjects and dates
                log.info('Got messages from Gmail')
                cleaned_units = Gmail_API_Lib.check_for_cleaners(msg_info) #Need to ensure Point Central has people properly labeled
                inspected_units = Gmail_API_Lib.check_for_inspectors(msg_info) #Figure out what to do with Inspected Units
                ready_units = Track_API_Lib.add_clean_and_inspected(cleaned_units, inspected_units)
                log.info("Updating clean properties")
                if (ready_units != None):
                    res = Track_API_Lib.set_unit_clean_status(ready_units, 1) #Sets units to clean. 1 sets status to clean
                log.info("Updating clean combo properties")
                res = Track_API_Lib.set_combo_properties_clean_status() #Sets combo properties to clean. Need to manually keep this list up to date. Is it necessary?
                log.info('Set unit statuses')
                check_for_cleans_time = today
            if (current_hour == unclean_property_alert_hour): #Check at ~3pm EST (2pm CST) and alert
                if ((set_cleans_time + datetime.timedelta(hours = 1)) < today):
                    #Alert for non-clean units
                    log.info('Checking for unclean properties to alert')
                    msg_info = Gmail_API_Lib.get_gmail_subjects_and_dates() #E-mail subjects and dates
                    log.info('Got messages from Gmail')
                    todays_checkins = Track_API_Lib.get_todays_arrivals_units_and_names()
                    check_for_clean = Gmail_API_Lib.remove_non_PC_properties(todays_checkins) #Removes non PC Properties from the clean check
                    unclean_units = Track_API_Lib.check_unclean_units(check_for_clean) #Need to cross reference the unit name as well
                    #Handle combo units based on what Track says
                    log.info('Sending Slack alerts if any')
                    for unit in unclean_units:
                        last_access = Gmail_API_Lib.last_cleaner(msg_info, unit['unit_name'])
                        res = Slack_API_Lib.send_slack_message('automated-alerts',"UNCLEAN CHECKIN POSSIBLE! " + last_access)
                    set_cleans_time = today
        time.sleep(60)

except Exception as e:
    Slack_API_Lib.send_slack_message("automation-errors", "Error with the Daily Checks code. Need to restart")
    print(e)

#Check Track for unit clean status, and set to Clean if a claner has been there. (For combo's, both units must be Clean, then Combo can be Clean)
#Check email subjects for owners, then verify it is used during an owner stay. If not...? How about if the unit is blocked? Still notify?


from collections import defaultdict
from github import Github

def get_git_skills(username):
    g = Github()
    user = g.get_user(username)

    tags = defaultdict()
    languages = defaultdict(int)

    for repo in user.get_repos():
        # new_repo_languages = repo.get_languages()
        # for lang in new_repo_languages:
        #     languages[lang] += new_repo_languages[lang]

        new_repo_topics = repo.get_topics()
        for topic in new_repo_topics:
            print (topic)

    print(languages)
    return sorted(languages.items(), key=lambda x: x[1], reverse=True)


# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

# useful for handling different item types with a single interface
from fanza.items import ImageItem
from fanza.common import download_image
from fanza.image.image_helper import handle_image_item

from scrapy.exceptions import DropItem
from scrapy import Spider

from time import sleep
from socket import timeout
from urllib.request import ProxyHandler, build_opener
from urllib.error import URLError, HTTPError
from os.path import isdir, isfile
from os import makedirs

class AvbookImagePipeline:
    def __init__(self) -> None:
        self.opener = None

    def open_spider(self, spider: Spider):
        img_download_proxy = spider.settings['IMAGE_DOWNLOAD_PROXY']
        self.opener = build_opener(ProxyHandler({'https': img_download_proxy, 'http': img_download_proxy}))
        self.img_fail = spider.settings['IMAGE_FAIL_FILE']
        self.failed = set()

    async def process_item(self, item, spider: Spider):
        if not isinstance(item, ImageItem):
            return item
        img_dir, img_des, prefix = handle_image_item(item, spider)
        if not isdir(img_dir):
            makedirs(img_dir)
        if not item.isUpdate and isfile(img_des):
            spider.logger.debug('already exist: %s %s', prefix, item.imageName)
            return
        retry = 0
        delay = 1
        retry_limit = spider.settings['RETRY_LIMIT']
        while True:
            try:
                download_image(self.opener, item.url, img_des)
                break
            except (URLError, HTTPError, timeout):
                if retry > retry_limit:
                    spider.logger.exception("download image error, url: %s", item.url)
                    if item.subDir not in self.failed:
                        self.failed.add(item.subDir)
                        with open(self.img_fail, 'w', encoding='utf-8') as f:
                            f.write(f'{item.subDir}\n')
                    raise DropItem(f'download error happend\titem: {item}')
                sleep(delay)
                retry += 1
                delay *= 2
                spider.logger.debug('retry download image: retry\t%s url\t%s', retry, item.url)
        spider.logger.info('save img:\t%s %s', prefix, item.imageName)

class SuccessResponsePipeline:
    def close_spider(self, spider: Spider):
        if spider.name != 'movie_detail' and spider.name != 'movie_image':
            return
        spider.logger.info('------------------------------------save failed------------------------------------')
        failed = spider.processed - spider.successed
        with open('failed.txt', 'w', encoding='utf-8') as f:
            for failed_id in failed:
                f.write(failed_id + '\n')


from preprocess import *
import os
import argparse
from csv import writer

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Process pcap file and integer data.")
    parser.add_argument("-pcap", nargs="+", help="The pcap file. Multiple pcaps can be added when separated by a space.")
    parser.add_argument("-protocol", help ="The application layer protocol (ex: HTTP)")
    args = parser.parse_args()

    columns=["src_ip", "dst_ip", "src_port", "dst_port", "t_proto", "dsfield", "ip_flags", "length", "d_proto", "payload"]

    output_prefix = os.getcwd() + "/output"
    if not os.path.exists(output_prefix):
        os.makedirs(output_prefix)
    filecount = 0
    ext = str(filecount) + ".csv"
    filename = (output_prefix + "/" + str(args.protocol))

    with open(filename + ext, "w", newline='') as my_csv:
        csv_writer = writer(my_csv)
        csv_writer.writerow(columns)

    total = 0
    oldtotal = 0
    for f in args.pcap:
        total += parsePacket(filename + ext, f, str(args.protocol))
        if (oldtotal + 100000 <= total):
            filecount += 1
            oldtotal = total
            ext = str(filecount) + ".csv"
            with open(filename + ext, "w", newline='') as my_csv:
                csv_writer = writer(my_csv)
                csv_writer.writerow(columns)

    print("Number of packets processed: %d" % total)


from utils import *

from fastapi import FastAPI, Query, Path, Body, Cookie, Header
from pydantic import BaseModel, Required, Field, HttpUrl

app = FastAPI()

@app.get('/')
def read_root():
    return {'Hello': 'World'}

# Examplos com path params
class ModelName(str, Enum):
    name1 = 'Phelipe'
    name2 = 'Marcos'
    name3 = 'Fran'

@app.get('/items/{item_id}')
def read_item(item_id: int, q: Union[str, None] = None):
    return {'item_id': item_id, 'q': q}

@app.get('/names/{name}')
def get_name(name: ModelName):
    response = {'model_name': name}
    if name.value == 'Phelipe':
        response.update({'message': 'Bad name.'})
    else:
        response.update({'Message': 'Cool name.'})

    return response

# Exemplo com query params
fake_items_db = [{'item_name': 'Foo'}, {'item_name': 'Bar'}, {'item_name': 'Baz'}]
_lmt = len(fake_items_db) - 1

@app.get('/items/')
async def read_item(skip: int = 0, limit: int = _lmt):
    return fake_items_db[skip : skip + limit]

# Exemplo com query param opcional
@app.get('/cats/')
async def get_cat(name: str = None):
    cats = [
        'zoe',
        'zulke',
        'zara',
        'miuda',
        'frajola',
        'cruel',
        'mafalda',
        'jade',
        'maria',
    ]
    if name:
        if name in cats:
            return {'Valor aleatorio': False, 'Nome do gato': name}

    return {'Valor aleatorio': True, 'Nome do gato': cats[random.randint(0, len(cats))]}

# Exemplo com Pydantic model

class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None

@app.post('/items_s/')
async def create_item(item: Item):
    if item.tax:
        price = item.price + item.price * item.tax
        item.price = price
    return item

"""
Examplo usando Query como valor padrão, validadores e sobrescrita de documentação

Old way - async def create_dog(name: str, age: int, description: Union[None, str] = None):

"""

class Dog(BaseModel):
    level: int
    name: str

@app.post('/dogs/')
async def create_dog(
    brothers: Union[List[Dog], None],
    age: int,
    name: str = Query(
        default=..., title='Nome', description='Esse é um nome', regex='^love'
    ),
    description: Union[None, str] = Query(
        default=None,
        title='Descrição',
        description='Essa é uma descrição',
        min_length=5,
        max_length=10,
        deprecated=True,
    ),
    hidden_query: Union[str, None] = Query(default=None, include_in_schema=False),
):
    dog = {'name': name, 'age': age, 'brothers': {}}
    if description:
        dog.update({'description': description})
    if brothers:
        list(map(lambda d: dog['brothers'].update({d.name: d.level}), brothers))

    return dog

"""
Exemplo usando Path pydantic
"""

@app.get('/memories/{person_id}')
def create_memories(
    *, person_id: int = Path(..., title='Uma pessoa existente.', gt=0, le=1000)
):
    people = {1: {'name': 'amourir'}, 2: {'name': 'joão'}}
    if person_id in people.keys():
        return people.get(person_id)

    return {}

"""
Exemplo usando Body e multiplos parâmetros
"""

class City(BaseModel):
    name: str
    country: int

@app.post('/cities/{country_id}')
def create_cities(
    *,
    country_id: int = Path(..., title='Id de um país existente.', gt=1, le=5),
    city: City = Body(..., embed=False),
    person_name: str = Body(..., regex='^mar', embed=False)
):
    countries = {1: 'Brazil', 2: 'Russia', 3: 'Senegal', 4: 'Marrocos', 5: 'Irã'}

    city_item = {'name': city.name}

    country_name = countries.get(country_id)

    city_item.update({'country': country_name})

    return city_item

"""
Exemplo usando Body Field
"""

class PersonalComputer(BaseModel):
    name: str = Field(
        regex='^pc', default=None, description='Nome do pc', max_length=10
    )
    serie: int = Field(gt=1, description='Passe uma série válida')

@app.put('/pcs/{pc_id}')
async def update_pc(
    *,
    pc_id: int = Path(..., description='Passe um válido.'),
    pc: PersonalComputer = Body(..., embed=True, description='Uma maquina')
):
    if pc_id // 2 == 0:
        return {'message': 'Inválido'}
    return pc

"""
Exemplo com subtipos como BaseModel
"""

class Image(BaseModel):
    url: HttpUrl
    size: int

class Product(BaseModel):
    name: str = Field(..., example='Produto base')
    price: float
    images: list[Union[Image, None]] = None

class Store(BaseModel):
    products: Union[list[Product], None]
    name: str = Field(..., description='Nome da loja')

    class Config:
        schema_extra = {
            'example': {
                'products': [
                    {
                        'name': 'Computador',
                        'price': 12.4,
                        'images': [{'url': 'http://test.com', 'size': 1}],
                    }
                ],
                'name': 'Loja dos fundos',
            }
        }

@app.post('/products/')
def create_products(store: Store = Body(..., embed=True)):
    return store

@app.get('/products/{product_id}')
def retrive_products(*, product_id: UUID = Path(..., description='Produto existente')):
    return {}

"""
Exemplo com Cookie e Header
"""

@app.get('/params/')
def request_params(
    user_agent: str = Header(default=None),
    ads_id: Union[str, None] = Cookie(default=None),
):
    return {'cookie': ads_id, 'user_agent': user_agent}


import requests
from bs4 import BeautifulSoup

def extract_teok_jobs(keyword):
    results = []
    url = f"https://remoteok.com/remote-{keyword}-jobs"
    request = requests.get(url, headers={"User-Agent": "Kimchi"})

    if request.status_code == 200:
        soup = BeautifulSoup(request.text, "html.parser")
        jobs = soup.find_all('tr', class_="job")

        for job_section in jobs:
            job_posts = job_section.find_all('td', class_="company")
            for post in job_posts:
                anchors = post.find_all('a')
                anchor = anchors[0]
                link = anchor['href']
                title = anchor.find("h2")

                organization = post.find_all('span', class_="companyLink")
                orga = organization[0]
                company = orga.find('h3')
                location = post.find_all('div', class_="location")[0]

                if company:
                    company = company.string.strip()
                if title:
                    title = title.string.strip()
                if location:
                    location = location.string

                job_data = {
                    'link': f"https://remoteok.com{link}",
                    'company': company.replace(",", " "),
                    'location': location.replace(",", " "),
                    'position': title,
                }
                results.append(job_data)
    return results


import logging
from os import environ
from unittest.mock import patch

import pytest

from bonobo import settings
from bonobo.errors import ValidationError

TEST_SETTING = "TEST_SETTING"

def test_to_bool():
    assert not settings.to_bool("")
    assert not settings.to_bool("FALSE")
    assert not settings.to_bool("NO")
    assert not settings.to_bool("0")

    assert settings.to_bool("yup")
    assert settings.to_bool("True")
    assert settings.to_bool("yes")
    assert settings.to_bool("1")

def test_setting():
    s = settings.Setting(TEST_SETTING)
    assert s.get() is None

    with patch.dict(environ, {TEST_SETTING: "hello"}):
        assert s.get() is None
        s.clear()
        assert s.get() == "hello"

    s = settings.Setting(TEST_SETTING, default="nope")
    assert s.get() is "nope"

    with patch.dict(environ, {TEST_SETTING: "hello"}):
        assert s.get() == "nope"
        s.clear()
        assert s.get() == "hello"

    s = settings.Setting(TEST_SETTING, default=0, validator=lambda x: x == 42)
    with pytest.raises(ValidationError):
        assert s.get() is 0

    s.set(42)

    with pytest.raises(ValidationError):
        s.set(21)

def test_default_settings():
    settings.clear_all()

    assert settings.DEBUG.get() is False
    assert settings.PROFILE.get() is False
    assert settings.QUIET.get() is False
    assert settings.LOGGING_LEVEL.get() == logging._checkLevel("INFO")

    with patch.dict(environ, {"DEBUG": "t"}):
        settings.clear_all()
        assert settings.LOGGING_LEVEL.get() == logging._checkLevel("DEBUG")

    settings.clear_all()

def test_check():
    settings.check()
    with patch.dict(environ, {"DEBUG": "t", "PROFILE": "t", "QUIET": "t"}):
        settings.clear_all()
        with pytest.raises(RuntimeError):
            settings.check()
    settings.clear_all()


# -*- coding: utf-8 -*-
from django.conf import urls
from django.contrib.auth import decorators

from .views import HistoriaCreateView
from .views import HistoriaDetailView
from .views import HistoriaPacienteListView
from .views import HistoriaUpdateView

HISTORIA_CREATE_URL_NAME = 'historia_create'
HISTORIA_UPDATE_URL_NAME = 'historia_update'
HISTORIA_DETAIL_URL_NAME = 'historia_detail'
HISTORIA_LIST_URL_NAME   = 'historia_list'

urlpatterns = urls.patterns("",
    urls.url(
        regex=r'^nueva/$',
        view=decorators.login_required(HistoriaCreateView.as_view()),
        name=HISTORIA_CREATE_URL_NAME
    ),
    urls.url(
        regex=r'^editar/(?P<pk>\d+)$',
        view=decorators.login_required(HistoriaUpdateView.as_view()),
        name=HISTORIA_UPDATE_URL_NAME
    ),
    urls.url(
        regex=r'^(?P<pk>\d+)/$',
        view=decorators.login_required(HistoriaDetailView.as_view()),
        name=HISTORIA_DETAIL_URL_NAME
    ),
    urls.url(
        regex=r'^paciente/(?P<paciente_id>\d+)/$',
        view=decorators.login_required(HistoriaPacienteListView.as_view()),
        name=HISTORIA_LIST_URL_NAME
    )
)


# coding: utf-8

__author__ = "Ciprian-Octavian Truică"
__copyright__ = "Copyright 2020, University Politehnica of Bucharest"
__license__ = "GNU GPL"
__version__ = "0.1"
__email__ = "ciprian.truica@cs.pub.ro"
__status__ = "Production"

from tokenization import Tokenization
from vectorization import Vectorization
from topicmodeling import TopicModeling
import sys
import pandas as pd
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
from multiprocessing import cpu_count
import time

def tkns(text):
    title = tkn.createCorpus(text['title'], apply_FE=False)
    content = tkn.createCorpus(text['content'], apply_FE=False)
    clean_text = title + content
    clean_text = ' '.join([' '.join(elem) for elem in clean_text])
    return clean_text

def processElement(row):
    title = tkn.createCorpus(row[0], apply_FE=False)
    content = tkn.createCorpus(row[1], apply_FE=False)
    clean_text = title + content
    clean_text = ' '.join([' '.join(elem) for elem in clean_text])
    return clean_text

if __name__ == '__main__':
    fin = sys.argv[1]
    num_topics = int(sys.argv[2])
    num_words = int(sys.argv[3])
    num_iterations = int(sys.argv[4])
    no_threads = cpu_count() - 2

    print("Start Read File!")
    df = pd.read_csv(fin)
    print("End Read File!")

    print("Start Tokenization!")
    start = time.time() * 1000
    tkn = Tokenization()
    # with UDF
    # df = df.apply(tkns, axis=1)
    # clean_texts = df.to_list()

    clean_texts = []
    with ProcessPoolExecutor(max_workers=no_threads) as worker:
        for result in worker.map(processElement, df.to_numpy()):
            if result:
                clean_texts.append(result)
    end = time.time() * 1000
    print("Execution time (ms)", end - start)
    print("End Tokenization!")

    print("Start Vectorization!")
    vec = Vectorization(clean_texts)
    vec.vectorize()
    id2word = vec.getID2Word()
    corpus = vec.getTFIDFNorm()
    print("End Vectorization!")

    tm = TopicModeling(id2word=id2word, corpus=corpus)

    print("Start Topic Modeling NNF!")
    start = time.time()
    topicsNMF = tm.topicsNMF(num_topics=num_topics, num_words=num_words, num_iterations=num_iterations)
    print("=============NMF=============")
    for topic in topicsNMF:
        print("TopicID", topic[0], topic[1])
    print("=============================")
    end = time.time()
    print("Execution time (ms)", end - start)
    print("End Topic Modeling NNF!")

    # print("Start Topic Modeling LDA!")
    # print("=============LDA=============")
    # topicsLDA = tm.topicsLDA(num_topics=num_topics, num_words=num_words, num_iterations=num_iterations)
    # for topic in topicsLDA:
    #     print("TopicID", topic[0], topic[1])
    # print("=============================")
    # print("End Topic Modeling LDA!")


import torch

from care_nl_ica.independence.hsic import HSIC

class IndependenceChecker(object):
    """
    Class for encapsulating independence test-related methods
    """

    def __init__(self, hparams) -> None:
        super().__init__()
        self.hparams = hparams

        self.test = HSIC(hparams.num_permutations)

        print("Using Bonferroni = 4")

    def check_bivariate_dependence(self, x1, x2):
        decisions = []
        var_map = [1, 1, 2, 2]
        with torch.no_grad():
            decisions.append(
                self.test.run_test(x1[:, 0], x2[:, 1], bonferroni=4).item()
            )
            decisions.append(
                self.test.run_test(x1[:, 0], x2[:, 0], bonferroni=4).item()
            )
            decisions.append(
                self.test.run_test(x1[:, 1], x2[:, 0], bonferroni=4).item()
            )
            decisions.append(
                self.test.run_test(x1[:, 1], x2[:, 1], bonferroni=4).item()
            )

        return decisions, var_map

    def check_multivariate_dependence(
        self, x1: torch.Tensor, x2: torch.Tensor
    ) -> torch.Tensor:
        """
        Carries out HSIC for the multivariate case, all pairs are tested
        :param x1: tensor of the first batch of variables in the shape of (num_elem, num_dim)
        :param x2: tensor of the second batch of variables in the shape of (num_elem, num_dim)
        :return: the adjacency matrix
        """
        num_dim = x1.shape[-1]
        max_edge_num = num_dim**2
        adjacency_matrix = torch.zeros(num_dim, num_dim).bool()

        print(max_edge_num)

        with torch.no_grad():
            for i in range(num_dim):
                for j in range(num_dim):
                    adjacency_matrix[i, j] = self.test.run_test(
                        x1[:, i], x2[:, j], bonferroni=4  # max_edge_num
                    ).item()

        return adjacency_matrix


import time
from datetime import datetime, timedelta

from num2words import num2words

# Todo: returns an timedelta:
def calculate_time(sleep_time: float) -> timedelta:
    """Function to calculate time to perform it's action,
    which is takes a .

    Args:
        sleep_time (float) : Time that the function will take to be executed.

    Returns:
        string: A string containing the time needed to execute the loop
                in the format hours:minutes:seconds.milliseconds
    """
    start_time = datetime.now()

    time.sleep(sleep_time)

    end_time = datetime.now()

    difference_time_function = end_time - start_time

    return difference_time_function

def split_time(time: timedelta) -> dict:
    """This function takes the time and create a dictionary from it with the splitted values

    Args:
        time(str) : The time that the function took to be performed.

    Returns:
        splitted_time(dict): A dictionary containing how many hours, minutes, seconds and milliseconds are 			   inside the time argument.
    """

    seconds = time.seconds
    hours = seconds // 3600
    minutes = (seconds // 60) % 60
    microseconds = time.microseconds

    # timer = time.split(":")
    # sec = timer[2].split(".")

    splitted_time = {
        "hours": hours,
        "minutes": minutes,
        "seconds": seconds,
        "milliseconds": microseconds,
    }

    return splitted_time

def readable_time(splitted_time: dict) -> str:
    """This function gets a dictionary containing hours, minutes, seconds and milliseconds and
    translate these numbers to a human comprehension

    Args:
        splitted_time(dict): Dictionary containing hours, minutes, seconds and milliseconds.

    Returns:
        str: How long the operation took to be performed in a human perspective.
    """

    hours = splitted_time["hours"]
    minutes = splitted_time["minutes"]
    seconds = splitted_time["seconds"]
    milliseconds = splitted_time["milliseconds"]
    readable_time = ""

    if hours > 0:
        descriptive_hours = num2words(hours)
        if hours == 1:
            support = "hour"
        else:
            support = "hours"
        readable_time += f"{descriptive_hours} {support}, "

    if minutes > 0:
        if minutes == 1:
            support = "minute"
        else:
            support = "minutes"
        descriptive_minutes = num2words(minutes)
        readable_time += f"{descriptive_minutes} {support} and "

    if seconds > 0:
        descriptive_seconds = num2words(seconds)
        if seconds == 1:
            support = "second"
        else:
            support = "seconds"

        readable_time += f"{descriptive_seconds} {support}"

    if milliseconds > 0 and minutes < 1:
        milli = str(milliseconds)
        rounded_milliseconds = milli[0:2]
        if int(rounded_milliseconds) == 1:
            support = "millisecond"
        else:
            support = "milliseconds"

        descriptive_milliseconds = num2words(rounded_milliseconds)
        readable_time += f" and {descriptive_milliseconds} {support}"

    return (
        f"Your function took {readable_time} to run ({time_to_run_function})"
    )

if __name__ == "__main__":
    sleep_time = 1.5
    time_to_run_function = calculate_time(sleep_time)
    splitted_time = split_time(time_to_run_function)
    human_time = readable_time(splitted_time)
    print(human_time)