"""Define the default Vector class."""
from __future__ import division

from copy import deepcopy
import numbers

from six import iteritems, itervalues
from six.moves import zip

import numpy as np

from openmdao.vectors.vector import Vector, INT_DTYPE
from openmdao.vectors.default_transfer import DefaultTransfer

real_types = (numbers.Real, np.float32, np.float64)

class DefaultVector(Vector):
    """
    Default NumPy vector.
    """

    TRANSFER = DefaultTransfer

    def _create_data(self):
        """
        Allocate list of arrays, one for each var_set.

        This happens only in the top level system.  Child systems use views of the arrays
        we allocate here.

        Returns
        -------
        [ndarray[:], ...]
            list of zeros arrays of correct size, one for each var_set.
        """
        system = self._system
        type_ = self._typ
        iproc = self._iproc
        ncol = self._ncol

        sizes_byset_t = system._var_sizes_byset[self._name][type_]
        sizes_t = system._var_sizes[self._name][type_]

        data = {}
        indices = {}
        nsets = len(sizes_byset_t)  # if we only have 1 varset, we can do some speedups
        for set_name in system._num_var_byset[self._name][type_]:
            size = np.sum(sizes_byset_t[set_name][iproc, :])
            data[set_name] = np.zeros(size) if ncol == 1 else np.zeros((size, ncol))
            if nsets == 1:
                indices[set_name] = slice(None)
            else:
                indices[set_name] = np.empty(size, int)

        if nsets > 1:
            abs2meta = system._var_abs2meta
            allprocs_abs2idx_byset_t = system._var_allprocs_abs2idx_byset[self._name]
            allprocs_abs2idx_t = system._var_allprocs_abs2idx[self._name]
            for abs_name in system._var_relevant_names[self._name][type_]:
                set_name = abs2meta[abs_name]['var_set']

                idx_byset = allprocs_abs2idx_byset_t[abs_name]
                ind_byset1 = np.sum(sizes_byset_t[set_name][iproc, :idx_byset])
                ind_byset2 = ind_byset1 + sizes_byset_t[set_name][iproc, idx_byset]

                idx = allprocs_abs2idx_t[abs_name]
                ind1 = np.sum(sizes_t[iproc, :idx])
                ind2 = ind1 + sizes_t[iproc, idx]

                indices[set_name][ind_byset1:ind_byset2] = np.arange(ind1, ind2)

        return data, indices

    def _update_root_data(self):
        """
        Resize the root data if necesary (i.e., due to reconfiguration).
        """
        system = self._system
        type_ = self._typ
        vec_name = self._name
        iproc = self._iproc
        root_vec = self._root_vector

        _, tmp_indices = self._create_data()

        ext_sizes_t = system._ext_sizes[vec_name][type_]
        int_sizes_t = np.sum(system._var_sizes[vec_name][type_][iproc, :])
        old_sizes_total = np.sum([len(data) for data in itervalues(root_vec._data)])

        old_sizes = (
            ext_sizes_t[0],
            old_sizes_total - ext_sizes_t[0] - ext_sizes_t[1],
            ext_sizes_t[1],
        )
        new_sizes = (
            ext_sizes_t[0],
            int_sizes_t,
            ext_sizes_t[1],
        )

        sizes_byset = system._var_sizes_byset[vec_name]
        nsets = len(sizes_byset)

        if nsets > 1:
            for set_name in system._num_var_byset[self._name][type_]:
                if isinstance(tmp_indices[set_name], slice):
                    start, stop, _ = tmp_indices[set_name].indices(old_sizes_total)
                    tmp_indices[set_name] = np.arange(start, stop, dtype=int)

                if isinstance(root_vec._indices[set_name], slice):
                    start, stop, _ = \
                        root_vec._indices[set_name].indices(root_vec._data[set_name].size)
                    root_vec._indices[set_name] = np.arange(start, stop, dtype=int)

        for set_name in system._num_var_byset[self._name][type_]:
            ext_sizes_byset_t = system._ext_sizes_byset[vec_name][type_][set_name]
            int_sizes_byset_t = np.sum(sizes_byset[type_][set_name][iproc, :])
            old_sizes_total_byset = len(root_vec._data[set_name])

            old_sizes_byset = (
                ext_sizes_byset_t[0],
                old_sizes_total_byset - ext_sizes_byset_t[0] - ext_sizes_byset_t[1],
                ext_sizes_byset_t[1],
            )
            new_sizes_byset = (
                ext_sizes_byset_t[0],
                int_sizes_byset_t,
                ext_sizes_byset_t[1],
            )

            root_vec._data[set_name] = np.concatenate([
                root_vec._data[set_name][:old_sizes_byset[0]],
                np.zeros(new_sizes_byset[1]),
                root_vec._data[set_name][old_sizes_byset[0] + old_sizes_byset[1]:],
            ])

            if nsets == 1:
                root_vec._indices[set_name] = slice(None)
            else:
                root_vec._indices[set_name] = np.concatenate([
                    root_vec._indices[set_name][:old_sizes_byset[0]],
                    tmp_indices[set_name] + new_sizes[0],
                    root_vec._indices[set_name][old_sizes_byset[0] + old_sizes_byset[1]:]
                        + new_sizes[1] - old_sizes[1],
                ])

        root_vec._initialize_views()

    def _extract_data(self):
        """
        Extract views of arrays from root_vector.

        Returns
        -------
        [ndarray[:], ...]
            list of zeros arrays of correct size, one for each var_set.
        """
        system = self._system
        type_ = self._typ
        iproc = self._iproc
        root_vec = self._root_vector

        offset = system._ext_sizes[self._name][type_][0]
        sizes_byset = system._var_sizes_byset[self._name][type_]

        data = {}
        imag_data = {}
        indices = {}
        scaling = {}
        if self._do_scaling:
            scaling['phys'] = {}
            scaling['norm'] = {}

        nsets = len(sizes_byset)  # if we only have 1 varset, we can do some speedups

        for set_name, sizes in iteritems(sizes_byset):
            ind_byset1 = system._ext_sizes_byset[self._name][type_][set_name][0]
            ind_byset2 = ind_byset1 + np.sum(sizes[iproc, :])

            data[set_name] = root_vec._data[set_name][ind_byset1:ind_byset2]
            if nsets == 1:
                indices[set_name] = slice(None)
            else:
                indices[set_name] = root_vec._indices[set_name][ind_byset1:ind_byset2] - offset

            # Extract view for imaginary part too
            if self._alloc_complex:
                if root_vec._alloc_complex:
                    imag_data[set_name] = root_vec._imag_data[set_name][ind_byset1:ind_byset2]
                else:
                    shape = root_vec._data[set_name][ind_byset1:ind_byset2].shape
                    imag_data[set_name] = np.zeros(shape)

            if self._do_scaling:
                for typ in ('phys', 'norm'):
                    root_scale = root_vec._scaling[typ][set_name]
                    rs0 = root_scale[0]
                    if rs0 is None:
                        scaling[typ][set_name] = (rs0, root_scale[1][ind_byset1:ind_byset2])
                    else:
                        scaling[typ][set_name] = (rs0[ind_byset1:ind_byset2],
                                                  root_scale[1][ind_byset1:ind_byset2])

        return data, imag_data, scaling, indices

    def _initialize_data(self, root_vector):
        """
        Internally allocate vectors.

        Sets the following attributes:
        _data
        _indices

        Parameters
        ----------
        root_vector : Vector or None
            the root's vector instance or None, if we are at the root.
        """
        if root_vector is None:  # we're the root
            self._data, self._indices = self._create_data()

            if self._do_scaling:
                self._scaling = {'phys': {}, 'norm': {}}
                for set_name, data in iteritems(self._data):
                    if self._name == 'linear':
                        # reuse the nonlinear scaling vecs since they're the same as ours
                        nlvec = self._system._root_vecs[self._kind]['nonlinear']
                        self._scaling['phys'][set_name] = (None,
                                                           nlvec._scaling['phys'][set_name][1])
                        self._scaling['norm'][set_name] = (None,
                                                           nlvec._scaling['norm'][set_name][1])
                    else:
                        dphys1 = np.ones(data.size)
                        dnorm1 = np.ones(data.size)
                        if self._name == 'nonlinear':
                            dphys0 = np.zeros(data.size)
                            dnorm0 = np.zeros(data.size)
                        else:
                            dphys0 = dnorm0 = None
                        self._scaling['phys'][set_name] = (dphys0, dphys1)
                        self._scaling['norm'][set_name] = (dnorm0, dnorm1)

            # Allocate imaginary for complex step
            if self._alloc_complex:
                self._imag_data = deepcopy(self._data)

        else:
            self._data, self._imag_data, self._scaling, self._indices = self._extract_data()

    def _initialize_views(self):
        """
        Internally assemble views onto the vectors.

        Sets the following attributes:
        _views
        _views_flat
        """
        system = self._system
        type_ = self._typ
        kind = self._kind
        iproc = self._iproc
        ncol = self._ncol
        do_scaling = self._do_scaling
        if do_scaling:
            factors = system._scale_factors
            scaling = self._scaling

        self._views = views = {}
        self._views_flat = views_flat = {}

        alloc_complex = self._alloc_complex
        self._imag_views = imag_views = {}
        self._imag_views_flat = imag_views_flat = {}

        allprocs_abs2idx_byset_t = system._var_allprocs_abs2idx_byset[self._name]
        sizes_byset_t = system._var_sizes_byset[self._name][type_]
        abs2meta = system._var_abs2meta
        for abs_name in system._var_relevant_names[self._name][type_]:
            idx_byset = allprocs_abs2idx_byset_t[abs_name]
            set_name = abs2meta[abs_name]['var_set']

            ind_byset1 = np.sum(sizes_byset_t[set_name][iproc, :idx_byset])
            ind_byset2 = ind_byset1 + sizes_byset_t[set_name][iproc, idx_byset]
            shape = abs2meta[abs_name]['shape']
            if ncol > 1:
                if not isinstance(shape, tuple):
                    shape = (shape,)
                shape = tuple(list(shape) + [ncol])

            views_flat[abs_name] = v = self._data[set_name][ind_byset1:ind_byset2]
            if shape != v.shape:
                v = v.view()
                v.shape = shape
            views[abs_name] = v

            if alloc_complex:
                imag_views_flat[abs_name] = v = self._imag_data[set_name][ind_byset1:ind_byset2]
                if shape != v.shape:
                    v = v.view()
                    v.shape = shape
                imag_views[abs_name] = v

            if do_scaling:
                for scaleto in ('phys', 'norm'):
                    scale0, scale1 = factors[abs_name][kind, scaleto]
                    vec = scaling[scaleto][set_name]
                    if vec[0] is not None:
                        vec[0][ind_byset1:ind_byset2] = scale0
                    vec[1][ind_byset1:ind_byset2] = scale1

        self._names = frozenset(views)

    def _clone_data(self):
        """
        For each item in _data, replace it with a copy of the data.
        """
        for set_name, data in iteritems(self._data):
            self._data[set_name] = np.array(data)

        if self._vector_info._under_complex_step:
            for set_name, data in iteritems(self._imag_data):
                self._imag_data[set_name] = np.array(data)

    def __iadd__(self, vec):
        """
        Perform in-place vector addition.

        Parameters
        ----------
        vec : <Vector>
            vector to add to self.

        Returns
        -------
        <Vector>
            self + vec
        """
        for set_name, data in iteritems(self._data):
            data += vec._data[set_name]

        if vec._alloc_complex and self._vector_info._under_complex_step:
            for set_name, data in iteritems(self._imag_data):
                data += vec._imag_data[set_name]
        return self

    def __isub__(self, vec):
        """
        Perform in-place vector substraction.

        Parameters
        ----------
        vec : <Vector>
            vector to subtract from self.

        Returns
        -------
        <Vector>
            self - vec
        """
        for set_name, data in iteritems(self._data):
            data -= vec._data[set_name]
        if vec._alloc_complex and self._vector_info._under_complex_step:
            for set_name, data in iteritems(self._imag_data):
                data -= vec._imag_data[set_name]
        return self

    def __imul__(self, val):
        """
        Perform in-place scalar multiplication.

        Parameters
        ----------
        val : int or float
            scalar to multiply self.

        Returns
        -------
        <Vector>
            self * val
        """
        if self._vector_info._under_complex_step:
            r_val = np.real(val)
            i_val = np.imag(val)
            for key in self._data:
                r_data = self._data[key]
                i_data = self._imag_data[key]
                self._data[key] = r_val * r_data + i_val * i_data
                self._imag_data[key] = r_val * i_data + i_val * r_data
        else:
            for data in itervalues(self._data):
                data *= val
        return self

    def add_scal_vec(self, val, vec):
        """
        Perform in-place addition of a vector times a scalar.

        Parameters
        ----------
        val : int or float
            scalar.
        vec : <Vector>
            this vector times val is added to self.
        """
        if self._vector_info._under_complex_step:
            r_val = np.real(val)
            i_val = np.imag(val)
            for set_name, data in iteritems(self._data):
                data += r_val * vec._data[set_name] + i_val * vec._imag_data[set_name]
            for set_name, data in iteritems(self._imag_data):
                data += i_val * vec._data[set_name] + r_val * vec._imag_data[set_name]
        else:
            for set_name, data in iteritems(self._data):
                data += val * vec._data[set_name]

    def set_vec(self, vec):
        """
        Set the value of this vector to that of the incoming vector.

        Parameters
        ----------
        vec : <Vector>
            the vector whose values self is set to.
        """
        for set_name, data in iteritems(self._data):
            data[:] = vec._data[set_name]
        if self._vector_info._under_complex_step:
            for set_name, data in iteritems(self._imag_data):
                data[:] = vec._imag_data[set_name][:]

    def set_const(self, val):
        """
        Set the value of this vector to a constant scalar value.

        Parameters
        ----------
        val : int or float
            scalar to set self to.
        """
        for data in itervalues(self._data):
            data[:] = val

    def dot(self, vec):
        """
        Compute the dot product of the real parts of the current vec and the incoming vec.

        Parameters
        ----------
        vec : <Vector>
            The incoming vector being dotted with self.

        Returns
        -------
        float
            The computed dot product value.
        """
        global_sum = 0
        for set_name, data in iteritems(self._data):
            global_sum += np.dot(data, vec._data[set_name])

        return global_sum

    def get_norm(self):
        """
        Return the norm of this vector.

        Returns
        -------
        float
            norm of this vector.
        """
        global_sum = 0
        for data in itervalues(self._data):
            global_sum += np.sum(data**2)
        return global_sum ** 0.5

    def _enforce_bounds_vector(self, du, alpha, lower_bounds, upper_bounds):
        """
        Enforce lower/upper bounds, backtracking the entire vector together.

        This method modifies both self (u) and step (du) in-place.

        Parameters
        ----------
        du : <Vector>
            Newton step; the backtracking is applied to this vector in-place.
        alpha : float
            step size.
        lower_bounds : <Vector>
            Lower bounds vector.
        upper_bounds : <Vector>
            Upper bounds vector.
        """
        u = self

        # The assumption is that alpha * du has been added to self (i.e., u)
        # just prior to this method being called. We are currently in the
        # initialization of a line search, and we're trying to ensure that
        # the u does not violate bounds in the first iteration. If it does,
        # we modify the du vector directly.

        # This is the required change in step size, relative to the du vector.
        d_alpha = 0

        # Loop over varsets and find the largest amount a bound is violated
        # where positive means a bound is violated - i.e. the required d_alpha.
        for u_data, du_data, lower_data, upper_data in zip(
                itervalues(u._data), itervalues(du._data),
                itervalues(lower_bounds._data), itervalues(upper_bounds._data)):

            mask = du_data != 0
            if mask.any():
                abs_du_mask = np.abs(du_data[mask])
                u_mask = u_data[mask]

                # Check lower bound
                max_d_alpha = np.amax((lower_data[mask] - u_mask) / abs_du_mask)
                if max_d_alpha > d_alpha:
                    d_alpha = max_d_alpha

                # Check upper bound
                max_d_alpha = np.amax((u_mask - upper_data[mask]) / abs_du_mask)
                if max_d_alpha > d_alpha:
                    d_alpha = max_d_alpha

        if d_alpha > 0:
            # d_alpha will not be negative because it was initialized to be 0
            # and we've only done max operations.
            # d_alpha will not be greater than alpha because the assumption is that
            # the original point was valid - i.e., no bounds were violated.
            # Therefore 0 <= d_alpha <= alpha.

            # We first update u to reflect the required change to du.
            u.add_scal_vec(-d_alpha, du)

            # At this point, we normalize d_alpha by alpha to figure out the relative
            # amount that the du vector has to be reduced, then apply the reduction.
            du *= 1 - d_alpha / alpha

    def _enforce_bounds_scalar(self, du, alpha, lower_bounds, upper_bounds):
        """
        Enforce lower/upper bounds on each scalar separately, then backtrack as a vector.

        This method modifies both self (u) and step (du) in-place.

        Parameters
        ----------
        du : <Vector>
            Newton step; the backtracking is applied to this vector in-place.
        alpha : float
            step size.
        lower_bounds : <Vector>
            Lower bounds vector.
        upper_bounds : <Vector>
            Upper bounds vector.
        """
        u = self

        # The assumption is that alpha * step has been added to this vector
        # just prior to this method being called. We are currently in the
        # initialization of a line search, and we're trying to ensure that
        # the initial step does not violate bounds. If it does, we modify
        # the step vector directly.

        # Loop over varsets and enforce bounds on step in-place.
        for u_data, du_data, lower_data, upper_data in zip(
                itervalues(u._data), itervalues(du._data),
                itervalues(lower_bounds._data), itervalues(upper_bounds._data)):

            # If u > lower, we're just adding zero. Otherwise, we're adding
            # the step required to get up to the lower bound.
            # For du, we normalize by alpha since du eventually gets
            # multiplied by alpha.
            change_lower = np.maximum(u_data, lower_data) - u_data

            # If u < upper, we're just adding zero. Otherwise, we're adding
            # the step required to get down to the upper bound, but normalized
            # by alpha since du eventually gets multiplied by alpha.
            change_upper = np.minimum(u_data, upper_data) - u_data

            change = change_lower + change_upper

            u_data += change
            du_data += change / alpha

    def _enforce_bounds_wall(self, du, alpha, lower_bounds, upper_bounds):
        """
        Enforce lower/upper bounds on each scalar separately, then backtrack along the wall.

        This method modifies both self (u) and step (du) in-place.

        Parameters
        ----------
        du : <Vector>
            Newton step; the backtracking is applied to this vector in-place.
        alpha : float
            step size.
        lower_bounds : <Vector>
            Lower bounds vector.
        upper_bounds : <Vector>
            Upper bounds vector.
        """
        u = self

        # The assumption is that alpha * step has been added to this vector
        # just prior to this method being called. We are currently in the
        # initialization of a line search, and we're trying to ensure that
        # the initial step does not violate bounds. If it does, we modify
        # the step vector directly.

        # Loop over varsets and enforce bounds on step in-place.
        for u_data, du_data, lower_data, upper_data in zip(
                itervalues(u._data), itervalues(du._data),
                itervalues(lower_bounds._data), itervalues(upper_bounds._data)):

            # If u > lower, we're just adding zero. Otherwise, we're adding
            # the step required to get up to the lower bound.
            # For du, we normalize by alpha since du eventually gets
            # multiplied by alpha.
            change_lower = np.maximum(u_data, lower_data) - u_data

            # If u < upper, we're just adding zero. Otherwise, we're adding
            # the step required to get down to the upper bound, but normalized
            # by alpha since du eventually gets multiplied by alpha.
            change_upper = np.minimum(u_data, upper_data) - u_data

            change = change_lower + change_upper

            u_data += change
            du_data += change / alpha

            # Now we ensure that we will backtrack along the wall during the
            # line search by setting the entries of du at the bounds to zero.
            changed_either = change.astype(bool)
            du_data[changed_either] = 0.

    def __getstate__(self):
        """
        Return state as a dict.

        For pickling vectors in case recording, we want to get rid of
        the system contained within Vectors, because MPI Comm objects cannot
        be pickled using Python3's pickle module.

        Returns
        -------
        dict
            state minus system member.
        """
        state = self.__dict__.copy()
        del state['_system']
        return state


