import argparse
import random
import numpy as np
import itertools
import logging
from nop import NOP
from copy import deepcopy
import time
import torch
import torchvision

from utils import *
from vae_model import VAE
from ae_model import AE
from data.GMIX.gmix_dataset import GMIXdataset
from misc import wandb_watch, standardize

import alfred
from alfred.utils.misc import create_management_objects
from alfred.utils.config import parse_bool, parse_log_level, save_config_to_json, load_config_from_json
from alfred.utils.recorder import Recorder
from alfred.utils.directory_tree import *
import alfred.defaults

def set_up_alfred():
    alfred.defaults.DEFAULT_DIRECTORY_TREE_ROOT = "./storage"
    alfred.defaults.DEFAULT_DIRECTORY_TREE_GIT_REPOS_TO_TRACK['sandbox'] = str(Path(__file__).absolute().parents[2])
    alfred.defaults.DEFAULT_DIRECTORY_TREE_GIT_REPOS_TO_TRACK['alfred'] = str(Path(alfred.__file__).parents[1])

    alfred.defaults.DEFAULT_PLOTS_ARRAYS_TO_MAKE = [
        ('epoch_i', 'train_loss', (None, None), (None, None)),
        ('epoch_i', 'valid_loss', (None, None), (None, None))
    ]

    alfred.defaults.DEFAULT_BENCHMARK_X_METRIC = "epoch_i"
    alfred.defaults.DEFAULT_BENCHMARK_Y_METRIC = "valid_perf"
    alfred.defaults.DEFAULT_BENCHMARK_PERFORMANCE_METRIC = "valid_perf"

def get_main_args(overwritten_args=None):
    parser = argparse.ArgumentParser()

    # alfred's arguments

    parser.add_argument('--alg_name', type=str, default='vae', choices=['vae', 'ae'])
    parser.add_argument('--task_name', type=str, default="gmix_3", choices=['mnist', 'gmix_1', 'gmix_3'])
    parser.add_argument("--desc", type=str, default="", help="Description of the experiment to be run")
    parser.add_argument("--seed", default=1, type=int, help="Random seed")
    parser.add_argument("--root_dir", default="./storage", type=str, help="Root directory")
    parser.add_argument("--log_level", default=logging.INFO, type=parse_log_level, help="Logging level")

    # common arguments

    parser.add_argument('--lr', type=float, default=0.001, metavar='G',
                        help='learning rate')

    parser.add_argument('--lr_decay_per_update', type=float, default=1., metavar='G',
                        help='learning rate decay coefficient')

    parser.add_argument('--batch_size', type=int, default=64, metavar='N',
                        help='batch size')

    parser.add_argument('--hidden_dims', type=int, default=[512, 256, 128], metavar='N',
                        help='size of hidden layers')

    parser.add_argument('--num_steps', type=int, default=5000, metavar='N',
                        help='maximum number of steps (default: 1000000)')

    parser.add_argument('--steps_bw_save', type=int, default=1000, metavar='N',
                        help='Number of environment steps between save')

    parser.add_argument('--steps_bw_eval', type=int, default=1000, metavar='N',
                        help='Number of environment episodes between evaluations')

    parser.add_argument('--use_cuda', type=parse_bool, default=False,
                        help='run on CUDA (default: False)')

    # VAE arguments

    parser.add_argument('--z_dim', type=int, default=10, metavar='N',
                        help='Dimensionality for continuous latent variables')

    parser.add_argument('--beta', type=float, default=1.,
                        help="Beta-parameter for beta-VAE (coefficient for the prior loss)")

    # wandb arguments

    parser.add_argument("--use_wandb", default=True, type=parse_bool)
    parser.add_argument("--sync_wandb", default=False, type=parse_bool)
    parser.add_argument("--save_best_model_to_wandb", default=False, type=parse_bool)
    return parser.parse_args(overwritten_args)

def validate_config(args, logger):
    original_args = deepcopy(args)

    # Re-validate args if they have been changed

    if original_args != args:
        args = validate_config(args, logger)

    return args

def main(config, dir_tree=None, logger=None, pbar="default_pbar"):
    # Setting up alfred's configs

    set_up_alfred()

    # Management objects

    dir_tree, logger, pbar = create_management_objects(dir_tree=dir_tree, logger=logger, pbar=None, config=config)

    # Scan arguments for errors

    config = validate_config(config, logger)

    # Determines which device will be used

    config.device = str(torch.device("cuda" if config.use_cuda and torch.cuda.is_available() else "cpu"))
    logger.info(f"Device to be used: {config.device}")

    # Saving config

    save_config_to_json(config, filename=str(dir_tree.seed_dir / "config.json"))
    if (dir_tree.seed_dir / "config_unique.json").exists():
        config_unique = load_config_from_json(filename=str(dir_tree.seed_dir / "config_unique.json"))
        for k in config_unique.__dict__.keys():
            config_unique.__dict__[k] = config.__dict__[k]
        save_config_to_json(config_unique, filename=str(dir_tree.seed_dir / "config_unique.json"))

    # Dataset

    if config.task_name == 'mnist':
        transform = torchvision.transforms.Compose([
            torchvision.transforms.ToTensor(),
            torchvision.transforms.Normalize((0.1307,), (0.3081,)),
            torchvision.transforms.Lambda(lambda x: torch.flatten(x))
        ])
        dataset = torchvision.datasets.MNIST('./data', train=True, download=True, transform=transform)
        n_eval_generations = 10

    elif config.task_name == 'gmix_3':
        dataset = GMIXdataset(path="data/GMIX/data_0.npy")
        # dataset.data = standardize(dataset.data, mean=0.1210, std=4.6956)
        n_eval_generations = 100

    elif config.task_name == 'gmix_1':
        dataset = GMIXdataset(path="data/GMIX/data_1.npy")
        # dataset.data = standardize(dataset.data, mean=5., std=0.9985)
        n_eval_generations = 100

    else:
        raise NotImplementedError

    data_loader = torch.utils.data.DataLoader(dataset, batch_size=config.batch_size, shuffle=True)

    # Set aside a fixed set of x's (to evaluate reconstruction)

    eval_x = next(iter(data_loader))[0][:n_eval_generations].detach()  # TODO: deal with batch_labels later (should not even be returned by __getitem__() for unlabeled datasets)

    # Seeding

    set_seeds(config.seed)

    # Agent

    if config.alg_name == "vae":
        algorithm = VAE(x_dim=eval_x.shape[1], config=config, logger=logger)
    elif config.alg_name == "ae":
        algorithm = AE(x_dim=eval_x.shape[1], config=config, logger=logger)
    else:
        raise NotImplementedError

    # Recorder

    os.makedirs(dir_tree.recorders_dir, exist_ok=True)
    train_recorder = Recorder(algorithm.metrics_to_record)

    # Wandb

    if not config.sync_wandb:
        os.environ['WANDB_MODE'] = 'dryrun'
        os.environ['WANDB_DISABLE_CODE'] = 'true'

    if config.use_wandb:
        import wandb
        os.environ["WANDB_DIR"] = str(dir_tree.seed_dir.absolute())
        wandb.init(name=dir_tree.get_run_name(), project='behavioral_preferences', reinit=True)
        wandb.config.update(config, allow_val_change=True)
        wandb_save_dir = Path(wandb.run.dir) if config.save_best_model_to_wandb else None
    else:
        wandb = NOP()
        wandb_save_dir = None

    wandb_watch(wandb, [algorithm])

    # First evaluation before initiating training
    # Set aside a fixed set of z's (to evaluate generation) and x's (to evaluate reconstruction)

    eval_z = algorithm.model.prior.sample(sample_shape=(n_eval_generations,)).detach()
    evaluation_step(algorithm, eval_z, eval_x, config.task_name, save_path=dir_tree.seed_dir / "eval_imgs" / f"Epoch0")

    # Training loop

    update_i = 0
    stop_training = False
    last_eval_step = 0

    for epoch_i in itertools.count(1):
        time_start = time.time()
        epoch_losses = []

        # Episode loop

        for (batch_data, batch_labels) in data_loader:  # TODO: deal with batch_labels later (should not even be returned by __getitem__() for unlabeled datasets)

            new_recordings = algorithm.update_parameters(batch_data, update_i)

            # Save loss recordings

            wandb.log(new_recordings)
            train_recorder.write_to_tape(new_recordings)
            update_i += 1
            epoch_losses.append(new_recordings['total_loss'])

            # Save plots and models

            if update_i % config.steps_bw_save == 0:
                save(algorithm, train_recorder, dir_tree, logger)

            # Decide whether to continue training

            if update_i >= config.num_steps:
                stop_training = True
                break

        if stop_training:
            break

        time_stop = time.time()

        # Save return recordings

        new_recordings = {
            'epoch_i': epoch_i,
            'epoch_time': time_stop - time_start,
            'wallclock_time': time.time(),
        }

        train_recorder.write_to_tape(new_recordings)
        wandb.log(new_recordings)
        logger.info(f"Epoch: {epoch_i}, total numsteps: {update_i} ({time_stop - time_start:.2f}s), total_loss: {np.mean(epoch_losses):.2f}")

        # Evaluate the model

        if update_i - last_eval_step > config.steps_bw_eval:
            logger.info("Saving plots for visualising reconstruction and generation performance.")
            evaluation_step(algorithm, eval_z, eval_x, config.task_name, save_path=dir_tree.seed_dir / "eval_imgs" / f"Epoch{epoch_i}")
            last_eval_step = update_i

    # Final evaluation and save

    evaluation_step(algorithm, eval_z, eval_x, config.task_name, save_path=dir_tree.seed_dir / "eval_imgs" / f"Epoch{epoch_i}")
    save(algorithm, train_recorder, dir_tree, logger)

    # finishes logging before exiting training script
    wandb.join()

def save(model, train_recorder, dir_tree, logger):
    train_recorder.save(dir_tree.recorders_dir / 'train_recorder.pkl')
    model.create_plots(train_recorder, save_dir=dir_tree.seed_dir)
    model.save_model(path=dir_tree.seed_dir, logger=logger)

def evaluation_step(algorithm, eval_z, eval_x, task_name, save_path):
    with torch.no_grad():
        generated_x = algorithm.model.decode(eval_z)

        encoded_z, _, _ = algorithm.model.encode(eval_x)
        reconstructed_x = algorithm.model.decode(encoded_z)

    if task_name == "mnist":
        eval_plot_mnist(x=eval_x, x_hat=reconstructed_x, x_new=generated_x, save_path=save_path)

    elif "gmix" in task_name:
        eval_plot_gmix(x=eval_x, x_hat=reconstructed_x, x_new=generated_x, save_path=save_path)

def set_seeds(seed):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.backends.cudnn.deterministic = True

if __name__ == "__main__":
    config = get_main_args()
    main(config)


import os
from flask import Flask, render_template, send_from_directory
from prometheus_metrics import setup_metrics

app = Flask(__name__, static_folder='static')

setup_metrics(app)

@app.route('/')
def home():
    return render_template('main.html')

