import cv2
import numpy as np

def load_frames(first_index, directory='data/images/nao_raw/', prefix='nao_img_'):
    file_name1 = directory + prefix + str(first_index).zfill(6) + '.png'
    file_name2 = directory + prefix + str(first_index + 1).zfill(6) + '.png'
    frame1 = cv2.imread(file_name1)
    frame2 = cv2.imread(file_name2)
    return frame1, frame2

def show_optical_flow(frame1, frame2, hsv):
    flow = cv2.calcOpticalFlowFarneback(frame1, frame2, None, 0.5, 3, 15, 3, 5, 1.2, 0)
    mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
    hsv[..., 0] = ang * 180 / np.pi / 2
    hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
    bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
    cv2.imshow('flow', bgr)
    cv2.waitKey(0)

def main(first_index):

    frame1, frame2 = load_frames(first_index)

    hsv = np.zeros_like(frame1)
    hsv[...,1] = 255

    frame1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
    frame2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)

    show_optical_flow(frame1, frame2, hsv)

    cv2.destroyAllWindows()

def show_image(idx, directory='data/images/nao_raw/', prefix='nao_img_'):
    file_name = directory + prefix + str(idx).zfill(6) + '.png'
    frame = cv2.imread(file_name)
    cv2.imshow('frame ' + str(idx), frame)
    cv2.waitKey(0)

if __name__ == '__main__':
    for idx in range(30, 141):
        show_image(idx)
    for idx in range(30, 141):
        main(idx)


# Author: Raphael Vallat <raphaelvallat9@gmail.com>
# Date: July 2018
# Python code inspired from the CircStats MATLAB toolbox (Berens 2009)
# and the brainpipe Python package.
# Reference:
# Berens, Philipp. 2009. CircStat: A MATLAB Toolbox for Circular Statistics.
# Journal of Statistical Software, Articles 31 (10): 1–21.
import numpy as np
from scipy.stats import norm

from .utils import remove_na

__all__ = [
    "convert_angles",
    "circ_axial",
    "circ_mean",
    "circ_r",
    "circ_corrcc",
    "circ_corrcl",
    "circ_rayleigh",
    "circ_vtest",
]

###############################################################################
# HELPER FUNCTIONS
###############################################################################

def _checkangles(angles, axis=None):
    """Internal function to check that angles are in radians."""
    msg = (
        "Angles are not in unit of radians. Please use the "
        "`pingouin.convert_angles` function to map your angles to "
        "the [-pi, pi] range."
    )
    ptp_rad = np.nanmax(angles, axis=axis) - np.nanmin(angles, axis=axis)
    ptp_mask = ptp_rad <= 2 * np.pi
    if not ptp_mask.all():
        raise ValueError(msg)

def convert_angles(angles, low=0, high=360, positive=False):
    """Element-wise conversion of arbitrary-unit circular quantities
    to radians.

    .. versionadded:: 0.3.4

    Parameters
    ----------
    angles : array_like
        Circular data.
    low : float or int, optional
        Low boundary for ``angles`` range.  Default is 0.
    high : float or int, optional
        High boundary for ``angles`` range.  Default is 360
        (for degrees to radians conversion).
    positive : boolean
        If True, radians are mapped on the :math:`[0, 2\\pi]`. Otherwise,
        the resulting angles are mapped from :math:`[-\\pi, \\pi)` (default).

    Returns
    -------
    radians : array_like
        Circular data in radians.

    Notes
    -----
    The formula to convert a set of angles :math:`\\alpha` from an arbitrary
    range :math:`[\\text{high},\\text{low}]` to radians
    :math:`[0, 2\\pi]` is:

    .. math::

        \\alpha_r = \\frac{2\\pi\\alpha}{\\text{high} - \\text{low}}

    If ``positive=False`` (default), the resulting angles in
    radians :math:`\\alpha_r` are then wrapped to the :math:`[-\\pi, \\pi)`
    range:

    .. math::

        (\\text{angle} + \\pi) \\mod 2 \\pi - \\pi

    Examples
    --------
    1. Convert degrees to radians

    >>> from pingouin import convert_angles
    >>> a = [0, 360, 180, 90, 45, 270]
    >>> convert_angles(a, low=0, high=360)
    array([ 0.        ,  0.        , -3.14159265,  1.57079633,  0.78539816,
           -1.57079633])

    with ``positive=True``:

    >>> convert_angles(a, low=0, high=360, positive=True)
    array([0.        , 6.28318531, 3.14159265, 1.57079633, 0.78539816,
           4.71238898])

    2. Convert hours (24h-format) to radians

    >>> sleep_onset = [22.5, 23.25, 24, 0.5, 1]
    >>> convert_angles(sleep_onset, low=0, high=24)
    array([-0.39269908, -0.19634954,  0.        ,  0.13089969,  0.26179939])

    3. Convert radians from :math:`[0, 2\\pi]` to :math:`[-\\pi, \\pi)`:

    >>> import numpy as np
    >>> rad = [0.1, 3.14, 5, 2, 6]
    >>> convert_angles(rad, low=0, high=2*np.pi)
    array([ 0.1       ,  3.14      , -1.28318531,  2.        , -0.28318531])

    4. Convert degrees from a 2-D array

    >>> np.random.seed(123)
    >>> deg = np.random.randint(low=0, high=360, size=(3, 4))
    >>> convert_angles(deg)
    array([[-0.66322512,  1.71042267, -2.26892803,  0.29670597],
           [ 1.44862328,  1.85004901,  2.14675498,  0.99483767],
           [-2.54818071, -2.35619449,  1.67551608,  1.97222205]])
    """
    assert isinstance(positive, bool)
    assert isinstance(high, (int, float)), "high must be numeric"
    assert isinstance(low, (int, float)), "low must be numeric"
    ptp = high - low
    assert ptp > 0, "high - low must be strictly positive."
    angles = np.asarray(angles)
    assert np.nanmin(angles) >= low, "angles cannot be >= low."
    assert np.nanmax(angles) <= high, "angles cannot be <= high."
    # Map to [0, 2pi] range
    rad = angles * (2 * np.pi) / ptp
    if not positive:
        # https://stackoverflow.com/a/29237626/10581531
        # Map to [-pi, pi) range:
        rad = (rad + np.pi) % (2 * np.pi) - np.pi  # [-pi, pi)
        # Map to (-pi, pi] range:
        # rad = np.angle(np.exp(1j * rad))
        # rad = -1 * ((-rad + np.pi) % (2 * np.pi) - np.pi)
    return rad

def circ_axial(angles, n):
    """Transforms n-axial data to a common scale.

    Parameters
    ----------
    angles : array
        Sample of angles in radians
    n : int
        Number of modes

    Returns
    -------
    angles : float
        Transformed angles

    Notes
    -----
    Tranform data with multiple modes (known as axial data) to a unimodal
    sample, for the purpose of certain analysis such as computation of a
    mean resultant vector (see Berens 2009).

    Examples
    --------
    Transform degrees to unimodal radians in the Berens 2009 neuro dataset.

    >>> import numpy as np
    >>> from pingouin import read_dataset
    >>> from pingouin.circular import circ_axial
    >>> df = read_dataset('circular')
    >>> angles = df['Orientation'].to_numpy()
    >>> angles = circ_axial(np.deg2rad(angles), 2)
    """
    angles = np.asarray(angles)
    return np.remainder(angles * n, 2 * np.pi)

###############################################################################
# DESCRIPTIVE STATISTICS
###############################################################################

def circ_mean(angles, w=None, axis=0):
    """Mean direction for (binned) circular data.

    Parameters
    ----------
    angles : array_like
        Samples of angles in radians. The range of ``angles`` must be either
        :math:`[0, 2\\pi]` or :math:`[-\\pi, \\pi]`. If ``angles`` is not
        expressed in radians (e.g. degrees or 24-hours), please use the
        :py:func:`pingouin.convert_angles` function prior to using the present
        function.
    w : array_like
        Number of incidences per bins (i.e. "weights"), in case of binned angle
        data.
    axis : int or None
        Compute along this dimension. Default is the first axis (0).

    Returns
    -------
    mu : float
        Circular mean, in radians.

    See also
    --------
    scipy.stats.circmean, scipy.stats.circstd, pingouin.circ_r

    Notes
    -----
    From Wikipedia:

    *In mathematics, a mean of circular quantities is a mean which is sometimes
    better-suited for quantities like angles, daytimes, and fractional parts
    of real numbers. This is necessary since most of the usual means may not be
    appropriate on circular quantities. For example, the arithmetic mean of 0°
    and 360° is 180°, which is misleading because for most purposes 360° is
    the same thing as 0°.
    As another example, the "average time" between 11 PM and 1 AM is either
    midnight or noon, depending on whether the two times are part of a single
    night or part of a single calendar day.*

    The circular mean of a set of angles :math:`\\alpha` is defined by:

    .. math::

        \\bar{\\alpha} =  \\text{angle} \\left ( \\sum_{j=1}^n \\exp(i \\cdot
        \\alpha_j) \\right )

    For binned angles with weights :math:`w`, this becomes:

    .. math::

        \\bar{\\alpha} =  \\text{angle} \\left ( \\sum_{j=1}^n w \\cdot
        \\exp(i \\cdot \\alpha_j) \\right )

    Missing values in ``angles`` are omitted from the calculations.

    References
    ----------
    * https://en.wikipedia.org/wiki/Mean_of_circular_quantities

    * Berens, P. (2009). CircStat: A MATLAB Toolbox for Circular
      Statistics. Journal of Statistical Software, Articles, 31(10),
      1–21. https://doi.org/10.18637/jss.v031.i10

    Examples
    --------
    1. Circular mean of a 1-D array of angles, in radians

    >>> import pingouin as pg
    >>> angles = [0.785, 1.570, 3.141, 0.839, 5.934]
    >>> round(pg.circ_mean(angles), 4)
    1.013

    Compare with SciPy:

    >>> from scipy.stats import circmean
    >>> import numpy as np
    >>> round(circmean(angles, low=0, high=2*np.pi), 4)
    1.013

    2. Using a 2-D array of angles in degrees

    >>> np.random.seed(123)
    >>> deg = np.random.randint(low=0, high=360, size=(3, 5))
    >>> deg
    array([[322,  98, 230,  17,  83],
           [106, 123,  57, 214, 225],
           [ 96, 113, 126,  47,  73]])

    We first need to convert from degrees to radians:

    >>> rad = np.round(pg.convert_angles(deg, low=0, high=360), 4)
    >>> rad
    array([[-0.6632,  1.7104, -2.2689,  0.2967,  1.4486],
           [ 1.85  ,  2.1468,  0.9948, -2.5482, -2.3562],
           [ 1.6755,  1.9722,  2.1991,  0.8203,  1.2741]])

    >>> pg.circ_mean(rad)  # On the first axis (default)
    array([1.27532162, 1.94336576, 2.23195927, 0.52110503, 1.80240563])
    >>> pg.circ_mean(rad, axis=-1)  # On the last axis (default)
    array([0.68920819, 2.49334852, 1.5954149 ])
    >>> round(pg.circ_mean(rad, axis=None), 4)  # Across the entire array
    1.6954

    Missing values are omitted from the calculations:

    >>> rad[0, 0] = np.nan
    >>> pg.circ_mean(rad)
    array([1.76275   , 1.94336576, 2.23195927, 0.52110503, 1.80240563])

    3. Using binned angles

    >>> np.random.seed(123)
    >>> nbins = 18  # Number of bins to divide the unit circle
    >>> angles_bins = np.linspace(0, 2 * np.pi, nbins)
    >>> # w represents the number of incidences per bins, or "weights".
    >>> w = np.random.randint(low=0, high=5, size=angles_bins.size)
    >>> round(pg.circ_mean(angles_bins, w), 4)
    0.606
    """
    angles = np.asarray(angles)
    _checkangles(angles)  # Check that angles is in radians
    w = np.asarray(w) if w is not None else np.ones(angles.shape)
    assert angles.shape == w.shape, "Input dimensions do not match"
    return np.angle(np.nansum(np.multiply(w, np.exp(1j * angles)), axis=axis))