# -*- coding: utf-8 -*-

import re

import os
import shutil

class Static:

    namespace = set()

def def_namespace(path: str, newpath: str) -> bool:

    if path in Static.namespace:
        return False

    Static.namespace.add(newpath)

    return True

def is_valid_path(*args, **kwargs) -> bool:

    for path in args:

        if not os.path.exists(path):

            print(f'path: {path}, doesn\'t exist')

            return False

    for key, path in kwargs.items():

        if not os.path.exists(path):

            print(f'{key}-path: {path}, doesn\'t exist')

            return False

    return True

def def_depth(path: str, iformat: str = '.') -> int:

    nlevels = path.count(iformat)

    return nlevels - 1

def def_rdepth(dest: str, path: str, iformat: str = '/') -> int:

    nlevels = path.count(iformat)

    if path.startswith(dest):

        nlevels = path[len(dest):].count(iformat)

    return nlevels - 1

def path_join(dirpath: str, fname: str, fext: str):

    path = os.path.join(dirpath, fname) + fext

    return path

def realpath(path: str):

    rpath = os.path.realpath(path)

    return rpath

def sstrip(sstr: str, substr: str):

    l, r = 0, len(sstr)
    ls, rs = 1, 1

    while (r >= l) and (ls or rs):

        ls = int(sstr.startswith(substr, l, r))
        rs = int(sstr.endswith(substr, l, r))

        l += ls * len(substr)
        r -= rs * len(substr)

    return sstr[l:r]

def normalize_str(sstr: str):

    # sstr = re.sub(r'[^a-zA-Z0-9]', '', sstr)

    sstr = re.sub(r'\s+', '', sstr)
    sstr = re.sub(r'[-|_]', '', sstr)
    sstr = sstr.lower()
    sstr = sstrip(sstr, '.')

    return sstr

def rclip(sstr: str, iformat: str, clipat: int):

    levels = sstr.split(iformat)

    if clipat >= len(levels):

        return sstr

    sstr = iformat.join(levels[-clipat:])

    return sstr

def find_ext(src: str, *ext):

    for (dirpath, _, filenames) in os.walk(src):

        for i in range(len(filenames)):

            fname, fext = os.path.splitext(filenames[i])

            for j in range(len(ext)):

                if fext == ext[j]:

                    yield dirpath, fname, fext

def ffind_ext(src: str, ext: str, src_include: bool = False, iformat: str = '.'):

    size = len(src)

    if src_include:

        size = 0

    for pargs in find_ext(src, ext):

        path = path_join(*pargs)

        newpath = path[size:].replace('/', iformat)
        newpath = normalize_str(newpath)

        yield path, newpath

#  [...........] =======================================================================================================

def fcopy_ext(src: str, dest: str, ext: str, src_include: bool = False, iformat: str = '.', clipat: int = 5):

    src = realpath(src)
    dest = realpath(dest)

    if not is_valid_path(src, dest):

        return

    pgen = ffind_ext(src, ext, src_include, iformat)

    for path, newpath in pgen:

        if not def_rdepth(dest, path):
            continue

        newpath = rclip(newpath, iformat, clipat)
        newpath = os.path.join(dest, newpath)

        if path == newpath:
            continue

        if not def_namespace(path, newpath):
            continue

        shutil.copyfile(path, newpath)

        print(newpath)