@app.route('/SpeedRacer')
def fakeSpeed_db():
    f = (
         {'name':'Speed', 'castMember':'18-year-old professional racer'},
         {'name':'Spritle', 'castMember':'Speed\'s little brother'},
         {'name':'Pops', 'castMember':'Speed\'s father'},
         {'name':'Mrs. Racer', 'castMember':'Speed\'s mother'},
        )
    o = (
         {'name':'Trixie', 'castMember':'Speed\'s girlfriend'},
         {'name':'Racer X', 'castMember':'Masked racer, and secretly, Speed\'s older brother'},
         {'name':'Chim-Chim', 'castMember':'Spritle\'s chimpanzee'},
         {'name':'Sparky', 'castMember':'Mach 5 mechanic'},
        )
    return render_template('SpeedRacer/main.html', family=f, others=o)

@app.route('/Archer')
def fakeArcher_db():
    a = (
         {'name':'Sterling Malory Archer', 'castMember':'Codename: Duchess. ISIS Field Agent. Considered the world\'s most dangerous secret agent.'},
         {'name':'Malory Duchess Archer', 'castMember':'ISIS Director. Streling Archer\'s mother.'},
         {'name':'Lana Anthony Kane', 'castMember':'ISIS Field Agent. ISIS\' top female agent and Archer\'s one-time girfriend.'},
        )
    o = (
         {'name':'Cyril Figgis', 'castMember':'ISIS Comptroller.  Also one-time boyfriend of Lana.'},
         {'name':'Cheryl/Carol/Cherlene (Gimble) Tunt', 'castMember':'Malory\'s mentally unstable assistant.'},
         {'name':'Pam Poovey', 'castMember':'ISIS Human Resources Director. Fond of bear claws.'},
         {'name':'Dr. Algernop Krieger', 'castMember':'Head of ISIS Applied Research. Questionable morals at best.'},
         {'name':'Arthur Henry Woodhouse', 'castMember':'Sterling\'s long-suffering British valet.'},
         {'name':'', 'castMember':''},
        )
    return render_template('Archer/main.html', agents=a, others=o)

@app.route('/favicon.ico')
def favicon():
    return send_from_directory(app.static_folder, 'favicon.ico', mimetype='image/vnd.microsoft.icon')

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


#!/usr/bin/env python3

from __future__ import print_function

import csv
import fnmatch
import getopt
import os
import shutil
import sys
import tempfile

import numpy as np
from osgeo import gdal
from osgeo_utils import gdal_retile

import genkml_func
import genkmz_func
import map_func
import tile_func

# temp directory
def create_tempdir(prefix, bdir):
    dprefix = prefix + "."
    tempd = tempfile.mkdtemp(prefix=dprefix, dir=bdir)
    return tempd

# cleanup temp directory
def cleanup_tempdir(tempd, keep):
    if not keep:
        shutil.rmtree(tempd)

# gdalwarp
def gdalwarp(ifile, ofile, nodata):
    src = gdal.Open(ifile)
    # EPSG:54004 == EPSG:3395
    opt = gdal.WarpOptions(dstSRS="EPSG:3395", resampleAlg="near", dstNodata=nodata)
    gdal.Warp(ofile, src, options=opt)

# gdal_retile
def gdalretile(ifile, opath, xps, yps):
    retile_argv = ["gdal_retile.py", "-q", "-ps", str(xps), str(yps), "-targetDir", opath, ifile]
    if 'osgeo_utils.gdal_retile' in sys.modules:
        gdal_retile.main(retile_argv)
    else:
        gdal_retile(retile_argv)

# tif2jpg
def tif2jpg(ipath, opath, maxsize):
    gdal.SetConfigOption("GDAL_PAM_ENABLED", "NO")
    opt = gdal.TranslateOptions(options="-of JPEG -co QUALITY=" + str(JPEG_QUALITY))
    for filename in fnmatch.filter(os.listdir(ipath), '*.tif'):
        ifile = ipath + os.sep + filename
        name, ext = os.path.splitext(os.path.basename(filename))
        ofile = opath + os.sep + name + ".jpg"
        src = gdal.Open(ifile)
        gdal.Translate(ofile, src, options=opt)

        fsize = os.path.getsize(ofile)
        if fsize > MAX_JPEG_SIZE:
            print("WARNING: jpeg tile larger than MAX_JPEG_SIZE: %s %d" % (os.path.basename(ofile), fsize))

# clip
def clip(ifile, ofile, xoff, yoff, xsize, ysize, proj):
    if proj:
        opt_str = "-projwin "
    else:
        opt_str = "-srcwin "
    opt_str += str(xoff) + " " + str(yoff) + " " + str(xsize) + " " + str(ysize) + " -of GTiff"
    opt = gdal.TranslateOptions(options=opt_str)

    src = gdal.Open(ifile)
    gdal.Translate(ofile, src, options=opt)

def clipbycutline(ifile, ofile, tempd, neatline):
    ds = gdal.Open(ifile)

    if not neatline:
        neatline = ds.GetMetadata()['NEATLINE']
        cutline = tempd + os.sep + "cutline.csv"
        header = ['record', 'wkt']
        row = [1, "%s" % neatline]

        with open(cutline, 'w', newline='') as fh:
            writer = csv.writer(fh)
            writer.writerow(header)
            writer.writerow(row)
    else:
        cutline = neatline

    opt = gdal.WarpOptions(options="-crop_to_cutline -cutline \"%s\"" % cutline)
    gdal.PushErrorHandler('CPLQuietErrorHandler')
    gdal.Warp(ofile, ds, options=opt)
    gdal.PopErrorHandler()

# pdf2tif
def pdf2tif(ifile, ofile):
    gdal.SetConfigOption("GDAL_PDF_DPI", str(GDAL_PDF_DPI))
    gdal.SetConfigOption("GDAL_SWATH_SIZE", "1000000000")

    src = gdal.Open(ifile)
    obj = gdal.Translate(ofile, src)
    del obj

# gdalscale
def gdalscale(ifile, ofile, scale, resample_alg):
    opt_str = "-outsize %d%% %d%% -scale -r %s" % (scale, scale, resample_alg)
    opt = gdal.TranslateOptions(options=opt_str)

    src = gdal.Open(ifile)
    gdal.Translate(ofile, src, options=opt)

# remove_rotation
def remove_rotation(ifile, ofile):
    src = gdal.Open(ifile)
    gdal.Warp(ofile, src)

def Usage():
    print("Usage: pdf2kmz.py [options]")
    print("")
    print("Options:")
    print("       -i INPUT_FILE |--input=INPUT_FILE : pdf or tif file to convert to kmz")
    print("       -o OUT_DIR |--outdir=OUT_DIR      : output directory")
    print("       -f | --force                      : force overwrite of output kmz/tif file if it exists")
    print("       -v | --verbose                    : increase verbosity")
    print("       -h | --help                       : show this help message")
    print("")
    print("Clip options:")
    print("       -c | --clip                       : auto clip")
    print("       -n | --neatline                   : use embedded neatline to clip")
    print("       -N NEATFILE |--nfile=NEATFILE     : use neatline csv file to clip")
    print("       --srcwin xoff,yoff,xsize,ysize    : subwindow to clip in pixels/lines")
    print("       --projwin ulx,uly,lrx,lry         : subwindow to clip in georeferenced coordinates")
    print("       -b PIXELS |--border PIXELS        : additional pixels to remove when auto clipping (default=5)")
    print("       -B PIXELS | -black-border=PIXELS  : additional pixels to remove when clipping the black border (default=5)")
    print("")
    print("PDF to TIF conversion options:")
    print("       -d DPI |--dpi=DPI                 : tif output resolution (default=250)")
    print("       -C | --convert_to_tif             : only convert PDF to TIF")
    print("")
    print("TIF to JPEG conversion options:")
    print("       -q QUAL | --quality=QUAL          : JPEG quality (default=80)")
    print("")
    print("Tiling options:")
    print("       -m NUM | --maxtiles=NUM           : maximum number of tiles (default=100)")
    print("       -r RES | --maxtileres=RES         : maximum tile resolution (default=1048576)")
    print("       -p PROFILE | --profile=PROFILE    : gps profile to use (default, etrex, montana, monterra, oregon, gpsmap)")
    print("       -M | --mintilesize                : use the smallest (default=largest) tile size within constraints")
    print("       -S RATIO | --squareratio=RATIO    : only select candidate tilings that have this ratio or less (default=1.2)")
    print("")
    print("Scaling options:")
    print("       -s SCALE | --scale=SCALE          : percentage to scale the image")
    print("       -a ALG | --algorithm=ALG          : resampling algorithm (default=lanczos)")
    print("")
    print("Warp options:")
    print("       -R | --remove-nodata              : remove nodata attribute")
    print("")
    print("Temporary directory options:")
    print("       -t TEMP | --tmpdir=TEMP           : temporary directory")
    print("       -k | --keep                       : keep temporary files")
    print("")

