import time

import numpy as np

from . import wrapper

__all__ = ['Sparse2Level', 'get_chisq', 'get_chisq_dense', 'get_gradient',
           'get_gradient_dense', 'chol', 'many_chol', 'tri_inv', 'many_tri_inv',
           'read_sparse', 'make_uv_grid', 'grid_data', 'mymatmul',
           'mult_vecs_by_blocks', 'make_uv_from_antpos']

# Check if `pyfof` (a friends of friends implementation in python) is available
# to be used for finding redundant groups.
# If `pyfof` is not installed the redundant grouping function `grid_data` cuts
# the uv-plane into a grid and selects baselines that fall into a grid cell
# as a redundant group.
#
# See grid_data in wrapper.py
try:
    import pyfof
    have_fof = True
except ImportError:
    have_fof = False

class Sparse2Level:
    """
    Sparse2Level contains/creates the data covariance matrix and all
    associated matrix operations

    Attributes
    ----------
    noise_variance  :   array_like
                        Contains the (1xN) thermal noise variance vector
    covariance_vectors  :   array_like
                            is an (MxN) array containing M eigenvectors of
                            the data covariance matrix
    source_model_vectors    :   array_like
                                is an MxN array containing M model visibility
                                vectors for M sources
    redudant_group_edges    :   array_like
                                is a vector that contains the start indices
                                of each new redundant group as organised by
                                the grid_data function
    is_inverse  :   bool
                    ????? Probably something to use if you've have inverted
                    the covariance matrices and did the
                    decomposition on that

    """

    def __init__(self, noise_variance, covariance_vectors, source_model_vectors,
                 redundant_group_edges, is_inverse=0):
        self.noise_variance = noise_variance.copy()
        self.covariance_vectors = np.matrix(covariance_vectors.copy())
        self.source_model_vectors = np.matrix(source_model_vectors.copy())
        # self.lims=lims.copy()
        # We copy data like this because something something pointers that
        # causes the C code to segmentation fault
        self.baseline_group_edges = np.zeros(len(redundant_group_edges),
                                             dtype='int64')
        self.baseline_group_edges[:] = redundant_group_edges
        self.is_inverse = is_inverse
        self.number_of_baseline_groups = len(redundant_group_edges) - 1

    def copy(self):
        """
        Method to copy Sparse2Level object

        Returns:
            copy of Sparse2Level object
        """
        return Sparse2Level(self.noise_variance, self.covariance_vectors,
                            self.source_model_vectors,
                            self.baseline_group_edges, self.is_inverse)

    # We define multiplication for this sparse2_level object
    def __mul__(self, input_vector):
        """
        Implements sparse matrix multiplication with any data length vector

        """

        result = np.zeros(input_vector.shape)
        # void sparse_mat_times_vec_wrapper(double *diag, double *vecs,
        # double *src, int n, int nvec, int nsrc,
        # int nblock, long *lims, double *vec, double *ans)
        number_of_measurements = self.noise_variance.size
        number_of_covariance_vectors = self.covariance_vectors.shape[0]
        number_of_source_vectors = self.source_model_vectors.shape[0]
        wrapper.sparse_matrix_vector_multiplication(
            self.noise_variance.ctypes.data,
            self.covariance_vectors.ctypes.data,
            self.source_model_vectors.ctypes.data,
            number_of_measurements,
            number_of_covariance_vectors,
            number_of_source_vectors,
            self.number_of_baseline_groups,
            self.baseline_group_edges.ctypes.data,
            self.is_inverse,
            input_vector.ctypes.data,
            result.ctypes.data
        )
        return result

    def expand(self):
        """
        Method ??????

        """
        m1 = self.source_model_vectors.transpose() * self.source_model_vectors
        for i in range(0, self.number_of_baseline_groups):
            i1 = self.baseline_group_edges[i]
            i2 = self.baseline_group_edges[i + 1]
            tmp = self.covariance_vectors[:, i1:i2].transpose() * \
                self.covariance_vectors[:, i1:i2]
            m1[i1:i2, i1:i2] += tmp
        mm = np.diag(self.noise_variance)
        if self.is_inverse:
            return mm - m1
        else:
            return mm + m1

    def inv(self):
        """
        Method that efficiently computes the inverse of a sparse matrix using
        provided c-code

        """
        # t1 = time.time()
        myinv = self.copy()
        myinv.is_inverse = ~self.is_inverse
        myinv.noise_variance = 1.0 / self.noise_variance
        number_covariance_vectors = self.covariance_vectors.shape[0]
        number_of_measurements = self.noise_variance.size
        tmp = np.zeros(
            [self.number_of_baseline_groups, number_covariance_vectors,
             number_covariance_vectors])

        wrapper.make_all_small_blocks_c(
            self.noise_variance.ctypes.data,
            self.covariance_vectors.ctypes.data,
            self.baseline_group_edges.ctypes.data,
            self.number_of_baseline_groups,
            number_of_measurements,
            number_covariance_vectors, tmp.ctypes.data
        )

        myeye = np.repeat([np.eye(number_covariance_vectors)],
                          self.number_of_baseline_groups, axis=0)
        if self.is_inverse:
            tmp2 = myeye - tmp
        else:
            tmp2 = myeye + tmp
        wrapper.cholesky_factorization_parallel(
            tmp2.ctypes.data,
            number_covariance_vectors,
            self.number_of_baseline_groups
        )
        tmp3 = many_tri_inv(tmp2)
        tmp4 = mult_vecs_by_blocks(self.covariance_vectors, tmp3,
                                   self.baseline_group_edges)

        for i in range(tmp4.shape[0]):
            tmp4[i, :] = tmp4[i, :] * myinv.noise_variance
        # return tmp4
        # wrapper.invert_all_small_blocks_c(tmp.ctypes.data,self.nblock,nvec,np.int(
        # self.isinv),tmp2.ctypes.data)
        # t2 = time.time()
        # print 'took ' + repr(t2-t1) + ' seconds to do inverse.'

        # return tmp,tmp3,tmp4
        myinv.covariance_vectors[:] = tmp4

        nsrc = self.source_model_vectors.shape[0]
        tmp = 0 * self.source_model_vectors
        n = self.noise_variance.size
        number_covariance_vectors = self.covariance_vectors.shape[0]
        nblock = self.baseline_group_edges.size - 1
        dptr = myinv.noise_variance.ctypes.data
        sptr = myinv.source_model_vectors.ctypes.data
        vptr = myinv.covariance_vectors.ctypes.data
        # we can do the block multiply simply by sending in 0 for nsrc
        for i in range(nsrc):
            wrapper.sparse_matrix_vector_multiplication(
                dptr, vptr, sptr, n,
                number_covariance_vectors, 0,
                nblock,
                self.baseline_group_edges.ctypes.data,
                myinv.is_inverse,
                self.source_model_vectors[
                    i].ctypes.data,
                tmp[i].ctypes.data
            )

        small_mat = tmp * self.source_model_vectors.transpose()

        if self.is_inverse:
            small_mat = np.eye(nsrc) - small_mat
        else:
            small_mat = np.eye(nsrc) + small_mat

        small_mat = np.linalg.inv(np.linalg.cholesky(small_mat))
        myinv.source_model_vectors = small_mat * tmp

        return myinv

    def apply_gains_to_mat(self, g, ant1, ant2):
        """
        Method that multiplies gain vector with the sparse representation of
        the data covariance matix G^T C G, using
        antenna indices for each baseline in the data vector it determines
        which gain is multiplied with which
        covariance entry

        Parameters
        ----------
        g   :   array_like
                (1xN) array that contains the gains of each antenna matching
                the indices in ant1 and ant2
        ant1    :   array_like
                    (1xN) array that contains the indices of the first
                    antenna in each baseline corresponding the gain
                    in g
        ant2    :   array_like
                    (1xN) array that contains the indices of the second
                    antenna in each baseline corresponding the gain
                    in g
        Returns
        -------

        None
        """
        wrapper.apply_gains_to_matrix(
            self.covariance_vectors.ctypes.data,
            g.ctypes.data, ant1.ctypes.data, ant2.ctypes.data,
            self.covariance_vectors.shape[1] // 2,
            self.covariance_vectors.shape[0]
        )
        wrapper.apply_gains_to_matrix(
            self.source_model_vectors.ctypes.data,
            g.ctypes.data, ant1.ctypes.data, ant2.ctypes.data,
            self.source_model_vectors.shape[1] // 2,
            self.source_model_vectors.shape[0]
        )
        return

def get_chisq_dense(g, data, noise, sig, ant1, ant2, scale_fac=1.0,
                    normfac=1.0):
    """
    Not used
    """
    g = g / scale_fac
    cov = sig.copy()
    n = sig.shape[0]
    assert (sig.shape[1] == n)

    wrapper.apply_gains_to_matrix(
        cov.ctypes.data,
        g.ctypes.data,
        ant1.ctypes.data,
        ant2.ctypes.data,
        n / 2, n
    )

    cov = cov.transpose().copy()
    wrapper.apply_gains_to_matrix(
        cov.ctypes.data,
        g.ctypes.data,
        ant1.ctypes.data,
        ant2.ctypes.data,
        n / 2, n
    )

    cov = cov.transpose().copy()
    cov = cov + noise
    cov = 0.5 * (cov + cov.transpose())
    cov_inv = np.linalg.inv(cov)
    rhs = np.dot(cov_inv, data)
    chisq = np.sum(data * np.asarray(rhs))
    nn = g.size / 2
    chisq = chisq + normfac * \
        ((np.sum(g[1::2])) ** 2 + (np.sum(g[0::2]) - nn) ** 2)
    print((chisq, np.mean(g[0::2]), np.mean(g[1::2])))
    return chisq

def get_gradient_dense(gains, data, covariance_matrix, sig, ant1, ant2,
                       scale_fac=1.0, normfac=1.0):
    """
    Not used
    """
    # do_times = False
    # if do_times:
    #     t1 = time.time()
    # mycov = mat.copy()
    gains = gains / scale_fac
    cov = sig.copy()
    n = sig.shape[0]
    wrapper.apply_gains_to_matrix(
        cov.ctypes.data,
        gains.ctypes.data,
        ant1.ctypes.data,
        ant2.ctypes.data,
        n / 2, n
    )
    cov = cov.transpose().copy()
    wrapper.apply_gains_to_matrix(
        cov.ctypes.data,
        gains.ctypes.data,
        ant1.ctypes.data,
        ant2.ctypes.data,
        n / 2, n
    )
    cov = cov + covariance_matrix
    cov = 0.5 * (cov + cov.transpose())
    cov_inv = np.linalg.inv(cov)
    sd = np.dot(cov_inv, data)

    # make g*(c_inv)*d
    gsd = sd.copy()
    wrapper.apply_gains_to_matrix(
        gsd.ctypes.data,
        gains.ctypes.data,
        ant2.ctypes.data,
        ant1.ctypes.data,
        n / 2, 1
    )

    cgsd = np.dot(gsd, sig)

    tmp = cgsd.copy()
    cgsd = np.zeros(tmp.size)
    cgsd[:] = tmp[:]

    tmp = sd.copy()
    sd = np.zeros(tmp.size)
    sd[:] = tmp[:]

    tmp = gsd.copy()
    gsd = np.zeros(tmp.size)
    gsd[:] = tmp[:]

    nant = np.max([np.max(ant1), np.max(ant2)]) + 1
    grad = np.zeros(2 * nant)

    r1 = gains[2 * ant1]
    r2 = gains[2 * ant2]
    i1 = gains[2 * ant1 + 1]
    i2 = gains[2 * ant2 + 1]
    m1r_v2 = 0 * cgsd
    m1i_v2 = 0 * cgsd
    m2r_v2 = 0 * cgsd
    m2i_v2 = 0 * cgsd

    m1r_v2[0::2] = r2 * sd[0::2] - i2 * sd[1::2]
    m1r_v2[1::2] = i2 * sd[0::2] + r2 * sd[1::2]
    m1i_v2[0::2] = i2 * sd[0::2] + r2 * sd[1::2]
    m1i_v2[1::2] = -r2 * sd[0::2] + i2 * sd[1::2]
    m2r_v2[0::2] = r1 * sd[0::2] + i1 * sd[1::2]
    m2r_v2[1::2] = -i1 * sd[0::2] + r1 * sd[1::2]
    m2i_v2[0::2] = i1 * sd[0::2] - r1 * sd[1::2]
    m2i_v2[1::2] = r1 * sd[0::2] + i1 * sd[1::2]

    # if do_times:
    #     t2 = time.time()
    #     print(t2 - t1)

    v1_m1r_v2 = cgsd * m1r_v2
    v1_m1r_v2 = v1_m1r_v2[0::2] + v1_m1r_v2[1::2]
    v1_m1i_v2 = cgsd * m1i_v2
    v1_m1i_v2 = v1_m1i_v2[0::2] + v1_m1i_v2[1::2]
    v1_m2r_v2 = cgsd * m2r_v2
    v1_m2r_v2 = v1_m2r_v2[0::2] + v1_m2r_v2[1::2]
    v1_m2i_v2 = cgsd * m2i_v2
    v1_m2i_v2 = v1_m2i_v2[0::2] + v1_m2i_v2[1::2]
    # if do_times:
    #     t2 = time.time()
    #     print(t2 - t1)

    # print v1_m1r_v2[0:5]

    wrapper.sum_grads_c(
        grad.ctypes.data,
        v1_m1r_v2.ctypes.data,
        v1_m1i_v2.ctypes.data,
        ant1.ctypes.data,
        v1_m2i_v2.size
    )
    wrapper.sum_grads_c(
        grad.ctypes.data,
        v1_m2r_v2.ctypes.data,
        v1_m2i_v2.ctypes.data,
        ant2.ctypes.data,
        v1_m2i_v2.size
    )
    # if do_times:
    #     t2 = time.time()
    #     print(t2 - t1)
    # chisq=np.sum(sd*data)
    # print chisq

    nn = gains.size / 2.0
    grad_real = 2 * (np.sum(gains[0::2]) - nn) / nn
    grad_im = 2 * np.sum(gains[1::2])

    return -2 * grad / scale_fac + normfac * (grad_real + grad_im) / scale_fac