def fmove_ext(src: str, dest: str, ext: str, src_include: bool = False, iformat: str = '.', clipat: int = 5):

    src = realpath(src)
    dest = realpath(dest)

    if not is_valid_path(src, dest):

        return

    pgen = ffind_ext(src, ext, src_include, iformat)

    for path, newpath in pgen:

        if not def_rdepth(dest, path):
            continue

        newpath = rclip(newpath, iformat, clipat)
        newpath = os.path.join(dest, newpath)

        if not def_namespace(path, newpath):
            continue

        os.rename(path, newpath)

        print(newpath)

def args_info(func):

    count = func.__code__.co_argcount
    name = func.__code__.co_varnames
    defaults = func.__defaults__
    types = func.__annotations__

    return count, name, defaults, types

def unsafe_call(func):

    count, name, defaults, types = args_info(func)

    ecount = count - len(defaults)

    args = []

    for i in range(count):

        value = input(name[i] + ': ')

        if not len(value) and i >= ecount:

            args.append(defaults[i - ecount])

        else:

            value = types[name[i]](value)

            args.append(value)

    func(*args)

#  [LatexSyntax] =======================================================================================================

def incb(sstr: str):
    """ in_curly_braces """
    return '{' + sstr + '}'

def parse_kwargs(**kwargs):

    size = len(kwargs)

    line = ''

    for i, (key, value) in enumerate(kwargs.items()):

        line += f'{key}={value}'

        if i != size - 1:
            line += ', '

    return line

def usepackage(packname: str, **options):

    return f'\\usepackage[{parse_kwargs(**options)}]{incb(packname)}\n'

#  [...........] =======================================================================================================

def readlines(path: str):

    if not is_valid_path(path):

        return []

    with open(path, 'r') as buffer:

        return buffer.readlines()

def package(name: str, src: str, childof: str = ''):

    src = realpath(src)

    if not is_valid_path(src):
        return

    name = os.path.splitext(name)[0]

    pack_path = os.path.join(src, name) + '.sty'

    filenames = os.listdir(src)

    lines = readlines(pack_path)

    with open(pack_path, 'a') as pack:

        for i in range(len(filenames)):

            fname, fext = os.path.splitext(filenames[i])

            if fname == name:
                continue

            if childof not in fname:
                continue

            if fext == '.sty':

                code = usepackage(fname)

                if code not in lines:

                    pack.write(code)

#  [...........] =======================================================================================================

def run_cmd(cmd):

    print('\n' + realpath('./') + '\n')

    if cmd == 'mv':

        unsafe_call(fmove_ext)

    elif cmd == 'cp':

        unsafe_call(fcopy_ext)

    elif cmd == 'pk':

        unsafe_call(package)

    else:

        print('Command not found')

#  [...........] =======================================================================================================

# package('custom.sty', '../test', 'custom')
# package('definitions.sty', '../test', 'custom.definition')


import sympy as sm
import numpy as np

print('='*15,'TUGAS BESAR ALOGAIRTMA DAN PEMROGRAMAN','='*15)
print('''
                +--------------------------------+
                |    SYARIIF ABDURRAHMAN BATHIK  |
                |           1202194114           |
                |            SI-43-09            |
                +--------------------------------+
''')

class HillCipher(): # Class untuk Hill Chiper
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def getKey(self): #Fungsi untuk matrix KEY agar merubah character menjadi angka
        k = 0
        for i in range(3):
            for j in range(3):
                keyMatrix[i][j] = ord(self.y[k]) % 65 # American Standart Code for Information Interchange
                k += 1

    def getInversKey(self): #fungsi untuk menginvers KEY matrix
        self.getKey()
        convertSM = sm.Matrix(keyMatrix)
        minorCoFactorKeyMatrix = convertSM.adjugate() #untuk mencari kofaktor
        determinantKeyMatrix = convertSM.det() #untuk mencari determinan

        inverseKeyMatrix = (determinantKeyMatrix * minorCoFactorKeyMatrix % 26) #invers key matriks harus tetap modulus 26
        inverseKeyMatrix = inverseKeyMatrix.tolist()

        return inverseKeyMatrix

    def encryptData(self, messageMatrix): #fungsi encrypt data
        for i in range(3):
            for j in range(1):
                cipherMatrix[i][j] = 0
                for x in range(3):
                    cipherMatrix[i][j] += (keyMatrix[i][x] * messageMatrix[x][j])
                    cipherMatrix[i][j] = cipherMatrix[i][j] % 26

        return cipherMatrix

    def decryptData(self, cipherMatrix): #fungsi decrypt data
        inverseKeyMatrix = self.getInversKey()
        cipherMatrix = self.encryptData(messageMatrix)
        for i in range(3):
            for j in range(1):
                messageMatrix[i][j] = 0
                for x in range(3):
                    messageMatrix[i][j] += (inverseKeyMatrix[i][x] * cipherMatrix[x][j])
                    messageMatrix[i][j] = messageMatrix[i][j] % 26

    def processEncrypt(self):
        self.getKey()
        for i in range(3):
            messageMatrix[i][0] = ord(self.x[i]) % 65 #untuk merubah char ke angka yg merepresentasikannya

        self.encryptData(messageMatrix)

        CipherText = []
        for i in range(3):
            CipherText.append(chr(cipherMatrix[i][0] + 65)) #untuk convert string ke char yang merepresentasikannya
        print("Ciphertext: ", "".join(CipherText))

    def processDecrypt(self):
        self.decryptData(cipherMatrix)
        for i in range(3):
            cipherMatrix[i][0] = ord(self.x[i]) % 65

        CipherText = []
        for i in range(3):
            CipherText.append(chr(cipherMatrix[i][0] + 65))
        print("Decrypted Message: ", "".join(CipherText))

if __name__ == "__main__":
    Message = input('Input Text To Encrypt :')

    keyMatrix = [[0] * 3 for i in range(3)]
    messageMatrix = [[0] for i in range(3)]
    cipherMatrix = [[0] for i in range(3)]

    default_Key = "HILLMAGIC"

output = HillCipher(Message, default_Key)
output.processEncrypt()
decrypt = input('Do You Want To Decrypt The Message ? (y/n) :')
if (decrypt == 'Y' or decrypt == 'y'):
    output.processDecrypt()
    print('Ok Thank You :)')
elif (decrypt == 'N' or decrypt == 'n'):
    print('Ok Thank You :)')
else:
    print('Wrong Input !!!')


from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.plugins.lookup import LookupBase
from ansible.module_utils.six import string_types
from ansible.errors import AnsibleError

import os

class LookupModule(LookupBase):

    def run(self, terms, variables=None, **kwargs):

        results = []

        wantstate = kwargs.pop('wantstate', None)

        files          = self._flatten(terms[0])
        filesExclusive = self._flatten(terms[1])
        filesDir       = terms[2]

        itemDefault = {
            'state': 'present'
        }

        # Mark exclusive files as absent
        for file in filesExclusive:
            item = itemDefault.copy()
            item.update({
                'file':  file['path'],
                'state': 'absent'
            })
            results.append(item)

        for file in files:

            items = []

            # Short syntax
            if isinstance(file, string_types):
                item = itemDefault.copy()
                item.update({
                    'url': file
                })
            else:
                # Must be a dict
                if not isinstance(file, dict):
                    raise AnsibleError('Expect a dict')

                if ('file' not in file) and ('url' not in file):
                    raise AnsibleError('Expect "url" or "file" key')

                if (file.get('state') == 'present') and ('url' not in file):
                    raise AnsibleError('Expect "url" key for present state')

                item = itemDefault.copy()
                item.update(file)

            if 'url' in item and 'file' not in item:
                item.update({
                    'file': os.path.basename(item['url'])
                })

            item.update({
                'file': os.path.join(filesDir, item['file'])
            })

            items.append(item)

            # Merge by index key
            for item in items:
                itemFound = False
                for i, result in enumerate(results):
                    if result['file'] == item['file']:
                        results[i] = item
                        itemFound = True
                        break

                if not itemFound:
                    results.append(item)

        # Filter by state
        if wantstate:
            results = [result for result in results if result.get('state') == wantstate]

        return results


'''
Created on Jan 1, 2019

@author: Anthony Kuzola
Renames any file og the format *.* (N) to *_N.*
'''
import os
import sys
import re

def number_dups(test_source_dir):
    '''
    Called on each directory

    Parameters: test_source_dir is the directory being backed up
                test_target_dir is the backup destination
    '''
    files_renamed = 0

    for dir_name, dummy_sub_dir_list, file_list in os.walk(test_source_dir):
        new_name = "ouch.txt"
        cur_source_dir = dir_name

        for fname in file_list:
            cur_source_file = os.path.join(cur_source_dir, fname)
            # See if there is a number _(#) at the end of the file
            search_obj = re.search(r' \(\d+\)$', fname)
            if search_obj:
                make_unique = search_obj.group()
                print(fname + " has " + make_unique)
                rid_of_space = re.sub(r'\s', "_", make_unique)
                no_parens = re.sub(r'[\)\(]', "", rid_of_space)

                new_name = fname[:search_obj.start() - 4] + no_parens + fname[search_obj.start() - 4:search_obj.start()]
                print("New name is " + new_name)
                files_renamed += 1

                cur_target_file = os.path.join(cur_source_dir, new_name)
                cur_source_info = os.stat(cur_source_file)

                try:
                    os.rename(cur_source_file, cur_target_file)

                except PermissionError as perr:
                    msg = "Permission error: {}".format(perr)
                    print(msg)
                    print("Could not rename " + cur_source_info + " to " + cur_target_file)
                except Exception as e:
                    msg = "Unexpected error: {}".format(sys.exc_info()[0])
                    print(msg)
                    msg2 = "Exception info: {}".format(e)
                    print(msg2)
                    raise

    return(files_renamed)

target_dir = "C:\\Users\\tkuzo\\Desktop\\Transfer"

if __name__ == '__main__':
    renamed = number_dups(target_dir)
    print(renamed)


import fitz, os, time

rut = input('Ubicación del archivo: ')
pdf = input('Nombre del pdf (sin la extensión *.pdf): ') # A mejorar: detectar la terminación *.pdf

ruta = os.path.join(rut, '')

nom_pdf = "{}{}.pdf".format(ruta, pdf)

ruta_imgs = os.path.join('{}{}'.format(ruta, nom_pdf), '')

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

doc = fitz.open(nom_pdf)
for i in range(len(doc)):
    for img in doc.getPageImageList(i):
        xref = img[0]
        pix = fitz.Pixmap(doc, xref)
        if pix.n < 5:       # this is GRAY or RGB
            pix.writePNG("{}/{}-{}.png".format(pdf, i, xref))
            print("extraida la imagen {}-{}.png".format(i, xref))
        else:               # CMYK: convert to RGB first
            pix1 = fitz.Pixmap(fitz.csRGB, pix)
            pix1.writePNG("{}/{}-{}.png".format(pdf, i, xref))
            print("extraida la imagen {}-{}.png".format(i, xref))
            pix1 = None
        pix = None
doc.close()

print("Ha finalizado la extracción de las imágenes de su PDF")
wait = input("Presione ENTER para salir.")


import cv2 as cv
import numpy as np
import mediapipe as mp
import math
from PIL import Image
from numpy.lib.function_base import average

LEFT_EYE = [362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385, 384, 398]
RIGHT_EYE = [33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161, 246]
# points of iris
RIGHT_IRIS = [474, 475, 476, 477]
LEFT_IRIS = [469, 470, 471, 472]

L_H_LEFT = [33]  # right eye right most landmark
L_H_RIGHT = [133]  # right eye left most landmark
R_H_LEFT = [362]  # left eye right most landmark
R_H_RIGHT = [263]  # left eye left most landmark

R_U_landmark = [386]  # right eye up most landmark
R_D_landmark = [374]  # right eye down most landmark
L_U_landmark = [159]  # left eye up most landmark
L_D_landmark = [145]  # left eye down most landmark