def circ_r(angles, w=None, d=None, axis=0):
    """Mean resultant vector length for circular data.

    Parameters
    ----------
    angles : array_like
        Samples of angles in radians. The range of ``angles`` must be either
        :math:`[0, 2\\pi]` or :math:`[-\\pi, \\pi]`. If ``angles`` is not
        expressed in radians (e.g. degrees or 24-hours), please use the
        :py:func:`pingouin.convert_angles` function prior to using the present
        function.
    w : array_like
        Number of incidences per bins (i.e. "weights"), in case of binned angle
        data.
    d : float
        Spacing (in radians) of bin centers for binned data. If supplied,
        a correction factor is used to correct for bias in the estimation
        of r.
    axis : int or None
        Compute along this dimension. Default is the first axis (0).

    Returns
    -------
    r : float
        Circular mean vector length.

    See also
    --------
    pingouin.circ_mean

    Notes
    -----
    The length of the mean resultant vector is a crucial quantity for the
    measurement of circular spread or hypothesis testing in directional
    statistics. The closer it is to one, the more concentrated the data
    sample is around the mean direction (Berens 2009).

    The circular vector length of a set of angles :math:`\\alpha` is defined
    by:

    .. math::

        \\bar{\\alpha} =  \\frac{1}{N}\\left \\| \\sum_{j=1}^n
        \\exp(i \\cdot \\alpha_j) \\right \\|

    Missing values in ``angles`` are omitted from the calculations.

    References
    ----------
    * https://en.wikipedia.org/wiki/Mean_of_circular_quantities

    * Berens, P. (2009). CircStat: A MATLAB Toolbox for Circular
      Statistics. Journal of Statistical Software, Articles, 31(10),
      1–21. https://doi.org/10.18637/jss.v031.i10

    Examples
    --------
    1. Mean resultant vector length of a 1-D array of angles, in radians

    >>> import pingouin as pg
    >>> angles = [0.785, 1.570, 3.141, 0.839, 5.934]
    >>> r = pg.circ_r(angles)
    >>> round(r, 4)
    0.4972

    Note that there is a close relationship between the vector length and the
    circular standard deviation, i.e. :math:`\\sigma = \\sqrt{-2 \\ln R}`:

    >>> import numpy as np
    >>> round(np.sqrt(-2 * np.log(r)), 4)
    1.1821

    which gives similar result as SciPy built-in function:

    >>> from scipy.stats import circstd
    >>> round(circstd(angles), 4)
    1.1821

    Sanity check: if all angles are the same, the vector length should be one:

    >>> angles = [3.14, 3.14, 3.14, 3.14]
    >>> round(pg.circ_r(angles), 4)
    1.0

    2. Using a 2-D array of angles in degrees

    >>> np.random.seed(123)
    >>> deg = np.random.randint(low=0, high=360, size=(3, 5))
    >>> deg
    array([[322,  98, 230,  17,  83],
           [106, 123,  57, 214, 225],
           [ 96, 113, 126,  47,  73]])

    We first need to convert from degrees to radians:

    >>> rad = np.round(pg.convert_angles(deg, low=0, high=360), 4)
    >>> rad
    array([[-0.6632,  1.7104, -2.2689,  0.2967,  1.4486],
           [ 1.85  ,  2.1468,  0.9948, -2.5482, -2.3562],
           [ 1.6755,  1.9722,  2.1991,  0.8203,  1.2741]])

    >>> pg.circ_r(rad)  # On the first axis (default)
    array([0.46695499, 0.98398294, 0.3723287 , 0.31103746, 0.42527149])
    >>> pg.circ_r(rad, axis=-1)  # On the last axis (default)
    array([0.28099998, 0.45456096, 0.88261161])
    >>> round(pg.circ_r(rad, axis=None), 4)  # Across the entire array
    0.4486

    Missing values are omitted from the calculations:

    >>> rad[0, 0] = np.nan
    >>> pg.circ_r(rad)
    array([0.99619613, 0.98398294, 0.3723287 , 0.31103746, 0.42527149])

    3. Using binned angles

    >>> np.random.seed(123)
    >>> nbins = 18  # Number of bins to divide the unit circle
    >>> angles_bins = np.linspace(0, 2 * np.pi, nbins)
    >>> # w represents the number of incidences per bins, or "weights".
    >>> w = np.random.randint(low=0, high=5, size=angles_bins.size)
    >>> round(pg.circ_r(angles_bins, w), 4)
    0.3642
    """
    angles = np.asarray(angles)
    _checkangles(angles)  # Check that angles is in radians
    w = np.asarray(w) if w is not None else np.ones(angles.shape)
    assert angles.shape == w.shape, "Input dimensions do not match."

    # Add np.nan in weight vector (otherwise nansum(w) is wrong)
    w = w.astype(float)
    w[np.isnan(angles)] = np.nan

    # Compute weighted sum of cos and sin of angles:
    r = np.nansum(np.multiply(w, np.exp(1j * angles)), axis=axis)

    # Calculate vector length:
    r = np.abs(r) / np.nansum(w, axis=axis)

    # For data with known spacing, apply correction factor
    if d is not None:
        c = d / 2 / np.sin(d / 2)
        r = c * r
    return r

###############################################################################
# INFERENTIAL STATISTICS
###############################################################################

def circ_corrcc(x, y, correction_uniform=False):
    """Correlation coefficient between two circular variables.

    Parameters
    ----------
    x : 1-D array_like
        First circular variable (expressed in radians).
    y : 1-D array_like
        Second circular variable (expressed in radians).
    correction_uniform : bool
        Use correction for uniform marginals.

    Returns
    -------
    r : float
        Correlation coefficient.
    pval : float
        Uncorrected p-value.

    Notes
    -----
    Adapted from the CircStats MATLAB toolbox [1]_.

    The range of ``x`` and ``y`` must be either
    :math:`[0, 2\\pi]` or :math:`[-\\pi, \\pi]`. If ``angles`` is not
    expressed in radians (e.g. degrees or 24-hours), please use the
    :py:func:`pingouin.convert_angles` function prior to using the present
    function.

    Please note that NaN are automatically removed.

    If the ``correction_uniform`` is True, an alternative equation from
    [2]_ (p. 177) is used. If the marginal distribution of ``x`` or ``y`` is
    uniform, the mean is not well defined, which leads to wrong estimates
    of the circular correlation. The alternative equation corrects for this
    by choosing the means in a way that maximizes the positive or negative
    correlation.

    References
    ----------
    .. [1] Berens, P. (2009). CircStat: A MATLAB Toolbox for Circular
           Statistics. Journal of Statistical Software, Articles, 31(10), 1–21.
           https://doi.org/10.18637/jss.v031.i10

    .. [2] Jammalamadaka, S. R., & Sengupta, A. (2001). Topics in circular
           statistics (Vol. 5). world scientific.

    Examples
    --------
    Compute the r and p-value of two circular variables

    >>> from pingouin import circ_corrcc
    >>> x = [0.785, 1.570, 3.141, 3.839, 5.934]
    >>> y = [0.593, 1.291, 2.879, 3.892, 6.108]
    >>> r, pval = circ_corrcc(x, y)
    >>> print(round(r, 3), round(pval, 4))
    0.942 0.0658

    With the correction for uniform marginals

    >>> r, pval = circ_corrcc(x, y, correction_uniform=True)
    >>> print(round(r, 3), round(pval, 4))
    0.547 0.2859
    """
    x = np.asarray(x)
    y = np.asarray(y)
    assert x.size == y.size, "x and y must have the same length."

    # Remove NA
    x, y = remove_na(x, y, paired=True)
    n = x.size

    # Compute correlation coefficient
    x_sin = np.sin(x - circ_mean(x))
    y_sin = np.sin(y - circ_mean(y))

    if not correction_uniform:
        # Similar to np.corrcoef(x_sin, y_sin)[0][1]
        r = np.sum(x_sin * y_sin) / np.sqrt(np.sum(x_sin**2) * np.sum(y_sin**2))
    else:
        r_minus = np.abs(np.sum(np.exp((x - y) * 1j)))
        r_plus = np.abs(np.sum(np.exp((x + y) * 1j)))
        denom = 2 * np.sqrt(np.sum(x_sin**2) * np.sum(y_sin**2))
        r = (r_minus - r_plus) / denom

    # Compute T- and p-values
    tval = (
        np.sqrt((n * (x_sin**2).mean() * (y_sin**2).mean()) / np.mean(x_sin**2 * y_sin**2))
        * r
    )

    # Approximately distributed as a standard normal
    pval = 2 * norm.sf(abs(tval))
    return r, pval

def circ_corrcl(x, y):
    """Correlation coefficient between one circular and one linear variable
    random variables.

    Parameters
    ----------
    x : 1-D array_like
        First circular variable (expressed in radians).
        The range of ``x`` must be either :math:`[0, 2\\pi]` or
        :math:`[-\\pi, \\pi]`. If ``angles`` is not
        expressed in radians (e.g. degrees or 24-hours), please use the
        :py:func:`pingouin.convert_angles` function prior to using the present
        function.
    y : 1-D array_like
        Second circular variable (linear)

    Returns
    -------
    r : float
        Correlation coefficient
    pval : float
        Uncorrected p-value

    Notes
    -----
    Please note that NaN are automatically removed from datasets.

    Examples
    --------
    Compute the r and p-value between one circular and one linear variables.

    >>> from pingouin import circ_corrcl
    >>> x = [0.785, 1.570, 3.141, 0.839, 5.934]
    >>> y = [1.593, 1.291, -0.248, -2.892, 0.102]
    >>> r, pval = circ_corrcl(x, y)
    >>> print(round(r, 3), round(pval, 3))
    0.109 0.971
    """
    from scipy.stats import pearsonr, chi2

    x = np.asarray(x)
    y = np.asarray(y)
    assert x.size == y.size, "x and y must have the same length."

    # Remove NA
    x, y = remove_na(x, y, paired=True)
    n = x.size

    # Compute correlation coefficent for sin and cos independently
    rxs = pearsonr(y, np.sin(x))[0]
    rxc = pearsonr(y, np.cos(x))[0]
    rcs = pearsonr(np.sin(x), np.cos(x))[0]

    # Compute angular-linear correlation (equ. 27.47)
    r = np.sqrt((rxc**2 + rxs**2 - 2 * rxc * rxs * rcs) / (1 - rcs**2))

    # Compute p-value
    pval = chi2.sf(n * r**2, 2)
    return r, pval

def circ_rayleigh(angles, w=None, d=None):
    """Rayleigh test for non-uniformity of circular data.

    Parameters
    ----------
    angles : 1-D array_like
        Samples of angles in radians. The range of ``angles`` must be either
        :math:`[0, 2\\pi]` or :math:`[-\\pi, \\pi]`. If ``angles`` is not
        expressed in radians (e.g. degrees or 24-hours), please use the
        :py:func:`pingouin.convert_angles` function prior to using the present
        function.
    w : array_like
        Number of incidences per bins (i.e. "weights"), in case of binned angle
        data.
    d : float
        Spacing (in radians) of bin centers for binned data. If supplied,
        a correction factor is used to correct for bias in the estimation
        of r.

    Returns
    -------
    z : float
        Z-statistic
    pval : float
        P-value

    Notes
    -----
    The Rayleigh test asks how large the resultant vector length R must be
    to indicate a non-uniform  distribution (Fisher 1995).

    H0: the population is uniformly distributed around the circle
    HA: the populatoin is not distributed uniformly around the circle

    The assumptions for the Rayleigh test are that (1) the distribution has
    only one mode and (2) the data is sampled from a von Mises distribution.

    Examples
    --------
    1. Simple Rayleigh test for non-uniformity of circular data.

    >>> from pingouin import circ_rayleigh
    >>> x = [0.785, 1.570, 3.141, 0.839, 5.934]
    >>> z, pval = circ_rayleigh(x)
    >>> print(round(z, 3), round(pval, 6))
    1.236 0.304844

    2. Specifying w and d

    >>> z, pval = circ_rayleigh(x, w=[.1, .2, .3, .4, .5], d=0.2)
    >>> print(round(z, 3), round(pval, 6))
    0.278 0.806997
    """
    angles = np.asarray(angles)
    _checkangles(angles)  # Check that angles is in radians
    if w is None:
        r = circ_r(angles)
        n = len(angles)
    else:
        assert len(angles) == len(w), "Input dimensions do not match"
        r = circ_r(angles, w, d)
        n = np.sum(w)

    # Compute Rayleigh's statistic
    R = n * r
    z = (R**2) / n

    # Compute p value using approxation in Zar (1999), p. 617
    pval = np.exp(np.sqrt(1 + 4 * n + 4 * (n**2 - R**2)) - (1 + 2 * n))
    return z, pval