def get_chisq(gains, data, mat, ant1, ant2, scale_fac=1.0, normfac=1.0):
    """
    Calculates the the chi square for the current gains, data and data
    covariance matrix

    Args:
        gains   :   array_like
                    vector that contains the gain of each antenna
        data    :   array_like
                    vector that contains the visibility data split up into
                    alternating real and imaginary pairs
        mat     :   Sparse2Level object
                    sparse representation of the data covariance matrix
        ant1    :   array_like
                    vector that contains the antenna index of the first
                    antenna in baseline, used to reference the gain
                    vector
        ant2    :   array_like
                    vector that contains the antenna index of the second
                    antenna in baseline, used to reference the gain
                    vector
        scale_fac   :   float
                        arbritary scale factor that rescales the gains to
                        help the minimiser actually reach a solution
                        (still unclear to me how this works)

        normfac :   float
                    unknown function

    Returns
    -------
    """
    gains = gains / scale_fac
    # do_times = False
    # if do_times:
    #     t1 = time.time()
    mycov = mat.copy()

    # Something is going wrong here due to np.array types
    mycov.apply_gains_to_mat(gains, ant1, ant2)

    # if do_times:
    #     t2 = time.time()
    #     print(t2 - t1)
    mycov_inv = mycov.inv()
    # if do_times:
    #     t2 = time.time()
    #     print((t2 - t1))
    sd = mycov_inv * data
    chisq = np.sum(sd * data)

    nn = gains.size / 2
    chisq = chisq + normfac * (
            (np.sum(gains[1::2])) ** 2 + (np.sum(gains[0::2]) - nn) ** 2)

    # print(chisq)
    # print(chisq)
    return chisq

def get_gradient(gains, data, covariance_matrix, antenna1_indices,
                 antenna2_indices, scale_factor=1.0, normfac=1.0,
                 do_code_timing=False):
    """

    Parameters
    ----------
    gains   :   array_like
                A (1xN) vector containing the current gain solutions
    data    :   array_like
                A (1x2M) vector that contains the real and imaginary
                components of M measured visibilities.
    covariance_matrix   : Sparse2Level object that contains the data covariance

    antenna1_indices    :   array_like
                            (1xM) vector that contains indices of the first
                            antenna in a baseline pertaining
                            referring to
                            the entries in gains
    antenna2_indices    :   array_like
                            (1xM) vector that contains indices of the second
                            antenna in a baseline pertaining
                            referring to
                            the entries in gains
    scale_factor    :   float
                        A scale factor that makes it easier for the scipy
                        solver to converge.
    normfac :   float
                ???????? Unknown function
    do_code_timing  :   bool
                        Flag when timining output is desired

    Returns
    -------
        Gradient values at current evaluation of the chi-square

    """
    gains = gains / scale_factor

    if do_code_timing:
        t1 = time.time()

    covariance_matrix_copy = covariance_matrix.copy()
    covariance_matrix_copy.apply_gains_to_mat(gains, antenna1_indices,
                                              antenna2_indices)

    if do_code_timing:
        t2 = time.time()
        print(t2 - t1)

    covariance_matrix_inverse = covariance_matrix_copy.inv()

    if do_code_timing:
        t2 = time.time()
        print(t2 - t1)

    sd = covariance_matrix_inverse * data
    gsd = sd.copy()
    wrapper.apply_gains_to_matrix(
        gsd.ctypes.data,
        gains.ctypes.data,
        antenna2_indices.ctypes.data,
        antenna1_indices.ctypes.data,
        gsd.size // 2,
        1
    )
    tmp = covariance_matrix.copy()
    tmp.noise_variance[:] = 0
    cgsd = tmp * gsd

    if do_code_timing:
        t2 = time.time()
        print(t2 - t1)

    nant = np.max([np.max(antenna1_indices), np.max(antenna2_indices)]) + 1
    grad = np.zeros(2 * nant)

    r1 = gains[2 * antenna1_indices]
    r2 = gains[2 * antenna2_indices]
    i1 = gains[2 * antenna1_indices + 1]
    i2 = gains[2 * antenna2_indices + 1]
    m1r_v2 = 0 * cgsd
    m1i_v2 = 0 * cgsd
    m2r_v2 = 0 * cgsd
    m2i_v2 = 0 * cgsd

    m1r_v2[0::2] = r2 * sd[0::2] - i2 * sd[1::2]
    m1r_v2[1::2] = i2 * sd[0::2] + r2 * sd[1::2]
    m1i_v2[0::2] = i2 * sd[0::2] + r2 * sd[1::2]
    m1i_v2[1::2] = -r2 * sd[0::2] + i2 * sd[1::2]
    m2r_v2[0::2] = r1 * sd[0::2] + i1 * sd[1::2]
    m2r_v2[1::2] = -i1 * sd[0::2] + r1 * sd[1::2]
    m2i_v2[0::2] = i1 * sd[0::2] - r1 * sd[1::2]
    m2i_v2[1::2] = r1 * sd[0::2] + i1 * sd[1::2]

    if do_code_timing:
        t2 = time.time()
        print((t2 - t1))

    v1_m1r_v2 = cgsd * m1r_v2
    v1_m1r_v2 = v1_m1r_v2[0::2] + v1_m1r_v2[1::2]
    v1_m1i_v2 = cgsd * m1i_v2
    v1_m1i_v2 = v1_m1i_v2[0::2] + v1_m1i_v2[1::2]
    v1_m2r_v2 = cgsd * m2r_v2
    v1_m2r_v2 = v1_m2r_v2[0::2] + v1_m2r_v2[1::2]
    v1_m2i_v2 = cgsd * m2i_v2
    v1_m2i_v2 = v1_m2i_v2[0::2] + v1_m2i_v2[1::2]
    if do_code_timing:
        t2 = time.time()
        print((t2 - t1))

    # print v1_m1r_v2[0:5]

    wrapper.sum_grads_c(
        grad.ctypes.data,
        v1_m1r_v2.ctypes.data,
        v1_m1i_v2.ctypes.data,
        antenna1_indices.ctypes.data,
        v1_m2i_v2.size
    )
    wrapper.sum_grads_c(
        grad.ctypes.data,
        v1_m2r_v2.ctypes.data,
        v1_m2i_v2.ctypes.data,
        antenna2_indices.ctypes.data,
        v1_m2i_v2.size
    )
    if do_code_timing:
        t2 = time.time()
        print((t2 - t1))
    # chisq=np.sum(sd*data)
    # print chisq

    nn = gains.size / 2.0
    grad_real = 2 * (np.sum(gains[0::2]) - nn) / nn
    grad_im = 2 * np.sum(gains[1::2])
    return -2 * grad / scale_factor + normfac * (
            grad_real + grad_im) / scale_factor
    # return -2*grad/scale_fac

def chol(mat):
    """
    Wrapper function that passes Sparse2Level matrix object to the c-code
    that performs cholesky factorisation

    Parameters
    ----------
    mat :   object
            Sparse2Level matrix object that contains the data covariance

    Returns
    -------
        None
    """
    n = mat.shape[0]
    wrapper.cholesky_factorization(mat.ctypes.data, n)

def many_chol(mat):
    """
    Wrapper function that passes the Sparse2Level matrix object to the
    parallelised c-code that performs cholesky
     into a triangular matrix.

    Parameters
    ----------
    mat :   object
            Sparse2Level matrix object that contains the data covariance

    Returns
    -------
        None
    """

    nmat = mat.shape[0]
    n = mat.shape[1]
    wrapper.cholesky_factorization_parallel(mat.ctypes.data, n, nmat)

def tri_inv(mat):
    """
    Wrapper function that computes the inverse of the data covariance matrix,
    by passing the Sparse2Level matrix object
    to the c-code that handles the inversion of a triangular matrix. Note:
    perform cholesky decomposition first?

    Parameters
    ----------
    mat :   object
            Sparse2Level matrix object that contains the data covariance

    Returns
    -------
    mat_inv :   the inverse of the data covariance matrix
    """
    n = mat.shape[0]
    mat_inv = 0 * mat
    wrapper.tri_inv_c(mat.ctypes.data, mat_inv.ctypes.data, n)
    return mat_inv

def many_tri_inv(mat):
    """
    Optimised matrix inversion?
    Wrapper function that computes the inverse of the data covariance matrix,
    by passing the Sparse2Level matrix object
    to the c-code that handles the inversion of a triangular matrix. Note:
    perform cholesky decomposition first?

    Parameters
    ----------
    mat :   object
            Sparse2Level matrix object that contains the data covariance

    Returns
    -------
    mat_inv :   the inverse of the data covariance matrix
    """

    mat_inv = 0 * mat
    sz = mat.shape

    # if only one matrix comes in, do the correct thing
    if len(sz) == 2:
        wrapper.tri_inv_c(mat.ctypes.data, mat_inv.ctypes.data, sz[0])
        return mat_inv

    nmat = mat.shape[0]
    n = mat.shape[1]
    wrapper.many_tri_inv_c(mat.ctypes.data, mat_inv.ctypes.data, n, nmat)
    return mat_inv

def read_sparse(fname):
    """
    Function that reads the covariance data set that comes with the
    corrcal2_example.py code
    Parameters
    ----------
    fname   :   str
                filename of the data file, should be binary

    Returns
    mat :   object
            Sparse2Level object that contains all covariance information
    -------

    """

    f = open(fname)
    n = np.fromfile(f, 'int32', 1)[0]
    isinv = (np.fromfile(f, 'int32', 1)[0] != 0)
    nsrc = np.fromfile(f, 'int32', 1)[0]
    nblock = np.fromfile(f, 'int32', 1)[0]
    nvec = np.fromfile(f, 'int32', 1)[0]
    lims = np.fromfile(f, 'int32', (nblock + 1))
    diag = np.fromfile(f, 'float64', n)
    vecs = np.fromfile(f, 'float64', nvec * n)
    src = np.fromfile(f, 'float64', nsrc * n)
    crap = np.fromfile(f)
    f.close()

    if crap.size > 0:
        print(('file ' + fname + ' had unexpected length.'))
        return

    vecs = vecs.reshape([nvec, n])
    if nsrc > 0:
        src = src.reshape([nsrc, n])
    mat = Sparse2Level(diag, vecs, src, lims, isinv)
    return mat