def euclidean_distance(point1, point2):
    """
    calculate euclidean distance between two points
    input: point1, point2 : x and y for each point
    output: distance between point1 and point2

    """
    x1, y1 = point1.ravel()
    x2, y2 = point2.ravel()
    distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
    return distance

# thresholds represent ratio of distance between center of iris and point
right_thre = 0.42
left_thre = 0.57
up_thre = 0.25
down_thre = 0.58

def iris_position_in_eye(iris_center, right_point, left_point, up_point, down_point):
    """
    iris position :
    is return position of iris center and ratio_horizontal,ratio_vertical
    """

    center_to_right_dist = euclidean_distance(iris_center, right_point)
    center_to_up_dist = euclidean_distance(iris_center, up_point)

    total_distance_horizontal = euclidean_distance(right_point, left_point)
    total_distance_vertical = euclidean_distance(up_point, down_point)

    ratio_horizontal = center_to_right_dist / total_distance_horizontal

    if total_distance_vertical == 0:
        ratio_vertical = 0.1
    else:
        ratio_vertical = center_to_up_dist / total_distance_vertical

    iris_position = ""
    # iris in center horizontal
    if right_thre < ratio_horizontal <= left_thre:
        if ratio_vertical <= up_thre:
            iris_position = "up"
        elif up_thre < ratio_vertical <= down_thre:
            iris_position = "center"
        else:
            iris_position = "down"

    # iris in center vertical
    elif up_thre < ratio_vertical <= down_thre:
        if ratio_horizontal <= right_thre:
            iris_position = "right"
        elif right_thre < ratio_horizontal <= left_thre:
            iris_position = "center"
        else:
            iris_position = "left"

    # iris in right horizontal
    elif ratio_horizontal <= right_thre:
        if ratio_vertical <= up_thre:
            iris_position = "right _ up"
        elif ratio_vertical > down_thre:
            iris_position = "right _ down"

    # iris in left horizontal
    else:
        if ratio_vertical <= up_thre:
            iris_position = "left _ up"
        elif ratio_vertical > down_thre:
            iris_position = "left _ down"

    return iris_position, ratio_horizontal, ratio_vertical

def crop_face_from_frame(face_detection_model, frame):
    height, width, channels = frame.shape
    results = face_detection_model.process(frame)

    if results.detections:
        for detection in results.detections:
            results.detections[0].location_data.relative_bounding_box.xmin
            l, t = normaliz_pixel(detection.location_data.relative_bounding_box.xmin,
                                  detection.location_data.relative_bounding_box.ymin, width, height)
            r = detection.location_data.relative_bounding_box.width * width + l
            b = detection.location_data.relative_bounding_box.height * height + t

            l -= (r - l) * 0.3
            r += (r - l) * 0.3
            t -= (b - t) * 0.3
            b += (b - t) * 0.3

            bbox = ([l, t, r, b])
        frame = Image.fromarray(frame)
        face = frame.crop(bbox)
        frame = np.asarray(face)

    return frame

def detect_iris_on_frame(face_mesh_model, frame):
    with face_mesh_model.FaceMesh(
            max_num_faces=1,
            refine_landmarks=True,
            min_detection_confidence=0.5,
            min_tracking_confidence=0.5
    ) as face_mesh:
        img_h, img_w = frame.shape[:2]
        results = face_mesh.process(frame)
        if results.multi_face_landmarks:
            mesh_points = np.array([np.multiply([p.x, p.y], [img_w, img_h]).astype(int) for p in
                                    results.multi_face_landmarks[0].landmark])
            (l_cx, l_cy), l_radius = cv.minEnclosingCircle(mesh_points[LEFT_IRIS])
            (r_cx, r_cy), r_radius = cv.minEnclosingCircle(mesh_points[RIGHT_IRIS])

            center_left = np.array([l_cx, l_cy], dtype=np.int32)
            center_right = np.array([r_cx, r_cy], dtype=np.int32)

            cv.circle(frame, center_left, int(l_radius), (255, 0, 255), 1, cv.LINE_AA)
            cv.circle(frame, center_right, int(r_radius), (255, 0, 255), 1, cv.LINE_AA)
            cv.circle(frame, mesh_points[R_H_LEFT][0], 1, (255, 255, 255), -1, cv.LINE_AA)
            cv.circle(frame, mesh_points[R_H_RIGHT][0], 1, (0, 255, 255), -1, cv.LINE_AA)
            cv.circle(frame, mesh_points[R_U_landmark][0], 1, (255, 255, 255), -1, cv.LINE_AA)
            cv.circle(frame, mesh_points[R_D_landmark][0], 1, (0, 255, 255), -1, cv.LINE_AA)

            #             time.sleep(0.2)
            #             print("right eye: the most right",mesh_points[R_H_RIGHT][0][1])
            #             print("right eye: the most left",mesh_points[R_H_LEFT][0][1])
            #             print("right eye: the center of iris",center_right[1])

            iris_pos, ratio_h, ratio_v = iris_position_in_eye(center_right, mesh_points[R_H_RIGHT][0],
                                                              mesh_points[R_H_LEFT][0],
                                                              mesh_points[R_U_landmark][0],
                                                              mesh_points[R_D_landmark][0])
            #             print(iris_pos)

            cv.putText(frame, f"Iris pos: {iris_pos} {ratio_h:.2f} {ratio_v:.2f} ", (30, 30),
                       cv.FONT_HERSHEY_PLAIN, 1.2, (0, 255, 0), 1, cv.LINE_AA)
        return frame

def normaliz_pixel(normalized_x, normalized_y, image_width, image_height):
    """
    input:
    x ,y :  both normalized to [0.0, 1.0] by the image width and the image height
    image_width, image_height: represent width and height of the image

    output:
    x , y: represent point in the image by pixels
    """
    x_px = min(math.floor(normalized_x * image_width), image_width - 1)
    y_px = min(math.floor(normalized_y * image_height), image_height - 1)
    return x_px, y_px

def detect_eye_contact_unsupervised():
    """
    # the main method:
    """
    # face detection
    mp_face_detection = mp.solutions.face_detection
    # face mesh
    mp_face_mesh = mp.solutions.face_mesh

    cap = cv.VideoCapture(0)

    with mp_face_detection.FaceDetection(
            model_selection=1,
            min_detection_confidence=0.5) as face_detection:
        while True:
            # Capture frame by frame
            ret, frame = cap.read()
            if not ret:
                break
            # may be using the lane to take frame vertically or horizontally
            # frame = cv.flip(frame,1)

            frame = crop_face_from_frame(face_detection, frame)
            frame = detect_iris_on_frame(mp_face_mesh, frame)

            cv.imshow('img', frame)
            key = cv.waitKey(1)
            if key == ord('q'):
                break

    cap.release()
    cv.destroyAllWindows()

if __name__ == '__main__':
    detect_eye_contact_unsupervised(

    )


# Python script to run fIC analysis functions

# Import relevant libraries
import numpy as np
import matplotlib.pyplot as plt
import glob
import os
import sys
import pickle
from scipy.io import savemat
import pandas as pd

# Import fIC analysis functions
sys.path.insert(0,r'C:\Users\adam\OneDrive - University College London\UCL PhD\PhD Year 1\Projects\VERDICT Screening\Code\VERDICT-Screening\VERDICT Fitting\My Functions')
import fIC_analysis #type: ignore

# Read output folder
OutputFolder = str(open('output_folder.txt', 'r').read())

# OutputFolder = r"C:\Users\adam\OneDrive - University College London\UCL PhD\PhD Year 1\Projects\ISMRM Submission\Outputs"

# ===================================================

# Define ROI drawer
ROIdrawer = 'NT'

# Define ROI averaging type
avg_type = 'median'

# Define Model numbers to analyse
ModelNames= ['RDIex903000']#, 2, 11, 15]#[0,0.5,1,11,12,2,15,19,20,21]

# Loop over model numbers
for ModelName in ModelNames:

    # ROI Name
    if ROIdrawer == 'NT':
        ROIName = 'L1_b3000_NT'
    else:
        ROIName = 'L1'

    # Find list of patients
    fnames = glob.glob(f'{OutputFolder}\VERDICT outputs\*')#/Model {ModelNum}/fIC.mat')

    PatNums = [ os.path.basename(fname) for fname in fnames  ]

    # Create list for 'good PatNums' which have a well defined ROI from Natasha
    goodPatNums = []

    # For each patient, save lesion ROI masks and extract fICs
    for indx, PatNum in enumerate(PatNums):

        # print(f'== {PatNum}')

        # # Excluding ROIs
        if PatNum in ['INN_291', 'INN_167']:
            continue

        # If ROI mask exists, try and extract ROI
        try:

            '''Using Natasha's RTstruct ROIs'''
            # # Extract fICs
            if ROIdrawer == 'NT':
                fIC_analysis.extractROIfICs(PatNum,
                                            ROIName = ROIName,
                                            ModelName = ModelName,
                                            parameter = 'fIC',
                                            MaskType = 'numpy',
                                            VERDICT_output_path = rf"{OutputFolder}\VERDICT outputs",
                                            ROI_path = r"C:\Users\adam\OneDrive - University College London\UCL PhD\PhD Year 1\Projects\ISMRM Submission\Outputs\ROIs",
                                            # ROI_path = r"C:\Users\adam\OneDrive - University College London\UCL PhD\PhD Year 1\Projects\VERDICT Screening\Outputs\fIC ROIs",
                                            output_path = rf"{OutputFolder}\fIC results"
                                            )

            # '''Using my ROIs'''
            # if ROIdrawer == 'AP':
            #     fIC_analysis.extractROIfICs(
            #         PatNum,
            #         ROIName = ROIName,
            #         ModelNum = ModelNum,
            #         MaskType = 'Analyze',
            #         ROI_path = r"C:\Users\adam\OneDrive - University College London\UCL PhD\PhD Year 1\INNOVATE\INNOVATE my ROIs"
            #         )

            # Append to goodPatNums list
            goodPatNums.append(PatNum)

        # If no ROI RTstruct file, or error, remove patient number from list
        except:
            print(f'No ROI for {PatNum}')

    PatNums = goodPatNums

    # == Extract average and variance of fICs in ROIs

    # Calculate median fICs
    fIC_analysis.avgROIfICs(
        PatNums = PatNums,
        ROIName = ROIName,
        ModelName = ModelName,
        avg_type = avg_type,
        results_path = rf"{OutputFolder}\fIC results"
        )

    # # fIC variance across ROI
    # fIC_analysis.ROIvariance(PatNums = PatNums, ROIName = ROIName, ModelNum = ModelNum)

    # Apply ROC analysis
    ResultsDF, fpr, tpr, thresholds, roc_auc = fIC_analysis.fIC_ROC(
                                                                    ModelName = ModelName,
                                                                    avg_type = avg_type,
                                                                    results_path = rf"{OutputFolder}\fIC results"
                                                                    )

    with pd.option_context('display.max_rows', None, 'display.max_columns', None):  # more options can be specified also
        print(ResultsDF)

    # == Save results

    # Make folder
    try:
        os.makedirs(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}')
    except:
        None

    # Save results dataframe as pickle
    with open(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}/Results_DF.pickle', 'wb') as handle:
        pickle.dump(ResultsDF, handle, protocol=pickle.HIGHEST_PROTOCOL)

    # Save results data frame as matlab structure
    savemat(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}/ResultsDF.mat', {'ResultsDF': ResultsDF.to_dict(orient = 'list')})

    # Save ROC results
    np.save(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}/fpr.npy', fpr)
    np.save(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}/tpr.npy', tpr)
    np.save(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}/thresholds.npy', thresholds)
    np.save(f'{OutputFolder}/ROC results/{ROIdrawer}/{ModelName}/{avg_type}/roc_auc.npy', roc_auc)

    print(roc_auc)

    Youden_Indices = tpr-fpr

    print(f'ROC AUC: {roc_auc}')
    print(f'Thresholds: {thresholds}')
    print(f' Best threshold: {thresholds[Youden_Indices == np.max(Youden_Indices)]}')

    plt.figure()
    plt.plot(fpr,tpr)
    plt.grid('on')
    plt.show()