def circ_vtest(angles, dir=0.0, w=None, d=None):
    """V test for non-uniformity of circular data with a specified
    mean direction.

    Parameters
    ----------
    angles : 1-D array_like
        Samples of angles in radians. The range of ``angles`` must be either
        :math:`[0, 2\\pi]` or :math:`[-\\pi, \\pi]`. If ``angles`` is not
        expressed in radians (e.g. degrees or 24-hours), please use the
        :py:func:`pingouin.convert_angles` function prior to using the present
        function.
    dir : float
        Suspected mean direction (angle in radians).
    w : array_like
        Number of incidences per bins (i.e. "weights"), in case of binned angle
        data.
    d : float
        Spacing (in radians) of bin centers for binned data. If supplied,
        a correction factor is used to correct for bias in the estimation
        of r.

    Returns
    -------
    V : float
        V-statistic
    pval : float
        P-value

    Notes
    -----
    H0: the population is uniformly distributed around the circle.
    HA: the population is not distributed uniformly around the circle but
    has a mean of dir.

    Note: Not rejecting H0 may mean that the population is uniformly
    distributed around the circle OR that it has a mode but that this mode
    is not centered at dir.

    The V test has more power than the Rayleigh test and is preferred if
    there is reason to believe in a specific mean direction.

    Adapted from the Matlab Circular Statistics Toolbox.

    Examples
    --------
    1. V-test for non-uniformity of circular data.

    >>> from pingouin import circ_vtest
    >>> x = [0.785, 1.570, 3.141, 0.839, 5.934]
    >>> v, pval = circ_vtest(x, dir=1)
    >>> print(round(v, 3), pval)
    2.486 0.05794648732225438

    2. Specifying w and d

    >>> v, pval = circ_vtest(x, dir=0.5, w=[.1, .2, .3, .4, .5], d=0.2)
    >>> print(round(v, 3), round(pval, 5))
    0.637 0.23086
    """
    angles = np.asarray(angles)
    if w is None:
        r = circ_r(angles)
        mu = circ_mean(angles)
        n = len(angles)
    else:
        assert len(angles) == len(w), "Input dimensions do not match"
        r = circ_r(angles, w, d)
        mu = circ_mean(angles, w)
        n = np.sum(w)

    # Compute Rayleigh and V statistics
    R = n * r
    v = R * np.cos(mu - dir)

    # Compute p value
    u = v * np.sqrt(2 / n)
    pval = 1 - norm.cdf(u)
    return v, pval


import random
import math

def distance(x,y):
    return math.sqrt(x**2+y**2)

if __name__ == '__main__':
    #N = int( input('Enter the number of points N, to simulate: ') )
    for n in range(1,8):
        N = 10**n
        inCircle = 0
        for i in range(N):
          x = random.random()
          y = random.random()
          if distance(x,y)<=1.0:
             inCircle +=1
         # print(x,y,distance(x,y),inCircle)
        PiEstimate = 4.0*inCircle/N
        absErr = abs(PiEstimate-math.pi)
        relErr = absErr/math.pi
        print(N,PiEstimate,absErr,relErr)


#!/usr/bin/env python3
"""html_utils.py

Tools to assist with graphyte HTML creation and other XML-related tasks.

"""

# imports
import logging
import os
import re
from subprocess import Popen, PIPE
from tempfile import mkstemp
from os import fdopen
from shutil import move, copy
import pprint
# import webbrowser

# info
__author__ = "Jorge Somavilla"

# initialize logger
logger = logging.getLogger('graphyte')

pp = pprint.PrettyPrinter(indent=4)

def yang_2_uml(gm):
    """Convert YANG module into UML format and store in
    graphyte module object (in_diagram_path attribute).

    :param gm: the graphyte module object
    :return: boolean result
    """
    yang_fname = os.path.basename(gm.in_diagram_path)
    work_yang_path = gm.work_dir + "/" + gm.in_diagram_name
    yang_fname_no_ext = os.path.splitext(yang_fname)[0]
    work_uml_path = gm.work_dir + "/" + yang_fname_no_ext + ".uml"
    copy(gm.in_diagram_path,work_yang_path)
    uml_no_option = "--uml-no=" + gm.pyang_uml_no
    #print ("\npyang --ignore-errors " + uml_no_option + " -f uml " + work_yang_path + " -o " + work_uml_path)
    p1 = Popen(["pyang", "--ignore-errors", uml_no_option, "-f", "uml", work_yang_path,
                "-o", work_uml_path], cwd=gm.run_dir, stdout=PIPE, stderr=PIPE)
    p1.communicate()  # wait for pyang execution
    result = False
    if p1.returncode == 0:
        if os.path.exists(work_uml_path):
            logger.info('         Successfully converted YANG to UML.' + '\r\n')
            result = True
        else:
            logger.info('         Failed to convert YANG to UML. Is pyang installed?' + '\r\n')
            result = True
    else:
        logger.error('              Couldn´t convert YANG to UML. Check ' + yang_fname + ' syntax?\r\n')
        result = False
        return result
    # remove pyang-generated title and footer
    remove_patterns = ['center footer', ' <size:20> UML Generated :', ' endfooter', 'Title ' + yang_fname_no_ext]
    tmp_fh, tmp_uml_path = mkstemp()  # temp file
    with fdopen(tmp_fh, 'w') as new_uml:
        with open(work_uml_path) as old_uml:
            for line in old_uml:
                if not any(p in line for p in remove_patterns):
                    new_uml.write(line)
    if os.path.exists(work_uml_path):
        os.remove(work_uml_path)
    copy(tmp_uml_path, work_uml_path)
    # store as reference diagram
    gm.in_diagram_path = work_uml_path
    return result

def uml_2_svg(gm):
    """Convert UML diagram into SVG format and store in
    graphyte module object (svg_path attribute).

    :param gm: the graphyte module object
    :return: None
    """
    # For pyang generated UML files:
    # Try to remove relative output path from UML file
    # '@startuml' line should be left without additional arguments
    logger.info('         Processing UML file...' + '\r\n')
    uml_fname = os.path.basename(gm.in_diagram_path)
    logger.info('             ' + uml_fname + '\r\n')
    tmp_fh, tmp_uml_path = mkstemp()  # temp file

    with fdopen(tmp_fh, 'w') as new_uml:
        with open(gm.in_diagram_path) as old_uml:
            for line in old_uml:
                new_uml.write(re.sub(r'@startuml.*', r'@startuml', line))
    work_uml_path = gm.work_dir + "/" + gm.in_diagram_name

    if os.path.exists(work_uml_path):
        os.remove(work_uml_path)
    copy(tmp_uml_path, work_uml_path)
    plantuml_out_file = gm.work_dir + "/" + os.path.splitext(gm.in_diagram_name)[0] + ".svg"

    # TODO: cath exception if plantuml not in place
    #print ("java -Xmx1024m -jar utils/plantuml.jar -v -tsvg " + work_uml_path + " -o " + gm.work_dir)
    p1 = Popen(["java","-Xmx1024m", "-jar", "utils/plantuml.jar", "-v", "-tsvg", work_uml_path,
                "-o", gm.work_dir], cwd=gm.run_dir, stdout=PIPE, stderr=PIPE)
    p1.communicate()  # wait for plantuml execution
    gm.svg_path = plantuml_out_file
    d = dict()
    d['modsvgpath'] = { os.path.basename(plantuml_out_file) : plantuml_out_file }
    #pp.pprint(d)
    logger.info('         ...done' + '\r\n')
    return d

def atag_2_gtag(svg_lines, i):
    """Figures out if <a tag should be replaced by <g tag
    in SVG link group, depending on link label used:

     do not replace by <g
     do not replace by <g
     replace by <g

    :param svg_lines: List of SVG lines
    :param i: current line
    :return: True if replace required, False otherwise.
    """
    while i < len(svg_lines):
        line = svg_lines[i]
        try:
            link = re.search('xlink:href="(.*?)"', line).group(1)
        except AttributeError:
            link = False
        if link:
            mod = re.search('mod:(.*)', link)
            lit = re.search('lit:(.*)', link)
            if mod:
                return False
            elif lit:
                return False
            else:
                return True
        i += 1
    return True

def guess_module(gm, name, fullname):
    """Finds the module linked with mod:module_name, looking into available
    modules in the model.

    If found, builds name of final html file for the module.

    If not found, issues a warning and ignores link.

    :param gm: the graphyte model
    :param name: the module specified by the user, stripped off extension,
    to be looked up among known modules.
    :param fullname: full link content as specified by the user.
    :return: "module" if name found, "fullname" if name not found.
    """
    if gm.menu_items:
        item_list = gm.menu_items.split(",")
        for item in item_list:
            item = item.strip()
            if name in item:
                item_no_sp = re.sub(r'\s+', r'_', item)
                module = gm.model_no_sp + "_" + item_no_sp + "_v" + gm.version + ".html"
                return module
    logger.warning('              Could not find linked module \"' + fullname + '\", ignoring.\r\n')
    return fullname