def make_uv_grid(u, v, tol=0.01, do_fof=True):
    """
    Function that grids data onto the uv-plane and to determine redundant groups

    Parameters
    ----------
    u   :   u-coordinates of each baseline (in wavelengths)
    v   :   v-coordinates of each baseline (in wavelengths)
    tol :   redundancy tolerance - withing what radius should baselines lie
    from each to be a redundant group. Depends
            on position precision of the array
    do_fof  :   bool
                if True it uses pyfof to find redundant groups. If False: it
                devides the uv_grid in block with size tol

    Returns
    -------
    ii  :   array_like
            Array that maps baselines as passed into make_uv_grid into
            redundant groups
    edges   :   array_like
                contains the starting index of each new redundant group

    iconj:  array_like
            Contains flags indicating whether visibilities are conjugated or
            not (e.g. which part of the uv-plane are
            they)
    """
    isconj = (v < 0) | ((v < tol) & (u < 0))
    u = u.copy()
    v = v.copy()
    u[isconj] = -1 * u[isconj]
    v[isconj] = -1 * v[isconj]
    if have_fof & do_fof:
        uv = np.stack([u, v]).transpose()
        groups = pyfof.friends_of_friends(uv, tol)
        myind = np.zeros(len(u))
        for j, mygroup in enumerate(groups):
            myind[mygroup] = j
        ii = np.argsort(myind)
        edges = np.where(np.diff(myind[ii]) != 0)[0] + 1
    else:
        # break up uv plane into tol-sized blocks
        u_int = np.round(u / tol)
        v_int = np.round(v / tol)
        uv = u_int + np.complex(0, 1) * v_int
        ii = np.argsort(uv)
        uv_sort = uv[ii]
        edges = np.where(np.diff(uv_sort) != 0)[0] + 1
    edges = np.append(0, edges)
    edges = np.append(edges, len(u))

    # map isconj into post-sorting indexing
    isconj = isconj[ii]
    return ii, edges, isconj

def grid_data(vis, u, v, noise, ant1, ant2, tol=0.1, do_fof=True):
    """Re-order the data into redundant groups.  Inputs are (vis,u,v,noise,
    ant1,ant2,tol=0.1)
    where tol is the UV-space distance for points to be considered redundant.
     Data will be
    reflected to have positive u, or positive v for u within tol of zero.  If
    pyfof is
    available, use that for group finding."""

    ii, edges, isconj = make_uv_grid(u, v, tol, do_fof)
    tmp = ant1[isconj]
    ant1[isconj] = ant2[isconj]
    ant2[isconj] = tmp
    vis = vis[ii]
    vis[isconj] = np.conj(vis[isconj])

    ant1 = ant1[ii]
    ant2 = ant2[ii]
    noise = noise[ii]

    return vis, u, v, noise, ant1, ant2, edges, ii, isconj

def mymatmul(a, b):
    n = a.shape[0]
    k = a.shape[1]
    # kk = b.shape[0]
    m = b.shape[1]
    c = np.zeros([n, m])

    wrapper.mymatmul_c(
        a.ctypes.data, k, b.ctypes.data, m, n, m, k, c.ctypes.data, m
    )
    return c

def mult_vecs_by_blocks(vecs, blocks, edges):
    n = vecs.shape[1]
    nvec = vecs.shape[0]
    nblock = edges.size - 1
    ans = np.zeros([nvec, n])
    if edges.dtype.name != 'int64':
        edges.np.asarray(edges, dtype='int64')
    wrapper.mult_vecs_by_blocs_c(
        vecs.ctypes.data, blocks.ctypes.data, n, nvec, nblock,
        edges.ctypes.data, ans.ctypes.data
    )
    return ans

def make_uv_from_antpos(xyz, rmax=0, tol=0.0):
    """Take a list of antenna positions and create a UV snapshot out of it."""
    xyz = xyz.copy()
    nant = xyz.shape[0]
    if xyz.shape[1] == 2:
        xyz = np.c_[xyz, np.zeros(nant)]
    mymed = np.median(xyz, axis=0)
    xyz = xyz - np.repeat([mymed], nant, axis=0)

    if rmax > 0:
        r = np.sqrt(np.sum(xyz ** 2, axis=1))
        xyz = xyz[r <= rmax, :]
        nant = xyz.shape[0]

    # fit to a plane by modelling z=a*x+b*y+c
    mat = np.c_[xyz[:, 0:2], np.ones(nant)]
    lhs = np.dot(mat.transpose(), mat)
    rhs = np.dot(mat.transpose(), xyz[:, 2])
    fitp = np.dot(np.linalg.inv(lhs), rhs)

    # now rotate into that plane.  z should now be vertical axis
    vz = np.dot(fitp, [1.0, 0, 0])
    vvec = np.asarray([1, 0, vz])
    vvec = vvec / np.linalg.norm(vvec)
    uz = np.dot(fitp, [0, 1.0, 0])
    uvec = np.asarray([0, 1, uz])
    uvec = uvec / np.linalg.norm(uvec)
    wvec = np.cross(uvec, vvec)
    rotmat = np.vstack([uvec, vvec, wvec]).transpose()
    xyz = np.dot(xyz, rotmat)

    x = xyz[:, 0].copy()
    xmat = np.repeat([x], nant, axis=0)
    y = xyz[:, 1].copy()
    ymat = np.repeat([y], nant, axis=0)
    antvec = np.arange(nant)

    ant1 = np.repeat([antvec], nant, axis=0)
    ant2 = ant1.copy().transpose()

    u = xmat - xmat.transpose()
    v = ymat - ymat.transpose()
    u = np.tril(u)
    v = np.tril(v)

    ii = (np.abs(u) > 0) & (np.abs(v) > 0)
    u = u[ii]
    v = v[ii]
    ant1 = ant1[ii]
    ant2 = ant2[ii]

    ii = (u < 0) | ((u < tol) & (v < 0))
    tmp = ant1[ii]
    ant1[ii] = ant2[ii]
    ant2[ii] = tmp
    u[ii] = u[ii] * -1
    v[ii] = v[ii] * -1

    return u, v, ant1, ant2, xyz


import os
import os.path as path
import sys
import utils
import pandas as pd

def prepare_data(protocol_file, data_dir, audio_dir):
    wav_scp = path.join(data_dir, "wav.scp")
    utt2spk = path.join(data_dir, "utt2spk")
    utt2lab = path.join(data_dir, "utt2lab")
    utt2id = path.join(data_dir, "utt2id")
    utils.ensure_dir(data_dir)
    df = pd.read_csv(protocol_file, sep=" ", header=None,
                     names=["spk", "utt", "eid", "sid", "label"])
    df = df.sort_values(by=["utt"])
    with open(wav_scp, "w") as wf, open(utt2spk, "w") as usf, open(utt2id, "w") as uif, open(utt2lab, "w") as ulf:
        for idx, row in df.iterrows():
            spk, utt, eid, sid, label = row.tolist()
            id = 0 if label == "bonafide" else 1
            print(f"{utt} flac -c -d -s {audio_dir}/{utt}.flac |", file=wf)
            print(f"{utt} {spk}", file=usf)
            print(f"{utt} {id}", file=uif)
            print(f"{utt} {label} {sid}", file=ulf)

def prepare_set(access, testset, data_dir, root_dir):
    trl = "trl" if testset in ["eval", "dev"] else "trn"
    protocol_file = f"{root_dir}/{access}/ASVspoof2019_{access}_cm_protocols/ASVspoof2019.{access}.cm.{testset}.{trl}.txt"
    audio_dir = f"{root_dir}/{access}/ASVspoof2019_{access}_{testset}/flac/"
    set_data_dir = path.join(data_dir, f"{access}_{testset}".lower())
    prepare_data(protocol_file, set_data_dir, audio_dir)

def prepare_allset(data_dir, root_dir):
    for access in ["LA", "PA"]:
        for testset in ["train", "dev", "eval"]:
            prepare_set(access, testset, data_dir, root_dir)

def main(arg_vec):
    params = {
        "root_dir": r"/home/boren/data/ASVspoof2019;str",
        "data_dir": r"features/data;str",
        "access": r"LA;str",
        "testset": r"eval;str"
    }
    parser = utils.dict2parser(params)
    args = parser.parse_args(arg_vec)
    print(vars(args))
    prepare_allset(args.data_dir, args.root_dir)
    print(f"prepare {args.access}_{args.testset} !")

if __name__ == '__main__':
    if len(sys.argv) > 2:
        arg_vec = sys.argv[1:]
    else:
        params = {
        }
        arg_vec = utils.dict2arg(params)
    main(arg_vec)


import torch
import torch.nn as nn

from nets.caffenet import CaffeNet

from nets.caffe2pynet import caffe2pynet

def caffenet_jigsaw(num_classes,
                    pretrained=True,
                    prototxt_filename='./caffe_models/cfn_jps/_JigsawPuzzleSolver_files_deploy_cfn_jps.prototxt',
                    caffemodel_filename='./caffe_models/cfn_jps/cfn_jps.caffemodel'):

    caffenet = CaffeNet(num_classes=num_classes)
    if pretrained:
        print('Converting jps caffe network to python list')
        pynet = caffe2pynet(prototxt_filename, caffemodel_filename)
        print('Initializing conv weights from pretrained jps')
        caffenet.conv.conv1.weight.data = torch.from_numpy(pynet[9]['weights'][0])
        caffenet.conv.conv1.bias.data = torch.from_numpy(pynet[9]['weights'][1])
        caffenet.conv.conv2.weight.data = torch.from_numpy(pynet[13]['weights'][0])
        caffenet.conv.conv2.bias.data = torch.from_numpy(pynet[13]['weights'][1])
        caffenet.conv.conv3.weight.data = torch.from_numpy(pynet[17]['weights'][0])
        caffenet.conv.conv3.bias.data = torch.from_numpy(pynet[17]['weights'][1])

        caffenet.conv.conv4.weight.data = torch.from_numpy(pynet[19]['weights'][0])
        caffenet.conv.conv4.bias.data = torch.from_numpy(pynet[19]['weights'][1])
        caffenet.conv.conv5.weight.data = torch.from_numpy(pynet[21]['weights'][0])
        caffenet.conv.conv5.bias.data = torch.from_numpy(pynet[21]['weights'][1])

        #caffenet.conv4[0].weight.data = torch.from_numpy(pynet[19]['weights'][0])
        #caffenet.conv4[0].bias.data = torch.from_numpy(pynet[19]['weights'][1])
        #caffenet.conv5[0].weight.data = torch.from_numpy(pynet[21]['weights'][0])
        #caffenet.conv5[0].bias.data = torch.from_numpy(pynet[21]['weights'][1])
        '''
        print('Initializing linear layers with gaussian distribution')
        def init_gaussian(layer):
            nn.init.normal_(layer.weight, mean=0.1, std=0.001)
            layer.bias.data.fill_(0.01)

        caffenet.fc6.fc6.apply(init_gaussian)
        caffenet.fc7.fc7.apply(init_gaussian)
        caffenet.classifier.fc8.apply(init_gaussian)
        '''

    return caffenet

