#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 11 12:49:24 2021

@author: bmoseley
"""

# This module defines the base class used by domains.py
# It defines a ND domain with overlapping rectangular subdomains

# This class is used by domains.py

import itertools
from bisect import bisect_left

import numpy as np

class _RectangularDomainND:
    """
    Base class which defines a ND domain with hyperrectangular subdomains
    FBPINN uses ActiveRectangularDomainND inherited from this class; PINN does not use this class.
    """

    def __init__(self, subdomain_xs, subdomain_ws):
        """Creates hyperrectangle subdomains with spacing subdomain_xs
        and overlap width subdomain_ws.
        subdomain_xs: list of rectangle edges along each dimension
        subdomain_ws: list of overlap widths along each dimension
        """

        ## VALIDATION

        if len(subdomain_xs) != len(subdomain_ws): #check if dimensions match (subdomain_xs is like [np.array, np.array, ...])
            raise Exception("ERROR: lengths of subdomain_xs (%s) and subdomain_ws do not match (%s)!"%(len(subdomain_xs), len(subdomain_ws)))

        for i in range(len(subdomain_xs)): #check if #partitions of each dimension match
            if len(subdomain_xs[i]) != len(subdomain_ws[i]):
                raise Exception("ERROR: length of subdomain_x does not equal length of subdomain_w at index %i"%(i))

        subdomain_xs = [np.array(x).copy() for x in subdomain_xs]
        subdomain_ws = [np.array(w).copy() for w in subdomain_ws]

        ## PREPARATION

        # get dimensions
        nd = len(subdomain_xs)# NUMBER OF DIMENSIONS
        nm = tuple([len(x)-1 for x in subdomain_xs])# Number of Middle Segments / Subdomains / Models IN EACH DIMENSION
        # for example, for a division like 田, nd=2, nm=(2,2), for a division like 目, nd=2, nm=(1,3)

        # make widths zero on boundaries
        for w in subdomain_ws: w[-1] = w[0] = 0
        # in other words, the "overlap width" at the left and right end point is set to 0
        # for example, let subdomain_xs = [np.array([-1.0, -0.5, 0, 0.5, 1])], subdomain_ws = [array([0.2, 0.2, 0.2, 0.2, 0.2])] (width=0.4)
        # after this operation, subdomain_ws = [array([0, 0.2, 0.2, 0.2, 0]], meaning that the domain be like
        # ws  |       0     |<- 0.2->| |<- 0.2->| |<- 0.2->|      0
        # xs  |     -1.0 ----- -0.5 -----  0.  ----- +0.5 ----- +1.0
        # segs|          ----=      =---=      =---=      =----          | ----:non-overlap, ====:overlap

        # expand out xs and ws
        xs = np.meshgrid(*subdomain_xs, indexing="ij")# nd x [(nm,)+1]
        # Example: for a division like 田 where nd=2;nm=(2,2)
        # xs = [array([[x1,x1,x1],[x2,x2,x2],[x3,x3,x3]]), array([[y1,y2,y3],[y1,y2,y3],[y1,y2,y3]])]
        # shape: 2 x [(2,2)+1] = 2 x [(3,3)]
        ws = np.meshgrid(*subdomain_ws, indexing="ij")# nd x [(nm,)+1]
        # Example usage of meshgrid:
        # x=np.linspace(0,1000,20); y=np.linspace(0,500,20); X,Y=np.meshgrid(x,y); plt.plot(X,Y,color='b',marker='.');

        # expand out iis and make initial maps
        iis = np.meshgrid(*[range(n) for n in nm], indexing="ij")# nd x [(nm,)]
        sm0 = ms0 = np.expand_dims(np.stack(iis, 0), 0)# (1,nd,nm)
        #for example, nm=(2,3), iis=[arr([[0,0,0],[1,1,1]]), arr([[0,1,2],[0,1,2]])],
        #np.stack(iis,0) is arr([[[0,0,0],[1,1,1]], [[0,1,2],[0,1,2]]]), ms0 is arr([[..., ...]])

        # convenience slicer class
        class NDSlicer:
            def __getitem__(self, s):
                return (s,)*nd
                # expands to nd dimensions, returns (s,s,...,s) where s appears nd times
                # Example sl[1] = (1,1), sl[1:] = (slice(1,None,None), slice(1,None,None)), '1:' is interpreted as a slice
                # ls = [1,2,3,4,5], then ls[slice(1,None,None)]==ls[1:]==[2,3,4,5], ls[slice(1,None,2)]==ls[1::2]==[2,4]
                # let nd=2
                # mt = np.array([[1,2,3],[4,5,6],[7,8,9]]), then mt[sl[1:]]==mt[(slice, slice)]==mt[1:,1:]==[[5,6],[8,9]]
            def cut_edges(self, constrained_axes):
                return tuple([slice(None)]*2+[slice(None, -1) if i in constrained_axes else slice(None) for i in range(nd)])
                # cuts edges of constrained axes
                # let nd = 3
                # sl.cut_edges(()) == (slice(None),...,slice(None)) where slice(None) appears 5 times
                # sl.cut_edges((0,1)) == (slice(None),slice(None),slice(None,-1,None),slice(None,-1,None),slice(None))
        sl = NDSlicer()

        ## COMPUTE SEGMENTS / MAPS

        # NOTE: models_segments will contain segment indices outside of grid (in order to maintain tensor structure)
        # these need to be discarded in outside code

        segments = []
        segments_models = []
        models_segments = []
        ca2idx = dict()

        # for increasing numbers of constrained axes
        for order in range(0, nd+1):

            # for each set of constrained axis combinations
            for constrained_axes in itertools.combinations(range(nd), order):
                # Example: nd=3 => constrained_axes is () (0,) (1,) (2,) (0,1,) (0,2,) (1,2,) (0,1,2,)
                ca2idx[constrained_axes] = len(segments)

                # SEGMENTS

                # (2,nd)+nd is a tuple. Example: (1,2)+(3,) = (1,2,3)
                s = np.zeros((2,nd)+nm)# (2,nd,nm)    (left or right, dim, grid) xyz

                # for each dimension
                for i in range(nd):
                    if i in constrained_axes:# if this dimension is constrained
                        # xn - wn/2, xn + wn/2 (illustration: ====, overlap)
                        s[0,i], s[1,i] = xs[i][sl[1:]]-ws[i][sl[1:]]/2, xs[i][sl[1:]]+ws[i][sl[1:]]/2
                        # should be xs[i][sl[1:-1]]\pm ws[i][sl[1:-1]]/2, but this would give (nm,)-1, inconsistent with shape of s
                    else:
                        # x{n-1} + w{n-1}/2, xn - wn/2 (illustration: ----, non-overlap)
                        s[0,i], s[1,i] = xs[i][sl[:-1]]+ws[i][sl[:-1]]/2, xs[i][sl[1:]]-ws[i][sl[1:]]/2
                s = s[sl.cut_edges(constrained_axes)]# cut constrained edges so that s[0/1,i] actually be xs[i][sl[1:-1]]\pm ws[i][sl[1:-1]]/2
                segments.append(s) #now s shape is (2,nd,nm') where nm' is nm after sl.cut_edges

                # MAPS

                n_overlap = 2**len(constrained_axes)# number of overlapping models
                sm = np.concatenate([sm0.copy() for _ in range(n_overlap)])# (ne,nd,nm)    (model, dim, grid) ijk
                ms = np.concatenate([ms0.copy() for _ in range(n_overlap)])# (ne,nd,nm)    (segment, dim, grid) ijk

                # for all overlapping elements
                for iel,offsets in enumerate(itertools.product(*([[0,1]]*len(constrained_axes)))):
                # let len(constrained_axes) be 2, then iel, offsets are (iel=i element)
                # 0, (0,0); 1, (0,1); 2, (1,0); 3, (1,1);
                    # for each constrained dimension
                    for ic,offset in enumerate(offsets):# add their offsets to map
                        #offset is either 0 or 1 (broadcast)
                        sm[iel,constrained_axes[ic]] += offset
                        ms[iel,constrained_axes[ic]] -= offset
                sm = sm[sl.cut_edges(constrained_axes)]# cut constrained edges
                segments_models.append(sm)
                models_segments.append(ms)
                # concerning this iteration's s, sm, ms
                # for example, if nm=(2,3), constrained_axes=(0,1), then
                # sm[:,:,0,0] = [[0,0], [0,1], [1,0], [1,1]], segment [0,0] maps to models [0,0],[0,1],[1,0],[1,1]
                # sm[:,:,0,1] = [[0,1], [0,2], [1,1], [1,2]], segment [0,1] maps to models [0,1],[0,2],[1,1],[1,2]
                # +--+--+
                # +--+--+
                # +--+--+
                # +--+--+

        ## SAVE SELF ATTRIBUTES

        self.nd = nd
        self.nm = nm
        self.segments = segments
        self.segments_models = segments_models
        self.models_segments = models_segments

        ## FINAL VALIDATION

        for ioa,s in enumerate(self.segments):
            if np.any(s[0] > s[1]):
                raise Exception("ERROR: segments are negative! (%i)"%(ioa))

        # HELPERS

        self.xx = np.stack(xs, 0)# (nd,nm+1), example 田: xs.shape=2x[(3,3)], xx.shape=(2, 3, 3)
        self.ww = np.stack(ws, 0)# (nd,nm+1)

        # helper for determining which segment a point belongs to (omn)

        self.xmarks = list()
        for ithd in range(self.nd):
            x = subdomain_xs[ithd]
            w = subdomain_ws[ithd]
            # x0+w0/2, x1-w1/2, x1+w1/2, ..., xn-wn/2
            xmark = []
            for xx, ww in zip(x,w):
                xmark.append(xx-ww/2); xmark.append(xx+ww/2)
            self.xmarks.append(xmark[1:-1])
        self.ca2idx = ca2idx

    def get_onm(self, x):
        constrained_axes = tuple()
        dim_index = [None for _ in range(self.nd)]
        for ithd in range(self.nd):
            pos = bisect_left(self.xmarks[ithd], x[ithd])-1
            pos = min(max(pos, 0), len(self.xmarks[ithd])-2)
            # x[ithd] is in the pos-th interval of xmark[ithd]
            if pos % 2 == 0: # dim is not constrained
                dim_index[ithd] = pos // 2
            else: # dim is constrained
                constrained_axes = constrained_axes + (ithd,)
                dim_index[ithd] = pos // 2
        return (self.ca2idx[constrained_axes],) + tuple(dim_index)

    def __str__(self):

        st  = "nd:"+str(self.nd)+"\n"
        st += "nm:"+str(self.nm)+"\n"

        st += "segments:"+"\n"
        for s in self.segments:
            st += str(s.shape)+" "+str(s)+"\n"

        st += "segments_models:"+"\n"
        for sm in self.segments_models:
            st += str(sm.shape)+" "+str(sm)+"\n"

        st += "models_segments:"+"\n"
        for ms in self.models_segments:
            st += str(ms.shape)+" "+str(ms)+"\n"

        return st[:-1]

if __name__ == "__main__":

    import matplotlib.pyplot as plt
    import matplotlib.patches as patches

    colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan']*100
    itergrid = lambda shape: enumerate(itertools.product(*[np.arange(d) for d in shape]))

    # 1D test

    for subdomain_xs, subdomain_ws in [
                                       [[np.array([-5,0,3,6,10])],
                                        [0.2*np.array([1,2,3,4,5])]],

                                       [[np.array([-5,0])],
                                        [0.2*np.array([1,2])]],
                                       ]:

        D = _RectangularDomainND(subdomain_xs, subdomain_ws)
        print(D)

        ss,xx = D.segments, D.xx
        plt.figure(figsize=(8*len(ss), 6))
        for iplot,(s,sm,ms) in enumerate(zip(D.segments, D.segments_models, D.models_segments)):
            # create a sub-plot for each combination of constrained_axes
            ax = plt.subplot(1,len(ss),iplot+1)

            # plot subdomains (same for every subplot)
            for im,(i,) in itergrid(D.nm):# for each subdomain, im is a counter like 0,1,2... , (i,) are the indices
                plt.plot([subdomain_xs[0][i], subdomain_xs[0][i+1]],[0,0], color=colors[im])

            # plot segments (for this combination of axes)
            for iseg,(i,) in itergrid(s.shape[2:]):# for each segment
                plt.plot([s[0,0,i], s[1,0,i]],[0.1,0.1], color=colors[iseg]) #shape of s: (left or right, which dim, grid)

            # plot maps (for this combination of axes)
            for iel in range(sm.shape[0]): #iel: ith element
                for iseg,(i,) in itergrid(sm.shape[2:]):
                    plt.scatter(xx[0,i]+sm[iel,0,i], [0.2,], c=colors[iseg], s=80) #dot is located sm[iel,0,i] units right to the left endpoint

            for iel in range(ms.shape[0]):
                for iseg,(i,) in itergrid(ms.shape[2:]):
                    plt.scatter(xx[0,i]+ms[iel,0,i], [0.3,], c=colors[iseg], s=40, linewidths=1, edgecolor="k")

            #ax.set_aspect("equal")
            plt.autoscale()
        plt.show()

    # 2D test

    for subdomain_xs, subdomain_ws in [
                                       [[np.array([-5,0,3,6,10]),np.array([5,15,35])],
                                        [0.2*np.array([1,2,3,4,5]),0.2*np.array([5,6,7])]],

                                       [[np.array([-5,0]),np.array([5,15,35])],
                                        [0.2*np.array([1,2]),0.2*np.array([5,6,7])]],
                                       ]:

        D = _RectangularDomainND(subdomain_xs, subdomain_ws)
        print(D)

        ss,xx = D.segments, D.xx
        plt.figure(figsize=(4*len(ss), 10))
        for iplot,(s,sm,ms) in enumerate(zip(D.segments, D.segments_models, D.models_segments)):
            ax = plt.subplot(1,len(ss),iplot+1)

            # plot subdomains
            for im,(i,j) in itergrid(D.nm):# for each subdomain
                rect = patches.Rectangle((subdomain_xs[0][i], subdomain_xs[1][j]), #xy
                                         subdomain_xs[0][i+1]-subdomain_xs[0][i], #width
                                         subdomain_xs[1][j+1]-subdomain_xs[1][j], #height
                                         linewidth=1, edgecolor=colors[im], facecolor='none')
                ax.add_patch(rect)

            # plot segments
            for iseg,(i,j) in itergrid(s.shape[2:]):# for each segment
                rect = patches.Rectangle((s[0,0,i,j], s[0,1,i,j]), #xy (x,y)
                                         s[1,0,i,j]-s[0,0,i,j], #width (along x)
                                         s[1,1,i,j]-s[0,1,i,j], #height (along y)
                                         linewidth=2, edgecolor=colors[iseg], facecolor='none')
                ax.add_patch(rect)

            # plot maps
            for iel in range(sm.shape[0]):
                for iseg,(i,j) in itergrid(sm.shape[2:]):
                    plt.scatter(xx[0,i,j]+sm[iel,0,i,j], xx[1,i,j]+sm[iel,1,i,j], c=colors[iseg], s=80)

            for iel in range(ms.shape[0]):
                for iseg,(i,j) in itergrid(ms.shape[2:]):
                    plt.scatter(xx[0,i,j]+ms[iel,0,i,j], xx[1,i,j]+ms[iel,1,i,j], c=colors[iseg], s=40, linewidths=1, edgecolor="k")

            ax.set_aspect("equal")
            plt.autoscale()
        plt.show()

    # 3D test

    for subdomain_xs, subdomain_ws in [
                                       [[np.array([-5,0,3,6,10]),np.array([5,15,35,45]),np.array([-2,5,12])],
                                        [0.2*np.array([1,2,3,4,5]),0.2*np.array([2,3,4,5]),0.2*np.array([4,5,6])]],

                                       [[np.array([-5,0]),np.array([5,15,35,45]),np.array([-2,5])],
                                        [0.2*np.array([1,2]),0.2*np.array([2,3,4,5]),0.2*np.array([4,5])]],
                                       ]:

        D = _RectangularDomainND(subdomain_xs, subdomain_ws)
        print(D)

        ss,xx = D.segments, D.xx
        plt.figure(figsize=(4*len(ss), 10))
        for iplot,(s,sm,ms) in enumerate(zip(D.segments, D.segments_models, D.models_segments)):
            ax = plt.subplot(1,len(ss),iplot+1)

            # plot subdomains
            for im,(i,j,k) in itergrid(D.nm):# for each subdomain
                rect = patches.Rectangle((subdomain_xs[0][i], subdomain_xs[1][j]), #xy
                                         subdomain_xs[0][i+1]-subdomain_xs[0][i], #width
                                         subdomain_xs[1][j+1]-subdomain_xs[1][j], #height
                                         linewidth=1, edgecolor=colors[im], facecolor='none')
                ax.add_patch(rect)

            # plot segments
            for iseg,(i,j,k) in itergrid(s.shape[2:]):# for each segment
                rect = patches.Rectangle((s[0,0,i,j,k], s[0,1,i,j,k]), #xy (x,y)
                                         s[1,0,i,j,k]-s[0,0,i,j,k], #width (along x)
                                         s[1,1,i,j,k]-s[0,1,i,j,k], #height (along y)
                                         linewidth=2, edgecolor=colors[iseg], facecolor='none')
                ax.add_patch(rect)

            # plot maps
            for iel in range(sm.shape[0]):
                for iseg,(i,j,k) in itergrid(sm.shape[2:]):
                    plt.scatter(xx[0,i,j,k]+sm[iel,0,i,j,k], xx[1,i,j,k]+sm[iel,1,i,j,k], c=colors[iseg], s=80)

            for iel in range(ms.shape[0]):
                for iseg,(i,j,k) in itergrid(ms.shape[2:]):
                    plt.scatter(xx[0,i,j,k]+ms[iel,0,i,j,k], xx[1,i,j,k]+ms[iel,1,i,j,k], c=colors[iseg], s=40, linewidths=1, edgecolor="k")

            ax.set_aspect("equal")
            plt.autoscale()
        plt.show()


from sys import set_coroutine_origin_tracking_depth
from tkinter import *
from turtle import width
from data import words

# main:
window = Tk()
window.title("Българо-Ромски речник")
# window.geometry("600x300")
#label:
text = Label (window,text="Напишете думата която искате да преведете :")
text.grid(row=0 ,column=0,sticky=W,pady=5 )

#entry text
en_text = Entry(window,width=42*2)
en_text.grid(row=1,column=0,sticky=W)

#prevod function
def click():
    entered_text=en_text.get() # colect the enter text
    prevod.delete(0.0, END) # delete the text
    try:
        definition = words[entered_text.title()]
    except:
        definition = "Съжеляваме не можем да преведем тази дума"
    prevod.insert(END, definition)

#button:
btn = Button(window, text="ПРЕВОД",width=36*2,border=0,command=click,)
btn.grid(row=2,column=0,sticky=W)

#text box
prevod = Text(window,wrap=WORD ,width=63,height=1)
prevod.grid(row=3,column=0,sticky=W)

window.mainloop()


import tensorflow as tf

import config

class Architecture:

    def __init__(self):
        variables = config.Config().variables()
        self.image_dimension = variables['images']['image_dimension']

    def baseline(self):
        """
        Sets-up the baseline architecture

        :return:
        """

        # Base Model
        base = tf.keras.applications.VGG19(include_top=False, input_shape=self.image_dimension, weights='imagenet')
        base.trainable = False

        return base

    def layers(self, hyperparameters, labels, metrics=None):

        # Base
        base = self.baseline()

        # Flattening Object
        flatten = tf.keras.layers.Flatten()

        # The fully connected layers
        alpha_units = tf.keras.layers.Dense(hyperparameters['alpha_units'], activation='relu', name='Alpha')
        alpha_dropout = tf.keras.layers.Dropout(rate=hyperparameters['alpha_dropout'], name='AlphaDropout')
        beta_units = tf.keras.layers.Dense(hyperparameters['beta_units'], activation='relu', name='Beta')
        beta_dropout = tf.keras.layers.Dropout(hyperparameters['beta_dropout'], name='BetaDropout')

        # The classification layer
        classifier = tf.keras.layers.Dense(len(labels), activation=tf.nn.softmax)

        # Build
        model = tf.keras.models.Sequential([base, flatten, alpha_units, alpha_dropout,
                                            beta_units, beta_dropout, classifier])

        # Labels. Case
        # one-hot-code: categorical_crossentropy
        # integers: sparse_categorical_crossentropy
        if metrics is None:
            model.compile(optimizer=hyperparameters['optimization'],
                          loss=tf.keras.losses.categorical_crossentropy)
        else:
            model.compile(optimizer=hyperparameters['optimization'],
                          metrics=metrics,
                          loss=tf.keras.losses.categorical_crossentropy)

        print(base.summary())
        print(model.summary())

        return model


import pprint
from pymongo import MongoClient

# Only for testing
from dict_to_json import state

client = MongoClient()

client = MongoClient("localhost", 27017)

db = client["sih-demo"]

try:
    client.admin.command("ping")
    print("Connected to database")
except Exception as e:
    print(e)

def generate_state_from_scratch():
    state = {}
    for dept in db["departments"].find():
        state[dept] = {}
        for doctor in dept["doctors"]:
            state[dept][doctor] = {
                "waitlist": [],
                "length_of_waitlist": 0,
                "current": None,
                "presence": True,
            }

    return state

def generate_state_from_tokens():
    state = {}
    for token in db["tokens"].find():
        if token["departmentId"] not in state:
            state[token["departmentId"]] = {}
        if token["doctorId"] not in state[token["departmentId"]]:
            state[token["departmentId"]][token["doctorId"]] = {
                "waitlist": [],
                "length_of_waitlist": 0,
                "current": None,
                "presence": True,
            }
        state[token["departmentId"]][token["doctorId"]
                                     ]["waitlist"].append(token["_id"])
        state[token["departmentId"]][token["doctorId"]
                                     ]["length_of_waitlist"] += 1
        state[token["departmentId"]][token["doctorId"]]["current"] = state[token["departmentId"]][token["doctorId"]]["waitlist"][0] if len(
            state[token["departmentId"]][token["doctorId"]]["waitlist"]
        ) > 0 else None

    return state

def add_token_to_db(token):
    db["tokens"].insert_one(token)

def remove_token_from_db(tokenId):
    db["tokens"].delete_one({"_id": tokenId})

def get_from_state():
    return db["state"].find_one({})

def save_state(state):
    db["state"].delete_many({})
    db["state"].insert_one(state)

if __name__ == "__main__":
    pprint.pprint(generate_state_from_tokens())
    save_state(state)
    pprint.pprint(get_from_state())

    client.close()


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

import importlib.machinery
import importlib.util
import sys

from .exception import Error
from .trace_decorator import traceLog

current_api_version = '1.1'

class Plugins(object):
    @traceLog()
    def __init__(self, config, state):
        self.config = config
        self._hooks = {}
        self.state = state

        self.already_initialized = False

        self.plugins = config['plugins']
        self.plugin_conf = config['plugin_conf']
        self.plugin_dir = config['plugin_dir']

    def __repr__(self):
        return "<mockbuild.plugin.Plugins: state={0}, _hooks={1}, already_initialized={2}".format(
            self.state, self._hooks, self.already_initialized)

    @traceLog()
    def init_plugins(self, buildroot):
        if self.already_initialized:
            return
        self.already_initialized = True
        for key in list(self.plugin_conf.keys()):
            if key.endswith('_opts'):
                self.plugin_conf[key]['basedir'] = buildroot.basedir
                self.plugin_conf[key]['cache_topdir'] = buildroot.cache_topdir
                self.plugin_conf[key]['cachedir'] = buildroot.cachedir
                self.plugin_conf[key]['root'] = buildroot.shared_root_name
                self.plugin_conf[key]['resultdir'] = buildroot.resultdir

        self.state.start("init plugins")
        # Import plugins  (simplified copy of what yum does). Can add yum
        # features later when we prove we need them.
        for plugin in self.plugins:
            if self.plugin_conf.get("{0}_enable".format(plugin)):
                spec = importlib.machinery.PathFinder.find_spec(plugin, [self.plugin_dir])
                if not spec:
                    buildroot.root_log.warning(
                        "{0} plugin is enabled in configuration but is not installed".format(plugin))
                    continue
                module = importlib.util.module_from_spec(spec)
                spec.loader.exec_module(module)
                sys.modules[spec.name] = module

                if not hasattr(module, 'requires_api_version'):
                    raise Error('Plugin "%s" doesn\'t specify required API version' % plugin)
                requested_api_version = module.requires_api_version
                if requested_api_version != current_api_version:
                    raise Error('Plugin version mismatch - requested = %s, current = %s'
                                % (requested_api_version, current_api_version))

                run_in_bootstrap = getattr(module, "run_in_bootstrap", True)
                # simplified (buildroot.is_bootstrap and run_in_bootstrap) or not buildroot.is_bootstrap
                if not buildroot.is_bootstrap or run_in_bootstrap:
                    module.init(self, self.plugin_conf["{0}_opts".format(plugin)], buildroot)
        self.state.finish("init plugins")

    @traceLog()
    def call_hooks(self, stage, *args, **kwargs):
        required = kwargs.get('required', False)
        if 'required' in kwargs:
            del kwargs['required']
        hooks = self._hooks.get(stage, [])
        if required and not hooks:
            raise Error(
                "Feature {0} is not provided by any of enabled plugins".format(stage))
        for hook in hooks:
            hook(*args, **kwargs)

    @traceLog()
    def add_hook(self, stage, function):
        hooks = self._hooks.get(stage, [])
        if function not in hooks:
            hooks.append(function)
            self._hooks[stage] = hooks


"""
Build a new dataset with random jittered images
"""

import argparse, os
import numpy as np
from tqdm import tqdm
from PIL import Image
import re

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Word Image Similarity script for constructing transformed ground truth')

    parser.add_argument("--data", type=str, default="../gw_20p_wannot", metavar="I",
                        help="directory for the data")
    parser.add_argument("--out", type=str, default="../dataset", metavar="O")

    args = parser.parse_args()

def create_transformed_dataset(args):

    data_dir = args.data
    data_out = args.out

    # Get the names of the image files
    imageFnames = []

    with open(os.path.join(data_dir, "file_order.txt"), "r") as f:
        imageFnames = f.readlines()

    imageFnames = [f.strip() for f in imageFnames]

    # Get the names of the files containing the boxes coordinates
    boxFnames = [f[:-4] + "_boxes.txt" for f in imageFnames]

    words = []

    with open(os.path.join(data_dir, "annotations.txt"), "r", encoding = "iso-8859-1") as f:
        words = [w.strip() for w in f.readlines()]

    nb_transforms = [2 * words.count(w) for w in words]

    if not os.path.exists(data_out):
        os.makedirs(data_out)

    boxes = []
    i = 0
    j = -1
    k = 0

    image = None
    new_words = []

    pbar = tqdm(total=np.sum(nb_transforms), position = 0)

    while i < len(words):

        if k == len(boxes):
            boxes = []
            j += 1
            k = 0

        if boxes == []:
            if j >= len(boxFnames):
                print("ERROR: the end of box files was reached before the end of word list. j={}; i={}".format(j, i))
                break
            image = np.array(Image.open(os.path.join(data_dir, imageFnames[j])))
            with open(os.path.join(data_dir, boxFnames[j])) as f:
                boxes_str = f.readlines()
            coords = [[float(i) for i in re.findall(r"[+-]? *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?", s)] for s in boxes_str]
            boxes = [c[:4] for c in coords if len(c) >= 4]

        new_words.append(words[i])

        a, b = image.shape[0], image.shape[1]
        x1, x2, y1, y2 = int(b * boxes[k][0]), int(b * boxes[k][1]), int(a * boxes[k][2]), int(a * boxes[k][3])
        Sx, Sy = 5, 5

        x1prim = x1 - Sx
        y1prim = y1 - Sy
        x2prim = x2 + Sx
        y2prim = y2 + Sy

        res = image[y1prim:y2prim, x1prim:x2prim]

        im = Image.fromarray(res)
        im.save(os.path.join(data_out, "word-{:06d}.png".format(len(new_words))))
        pbar.update(1)

        k += 1
        i += 1

    with open(os.path.join(data_out, "words.txt"), "w") as f:
        for w in new_words:
            f.write(w + '\n')

if __name__ == "__main__":
    create_transformed_dataset(args)


import pandas as pd
import re
from datetime import datetime
now = datetime.now()

file_name = 'current.xlsx'  # path to file + file name

def czytaj_plik(sheetExela):
    sheet = sheetExela  # sheet name or sheet number or list of sheet numbers and names
    return pd.read_excel(io=file_name, sheet_name=sheet, skiprows=list(range(1)), index_col=0)

def przerob_pliki(sheetExela, plikTxt):

    data = czytaj_plik(sheetExela)

    data.to_csv(plikTxt)
    with open(plikTxt, "r", encoding="utf8") as f:
        partner_code = sheetExela[6:14]
        # print(partnerCode)
        with open("NA_" + now.strftime("%Y%m%d_%H_%M_%S_") + partner_code + ".trn", "w") as out:
            out.write('H|' + now.strftime("%Y-%m-%dT0%H:%M:%S") + '|' + partner_code + '\n')
            i = 0
            for line in f:
                # print('R|' + line.split(',')[1] + '||||||||' + line.split(',')[2] + '|||' + line.split(',')[4])
                out.write('R|' + line.split(',')[1] + '||||||||' + line.split(',')[2] + '|||' + line.split(',')[4]+'\n')
                i += 1

            out.write('F|'+str(i))

przerob_pliki('Input 10009017', "in17.txt")
# przerob_pliki('Input 10009018', "in18.txt")


#Ancora incasinato, pusho per backup piu che altro

import time
import collections
import matrix_generator
import os
from matrix_generator import print_matrix
from collections import namedtuple

punti = []

Point =  namedtuple('Point', ['x','y','w'])
#Il sistema di riferimento e' il quarto quadrante, quindi andando a SUD, Y aumenta, mentre a NORD diminuisce
modifier = { 0 : (1,0), 1 : (1,-1), 2 : (0,-1), 3 : (-1,-1), 4 : (-1,0), 5 : (-1,1), 6 : (0,1), 7 : (1,1) }

#direzioni:
# 0 = E (+1,0)
# 1 = NE (+1,+1)
# 2 = N (0,+1)
# 3 = NO (-1,+1)
# 4 = O (-1,0)
# 5 = SO (-1,-1)
# 6 = S (0,-1)
# 7 = SE (+1,-1)
# 8 = ARRIVATI

def addPoints(p, matrix):
 punti.append(p)
 x = getattr(p, 'x')
 y = getattr(p, 'y')
 val = matrix[x][y][1]
 if val != 'S':
  matrix[x][y][1] = 'O'

 if len(punti) >= 2 :
  pprev = punti[-2]
  xprev = getattr(pprev, 'x')
  yprev = getattr(pprev, 'y')
  valprev = matrix[xprev][yprev][1]
  if valprev != 'S':
   matrix[xprev][yprev][1] = '+'

 os.system('clear')
 print_matrix(matrix)
 raw_input()

#[p]unto di partenza, [m]atrice, [d]irezione
def move(p,m,d):
 pass

def farAlg(matrix, SP):
#This is a comment
 direction = 0
 distance = []

 x1 = SP[0]
 y1 = SP[1]
 ps = Point(x1,y1,0)
 distance.append(matrix[x1][y1][0])
 addPoints(ps, matrix)

 x2 = x1 + modifier[direction][0]
 y2 = y1 + modifier[direction][1]
 p = Point(x2,y2,0)
 distance.append(matrix[x2][y2][0])
 addPoints(p, matrix)

 direction = direction + 2

 x3 = x2 + modifier[direction][0]
 y3 = y2 + modifier[direction][1]
 p = Point(x3,y3,0)
 addPoints(p, matrix)
 distance.append(matrix[x3][y3][0])

 addPoints(ps, matrix)

 dist1 = distance[0]
 dist2 = distance[1]
 dist3 = distance[2]

 xdir = 0
 ydir = 0
 if dist1 > dist2:
  xdir = '+'
 else:
  xdir = '-'
 if dist2 < dist3:
  ydir = '+'
 else:
  ydir = '-'

 if xdir == '+':
  if ydir == '+':
   direction = 7
  elif ydir == '-':
   direction = 1
  else:
   direction = 0
 elif xdir == '-':
  if ydir == '+':
   direction = 5
  elif ydir == '-':
   direction = 3
  else:
   direction = 4
 else:
  if ydir == '+':
   direction = 6
  elif ydir == '-':
   direction = 2
  else:
   direction = 8

 return direction


import os
from typing import Literal, Dict, NoReturn
from contextlib import contextmanager

def load_env_file(filename: str) -> Dict[str, str]:
    """Load environmental variables from a `.env` file."""
    result = {}
    with open(filename) as f:
        for line in f.readlines():
            if line.startswith('#') or not line.strip():
                # Skip this line. It's a comment or just whitespace.
                continue
            # Remove the end of line character
            line = line.rstrip()
            # Split the line into key and value
            key, value = line.split('=', 1)
            # Add the variable to the result
            result[key] = value
    return result

def yes_or_no(prompt: str, default: Literal['y', 'n'] = None):
    """Ask a yes/no question that returns True for "y/yes" and False for "n/no"."""
    if default not in ('y', 'n', None):
        raise ValueError("'The default value must be 'y' or 'n'.")

    while True:
        valid = ['y', 'n', 'yes', 'no']
        if not default:
            options_str = ' [y/n] '
        elif default == 'y':
            options_str = ' [Y/n] '
        else:
            options_str = ' [y/N] '
        response = input(prompt + options_str).strip().lower()
        if response not in valid:
            if default and response == '':
                return default == 'y'
            print("Please answer 'y' or 'n'.")
        else:
            return response.startswith('y')

def print_and_exit(msg: str, exit_code: int = 1) -> NoReturn:
    """Print a message and exit with the provided exit code."""
    print(msg)
    exit(exit_code)

@contextmanager
def using_dir(dir_new: str) -> None:
    """Temporarily change the working directory within a context manager."""
    dir_start = os.getcwd()
    try:
        os.chdir(dir_new)
        yield
    finally:
        os.chdir(dir_start)

if __name__ == '__main__':
    env = load_env_file('.env')
    print(env)


# %%
from dataset import Face3dDataset
from torch.utils.data import DataLoader
import torch
import glob

from training import training_loop
import os

# %%
train_filelist = list(glob.iglob("data/train/mp4/" + '**/*.mp4', recursive=True))
test_filelist = list(glob.iglob("data/test/mp4/" + '**/*.mp4', recursive=True))

# %%
train_dataset = Face3dDataset(train_filelist, 16, is_gpu=False)
test_filelist = Face3dDataset(test_filelist, 2, is_gpu=False)

# %%
train_dataset = train_dataset
test_dataset = test_filelist
batch_size = 1
epochs = 10
step_size = 200
lr = 0.001
beta1 = 0.5
beta2 = 0.999
use_cuda = True if torch.cuda.is_available() else False
log_dir = "logs"
model_dir = "models"
model_name = "model"
num_workers = 0
checkpoint = "models/model"
checkpoint_epoch = 1
checkpoint_step = 300

training_loop(train_dataset, test_dataset, batch_size, epochs, step_size, lr, beta1, beta2, use_cuda, log_dir, model_dir, model_name, num_workers, checkpoint, checkpoint_step, checkpoint_epoch )

# %%


from __future__ import print_function

from collections import Mapping, namedtuple
import copy
import itertools
import os
import shlex
import stat
import subprocess
import tempfile

from cached_property import cached_property

from .base import Enumerator, Leaf, SEQUENCES, ConstraintTag, write_yaml_report

import six

from hpcbench import jinja_environment
from hpcbench.toolbox.contextlib_ext import pushd
from hpcbench.toolbox.process import (
    build_slurm_arguments,
    find_executable,
    parse_constraint_in_args,
)

Command = namedtuple('Command', ['execution', 'srun'])
Command.__new__.__defaults__ = (None,) * len(Command._fields)

class ExecutionDriver(Leaf):
    """Abstract representation of a benchmark command execution
    (a benchmark is made of several commands)
    """

    name = 'local'

    def __init__(self, parent):
        super(ExecutionDriver, self).__init__(parent)
        self.benchmark = self.parent.benchmark
        self.execution = parent.execution
        self.command_expansion_vars = dict(process_count=1)
        self.config = parent.config
        self.exec_context = parent.exec_context

    @classmethod
    def commands(cls, campaign, config, execution):
        """
        :param campaign: global configuration
        :param config: benchmark YAML configuration
        :param execution: one benchmark to execute,
        provided by `Benchmark.execution_matrix`
        """
        yield Command(execution=execution, srun=None)

    @cached_property
    def _executor_script(self):
        """Create shell-script in charge of executing the benchmark
        and return its path.
        """
        fd, path = tempfile.mkstemp(suffix='.sh', dir=os.getcwd())
        os.close(fd)
        with open(path, 'w') as ostr:
            self._write_executor_script(ostr)
        mode = os.stat(path).st_mode
        os.chmod(path, mode | stat.S_IEXEC | stat.S_IRGRP | stat.S_IRUSR)
        return path

    @property
    def _jinja_executor_template(self):
        """:return Jinja template of the shell-script executor"""
        template = self.campaign.process.executor_template
        if template.startswith('#!'):
            return jinja_environment.from_string(template)
        else:
            return jinja_environment.get_template(template)

    def _write_executor_script(self, ostr):
        """Write shell script in charge of executing the command"""
        environment = self.execution.get('environment') or {}
        if not isinstance(environment, Mapping):
            msg = 'Expected mapping for environment but got '
            msg += str(type(environment))
            raise Exception(msg)
        escaped_environment = dict(
            (var, six.moves.shlex_quote(str(value)))
            for var, value in environment.items()
        )
        modules = self.execution.get('modules') or []
        properties = dict(
            command=self.command,
            cwd=os.getcwd(),
            modules=modules,
            environment=escaped_environment,
        )
        self._jinja_executor_template.stream(**properties).dump(ostr)

    @cached_property
    def command(self):
        """:return command to execute inside the generated shell-script"""
        exec_prefix = self.parent.parent.parent.config.get('exec_prefix', [])
        command = self.execution['command']
        if isinstance(command, SEQUENCES):
            command = [arg.format(**self.command_expansion_vars) for arg in command]
        else:
            command = command.format(**self.command_expansion_vars)
        if self.execution.get('shell', False):
            if not isinstance(exec_prefix, six.string_types):
                exec_prefix = ' '.join(exec_prefix)
            if not isinstance(command, six.string_types):
                msg = "Expected string for shell command, not {type}: {value}"
                msg = msg.format(type=type(command).__name__, value=repr(command))
                raise Exception(msg)
            eax = [exec_prefix + command]
        else:
            if not isinstance(exec_prefix, SEQUENCES):
                exec_prefix = shlex.split(exec_prefix)
            if not isinstance(command, SEQUENCES):
                eax = exec_prefix + shlex.split(command)
            else:
                eax = exec_prefix + command
            eax = [six.moves.shlex_quote(arg) for arg in eax]
        return eax

    @cached_property
    def command_str(self):
        """get command to execute as string properly escaped

        :return: string
        """
        if isinstance(self.command, six.string_types):
            return self.command
        return ' '.join(map(six.moves.shlex_quote, self.command))

    def popen(self, stdout, stderr):
        """Build popen object to run

        :rtype: subprocess.Popen
        """
        self.logger.info('Executing command: %s', self.command_str)
        return subprocess.Popen([self._executor_script], stdout=stdout, stderr=stderr)

    def __execute(self, stdout, stderr):
        with self.module_env(), self.spack_env():
            self.benchmark.pre_execute(self.execution, self.exec_context)
        exit_status = self.popen(stdout, stderr).wait()
        with self.module_env(), self.spack_env():
            self.benchmark.post_execute(self.execution, self.exec_context)
        return exit_status

    def module_env(self):
        category_driver = self.parent.parent
        return category_driver._module_env(self.execution)

    def spack_env(self):
        category_driver = self.parent.parent
        return category_driver._spack_env(self.execution)

    @write_yaml_report
    @Enumerator.call_decorator
    def __call__(self, **kwargs):
        with open('stdout', 'w') as stdout, open('stderr', 'w') as stderr:
            cwd = self.execution.get('cwd')
            if cwd is not None:
                ctx = self.parent.parent.parent.exec_context
                cwd = cwd.format(node=ctx.node, tag=ctx.tag)
                with pushd(cwd):
                    exit_status = self.__execute(stdout, stderr)
            else:
                exit_status = self.__execute(stdout, stderr)
        report = dict(
            exit_status=exit_status,
            benchmark=self.benchmark.name,
            executor=type(self).name,
        )

        expected_es = self.execution.get('expected_exit_statuses', {0})
        report['command_succeeded'] = exit_status in expected_es
        if not report['command_succeeded']:
            self.logger.error('Command failed with exit status: %s', exit_status)
        report.update(self.execution)

        report.update(command=self.command)
        return report

class SrunExecutionDriver(ExecutionDriver):
    """Manage process execution with srun (SLURM)
    """

    name = 'srun'

    @classmethod
    def commands(cls, campaign, config, execution):
        top = super(SrunExecutionDriver, cls)
        for top_cmd in top.commands(campaign, config, execution):
            top_cmd = top_cmd._asdict()
            for srun in cls.srun_cartesian_product(campaign, config):
                top_cmd.update(srun=srun)
                yield Command(**top_cmd)

    @classmethod
    def srun_cartesian_product(cls, campaign, config):
        """
        :param campaign: global YAML configuration
        :param config: benchmark YAML configuration
        """
        options = copy.copy(cls.common_srun_options(campaign))
        options.update(config.get('srun') or {})
        options_tuple = []
        for k, v in options.items():
            if not isinstance(v, SEQUENCES):
                v = [v]
            options_tuple.append([(k, e) for e in v])
        return [dict(combination) for combination in itertools.product(*options_tuple)]

    @cached_property
    def srun(self):
        """Get path to srun executable

        :rtype: string
        """
        commands = self.campaign.process.get('commands', {})
        srun = find_executable(commands.get('srun', 'srun'))
        if six.PY2:
            srun = srun.encode('utf-8')
        return srun

    @classmethod
    def common_srun_options(cls, campaign):
        """Get options to be given to all srun commands

        :rtype: list of string
        """
        default = dict(campaign.process.get('srun') or {})
        default.update(output='slurm-%N-%t.stdout', error='slurm-%N-%t.error')
        return default

    @cached_property
    def tag(self):
        return self.parent.parent.parent.parent.name

    @cached_property
    def command(self):
        """get command to execute

        :return: list of string
        """
        srun_optlist = build_slurm_arguments(self.parent.command.srun or {})
        if not isinstance(self.root.network.nodes(self.tag), ConstraintTag):
            pargs = parse_constraint_in_args(srun_optlist)
            self.command_expansion_vars['process_count'] = pargs.ntasks
            if not pargs.constraint:
                # Expand nodelist if --constraint option is not specified
                # in srun options
                srun_optlist += [
                    '--nodelist=' + ','.join(self.srun_nodes),
                    '--nodes=' + str(len(self.srun_nodes)),
                ]
        command = super(SrunExecutionDriver, self).command
        return [self.srun] + srun_optlist + command

    @cached_property
    def srun_nodes(self):
        """Get list of nodes where to execute the command
        """
        count = self.execution.get('srun_nodes', 0)
        if isinstance(count, six.string_types):
            tag = count
            count = 0
        elif isinstance(count, SEQUENCES):
            return count
        else:
            assert isinstance(count, int)
            tag = self.tag
        nodes = self._srun_nodes(tag, count)
        if 'srun_nodes' in self.execution:
            self.execution['srun_nodes'] = nodes
            self.execution['srun_nodes_count'] = len(nodes)
        return nodes

    def _srun_nodes(self, tag, count):
        assert count >= 0
        if tag != '*' and not self.root.network.has_tag(tag):
            raise ValueError('Unknown tag: {}'.format(tag))
        nodes = sorted(list(self.root.network.nodes(tag)))
        if count > 0:
            return self._filter_srun_nodes(nodes, count)
        return nodes

    def _filter_srun_nodes(self, nodes, count):
        assert count <= len(nodes)
        pos = nodes.index(self.node)
        nodes = nodes * 2
        return nodes[pos : pos + count]


# coding: utf-8

# 2^j が 123 で始まる 678910 番目の j を求める.
#
# N = 2^j  ==>  log10(N) = jlog10(2) より, jlog10(2) = d + r (d は正の整数, 0 < r < 1)
# とおくと, N は (d+1) 桁であり, N = 10^d * 10^r である.
# 10^d は先頭の数値に影響しないので, 10^r の先頭が 123 か判定する.

from math import log10
from itertools import count, islice

def gen_p(L):
    d_L = int(log10(L))
    log10_L = log10(L)
    log10_L1 = log10(L + 1)
    log10_2 = log10(2)

    for j in count(1):
        d = log10_2 * j
        int_d = int(d)

        # L < 10**(d - int_d + d_L) < L + 1 <==>
        # log10(L) < d - int_d + d_L < log10(L + 1)
        if log10_L < d - int_d + d_L < log10_L1:
            yield j

def main():
    L = 123
    n = 678910

    return next(islice(gen_p(L), n - 1, None))

if __name__ == '__main__':
    print(main())


import json
from .models import *
from django.shortcuts import render
from django.shortcuts import get_object_or_404, redirect, render
from django.db.models import Q
from django.views.generic import ListView, DetailView

# class SchoolListView(ListView):
#     model = School
#     context_object_name = 'schools'
#     template_name = 'schools/index.html'
#     ordering = ['date_added']

# class SchoolDetailView(DetailView):
#     model = School
#     # teachers = School.teacher_set.all()
#     template_name = 'schools/detail.html'

def schools(req):
    # data = whishlist(req)
    # wishes = data['wishes']

    # schools = School.objects.all()
    query = req.GET.get('query') if req.GET.get('query') != None else ''
    schools = School.objects.filter(
        Q(name__icontains=query) | Q(address__icontains=query))
    edu_levels = EduLevel.objects.all()
    ordering = ['is_featured == True']
    context = {'schools': schools,
               'edu_levels': edu_levels,  'title': 'schools', 'ordering': ordering}

    return render(req, 'schools/index.html', context)

def school(req, slug):
    school = School.objects.get(slug=slug)
    # people
    teachers = school.teacher_set.all()
    # structures
    libraries = school.library_set.all()
    canteens = school.canteen_set.all()
    laboratories = school.laboratory_set.all()
    classrooms = school.classroom_set.all()
    # information
    reports = school.report_set.all()
    advantages = school.advantage_set.all()

    context = {'title': 'school',
               'school': school,
               'teachers': teachers,
               'classrooms': classrooms,
               'laboratories': laboratories,
               'libraries': libraries,
               'canteens': canteens,
               'reports': reports,
               'advantages': advantages
               }
    return render(req, 'schools/detail.html', context)

def allSchools(req):
    query = req.GET.get('query') if req.GET.get('query') != None else ''
    schools = School.objects.filter(
        Q(name__icontains=query) | Q(address__icontains=query))
    edu_levels = EduLevel.objects.all()
    context = {'schools': schools,
               'edu_levels': edu_levels,  'title': 'all-schools'}

    return render(req, 'schools/all-schools.html', context)

def favs(req):
    # data = whishlist(req)
    # wishes = data['wishes']
    # order = data['order']
    # items = data['items']

    context = {
        # 'items': items, 'order': order, 'wishes': wishes,
        'title': 'favorites'
    }
    return render(req, 'schools/favs.html', context)