def process_svg(gm):
    """Process SVG so it can be embedded in HTML as
    supported by current web browsers.

    Plantuml, Draw.io, Visio specific sections are
    removed or modified to achieve browser support.

    :param gm: the graphyte module object
    :return: processed_svg, the processed SVG

    """

    # TODO: use xpath

    logger.info('         Processing SVG file...' + '\r\n')
    with open(gm.svg_path, 'r') as f:
        svg_fname = os.path.basename(gm.svg_path)
        logger.info('             ' + svg_fname + '\r\n')
        content = f.read()

        # plantuml svg
        if gm.diagram_is_uml():
            content = re.sub(r'\s+<(?!/text)', '<', content)
            content = re.sub(r'><', '>\n<', content)

        # draw.io svg
        is_drawio_file = False

        if "editor="www.draw.io"" in content \
                or "host="www.draw.io"" in content \
                or "host="scdp.cisco.com&quot" in content \
                or "host="app.diagrams.net&quot" in content \
                or re.search(r'<[^<>]+agent=[^>]+draw.io', content):
            is_drawio_file = True
            content = re.sub(r'width="(.*?)px" height="(.*?)px"'
                             , r'viewBox="0 0 \1 \2"', content)
            content = re.sub(r'><', '>\n<', content)

        processed_svg = ""
        a_tag = False
        svg_tag_level = 0
        svg_tag = False
        in_foreign_obj = False
        skip_line = False
        a_g_stack = []
        curr_tag = ""

        svg_lines = content.splitlines()
        i = 0
        while i < len(svg_lines):
            line = svg_lines[i]
            if re.match(r'^\s*<svg', line):
                svg_tag = True
                svg_tag_level += 1
                if svg_tag_level == 1:
                    line = re.sub(
                        r'<svg',
                        r'<svg id="svg" width="100%" preserveAspectRatio="xMinYMin slice"'
                        , line.rstrip()
                    )
            if svg_tag and svg_tag_level == 1:
                line = re.sub(r'id="(?!svg)"', r'', line.rstrip())
                line = re.sub(r'width="(?!%100)"', r'', line.rstrip())
                line = re.sub(r'height=".*?"', r'', line.rstrip())
                line = re.sub(
                    r'preserveAspectRatio="(?!xMinYMin slice)"'
                    , r'', line.rstrip())  # plantuml
                line = re.sub(r'style=".*?"', r'', line.rstrip())  # plantuml
            if re.match(r'^\s*<g', line):
                a_g_stack.append("g")
            if re.match(r'^\s*<a', line):
                a_tag = True
                if atag_2_gtag(svg_lines, i):
                    a_g_stack.append("g")
                    line = re.sub(r'<a', r'<g', line.rstrip())
                else:
                    a_g_stack.append("a")
            if a_tag:
                link = re.search('xlink:href="(.*?)"', line)
                if link:
                    link_str_old = link.group(1)
                    link_str_old_no_ext = os.path.splitext(link_str_old)[0]
                    mod = re.search('mod:(.*)', link_str_old_no_ext)
                    lit = re.search('lit:(.*)', link_str_old)
                    if mod:
                        link_str_new = guess_module(gm, mod.group(1), link_str_old)
                        line = line.replace(link_str_old, link_str_new)
                    elif lit:
                        link_str_new = lit.group(1)
                        line = line.replace(link_str_old, link_str_new)
                    else:
                        link_str_new = link_str_old.split("\\")[-1]
                        link_str_new = link_str_new.split("/")[-1]
                        gm.push_link(link_str_new)
                        line = line.replace(link_str_old, link_str_new)
                        line = re.sub(r'id=".*"', r'', line.rstrip())
                        line = re.sub(r'xlink:href="(.*?)"', r'class="wrapper" id="\1"', line.rstrip())
                        line = re.sub(r'xlink:actuate="onRequest"', r'', line.rstrip())  # plantuml
                        line = re.sub(r'xlink:show="new"', r'', line.rstrip())  # plantuml
                        line = re.sub(r'xlink:type="simple"', r'', line.rstrip())  # plantuml
            if re.match(r'.*>\s*$', line):
                svg_tag = False
                if a_tag:
                    a_tag = False
            if re.match(r'.*</(a|g)>.*$', line):
                if len(a_g_stack) > 0:
                    curr_tag = a_g_stack.pop()
                if curr_tag == "g":
                    line = re.sub(r'</a', r'</g', line.rstrip())
                curr_tag = ""
            if not is_drawio_file:
                if re.match(r'^\s*<foreignObject', line):
                    in_foreign_obj = True
                if re.match(r'^\s*</foreignObject>', line):
                    in_foreign_obj = False
                    line = ""
            if in_foreign_obj and not is_drawio_file:  # add here other conditions to skip line
                skip_line = True
            if not skip_line:
                processed_svg += (line.rstrip() + "\n")  # appends
            skip_line = False

            # increase loop index
            i += 1
    logger.info('         ...ok' + '\r\n')
    return processed_svg

def build_menu(gm):
    """Build navigation menu for final HTML module, which allows
    navigation between different modules in the graphyte model.

    Stores navigation menu on menu_tags attribute of graphyte module
    object.

    :param gm: the graphyte module object
    :return: None
    """
    logger.info('         Building navigation menu...' + '\r\n')
    if gm.menu_items:
        item_list = gm.menu_items.split(",")
        for item in item_list:
            item = item.strip()
            item_no_sp = re.sub(r'\s+', r'_', item)
            gm.menu_tags += "
" + item + "
"
    else:
        gm.menu_tags += "
" + gm.module + "
"
    logger.info('         ...ok' + '\r\n')
    return

def build_html(gm, processed_svg, file_script, xls_to_script):
    """Assembles final HTML module.

    Uses pre-cooked mod_template and embeds module specific templates,
    parameter lists, navigation menu, SVG diagram, title and viewer initial
    content.

    :param gm: the graphyte module object
    :param processed_svg: the SVG diagram adapted for browser support
    :param file_script: templates and template parameter table
     in JS


# -*- coding: utf-8 -*-
"""
Created on Wed Oct 21 14:16:36 2020

@Author: Zhi-Jiang Yang, Dong-Sheng Cao
@Institution: CBDD Group, Xiangya School of Pharmaceutical Science, CSU, China
@Homepage: http://www.scbdd.com
@Mail: yzjkid9@gmail.com; oriental-cds@163.com
@Blog: https://blog.iamkotori.com

♥I love Princess Zelda forever♥
"""

import numpy as np
import matplotlib.pyplot as plt

def bubble_plot(x, y, sizeData, hueData, yticklabels=None):

    fig, ax = plt.subplots(figsize=(6,20))

    s = 800
    radius = sizeData/sizeData.max()
    hue = hueData/hueData.max()

    scatter = ax.scatter(x, y, c=hue, s=radius*s, cmap='RdBu', edgecolor='grey')

    if yticklabels is not None:
        ax.set_yticks(y)
        ax.set_yticklabels(
            yticklabels,
            fontdict={'family':'arial', 'size':18}
        )

    ax.set_xticklabels(
            [int(x) for x in plt.xticks()[0]],
            fontdict={'family':'arial', 'size':18}
        )

    handles, labels = scatter.legend_elements(
        prop="sizes", alpha=0.8, num=5,
        func=lambda x: (x/s)*sizeData.max(),
        color=(182/255, 31/255, 46/255)
    )

    legendSize = ax.legend(
        handles, labels,
        loc="upper right", title="Gene Number",
        fontsize=14, title_fontsize=12,
        labelspacing=1.3,
        bbox_to_anchor=(1.35, 0.75),
        edgecolor='#ffffff'
    )

    ax.grid(True, zorder=-1000)
    ax.set_axisbelow(True)

    cbar = plt.colorbar(scatter,
                 cax = fig.add_axes([0.98, 0.41, 0.06, 0.1]),
                 # ticks=hueData
                 )
    cbar.ax.set_title('$pP_{value}$', pad=12, fontsize=14)
    cbar.ax.set_yticklabels(["{:.1f}".format(x*hueData.max()) for x in cbar.get_ticks()],
                            fontsize=12)

    ax.add_artist(legendSize)

    ax.set_xlim([x.min()-1, x.max()+1])
    ax.spines['right'].set_linewidth(3)
    ax.spines['left'].set_linewidth(3)
    ax.spines['top'].set_linewidth(3)
    ax.spines['bottom'].set_linewidth(3)

    ax.tick_params(
            direction='in', which='both',
            length=8, width=2,
        )

    cbar.ax.tick_params(
            direction='in', which='both',
            length=4, color='#000000',
            left=True
        )

    ax.set_title(
        'The Most Enriched Pathways',
        fontdict={'size':20, 'family':'arial'},
        pad=18,
        weight='bold'
        )

    plt.show()
    return fig

if "__main__" == __name__:

    import pandas as pd

    data = pd.read_csv(
        'Annotation_filter_KEGG.csv',
        usecols=['Term_1', 'Count', 'PValue', 'FoldEnrichment']
    )

    x = data.FoldEnrichment.values
    y = np.arange(1, len(x)+1)

    sizeData = data.Count.values
    hueData = -np.log10(data.PValue.values)

    fig = bubble_plot(x, y, sizeData, hueData, data.Term_1.values)


import numpy as np
import paddle
from paddlevideo.utils import get_logger
from ..loader.builder import build_dataloader, build_dataset
from ..metrics import build_metric
from ..modeling.builder import build_model
from paddlevideo.utils import load

logger = get_logger("paddlevideo")

@paddle.no_grad()
def val_model(cfg, weights, parallel=True):
    """Test model entry

    Args:
        cfg (dict): configuration.
        weights (str): weights path to load.
        parallel (bool): Whether to do multi-cards testing. Default: True.

    """
    # 1. Construct model.
    if cfg.MODEL.backbone.get('pretrained'):
        cfg.MODEL.backbone.pretrained = ''  # disable pretrain model init
    model = build_model(cfg.MODEL)
    if parallel:
        model = paddle.DataParallel(model)

    model.eval()

    state_dicts = load(weights)
    model.set_state_dict(state_dicts)
    # 2. Construct dataset and dataloader.
    cfg.DATASET.test.test_mode = True
    dataset = build_dataset((cfg.DATASET.valid, cfg.PIPELINE.valid))
    batch_size = cfg.DATASET.get("test_batch_size", 8)
    places = paddle.set_device('gpu')
    # default num worker: 0, which means no subprocess will be created
    num_workers = cfg.DATASET.get('num_workers', 0)
    num_workers = cfg.DATASET.get('test_num_workers', num_workers)
    dataloader_setting = dict(batch_size=batch_size,
                              num_workers=num_workers,
                              places=places,
                              drop_last=False,
                              shuffle=False)

    data_loader = build_dataloader(dataset, **dataloader_setting)

    # add params to metrics
    cfg.METRIC.data_size = len(dataset)
    cfg.METRIC.batch_size = batch_size

    total_num = len(dataset)
    true1 = 0
    true5 = 0
    res = []
    for batch_id, data in enumerate(data_loader):
        label = data[1].numpy()[0]
        outputs,classcore = model(data, mode='valid')
        classcore = classcore.numpy()
        res.append([label]+classcore[0].tolist())

    res = np.array(res,dtype=np.float32)
    np.save(cfg.METRIC.val_npy,res)


import subprocess

def convert_to_seq():
 #input = r"rtsp://admin:Jingdongyun@192.168.1.64:554/h264/ch1/main/av_stream"
 input = r"D:\CCTV.mp4"
 output = r"D:\temp/%04d.png"

 cmd= f'ffmpeg -i "{input}" "{output}"'
 print(cmd)
 subprocess.check_output(cmd,shell=True)

convert_to_seq()


import asyncio
import websockets
import os
import logging
import time

connected = set()

port = os.environ['PORT']

async def producer():
    t = time.localtime()
    current_time = time.strftime("%H:%M:%S", t)
    return current_time + " Number or connected clients: " + str(len(connected))

async def hello(websocket, path):
    try:
        while True:
            connected.add(websocket)
            message = await producer()
            await websocket.send(message)
            await asyncio.sleep(1)
    finally:
        connected.remove(websocket)

start_server = websockets.serve(hello, "0.0.0.0", port)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()


"""project URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from AppMaquinas.views import (index, MaquinaList,
Login, Logout, SignUp, ProfileCreate, ProfileUpdate,
MaquinaMineList, MaquinaUpdate, MaquinaDetail, MaquinaDelete,
MaquinaCreate, MaquinaSearch, MensajeCreate, MensajeDelete, MensajeList,
about
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('about', about, name="about"),
    #urls maquinas
    path('', index, name="index"),
    path('maquina/list', MaquinaList.as_view(), name="maquina-list"),
    path('maquina/list/mine', MaquinaMineList.as_view(),name="maquina-mine"),
    path('maquina/detail/<pk>/', MaquinaDetail.as_view(), name="maquina-detail"),
    path('maquina/update/<pk>/', MaquinaUpdate.as_view(), name="maquina-update"),
    path('maquina/delete/<pk>/', MaquinaDelete.as_view(), name="maquina-delete"),
    path('maquina/create/', MaquinaCreate.as_view(), name="maquina-create"),
    path('maquina/buscar/', MaquinaSearch.as_view(), name="maquina-buscar"),
    #url ingreso sistema
    path('login/', Login.as_view(), name="login"),
    path('logout/', Logout.as_view(), name="logout"),
    path('signup/', SignUp.as_view(), name="signup"),
    #urls manejo perfil
    path('profile/create', ProfileCreate.as_view(), name="profile-create"),
    path('profile/update/<pk>/', ProfileUpdate.as_view(), name="profile-update"),
    # urls Mensaje
    path('mensaje/create/', MensajeCreate.as_view(), name= "mensaje-create"),
    path('mensaje/list', MensajeList.as_view(), name="mensaje-list"),
    path('mensaje/delete/<pk>/', MensajeDelete.as_view(), name="mensaje-delete"),

]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import re
import time