import requests
import json
import time
# -------------------------
# Jinja2
# -------------------------
from jinja2 import Environment, FileSystemLoader
template_dir = 'Templates/'
env = Environment(loader=FileSystemLoader(template_dir))
xmen_template = env.get_template('XMen.j2')

# -------------------------
# Headers
# -------------------------
headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json',
}

# -------------------------
# All Characters
# -------------------------

person_template = env.get_template('character.j2')
people = requests.request("GET", "https://xmenapiheroku.herokuapp.com/api/characters", headers=headers)
peopleJSON = people.json()
peopleList = peopleJSON['results']
while peopleJSON['info']['next']:
    people = requests.request("GET", peopleJSON['info']['next'], headers=headers)
    peopleJSON = people.json()
    peopleList.extend(peopleJSON["results"])

peoplePersonList = []
for person in peopleList:
    singlePersonJSON = person
    if singlePersonJSON not in peoplePersonList:
        peoplePersonList.append(singlePersonJSON)

# -------------------------
# Person Template
# -------------------------

    parsed_all_output = person_template.render(
        singlePerson = singlePersonJSON,
        )

# -------------------------
# Save Characters File
# -------------------------

    with open(f"Cerebro/Characters/{ singlePersonJSON['name'] }.md", "w") as fh:
        fh.write(parsed_all_output)
        fh.close()

# -------------------------
# All Episodes
# -------------------------

episode_template = env.get_template('episode.j2')
episode = requests.request("GET", "https://xmenapiheroku.herokuapp.com/api/episodes", headers=headers)
episodeJSON = episode.json()
episodeList = episodeJSON['results']
while episodeJSON['info']['next']:
    episode = requests.request("GET", episodeJSON['info']['next'], headers=headers)
    episodeJSON = episode.json()
    episodeList.extend(episodeJSON["results"])

episodeEpisodeList = []
for episode in episodeList:
    episodeJSON = episode
    if episodeJSON not in episodeEpisodeList:
        episodeEpisodeList.append(episodeJSON)

# -------------------------
# Episode Template
# -------------------------

    parsed_all_output = episode_template.render(
        singleEpisode = episodeJSON,
        )

# -------------------------
# Save Characters File
# -------------------------

    with open(f"Cerebro/Episodes/{ episodeJSON['title'] }.md", "w") as fh:
        fh.write(parsed_all_output)
        fh.close()

# -------------------------
# X-Men TAS Template
# -------------------------

parsed_all_output = xmen_template.render(
    peoplePersonList = peoplePersonList,
    episodeEpisodeList = episodeEpisodeList
    )

# -------------------------
# Save X-Men TAS File
# -------------------------

with open("Cerebro/X-Men The Animated Series.md", "w") as fh:
    fh.write(parsed_all_output)
    fh.close()


import politics_lab as p
from hw2 import *
from vec import Vec

votes=p.create_voting_dict()
print(votes['Obama'])

print(set(votes.keys()))

most=p.most_similar('Obama',votes)
print(most)
print(p.policy_compare('Obama',most,votes))

least=p.least_similar('Obama',votes)
print(least)
print(p.policy_compare('Obama',least,votes))

print(p.most_like_chafee)
print(p.least_like_santorum)

print(p.democrats)

for democrat in p.democrats:
	print("Senator: %s = %s" % (democrat,p.find_average_similarity(democrat, p.democrats,votes)))

print(p.most_average_Democrat)

print(p.average_Democrat_record)
votes['Average']=p.average_Democrat_record

print(p.most_similar('Average',votes))

votes=p.create_voting_dict()
rivals=p.bitter_rivals(votes)
print(rivals)

for senator in rivals:
	print(votes[senator])

D = {'a','b','c'}
v1 = Vec(D, {'a': 1})
v2 = Vec(D, {'a': 0, 'b': 1})
v3 = Vec(D, {				 'b': 2})
v4 = Vec(D, {'a': 10, 'b': 10})

assert vec_select([v1, v2, v3, v4], 'a') == [Vec(D,{'b': 1}), Vec(D,{'b': 2})]

assert vec_sum([v1, v2, v3, v4], D) == Vec(D, {'b': 13, 'a': 11})
assert vec_sum([],D) == Vec(D,{})

assert vec_select_sum([v1, v2, v3, v4], 'a', D) == Vec(D, {'b': 3})

v1 = Vec({1,2,3}, {2: 9})
v2 = Vec({1,2,4}, {1: 1, 2: 2, 4: 8})
assert scale_vecs({3: v1, 5: v2}) == [Vec({1,2,3},{2: 3.0}), Vec({1,2,4},{1: 0.2, 2: 0.4, 4: 1.6})]

from GF2 import one
D = {'a', 'b', 'c'}
L = [Vec(D, {'a': one, 'c': one}), Vec(D, {'b': one})]
print(GF2_span(D, L))
assert len(GF2_span(D, L)) == 4
assert Vec(D, {}) in GF2_span(D, L)
assert Vec(D, {'b': one}) in GF2_span(D, L)
assert Vec(D, {'a':one, 'c':one}) in GF2_span(D, L)
assert Vec(D, {x:one for x in D}) in GF2_span(D, L)


import logging
from typing import List

import torch
from torch.nn.utils.rnn import pad_sequence

from asr.text_encoder.text_encoder import TextEncoder

logger = logging.getLogger(__name__)

def collate_fn(dataset_items: List[dict]):
    """
    Collate and pad fields in dataset items.
    """
    audio = [item["audio"] for item in dataset_items]

    spectrogram_length = torch.LongTensor([item["spectrogram"].size()[-1] for item in dataset_items])

    spectrogram = [item["spectrogram"].squeeze().T for item in dataset_items]
    spectrogram = pad_sequence(sequences=spectrogram, padding_value=0)
    spectrogram = spectrogram.permute(1, 0, 2)

    text_encoded = [item["text_encoded"].T for item in dataset_items]
    text_encoded = pad_sequence(sequences=text_encoded, padding_value=0)
    text_encoded = text_encoded.permute(1, 0, 2).squeeze()

    text_encoded_length = torch.LongTensor([item["text_encoded"].shape[1] for item in dataset_items])

    text = [TextEncoder.normalize_text(item["text"]) for item in dataset_items]

    batch = {
        "audio": audio,
        "spectrogram": spectrogram,
        "spectrogram_length": spectrogram_length,
        "text_encoded": text_encoded,
        "text_encoded_length": text_encoded_length,
        "text": text
    }

    return batch


from conans import ConanFile, CMake, tools

import os

class PocoConan(ConanFile):
    name = "poco"
    version = "1.10.1"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False], "fPIC": [True, False]}
    default_options = {"shared": True, "fPIC": True}
    #requires = "openssl/1.1.1"
    python_requires = "nla_pkg_helper/1.0"
    python_requires_extend = "nla_pkg_helper.ConanPackageHelper"
    pkg_helper = None

    def init(self):
        self.pkg_helper = self.python_requires["nla_pkg_helper"].module.ConanPackageHelper

        self.pkg_helper.clean_conan_cache_by_detected_os_host_and_arch(self, self.name, self.version)

    def requirements(self):
        if self.settings.os == "Windows":
            self.requires("openssl/1.1.1j")
        elif self.settings.os == "Macos":
            self.requires("openssl/1.1.1")

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def source(self):
        git = tools.Git()
        git.clone("https://github.com/pocoproject/poco.git", "poco-1.10.1-release")

    def build(self):
        cmake = CMake(self)

        config_args = [f"-DCMAKE_INSTALL_PREFIX={self.pkg_helper.get_bin_export_path(self)}"]

        if self.settings.os == "Macos":
            cmake.configure(args=config_args)

            cmake.build()
            cmake.install()

        elif self.settings.os == "Windows":
            cmake_build = "cmake-build"

            tools.mkdir(cmake_build)

            with tools.chdir(cmake_build):
                config_args.append('-G')
                config_args.append("Visual Studio 16 2019")
                cmake.configure(args=config_args, cache_build_folder=cmake_build)

                cmake.build()
                cmake.install()

    def package(self):
        self.pkg_helper.build_macosx_universal_bins(self)


# to allow running from terminal without pyCharm :(
import sys

sys.path.append('../ope')
sys.path.append('../models/q_learning')
sys.path.append('../models/attention/nn')
sys.path.append('../models/ckconv/nn')
sys.path.append('../models')

import torch
import argparse
import pandas as pd
from q_learning import DQN, fit_double_dqn
from baseline_encoders import *
from utils import load_data, count_parameters, OPECallback, add_intermediate_reward

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Trains policy with pretrained encoder')
    parser.add_argument('-d', "--dataset", type=str, default='amsterdam-umc-db')
    parser.add_argument('-m', "--baseline", type=str, default='last_state')
    parser.add_argument('-o', "--out_dims", type=int, default=96)
    parser.add_argument('-a', "--alpha", type=float, default=1e-4)
    parser.add_argument('-g', "--gamma", type=float, default=0.95)
    parser.add_argument('-l', "--tau", type=float, default=1e-4)
    parser.add_argument('-e', "--episodes", type=int, default=200000)
    parser.add_argument('-k', "--eval_after", type=int, default=5000)
    parser.add_argument('-z', "--freeze_encoder", type=bool, default=False)
    parser.add_argument('-w', "--replay_alpha", type=float, default=0.6)
    parser.add_argument('-q', "--replay_beta", type=float, default=0.9)
    parser.add_argument('-b', "--batch_size", type=int, default=32)
    args = vars(parser.parse_args())
    print('Running with args:', args)

    behavior_policy_file = '../ope/physician_policy/%s_aggregated_full_cohort_2h_mlp/valid_behavior_policy.csv' % args['dataset']
    train_df = load_data('../preprocessing/datasets/%s/aggregated_full_cohort_2h/train.csv' % args['dataset'])
    valid_df = load_data('../preprocessing/datasets/%s/aggregated_full_cohort_2h/valid.csv' % args['dataset'])

    # Add intermediate rewards based on MAP and lactate
    train_df.reward = add_intermediate_reward(train_df)
    valid_df.reward = add_intermediate_reward(valid_df)

    in_channels = train_df.filter(regex='x\d+').shape[1]

    if args['baseline'] == 'last_state':
        encoder = LastState(in_channels, args['out_dims'])
    elif args['baseline'] == 'concat-2':
        encoder = StateConcatenation(in_channels, args['out_dims'], k=2)
    elif args['baseline'] == 'concat-3':
        encoder = StateConcatenation(in_channels, args['out_dims'], k=3)
    elif args['baseline'] == 'tcn':
        encoder = TCN((in_channels, 64, args['out_dims']), kernel_sizes=(18, 3), dilations=(2, 1))
    elif args['baseline'] == 'lstm':
        encoder = LSTM(in_channels, args['out_dims'])
    elif args['baseline'] == 'gru':
        encoder = GRU(in_channels, args['out_dims'])
    else:
        raise Exception('Method %s not recognized' % args['baseline'])
    print('%s parameters: %d' % (args['baseline'], count_parameters(encoder)))

    dqn = DQN(state_dim=args['out_dims'], hidden_dims=(96, 96,), num_actions=25, disallowed_actions=(1, 2, 3, 4))
    print('DQN parameters:  ', count_parameters(dqn))

    # intermittently evaluate policy
    callback = OPECallback(behavior_policy_file=behavior_policy_file, valid_data=valid_df)

    # Fit!
    fit_double_dqn(
        experiment='results/%s/%s_experiment' % (args['dataset'], args['baseline']),
        policy=dqn,
        encoder=encoder,
        dataset=train_df,
        lrate=args['alpha'],
        gamma=args['gamma'],
        tau=args['tau'],
        lambda_reward=1,
        num_episodes=args['episodes'],
        batch_size=args['batch_size'],
        freeze_encoder=args['freeze_encoder'],  # set freeze_encoder=True to disable fine-tuning!
        eval_func=callback,
        eval_after=args['eval_after'],
        scheduler_gamma=0.95,
        replay_alpha=args['replay_alpha'],
        replay_beta=args['replay_beta'],
        step_scheduler_after=10000,
        min_max_reward=(-27, 10),
        save_on=None
    )