def cfn_rec(num_classes,
            pretrained=True,
            prototxt_filename='./caffe_models/cfn_rec/_JigsawPuzzleSolver_files_deploy_cfn_rec.prototxt',
            caffemodel_filename='./caffe_models/cfn_rec/cfn_rec.caffemodel'):

    cfn_rec = CFN_REC(num_classes=num_classes)

    if pretrained:
        print('Converting caffe network to python list')
        pynet = caffe2pynet(prototxt_filename, caffemodel_filename)

        print('Loading weights')
        cfn_rec.conv.conv1.weight.data = torch.from_numpy(pynet[9]['weights'][0])
        cfn_rec.conv.conv1.bias.data = torch.from_numpy(pynet[9]['weights'][1])
        cfn_rec.conv.conv2.weight.data = torch.from_numpy(pynet[13]['weights'][0])
        cfn_rec.conv.conv2.bias.data = torch.from_numpy(pynet[13]['weights'][1])
        cfn_rec.conv.conv3.weight.data = torch.from_numpy(pynet[17]['weights'][0])
        cfn_rec.conv.conv3.bias.data = torch.from_numpy(pynet[17]['weights'][1])
        cfn_rec.conv.conv4.weight.data = torch.from_numpy(pynet[19]['weights'][0])
        cfn_rec.conv.conv4.bias.data = torch.from_numpy(pynet[19]['weights'][1])
        cfn_rec.conv.conv5.weight.data = torch.from_numpy(pynet[21]['weights'][0])
        cfn_rec.conv.conv5.bias.data = torch.from_numpy(pynet[21]['weights'][1])

        cfn_rec.fc6.fc6.weight.data = torch.from_numpy(pynet[24]['weights'][0])
        cfn_rec.fc6.fc6.bias.data = torch.from_numpy(pynet[24]['weights'][1])

        cfn_rec.fc7.fc7.weight.data = torch.from_numpy(pynet[172]['weights'][0])
        cfn_rec.fc7.fc7.bias.data = torch.from_numpy(pynet[172]['weights'][1])

        #cfn_rec.classifier.fc8.weight.data = torch.from_numpy(pynet[175]['weights'][0])
        #cfn_rec.classifier.fc8.bias.data = torch.from_numpy(pynet[175]['weights'][1])

    return cfn_rec

class Concat_Layer(nn.Module):
    def __init__(self):
        super(Concat_Layer, self).__init__()

    def forward(self, x):
        x = torch.cat(x, 1)
        return x

class CFN_REC(nn.Module):
    def __init__(self, num_classes=1000):
        super(CFN_REC, self).__init__()

        self.conv = nn.Sequential()
        self.conv.add_module('conv1',nn.Conv2d(3, 96, kernel_size=11, stride=2, padding=0))
        self.conv.add_module('relu1',nn.ReLU(inplace=True))
        self.conv.add_module('pool1',nn.MaxPool2d(kernel_size=3, stride=2))
        self.conv.add_module('lrn1',nn.LocalResponseNorm(5))

        self.conv.add_module('conv2',nn.Conv2d(96, 256, kernel_size=5, padding=2, groups=2))
        self.conv.add_module('relu2',nn.ReLU(inplace=True))
        self.conv.add_module('pool2',nn.MaxPool2d(kernel_size=3, stride=2))
        self.conv.add_module('lrn2',nn.LocalResponseNorm(5))

        self.conv.add_module('conv3',nn.Conv2d(256, 384, kernel_size=3, padding=1))
        self.conv.add_module('relu3',nn.ReLU(inplace=True))

        self.conv.add_module('conv4',nn.Conv2d(384, 384, kernel_size=3, padding=1, groups=2))
        self.conv.add_module('relu4',nn.ReLU(inplace=True))

        self.conv.add_module('conv5',nn.Conv2d(384, 256, kernel_size=3, padding=1, groups=2))
        self.conv.add_module('relu5',nn.ReLU(inplace=True))
        self.conv.add_module('pool5',nn.MaxPool2d(kernel_size=3, stride=2))

        self.fc6 = nn.Sequential()
        self.fc6.add_module('fc6',nn.Linear(256*4*4, 512))
        self.fc6.add_module('relu6',nn.ReLU(inplace=True))
        self.fc6.add_module('drop6',nn.Dropout(p=0.5))

        self.concat = Concat_Layer()

        self.fc7 = nn.Sequential()
        self.fc7.add_module('fc7',nn.Linear(9*512,4096))
        self.fc7.add_module('relu7',nn.ReLU(inplace=True))
        self.fc7.add_module('drop7',nn.Dropout(p=0.5))

        self.classifier = nn.Sequential()
        self.classifier.add_module('fc8',nn.Linear(4096, num_classes))

    def forward_once(self, x):
        x = self.conv(x)
        x = x.view(x.size(0), -1)
        x = self.fc6(x)
        return x

    def forward(self, x):

        x_list = []
        for i in range(9):
            z = self.forward_once(x[i])
            x_list.append(z)

        x = self.concat(x_list)
        x = self.fc7(x)
        x = self.classifier(x)
        return x


# The Luhn algorithm is a simple checksum formula used to validate a variety of identification #numbers such as
# credit card numbers, IMEI numbers

def luhn_algorithm(num, length):
    checksum = num % 10
    card_sum = 0
    for i in range(1, length):
        num = int(num / 10)
        number = num % 10
        if i % 2 == 0:
            card_sum += number
        else:
            double_num = number * 2
            if double_num >= 10:
                double_num -= 9
            card_sum += double_num
    return 10 - (card_sum % 10) == checksum

def main():
    card_str = input('Enter a credit card number ')
    card_str = card_str.replace(' ', '')
    length_num = len(card_str)
    card_validator = luhn_algorithm(int(card_str), length_num)
    if card_validator:
        print("This is a valid credit card")
    else:
        print("This is not a valid credit card")

main()


import requests
import datetime
from datetime import datetime
from pytz import timezone

def fetch_data_from_url(payload, url):
    return requests.get(url, params=payload).json()

def load_user_info(data_attempt):
    for attempt in data_attempt["records"]:
        yield {
            "username": attempt["username"],
            "timestamp": attempt["timestamp"],
            "timezone": attempt["timezone"],
        }

def get_midnighters(attempts):
    midnighters = set()
    beginning_of_midnight = 0
    end_of_midnight = 5
    for attempt in attempts:
        time_zone = timezone(attempt["timezone"])
        timestamp = attempt["timestamp"]
        user_commit_time = (datetime.fromtimestamp(timestamp, tz=time_zone))
        if beginning_of_midnight <= user_commit_time.hour <= end_of_midnight:
            midnighters.add(attempt["username"])
    return midnighters

def collect_midnighters_from_all_pages(url):
    page = 1
    set_of_all_midnighters = set()
    while True:
        payload = {"page": page}
        data_attempt = fetch_data_from_url(payload, url)
        num_of_pages = data_attempt["number_of_pages"]
        active_devman_users = load_user_info(data_attempt)
        midnighters = get_midnighters(active_devman_users)
        for user in midnighters:
            set_of_all_midnighters.add(user)
        page += 1
        if page > num_of_pages:
            break
    return set_of_all_midnighters

if __name__ == "__main__":
    url = "https://devman.org/api/challenges/solution_attempts/"
    print("The midnighters are: ")

    for user in collect_midnighters_from_all_pages(url):
        print(user)


import boto3
from botocore.exceptions import ClientError

# Replace sender@example.com with your "From" address.
# This address must be verified with Amazon SES.
SENDER = "S3 Webapp Pipeline Automation <donotreply@np-dev.pge.com>"

# Replace recipient@example.com with a "To" address. If your account
# is still in the sandbox, this address must be verified.
RECIPIENT = owner + "@pge.com"

# Specify a configuration set. If you do not want to use a configuration
# set, comment the following variable, and the
# ConfigurationSetName=CONFIGURATION_SET argument below.
CONFIGURATION_SET = "AED_Notifications_ConfigSet"

# If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
AWS_REGION = "us-west-2"

# The subject line for the email.
SUBJECT = "Your S3 Webapp Pipeline for {fqdn} has been provisioned."

# The email body for recipients with non-HTML email clients.
BODY_TEXT = ("Your S3 Webapp Pipeline for {fqdn} has been provisioned.")

# The HTML body of the email.
BODY_HTML = """

Congrations!  Your S3 Webapp Pipeline for {fqdn} has been provisioned.

This email confirms that your Webapp is currently available at
    https://{fqdn}, which is pipelined to Githubo

      AWS SDK for Python (Boto).

            """

# The character encoding for the email.
CHARSET = "UTF-8"

# Create a new SES resource and specify a region.
client = boto3.client('ses',region_name=AWS_REGION)

# Try to send the email.
try:
    #Provide the contents of the email.
    response = client.send_email(
        Destination={
            'ToAddresses': [
                RECIPIENT,
            ],
        },
        Message={
            'Body': {
                'Html': {
                    'Charset': CHARSET,
                    'Data': BODY_HTML,
                },
                'Text': {
                    'Charset': CHARSET,
                    'Data': BODY_TEXT,
                },
            },
            'Subject': {
                'Charset': CHARSET,
                'Data': SUBJECT,
            },
        },
        Source=SENDER,
        # If you are not using a configuration set, comment or delete the
        # following line
        ConfigurationSetName=CONFIGURATION_SET,
    )
# Display an error if something goes wrong.
except ClientError as e:
    print(e.response['Error']['Message'])
else:
    print("Email sent! Message ID:"),
    print(response['MessageId'])


# -*- coding:utf-8 -*-
"""
author: chuanwu.sun
created: 2017-03-10 22:25
e-mail: chuanwusun at gmail.com
"""
import logging
import sys

logger = logging.getLogger("Tinker")
formatter = logging.Formatter('%(name)-12s %(asctime)s %(levelname)-8s %(message)s', '%a, %d %b %Y %H:%M:%S',)
file_handler = logging.FileHandler("tinker.log")
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler(sys.stdout)
logger.setLevel(logging.INFO)
# print log to file and console
logger.addHandler(stream_handler)
logger.addHandler(file_handler)


from random import random, randint, shuffle, getrandbits, random

class GeneticWordBreak:

    def __init__(self, s: str, dic: set, p_mutation: float = 0.20):
        self.s = s
        self.dic = dic
        self.n = len(s)
        self.p_mutation = p_mutation

    def fitness(self, partitions: list):
        good, bad, pos = 0, 0, 0
        for i, x in enumerate(partitions):
            if x:  #
                ok = self.s[pos:i + 1] in self.dic
                good += ok
                bad += not ok
                pos = i + 1
        ok = self.s[pos:self.n] in self.dic
        good += ok
        bad += not ok
        ret = good / bad if bad > 0 else float('inf')
        return ret

    def tournament(self, pop: list):
        shuffle(pop)
        m = len(pop) // 2
        return [max(pop[i], pop[i + m], key=self.fitness) for i in range(m)] * 2

    def cross(self, pop: list):
        shuffle(pop)
        m = len(pop) // 2
        splits = [randint(0, self.n - 1) for _ in range(m)]
        return [pop[i][0:mid] + pop[i + m][mid:self.n - 1] for i, mid in enumerate(splits)] + \
               [pop[i + m][0:mid] + pop[i][mid:self.n - 1] for i, mid in enumerate(splits)]

    def mutate(self, pop: list):
        def random_flip(partitions: list):
            if random() <= self.p_mutation:
                partitions[randint(0, self.n - 2)] ^= True
            return partitions

        return [random_flip(x) for x in pop]

    def generate_population(self, n):
        n += n & 1  # para que n sea par siempre
        return [[not getrandbits(1) for _ in range(self.n - 1)] for _ in range(n)]

    def iterate(self, pop):
        return self.mutate(self.cross(self.tournament(pop)))

    @staticmethod
    def show(pop):
        for a in pop:
            print(a)

    def getPartition(self, partitions: list):
        words = []
        pos = 0
        for i, x in enumerate(partitions):
            if x:
                word = self.s[pos:i + 1]
                pos = i + 1
                words.append(word)
        word = self.s[pos:self.n]
        words.append(word)
        return words

    def solve(self, iterations: int = 10 ** 3, pop_size: int = 100):
        pop = self.generate_population(pop_size)
        cur = 1
        ans = max(pop, key=self.fitness)
        while cur <= iterations:
            pop = self.iterate(pop)
            best = max(pop, key=self.fitness)
            ans = max(ans, best, key=self.fitness)
            print(f'Iteracion #{cur}')
            print(f'Mejor respuesta en esta iteracion: Fitness = {self.fitness(best)}',
                  ' '.join(self.getPartition(best)), sep='\n')
            print(f'Mejor respuesta hasta el momento: Fitness = {self.fitness(ans)}',
                  ' '.join(self.getPartition(ans)), sep='\n')
            print()
            cur += 1

        pass