import framework.utils as utils
from framework.packet import Packet
from framework.pmd_output import PmdOutput
from framework.settings import HEADER_SIZE
from framework.test_case import TestCase
from framework.utils import RED
from framework.virt_common import VM

VM_CORES_MASK = "all"

ETHER_STANDARD_MTU = 1518
ETHER_JUMBO_FRAME_MTU = 9000

class TestVfJumboFrame(TestCase):

    supported_vf_driver = ["pci-stub", "vfio-pci"]

    def set_up_all(self):

        self.dut_ports = self.dut.get_ports(self.nic)
        self.verify(len(self.dut_ports) >= 1, "Insufficient ports")
        self.vm0 = None
        self.env_done = False

        self.port = self.dut_ports[0]
        self.vm_port = 0
        cores = self.dut.get_core_list("1S/1C/1T")
        self.port_mask = utils.create_mask([self.port])

        # set vf assign method and vf driver
        self.dut.send_expect("modprobe vfio-pci", "#")
        self.vf_driver = self.get_suite_cfg()["vf_driver"]
        if self.vf_driver is None:
            self.vf_driver = "pci-stub"
        self.verify(self.vf_driver in self.supported_vf_driver, "Unspported vf driver")
        if self.vf_driver == "pci-stub":
            self.vf_assign_method = "pci-assign"
        else:
            self.vf_assign_method = "vfio-pci"
            self.dut.send_expect("modprobe vfio-pci", "#")

        # enable tester mtu
        tester_port = self.tester.get_local_port(self.port)
        self.netobj = self.tester.ports_info[tester_port]["port"]
        self.netobj.enable_jumbo(framesize=ETHER_JUMBO_FRAME_MTU + 100)

        self.setup_vm_env()

    def set_up(self):
        pass

    def setup_vm_env(self, driver="default"):
        """
        Create testing environment with 1VF generated from 1PF
        """
        if self.env_done:
            return

        # bind to default driver
        self.bind_nic_driver(self.dut_ports[:1], driver="")

        self.used_dut_port = self.dut_ports[0]
        self.host_intf = self.dut.ports_info[self.used_dut_port]["intf"]
        tester_port = self.tester.get_local_port(self.used_dut_port)
        self.tester_intf = self.tester.get_interface(tester_port)

        self.dut.generate_sriov_vfs_by_port(self.used_dut_port, 1, driver=driver)
        self.sriov_vfs_port = self.dut.ports_info[self.used_dut_port]["vfs_port"]
        self.vf_mac = "00:10:00:00:00:00"
        self.dut.send_expect(
            "ip link set %s vf 0 mac %s" % (self.host_intf, self.vf_mac), "# "
        )

        try:

            for port in self.sriov_vfs_port:
                port.bind_driver(self.vf_driver)

            time.sleep(1)
            vf_popt = {"opt_host": self.sriov_vfs_port[0].pci}

            # set up VM ENV
            self.vm = VM(self.dut, "vm0", "vf_jumboframe")
            self.vm.set_vm_device(driver=self.vf_assign_method, **vf_popt)
            self.vm_dut = self.vm.start()
            if self.vm_dut is None:
                raise Exception("Set up VM ENV failed!")

            self.vm_testpmd = PmdOutput(self.vm_dut)

        except Exception as e:
            self.destroy_vm_env()
            raise Exception(e)

        self.env_done = True

    def destroy_vm_env(self):
        if getattr(self, "vm", None):
            if getattr(self, "vm_dut", None):
                self.vm_dut.kill_all()
            self.vm_testpmd = None
            self.vm_dut_ports = None
            # destroy vm0
            self.vm.stop()
            self.dut.virt_exit()
            time.sleep(3)
            self.vm = None

        if getattr(self, "used_dut_port", None) != None:
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port)
            self.used_dut_port = None
        self.bind_nic_driver(self.dut_ports[:1], driver=self.drivername)

        self.env_done = False

    def jumboframes_get_stat(self, portid, rx_tx):
        """
        Get packets number from port statistic
        """
        stats = self.vm_testpmd.get_pmd_stats(portid)
        if rx_tx == "rx":
            return [stats["RX-packets"], stats["RX-errors"], stats["RX-bytes"]]
        elif rx_tx == "tx":
            return [stats["TX-packets"], stats["TX-errors"], stats["TX-bytes"]]
        else:
            return None

    def jumboframes_send_packet(self, pktsize, received=True):
        """
        Send 1 packet to portid
        """
        tx_pkts_ori, _, tx_bytes_ori = [
            int(_) for _ in self.jumboframes_get_stat(self.vm_port, "tx")
        ]
        rx_pkts_ori, rx_err_ori, rx_bytes_ori = [
            int(_) for _ in self.jumboframes_get_stat(self.vm_port, "rx")
        ]

        mac = self.vm_dut.get_mac_address(self.vm_port)

        pkt = Packet(pkt_type="UDP", pkt_len=pktsize)
        pkt.config_layer("ether", {"dst": mac})
        pkt.send_pkt(self.tester, tx_port=self.tester_intf, timeout=30)

        time.sleep(1)

        tx_pkts, _, tx_bytes = [
            int(_) for _ in self.jumboframes_get_stat(self.port, "tx")
        ]
        rx_pkts, rx_err, rx_bytes = [
            int(_) for _ in self.jumboframes_get_stat(self.vm_port, "rx")
        ]

        tx_pkts -= tx_pkts_ori
        tx_bytes -= tx_bytes_ori
        rx_pkts -= rx_pkts_ori
        rx_bytes -= rx_bytes_ori
        rx_err -= rx_err_ori

        if received:
            self.verify(
                (rx_pkts == 1) and (tx_pkts == 1), "Packet forward assert error"
            )

            if self.kdriver in ["ixgbe", "ice", "i40e"]:
                self.verify(
                    (rx_bytes + 4) == pktsize,
                    "Rx packet size should be packet size - 4",
                )

            if self.kdriver == "igb":
                self.verify(tx_bytes == pktsize, "Tx packet size should be packet size")
            else:
                self.verify(
                    (tx_bytes + 4) == pktsize,
                    "Tx packet size should be packet size - 4",
                )
        else:
            self.verify(rx_err == 1 or tx_pkts == 0, "Packet drop assert error")

    def test_vf_normal_nojumbo(self):
        """
        This case aims to test transmitting normal size packet without jumbo enable
        """
        # should enable jumbo on host
        self.dutobj = self.dut.ports_info[self.port]["port"]
        self.dutobj.enable_jumbo(framesize=ETHER_STANDARD_MTU)

        self.vm_testpmd.start_testpmd(
            "Default",
            "--max-pkt-len=%d --port-topology=loop --tx-offloads=0x8000"
            % (ETHER_STANDARD_MTU),
        )

        self.vm_testpmd.execute_cmd("set fwd mac")
        self.vm_testpmd.execute_cmd("set promisc all off")
        self.vm_testpmd.execute_cmd("start")

        self.jumboframes_send_packet(ETHER_STANDARD_MTU - 1)
        self.jumboframes_send_packet(ETHER_STANDARD_MTU)

    def test_vf_normal_withjumbo(self):
        """
        When jumbo frame supported, this case is to verify that the normal size
        packet forwarding should be support correct.
        """
        # should enable jumbo on host
        self.dutobj = self.dut.ports_info[self.port]["port"]
        self.dutobj.enable_jumbo(framesize=ETHER_JUMBO_FRAME_MTU)

        self.vm_testpmd.start_testpmd(
            "Default",
            "--max-pkt-len=%d --port-topology=loop --tx-offloads=0x8000"
            % (ETHER_JUMBO_FRAME_MTU),
        )

        self.vm_testpmd.execute_cmd("set fwd mac")
        self.vm_testpmd.execute_cmd("set promisc all off")
        self.vm_testpmd.execute_cmd("start")

        self.jumboframes_send_packet(ETHER_STANDARD_MTU - 1)
        self.jumboframes_send_packet(ETHER_STANDARD_MTU)

    def test_vf_jumbo_nojumbo(self):
        """
        This case aims to test transmitting jumbo frame packet on testpmd without
        jumbo frame support.
        """
        # should enable jumbo on host
        self.dutobj = self.dut.ports_info[self.port]["port"]
        self.dutobj.enable_jumbo(framesize=ETHER_STANDARD_MTU)

        self.vm_testpmd.start_testpmd(
            "Default", "--port-topology=loop --tx-offloads=0x8000"
        )

        self.vm_testpmd.execute_cmd("set fwd mac")
        self.vm_testpmd.execute_cmd("set promisc all off")
        self.vm_testpmd.execute_cmd("start")

        # On igb, for example i350, refer to :DPDK-1117
        # For PF, the max-pkt-len = mtu + 14(IP_hearder_len) + 4(CRC) + 4(VLAN header len) + 4(VLAN header len).
        # For VF, the real max-pkt-len = the given max-pkt-len + 4(VLAN header len).
        # This behavior is leveraged from kernel driver.
        # And it means max-pkt-len is always 4 bytes longer than assumed.

        if self.kdriver == "igb":
            self.jumboframes_send_packet(ETHER_STANDARD_MTU + 1 + 4, False)
        else:
            self.jumboframes_send_packet(ETHER_STANDARD_MTU + 1 + 4 + 4, False)

    def test_vf_jumbo_withjumbo(self):
        """
        When jumbo frame supported, this case is to verify that jumbo frame
        packet can be forwarded correct.
        """
        # should enable jumbo on host
        self.dutobj = self.dut.ports_info[self.port]["port"]
        self.dutobj.enable_jumbo(framesize=ETHER_JUMBO_FRAME_MTU)

        self.vm_testpmd.start_testpmd(
            "Default",
            "--max-pkt-len=%d --port-topology=loop --tx-offloads=0x8000"
            % (ETHER_JUMBO_FRAME_MTU),
        )

        self.vm_testpmd.execute_cmd("set fwd mac")
        self.vm_testpmd.execute_cmd("set promisc all off")
        self.vm_testpmd.execute_cmd("start")

        self.jumboframes_send_packet(ETHER_STANDARD_MTU + 1)
        self.jumboframes_send_packet(ETHER_JUMBO_FRAME_MTU - 1)
        self.jumboframes_send_packet(ETHER_JUMBO_FRAME_MTU)

    def test_vf_jumbo_overjumbo(self):
        """
        When the jubmo frame MTU set as 9000, this case is to verify that the
        packet which the length bigger than MTU can not be forwarded.
        """
        # should enable jumbo on host
        self.dutobj = self.dut.ports_info[self.port]["port"]
        self.dutobj.enable_jumbo(framesize=ETHER_JUMBO_FRAME_MTU)

        self.vm_testpmd.start_testpmd(
            "Default",
            "--max-pkt-len=%d --port-topology=loop --tx-offloads=0x8000"
            % (ETHER_JUMBO_FRAME_MTU),
        )

        self.vm_testpmd.execute_cmd("set fwd mac")
        self.vm_testpmd.execute_cmd("set promisc all off")
        self.vm_testpmd.execute_cmd("start")

        # On 1G NICs, when the jubmo frame MTU set as 9000, the software adjust it to 9004.
        if self.kdriver == "igb":
            self.jumboframes_send_packet(ETHER_JUMBO_FRAME_MTU + 4 + 1, False)
        else:
            self.jumboframes_send_packet(ETHER_JUMBO_FRAME_MTU + 1, False)

    def tear_down(self):
        """
        Run after each test case.
        """
        self.vm_testpmd.execute_cmd("stop")
        self.vm_testpmd.quit()

    def tear_down_all(self):
        """
        When the case of this test suite finished, the environment should
        clear up.
        """
        self.destroy_vm_env()
        self.netobj.enable_jumbo(framesize=ETHER_STANDARD_MTU)


import random
import os
############################################################################
###### Fresh assignments ###################################################
############################################################################
#1  # writing a poem in a file  ######################