def main(args=None):
    global GDAL_PDF_DPI
    global JPEG_QUALITY
    global MAX_TILES
    global MAX_TILE_RES
    global IMAGE_SCALE
    global RESAMPLE_ALG
    global SORT_DIR
    global SQUARE_RATIO
    global MAX_JPEG_SIZE
    global CLIP_OFFSET
    global BORDER_OFFSET
    global WARP_NODATA

    Ifile = False
    Odir = False
    Tdir = False
    Nfile = False
    Force = False
    Keep = False
    AutoClip = False
    Neatline = False
    Srcwin = False
    Projwin = False
    Verbose = False
    Tiles = False
    Profile = False
    Scale = False
    Algorithm = False
    odir = ""
    tdir = ""
    nfile = None
    btmpdir = None
    Tif = False

    gps_profiles = {'default': 100, 'etrex': 100, 'montana': 500, 'monterra': 99, 'oregon': 500, 'gpsmap': 500}
    resample_mthds = ["nearest", "average", "rms", "bilinear", "cubic", "cupicspline", "lanczos", "mode"]

    try:
        short_args = "-hi:o:fkd:q:cnm:r:vp:s:a:t:MS:b:N:CB:R"
        long_args = ["help", "input=", "outdir=", "force", "keep", "dpi=", "quality=", "clip", "neatline", "maxtiles=",
                     "maxtileres=", "verbose", "profile=", "scale=", "algorithm=", "tmpdir=", "mintilesize",
                     "squareratio=", "border=", "srcwin=", "projwin=", "nfile=", "convert_to_tif", "black-border=",
                     "remove-nodata"]
        opts, args = getopt.getopt(sys.argv[1:], short_args, long_args)
    except getopt.GetoptError as err:
        Usage()
        print(err)
        return 1

    for o, a in opts:
        if o in ("-h", "--help"):
            Usage()
            return 0
        elif o in ("-i", "--input"):
            Ifile = True
            ifile = a
        elif o in ("-o", "--outdir"):
            Odir = True
            odir = a
        elif o in ("-t", "--tmpdir"):
            Tdir = True
            tdir = a
        elif o in ("-N", "--neatlinefile"):
            Nfile = True
            nfile = a
        elif o in ("-f", "--force"):
            Force = True
        elif o in ("-k", "--keep"):
            Keep = True
        elif o in ("-C", "--convert_to_tif"):
            Tif = True
        elif o in ("-d", "--dpi"):
            try:
                GDAL_PDF_DPI = int(a)
            except:
                Usage()
                print("dpi must be an integer")
                return 1
        elif o in ("-q", "--quality"):
            try:
                JPEG_QUALITY = int(a)
                if JPEG_QUALITY < 0 or JPEG_QUALITY > 100:
                    Usage()
                    print("quality must be between 0 and 100")
                    return 1
            except:
                Usage()
                print("quality must be an integer")
                return 1
        elif o in ("-c", "--clip"):
            AutoClip = True
        elif o in ("-n", "--neatline"):
            Neatline = True
        elif o == "--srcwin":
            Srcwin = True
            try:
                win = np.array(a.split(","), int)
            except:
                Usage()
                print("srcwin must be an integer")
                return 1
            if len(win) != 4:
                Usage()
                print("srcwin has 4 arguments")
                return 1
            if any(win < 0):
                Usage()
                print("srcwin arguments must be 0 or greater")
                return 1
        elif o == "--projwin":
            Projwin = True
            try:
                win = np.array(a.split(","), float)
            except:
                Usage()
                print("projwin must be a float")
                return 1
            if len(win) != 4:
                Usage()
                print("projwin has 4 arguments")
                return 1
            if any(win < 0):
                Usage()
                print("projwin arguments must be 0 or greater")
                return 1
        elif o in ("-m", "--maxtiles"):
            Tiles = True
            MAX_TILES = int(a)
        elif o in ("-r", "--maxtileres"):
            MAX_TILE_RES = int(a)
        elif o in ("-v", "--verbose"):
            Verbose = True
        elif o in ("-p", "--profile"):
            Profile = True
            if a in gps_profiles:
                MAX_TILES = int(gps_profiles[a])
            else:
                Usage()
                print("unknown gps profile")
                print("supported profiles are: %s" % list(gps_profiles.keys()))
                return 1
        elif o in ("-s", "--scale"):
            Scale = True
            try:
                IMAGE_SCALE = int(a)
                if IMAGE_SCALE < 0:
                    Usage()
                    print("quality must be greater than 0")
                    return 1
            except:
                Usage()
                print("image scale must be an integer")
                return 1
        elif o in ("-a", "--algorithm"):
            Algorithm = True
            RESAMPLE_ALG = a
        elif o in ("-M", "--mintilesize"):
            SORT_DIR = 1
        elif o in ("-S", "--squareratio"):
            try:
                SQUARE_RATIO = float(a)
            except:
                Usage()
                print("squareratio must be a float")
                return 1
        elif o in ("-b", "--border"):
            try:
                CLIP_OFFSET = int(a)
                if CLIP_OFFSET < 0:
                    Usage()
                    print("auto clip border offset must be greater than 0")
                    return 1
            except:
                Usage()
                print("auto clip border offset must be an integer")
                return 1
        elif o in ("-B", "--black-border"):
            try:
                BORDER_OFFSET = int(a)
                if BORDER_OFFSET < 0:
                    Usage()
                    print("black border offset must be greater than 0")
                    return 1
            except:
                Usage()
                print("black border offset must be an integer")
                return 1
        elif o in ("-R", "--remove-nodata"):
            WARP_NODATA = "None"
        else:
            Usage()
            print("unknown option", o, a)
            return 1

    if not Ifile:
        Usage()
        print("option [-i|--input] required")
        return 1
    else:
        if Verbose:
            print("Input file = %s" % ifile)

    if Nfile:
        if not os.path.isfile(nfile):
            Usage()
            print("neatline file does not exist: %s" % nfile)
            return 1

    if (AutoClip and Srcwin) or (AutoClip and Projwin) or (Srcwin and Projwin):
        Usage()
        print("only specify one clipping option")
        return 1

    if (Neatline and Srcwin) or (Neatline and Projwin) or (Neatline and AutoClip):
        Usage()
        print("only specify one clipping option")
        return 1

    if (Nfile and Srcwin) or (Nfile and Projwin) or (Nfile and Neatline) or (Nfile and AutoClip):
        Usage()
        print("only specify one clipping option")
        return 1

    if Tiles and Profile:
        Usage()
        print("gps profile cannot be specified with maxtiles")
        return 1

    if Algorithm:
        if not RESAMPLE_ALG in resample_mthds:
            Usage()
            print("invalid resampling algorithm %s" % resample_mthds)
            return 1

    if not Odir:
        name, ext = os.path.splitext(os.path.basename(ifile))
        odir = os.path.dirname(os.path.realpath(ifile))
    else:
        if os.path.isdir(odir):
            name, ext = os.path.splitext(os.path.basename(ifile))
        else:
            try:
                os.mkdir(odir)
                name, ext = os.path.splitext(os.path.basename(ifile))
            except:
                Usage()
                print("cannot create output directory")
                return 1

    if Verbose:
        print("Output directory = %s" % odir)

    if not Tif:
        kmzfile = odir + os.sep + name + ".kmz"
        if os.path.exists(kmzfile):
            if os.path.isfile(kmzfile):
                if not Force:
                    Usage()
                    print("output file %s exists, -f to force overwrite" % kmzfile)
                    return 1
            else:
                Usage()
                print("output file %s exists but is not a regular file" % kmzfile)
                return 1

    if Tif:
        ofile = odir + os.sep + name + ".tif"
        if os.path.exists(ofile):
            if os.path.isfile(ofile):
                if not Force:
                    Usage()
                    print("output file %s exists, -f to force overwrite" % ofile)
                    return 1
            else:
                Usage()
                print("output file %s exists but is not a regular file" % ofile)
                return 1
    else:
        if Tdir:
            if os.path.isdir(tdir):
                btmpdir = os.path.realpath(tdir)
            else:
                Usage()
                print("temporary directory base is not an existing directory")
                return 1

        ""
        tempd = create_tempdir(name, btmpdir)

        if Verbose:
            print("Temporary directory: %s" % tempd)

    if ext.lower() == ".pdf":
        if not Tif:
            path = tempd + os.sep + "tif"
            if not os.path.isdir(path):
                os.mkdir(path)
            ofile = path + os.sep + name + ".tif"

        if Verbose:
            print("Coverting input pdf to tif with dpi = %d" % GDAL_PDF_DPI)

        pdf2tif(ifile, ofile)

        if Tif:
            return 1
    elif ext.lower() in (".tif", ".tiff"):
        if Verbose:
            if Tif:
                print("Input file already in tif format: %s" % ifile)
                return 1
            else:
                print("Input file in tif format: %s" % ifile)
        ofile = ifile

    if AutoClip:
        ifile = ofile
        path = tempd + os.sep + "clipped"
        if not os.path.isdir(path):
            os.mkdir(path)
        name, ext = os.path.splitext(os.path.basename(ifile))
        ofile = path + os.sep + name + ext

        if Verbose:
            print("Using auto clip with offset=%d" % CLIP_OFFSET)

        xoff, yoff, xsize, ysize = map_func.find_map_extent(ifile, CLIP_OFFSET)

        if Verbose:
            print("auto clip offset (%d,%d) and size (%d,%d)" % (xoff, yoff, xsize, ysize))

        clip(ifile, ofile, xoff, yoff, xsize, ysize, False)
    elif Srcwin:
        ifile = ofile
        path = tempd + os.sep + "clipped"
        if not os.path.isdir(path):
            os.mkdir(path)
        name, ext = os.path.splitext(os.path.basename(ifile))
        ofile = path + os.sep + name + ext
        xoff, yoff, xsize, ysize = win

        if Verbose:
            if Projwin:
                print("clip using projwin offset (%d %d) and size (%d %d)" % (xoff, yoff, xsize, ysize))
            else:
                print("clip using srcwin offset (%d %d) and size (%d %d)" % (xoff, yoff, xsize, ysize))

        clip(ifile, ofile, xoff, yoff, xsize, ysize, Projwin)

    if Projwin:
        ifile = ofile
        path = tempd + os.sep + "rotated"
        if not os.path.isdir(path):
            os.mkdir(path)
        ofile = path + os.sep + name + ".tif"

        if Verbose:
            print("Removing rotation from input file")

        remove_rotation(ifile, ofile)

    if Neatline:
        ifile = ofile
        path = tempd + os.sep + "clipped"
        if not os.path.isdir(path):
            os.mkdir(path)
        name, ext = os.path.splitext(os.path.basename(ifile))
        ofile = path + os.sep + name + ext

        if Verbose:
            print("Using neatline to clip")

        clipbycutline(ifile, ofile, tempd, nfile)
    elif Nfile:
        ifile = ofile
        path = tempd + os.sep + "clipped"
        if not os.path.isdir(path):
            os.mkdir(path)
        name, ext = os.path.splitext(os.path.basename(ifile))
        ofile = path + os.sep + name + ext

        if Verbose:
            print("Using neatline csv file to clip")

        clipbycutline(ifile, ofile, tempd, nfile)
    elif Projwin:
        ifile = ofile
        path = tempd + os.sep + "clipped"
        if not os.path.isdir(path):
            os.mkdir(path)
        name, ext = os.path.splitext(os.path.basename(ifile))
        ofile = path + os.sep + name + ext
        xoff, yoff, xsize, ysize = win

        if Verbose:
            if Projwin:
                print("clip using projwin offset (%d %d) and size (%d %d)" % (xoff, yoff, xsize, ysize))
            else:
                print("clip using srcwin offset (%d %d) and size (%d %d)" % (xoff, yoff, xsize, ysize))

        clip(ifile, ofile, xoff, yoff, xsize, ysize, Projwin)

    ifile = ofile
    path = tempd + os.sep + "warped"
    if not os.path.isdir(path):
        os.mkdir(path)
    name, ext = os.path.splitext(os.path.basename(ifile))
    ofile = path + os.sep + name + ext

    if Verbose:
        print("Running gdalwarp")

    gdalwarp(ifile, ofile, WARP_NODATA)

    xoff, yoff, xsize, ysize, nx, ny = map_func.find_map_trim(ofile, 0, BORDER_OFFSET)
    if xoff - BORDER_OFFSET != 0 or yoff - BORDER_OFFSET != 0 or xoff + xsize + BORDER_OFFSET - 1 != nx or yoff + ysize + BORDER_OFFSET - 1 != ny:
        ifile = ofile
        path = tempd + os.sep + "border"
        if not os.path.isdir(path):
            os.mkdir(path)
        name, ext = os.path.splitext(os.path.basename(ifile))
        ofile = path + os.sep + name + ext

        if Verbose:
            print("auto clip black border (%d,%d) and size (%d,%d) on image (%d,%d)" % (xoff, yoff, xsize, ysize, nx, ny))

        clip(ifile, ofile, xoff, yoff, xsize, ysize, False)
    else:
        if Verbose:
            print("a black border does not exist - skipping")

    if Scale:
        ifile = ofile
        path = tempd + os.sep + "rescaled"
        if not os.path.isdir(path):
            os.mkdir(path)

        name, ext = os.path.splitext(os.path.basename(ifile))
        ofile = path + os.sep + name + ext

        if Verbose:
            print("Rescaling image with scale = %s%% and resampling method = %s" % (IMAGE_SCALE, RESAMPLE_ALG))

        gdalscale(ifile, ofile, IMAGE_SCALE, RESAMPLE_ALG)

    path = tempd + os.sep + "tiled"
    if not os.path.isdir(path):
        os.mkdir(path)

    if Verbose:
        print("Maximum number of tiles = %d" % MAX_TILES)
        print("Maximum tile resolution = %d" % MAX_TILE_RES)

    ts = tile_func.get_tile_size(ofile, MAX_TILES, MAX_TILE_RES, SORT_DIR, SQUARE_RATIO)
    if ts == None:
        Usage()
        print("tiling not found")
        cleanup_tempdir(tempd, Keep)
        return 1
    else:
        if Verbose:
            print("retiling with %dx%d tile" % (ts[3], ts[4]))
        gdalretile(ofile, path, ts[3], ts[4])

        if Verbose:
            print("retiled with %d tiles (%dx%d)" % (ts[2], ts[0], ts[1]))

    opath = path + os.sep + "files"
    if not os.path.isdir(opath):
        os.mkdir(opath)

    if Verbose:
        print("Converting tiled tif files to jpeg with a quality of %s" % JPEG_QUALITY)

    tif2jpg(path, opath, MAX_JPEG_SIZE)

    if Verbose:
        print("Generating kml file")

    genkml_func.genkml(path, 4326)

    if Verbose:
        print("Generating kmz = %s" % kmzfile)

    genkmz_func.genkmz(kmzfile, path)

    cleanup_tempdir(tempd, Keep)

    return 0