# string = 'Wordbreakproblem'
# string = 'Wordbreakproblem'
# dic = { 'this', 'th', 'is', 'famous', 'Word', 'break', 'b', 'r', 'e','a', 'k','br', 'bre', 'brea', 'ak', 'problem' }

string = "JonathanVillegasOscarBurgaMarceloMartinezInteligenciaArtificial"
dic = {'Jonathan', 'Villegas', 'Oscar', 'Burga', 'Marcelo', 'Martinez',
       'Inteligencia', 'Artificial', 'VillegasOscar', 'celoMar'}
solver = GeneticWordBreak(string, dic)

solver.solve()


import csv

def fileSize():
    with open('coordenadas.csv', 'r') as file:
        size = 0
        reader = csv.reader(file)
        for line in reader:
            size = size + 1
        return size

def altura():
    size = fileSize()
    height = [0] * size
    i = 0
    with open('coordenadas.csv', 'r') as file:
        reader = csv.reader(file)
        for row in reader:
            if i < size:
                height[i] = float(row[0])
                i = i + 1
        # print(height)
    height = height[::-1]
    return height

def latitude():
    size = fileSize()
    latituds = [0] * size
    i = 0
    with open('coordenadas.csv', 'r') as file:
        reader = csv.reader(file)
        for row in reader :
            if i<size :
                latituds[i] = float(row[1])
                i = i + 1
    # print(latituds)
    latituds = latituds[::-1]
    return latituds

def longitud():
    size = fileSize()
    lenghts = [0]*size
    i = 0
    with open('coordenadas.csv', 'r') as file:
        reader = csv.reader(file)
        for row in reader :
            if i<size :
                lenghts[i] = float(row[2])
                i = i + 1
        # print(lenghts)
        lenghts = lenghts[::-1]
        return lenghts

# print(altura())
# print()
# print(longitud())
# print()
# print(longitud())


"""
    Ejemplos de manejo de exepciones
"""

# try:

print("Ingrese las edades de estudiantes")

limite = input("Ingrese el numero de estudiantes\n")
limite = int(limite)
suma = 0
contador = 1

while(contador <= limite):
    edad = input("Ingrese la edad del estudiante %d: \n" % contador)
    edad = int(edad)
    suma = suma + edad
    contador = contador + 1
promedio = suma / limite
print("El valor promedio de edades es %f" % (promedio))
# except NameError as e:
#     print("Existe un error:(%s): %s" % (e.__class__, e))
#
# except ValueError as e:
#     print("Existe un error (%s): %s" % (e.__class__, e))
#
# except ZeroDivisionError as e:
#     print("Existe un error (%s): %s" % (e.__class__, e))
#
# except Exception as e:
#    print("Existe un error (%s): %s" % (e.__class__, e))


#!/usr/bin/env python
import os
import platform
import sys

if __name__ == "__main__":
    os.system('conan export TyRoXx/stable')

    def test(settings):
        argv =  " ".join(sys.argv[1:])
        command = "conan test %s %s" % (settings, argv)
        retcode = os.system(command)
        if retcode != 0:
            exit("Error while executing:\n\t %s" % command)

    test('-s arch=x86_64 -s build_type=Debug')
    test('-s arch=x86_64 -s build_type=Release')


"""Author: Brandon Trabucco, Copyright 2019
Serializes the conceptual captions dataset into tf records."""

import tensorflow as tf
import math
import numpy as np
import pickle as pkl
import os
import time
import collections
import sys
import threading
import multiprocessing
import FasterRCNN.inference
from FasterRCNN.inference import create_r101fpn_mask_rcnn_model_graph as create_model
from nltk.tokenize import wordpunct_tokenize as word_tokenize

tf.logging.set_verbosity(tf.logging.INFO)
tf.flags.DEFINE_string("image_folder", "./train_images", "Path to the image files.")
tf.flags.DEFINE_string("tsv_filename", "./Train_GCC-training.tsv", "Path to the TSV file.")
tf.flags.DEFINE_string("vocab_filename", "./word.vocab", "Path to the vocab file.")
tf.flags.DEFINE_string("output_dir", "./train_mask_tfrecords/", "Output data directory.")
tf.flags.DEFINE_integer("num_threads", 16, "Number of threads to use when serializing the dataset.")
tf.flags.DEFINE_integer("num_examples_per_file", 5096, "Number of threads to use when serializing the dataset.")
tf.flags.DEFINE_integer("queue_size", 64, "The number of examples to extract at once.")
tf.flags.DEFINE_integer("image_size", 600, "The size of the image smaller dimension.")
tf.flags.DEFINE_integer("start_at_file_index", 0, "The number of tfrecords to skip.")
tf.flags.DEFINE_string("visible_gpus", "0,0,1", "Which GPU to use.")
tf.flags.DEFINE_string("gpus_memory", "0.4,0.4,1", "Which GPU to use.")
FLAGS = tf.flags.FLAGS

TrainingExample = collections.namedtuple("TrainingExample", (
    "image_id", "image", "caption", "boxes", "masks", "labels",
    "r1", "r2", "r3", "r4", "m1", "m2", "m3", "m4"))

def rescale_shorter_edge(images, new_shorter_edge):
    def compute_longer_edge(height, width, new_shorter_edge):
        return tf.cast(width * new_shorter_edge / height, tf.int32)
    height, width = tf.shape(images)[0], tf.shape(images)[1]
    height_smaller_than_width = tf.less_equal(height, width)
    new_height_and_width = tf.cond(
        height_smaller_than_width,
        lambda: (new_shorter_edge, compute_longer_edge(height, width, new_shorter_edge)),
        lambda: (compute_longer_edge(width, height, new_shorter_edge), new_shorter_edge))
    return tf.image.resize_images(images, new_height_and_width)

class Extractor(object):
    """Helper class for extracting features from images."""
    def __init__(self, list_of_images, gpu_id=0, memory_fraction=1):
        """Creates handles to the TensorFlow computational graph."""
        self.list_of_images = list_of_images
        self.list_of_filenames = []
        for x in list_of_images:
            self.list_of_filenames.append(x.image)
        self.graph = tf.Graph()
        with self.graph.as_default():
            self.image_tensor = self.get_image_tensor(self.list_of_filenames, gpu_id)
            # Load the image tensor using a queue for massive parallelization.
            batch_size = tf.shape(self.image_tensor)[0]
            # Create a single TensorFlow Session for all image decoding calls.
            self.sess = tf.Session(
                graph=self.graph,
                config=tf.ConfigProto(
                    gpu_options=tf.GPUOptions(
                        visible_device_list=str(gpu_id),
                        per_process_gpu_memory_fraction=memory_fraction,
                        )))
            # Creates the mask rcnn model
            self.fetch_dict = create_model(self.image_tensor, self.sess)
            self.boxes = self.fetch_dict["boxes"]
            self.labels = self.fetch_dict["labels"]
            self.masks = self.fetch_dict["masks"]
            self.r1 = self.fetch_dict["region_features_1"]
            self.r2 = self.fetch_dict["region_features_2"]
            self.r3 = self.fetch_dict["region_features_3"]
            self.r4 = self.fetch_dict["region_features_4"]
            self.m1 = self.fetch_dict["mask_features_1"]
            self.m2 = self.fetch_dict["mask_features_2"]
            self.m3 = self.fetch_dict["mask_features_3"]
            self.m4 = self.fetch_dict["mask_features_4"]
            # store a handle to how many images were extracted so far
            self.num_images_so_far = tf.Variable(0)
            self.increment_by_batch_size = tf.assign(self.num_images_so_far, self.num_images_so_far + batch_size)
            self.sess.run(tf.variables_initializer([self.num_images_so_far]))
    def get_image_tensor(self, list_of_filenames, gpu_id):
        """Loads the images using the CPU and coppies them to the GPU.
        Args:
            list_of_bytes: a list of filename strings for encoded images.
        Returns:
            a batch representing an image tensor."""
        # Create a queue of strings
        dataset = tf.data.Dataset.from_tensor_slices(list_of_filenames)
        # Create a lazy image loading function to save memory.
        def lazy_load_image(image_filename):
            with tf.gfile.FastGFile(image_filename, "rb") as f:
                return f.read()
        # Convert from a filename to an image tensor.
        def filename_to_tensor(image_filename):
            image_bytes = tf.py_func(lazy_load_image, [image_filename], tf.string)
            image_tensor = tf.image.decode_jpeg(image_bytes, channels=3)
            return rescale_shorter_edge(image_tensor, FLAGS.image_size)
        # Convert each element to a tensor
        dataset = dataset.map(filename_to_tensor, num_parallel_calls=16)
        dataset = dataset.apply(tf.contrib.data.ignore_errors())
        # Create a batch with the specified size
        dataset = dataset.batch(1)
        def prepare_final_batch(image_tensor):
            return tf.transpose(tf.cast(image_tensor, tf.float32), [0, 3, 1, 2])
        # Copy the data to the GPU ahead of time
        dataset = dataset.map(prepare_final_batch, num_parallel_calls=16)
        dataset = dataset.apply(tf.contrib.data.prefetch_to_device("/gpu:0", buffer_size=2))
        image_tensor = dataset.make_one_shot_iterator().get_next()
        return image_tensor
    def get_num_extracted_so_far(self):
        """Extracts a single batch of image features.
        Returns:
            a integer representing the number of examples processed."""
        return int(self.sess.run(self.num_images_so_far))
    def extract(self):
        """Extracts a single batch of image features.
        Returns:
            a list of ImageMetadata objects."""
        position_before = self.get_num_extracted_so_far()
        (boxes, masks, labels,
            r1, r2, r3, r4, m1, m2, m3, m4, _) = self.sess.run([
            self.boxes, self.masks, self.labels,
            self.r1, self.r2, self.r3, self.r4,
            self.m1, self.m2, self.m3, self.m4,
            self.increment_by_batch_size])
        position_after = self.get_num_extracted_so_far()
        list_of_preextracted_images = []
        for i in range(position_after - position_before):
            image_meta = self.list_of_images[i + position_before]
            list_of_preextracted_images.append(
                TrainingExample(
                    image_id=image_meta.image_id,
                    image=image_meta.image,
                    caption=image_meta.caption,
                    boxes=boxes,
                    masks=masks,
                    labels=labels,
                    r1=r1, r2=r2, r3=r3, r4=r4,
                    m1=m1, m2=m2, m3=m3, m4=m4))
        return list_of_preextracted_images

def process_string(input_string, vocab):
    stage_two = word_tokenize(input_string.strip().lower())
    return [vocab[word] if word in vocab else vocab["<unk>"] for word in (["<s>"] + stage_two + ["</s>"])]

def get_all_training_examples(image_folder, tsv_filename):
    start_time = time.time()
    training_examples = []
    with open(tsv_filename, "r", encoding="utf-8") as tsv_file:
        for i, line in enumerate(tsv_file):
            training_examples.append(
                TrainingExample(
                    i, os.path.join(image_folder, "{0}.jpg".format(i)),
                    line.strip().split("\t")[0], None, None, None,
                        None, None, None, None, None, None, None, None))
    end_time = time.time()
    print("Finished loading training examples, took {0} seconds.".format(end_time - start_time))
    return training_examples