from flask import Flask

webapp = Flask(__name__)

#statistic
global T_max_miss
global T_min_miss
global expand
global shrink

control = "http://127.0.0.1:5000/controller"

from AutoScaler import main


from django.http import HttpResponse
from upload.forms import UploadFileForm
from django.shortcuts import render
from upload.models import *
import openpyxl

def upload_file(request):
    def get_last_used_row(sheet):
        row = sheet.max_row
        while row > 0:
            cells = sheet[row]
            if all([cell.value is None for cell in cells]):
                row -= 1
            else:
                break
        if row == 0:
            return 0, 0
        return row

    def get_first_row_by_value(sheet, value):
        for row in sheet.iter_rows(min_row=1, min_col=1, max_row=get_last_used_row(wb.worksheets[0]), max_col=1):
            for cell in row:
                if cell.value == value:
                    return sheet.cell(row=cell.row, column=cell.column).row

    def get_nonempty_rows_in_first_column(sheet):
        filled = 0
        for row in range(get_first_row_by_value(wb.worksheets[0], 1), get_last_used_row(wb.worksheets[0]) + 1):
            if sheet.cell(row=row, column=1).value is not None:
                filled += 1
        return filled

    if request.method == 'POST':
        upload_file_from = UploadFileForm(request.POST, request.FILES)
        if upload_file_from.is_valid():
            file = request.FILES['file']
            # >>>
            wb = openpyxl.load_workbook(file)
            sheet = wb.active
            testName = sheet.cell(row=1, column=1).value
            testDescriptionShort = sheet.cell(row=2, column=1).value
            testDescriptionLong = sheet.cell(row=3, column=1).value
            QuestionsArr = []
            Answers = [[0 for j in range(0)] for i in range(get_nonempty_rows_in_first_column(wb.worksheets[0]))]
            Corrects = [[0 for j in range(0)] for i in range(get_nonempty_rows_in_first_column(wb.worksheets[0]))]

            for row in range(get_first_row_by_value(wb.worksheets[0], 1), get_last_used_row(wb.worksheets[0]) + 1):
                if sheet.cell(row, 1).value is not None:
                    QuestionsArr.append(sheet.cell(row, 2).value)
                else:
                    Answers[len(QuestionsArr) - 1].append(sheet.cell(row, 2).value)
                    if sheet.cell(row, 3).value == 0:
                        Corrects[len(QuestionsArr) - 1].append(False)
                    if sheet.cell(row, 3).value == 1:
                        Corrects[len(QuestionsArr) - 1].append(True)

            db_testName = tests(name = testName, description=testName)
            db_testName.save()
            for Question in range(len(QuestionsArr)):
                answer = Question
                db_questions = questions(test_name = testName, name = QuestionsArr[Question], type = 'NONE', order= Question)
                db_questions.save()
                for answer in range(len(Answers[Question])):
                    db_answer = answers(questions_name = QuestionsArr[Question], answer = Answers[Question][answer], correct = Corrects[Question][answer], comments = '', order = answer)
                    db_answer.save()

            return HttpResponse('Тест успешно загружен', status=200)
            # >>>
    else:
        upload_file_from = UploadFileForm()

    context = {
        'form': upload_file_from
    }
    return render(request, 'upload/upload.html', context=context)


'''
Convolutional autoencoder that imposes a gaussion prior
on latent variables.
'''
import numpy as np
import tensorflow as tf
import mnist
import matplotlib.pyplot as plt
import sklearn.decomposition
from mpl_toolkits.mplot3d import Axes3D
tfd = tf.contrib.distributions

DISPLAY = True
USE_LAST = False

lat_dim = 8

print("Setting up Graph...")
data = tf.placeholder(tf.float32, [None,28,28])

c_1 = tf.layers.conv2d(inputs=tf.reshape(data,[-1,28,28,1]),filters=2,kernel_size=5)
#24x24x2

mp_1 = tf.layers.max_pooling2d(inputs=c_1,pool_size=2,strides=2)
#12x12x2

c_2 = tf.layers.conv2d(inputs=mp_1,filters=4,kernel_size=5)
#8x8x4

mp_2 = tf.layers.max_pooling2d(inputs=c_2,pool_size=2,strides=2)
#4x4x4

c_3 = tf.layers.conv2d(inputs=mp_2,filters=8,kernel_size=3)
#2x2x8

flat = tf.reshape(c_3,[-1,2*2*8])
#32

means = tf.layers.dense(flat,lat_dim)
devs = tf.layers.dense(flat,lat_dim,tf.nn.softplus)

posterior = tfd.MultivariateNormalDiag(means,devs)

encoding = posterior.sample()
#lat

u_flat = tf.layers.dense(encoding,2*2*8)
#32

t_1 = tf.layers.conv2d_transpose(inputs=tf.reshape(u_flat,[-1,2,2,8]),filters=4,kernel_size=3)
#2x2x8 -> 4x4x4