# Global vars
###############################################################################
# Copy and paste from gdal_retile.py - see LICENSE file
###############################################################################
Verbose = False
Quiet = False
CreateOptions = []
Names = []
TileWidth = 256
TileHeight = 256
Overlap = 0
Format = 'GTiff'
BandType = None
Driver = None
Extension = None
MemDriver = None
TileIndexFieldName = 'location'
TileIndexName = None
TileIndexDriverTyp = "Memory"
CsvDelimiter = ";"
CsvFileName = None
Source_SRS = None
TargetDir = None
ResamplingMethod = gdal.GRA_NearestNeighbour
Levels = 0
PyramidOnly = False
LastRowIndx = -1
UseDirForEachRow = False
###############################################################################

# Global vars
GDAL_PDF_DPI = 250
JPEG_QUALITY = 80
MAX_TILES = 100
MAX_TILE_RES = 1048576
IMAGE_SCALE = 100
RESAMPLE_ALG = "lanczos"
SORT_DIR = -1
SQUARE_RATIO = 1.2
MAX_JPEG_SIZE = 3000000
CLIP_OFFSET = 5
BORDER_OFFSET = 5
WARP_NODATA = None

if __name__ == '__main__':
    sys.exit(main(sys.argv))


from app_seller import views
from django.urls import  path

urlpatterns = [
    path('',views.index_seller,name="index_seller"),
    path('register_seller/',views.register_seller,name="register_seller"),
    path('otp_seller/',views.otp_seller, name="otp_seller"),
    path('login_seller/',views.login_seller, name="login_seller"),
    path('logout_seller/',views.logout_seller, name="logout_seller"),
    path('add_to_cart/<int:pk>',views.add_to_cart, name="add_to_cart"),
    path('basket',views.basket, name="basket"),
    path('remove_cart/<int:pk>',views.remove_cart, name="remove_cart"),
    path('checkout/',views.checkout, name="checkout"),
]


from flask import Flask, request, jsonify
from flask_cors import CORS
#from server import get_model_api
from server import get_tpu_model_api
import os
#print os.environ.get('ENV_01')

# define the app
app = Flask(__name__)
app.config['DEBUG'] = True
CORS(app , origins=[os.environ.get('ENV_01'),os.environ.get('ENV_02'),os.environ.get('ENV_03'),os.environ.get('ENV_04'),os.environ.get('ENV_05'),os.environ.get('ENV_06') ])  # needed for cross-domain requests, allow everything by default

# load the model
#model_api = get_model_api()
model_tpu_api = get_tpu_model_api()

# API route

@app.route('/')
def index():
    return "Index API"

# HTTP Errors handlers
@app.errorhandler(404)
def url_error(e):
    return """
    Wrong URL!

{}
""".format(e), 404

@app.errorhandler(500)
def server_error(e):
    return """
    An internal error occurred:
{}

    See logs for full stacktrace.
    """.format(e), 500

""" @app.route('/apiUS', methods=['POST'])
def api():
    input_data = request.form['input']
    # print("Input data : ", input_data)
    output_data = model_api(input_data)
    print(output_data)
    response = jsonify(output_data)
    return response """

@app.route('/apiTPU', methods=['POST'])
def apitpu():
    input_data = request.form['input']
    #print("Input data : ", input_data)
    output_data = model_tpu_api(input_data)
    print(output_data)
    response = jsonify(output_data)
    return response

@app.route('/apiTPUJSON', methods=['POST'])
def apitpujson():
    content = request.json
    print("Input data : ", content['input'])
    output_data = model_tpu_api(content['input'])
    print(output_data)
    response = jsonify(output_data)
    return response

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port, debug=True)


import pygame
from pygame.locals import *
import webbrowser
import os.path
import pickle

class Option:

    hovered = False

    def __init__(self, text, pos):
        self.text = text
        self.pos = pos
        self.set_rect()
        self.draw()

    def draw(self):
        self.set_rend()
        screen.blit(self.rend, self.rect)

    def set_rend(self):
        self.rend = menu_font.render(self.text, True, self.get_color())
        self.pos = ((pygame.display.get_surface().get_width()/2)-(self.rend.get_width()/2), #(Taille de la fenetre/2)-(Taille du texte/2)
        			self.pos[1])

    def get_color(self):
        if self.hovered:
            if self.get_name() == option_mainmenu_5 :
                return color_option_quit
            else :
                return color_options_hovered
        else:
            return color_options_nhovered

    def set_rect(self):
        self.set_rend()
        self.rect = self.rend.get_rect()
        self.rect.topleft = self.pos

    def get_name(self):
    	return self.text

    def get_text_height(self):
    	return self.rend.get_height()

    def get_pos(self):
    	return self.pos

    def set_pos_y_default(self):
    	global text_options_height_total
    	return (pygame.display.get_surface().get_height()/2)-(text_options_height_total/2)

    def refresh_options(self):
    	self.set_rect()

class Titre:

    def __init__(self, text, pos, color):
        self.text = text
        self.pos = pos
        self.color = color
        self.set_rect()
        self.draw()

    def draw(self):
        self.set_rend()
        screen.blit(self.rend, self.rect)

    def set_rend(self):
        self.rend = title_font.render(self.text, True, self.color)
        self.pos = ((pygame.display.get_surface().get_width()/2)-(self.rend.get_width()/2), #(Taille de la fenetre/2)-(Taille du texte/2)
        			self.pos[1])

    def set_rect(self):
        self.set_rend()
        self.rect = self.rend.get_rect()
        self.rect.topleft = self.pos

    def refresh_title(self):
    	self.set_rect()

    def get_color(self):
        return self.color

    def get_name(self):
        return self.text

    def get_text_height(self):
        return self.rend.get_height()

    def get_pos(self):
        return self.pos

class About:

    def __init__(self, text, pos):
        self.text = text
        self.pos = pos
        self.set_rect()
        self.draw()

    def draw(self):
        self.set_rend()
        screen.blit(self.rend, self.rect)

    def set_rend(self):
        self.rend = about_font.render(self.text, True, color_about)
        self.pos = ((pygame.display.get_surface().get_width()/2)-(self.rend.get_width()/2), #(Taille de la fenetre/2)-(Taille du texte/2)
        			self.pos[1])

    def set_rect(self):
        self.set_rend()
        self.rect = self.rend.get_rect()
        self.rect.topleft = self.pos

    def refresh_about(self):
    	self.set_rect()

    def get_name(self):
        return self.text

    def get_text_height(self):
        return self.rend.get_height()

    def get_pos(self):
        return self.pos

    def set_pos_y_default(self):
        global text_desc_height_total
        return (pygame.display.get_surface().get_height()/2)-(text_desc_height_total/2)

class News:

    def __init__(self, text, pos, color):
        self.text = text
        self.pos = pos
        self.color = color
        self.set_rect()
        self.draw()

    def draw(self):
        self.set_rend()
        screen.blit(self.rend, self.rect)

    def set_rend(self):
        self.rend = news_font.render(self.text, True, self.color)
        self.rend = pygame.transform.rotate(self.rend, 45)

    def set_rect(self):
        self.set_rend()
        self.rect = self.rend.get_rect()
        self.rect.topleft = self.pos

    def get_pos(self):
        return self.pos

    def get_width(self):
        return self.rend.get_width()

def set_key_name(key_name):
    if key_name == "left" :
        key_name = "Fleche gauche"
    elif key_name == "right" :
        key_name = "Fleche droite"
    elif key_name == "up" :
        key_name = "Fleche haut"
    elif key_name == "down" :
        key_name = "Fleche bas"
    elif key_name == "space" :
        key_name = "Espace"
    elif key_name == "print screen" :
        key_name = "Impécran."
    elif key_name == "pause" :
        key_name = "Pause"
    elif key_name == "insert" :
        key_name = "Inser"
    elif key_name == "delete" :
        key_name = "Suppr."
    elif key_name == "numlock" :
        key_name = "VerrNum."
    elif key_name == "backspace" :
        key_name = "Retour"
    elif key_name == "return" :
        key_name = "Entrée"
    elif key_name == "caps lock" :
        key_name = "VerrMaj."
    elif key_name == "left shift" :
        key_name = "Maj gauche"
    elif key_name == "right shift" :
        key_name = "Maj droite"
    elif key_name == "left ctrl" :
        key_name = "Ctrl gauche"
    elif key_name == "right ctrl" :
        key_name = "Ctrl droite"
    elif key_name == "left super" :
        key_name = "Win gauche"
    elif key_name == "right super" :
        key_name = "Win droite"
    elif key_name == "left alt" :
        key_name = "Alt gauche"
    elif key_name == "enter" :
        key_name = "Entrée"
    elif key_name == "[/]" :
        key_name = "Num. /"
    elif key_name == "[*]" :
        key_name = "Num. *"
    elif key_name == "[.]" :
        key_name = "Num. ."
    elif key_name == "[+]" :
        key_name = "Num. +"
    elif key_name == "[-]" :
        key_name = "Num. -"
    elif key_name == "[9]" :
        key_name = "Num. 9"
    elif key_name == "[8]" :
        key_name = "Num. 8"
    elif key_name == "[7]" :
        key_name = "Num. 7"
    elif key_name == "[6]" :
        key_name = "Num. 6"
    elif key_name == "[5]" :
        key_name = "Num. 5"
    elif key_name == "[4]" :
        key_name = "Num. 4"
    elif key_name == "[3]" :
        key_name = "Num. 3"
    elif key_name == "[2]" :
        key_name = "Num. 2"
    elif key_name == "[1]" :
        key_name = "Num. 1"
    elif key_name == "[0]" :
        key_name = "Num. 0"
    elif key_name == "unknown key" :
        key_name = "Touche non reconnue"
    else:
        key_name = key_name.capitalize()
    return key_name