def to_sequence_example(metadata, image_bytes, vocab):
    num_regions = metadata.boxes.shape[0]
    return tf.train.SequenceExample(
        context=tf.train.Features(feature={
            "image/image_id": tf.train.Feature(int64_list=tf.train.Int64List(value=[metadata.image_id])),
            "image/data": tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_bytes])),
        }), feature_lists=tf.train.FeatureLists(feature_list={
            "image/caption": tf.train.FeatureList(feature=[tf.train.Feature(int64_list=tf.train.Int64List(
                value=[word])) for word in process_string(metadata.caption, vocab)]),
            "image/labels": tf.train.FeatureList(feature=[tf.train.Feature(int64_list=tf.train.Int64List(
                value=metadata.labels[i:(i + 1)].ravel())) for i in range(num_regions)]),
            "image/boxes": tf.train.FeatureList(feature=[tf.train.Feature(float_list=tf.train.FloatList(
                value=metadata.boxes[i, :].ravel())) for i in range(num_regions)]),
            "image/masks": tf.train.FeatureList(feature=[tf.train.Feature(float_list=tf.train.FloatList(
                value=metadata.masks[i, :, :].ravel())) for i in range(num_regions)]),
            "image/r1": tf.train.FeatureList(feature=[tf.train.Feature(float_list=tf.train.FloatList(
                value=metadata.r1[i, :].ravel())) for i in range(num_regions)]),
            "image/r2": tf.train.FeatureList(feature=[tf.train.Feature(float_list=tf.train.FloatList(
                value=metadata.r2[i, :].ravel())) for i in range(num_regions)]),
            "image/r3": tf.train.FeatureList(feature=[tf.train.Feature(float_list=tf.train.FloatList(
                value=metadata.r3[i, :].ravel())) for i in range(num_regions)]),
            "image/r4": tf.train.FeatureList(feature=[tf.train.Feature(float_list=tf.train.FloatList(
                value=metadata.r4[i, :].ravel())) for i in range(num_regions)]),
            "image/m1": tf.train.FeatureList(feature=[tf.train.Feature(float_list=tf.train.FloatList(
                value=metadata.m1[i, :].ravel())) for i in range(num_regions)]),
            "image/m2": tf.train.FeatureList(feature=[tf.train.Feature(float_list=tf.train.FloatList(
                value=metadata.m2[i, :].ravel())) for i in range(num_regions)]),
            "image/m3": tf.train.FeatureList(feature=[tf.train.Feature(float_list=tf.train.FloatList(
                value=metadata.m3[i, :].ravel())) for i in range(num_regions)]),
            "image/m4": tf.train.FeatureList(feature=[tf.train.Feature(float_list=tf.train.FloatList(
                value=metadata.m4[i, :].ravel())) for i in range(num_regions)]),
        })).SerializeToString()

def write_all_training_examples(training_examples, vocab_filename, output_dir, num_files_so_far,
        num_examples_per_file, queue_is_finished):
    start_time = time.time()
    print("Starting to write training examples.")
    with open(vocab_filename, "rb") as f:
        vocab = {word: i for i, word in enumerate(list(pkl.load(f)) + ["<s>", "</s>", "<unk>"])}
    while len(training_examples) > 0 or not all(queue_is_finished):
        num_examples_in_this_file = 0
        print("Starting to write a file.")
        output_filename = "%.5d.tfrecord" % (num_files_so_far[0])
        num_files_so_far[0] += 1
        writer = tf.python_io.TFRecordWriter(os.path.join(output_dir, output_filename))
        while num_examples_in_this_file < num_examples_per_file and (len(training_examples) > 0 or not all(queue_is_finished)):
            try:
                x = training_examples.pop(0)
            except Exception as e:
                time.sleep(.5)
                continue
            with tf.gfile.FastGFile(x.image, "rb") as f:
                image_bytes = f.read()
            writer.write(to_sequence_example(x, image_bytes, vocab))
            num_examples_in_this_file += 1
        writer.close()
        sys.stdout.flush()
        print("Finished writing a file.")
    end_time = time.time()
    print("Finished writing training examples, took {0} hours.".format((end_time - start_time) / 3600))

def launch_extractor_thread(thread_id, input_metadata, output_metadata, total_count, queue_is_finished, gpu_id, memory_fraction):
    print("Loading images for the Extractor.")
    try:
        extractor = Extractor(input_metadata, gpu_id=gpu_id, memory_fraction=memory_fraction)
    except Exception as e:
        print("An error was encountered when creating extractor: {0}".format(str(e)))
        exit()
    print("Starting the GPU thread for Extractor.")
    time_since_last_extract = time.time()
    num_extracted_so_far = 0
    while time.time() - time_since_last_extract < 3600.0 and num_extracted_so_far < total_count:
        if len(output_metadata) >= FLAGS.queue_size:
            time.sleep(.5)
            continue
        try:
            buffer = extractor.extract()
            output_metadata.extend(buffer)
            num_extracted_so_far += len(buffer)
            print("Extractor {0} extracted {1:07d} examples: {2:03.3f}% complete: eta {3:05.3f} hours.".format(
                thread_id,
                num_extracted_so_far, num_extracted_so_far / total_count * 100.0,
                (time.time() - time_since_last_extract) / len(buffer) * (total_count - num_extracted_so_far) / 3600.0 ))
            time_since_last_extract = time.time()
        except Exception as e:
            #print("An error was encountered when extracting extractor: {0}".format(str(e)))
            time.sleep(.1)
        queue_is_finished[thread_id] = len(input_metadata) == 0

if __name__ == "__main__":
    if not tf.gfile.IsDirectory(FLAGS.output_dir):
        tf.gfile.MakeDirs(FLAGS.output_dir)
    training_examples = get_all_training_examples(FLAGS.image_folder, FLAGS.tsv_filename)
    training_examples = training_examples[(FLAGS.start_at_file_index * FLAGS.num_examples_per_file):]
    manager = multiprocessing.Manager()
    extracted_training_examples = manager.list()
    num_files_so_far = [FLAGS.start_at_file_index]
    gpu_ids = [int(x) for x in FLAGS.visible_gpus.split(",")]
    memory_fractions = [float(x) for x in FLAGS.gpus_memory.split(",")]
    queue_is_finished = [False for i in gpu_ids]
    split_length = math.ceil(len(training_examples) / len(gpu_ids))
    split_training_examples = [training_examples[i:i + split_length] for i in range(
        0, len(training_examples), split_length)]
    extractor_threads = [
        multiprocessing.Process(
            target=launch_extractor_thread,
            args=(i, z, extracted_training_examples,
                len(z), queue_is_finished, x, y)) for i, x, y, z in zip(
                    list(range(len(gpu_ids))), gpu_ids, memory_fractions, split_training_examples)]
    serializer_threads = [
        threading.Thread(
            target=write_all_training_examples,
            args=(extracted_training_examples, FLAGS.vocab_filename, FLAGS.output_dir, num_files_so_far,
                FLAGS.num_examples_per_file, queue_is_finished)) for i in range(FLAGS.num_threads)]
    for thread in extractor_threads:
        thread.start()
    for thread in serializer_threads:
        thread.start()
    coord = tf.train.Coordinator()
    coord.join(extractor_threads + serializer_threads)


import networkx  as nx
import pandas as pd
import csv

def Hindex(indexList):
    indexSet = sorted(list(set(indexList)), reverse=True)
    for index in indexSet:
        # clist为大于等于指定引用次数index的文章列表
        clist = [i for i in indexList if i >= index]
        # 由于引用次数index逆序排列，当index<=文章数量len(clist)时，得到H指数
        if index <= len(clist):
            break
    return index

def LHindex(graph):
    node = {}  # 每个节点的度数
    H_index = {}  # 每个节点的H-index
    LHindex1 = {}  # 每个节点的LH-index
    for i in graph.nodes():
        node[i] = graph.degree(i)
    for i in graph.nodes():
        test = [node[j] for j in graph[i]]
        H_index[i] = Hindex(test)
    for i in graph.nodes():
        LHindex1[i] = sum(H_index[j] for j in graph[i])
    return LHindex1

G = nx.Graph()
with open('D:/node importance/data.csv', encoding='gbk', errors='ignore') as f:
    rander = csv.reader(f)
    # 对数据循环获取
    edge=[]
    for i in rander:
         line=[]
         line.append(i[0])   #两个点的编号
         line.append(i[1])
         edge.append(line)
G.add_edges_from(edge)
lhindex = LHindex(G)
lhindex=sorted(lhindex.items(), key=lambda e: e[0], reverse=False)
print(len(G.nodes()))
print(G.size())
print(lhindex)
data2 = pd.DataFrame(lhindex)
# with open('D://节点数据//email_betweenness.csv', 'w') as f:
#     [f.write('{0},{1}\n'.format(key, value)) for key, value in bc.items()]
writer = pd.ExcelWriter('D:/node importance/data4/lhindex.xlsx')		# 写入Excel文件
data2.to_excel(writer, 'page_3', float_format='%.5f')		# ‘page_1’是写入excel的sheet名
writer.save()
writer.close()


from django.test import TestCase
from django.core.exceptions import ValidationError

from core.registration.static.models import StaticReg
from systems.tests.utils import create_fake_host
from mozdns.domain.models import Domain
from mozdns.address_record.models import AddressRecord
from mozdns.ptr.models import PTR
from mozdns.view.models import View

from mozdns.ip.utils import ip_to_domain_name

class StaticRegTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()

        self.r2 = self.create_domain(name="128")
        self.r2.save()
        self.n = create_fake_host(hostname="foo.mozilla.com")
        self.n.clean()
        self.n.save()
        View.objects.get_or_create(name="private")

    def do_add(self, label, domain, ip_str, ip_type='4'):
        r = StaticReg(
            label=label, domain=domain, ip_str=ip_str, ip_type=ip_type,
            system=self.n
        )
        r.clean()
        r.save()
        r.details()
        r.get_edit_url()
        r.get_delete_url()
        r.get_absolute_url()
        repr(r)
        return r

    def do_delete(self, r):
        ip_str = r.ip_str
        fqdn = r.fqdn
        r.delete()
        self.assertFalse(
            AddressRecord.objects.filter(ip_str=ip_str, fqdn=fqdn)
        )

    def test1_create_basic(self):
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'label': label, 'domain': domain,
            'ip_str': ip_str
        }
        self.do_add(**kwargs)

    def test2_create_basic(self):
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.1"
        kwargs = {
            'label': label, 'domain': domain, 'ip_str': ip_str,
        }
        i = self.do_add(**kwargs)
        self.assertEqual(self.r1, i.reverse_domain)

        i2 = StaticReg.objects.get(pk=i.pk)
        self.assertEqual(self.r1, i2.reverse_domain)

    def test3_create_basic(self):
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'label': label, 'domain': domain, 'ip_str': ip_str,
        }
        self.do_add(**kwargs)

    def test4_create_basic(self):
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'label': label, 'domain': domain, 'ip_str': ip_str,
        }
        self.do_add(**kwargs)

    def test6_create_basic(self):
        label = "foo1"
        domain = self.f_c
        ip_str = "128.0.0.2"
        kwargs = {
            'label': label, 'domain': domain, 'ip_str': ip_str,
        }
        i = self.do_add(**kwargs)
        self.assertEqual(self.r2, i.reverse_domain)

    def test1_delete(self):
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        r = self.do_add(**kwargs)
        self.do_delete(r)

    def test1_dup_create_basic(self):
        label = "foo3"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'label': label, 'domain': domain, 'ip_str': ip_str,
        }
        self.do_add(**kwargs)
        self.assertRaises(ValidationError, self.do_add, **kwargs)

    def test1_bad_add_for_a_ptr(self):
        # sreg exists, then try ptr and A
        label = "9988food"
        domain = self.c
        ip_str = "10.0.0.1"
        kwargs = {
            'label': label, 'domain': domain, 'ip_str': ip_str,
        }
        ip_type = '4'
        i = self.do_add(**kwargs)
        i.clean()
        i.save()
        a = AddressRecord(label=label, domain=domain, ip_str=ip_str,
                          ip_type=ip_type)
        self.assertRaises(ValidationError, a.save)
        ptr = PTR(ip_str=ip_str, ip_type=ip_type, name=i.fqdn)
        self.assertRaises(ValidationError, ptr.save)

    def test2_bad_add_for_a_ptr(self):
        # PTR and A exist, then try add sreg
        label = "9988fdfood"
        domain = self.c
        ip_str = "10.0.0.1"
        kwargs = {
            'label': label, 'domain': domain, 'ip_str': ip_str,
        }
        ip_type = '4'
        a = AddressRecord.objects.create(
            label=label, domain=domain, ip_str=ip_str, ip_type=ip_type
        )
        PTR.objects.create(ip_str=ip_str, ip_type=ip_type, name=a.fqdn)
        self.assertRaises(ValidationError, self.do_add, **kwargs)

    def test1_bad_reverse_domain(self):
        label = "8888foo"
        domain = self.f_c
        ip_str = "10.0.0.1"
        kwargs = {
            'label': label, 'domain': domain, 'ip_str': ip_str,
        }
        i = self.do_add(**kwargs)
        i.ip_str = "9.0.0.1"
        self.assertRaises(ValidationError, i.save)

    def test1_no_system(self):
        label = "8888foo"
        domain = self.f_c
        ip_str = "10.0.0.1"
        ip_type = '4'
        r = StaticReg(
            label=label, domain=domain, ip_str=ip_str, ip_type=ip_type,
            system=None
        )
        self.assertRaises(ValidationError, r.save)