f=open("twinkle_poem.txt","w")
f.write("Twinkle tinkle little star \n\t how are wonder what youa are\n\t up above the world is so high\n\t like a diamond in the sky")
f.close()

#1  # searching text in file (normal which is case sensitive) ######################
srch=input("Enter text which you want to search: ")
with open("twinkle_poem.txt") as x:
    f=x.read()
    if srch in f:
        print(f"Entered text {srch.upper()} exists in file")
    else:
        print("text does not exists in file")

#1  # searching text in file (now case insensitive) ######################
srch=input("Enter text which you want to search: ")
with open("twinkle_poem.txt") as x:
    f=x.read()
    f1=f.upper()
    if srch.upper() in f1:
        print(f"Entered text {srch.upper()} exists in file")
    else:
        print("text does not exists in file")

###################################################################################
#2  # find value and compare text in file ######################

def game():
    return random.randint(50,100)

with open("gamescore.txt") as f:
        oldscore=f.read()
        if len(oldscore)==0:
            score=int(input("Enter your number to your chance: "))
            oldscore=0
        else:
            score=oldscore
            print(f"previous score already exists in file which is {score}")

gamescore=game()
print("Computer generated number is : "+str(gamescore))

if int(oldscore)==gamescore:
    print("Both score are same so no need to update file")
elif int(oldscore)>gamescore:
    print("No need to update in file")
elif int(score)>gamescore:
    with open("gamescore.txt","w") as f:
        x=f.write(str(score))
    with open("gamescore.txt") as f:
        x=f.read()
        print(f"This value {score} have been updated in file")
else:
    with open("gamescore.txt","w") as f:
        x=f.write(str(gamescore))
    with open("gamescore.txt") as f:
        x=f.read()
        print(f"This value {gamescore} have been updated in file")

###################################################################################
##3  # Generate multiple files for tables #########################################
print("************* Table generation in seperate files ***********************")
i=int(input("Enter FROM number: "))
j=int(input("Enter TO number: "))+1
os.mkdir("assignment_table")    ## generate a folder using os module
for i in range(i,j):
    with open(f"assignment_table\\table of {i}.txt","w") as f:
        for k in range(1,11):
            f.write(f"{i}x{k}={i*k}")
            if k!=10:
                f.write(f"\n")

###################################################################################
##4  # Program to search text in large file  #########################################
print("************* Program to search text in large file ***********************")
content= True
searchstring=input("Enter text to search")
with open("log.txt") as f:
#    content=f.read()
    i=0
    while content:
        content=f.readline()
        if searchstring in content:
            print(f"Enetered text {searchstring.upper()} exists in file at {i+1}")
        i+=1

###################################################################################
##4  # Program to show file information  #########################################

with open("log.txt") as f:
    print("filename is: ",f.name)   ## using os module which is imported on top
    print("File open mode is ",f.mode)  ## using os module which is imported on top
    print("File open or closed ",f.closed)  ## using os module which is imported on top

check=input("do you want to remove directory genrated for assigment table: y/n ")
if check=='y':
    os.rmdir("assignment_table")
    print("directory deleted successfully")
else:
    print("directory not deleted")


from pyspark.sql import SparkSession
from pyspark.sql.functions import *

spark = SparkSession.builder \
    .appName("Employee") \
    .config("hive.metastore.uris", "thrift://hive-metastore:9083") \
    .config("spark.jars", "/Drivers/SQL_Sever/jdbc/sqljdbc42.jar")\
    .enableHiveSupport() \
    .getOrCreate()

spark.sql("use bronze")
print("_______Bronze Schema is used________")

bronze_Employee = spark.sql("select * from bronze.dimemployee")

bronze_Employee= bronze_Employee\
            .withColumn("Address", array(col("AddressLine1"), col("AddressLine2")))\
            .drop(col("AdditionalContactInfo"), col("Suffix"), col("Title"), col("AddressLine1"), col("AddressLine2"))

bronze_Employee.createOrReplaceTempView("sEmp")
bronze_Employee = spark.sql("""
    select *
    from semp
    where OrganizationNode is not null or OrganizationLevel is not null
""")

print("________Has Been finished from Cleaning_______")

bronze_Employee.write.format("hive").mode("overwrite").saveAsTable("silver.DimEmployee")
print("____________written_____________")


from django.shortcuts import render, HttpResponse
from booktest.models import BookInfo
from PIL import Image, ImageDraw, ImageFont
from django.utils.six import BytesIO
# Create your views here.

# 首页视图
def index(request):
	return render(request,'booktest/index.html')

# 向HTML文件中传递参数
def temp_var(request):
	dict1 = {'title':'haha'}
	list1 = [1,2,3,4]
	books = BookInfo.objects.all()[0]

	return render(request,'booktest/temp_var.html',{'dict1':dict1,'list1':list1,'books':books})

# 标签
def temp_tags(request):
	books = BookInfo.objects.all()
	return render(request,'booktest/temp_tags.html',{'books':books})

# 过滤器
def temp_filter(request):
	books = BookInfo.objects.all()
	return render(request,'booktest/temp_filter.html',{'books':books})

# 继承
def temp_inherit(request):
	return render(request,'booktest/temp_inherit.html')
def temp_inherit_son(request):
	books = BookInfo.objects.all()
	return render(request,'booktest/temp_inherit_son.html',{'books':books})