def settings():
    if os.path.exists("Settings") == False :
        with open("DefaultSettings", "rb") as default_settings:
            file = pickle.Unpickler(default_settings)
            read_default_settings = file.load()
        with open("Settings", "wb") as settings:
            file = pickle.Pickler(settings)
            file.dump(read_default_settings)

    with open("Settings", "rb") as settings:
        file = pickle.Unpickler(settings)
        read_settings = file.load()
    options = [Option(" ", default_pos)]
    global name_options
    name_options = []
    for name in read_settings:
        name_options.append(name + "  ~  " + set_key_name(read_settings[name]))
        options.append(Option(name + "  ~  " + set_key_name(read_settings[name]), default_pos))
    options.append(Option(option_back_to_menu, default_pos))
    return options

def set_settings(name_option, new_key_name):
    with open("Settings", "rb") as settings:
        file = pickle.Unpickler(settings)
        old_read_settings = file.load()

    options = [Option(" ", default_pos)]
    global name_options
    name_options = []
    new_settings = {}
    for name in old_read_settings:
        if name == name_option:
            name_options.append(name + "  ~  " + set_key_name(new_key_name))
            options.append(Option(name + "  ~  " + set_key_name(new_key_name), default_pos))
            new_settings[name] = set_key_name(new_key_name)
        else:
            name_options.append(name + "  ~  " + set_key_name(old_read_settings[name]))
            options.append(Option(name + "  ~  " + set_key_name(old_read_settings[name]), default_pos))
            new_settings[name] = set_key_name(old_read_settings[name])
    options.append(Option(option_back_to_menu, default_pos))

    with open("Settings", "wb") as settings:
        file = pickle.Pickler(settings)
        file.dump(new_settings)
    return options

def Change_key(name_option):
	key = False
	while key == False:
		event = pygame.event.wait()
		if event.type == pygame.KEYDOWN:
			if event.key == K_ESCAPE:
				options = settings()
				key = True
			else:
				key_name = pygame.key.name(event.key)
				options = set_settings(name_option, key_name)
				key = True
		else:
			key = False
	return options

def set_music(name_option):
    with open("Settings",  "rb") as settings:
        file = pickle.Unpickler(settings)
        old_read_settings = file.load()

    options = [Option(" ", default_pos)]
    global name_options
    name_options = []
    new_settings = {}
    for name in old_read_settings:
        if name == name_option:
            if old_read_settings[name] == "Activée":
                name_options.append(name + "  ~  Désactivée")
                options.append(Option(name + "  ~  Désactivée", default_pos))
                new_settings[name] = "Désactivée"
            elif old_read_settings[name] == "Désactivée":
                name_options.append(name + "  ~  Activée")
                options.append(Option(name + "  ~  Activée", default_pos))
                new_settings[name] = "Activée"
        else:
            name_options.append(name + "  ~  " + set_key_name(old_read_settings[name]))
            options.append(Option(name + "  ~  " + set_key_name(old_read_settings[name]), default_pos))
            new_settings[name] = set_key_name(old_read_settings[name])
    options.append(Option(option_back_to_menu, default_pos))

    with open("Settings", "wb") as settings:
        file = pickle.Pickler(settings)
        file.dump(new_settings)
    return options

pygame.init()

#Ouverture de la fenêtre Pygame
WinSize = (889, 460)
screen = pygame.display.set_mode(WinSize, RESIZABLE)

#Chargement du titre de la fenetre
pygame.display.set_caption("Borne d'Arcade | Menu")

#Chargement et collage de l'icon de la fenetre
WinIcon = pygame.image.load("Images/Icon.png").convert()
pygame.display.set_icon(WinIcon)

#Chargement et collage du Background
Background = pygame.image.load("Images/Background_Menu.png").convert()
BackgroundSize = Background.get_size()
BackgroundSizeDisplay = BackgroundSize
screen.blit(Background, (0,0))

#Chargement et collage du Logo
Icon = pygame.image.load("Images/Icon_alpha.png").convert_alpha()
difference_logo = 15
Icon_pos = (pygame.display.get_surface().get_width()-pygame.transform.scale(Icon, (100, 100)).get_width()-difference_logo, pygame.display.get_surface().get_height()-pygame.transform.scale(Icon, (100, 100)).get_height()-difference_logo)
screen.blit(pygame.transform.scale(Icon, (100, 100)), Icon_pos)

#Chargement et collage de la bannière news
news_banner = pygame.image.load("Images/Banner.png")
screen.blit(news_banner, (0, 0))

#Mise en place du pointer
pygame.mouse.set_visible(False)
pointer_img = pygame.image.load("Images/Pointer.png")

#Mise en place des menus
menu_font = pygame.font.Font("Fonts/game_over.ttf", 80)
title_font = pygame.font.Font("Fonts/game_over.ttf", 150)
about_font = pygame.font.Font("Fonts/game_over.ttf", 60)
news_font = pygame.font.Font("Fonts/game_over.ttf", 55)

default_pos = (0, -150)

color_options_hovered = (191, 129, 28)
color_option_quit = (191, 0, 0)
color_options_nhovered = (136, 136, 136)
color_titles = (253, 165, 16)
color_about = (200, 200, 200)
color_news = (255, 217, 0)

#Main menu
option_mainmenu_1 = "Pong 1 joueur"
option_mainmenu_2 = "Pong 2 joueurs"
option_mainmenu_3 = "Options"
option_mainmenu_4 = "A propos"
option_mainmenu_5 = "Quitter"
title_mainmenu = "BORNE D'ARCADE"

#About menu
option_back_to_menu = "Retour au menu"
title_about = "A PROPOS"
description_about_1 = "Menu réalisé par Valentin Bonnet"
description_about_2 = "Pong 1 joueur réalisé par Etienne Faviere"
description_about_3 = "Pong 2 joueurs réalisé par Maximilien Beaulaton"
description_about_4 = "Menu et jeux réalisés pour les TPE d'ISN 2017-2018 au lycée St Eloi"
description_about_5 = "©Copyright Beaulaton Maximilien & Bonnet Valentin"
description_about_6 = "& Faviere Etienne - 2017/2018"

#Options
title_settings = "OPTIONS"

#Quit menu
option_quit_1 = "Oui"
option_quit_2 = "Non"
title_quit = "Etes vous sur de vouloir quitter ?"

#News
News_text = " Jeux réalisés avec Python !"
News_pos = (5, 5)
news_link = "http://www.python.org"

#Click sound
click_sound = pygame.mixer.Sound("Sounds/click.wav")
sound_state = True

options = [Option(option_mainmenu_1, default_pos), Option(option_mainmenu_2, default_pos), Option(option_mainmenu_3, default_pos), Option(option_mainmenu_4, default_pos), Option(option_mainmenu_5, default_pos)]
titres = [Titre(title_mainmenu, default_pos, color_titles)]
descriptions = []
info = [News(News_text, News_pos, color_news)]

settings()