from cparser import rsParser

# Collects information about a quest
class questParser(rsParser):
  def __init__(self):
    super().__init__()
    self.title = ""
    # Switches to collect data: Title, Quest, Characters (<a>) in the quest
    self.titleCount = False
    self.questStart = False
    self.linkCancel = False
    # A Subsection's Font Size (Eg: A chapter such as "King Veldaban's Dilemma", 2 for <h2>)
    self.headers = 0
    # Additional Section Switches: Prompts on top and table on bottom
    self.ignore = False
    self.infoTable = False

    # docx writing and setting up
    self.document = Document()
    cstyle = self.document.styles['Normal']
    cstyle.paragraph_format.space_before = Pt(0)
    cstyle.paragraph_format.space_after = Pt(1)
    cstyle.font.name = 'Heveltica'

  def handle_starttag(self, tag, attrs):
    if tag == "h1" and ("class", "page-header__title") in attrs:
      self.titleCount = True
    # Quest Start:
    elif tag == "div":
      for (name, data) in attrs:
        if (name == "id" and data == "mw-content-text") or (name == "class" and "mw-content-text" in data):
          self.questStart = True
          break
    # Tables: Prompts on top & info on bottom
    elif self.questStart and tag == "table":
      for (name, data) in attrs:
        if name == "class" and "messagebox" in data:
          self.ignore = True
          self.ignoreConctent = tag
          break
        # Table with links to related items transformed into text
        elif name == "class" and "nowraplinks" in data:
          self.infoTable = True
          self.lastp = self.document.add_paragraph("")
    # Subsection Headers
    elif self.questStart and tag[0] == "h":
      print(tag, attrs)
      self.headers = int(tag[1])

    # Record quest information if quest starts
    if self.questStart:
      super().handle_starttag(tag,attrs)

  def handle_data(self, data):
    processed_data = (data.replace("\n", "")).encode(cencode, 'ignore').decode(cencode)

    # Title
    if self.titleCount:
      self.title = processed_data
      self.document.add_heading(self.title, 0).bold = True
      self.titleCount = False

    # Quest Body
    elif self.questStart and processed_data.strip() != "":
      recent = self.parents[-1]
      # Message Prompt: Ignore
      if self.ignore:
        pass
      elif self.infoTable:
        # Remove three valueless characters in the table on the bottom
        if len(data) <= 1:
          return
        # Content below can be formulated into a table
        # [Similar to] Column in the table
        if recent == "a":
          self.lastp.add_run(processed_data + ", ")
        # [Similar to] New row in the table
        else:
          self.lastp = self.document.add_paragraph(processed_data + ": ")

      # <a> The HTML element after <a> is inline instead of a paragraph like <p>
      elif self.linkCancel:
        self.lastp.add_run(processed_data)
        self.linkCancel = False

      # Section Header
      elif self.headers != 0 and recent == "span":
        head = self.parents[-1]
        print("hmm", type(self.headers), self.headers)
        self.document.add_heading(processed_data, level=self.headers)
        self.headers = 0

      # Bold, Italics, Inline links
      elif recent not in ["b", "i", "a"]:
        self.lastp = self.document.add_paragraph(processed_data)

      elif recent == "b":
        self.lastp = self.document.add_paragraph()
        self.lastp.add_run(processed_data).bold = True

      elif recent == "i":
        self.lastp = self.document.add_paragraph()
        self.lastp.add_run(processed_data).italic = True

      # Links are inline and must not be counted as a new paragraph, as well as the element after that
      elif recent == "a":
        self.lastp.add_run(processed_data)
        self.linkCancel = True
      # Contains a new paragraph
      else:
        self.lastp.add_run(processed_data)

      # Logging:
      print(self.parents, processed_data)

  def handle_endtag(self, tag):
    if not self.questStart or (tag in rsParser.self_closing):
      return
    # Ignore message: Match <table> and ignore condition
    if self.ignore and self.ignoreConctent == tag:
      self.ignore = False

    # HTML tags are like a stack except for tables
    #if self.parents[-1] != tag:
    #  print("open-tag does not match end-tag:", self.parents[-1], tag)
    self.parents = self.parents[:-1]

    if len(self.parents) == 0:
      # print("End quest section")
      self.questStart = False
      try:
        os.remove(self.title+".docx")
      except OSError:
        pass
      validTitle = re.sub("[^a-zA-Z_'!() ]", "", self.title)
      self.document.save("./quests/" + validTitle + ".docx")
    # Subsection Font Size Reset
    if tag[0] == "h":
      self.headers = 0


from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template

def send_email(subject, to_address, from_address, text_template, alternate_templates=None, template_data=None,
               reply_to=None, fail_silently=False):
    alternate_templates = alternate_templates or {}
    template_data = template_data or {}
    plaintext = get_template(text_template)
    text_content = plaintext.render(template_data)
    email = EmailMultiAlternatives(
        subject,
        text_content,
        from_address,
        (to_address,),
        reply_to=reply_to and (reply_to,) or None
    )
    for mimetype, template in alternate_templates.items():
        template_content = get_template(template)
        rendered_content = template_content.render(template_data)
        email.attach_alternative(rendered_content, mimetype)
    email.send(fail_silently=fail_silently)


import time
import random
import datetime
import os
from pandas import DataFrame, to_datetime, DatetimeIndex
import numpy as np
import idealreport.create_html as ch
from idealreport.reporter import Reporter
import htmltag

# simple vertical bar chart, notice additional entry in the data field for orientation

dfhb = DataFrame({'Entity': ['Entity 1', 'Entity 2', 'Entity 3', 'Entity 4', 'Entity 5'],
                  'Stat 1': np.random.randn(5).tolist(),
                  'Stat 2': np.random.randn(5).tolist(),
                 })
dfhb = dfhb.set_index('Entity')
dfhb = dfhb.sort_values(by='Stat 1', ascending=False)

bar_plot = {
    'title': 'Vertical Bar Chart by Create HTML',
    'data': [
        {
            'df': dfhb,
            'type': 'bar',
            'orientation': 'v',
        },
    ],
    'labelX': 'Entity',
    'labelY': '%',
    'staticPlot': False
}

# stacked horizontal bar chart, notice additional entry in the data field for orientation

dfsb = DataFrame({'Entity': ['Entity 1', 'Entity 2', 'Entity 3', 'Entity 4', 'Entity 5'],
                'Foo': [8, 10, 50, 85, 42],
                'Bar': [100, 50, 10, 100, 25]})
dfsb = dfsb.set_index('Entity')
dfsb = dfsb[['Foo', 'Bar']] # make sure columns have desired order

stacked_bar_plot = {
    'title': 'Horizontal Stacked Bar Chart by Create HTML',
    'data': [
        {
            'df': dfsb,
            'type': 'stackedBar',
            'orientation': 'h',
        },
    ],
    'labelX': '$',
    'staticPlot': False
}

# simple line chart

dfl = DataFrame(np.random.randn(200, 3))
dfl.columns = ['a', 'b', 'c']
dfl.a = range(200)
dfl = dfl.set_index('a')
line_plot = {
    'title': 'Line Plot by Create HTML',
    'data': [
        {
            'df': dfl,
            'type': 'line',
        },
    ],
    'staticPlot': False
}

# scatter plot

dfsp = DataFrame(np.random.randn(20, 3))
dfsp.columns = ['a', 'b', 'c']
dfsp = dfsp.set_index('a')
scatter_plot = {
    'title': 'Scatter Plot by Create HTML',
    'data': [
        {
            'df': dfsp,
            'type': 'scatter',
        },
    ],
    'labelX': 'alpha',
    'labelY': 'beta',
    'staticPlot': False
}

# mixed plot types on one graph

df1 = DataFrame(np.random.randn(200, 2))
df1.columns = ['a', 'b1']
df1.sort_values(by='a', ascending=True, inplace=True)
df1 = df1.set_index('a')

df2 = DataFrame(np.random.randn(200, 2))
df2.columns = ['a', 'b2']
df2.sort_values(by='a', ascending=True, inplace=True)
df2 = df2.set_index('a')

multi_scatter_plot = {
    'title': 'Mixed Line and Bar Plot by Create HTML',
    'data': [
        {
            'df': df1,
            'type': 'line',
        },
        {
            'df': df2,
            'type': 'bar',
            'orientation': 'v',
        },
    ],
    'labelX': 'x label',
    'labelY': 'y label',
    'staticPlot': False
}

#different axes

dfa = DataFrame(np.random.randn(20, 2))
dfa.columns = ['x', 'a']
dfa.x = range(20)
dfa = dfa.set_index('x')
dfb = DataFrame(np.random.randn(20, 2))
dfb.columns = ['x', 'b']
dfb.x = range(20)
dfb = dfb.set_index('x')
dfb.b *= 100
multi_axis_plot = {
    'type': 'line',
    'title': 'Multi-Axis Plot by Create HTML',
    'data': [
        {
            'df': dfa,
            'type': 'line',
        },
        {
            'df': dfb,
            'type': 'line',
            'y2': True,
        },
    ],
    'labelX': 'x',
    'labelY': 'ya',
    'labelY2': 'yb',
}

output_path = '/home/jason/ideal/reports/sample_report/output'
output_file = os.path.join(output_path, 'sample_plots.html')
r = Reporter(title='Sample Plots', output_file=output_file)

# report start
r.h += htmltag.h3('This report provides an overivew of the different plot types.')

# bar charts
r.h += htmltag.h4('Bar Charts')
r.plot.bar(df=dfhb, title='Vertical Bar Chart by Reporter', xlabel='Entity', ylabel='%')
r.h += ch.plot(bar_plot)
r.plot.barh(df=dfsb, title='Horizontal Stacked Bar Chart by Reporter', stacked=True, xlabel='$')
r.h += ch.plot(stacked_bar_plot)

# line charts
r.h += htmltag.h4('Line Charts')
r.plot.line(df=dfl, title='Line Chart by Reporter', xlabel='Entity', ylabel='%')
r.h += ch.plot(line_plot)

# scatter plots
r.h += htmltag.h4('Scatter Plots')
r.plot.scatter(df=dfsp, title='Scatter Plot by Reporter', xlabel='alpha', ylabel='beta')
r.h += ch.plot(scatter_plot)

# mixed plot types
r.h += htmltag.h4('Multi Series, Mixed Type Plots')
r.plot.multi(dfs=[df1, df2], types=['line', 'bar'], title='Mixed Line and Bar Plot by Reporter', xlabel='x label', ylabel='y label')
r.h += ch.plot(multi_scatter_plot)
r.plot.multi(dfs=[dfa, dfb], types=['line', 'line'], title='Multi-Axis Plot by Reporter', xlabel='x', ylabel='ya', y2_axis=[False,True], y2label='yb')
r.h += ch.plot(multi_axis_plot)

# generate and save the report HTML
r.generate()


import sentencepiece as spm
from tqdm import tqdm
import os
import re

dir_path = '../data/Pdata/reddit'

sp = spm.SentencePieceProcessor()
sp.load(os.path.join(dir_path, 'spm_8000.model'))

unk_token = 0
sos_token = 1
eos_token = 2

def test():
    text = '我想你啦！'
    tokenized = sp.EncodeAsIds(text)
    print(tokenized)
    retext = sp.DecodeIds(tokenized)
    print(retext)
    print(sp.pad_id())

# def prepross(line, target):
#     if 'his' in target:
#         line = line.replace('\t', eos)
#     line = line.replace(' ','')
#     return line