u_1 = tf.image.resize_images(t_1,size=(8,8),method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
#8x8x4

t_2 = tf.layers.conv2d_transpose(inputs=u_1,filters=2,kernel_size=5)
#12x12x2

u_2 = tf.image.resize_images(t_2,size=(24,24),method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
#24x24x2

t_3 = tf.layers.conv2d_transpose(inputs=u_2,filters=1,kernel_size=5)
#28x28x1

rec = tf.reshape(t_3,[-1,28,28])

reconstruction_loss = tf.reduce_mean(tf.square(tf.subtract(data,rec)))

prior = tfd.MultivariateNormalDiag(tf.zeros(lat_dim),tf.ones(lat_dim))
alpha = 0
KL_loss = alpha*tf.reduce_mean(tfd.kl_divergence(posterior,prior))

loss = reconstruction_loss+KL_loss

LEARNING_RATE = 5e-4
train_step = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)

print("Loading Training Images")
training_images = mnist.train_images()
training_images = training_images.astype(float)/255.0
training_labels = mnist.train_labels()

sess = tf.InteractiveSession()

saver = tf.train.Saver()

if(USE_LAST):
	saver.restore(sess,'/tmp/model.ckpt')
else:
	sess.run(tf.global_variables_initializer())

EPOCHS = 10
MB_SIZE = 10
batches = len(training_images)//MB_SIZE
numV = 5

for i in range(EPOCHS):
	print("")
	print("Calculating Metrics")
	if(DISPLAY):
		encodings = sess.run(encoding,feed_dict={data:training_images})
		pca = sklearn.decomposition.PCA(n_components=2)
		pca.fit(encodings)
		p_comps = pca.transform(encodings)
		plt.scatter(p_comps[:,0],p_comps[:,1],c=training_labels,cmap='tab10')
		plt.show()
		plt.close('all')
	rl,kl,l = sess.run((reconstruction_loss,KL_loss,loss),feed_dict={data:training_images})
	print("Loss:",l)
	print("reconstruction_loss:",rl)
	print("KL_loss:",kl)
	print("Training Epoch", i+1)
	for b in range(batches):
		print(f"Batch: {b+1}",end='\r')
		if(DISPLAY and b%3000==0):
			indices = np.random.choice(len(training_images),numV)
			images = training_images[indices]
			recs = sess.run(rec,feed_dict={data:images})
			for t in range(numV):
				plt.subplot(numV,2,t*2+1)
				plt.imshow(images[t],cmap='gray')
				plt.subplot(numV,2,t*2+2)
				plt.imshow(recs[t],cmap='gray')
			plt.show()
			plt.close('all')
		batch = training_images[b*MB_SIZE:(b+1)*MB_SIZE]
		sess.run(train_step,feed_dict={data:batch})

saver.save(sess,'/tmp/model.ckpt')


class Solution:
    def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
        def getNegDist(point):
            x,y = point
            return -(((x**2)+(y**2))**0.5)
        q = []
        for point in points:
            heappush(q,(getNegDist(point),point))
            if len(q)>k:
                heappop(q)
        return [p[1] for p in q]


import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
from scipy.signal import find_peaks_cwt
from uncertainties import ufloat
import scipy.constants as c
import math
from lmfit import minimize, Parameter, Model

DA, ErrDA, IA, MA = np.loadtxt('data/Aluminium.txt', unpack=True)
DB, IB, MB = np.loadtxt('data/Blei.txt', unpack=True)
DK, IK, MK = np.loadtxt('data/Kupfer.txt', unpack=True)

NullGamma = int(804/900); NullBeta = int(248/900)
DA *= (10**(-6)*2760); DB *= 0.001; DK *= 0.001;

#Berechnet bereinigte Zählrate
def berechneZaehlrate(Impulszahl, Zeit, Null, laenge):
	Z = [0]*laenge
	for x in range(laenge):
	  Z[x] = Impulszahl[x]/Zeit[x] - Null
	return np.array(Z)

# Berechnet den Fehler der Zählrate
def calcerr(Z, A):
        Zerr= [0]*A
        for x in range(A):
            Zerr[x] = np.sqrt(Z[x])
        return Zerr

#lineraer Fit
def f(x, a, b):
    return np.exp(-a*x + b)

#Fitte durch die Punkte x,y
def fitfunc(x,y,xdeff):
    fx, ferr = curve_fit(f, x, y)
    #fx = np.exp(fx)
    ferr = np.sqrt(np.diag(ferr))
    plt.plot(xdeff, f(xdeff, *fx))
    m = ufloat(fx[0],ferr[0])
    b = ufloat(fx[1],ferr[0])
    print('Steigung', m, 'y-Achseschnitt', b)
    return m, b

print('--------------Aluminium-------------')
#Berechnet Zählrate mit Fehler
ZA = berechneZaehlrate(IA,MA,NullBeta, 11)
ZAerr = calcerr(ZA,11)
print(ZA, ZAerr)
plt.semilogy(DA,ZA,'bx', label='Messwerte')
plt.errorbar(DA, ZA, yerr=ZAerr, fmt='bx')
xdeff = np.linspace(0,2,50)
m1, b1 = fitfunc(DA[:5],ZA[:5],xdeff)
m2, b2 = fitfunc(DA[5:],ZA[5:],xdeff)
s = (b1-b2)/(m1-m2)
Emax = 1.92*(((s*0.10)**2+0.22*(s*0.10))**0.5)
print('Schnittpunkt = ', s, "und Maximale Energie von: ", Emax)
plt.xlim(0.2,1.4)
plt.xlabel(r'Massenbelegung / $\frac{kg}{m^2}$')
plt.ylim(10**(-1),10**2)
plt.ylabel(r'Zählrate / $cps$')
plt.legend(loc='best')
plt.grid()
plt.tight_layout()
plt.savefig('build/Aluminium.pdf')
plt.close()

print('--------------Blei------------------')
ZB = berechneZaehlrate(IB,MB,NullGamma, 15)
ZBerr = calcerr(ZB, 15)
#print(ZB, ZBerr)
plt.semilogy(DB,ZB,'rx')
plt.errorbar(DB,ZB, yerr=ZBerr, fmt='rx')
xdeff = np.linspace(0, 0.065, 50)
fitfunc(DB,ZB,xdeff)
plt.xlim(0,0.065)
plt.xlabel(r'Dicke / m')
plt.ylabel(r'Zaehlrate / $cps$')
plt.tight_layout()
plt.savefig('build/Blei.pdf')
plt.close()

print('--------------Kupfer----------------')
ZK = berechneZaehlrate(IK,MK,NullGamma, 22)
ZKerr = calcerr(ZK,22)
#print(ZK, ZKerr)
plt.semilogy(DK,ZK,'rx')
plt.errorbar(DK, ZK, yerr=ZKerr, fmt='rx')
xdeff= np.linspace(0, 0.065, 50)
fitfunc(DK,ZK, xdeff)
plt.xlim(0,0.065)
plt.xlabel(r'Dicke / m')
plt.ylabel(r'Zaehlrate / $cps$')
plt.tight_layout()
plt.savefig('build/Kupfer.pdf')
plt.close()

V704 - Absorbtion von gamma- und beta-Strahlung/plot.py


import datetime
from torch.optim import AdamW
from torch.optim.lr_scheduler import ReduceLROnPlateau

from src.__init__ import *
from src.utils.bbox import convert_bbox, untransform_bbox
from src.dataset.metric import Evaluate_COCO
from src.model.efficientdet import EfficientDet
from src.loss.focal_loss import Focal_Loss
from src.utils.nms.hard_nms import Hard_NMS

class COCO_EfficientDet(pl.LightningModule):

    __doc__ = r"""
        This class is to manage the hyper-parameters at once, involved in training.

        Args:
            coeff: coefficient for EfficientDet
            pretrained_backbone: load checkpoints to the model's backbone
            ckpt_path: checkpoint path of only EfficientDet, not COCO_EfficientDet
                if you load the checkpoints of COCO_EfficientDet, use 'load_from_checkpoint'
            fore_th: foreground threshold for the loss function
            back_th: background threshold for the loss function
            alpha: alpha for focal-loss
            gamma: gamma for focal-loss
            beta: beta for smooth-L1 loss
            fore_mean: average the loss values by the number of foregrounds
            reg_weight: weight for smooth-L1 loss
            average: average the loss values by the number of mini-batch
            iou_th: IoU threshold for Soft-NMS
            conf_th: confidence or score threshold for Soft-NMS
            gaussian: gaussian penalty for Soft-NMS
            sigma: sigma for Soft-NMS
            max_det: max detection number after Soft-NMS
            lr: learning rate
            lr_exp_base: gamma for the exponential scheduler
            warmup_epochs: warm-up start epochs for the exponential scheduler
            val_annFile: file path of annotation for validation(instances_train2017.json)

        * You can alter NMS or optimizer by modifications of lines.
    """

    def __init__(self,
                 coeff: int,
                 pretrained_backbone: bool = True,
                 ckpt_path: str = None,
                 fore_th: float = 0.5,
                 back_th: float = 0.4,
                 alpha: float = 0.25,
                 gamma: float = 1.5,
                 beta: float = 0.1,
                 fore_mean: bool = True,
                 reg_weight: float = None,
                 average: bool = True,
                 iou_th: float = 0.5,
                 max_det: Optional[int] = 400,
                 lr: float = 1e-4,
                 val_annFile: str = None,
                 ):

        super().__init__()
        self.save_hyperparameters()

        self.coeff = coeff
        self.pretrained_backbone = pretrained_backbone
        self.ckpt_path = ckpt_path
        self.fore_th = fore_th
        self.back_th = back_th
        self.alpha = alpha
        self.gamma = gamma
        self.beta = beta
        self.fore_mean = fore_mean
        self.reg_weight = reg_weight
        self.average = average
        self.iou_th = iou_th
        self.max_det = max_det
        self.lr = lr
        self.annFile = val_annFile

        self.model = self.configure_model()
        self.anchors = self.model.anchors
        self.loss = self.configure_loss_function()
        self.nms = self.configure_nms()

        self.val_result_dir = None
        self.test_result_dir = None

    def configure_model(self):
        model = EfficientDet(self.coeff, 80, False, self.pretrained_backbone)

        if not self.pretrained_backbone:
            self.initialize_weight(model)
        else:
            self.initialize_weight(model.fpn)
            self.initialize_weight(model.head)

        if self.ckpt_path:
            ckpt = torch.load(self.ckpt_path)
            assert isinstance(ckpt, OrderedDict), 'please load EfficientDet checkpoints'
            assert next(iter(ckpt)).split('.')[0] != 'model', 'please load EfficientDet checkpoints'
            model.load_state_dict(torch.load(self.ckpt_path))

        return model

    def configure_loss_function(self):
        return Focal_Loss(self.fore_th, self.back_th, self.alpha, self.gamma, self.beta,
                          self.fore_mean, self.reg_weight, self.average, 'cxcywh')

    def configure_nms(self):
        return Hard_NMS(self.iou_th, self.max_det, 'cxcywh')

    def configure_optimizers(self):
        optimizer = AdamW(self.model.parameters(), self.lr)
        scheduler = ReduceLROnPlateau(optimizer, mode='max', factor=0.5, patience=3,
                                      threshold=0.001, threshold_mode='abs', verbose=True)

        return {"optimizer": optimizer,
                "lr_scheduler": {"scheduler": scheduler, "monitor": 'AP'}}

    @classmethod
    def initialize_weight(cls, model):
        for module in model.modules():
            if isinstance(module, nn.Conv2d):
                nn.init.kaiming_normal_(module.weight, mode='fan_out', nonlinearity='relu')

                if module.bias is not None:
                    nn.init.constant_(module.bias, 0)

    def forward(self, input, detect):
        return self.model(input, detect)

    def training_step(self, batch, batch_idx):
        inputs, labels = batch
        preds, anchors = self.model(inputs, detect=False)
        sync_labels = convert_bbox(labels, 'xywh', 'cxcywh')

        loss, cls_loss, reg_loss = self.loss(preds, anchors, sync_labels)
        self.log('train_loss', loss)
        self.log('train_cls_loss', cls_loss)
        self.log('train_reg_loss', reg_loss)

        return loss

    def validation_step(self, batch, batch_idx):
        ids, inputs, scales, pads = batch
        preds, _ = self.model(inputs, detect=True)
        preds = self.nms(preds)

        for i, (scale, pad) in enumerate(zip(scales, pads)):
            preds[i] = convert_bbox(preds[i], 'cxcywh', 'xywh')
            preds[i] = untransform_bbox(preds[i], scale, pad, 'xywh')

        return ids, preds

    def test_step(self, batch, batch_idx):
        ids, inputs, scales, pads = batch
        preds, _ = self.model(inputs, detect=True)
        preds = self.nms(preds)

        for i, (scale, pad) in enumerate(zip(scales, pads)):
            preds[i] = convert_bbox(preds[i], 'cxcywh', 'xywh')
            preds[i] = untransform_bbox(preds[i], scale, pad, 'xywh')

        return ids, preds

    def validation_epoch_end(self, val_step):
        result = OrderedDict()

        for batch in val_step:
            for id, pred in zip(*batch):
                result[id] = pred

        if not self.val_result_dir:
            self.val_result_dir = os.path.join('result/val', datetime.datetime.now().strftime("run-%Y-%m-%d-%H-%M"))
        result_file = os.path.join(self.val_result_dir, 'epoch-{}.json'.format(self.current_epoch))

        val_metric = Evaluate_COCO(result, result_file, self.annFile, test=False)

        self.log('AP', val_metric['AP'])
        self.log('AP50', val_metric['AP50'])
        self.log('AR', val_metric['AR'])

    def test_epoch_end(self, test_step):
        result = OrderedDict()

        for batch in test_step:
            for img_id, pred in zip(*batch):
                result[img_id] = pred

        if not self.test_result_dir:
            self.test_result_dir = 'result/test'
        result_file = os.path.join(self.test_result_dir, datetime.datetime.now().strftime("run-%Y-%m-%d-%H-%M.json"))

        Evaluate_COCO(result, result_file, None, test=True)


from django.shortcuts import render, redirect

# Create your views here.
from .models import Service
from django.template import loader
from .forms import ServiceForm
from datetime import datetime
from django.contrib.auth.models import User

from rest_framework import generics
from .serializers import ServiceSerializer, ServiceCreateSerializer
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from django.views.decorators.csrf import csrf_exempt

def index(request):
    services = Service.objects.filter(receiver_id=request.user.id)
    context = {'services': services}
    return render(request, 'services/index.html', context)

def new(request):
    print(request.method)
    if request.method == 'GET':
        form = ServiceForm()
    else:
        form = ServiceForm(request.POST)
        user = request.user
        print("Valid form", form.is_valid())

        if form.is_valid():
            print('valid')
            # title = form.cleaned_data['title']
            # image = form.cleaned_data['image']
            instance = form.save(commit=False)
            instance.creator_id = user.id
            instance.updater_id = user.id
            instance.save()
            # form.save()
            return redirect('/services')
    users = User.objects.all()
    context = {'users': users}
    return render(request, "services/new.html", context)

def show(request):
    services = Service.objects.all()
    return render(request, "services/show.html", {'services': services})

def edit(request, id):
    services = Service.objects.get(id=id)
    return render(request, 'services/edit.html', {'services': services})

def update(request, id):
    services = Service.objects.get(id=id)
    form = ServicesForm(request.POST, instance=services)
    if form.is_valid():
        form.save()
        return redirect("/services")
    return render(request, 'services/edit.html', {'services': services})

def destroy(request, id):
    services = Service.objects.get(id=id)
    services.delete()
    return redirect("/services")

@csrf_exempt
@api_view(["GET"])
def services(request):
    services = Service.objects.filter(creator_id=request.user.id)
    # return Response(latest_post_list, status=HTTP_200_OK)
    # the many param informs the serializer that it will be serializing more than a single article.
    serializer = ServiceSerializer(services, many=True)
    return Response({"services": serializer.data})

@api_view(["POST"])
def create_service(request):
    serializer = ServiceCreateSerializer(data=request.data)
    print("Data", request.data)
    print("User", request.user.id)
    if serializer.is_valid():
        # user = Document.objects.create(serializer.validated_data)
        serializer.save(creator=request.user, updater=request.user)
        return Response({"message": "Document created"})
    else:
        data = {
            "error": True,
            "errors": serializer.errors,
        }
        return Response(data)

class ServiceList(generics.ListCreateAPIView):
    queryset = Service.objects.all()
    serializer_class = ServiceSerializer

class ServiceDetail(generics.RetrieveAPIView):
    queryset = Service.objects.all()
    serializer_class = ServiceSerializer

class ServiceDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Service.objects.all()
    serializer_class = ServiceSerializer

def socialVisa(request):
    return render(request, 'socialVisa.html')


import http
from datetime import datetime

from .utils import *

def init_upload_routes(app, socketio):
    @app.route('/upload', methods=['POST'])
    def upload():
        file = request.files['file']
        path = request.form['path']
        # overwrite = request.form['overwrite']
        # print(overwrite)

        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)

            # if os.path.exists(os.path.join(path, filename)) and overwrite == 'false':
            #     warnings_msg = f"File already exists: {filename}"
            #     app.logger.warning(warnings_msg)
            #     return jsonify(status=http.HTTPStatus.CONFLICT, message=warnings_msg)

            file.save(os.path.join(path, filename))
            app.logger.info(f"File uploaded: {filename}")
            return jsonify(status=http.HTTPStatus.OK)
        elif not allowed_file(file.filename):
            warning_msg = f"File type not allowed: {file.filename}"
            app.logger.warning(warning_msg)
            return jsonify(status=http.HTTPStatus.BAD_REQUEST, message=warning_msg)

    @app.route('/upload-fill-data', methods=['POST'])
    def upload_fill_data():
        try:
            data = request.get_json()['fillData']
            df = pd.DataFrame(data)
            file_path = os.path.join(Config.UPLOAD_TMP_PREDICT_FOLDER, f'fill_data_{datetime.timestamp(datetime.today())}.csv')
            df.to_csv(file_path, index=False)
            # app.logger.info(file_path)
            return jsonify(status=http.HTTPStatus.OK, filePath=file_path)

        except Exception as e:
            error_msg = f"Exception occurred: {str(e)}\n{traceback.format_exc()}"
            app.logger.error(error_msg)
            return jsonify(status=http.HTTPStatus.INTERNAL_SERVER_ERROR, message=error_msg)