while True:
    pygame.event.pump()
    pygame.display.set_caption("Borne d'Arcade | Menu")

    #Chargement, collage et mise à l'échelle du Background
    screen_x = pygame.display.get_surface().get_width()
    screen_y = pygame.display.get_surface().get_height()
    if screen_x > screen_y :
        BackgroundSizeDisplay = (screen_x, int(WinSize[1] * screen_x / WinSize[0]))
    else :
        BackgroundSizeDisplay = (int(WinSize[1] * screen_y / WinSize[0]), screen_y)

    screen = pygame.display.set_mode(BackgroundSizeDisplay, RESIZABLE)
    screen.blit(pygame.transform.scale(Background, BackgroundSizeDisplay), (0,0))

    #Chargement, collage, mise à l'échelle du Logo et zoom lors du passage de la souris
    Icon_pos = (pygame.display.get_surface().get_width()-pygame.transform.scale(Icon, (100, 100)).get_width()-difference_logo, pygame.display.get_surface().get_height()-pygame.transform.scale(Icon, (100, 100)).get_height()-difference_logo)
    Icon_resize = (100, 100)
    zoom_icon = 10
    if pygame.Rect(Icon_pos, (Icon_pos[0]+Icon.get_width(), Icon_pos[1]+Icon.get_height())).collidepoint(pygame.mouse.get_pos()):
        screen.blit(pygame.transform.scale(Icon, ((Icon_resize[0]+zoom_icon), (Icon_resize[1]+zoom_icon))), (Icon_pos[0]-(zoom_icon/2), Icon_pos[1]-(zoom_icon/2)))
        if event.type == MOUSEBUTTONDOWN and event.button == 1:
            options = [Option(option_back_to_menu, default_pos)]
            titres = [Titre(title_about, default_pos, color_titles)]
            descriptions = [About(description_about_1, default_pos), About(description_about_2, default_pos), About(description_about_3, default_pos), About(description_about_4, default_pos), About(description_about_5, default_pos), About(description_about_6, default_pos)]
    else :
        screen.blit(pygame.transform.scale(Icon, Icon_resize), Icon_pos)

    #Position y des options
    nb_options = len(options)
    text_options_height_total = 0
    difference_options = 0
    difference_about_option = 15
    add_difference_options = (20, 2) #2x - Quit & Settings
    for i in range(0, nb_options):
    	text_options_height_total = text_options_height_total + options[i].get_text_height()
    text_options_height_total = text_options_height_total + (add_difference_options[0]*add_difference_options[1])

    texts_options_height = - options[0].get_text_height()

    for i in range(0, nb_options):
        texts_options_height = texts_options_height + options[i].get_text_height()
        if i > 0 :
            texts_options_height = texts_options_height + difference_options
        if options[i].get_name() == option_mainmenu_3:
            options[i] = Option(options[i].get_name(), (options[i].get_pos()[0], (options[i].set_pos_y_default()+texts_options_height+add_difference_options[0])))
        elif options[i].get_name() == option_mainmenu_4:
            options[i] = Option(options[i].get_name(), (options[i].get_pos()[0], (options[i].set_pos_y_default()+texts_options_height+add_difference_options[0])))
        elif options[i].get_name() == option_mainmenu_5:
            options[i] = Option(options[i].get_name(), (options[i].get_pos()[0], (options[i].set_pos_y_default()+texts_options_height+(add_difference_options[0]*add_difference_options[1]))))
        elif options[i].get_name() == option_back_to_menu:
            options[i] = Option(options[i].get_name(), (options[i].get_pos()[0], (pygame.display.get_surface().get_height()-options[i].get_text_height()-difference_about_option)))
        else:
            options[i] = Option(options[i].get_name(), (options[i].get_pos()[0], (options[i].set_pos_y_default()+texts_options_height)))

    #Position y des descriptions
    if descriptions != [] :
        nb_desc = len(descriptions)
        text_desc_height_total = 0
        difference_desc = 10
        for i in range(0, nb_desc):
            text_desc_height_total = text_desc_height_total + descriptions[i].get_text_height()

        texts_desc_height = - descriptions[0].get_text_height()

        for i in range(0, nb_desc):
            texts_desc_height = texts_desc_height + descriptions[i].get_text_height()
            if i > 0 :
                texts_desc_height = texts_desc_height + difference_desc
            descriptions[i] = About(descriptions[i].get_name(), (descriptions[i].get_pos()[0], (descriptions[i].set_pos_y_default()+texts_desc_height)))

    #Position y des titres
    nb_titles = len(titres)
    text_titles_height_total = 0
    title_pos_y = 95
    if nb_titles == 1 :
        difference_titles = 0
    else :
        difference_titles = 5
    for i in range(0, nb_titles):
        text_titles_height_total = text_titles_height_total + titres[i].get_text_height()

    for i in range(0, nb_titles):
        if i == 0 :
            titres[i] = Titre(titres[i].get_name(), (titres[i].get_pos()[i], (title_pos_y-titres[i].get_text_height()-difference_titles/2)), titres[i].get_color())
        else :
            titres[i] = Titre(titres[i].get_name(), (titres[i].get_pos()[i], (title_pos_y+difference_titles/2)), titres[i].get_color())

    #Traitement des options
    for option in options:
        if option.rect.collidepoint(pygame.mouse.get_pos()):
            option.hovered = True
            for event in pygame.event.get():
                if event.type == MOUSEBUTTONDOWN and event.button == 1:
                    click_sound.play()
                    #Option 1
                    if option.get_name() == option_mainmenu_1 :
                        print("Lancement du jeu Pong 1 joueur...")
                        exec(open("Jeux/Pong 1.py").read())
                    #Option 2
                    elif option.get_name() == option_mainmenu_2 :
                        print("Lancement du jeu Pong 2 joueurs...")
                        exec(open("Jeux/Pong 2.py").read())
                    #Option 3
                    elif option.get_name() == option_mainmenu_3 :
                        titres = [Titre(title_settings, default_pos, color_titles)]
                        options = settings()
                    #Option 1 de l'option 3
                    elif option.get_name() == name_options[0] :
                        options = Change_key("Pong : Joueur 2 - Droite")
                    #Option 2 de l'option 3
                    elif option.get_name() == name_options[1] :
                        options = Change_key("Pong : Joueur 2 - Gauche")
                    #Option 3 de l'option 3
                    elif option.get_name() == name_options[2] :
                        options = Change_key("Pong : Joueur 1 - Gauche")
                    #Option 4 de l'option 3
                    elif option.get_name() == name_options[3] :
                        options = Change_key("Pong : Joueur 1 - Droite")
                    #Option 5 de l'option 3
                    elif option.get_name() == name_options[4] :
                        options = set_music("Pong : Musique")
                    #Option 4
                    elif option.get_name() == option_mainmenu_4 :
                        options = [Option(option_back_to_menu, default_pos)]
                        titres = [Titre(title_about, default_pos, color_titles)]
                        descriptions = [About(description_about_1, default_pos), About(description_about_2, default_pos), About(description_about_3, default_pos), About(description_about_4, default_pos), About(description_about_5, default_pos), About(description_about_6, default_pos)]
                    #Option de l'option 4 & 3
                    elif option.get_name() == option_back_to_menu :
                        options = [Option(option_mainmenu_1, default_pos), Option(option_mainmenu_2, default_pos), Option(option_mainmenu_3, default_pos), Option(option_mainmenu_4, default_pos), Option(option_mainmenu_5, default_pos)]
                        titres = [Titre(title_mainmenu, default_pos, color_titles)]
                        descriptions = []
                    #Option 5
                    elif option.get_name() == option_mainmenu_5 :
                        title_font = pygame.font.Font("Fonts/game_over.ttf", 100)
                        titres = [Titre(title_quit, default_pos, color_titles)]
                        options = [Option(option_quit_1, default_pos), Option(option_quit_2, default_pos)]
                    #Option de l'option 5
                    elif option.get_name() == option_quit_1 :
                        pygame.quit()
                    elif option.get_name() == option_quit_2 :
                        options = [Option(option_mainmenu_1, default_pos), Option(option_mainmenu_2, default_pos), Option(option_mainmenu_3, default_pos), Option(option_mainmenu_4, default_pos), Option(option_mainmenu_5, default_pos)]
                        title_font = pygame.font.Font("Fonts/game_over.ttf", 150)
                        titres = [Titre(title_mainmenu, default_pos, color_titles)]
        else:
            option.hovered = False

        option.draw()

    #Traitement des titres
    for titre in titres:
    	titre.draw()

    #Traitement des descriptions
    for description in descriptions:
    	description.draw()

    #Traitement des événements sur les news
    for news in info:
        if news.rect.collidepoint(pygame.mouse.get_pos()):
            if sound_state == True:
                click_sound.play()
                sound_state = False
            for event in pygame.event.get():
                if event.type == MOUSEBUTTONDOWN and event.button == 1:
                    webbrowser.open(news_link)
        else:
            sound_state = True

    #Traitement des événements
    for event in pygame.event.get():
        if event.type == QUIT: #Si on quite la fenetre
            pygame.quit()

        if event.type == VIDEORESIZE:
            if event.dict['size'] < WinSize : #Si la fenetre est redimensionnée
                screen = pygame.display.set_mode(WinSize, RESIZABLE)
            else :
                screen = pygame.display.set_mode(event.dict['size'], RESIZABLE)
            screen.blit(pygame.transform.scale(Background, event.dict['size']), (0,0))

            for titre in titres:
                titre.refresh_title()
            for option in options:
                option.refresh_options()
            for description in descriptions:
                description.refresh_about()

            pygame.display.flip()
            pygame.display.update()

    if pygame.display.get_surface().get_size() < WinSize :
    	screen = pygame.display.set_mode(WinSize, RESIZABLE)

    #Rafraichissement de la bannière news
    screen.blit(news_banner, (-28, -28))

    #Rafraichissement des News
    if len(News_text) > 15 :
        News_text_1 = ""
        News_text_2 = ""
        for i in News_text :
            if len(News_text_1) < 15 :
                News_text_1 = News_text_1 + i
            elif len(News_text_2) < 15 :
                News_text_2 = News_text_2 + i
            elif len(News_text_2) == 15 :
                News_text_2 = News_text_2 + "..."
        info = [News(News_text_1, News_pos, color_news), News(News_text_2, (23, 23), color_news)]
    else :
        info = [News(News_text, News_pos, color_news)]

    #Rafraichissement du pointer
    pointer_pos = pygame.mouse.get_pos()
    if pointer_pos == (0, 0):
        screen.blit(pointer_img, (-100, -100))
    else:
        screen.blit(pointer_img, (pointer_pos[0], pointer_pos[1]))

    #Rafraichissement
    pygame.display.flip()
    pygame.display.update()


from mqtt_plugin import Mqttplugin
import time
import logging as log
import sys

log.basicConfig(level=log.INFO)

mqtt_broker_url = "mosquitto"
port = 1883

def start_backend_client(client_id):
    """
        Connect backend mqtt client

    Args:
        client_id (str): mqtt backend client id
    """
    mqtt_plugin = Mqttplugin(client_id)

    def stop_device():
        mqtt_plugin.stop_sensor_reader()
        sys.exit()

    start_success = mqtt_plugin.run_sensor_reader(mqtt_broker_url,port)
    if not start_success:
        log.error(f"Failed to start the Sensor with ID: {client_id}")
        return

    try:
        while True:
            if mqtt_plugin.sensor_reading:
                time.sleep(2)
    except KeyboardInterrupt:
        stop_device()

if __name__ == '__main__':
    client_id = sys.argv[1]
    log.info(f"Sensor backend client ID: {client_id}")
    start_backend_client(client_id)


"""
SWIRL Code
    vorticity.py

José Roberto Canivete Cuissa
IRSOL, 10.02.2021

----------------

This code contains the routines needed to compute the
vorticity criterion.
"""
# Imports
import numpy as np
from .utils import create_U
# -------------------------

def compute_vorticity(v, dl, l):
    """
    Computes the vorticity criterion.

    Parameters
    ----------
    v : arrays
        The velocity field [vx, vy].
    dl : list
        Grid cells sizes [dx, dy].
    l : list
        Stencil sizes.

    Returns
    -------
    W : list of arrays
        List of vorticity arrays
    U : list of arrays
        List of velocity gradient tensor arrays

    Raises
    ------
    """
    # Prepare the W,U instance
    W = []
    U = []
    nx = v.x.shape[0]
    ny = v.x.shape[1]

    # loop over stencils
    for il in l:
        # initialize arrays
        Wi = np.zeros((nx, ny))
        Ui = np.zeros((nx, ny))

        # fill velocity gradient tensor:
        Ui = create_U(v, dl, il)

        # compute vorticity
        Wi[il:-il, il:-il] = ( (v.y[2*il:, il:-il] - v.y[:-2*il, il:-il])/(2.*dl.x*il)
                              -(v.x[il:-il, 2*il:] - v.x[il:-il, :-2*il])/(2.*dl.y*il))

        # Clean velocity gradient tensors
        mask = (Wi == 0.0)
        Ui[mask] = np.array([[1., 0.], [0., 1.]])

        # add to W,U
        W.append(Wi)
        U.append(Ui)

    return W, U


import sys
import os
import numpy as np
import traceback

import tvm
import tvm.testing
from tvm import te
from tvm import meta_schedule as ms
from tvm.ir.module import IRModule
from tvm.target import Target

from typing import Callable, Dict, List, Optional, Union
from tvm.meta_schedule.builder import LocalBuilder
from tvm.runtime import NDArray, Module
from tvm._ffi import register_func
from tvm.tir.tensor_intrin import cuda  # get tensor intrin
from tvm.meta_schedule.testing.space_generation import (
    check_sketches,
    generate_design_space,
    print_sketches,
)

from util import get_mm_dirn, rdir, dbn

def multi_level_tiling_tensor_core(
    *,
    read_reuse_scope="shared.dyn",
    write_reuse_scope="shared",
    in_dtype="float16",
    out_dtype="float16",
    trans_b=False,
    use_software_pipeline=True,
) -> ms.schedule_rule.ScheduleRule:
    assert read_reuse_scope in ["shared", "shared.dyn"]
    assert write_reuse_scope in ["shared", "shared.dyn", "global"]
    if not isinstance(in_dtype, list):
        in_dtype = [in_dtype]
    if not isinstance(out_dtype, list):
        out_dtype = [out_dtype]
    if not isinstance(trans_b, list):
        trans_b = [trans_b]
    return ms.schedule_rule.MultiLevelTilingTensorCore(
        intrin_groups=[
            {
                "init": "mma_init_m16n8k8_f16",
                "load_a": "mma_load_m16n8k8_f16_A_shared_dyn",
                "load_b": "mma_load_m16n8k8_f16_B_shared_dyn",
                "compute": "mma_sync_m16n8k8_f16f16f16",
                "store": "mma_store_m16n8k8_f16_global",
            },
        ],
        structure="SSSRRSRS",
        tile_binds=["blockIdx.y", "blockIdx.x", "threadIdx.y"],
        max_innermost_factor=4,  # 64 // tensor intrin size
        vector_load_lens=[1, 2, 3, 4, 8, 16],
        reuse_read=ms.schedule_rule.ReuseType(
            req="must",
            levels=[4],
            scope=read_reuse_scope,
        ),
        reuse_write=ms.schedule_rule.ReuseType(
            # req="must" if write_reuse_scope.startswith("shared") else "no",
            req="no",
            levels=[2],
            scope=write_reuse_scope,
        ),
        use_software_pipeline=use_software_pipeline,
    )