def tokenize(input_path, output_path):
    target = os.path.split(input_path)[-1]
    cnt = 0
    with open(input_path, 'r') as fin, open(output_path, 'w') as fout:
        for line in tqdm(fin, desc='tokenizing {}'.format(target)):
            cnt += 1
            # if cnt > 100:
            #     break
            line = line.strip()
            if 'his' in target:
                if cnt == 1:
                    tokenized = [2]
                else:
                    tokenized = []
                    for utterance in line.split('\t'):
                        if 'weibo' in dir_path:
                            utterance = utterance.replace(' ','')
                        utterance_ids = sp.EncodeAsIds(utterance) + [2]
                        tokenized.extend(utterance_ids)
            else:
                if 'weibo' in dir_path:
                    line = line.replace(' ', '')
                tokenized = sp.EncodeAsIds(line)
            if target in ['resp.train', 'resp.dev', 'resp.test']:
                tokenized = [sos_token] + tokenized + [eos_token]
            fout.write(' '.join(list(map(lambda x: str(x), tokenized))) + '\n')

if __name__ == "__main__":
    # test()
    phase = ['train', 'dev', 'test']
    filelist = ['post', 'resp']
    for p in phase:
        for f in filelist:
            input_path = os.path.join(dir_path, p, f+'.'+p)
            output_path = os.path.join(dir_path, p, f+'.'+p+'.'+'id'+'.8000')
            tokenize(input_path, output_path)
        for f in filelist:
            input_path = os.path.join(dir_path, p, f+'.'+'his')
            output_path = os.path.join(dir_path, p, f+'.'+'his'+'.'+'id'+'.8000')
            tokenize(input_path, output_path)


import string
import nltk
import pymorphy2

with open('Морфемно-орфографический словарь А. Н. Тихонова.txt', encoding='windows-1251') as f:
    morphs = []
    for line in f:
        line = line.split(' | ')[1].strip().split()[0]
        if line[-1] in string.punctuation:
            line = line[:-1]
        if line[-1].isdigit():
            line = line[:-1]
        line = line.replace("'", '').split('/')
        for morph in line:
            if morph.startswith('-'):
                morph = morph[1:]
            if morph != '':
                morphs.append(morph)

def maxmatch(s):
    i = 0
    j = len(s)
    word_parts = []
    while True:
        if s[i:j] in morphs:
            word_parts.append(s[i:j])
            i = j
            j = len(s)
        else:
            j -= 1
        if i == j:
            break
    return '/'.join(word_parts)

word = ''
while True:
    word_or_text = input('Выберите режим работы: слово (с) или текст (т) (для выхода наберите "exit"): ')
    if word_or_text == 'exit':
        break
    if word_or_text == 'с':
        word = input('Введите слово: ')
        print(maxmatch(word))
    elif word_or_text == 'т':
        file_name = input('Укажите название файла: ')
        morph = pymorphy2.MorphAnalyzer()
        with open(file_name, encoding='utf-8') as f:
            text = f.read().lower()
            text_tokenized = nltk.word_tokenize(text)
            new_text = ''
            for t in text_tokenized:
                p_t = morph.parse(t)[0]
                if t.isalpha() and p_t.tag.POS not in {'PREP', 'CONJ', 'PRCL', "INTJ"}:
                    word_parts = maxmatch(t)
                    new_text += t + ' | ' + word_parts + '\n'
        annotation_file = input('Введите название файла, в который Вам нужно сохранить результаты аннотирования: ')
        with open(annotation_file, 'w', encoding='utf-8') as f:
            f.write(new_text[:-1])
            print('В файл {} были записаны результаты аннотирования.'.format(annotation_file))


__author__ = 'David'

from skimage import data, img_as_float, img_as_int
from skimage import exposure
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib

def plot_img_and_hist(img, axes, bins=256):
    """Plot an image along with its histogram and cumulative histogram.

    """
    img = img_as_float(img)
    ax_img, ax_hist = axes
    ax_cdf = ax_hist.twinx()

    # Display image
    ax_img.imshow(img, cmap=plt.cm.gray)
    ax_img.set_axis_off()

    # Display histogram
    ax_hist.hist(img.ravel(), bins=bins, histtype='step', color='black')
    ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))
    ax_hist.set_xlabel('Pixel intensity')
    ax_hist.set_xlim(0, 1)
    ax_hist.set_yticks([])

    # Display cumulative distribution
    img_cdf, bins = exposure.cumulative_distribution(img, bins)
    ax_cdf.plot(bins, img_cdf, 'r')
    ax_cdf.set_yticks([])

    return ax_img, ax_hist, ax_cdf

def method1(image):
    """
    :param image:
    :return:
    """
    n_intensity_level = 255
    rank = image.ravel().argsort().argsort()
    pixels_per_intensity_level = image.size / n_intensity_level
    return (rank / pixels_per_intensity_level).reshape(image.shape)

def method2(image):
    """
    Adapted from http://www.janeriksolem.net/2009/06/histogram-equalization-with-python-and.html
    :param image:
    :return:
    """
    frequency = np.zeros(255, dtype=int)
    for intensity_level in image.ravel():
        frequency[intensity_level] += 1

    cumulative_frequency = np.zeros(255, dtype=int)
    for i, level in enumerate(frequency):
        cumulative_frequency[i] = level + cumulative_frequency[i - 1] if i > 0 else level

    # Use linear interpolation to get new intensity level for each original intensity level.
    # The cumulative frequency will be the basis for the interpolation.
    return (np.interp(image.flat, np.arange(255), cumulative_frequency)).reshape(image.shape)

if __name__ == '__main__':
    image = data.imread('input/london_bridge.jpg')

    if image.ndim > 2:
        print('Currently only support grayscale image')
        exit(0)

    plt.subplot(131)
    plt.imshow(image, cmap=plt.cm.gray)
    plt.title('Original Image')

    image1 = method1(image)
    plt.subplot(132)
    plt.imshow(image1, cmap=plt.cm.gray)
    plt.title('Method 1')

    image2 = method2(image)
    plt.subplot(133)
    plt.imshow(image2, cmap=plt.cm.gray)
    plt.title('Method 2')

    # Maximize window and show
    figManager = plt.get_current_fig_manager()
    figManager.window.showMaximized()
    plt.show()


from flask import Flask, request
from flask_restful import Resource, Api
import requests
from random import randrange
import json
from back_end_processor import Quote
from time import sleep

app = Flask(__name__)
api = Api(app)

def return_to_front_end(to_send):
    requests.post("http://127.0.0.1:5000/recieve/", json=to_send)

def ask_processing(to_process):
    """ changes capitalization of letters at random """
    to_output = ""

    for letter in to_process["entry_string"]:
        if randrange(0, 2) is 1:
            to_output += letter.capitalize()
        else:
            to_output += letter

    # update dict with processed entry
    to_process.update({"processed_entry": to_output})

    return to_process

class Index(Resource):

    def get(self):
        return "IT WIRKLS"

class Processor(Resource):

    def post(self):
        user_input = request.get_json()
        response = Quote.quote(user_input["user_input"])
        response = {"response": response}

        return_to_front_end(response)

api.add_resource(Index, "/")
api.add_resource(Processor, "/processor/")

if __name__ == "__main__":
    app.run(port=5001, threaded=True, debug=True)


#%%
import numpy as np
from graphtree import graphtree
from starboost import BoostingClassifier
from tests import Gnp, Gnp2, GnpMax, BA_vs_Watts_Strogatz, BAmax, BAone, GnpMaxFeature, Gnp1Q,Gnp2Q
from sklearn.metrics import roc_auc_score
import wandb
wandb.init(project='gta', entity='mlwell-rgb')

#%%

np.random.seed(seed=1133)

#%%
#data_generator = Gnp(10, 0.3, 10, 0.2)
#data_generator = Gnp2(p=0.5, max_size=30)
#data_generator = GnpMax()
#data_generator = BA_vs_Watts_Strogatz()
#data_generator = BAmax()
#data_generator = BAone()
#data_generator = GnpMaxFeature()
data_generator = Gnp2Q()
X_train,y_train = data_generator.get_sample(10000)
X_test,y_test = data_generator.get_sample(1000)

# %%
def test(model, X, y):
    preds = model.predict(X).flatten()
    L2 = np.mean((preds - y) ** 2)
    auc = roc_auc_score(y, preds)

    return(L2, auc)

# %%

def run(max_attention_depth, graph_depth, train_size):
    n_estimators = 50
    learning_rate = 0.1
    gbgta = BoostingClassifier( \
        init_estimator=graphtree(max_attention_depth=max_attention_depth, graph_depths=list(range(0, graph_depth + 1))), \
        base_estimator=graphtree(max_attention_depth=max_attention_depth, graph_depths=list(range(0, graph_depth + 1))), \
        n_estimators = n_estimators,\
        learning_rate = learning_rate)

    y = np.array(y_train[:train_size])
    y = y.flatten()
    gbgta.fit(X_train[:train_size], y)
    (L2_train, auc_train) = test(gbgta, X_train, y_train)
    print("Train: L2 error %5f auc %5f" % (L2_train,auc_train))
    (L2_test, auc_test) = test(gbgta, X_test, y_test)
    print("Test: L2 error %5f auc %5f" % (L2_test,auc_test))
    wandb.log({"problem": data_generator.name, "train-size": train_size,
                "attention-depth": max_attention_depth,
                "graph-depth": graph_depth,
                "n_estimators": n_estimators,
                "learning_rate": learning_rate,
                "L2-train": L2_train,
                "AUC-train": auc_train,
                "L2-test": L2_test,
                "AUC_test": auc_test})
for attention in range(0,3):
    for graph_depth in range(0,3):
        wb = wandb.init(reinit=True)
        for train_size in [10,20,50,100, 200, 500, 1000, 2000, 5000, 10000]:
            print("*** Training with attention=%d graph-depth=%d train_size=%d***" % (attention, graph_depth, train_size))
            run(attention, graph_depth, train_size)
        wb.finish()

# %%


import random
import time
print("Stone Paper Scissors: Reboot13")
time.sleep(1.0)
print('You have 10 Chances')
time.sleep(1.0)
print('Start')
time.sleep(2.0)

level = 11
onLevel = 0
score = 0
opponentScore = 0

while level >= 1:
    level = level - 1
    onLevel = onLevel + 1
    if level == 0 and score == opponentScore:
        print("Result: Tie")
    elif level == 0 and score > opponentScore:
        print("Result: You win")
    elif level == 0 and score < opponentScore:
        print("Result: You loose")
    else:
        randChoice = ["STONE", "PAPER", "SCISSOR"]
        opponentChoice = random.choice(randChoice)
        print("Level: " + str(onLevel))
        yourChoice = input('Choose STONE  PAPER or SCISSOR: ')
        yourChoice = yourChoice.upper()
        if yourChoice == opponentChoice:
            print("Your Choice: {0} Opponent Choice: {1}".format(yourChoice, opponentChoice))
            print("Your Score: " + str(score) + " || Opponent Score: " + str(opponentScore) + "\n")
        elif yourChoice == "STONE" and opponentChoice == "SCISSOR":
            score += 1
            print("Your Choice: {0} Opponent Choice: {1}".format(yourChoice, opponentChoice))
            print("Your Score: {0} || Opponent Score: {1}\n".format(str(score), str(opponentScore)))
        elif yourChoice == "PAPER" and opponentChoice == "STONE":
            score += 1
            print("Your Choice: {0} Opponent Choice: {1}".format(yourChoice, opponentChoice))
            print("Your Score: {0} || Opponent Score: {1}\n".format(str(score), str(opponentScore)))
        elif yourChoice == "SCISSOR" and opponentChoice == "PAPER":
            score += 1
            print("Your Choice: {0} Opponent Choice: {1}".format(yourChoice, opponentChoice))
            print("Your Score: {0} || Opponent Score: {1}\n".format(str(score), str(opponentScore)))
        elif yourChoice not in randChoice:
            print("Invalid input")
        else:
            opponentScore += 1
            print("Your Choice: {0} Opponent Choice: {1}".format(yourChoice, opponentChoice))
            print("Your Score: {0} || Opponent Score: {1}\n".format(str(score), str(opponentScore)))