# 模板转义 不转义会引起xss攻击
def html_zhuan(request):
	return render(request,'booktest/html_zhuan.html',{'content':'
hello world
'})

# xss攻击
def xss_test(request):
    text = ''
    if request.method == 'POST':
        text = request.POST.get('text')
    return render(request, 'booktest/xss.html', {'text': text})

# 生成验证码
def verify_code(request):
	import random
	# 背景色(偏蓝)，宽，高
	bgcolor = (random.randrange(20, 100),random.randrange(20, 100), 255)
	width = 100
	height = 25

	# 创建画面对象
	im = Image.new('RGB', (width, height), bgcolor)
	# 创建画笔对象
	draw = ImageDraw.Draw(im)
	# 调用画笔的point()函数绘制噪点(绘制100个噪点)
	for i in range(1, 100):
		xy = (random.randrange(0,width), random.randrange(0, height))
		fill = (random.randrange(0, 255), 255, random.randrange(0, 255))
		draw.point(xy, fill=fill)
	# 定义验证码的备选值
	str1 = 'ABCD123EFGHIJK456LMNOPQRS789TUVWXYZ0'
	rand_str = ''
	for i in range(0,4):
		rand_str += str1[random.randrange(0, len(str1))]

	# 构造字体对象，
	font = ImageFont.truetype('static/font/MicroExtendFLF.ttf', 23)
	# 构造字体颜色（偏红）
	fontcolor = (255, random.randrange(0, 255), random.randrange(0, 255))

	# 绘制四个字
	draw.text((5, 2), rand_str[0], font=font, fill=fontcolor)
	draw.text((25, 2), rand_str[1], font=font, fill=fontcolor)
	draw.text((50, 2), rand_str[2], font=font, fill=fontcolor)
	draw.text((75, 2), rand_str[3], font=font, fill=fontcolor)
	# 释放画笔
	del draw
	# 存入session 用于做进一步的验证
	request.session['verifycode'] = rand_str

	buf = BytesIO()
	im.save(buf, 'png')
	return HttpResponse(buf.getvalue(), 'image/png')

# 验证码显示
def verify_show(request):
	return render(request, 'booktest/verify_show.html')

# 验证码验证
def verify_yz(request):
	# 获取用户输入
	yzm = request.POST.get('yzm')
	if yzm == '':
		response=HttpResponse('no')

	# 获取服务器session
	verifycode = request.session['verifycode'].lower()
	response=HttpResponse('no')
	if yzm.lower()==verifycode:
		# 登陆成功
		response=HttpResponse('ok')
	return response

# 反向解析
def fan1(request):
	return render(request,'booktest/fan1.html')

def fan2(request):
	return HttpResponse('fan2')


"""
This module contains a wrapper for the Faiss library by Facebook AI Research.
"""

from typing import List, Union, Optional, Dict, Any
import os
os.environ["TOKENIZERS_PARALLELISM"] = "false"

import torch
from transformers import AutoTokenizer, AutoModel
from datasets import Dataset

import pandas as pd

# FAISS library wrapper class
class FaissSearch:
    def __init__(self,
        model_name_or_path: str = 'facebook/bart-large',
        tokenizer_name_or_path: str = 'facebook/bart-large',
        device: str = 'cpu',
        ) -> None:
        r"""
        This function initializes the wrapper for the FAISS library, which is used to perform semantic search.

        .. attention::

            * If you use this class, please make sure to cite the following paper:

                .. code-block:: latex

                    @article{johnson2019billion,
                        title={Billion-scale similarity search with {GPUs}},
                        author={Johnson, Jeff and Douze, Matthijs and J{\'e}gou, Herv{\'e}},
                        journal={IEEE Transactions on Big Data},
                        volume={7},
                        number={3},
                        pages={535--547},
                        year={2019},
                        publisher={IEEE}
                    }

            * The code is based on the following GitHub repository:
                https://github.com/facebookresearch/faiss

        Arguments:
            model_name_or_path (str, optional): The name or path of the model to use. Defaults to 'facebook/bart-large'.
            tokenizer_name_or_path (str, optional): The name or path of the tokenizer to use. Defaults to 'facebook/bart-large'.
            device (str, optional): The device to use. Defaults to 'cpu'.

        Returns:
            None
        """

        # Set the device
        self.device = device

        # If the tokenizer is not specified, use the model name or path
        if tokenizer_name_or_path is None:
            tokenizer_name_or_path = model_name_or_path

        # Load the tokenizer
        self.tokenizer = AutoTokenizer.from_pretrained(tokenizer_name_or_path)

        # Load the model
        self.model = AutoModel.from_pretrained(model_name_or_path).to(self.device)

        # Set the model to evaluation mode (since we do not need the gradients)
        self.model.eval()

        # Initialize the dataset
        self.dataset = None

    # Auxiliary function to get the last hidden state
    def get_last_hidden_state(self,
        embeddings: torch.Tensor,
    ) -> torch.Tensor:
        """
        This function returns the last hidden state (e.g., [CLS] token's) of the input embeddings.

        Arguments:
            embeddings (torch.Tensor): The input embeddings.

        Returns:
            torch.Tensor: The last hidden state.
        """

        # Get the last hidden state
        last_hidden_state = embeddings.last_hidden_state

        # Return the last hidden state
        return last_hidden_state[:, 0, :]

    # Auxiliary function to get the mean pooling
    def get_mean_pooling(self,
        embeddings: torch.Tensor,
    ) -> torch.Tensor:
        """
        This function returns the mean pooling of the input embeddings.

        Arguments:
            embeddings (torch.Tensor): The input embeddings.

        Returns:
            torch.Tensor: The mean pooling.
        """

        # Get the mean pooling
        mean_pooling = embeddings.last_hidden_state.mean(dim=1)

        # Return the mean pooling
        return mean_pooling

    # Get the embeddings
    def get_embeddings(self,
        text: Union[str, List[str]],
        embedding_type: str = 'last_hidden_state',
        batch_size: int = 8,
        num_workers: int = 4,
    ) -> torch.Tensor:
        """
        This function returns the embeddings of the input text.

        Arguments:
            text (Union[str, List[str]]): The input text.
            embedding_type (str, optional): The type of embedding to use. Defaults to 'last_hidden_state'.
            batch_size (int, optional): The batch size to use. Defaults to 8.
            num_workers (int, optional): The number of workers to use. Defaults to 4.

        Returns:
            torch.Tensor: The embeddings.

        Raises:
            ValueError: If the embedding type is invalid.
        """

        # Check if the embedding type is valid
        if embedding_type not in ['last_hidden_state', 'mean_pooling']:
            raise ValueError(f'Invalid embedding type: {embedding_type}. Only "last_hidden_state" and "mean_pooling" are supported.')

        # Tokenize the input text
        encoded_text = self.tokenizer(
            text,
            padding=True,
            truncation=True,
            return_tensors='pt',
        )

        # Move the input text to the device
        encoded_text = encoded_text.to(self.device)

        # encoded_inputs = {k: v.to(self.device) for k, v in encoded_inputs.items()}

        # Get the embeddings
        with torch.no_grad():
            embeddings = self.model(**encoded_text)

        # Get the proper embedding type
        if embedding_type == 'last_hidden_state':
            # Get the last hidden state
            embeddings = self.get_last_hidden_state(embeddings)
        elif embedding_type == 'mean_pooling':
            # Get the mean pooling
            embeddings = self.get_mean_pooling(embeddings)

        # Return the embeddings
        return embeddings

    # Add FAISS index
    def add_faiss_index(self,
        column_name: str = 'embeddings',
        metric_type: Optional[int] = None,
        batch_size: int = 8,
        **kwargs,
    ) -> None:
        """
        This function adds a FAISS index to the dataset.

        Arguments:
            column_name (str, optional): The name of the column containing the embeddings. Defaults to 'embeddings'.
            index_type (str, optional): The index type to use. Defaults to 'Flat'.
            metric_type (str, optional): The metric type to use. Defaults to 'L2'.

        Returns:
            None

        Raises:
            ValueError: If the dataset is not initialized.
        """

        # Check if the dataset is initialized
        if self.dataset is None:
            raise ValueError('The dataset is not initialized. Please initialize the dataset first.')

        print('Adding FAISS index...')
        self.dataset.add_faiss_index(
            column_name,
            # metric_type=metric_type,
            # device=self.device,
            # batch_size=batch_size,
            faiss_verbose=True,
            # **kwargs,
        )

    def save_faiss_index(self,
        index_name: str,
        file_path: str,
    ) -> None:
        """
        This function saves the FAISS index to the specified file path.
            * This is a wrapper function for the `save_faiss_index` function in the `Dataset` class.

        Arguments:
            index_name (str): The name of the FAISS index  (e.g., "embeddings")
            file_path (str): The file path to save the FAISS index.

        Returns:
            None

        Raises:
            ValueError: If the dataset is not initialized.
        """

        # Check if the dataset is initialized
        if self.dataset is None:
            raise ValueError('The dataset is not initialized. Please initialize the dataset first.')

        print('Saving FAISS index...')
        self.dataset.save_faiss_index(index_name=index_name, file=file_path)

    def load_faiss_index(self,
        index_name: str,
        file_path: str,
        device: str = 'cpu',
    ) -> None:
        """
        This function loads the FAISS index from the specified file path.
            * This is a wrapper function for the `load_faiss_index` function in the `Dataset` class.

        Arguments:
            index_name (str): The name of the FAISS index  (e.g., "embeddings")
            file_path (str): The file path to load the FAISS index from.
            device (str, optional): The device to use ("cpu" or "cuda") (default: "cpu").

        Returns:
            None

        Raises:
            ValueError: If the dataset is not initialized.
        """

        # Check if the dataset is initialized
        if self.dataset is None:
            raise ValueError('The dataset is not initialized. Please initialize the dataset first.')

        print('Loading FAISS index...')
        self.dataset.load_faiss_index(index_name=index_name, file=file_path, device=device)

    # Initialize the corpus using a dictionary or pandas DataFrame or HuggingFace Datasets object
    def initialize_corpus(self,
        corpus: Union[Dict[str, List[str]], pd.DataFrame, Dataset],
        section: str = 'text',
        index_column_name: str = 'embeddings',
        embedding_type: str = 'last_hidden_state',
        batch_size: Optional[int] = None,
        num_workers: Optional[int] = None,
        save_path: Optional[str] = None,
    ) -> Dataset:
        """
        This function initializes a dataset using a dictionary or pandas DataFrame or HuggingFace Datasets object.

        Arguments:
            dataset_dict (Dict[str, List[str]]): The dataset dictionary.
            section (str): The section of the dataset to use whose embeddings will be used for semantic search (e.g., 'text', 'title', etc.) (default: 'text').
            index_column_name (str): The name of the column containing the embeddings (default: 'embeddings')
            embedding_type (str): The type of embedding to use (default: 'last_hidden_state').
            batch_size (int, optional): The batch size to use (default: 8).
            max_length (int, optional): The maximum length of the input sequences.
            num_workers (int, optional): The number of workers to use.
            save_path (Optional[str], optional): The path to save the dataset (default: None).

        Returns:
            Dataset: The dataset object (HuggingFace Datasets).

        Raises:
            ValueError: If the dataset is not a dictionary or pandas DataFrame or HuggingFace Datasets object.
        """

        # Create the dataset
        if isinstance(corpus, dict):
            self.dataset = Dataset.from_dict(corpus)
        elif isinstance(corpus, pd.DataFrame):
            self.dataset = Dataset.from_pandas(corpus)
        elif isinstance(corpus, Dataset):
            self.dataset = corpus
        else:
            raise ValueError('The dataset must be a dictionary or pandas DataFrame.')

        # Set the embedding_type
        self.embedding_type = embedding_type

        # Tokenize the dataset
        # self.dataset = self.dataset.map(
        #     lambda x: x[section],
        #     batched=True,
        #     batch_size=batch_size,
        #     num_proc=num_workers,
        # )

        # Map the section of the dataset to the embeddings
        self.dataset = self.dataset.map(
            lambda x: {
                index_column_name: self.get_embeddings(x[section], embedding_type=self.embedding_type).detach().cpu().numpy()[0]
                },
            # batched=True,
            batch_size=batch_size,
            num_proc=num_workers,
        )

        # Save the dataset
        if save_path is not None:
            self.dataset.to_json(save_path)

        # Add FAISS index
        self.add_faiss_index(
            column_name=index_column_name,
        )

        # Return the dataset
        return self.dataset

    # Initialize the dataset using a JSON file
    def load_dataset_from_json(self,
        json_path: str,
    ) -> Dataset:
        """
        This function loads a dataset from a JSON file.

        Arguments:
            json_path (str): The path to the JSON file.

        Returns:
            Dataset: The dataset.
        """

        # Load the dataset
        self.dataset = Dataset.from_json(json_path)

        # Return the dataset
        return self.dataset

    # Search for the most similar elements in the dataset, given a query
    def search(self,
        query: str,
        k: int = 1,
        index_column_name: str = 'embeddings',
    ) -> pd.DataFrame:
        """
        This function searches for the most similar elements in the dataset, given a query.

        Arguments:
            query (str): The query.
            k (int, optional): The number of elements to return  (default: 1).
            index_column_name (str, optional): The name of the column containing the embeddings (default: 'embeddings')

        Returns:
            pd.DataFrame: The most similar elements in the dataset (text, score, etc.), sorted by score.

        Remarks:
            The returned elements are dictionaries containing the text and the score.
        """

        # Get the embeddings of the query
        query_embeddings = self.get_embeddings([query], embedding_type=self.embedding_type).detach().cpu().numpy()

        # Search for the most similar elements in the dataset
        scores, similar_elts = self.dataset.get_nearest_examples(
            index_name=index_column_name,
            query=query_embeddings,
            k=k,
        )

        # Convert the results to a pandas DataFrame
        results_df = pd.DataFrame.from_dict(similar_elts)

        # Add the scores
        results_df['score'] = scores

        # Sort the results by score
        results_df.sort_values("score", ascending=True, inplace=True)

        # Return the most similar elements
        return results_df


from resources.common import RadialOptions
from services.sui.SUIService import MessageBoxType
from services.sui.SUIWindow import Trigger
from java.util import Vector
import sys

def createRadial(core, owner, target, radials):
	return

def handleSelection(core, owner, target, option):

	if option == 21 and target:
		# REALLY Dirty hack until this can be resolved SWG way !!
		stra = "Will you pay the %DI credits to clone yourself here?"
		level = int(owner.getLevel())

		if level <= 10:
			cloningFee = 100
		elif level >= 11:
			cloningFee = (((owner.getLevel() + 17) * owner.getLevel())/2)
		elif level == 90:
			cloningFee = 5000

		suiSvc = core.suiService
		suiWindow = suiSvc.createMessageBox(MessageBoxType.MESSAGE_BOX_OK_CANCEL, '@base_player:clone_confirm_title', stra.replace ("%DI" , str(cloningFee)), owner, target, 15)
		returnParams = Vector()
		returnParams.add('btnOk:Text')
		returnParams.add('btnCancel:Text')
		suiWindow.addHandler(0, '', Trigger.TRIGGER_OK, returnParams, handleSUI)
		suiWindow.addHandler(1, '', Trigger.TRIGGER_CANCEL, returnParams, handleSUI)
		suiSvc.openSUIWindow(suiWindow)

	return

def handleSUI(owner, window, eventType, returnList):

	if eventType == 0:
		level = int(owner.getLevel())
		if level <= 10:
			cloningFee = 100
		elif level >= 11:
			cloningFee = (((owner.getLevel() + 17) * owner.getLevel())/2)
		elif level == 90:
			cloningFee = 5000

		cash = owner.getCashCredits()
		bank = owner.getBankCredits()

		if bank < cloningFee and cash < cloningFee:
			owner.sendSystemMessage('You lack the credits required to cover the cost of cloning.', 0)
			return
		elif bank >= cloningFee:
			owner.setBankCredits(bank - cloningFee)
		elif cash >= cloningFee and bank < cloningFee:
			owner.setCashCredits(cash - cloningFee)

		owner.getPlayerObject().setBindLocation(window.getRangeObject().getGrandparent().getObjectID())
		owner.sendSystemMessage('@base_player:clone_success', 0)

	return


import sys

cookbook = {
        'sandwich': {'ingredients': ['ham', 'bread', 'cheese', 'tomatoes'],
                     'meal': 'lunch', 'time': '10 minutes'},
        'cake': {'ingredients': ['flour', 'sugar', 'eggs'],
                 'meal': 'dessert', 'time': '60 minutes'},
        'salad': {'ingredients': ['avocado', 'arugula', 'tomatoes', 'spinach'],
                  'meal': 'lunch', 'time': '15 minutes'}
}

def print_cookbook(recipe):
    for name, recipe_info in cookbook.items():
        if name == recipe:
            print("")
            print("Recipe for " + name + ":")
            print("Ingredients list:", recipe_info["ingredients"])
            print("To be eaten for " + recipe_info["meal"] + ".")
            print("Takes " + recipe_info["time"] + " of cooking.\n")
            return
    print("This recipe does not exist, please type an existing recipe or add" +
          "a new one.")
    print("To exit, enter 5\n")

def delete_from_cookbook(recipe):
    for name, recipe_info in cookbook.items():
        if name == recipe:
            del cookbook[recipe]
            print("")
            return
    print("This recipe does not exist, please type an existing recipe or add" +
          "a new one.")
    print("To exit, enter 5\n")

def add_in_cookbook(recipe, ingredients, meal, time):
    cookbook[recipe] = {}

    formatted_ingredients = ingredients.split(', ')
    cookbook[recipe]['ingredients'] = formatted_ingredients
    cookbook[recipe]['meal'] = meal
    cookbook[recipe]['time'] = time
    print("")

def print_all_cookbook():
    for name, recipe_info in cookbook.items():
        print_cookbook(name)

if len(sys.argv) == 1:
    while 1:
        print("Please select an option by typing the corresponding number:")
        print("1: Add a recipe")
        print("2: Delete a recipe")
        print("3: Print a recipe")
        print("4: Print the cookbook")
        print("5: Quit")
        try:
            value = int(input())
            if value == 1:
                recipe = input("\nPlease enter the recipe's name you want to"
                               "add: \n")
                ingredients = input("Please enter its ingredients(Usage: "
                                    "ingredient1, ingredient2, ...)\n")
                meal = input("Please enter its meal type\n")
                time = input("Please enter its preparation time in minutes"
                             "(Example: 10 minutes)\n")
                add_in_cookbook(recipe, ingredients, meal, time)
            elif value == 2:
                recipe = input("\nPlease enter the recipe's name you want to"
                               "delete: \n")
                delete_from_cookbook(recipe)
            elif value == 3:
                recipe = input("\nPlease enter the recipe's name to get its"
                               "details: \n")
                print_cookbook(recipe)
            elif value == 4:
                print_all_cookbook()
            elif value == 5:
                break
            else:
                print("\nThis option does not exist, please type the " +
                      "corresponding number.")
                print("To exit, enter 5.\n")
        except ValueError:
            print("\nThis option does not exist, please type the " +
                  "corresponding number.")
            print("To exit, enter 5.\n")
else:
    print("Wrong usage please use python3 recipe.py")


# 1
names = ["Alex", "Nserweko", "Caroline", "Dave", "Eleanor"]
print(names[1])

# 2
names[1] = "julis"

# 3
names.append("Adam")
# 4
names.insert(2, "Bathel")
# 5
names.pop(3)
# 6
print(names[-1])
# 7
years = [2000, 2001, 2002, 2003, 2004, 2005, 2006]
print(years[2:5])
# 8
east_africa = ["uganda", "kenya", "Rwanda", "burundi"]
east_african_comunity = east_africa.copy()
print(east_african_comunity)
# 9
for item in east_african_comunity:
    print(item)

# 10
animals = ["goat", "dog", "cat", "cow", "sheep"]
# sort in descending order
animals.sort(reverse=True)
print(animals)

# sort in ascending order
animals.sort()
print(animals)

# 11
for animal in animals:
    if "a" in animal:
        print(animal)


from fastai import *
from fastai.text import *
import pandas as pd
#--------------------------------------------------------------------

PATH=Path(os.getcwd())#/home/fazl/Desktop/fake_gen/code
csv_path = PATH/'csv'
mdl_path = csv_path/'models'

#---------------------------------------------------------------------
en_df=pd.read_csv(csv_path/'en.csv')
es_df=pd.read_csv(csv_path/'es.csv')

new_en_df=pd.concat([en_df['label'],en_df['tweets']],axis=1)
new_en_df.to_csv(csv_path/'new_en.csv',index=False)

new_es_df=pd.concat([es_df['label'],es_df['tweets']],axis=1)
new_es_df.to_csv(csv_path/'new_es.csv',index=False, header=False)

#-------------------------------------------------
print('loading Data language model---en')
#English
tokenizer = Tokenizer(lang='en', n_cpus=6)
en_data_lm = (TextList.from_csv(csv_path, csv_name='enfulltrain.csv', cols=0, processor=[TokenizeProcessor(tokenizer=tokenizer), NumericalizeProcessor(max_vocab=60000)])
           #Inputs: all the text files in path
            .split_from_df(col=1)
           #We may have other temp folders that contain text files so we only keep what's in train and test
            .label_for_lm()
           #We want to do a language model so we label accordingly
            .databunch(bs=32))
print('loading Data language model---es')
#Spanish
tokenizer = Tokenizer(lang='es', n_cpus=6)
es_data_lm = (TextList.from_csv(csv_path, csv_name='esfulltrain.csv', cols=0, processor=[TokenizeProcessor(tokenizer=tokenizer), NumericalizeProcessor(max_vocab=60000)])
           #Inputs: all the text files in path
            .split_from_df(col=1)
           #We may have other temp folders that contain text files so we only keep what's in train and test
            .label_for_lm()
           #We want to do a language model so we label accordingly
            .databunch(bs=32))

#-------------------------------------------
print('loading Data class model---en')
en_data_clas = TextClasDataBunch.from_csv(csv_path, vocab=en_data_lm.train_ds.vocab, bs=64, csv_name='new_en.csv', min_freq=1)
print('loading Data class model---es')
es_data_clas = TextClasDataBunch.from_csv(csv_path, vocab=es_data_lm.train_ds.vocab, bs=64, csv_name='new_es.csv', min_freq=1)

print('loading fine tuned encoder---en')
en_learn = text_classifier_learner(en_data_clas, AWD_LSTM, drop_mult=0.3,pretrained = False)
en_learn.load_encoder(mdl_path/'en_ft_enc')
en_learn.freeze()
print('loading fine tuned encoder---es')
es_learn = text_classifier_learner(es_data_clas, AWD_LSTM, drop_mult=0.3,pretrained = False)
es_learn.load_encoder(mdl_path/'es_ft_enc')
es_learn.freeze()
#-----------------------------------------------------
print('learning classifier ...')

en_learn.lr_find(start_lr=1e-8,end_lr=1e2)
#en_learn.recorder.plot()
for i in range(3):
	en_learn.freeze_to(-2)
	en_learn.fit_one_cycle(1, slice(1e-4,1e-2), moms=(0.8,0.7))

	en_learn.freeze_to(-3)
	en_learn.fit_one_cycle(1, slice(1e-5,5e-3), moms=(0.8,0.7))
	en_learn.unfreeze()
	en_learn.fit_one_cycle(4, slice(1e-5,1e-3), moms=(0.8,0.7))

	en_learn.fit_one_cycle(2, slice(1e-3/(2.6**4),1e-3), moms=(0.8,0.7))
#-------------------------------------------------------------------------
es_learn.lr_find()
#es_learn.recorder.plot(skip_start=0)
for i in range(3):
	es_learn.freeze_to(-2)
	es_learn.fit_one_cycle(1, slice(1e-4,1e-2), moms=(0.8,0.7))

	es_learn.freeze_to(-3)
	es_learn.fit_one_cycle(1, slice(1e-5,5e-3), moms=(0.8,0.7))
	es_learn.unfreeze()
	es_learn.fit_one_cycle(4, slice(1e-5,1e-3), moms=(0.8,0.7))

	es_learn.fit_one_cycle(2, slice(1e-3/(2.6**4),1e-3), moms=(0.8,0.7))

#-----------------------------------------------------------------------
out_path=PATH/'output'
out_path.mkdir(exist_ok=True)

en_path=out_path/'en'
en_path.mkdir(exist_ok=True)

es_path=out_path/'es'
es_path.mkdir(exist_ok=True)
#ENGLISH
print('prediction---en')

test=en_df
test = test.drop(['label'], axis=1)

test['label'] = test['tweets'].apply(lambda row: str(en_learn.predict(row)[0]))

print(test['label'].value_counts())

test.to_csv(PATH/'enpredicted.txt', sep='\t', index=True, header=['author','tweets','label'], index_label='ID')
engold=en_df
engold.to_csv(PATH/'engold.txt', sep='\t', index=True, header=['author','tweets','label'], index_label='ID')
#------

for t in range(len(test)):

  id_=test['author'][t]
  lang='en'
  if test['label'][t]=='1':
    type_='fake'
  else:
    type_='genuine'
  res='<author id='+id_+'  lang='+lang+' type='+type_+' />'
  name=id_+".xml"
  with open(en_path/name, "w") as f:
    f.write(res)
#----

#Spanish
print('prediction---es')
test=es_df
test = test.drop(['label'], axis=1)
test['label'] = test['tweets'].apply(lambda row: str(es_learn.predict(row)[0]))
print(test['label'].value_counts())
test.to_csv(PATH/'espredicted.txt', sep='\t', index=True, header=['author','tweets','label'], index_label='ID')
esgold=en_df
esgold.to_csv(PATH/'esgold.txt', sep='\t', index=True, header=['author','tweets','label'], index_label='ID')
#--------------------------------------------------------------------------------------------------------------------

#------

for t in range(len(test)):

  id_=test['author'][t]
  lang='es'
  if test['label'][t]=='1':
    type_='fake'
  else:
    type_='genuine'
  res='<author id='+id_+'  lang='+lang+' type='+type_+' />'
  name=id_+".xml"
  with open(es_path/name, "w") as f:
    f.write(res)
#----


class Solution:
    def canBeEqual(self, target: List[int], arr: List[int]) -> bool:
        d = Counter(target)
        for num in arr:
            if num in d:
                d[num] -= 1
        for v in d.values():
            if v != 0:
                return False
        return True


import subprocess
from flask import Flask, request

from model_provider import ModelProvider

app = Flask(__name__)

model_provider = ModelProvider()

@app.route('/')
def hello():
    command = "`echo hostname`"
    hostname = subprocess.check_output(['bash', '-c', command])
    return f'hello from docker container {hostname}'

@app.route('/api/model', methods=['POST'])
def set_model():
    if not request.data:
        return 'Empty content', 400
    if model_provider.save_model(str(request.data, 'utf-8')):
        return 'Model was received', 200
    return 'Error while submitting model', 500

@app.route('/api/prediction', methods=['POST'])
def prediction():
    log_dic = request.get_json()
    data = log_dic.get('log', None)
    if not data:
        return 'Invalid parameter', 400
    res = model_provider.get_prediction(data)
    if not res:
        return 'Prediction was failed, try submit a valid model', 500
    return {'result': res}

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


import logging
import os
import sys

import yaml

from nlg_analysis.cfg import TrainConfig
from nlg_analysis.models import BERTModel, GPTModel, RNNModel
from nlg_analysis.utils import parse_arguments

logger = logging.getLogger()
logging.basicConfig(
    format="%(asctime)s : %(levelname)s : %(message)s",
    level=os.environ.get("LOGLEVEL", "INFO"),
)

def main():
    args = parse_arguments(sys.argv[1:])

    # Load config file
    logger.info("Loading config...")
    with open(args.config_file) as f:
        config = yaml.safe_load(f)
    train_cfg = TrainConfig.from_dict(config)

    # Setup models
    models = []
    if train_cfg.gpt_model:
        models.append(
            {
                "model": GPTModel(model_name=train_cfg.gpt_model_name),
                "output_path": train_cfg.gpt_output_path,
            }
        )
    if train_cfg.rnn_model:
        models.append(
            {"model": RNNModel(), "output_path": train_cfg.rnn_output_path}
        )
    if train_cfg.bert_model:
        models.append(
            {
                "model": BERTModel(model_name=train_cfg.bert_model_name),
                "output_path": train_cfg.bert_output_path,
            }
        )
    logger.info(
        "Train models: {0}".format(
            ", ".join([model["model"].approach() for model in models])
        )
    )

    # Train models
    for model in models:
        logger.info("Train model {0}".format(model["model"].approach()))
        model["model"].train(model["output_path"], train_cfg)

if __name__ == "__main__":
    main()


import os
from django.conf import settings

ALLOWED_HOSTS = ['b802-01.fsv.cvut.cz']

# Application definition

INSTALLED_APPS = settings.INSTALLED_APPS + ['users.apps.UsersConfig', 'django_python3_ldap', ]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(settings.BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# define global static
STATICFILES_DIRS = (
    os.path.join(settings.BASE_DIR, 'static'),
)

LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'home'

### CUSTOM USER MODEL ###
AUTH_USER_MODEL = 'users.CustomUser'

### LDAP AUTHENTICATION ###
AUTHENTICATION_BACKENDS = (
    'django_python3_ldap.auth.LDAPBackend',
#    'django.contrib.auth.backends.ModelBackend',
)

# The URL of the LDAP server.
LDAP_AUTH_URL = "ldap://127.0.0.1:389"

# Initiate TLS on connection
LDAP_AUTH_USE_TLS = True

# The LDAP search base for looking up users.
LDAP_AUTH_SEARCH_BASE = "ou=people,dc=gis,dc=lab"

# User model fields mapped to the LDAP attributes that represent them.
LDAP_AUTH_USER_FIELDS = {
    "username": "uid",
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail",
    "description": "description",
}

# A tuple of django model fields used to uniquely identify a user.
LDAP_AUTH_USER_LOOKUP_FIELDS = ("username",)

LDAP_AUTH_SYNC_USER_RELATIONS =   "web_console_project.ldap_auth.custom_sync_user_relations"

### LOGGING ###
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
            'propagate': True
        }
    },
    "loggers": {
        "django_python3_ldap": {
            "handlers": ["console"],
            "level": "INFO",
        },
    }
}