M = int(sys.argv[1])
N = int(sys.argv[2])
K = int(sys.argv[3])
t = int(sys.argv[4])

original_block_name = "Y"

A = te.placeholder((M, K), "float16", name="A")
B = te.placeholder((K, N), "float16", name="B")
k = te.reduce_axis((0, K), name="k")
Y = te.compute((M, N), lambda i, j: te.sum(A[i, k] * B[k, j], axis=k), name=original_block_name)
GemmFunc = te.create_prim_func([A, B, Y]).with_attr({"global_symbol": "main"})
GemmModule = tvm.IRModule({"main": GemmFunc})

def initializer():

    @register_func("meta_schedule.builder.async_build")
    def async_build(mod: IRModule, target: Target, _params: Optional[Dict[str, NDArray]]) -> Module:
        """Async build function.

        Parameters
        ----------
        mod : IRModule
            The IRModule to be built.
        target : Target
            The target to be built.
        _params : Optional[Dict[str, NDArray]]
            The parameters to be used for the build. Must be None.

        Returns
        -------
        rt_mod : Module
            The built Module with async copy.
        """
        # pylint: disable=import-outside-toplevel
        from tvm.driver import build as tvm_build
        from tvm.tir.transform import RemoveWeightLayoutRewriteBlock
        # from tvm.tir.tensor_intrin import cuda  # re-import here for local builder

        # pylint: enable=import-outside-toplevel
        mod = RemoveWeightLayoutRewriteBlock(skip_ndarray_rewrite=True)(mod)
        with tvm.transform.PassContext(config={"tir.use_async_copy": 1}):
            rt_mod = tvm_build(mod, target=target)
        return rt_mod

db = ms.tir_integration.tune_tir(
    mod=GemmModule,
    target="nvidia/geforce-rtx-3080",
    work_dir=f"./{dbn}/{get_mm_dirn(M, N, K, t)}",
    max_trials_global=t,
    builder=LocalBuilder(f_build="meta_schedule.builder.async_build", initializer=initializer),
    space=ms.space_generator.PostOrderApply(
        sch_rules=[
            multi_level_tiling_tensor_core(out_dtype="float16")
        ],
    ),
)
sch = db.query_schedule(GemmModule, target=Target("nvidia/geforce-rtx-3080"), workload_name="main")

os.makedirs(f"{rdir}/{get_mm_dirn(M, N, K, t)}", exist_ok=True)

with open(f"{rdir}/{get_mm_dirn(M, N, K, t)}/script.py", "w") as f:
    print(sch.mod.script(), file=f)
with open(f"{rdir}/{get_mm_dirn(M, N, K, t)}/lower.py", "w") as f:
    print(tvm.lower(sch.mod).script(), file=f)
with open(f"{rdir}/{get_mm_dirn(M, N, K, t)}/trace.py", "w") as f:
    print(sch.trace, file=f)
with tvm.transform.PassContext(config={"tir.use_async_copy": 1}):
    rt_mod = tvm.build(sch.mod, target="cuda")
with open(f"{rdir}/{get_mm_dirn(M, N, K, t)}/cuda.cu", "w") as f:
    print(rt_mod.imported_modules[0].get_source(), file=f)


from urllib.request import urlopen
import json
url = "https://www.google.com/doodles/json/2018/12?hl=zh_TW"
response = urlopen(url)

doodles = json.load(response)
for d in doodles:
    url = "https:" + d["url"]
    print(d["title"], url)

    response = urlopen(url)
    img = response.read()

    fname = "doodles/" + url.split("/")[-1]

    f = open(fname, "wb")
    f.write(img)
    f.close()


import os
import time
import shutil
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

import requests
from config import API_KEY, API_SECRET, GESTURE_PATH

input_dirname = os.path.abspath(os.path.dirname(__file__)) + '/input/'
return_gesture = 1

def batch_gesture():
    image_filenames = [input_dirname + filename for filename in os.listdir(input_dirname)
                       if any([filename.endswith('.png'), filename.endswith('.jpg')])]
    data = {
        'api_key': API_KEY,
        'api_secret': API_SECRET,
        'return_gesture': return_gesture
    }
    for image_file in image_filenames:
        files = {
            'image_file': open(image_file, 'rb').read()
        }
        resp = requests.post(GESTURE_PATH, data=data, files=files).json()
        print(resp)
        hands = resp.get('hands', [])
        for hand in hands:
            gesture = hand.get('gesture', {})
            key_name = max(gesture, key=gesture.get)
            dest_dirname = os.path.abspath(os.path.dirname(__file__)) + '/' + key_name
            if not os.path.exists(dest_dirname):
                os.makedirs(dest_dirname)
            shutil.copy(image_file, dest_dirname)
        time.sleep(2)

if __name__ == "__main__":
    batch_gesture()


import logging
import itertools

def build_recipe_lists(flattened_recipes):
    # ever "flattened recipe" in "flattened recipes" has a "flattened commands", we
    # need to build a list of all those lists of flattened commands.

    assert isinstance(flattened_recipes, dict), 'incorrect data type for flattened_recipes'

    recipe_commands = []

    for flattened_recipe in flattened_recipes.keys():
        recipe_commands.append(flattened_recipes[flattened_recipe].get('flattened_commands'))

    logging.debug('recipe_commands are %s' % recipe_commands)
    return recipe_commands

def build_command_lists(recipe_commands):
    # with a list of list of commands, use itertools.product to build a combination of all items
    # in each of the list of commands
    assert isinstance(recipe_commands, list), 'recipe_commands is not a list, can never happen'
    assert len(recipe_commands) > 0, 'recipe_commands is empty, can never happen'
    unordered_comand_list = []

    # https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists
    command_lists = itertools.product(*recipe_commands)
    for command_list in command_lists:
        unordered_comand_list.append(command_list)
    logging.debug('unordered_comand_list is %s' % unordered_comand_list)
    return unordered_comand_list

def order_command_lists(flattened_recipes, unordered_command_list):
    # takes an unordered list of unordered lists of commands
    # retunrs an underoder list or ordered lists of commands, ordered by recipe weight
    ordered_command_lists = []
    command_weight_ceiling = get_command_weight_ceiling(flattened_recipes)
    for command_list in unordered_command_list:
        command_weight_ceiling = get_command_weight_ceiling(flattened_recipes)
        ordered_command_list = []
        while command_weight_ceiling > 0:
            for command in command_list:
                command_weight = get_weight_of_command(flattened_recipes, command)
                if command_weight is command_weight_ceiling:
                    ordered_command_list.insert(0, command)
            command_weight_ceiling -= 1
        ordered_command_lists.append(ordered_command_list)

    logging.debug('ordered_command_lists is %s' % ordered_command_lists)
    return ordered_command_lists

def get_command_weight_ceiling(flattened_recipes):
    counter = 0
    for recipe in flattened_recipes.keys():
        if flattened_recipes[recipe].get('recipe_weight') >= counter:
            counter = flattened_recipes[recipe].get('recipe_weight')
    return counter

def get_weight_of_command(flattened_recipes, command):
    for recipe in flattened_recipes.keys():
        if command in flattened_recipes[recipe].get('flattened_commands'):
            return flattened_recipes[recipe].get('recipe_weight')

def get_ordered_command_lists(flattened_recipes):
    recipe_commands = build_recipe_lists(flattened_recipes)
    unordered_command_list = build_command_lists(recipe_commands)
    ordered_command_lists = order_command_lists(flattened_recipes, unordered_command_list)
    return ordered_command_lists


from glob import glob

import earthpy as et
import earthpy.spatial as es
import earthpy.plot as ep

import rasterio as rio
from rasterio.plot import plotting_extent
from rasterio.plot import show
from rasterio.plot import reshape_as_raster, reshape_as_image

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap

from scipy.io import loadmat
from sklearn.metrics import classification_report, accuracy_score

import plotly.graph_objects as go

import configparser

import pathlib
import sys
import json
from itertools import groupby
from scipy.ndimage import median_filter

from sklearn.preprocessing import MinMaxScaler
# directory = pathlib.Path(__file__).parent.resolve()
#
# # setting path
# sys.path.append(directory.parent)

from .binarytree_creator import *

from .dataset5 import *

np.seterr(divide='ignore', invalid='ignore')

def getCurrentDir():
    """
    Returns parent paths
    :return:
    """
    return pathlib.Path(__file__).parent.resolve()

def getCategory(path):
    url = path.split("/")
    category = url[5]
    return category

def getParentImageNumber(path):
    url = path.split("/")
    category = url[6]
    return category

def getImagePartNumber(path):
    url = path.split("/")
    category = url[7]
    return category

def toGrouped(array):
    np_array = np.asarray(array)
    print(f"Shape of array is {np_array.shape}")
    grouped_items = []
    for i in np_array:
        tpl = (i, getCategory(i), getParentImageNumber(i), getImagePartNumber(i))
        grouped_items.append(tpl)

    return np.asarray(grouped_items)

def getLearnCategoriesPaths(categories_to_include, grouped_items):
    dataset_map = {}
    for category in categories_to_include:
        items = grouped_items[grouped_items[:, 1] == category]
        dataset_map[category] = items
    return dataset_map

def createCategoriesNumbersMap(categories_to_learn):
        numbers_map = {}
        for idx, category in enumerate(categories_to_learn):
            numbers_map[category] = idx + 1
        return numbers_map

def loadConfig():
    config = configparser.ConfigParser()
    currentDir = getCurrentDir()
    config_path = currentDir.parent.joinpath("dataset_config")
    print(f"COnfig path {config_path.absolute()}")
    config.read(config_path.absolute())
    return config

def prepareBeforeTraining(categories_to_learn):
    categories = {}
    for category_key, category_value in categories_to_learn.items():
        items = []
        image_part_items = groupby(category_value, key= lambda x: x[2])
        for key, group in image_part_items:
            grouped_parts = groupby(group, key=lambda x: x[3])
            for key1, group1 in grouped_parts:
                items.append(list(group1))
       # items = sorted(items)
        categories[category_key] = np.asarray(items)
    return categories

def loadImages(prepared_items):
    loaded_images = {}
    for category_key, category_value in prepared_items.items():
        ff = []
        for item in category_value:
            l = []

            if len(item) > 12:
                print(f"item {item[0][0]}")

            for file_path in item:
                with rio.open(file_path[0], 'r') as f:
                    img = f.read(1)
                    l.append(np.asarray(img))

            if len(l[0]) == len(l[1]) and len(l[0][0]) == len(l[1][0]):
                ff.append(np.asarray(l))
        loaded_images[category_key] = ff
    return loaded_images