import numpy as np
import math
from matplotlib import pyplot as plt
# B is self_localization
def destinationPoint(objeCordinates, destinationPoints, b,finishCounter,pastObje):
    dist=np.array([])
    index=np.array([])
    p=0
    k=0

    for j in range(objeCordinates.shape[0]):
        deltX= objeCordinates[j][0]-b[0][0]
        deltY=objeCordinates[j][1]-b[1][0]
        fiOb= np.arctan2(deltY,deltX)
        rOb= np.sqrt(deltX*deltX+deltY*deltY)
        rOb=((rOb*rOb*rOb*1.24)/1000000) - (0.002286*rOb*rOb) + (1.904*rOb) -111
        objeCordinates[j][0]= b[0][0]+ rOb*np.cos(fiOb)
        objeCordinates[j][1]= b[1][0]+rOb*np.sin(fiOb)

    for i in range(pastObje.shape[1]):
          for j in range(objeCordinates.shape[0]):
              distance=np.sqrt((pastObje[0][i]-objeCordinates[j][0])**2 + (pastObje[1][i]-objeCordinates[j][1])**2)
              if distance<400:
                  index=np.append(index,j)

    try:
        index=np.asarray(index)
        index= index.astype(int)
        objeCordinates=np.delete(objeCordinates,index,axis=0)
    except ValueError:
        print('No past Object')

    for i in range(objeCordinates.shape[0]):
        dist = np.append(dist,np.sqrt((objeCordinates[i][0] - b[0][0])**2+(objeCordinates[i][1] - b[1][0])**2))

    try:
        close=np.where(dist<200)
        close=np.asarray(close)
        close= close.astype(int)
        closeObje=np.argmin(objeCordinates[close])
        plt.scatter(objeCordinates[close][0],objeCordinates[close][0],s=40, c='b')
        print("Obje Detection")
        k=1
    except ValueError:
        print('No close object')

    try:
        index= np.where(dist>1200)
        index=np.asarray(index)
        index= index.astype(int)
        objeCordinates=np.delete(objeCordinates,index,axis=0)
    except ValueError:
        print ('No far object')

    dist=np.array([])
    for i in range(objeCordinates.shape[0]):
        dist = np.append(dist,np.sqrt((objeCordinates[i][0] - b[0][0])**2+(objeCordinates[i][1] - b[1][0])**2))

    try:
        minimum=np.argmin(dist)
        print(objeCordinates)
        D= objeCordinates[minimum]
        D.shape=(1,2)
        print("Yes")

#        plt.scatter(D[0][0],D[0][1],s=60,c='r')
#        plt.text(D[0][0],D[0][1],"Obje"+str(forCounter+1))
        p=1
        if D[0][0] ==0 and D[0][1]==0:
            D= destinationPoints
            D.shape=(1,2)
            print("bug2")
            finishCounter+=1

    except ValueError:
        D= destinationPoints
        D.shape=(1,2)
        print("bug")
        finishCounter+=1
#    pastObje1=D.T
#    pastObje=np.concatenate((pastObje,pastObje1),axis=1)

    return D,p,k,finishCounter


#PIC16
#Assignment10M
#Keith Atienza
#Li, Chak Yin (Anders) 704950093

import sys
import socket
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QTextEdit, QVBoxLayout, QSizePolicy, QLineEdit, QMessageBox
# from PyQt5.QtGui import QPainter, QColor

class ChatWindow(QWidget):

    def __init__(self):
        super(ChatWindow, self).__init__()
        self.initUi()
        self.setServer()

    def initUi(self):
        self.setWindowTitle('Chat App')
        self.resize(350,130)

        self.textLabel = QLabel('started') # to display the message
        self.textBox = QLineEdit(self) #so enter can be pressed

        ###### for appearance only when window is resized #####
        self.layout = QVBoxLayout()
        self.textBox.setMaximumHeight(30)
        self.textBox.setSizePolicy(QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum))
        self.layout.addWidget(self.textLabel)
        self.layout.addWidget(self.textBox)
        self.setLayout(self.layout)
        ##########################################################

        self.timer = QTimer()
        self.timer.setInterval(2000)
        self.timer.timeout.connect(self.receive)
        self.timer.start()

        self.textBox.returnPressed.connect(self.send)

    def setServer(self):
        self.mySocket = socket.socket()
        self.host = '127.0.0.1'
        self.port = 5001

        try:
            self.mySocket.connect((self.host, self.port))
            self.textLabel.setText('client <connected>')
            self.timer.start()

        except:
            self.mySocket = socket.socket()
            self.mySocket.bind((self.host,self.port))
            self.mySocket.listen(1)
            self.mySocket, addr = self.mySocket.accept()
            self.textLabel.setText('server <connected>')
            self.timer.start()

    def receive(self):
        self.mySocket.settimeout(0.25)
        try:
            data = self.mySocket.recv(1024)
            if data == '<connection closed>':
                self.timer.stop()
                self.textBox.returnPressed.disconnect(self.send)
            self.textLabel.setText(data)
        except:
            pass

    def send(self):
        try:
            self.mySocket.send(self.textBox.text())
        except:
            pass

    def closeEvent(self, event):
        self.mySocket.send('<connection closed>')
        self.mySocket.close()

def main():

    app = QApplication(sys.argv)
    ex = ChatWindow()
    ex.show()
    app.exec_()

if __name__ == "__main__":
    main()


from queue import Queue
# Created by Marcin "Cozoob" Kozub at 08.05.2021 16:32
# Implementacja BFS dla grafu w postaci list sąsiedztwa

# repr. listowa O(V + E), repr. macierzowa O(V^2)
def bfs(G, s):
    n = len(G)
    queue = Queue()
    visited = [False for _ in range(n)]

    queue.put(s)
    visited[s] = True

    while not queue.empty():
        u = queue.get()
        for v in G[u]:
            if not visited[v]:
                visited[v] = True
                queue.put(v)

ASD - na egzamin/Algorytmy do 2 kolosa/BFS.py


# -*- coding: utf-8 -*- #
# ------------------------------------------------------------------
# File Name:        Sniffer.py
# Author:           harry-hhj
# Version:
# Created:
# Description:      Main Function:    按日增量清洗用户上课日志
#                   Outer Parameters: date_key  eg： 20201215
# Function List:    exit() -- exit the interpreter by raising SystemExit
#                   getdlopenflags() -- returns flags to be used for dlopen() calls
#                   getprofile() -- get the global profiling function
# History:
#       <author>        <version>       <time>      <desc>
#       sacevan         ver0_1          2020/12/15  xxx
# ------------------------------------------------------------------

from scapy.all import *
import time

protrol_filter = ''

def arp_monitor_callback(pkt):
    if 'ARP' in pkt and pkt['ARP'].op in (1, 2):  # who-has or is-at
        return pkt.sprintf("%ARP.hwsrc% %ARP.psrc%")

# 时间戳转换函数
def TimeStamp2Time(timeStamp):
    timeTmp = time.localtime(timeStamp)  # time.localtime()格式化时间戳为本地时间
    myTime = time.strftime("%Y-%m-%d %H:%M:%S", timeTmp)  # 将本地时间格式化为字符串
    return myTime

class Sniffer(object):
    def __init__(self, iface="en0", session="", filter="ip", count=0, timeout=None, prn=None):
        self.dpkts = None
        self.dpkt_list = []
        self.auto_show = True
        self.sniffer = None
        if iface == '':
            self.iface = iface
        else:
            self.iface=iface
        self.filter = filter
        if session == 'IPSession':
            self.session = IPSession
        elif session == 'TCPSession':
            self.session = TCPSession
        else:
            self.session = None
        self.count = count
        self.timeout = timeout
        print(timeout)
        self._last_time = time.time()
        self.prn = prn
        # self.packet = None

    def packet_callback(self, packet):
        # print("Flow rate: ", packet['IP'].len / 1024 / 1024 / (time.time() - self._last_time))
        # if time.time() - self._last_time > 0:
        #     self._last_time = time.time()
        #     summary = packet.summary()
        #     self.dpkt_list.append(packet)
        #     self.dpkts = PacketList(self.dpkt_list)
        #     if self.prn is not None:
        #         self.prn.emit(summary)
        summary = packet.summary()
        self.dpkt_list.append(packet)
        if self.prn is not None:
            # self.packet = packet
            self.prn.emit(summary)
        # print(summary)
        # packet.draw()
        # print(type(hexdump(packet)))
        # print(hexstr(packet).split('  '))
        # self._last_time = time.time()

        # if packet['Ether'].payload:
        #     print(packet['Ether'].src)
        #     print(packet['Ether'].dst)
        #     print(packet['Ether'].type)
        #     print(hexstr(packet['Ether'].payload))

        # if packet['ARP'].payload:
        #     print(packet['ARP'].psrc)
        #     print(packet['ARP'].pdst)
        #     print(packet['ARP'].hwsrc)
        #     print(packet['ARP'].hwdst)

    def run(self, auto_show=False):
        self.auto_show = auto_show
        self._last_time = time.time()
        self.dpkts = sniff(iface=self.iface, session=self.session, filter=self.filter, count=self.count,
                           timeout=self.timeout, prn=self.packet_callback)

    def set(self, param, value):
        pass

    def save(self, name=None):
        if name is None:
            wrpcap("./pcap/{}.pcap".format(TimeStamp2Time(time.time())), self.dpkts)
        else:
            wrpcap("./pcap/{}.pcap".format(str(name)), self.dpkts)

    def load(self, file="./pcap/2020-12-16 13:54:18.pcap", filter=''):
        self.dpkts = sniff(offline=file, filter=filter)

    def search(self, string):
        pass

    def modify(self, idx):
        self.dpkts[idx].replace( IP.src, "192.168.1.1", "10.0.0.1" )

    def draw(self):
        self.dpkts.conversations(type='jpg', target="> tmp/test.jpg")

if __name__ == '__main__':
    # dpkts = sniff(iface="en0", session=IPSession, filter='', count=100)
    # dpkts.conversations(type='jpg', target="> test.jpg")
    # ls('ARP')
    # for dpkt in dpkts:
    #     print(dpkt.show(dump=True))
    #
    # print('-' * 10)
    # flags = []
    # for dpkt in dpkts:
    #     if 'IP' in dpkt:
    #         print(dpkt.summary())
    #         print(int(dpkt['IP'].flags))
    #         print(dpkt['IP'].flags)
    #         if dpkt['IP'].flags not in flags:
    #             flags.append(dpkt['IP'].flags)
    # print(flags)

    s = Sniffer(iface="en0", filter='ip', timeout=2)
    s.run()
    # print(str(s.dpkts.nsummary()))
    for pkt in s.dpkts:
        if pkt.haslayer(IP):
            print(pkt['IP'].flags, int(pkt['IP'].flags))

    # s = Sniffer()
    # s.load()
    # print('--'*10)
    # print('--'*10)
    # print('--'*10)
    # s.dpkts = s.dpkts.replace(IP.ttl, 64)
    # s.dpkts.show()
    # print('--'*20)
    # for dpk in s.dpkts:
    #     if dpk['IP'].ttl != 64:
    #         print(dpk['IP'].ttl)
    # print(s.dpkts.sr()[0].show())
    # print(s.dpkts.sr()[1].show())

    # dpkts = sniff(timeout=5)
    # print(dpkts)
    # try:
    #     import os
    #     os.remove('tmp/reserved.pcap')
    # except:
    #     pass
    # wrpcap('tmp/reserved.pcap', dpkts)
    # dpkts = sniff(offline='tmp/reserved.pcap', session=TCPSession, filter='')
    # print(dpkts)

    # print(sniff(iface='lo0', filter='icmp', timeout=5))