def createVectors(loaded_images):
    cat_vec = {}
    for cat_key, cat_val in loaded_images.items():
        print(f"Processing {cat_key}")
        itms = [[] for _ in range(12)]
        for img_group in cat_val:
            #grp = [[] for _ in range(12)]
            #print(f"Size of img group {len(img_group)}")
            for grp_idx,img in enumerate(img_group):
                #print(f"Index of type {grp_idx}. Array size: {len(grp)}")
                if len(img.shape) == 2:
                    vec = img.flatten()
                    itms[grp_idx].extend(vec)
            # if cat_key == "field":
            #         print(f"Size of itwm {len(itms[0])}")
        print(f"Size of {cat_key} is {len(itms[1])}")
        cat_vec[cat_key] = np.asarray(itms,  dtype=int)
    return cat_vec

def createYDatDict(categories_map, vectors):
    y_dict = {}
    for val, num in categories_map.items():
        shape_length = vectors[val].shape[1]
        y_dict[val] = np.full(shape_length, num)
    return y_dict

def flattenXY(vectors, y_data_cat):
    x_dt = [[] for _ in range(12)]
    for item in vectors.values():
        for idx, item1 in enumerate(item):
            x_dt[idx].extend(item1)

    y_dt = []
    for item in y_data_cat.values():
        y_dt.extend(item)

    x_array = np.asarray(x_dt)
    x_array_changed = np.moveaxis(x_array, -1, 0)

    return (np.asarray(x_array_changed), np.asarray(y_dt))

def median(array, window_size):
    l_filtred = []
    for ll in array:
        l_filtred.append(median_filter(array, window_size))
    return l_filtred

import random

def split_train_data2(x, y, coef):
    cnt = x.shape[0]
    items_to_train = int(cnt * coef)
    items_to_test = cnt - items_to_train
    percentile = int((1 - coef) * 100.0)
    print(f"Train size: {items_to_train}. Test size: {items_to_test}")
    test_indexes = random.sample(range(cnt), items_to_test)

    x_train = x[np.logical_not(np.isin(np.arange(cnt), test_indexes))]
    y_train = y[np.logical_not(np.isin(np.arange(cnt), test_indexes))]
    x_test = x[np.isin(np.arange(cnt), test_indexes)]
    y_test = y[np.isin(np.arange(cnt), test_indexes)]

    return x_train, y_train, x_test, y_test

def load_dataset5(train_test_coeff = 0.8):
    from glob import glob

    S_sentinel_bands = glob("/mnt/d/shared_folder/dataset_5_fixed/**/*B?*.tiff", recursive=True)
    S_sentinel_bands.sort()
    S_sentinel_bands

    print("test")

    grouped_items = toGrouped(S_sentinel_bands)

    config = loadConfig()
    print(config)
    to_learch = config.get("config","categories")
    print(f"to_learch {to_learch}")

    categories_to_include = np.asarray(to_learch.split(","))

    categories_map = createCategoriesNumbersMap(categories_to_include)

    categories_to_learn = getLearnCategoriesPaths(categories_to_include, grouped_items)
    print(f"Categories to learn: {categories_to_learn}")
    prepared_images = prepareBeforeTraining(categories_to_learn)
    print(f"prepared images: {prepared_images}")

    loaded_images = loadImages(prepared_images)

    print(f"Imags: {loaded_images}")

    images_vectors = createVectors(loaded_images)

    y_data_cat = createYDatDict(categories_map, images_vectors)

    x_data, y_data = flattenXY(images_vectors, y_data_cat)

    # scaler = MinMaxScaler(feature_range=(0, 1))
    # x_scaled = scaler.fit_transform(x_data)
    x_scaled = x_data/255

    x_train, y_train, x_test, y_test = split_train_data2(x_scaled, y_data, train_test_coeff)

    dataset = Dataset5(categories_to_include, categories_map, x_train, y_train, x_test, y_test)

    return dataset
    #print("Load tree config, convert to np array, and then to tree")

    #categories_tree = config.get("config", "categories_tree")
    #print(f"categories tree string {categories_tree}")

    #categoreis_tree_np = json.loads(categories_tree)

    #print(f"List array {categoreis_tree_np}.")

    #tree = create_tree(categoreis_tree_np)

    #print(f"Tree created {tree}")

    #return tree


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

import os
from web3 import Web3
import json
import subprocess
import logging

class Solc:
    def __init__(self, solc_path=''):
        logging.getLogger().setLevel(logging.INFO)
        logging.basicConfig(
            format='%(asctime)-15s [%(levelname)s] %(message)s',
            datefmt='%Y-%m-%d %H:%M:%S')
        self.version = None
        if solc_path:
            solc_path = solc_path
            if solc_path[-1] == '/':
                solc_path = solc_path[:-1]
            if solc_path[::-1][:4] == 'clos':
                self.solc_path = solc_path
            else:
                self.solc_path = solc_path + '/solc'
        else:
            self.solc_path = 'solc'

    def get_version(self):
        if self.version == None:
            self.version = self.invoke_solc('--version').split('\n')[1].split()[1]
        return self.version

    def invoke_solc(self, args):
        if type(args) == str:
            args = [args]
        str_args = f'{self.solc_path} ' + ' '.join(args)
        try:
            output = subprocess.check_output(
                str_args, shell=True, stderr=subprocess.STDOUT).decode('utf-8')
            return output
        except subprocess.CalledProcessError as e:
            output = e.output.decode('utf-8')
            logging.error(output)

    def get_contract_instance(self,
                              contract_dict=None,
                              w3=None,
                              source=None,
                              contract_name=None,
                              address=None,
                              abi_file=None,
                              bytecode_file=None):
        if w3 == None:
            w3 = Web3()
        contract = None
        if source and contract_name:
            contract_dict = self.compile(source=source)[contract_name]
        if contract_dict:
            contract = w3.eth.contract(
                abi=contract_dict['abi'], bytecode=contract_dict['bytecode'], address=address)
        elif abi_file:
            with open(abi_file, 'r') as abi_file:
                abi = json.loads(abi_file.read())
            if address:
                contract = w3.eth.contract(abi=abi, address=address)
            elif bytecode_file:
                bytecode = None
                if bytecode_file:
                    with open(bytecode_file, 'r') as bytecode_file:
                        bytecode = bytecode_file.read()
                    contract = w3.eth.contract(abi=abi, bytecode=bytecode)
                else:
                    raise ValueError("The bytecode or the address must be provided")
        return contract

    def compile(self,
                source='*.sol',
                args=None,
                optimize=False,
                optimize_runs=200,
                pretty_json=False,
                libraries=None,
                output_dir=None,
                overwrite=True,
                combined_json=None,
                gas=False,
                standard_json=False,
                assemble=False,
                yul=False,
                strict_assembly=False,
                machine=None,
                link=False,
                metadata_literal=False,
                allow_paths=None,
                ignore_missing=False,
                ast=False,
                ast_json=False,
                ast_compact_json=False,
                asm=False,
                asm_json=False,
                opcodes=False,
                bin_=True,
                bin_runtime=False,
                abi=True,
                hashes=False,
                userdoc=False,
                devdoc=False,
                metadata=False):

        logging.info(f'Solc version: {self.get_version()}')
        logging.info(f'Compiling: {source}')

        if type(args) == str:
            args = args.split()
        if not args:
            args = []
            if optimize:
                args.append('--optimize')
                args += ['--optimize-runs', optimize_runs]
            if pretty_json:
                args.append('--pretty-json')
            if libraries:
                if type(libraries) == str:
                    libraries = libraries.split()
            if output_dir:
                args += ['--output-dir', output_dir]
            if overwrite:
                args.append('--overwrite')
            if combined_json:
                if type(combined_json) == str:
                    combined_json = combined_json.split()
                args += ['--combined-json'] + combined_json
            if gas:
                args.append('--gas')
            if standard_json:
                args.append('--standard-json')
            if assemble:
                args.append('--assemble')
            if yul:
                args.append('--yul')
            if strict_assembly:
                args.append('--strict_assembly')
            if machine:
                args.append('--machine')
            if link:
                args.append('--link')
            if metadata_literal:
                args.append('--metadata-literal')
            if allow_paths:
                if type(allow_paths) == str:
                    allow_paths = allow_paths.split()
                args += ['--allow-paths'] + allow_paths
            if ignore_missing:
                args.append('--ignore-missing')
            if ast:
                args.append('--ast')
            if ast_json:
                args.append('--ast-json')
            if ast_compact_json:
                args.append('--ast-compact-json')
            if asm:
                args.append('--asm')
            if asm_json:
                args.append('--asm-json')
            if opcodes:
                args.append('--opcodes')
            if bin_:
                args.append('--bin')
            if bin_runtime:
                args.append('--bin-runtime')
            if abi:
                args.append('--abi')
            if hashes:
                args.append('--hashes')
            if userdoc:
                args.append('--userdoc')
            if devdoc:
                args.append('--devdoc')
            if metadata:
                args.append('--metadata')

        args += [source]
        output = self.invoke_solc(args)

        library = {}
        dict_ = {}
        output = output.split('\n')
        for i, val in enumerate(output):
            if val == "":
                output[i] = " "
        for i in range(len(output)):
            if not output[i]:
                continue
            if output[i][0] == '=':
                contract_name = output[i].split()[1].split(':')[1]
                dict_[contract_name] = {}
                i += 1
                while not output[i]:
                    i += 1
                while i < len(output) and output[i] and output[i][0] != '=':
                    if output[i][:7] == 'Binary:':
                        i += 1
                        dict_[contract_name]['bytecode'] = output[i]
                    elif output[i][:17] == 'Contract JSON ABI':
                        i += 1
                        dict_[contract_name]['abi'] = json.loads(output[i])
                    elif output[i][:4] == '// $':
                        file_path = output[i]
                        if self.parse_for_file(file_path).split(':')[1] not in library.keys():
                            library[self.parse_for_file(file_path).split(':')[1]] = "__" + output[i][3:39] + "__"
                    elif output[i] == " ":
                        pass
                    else:
                        logging.warn(output[i])
                    i += 1
        if libraries is not None and len(libraries) > 0:
            for name, contract in dict_.items():
                # print("Inserting Libraries For {}".format(name))
                # print("Libraries are: {}".format(library))
                for library_name, library_id in library.items():
                    for lib in libraries:
                        lib_name = lib.split(':')[0]
                        lib_address = lib.split(':')[1]
                        if library_name == lib_name:
                            contract['bytecode'] = self.insert_library(contract['bytecode'], library_id, lib_address)
                lib_check = contract['bytecode'].find("__$")
                if lib_check != -1:
                    raise Exception("There the library '{}' was not passed.".format(contract['bytecode'][contract][lib_check + 36]))
        return dict_

    def parse_for_file(self, string):
        directories = []
        for i, val in enumerate(string):
            if val == "/":
                directories.append(i)
        if len(directories) > 0:
            file_start = directories[len(directories) - 1] + 1
            return string[file_start:]
        else:
            return ""

    def insert_library(self, bytecode, library_id, library_address):
        while True:
            if bytecode.find(library_id) != -1:
                addr = library_address[2:]
                bytecode = bytecode.replace(library_id, addr)
            else:
                return bytecode