"""This module provides various helper functions."""
import logging
import sys
import os
import warnings
from functools import wraps
import urllib

import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
from tqdm import tqdm

import astropy
from astropy.utils.data import download_file
from astropy.units.quantity import Quantity
import astropy.units as u
from astropy.visualization import (
    PercentileInterval,
    ImageNormalize,
    SqrtStretch,
    LinearStretch,
)
from astropy.time import Time

log = logging.getLogger(__name__)

__all__ = [
    "LightkurveError",
    "LightkurveWarning",
    "KeplerQualityFlags",
    "TessQualityFlags",
    "bkjd_to_astropy_time",
    "btjd_to_astropy_time",
    "show_citation_instructions",
    "finalize_notebook_url",
    "remote_jupyter_proxy_url"
]

class QualityFlags(object):
    """Abstract class"""

    STRINGS = {}
    OPTIONS = {}

    @classmethod
    def decode(cls, quality):
        """Converts a QUALITY value into a list of human-readable strings.

        This function takes the QUALITY bitstring that can be found for each
        cadence in Kepler/K2/TESS' pixel and light curve files and converts into
        a list of human-readable strings explaining the flags raised (if any).

        Parameters
        ----------
        quality : int
            Value from the 'QUALITY' column of a Kepler/K2/TESS pixel or lightcurve file.

        Returns
        -------
        flags : list of str
            List of human-readable strings giving a short description of the
            quality flags raised.  Returns an empty list if no flags raised.
        """
        # If passed an astropy quantity object, get the value
        if isinstance(quality, Quantity):
            quality = quality.value
        result = []
        for flag in cls.STRINGS.keys():
            if quality & flag > 0:
                result.append(cls.STRINGS[flag])
        return result

    @classmethod
    def create_quality_mask(cls, quality_array, bitmask=None):
        """Returns a boolean array which flags good cadences given a bitmask.

        This method is used by the readers of :class:`KeplerTargetPixelFile`
        and :class:`KeplerLightCurve` to initialize their `quality_mask`
        class attribute which is used to ignore bad-quality data.

        Parameters
        ----------
        quality_array : array of int
            'QUALITY' column of a Kepler target pixel or lightcurve file.
        bitmask : int or str
            Bitmask (int) or one of 'none', 'default', 'hard', or 'hardest'.

        Returns
        -------
        boolean_mask : array of bool
            Boolean array in which `True` means the data is of good quality.
        """
        # Return an array filled with `True` by default (i.e. ignore nothing)
        if bitmask is None:
            return np.ones(len(quality_array), dtype=bool)
        if isinstance(quality_array, u.Quantity):
            quality_array = quality_array.value
        # A few pre-defined bitmasks can be specified as strings
        if isinstance(bitmask, str):
            try:
                bitmask = cls.OPTIONS[bitmask]
            except KeyError:
                valid_options = tuple(cls.OPTIONS.keys())
                raise ValueError(
                    "quality_bitmask='{}' is not supported, "
                    "expected one of {}"
                    "".format(bitmask, valid_options)
                )
        # The bitmask is applied using the bitwise AND operator
        quality_mask = (quality_array & bitmask) == 0
        # Log the quality masking as info or warning
        n_cadences = len(quality_array)
        n_cadences_masked = (~quality_mask).sum()
        percent_masked = 100.0 * n_cadences_masked / n_cadences
        logmsg = (
            "{:.0f}% ({}/{}) of the cadences will be ignored due to the "
            "quality mask (quality_bitmask={})."
            "".format(percent_masked, n_cadences_masked, n_cadences, bitmask)
        )
        if percent_masked > 20:
            log.warning("Warning: " + logmsg)
        else:
            log.info(logmsg)
        return quality_mask

class KeplerQualityFlags(QualityFlags):
    """
    This class encodes the meaning of the various Kepler QUALITY bitmask flags,
    as documented in the Kepler Archive Manual (Ref. [1], Table 2.3).

    References
    ----------
    .. [1] Kepler: A Search for Terrestrial Planets. Kepler Archive Manual.
        http://archive.stsci.edu/kepler/manuals/archive_manual.pdf
    """

    AttitudeTweak = 1
    SafeMode = 2
    CoarsePoint = 4
    EarthPoint = 8
    ZeroCrossing = 16
    Desat = 32
    Argabrightening = 64
    ApertureCosmic = 128
    ManualExclude = 256
    # Bit 2**10 = 512 is unused by Kepler
    SensitivityDropout = 1024
    ImpulsiveOutlier = 2048
    ArgabrighteningOnCCD = 4096
    CollateralCosmic = 8192
    DetectorAnomaly = 16384
    NoFinePoint = 32768
    NoData = 65536
    RollingBandInAperture = 131072
    RollingBandInMask = 262144
    PossibleThrusterFiring = 524288
    ThrusterFiring = 1048576

    #: DEFAULT bitmask identifies all cadences which are definitely useless.
    DEFAULT_BITMASK = (
        AttitudeTweak
        | SafeMode
        | CoarsePoint
        | EarthPoint
        | Desat
        | ManualExclude
        | DetectorAnomaly
        | NoData
        | ThrusterFiring
    )
    #: HARD bitmask is conservative and may identify cadences which are useful.
    HARD_BITMASK = (
        DEFAULT_BITMASK
        | SensitivityDropout
        | ApertureCosmic
        | CollateralCosmic
        | PossibleThrusterFiring
    )
    #: HARDEST bitmask identifies cadences with any flag set. Its use is not recommended.
    HARDEST_BITMASK = 2096639

    #: Dictionary which provides friendly names for the various bitmasks.
    OPTIONS = {
        "none": 0,
        "default": DEFAULT_BITMASK,
        "hard": HARD_BITMASK,
        "hardest": HARDEST_BITMASK,
    }

    #: Pretty string descriptions for each flag
    STRINGS = {
        1: "Attitude tweak",
        2: "Safe mode",
        4: "Coarse point",
        8: "Earth point",
        16: "Zero crossing",
        32: "Desaturation event",
        64: "Argabrightening",
        128: "Cosmic ray in optimal aperture",
        256: "Manual exclude",
        1024: "Sudden sensitivity dropout",
        2048: "Impulsive outlier",
        4096: "Argabrightening on CCD",
        8192: "Cosmic ray in collateral data",
        16384: "Detector anomaly",
        32768: "No fine point",
        65536: "No data",
        131072: "Rolling band in optimal aperture",
        262144: "Rolling band in full mask",
        524288: "Possible thruster firing",
        1048576: "Thruster firing",
    }

class TessQualityFlags(QualityFlags):
    """
    This class encodes the meaning of the various TESS QUALITY bitmask flags,
    as documented in the TESS Data Products Description Document (Ref. [1], Table 28).

    References
    ----------
    .. [1] TESS Science Data Products Description Document (EXP-TESS-ARC-ICD-0014)
        https://archive.stsci.edu/missions/tess/doc/EXP-TESS-ARC-ICD-TM-0014.pdf
    """

    AttitudeTweak = 1
    SafeMode = 2
    CoarsePoint = 4
    EarthPoint = 8
    Argabrightening = 16
    Desat = 32
    ApertureCosmic = 64
    ManualExclude = 128
    Discontinuity = 256
    ImpulsiveOutlier = 512
    CollateralCosmic = 1024
    #: The first stray light flag is set manually by MIT based on visual inspection.
    Straylight = 2048
    #: The second stray light flag is set automatically by Ames/SPOC based on background level thresholds.
    Straylight2 = 4096
    # See TESS Science Data Products Description Document
    PlanetSearchExclude = 8192
    BadCalibrationExclude = 16384
    # Set in the sector 20 data release notes
    InsufficientTargets = 32768

    #: DEFAULT bitmask identifies all cadences which are definitely useless.
    DEFAULT_BITMASK = (
        AttitudeTweak | SafeMode | CoarsePoint | EarthPoint | Desat | ManualExclude
    )
    #: HARD bitmask is conservative and may identify cadences which are useful.
    HARD_BITMASK = (
        DEFAULT_BITMASK | ApertureCosmic | CollateralCosmic | Straylight | Straylight2
    )
    #: HARDEST bitmask identifies cadences with any flag set. Its use is not recommended.
    HARDEST_BITMASK = 65535

    #: Dictionary which provides friendly names for the various bitmasks.
    OPTIONS = {
        "none": 0,
        "default": DEFAULT_BITMASK,
        "hard": HARD_BITMASK,
        "hardest": HARDEST_BITMASK,
    }

    #: Pretty string descriptions for each flag
    STRINGS = {
        1: "Attitude tweak",
        2: "Safe mode",
        4: "Coarse point",
        8: "Earth point",
        16: "Argabrightening",
        32: "Desaturation event",
        64: "Cosmic ray in optimal aperture",
        128: "Manual exclude",
        256: "Discontinuity corrected",
        512: "Impulsive outlier",
        1024: "Cosmic ray in collateral data",
        2048: "Straylight",
        4096: "Straylight2",
        8192: "Planet Search Exclude",
        16384: "Bad Calibration Exclude",
        32768: "Insufficient Targets for Error Correction Exclude",
    }

def channel_to_module_output(channel):
    """Returns a (module, output) pair given a CCD channel number.

    Parameters
    ----------
    channel : int
        Channel number

    Returns
    -------
    module, output : tuple of ints
        Module and Output number
    """
    if channel < 1 or channel > 88:
        raise ValueError("Channel number must be in the range 1-88.")
    lookup = _get_channel_lookup_array()
    lookup[:, 0] = 0
    modout = np.where(lookup == channel)
    return modout[0][0], modout[1][0]

def module_output_to_channel(module, output):
    """Returns the CCD channel number for a given module and output pair.

    Parameters
    ----------
    module : int
        Module number
    output : int
        Output number

    Returns
    -------
    channel : int
        Channel number
    """
    if module < 1 or module > 26:
        raise ValueError("Module number must be in range 1-26.")
    if output < 1 or output > 4:
        raise ValueError("Output number must be 1, 2, 3, or 4.")
    return _get_channel_lookup_array()[module, output]

def _get_channel_lookup_array():
    """Returns a lookup table which maps (module, output) onto channel."""
    # In the array below, channel == array[module][output]
    # Note: modules 1, 5, 21, 25 are the FGS guide star CCDs.
    return np.array(
        [
            [0, 0, 0, 0, 0],
            [1, 85, 0, 0, 0],
            [2, 1, 2, 3, 4],
            [3, 5, 6, 7, 8],
            [4, 9, 10, 11, 12],
            [5, 86, 0, 0, 0],
            [6, 13, 14, 15, 16],
            [7, 17, 18, 19, 20],
            [8, 21, 22, 23, 24],
            [9, 25, 26, 27, 28],
            [10, 29, 30, 31, 32],
            [11, 33, 34, 35, 36],
            [12, 37, 38, 39, 40],
            [13, 41, 42, 43, 44],
            [14, 45, 46, 47, 48],
            [15, 49, 50, 51, 52],
            [16, 53, 54, 55, 56],
            [17, 57, 58, 59, 60],
            [18, 61, 62, 63, 64],
            [19, 65, 66, 67, 68],
            [20, 69, 70, 71, 72],
            [21, 87, 0, 0, 0],
            [22, 73, 74, 75, 76],
            [23, 77, 78, 79, 80],
            [24, 81, 82, 83, 84],
            [25, 88, 0, 0, 0],
        ]
    )

def running_mean(data, window_size):
    """Returns the moving average of an array `data`.

    Parameters
    ----------
    data : array of numbers
        The running mean will be computed on this data.
    window_size : int
        Window length used to compute the running mean.
    """
    if window_size > len(data):
        window_size = len(data)
    cumsum = np.cumsum(np.insert(data, 0, 0))
    return (cumsum[window_size:] - cumsum[:-window_size]) / float(window_size)

def bkjd_to_astropy_time(bkjd) -> Time:
    """Converts Kepler Barycentric Julian Day (BKJD) time values to an
    `astropy.time.Time` object.

    Kepler Barycentric Julian Day (BKJD) is a Julian day minus 2454833.0
    (UTC=January 1, 2009 12:00:00) and corrected to the arrival times
    at the barycenter of the Solar System.
    BKJD is the format in which times are recorded in the Kepler data products.
    The time is in the Barycentric Dynamical Time frame (TDB), which is a
    time system that is not affected by leap seconds.
    See Section 2.3.2 in the Kepler Archive Manual for details.

    Parameters
    ----------
    bkjd : float or array of floats
        Barycentric Kepler Julian Day.

    Returns
    -------
    time : `astropy.time.Time` object
        Resulting time object.
    """
    bkjd = np.atleast_1d(bkjd)
    # Some data products have missing time values;
    # we need to set these to zero or `Time` cannot be instantiated.
    bkjd[~np.isfinite(bkjd)] = 0
    return Time(bkjd, format="bkjd", scale="tdb")

def btjd_to_astropy_time(btjd) -> Time:
    """Converts TESS Barycentric Julian Day (BTJD) values to an
    `astropy.time.Time` object.

    TESS Barycentric Julian Day (BTJD) is a Julian day minus 2457000.0
    and corrected to the arrival times at the barycenter of the Solar System.
    BTJD is the format in which times are recorded in the TESS data products.
    The time is in the Barycentric Dynamical Time frame (TDB), which is a
    time system that is not affected by leap seconds.

    Parameters
    ----------
    btjd : float or array of floats
        Barycentric TESS Julian Day

    Returns
    -------
    time : `astropy.time.Time` object
        Resulting time object.
    """
    btjd = np.atleast_1d(btjd)
    btjd[~np.isfinite(btjd)] = 0
    return Time(btjd, format="btjd", scale="tdb")

def plot_image(
    image,
    ax=None,
    scale="linear",
    origin="lower",
    xlabel="Pixel Column Number",
    ylabel="Pixel Row Number",
    clabel="Flux ($e^{-}s^{-1}$)",
    title=None,
    show_colorbar=True,
    vmin=None,
    vmax=None,
    **kwargs
):
    """Utility function to plot a 2D image

    Parameters
    ----------
    image : 2d array
        Image data.
    ax : `~matplotlib.axes.Axes`
        A matplotlib axes object to plot into. If no axes is provided,
        a new one will be generated.
    scale : str
        Scale used to stretch the colormap.
        Options: 'linear', 'sqrt', or 'log'.
    origin : str
        The origin of the coordinate system.
    xlabel : str
        Label for the x-axis.
    ylabel : str
        Label for the y-axis.
    clabel : str
        Label for the color bar.
    title : str or None
        Title for the plot.
    show_colorbar : bool
        Whether or not to show the colorbar
    vmin : float
        Minimum colorbar value. By default, the 2.5%-percentile is used.
    vmax : float
        Maximum colorbar value. By default, the 97.5%-percentile is used.
    kwargs : dict
        Keyword arguments to be passed to `matplotlib.pyplot.imshow`.

    Returns
    -------
    ax : `~matplotlib.axes.Axes`
        The matplotlib axes object.
    """
    if isinstance(image, u.Quantity):
        image = image.value
    if ax is None:
        _, ax = plt.subplots()

    if vmin is None or vmax is None:
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", RuntimeWarning)  # ignore image NaN values
            mask = np.nan_to_num(image) > 0
            if mask.any() > 0:
                vmin_default, vmax_default = PercentileInterval(95.0).get_limits(
                    image[mask]
                )
            else:
                vmin_default, vmax_default = 0, 0
            if vmin is None:
                vmin = vmin_default
            if vmax is None:
                vmax = vmax_default

    norm = None
    if scale is not None:
        if scale == "linear":
            norm = ImageNormalize(
                vmin=vmin, vmax=vmax, stretch=LinearStretch(), clip=False
            )
        elif scale == "sqrt":
            norm = ImageNormalize(
                vmin=vmin, vmax=vmax, stretch=SqrtStretch(), clip=False
            )
        elif scale == "log":
            # To use log scale we need to guarantee that vmin > 0, so that
            # we avoid division by zero and/or negative values.
            norm = LogNorm(vmin=max(vmin, sys.float_info.epsilon), vmax=vmax, clip=True)
        else:
            raise ValueError("scale {} is not available.".format(scale))
    cax = ax.imshow(image, origin=origin, norm=norm, **kwargs)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_title(title)
    if show_colorbar:
        cbar = plt.colorbar(cax, ax=ax, label=clabel)
        cbar.ax.yaxis.set_tick_params(tick1On=False, tick2On=False)
        cbar.ax.minorticks_off()
    return ax

class LightkurveError(Exception):
    """Class for Lightkurve exceptions."""

    pass

class LightkurveWarning(Warning):
    """Class for all Lightkurve warnings."""

    pass

class LightkurveDeprecationWarning(LightkurveWarning):
    """Class for all Lightkurve deprecation warnings."""

    pass

def suppress_stdout(f, *args, **kwargs):
    """A simple decorator to suppress function print outputs."""

    @wraps(f)
    def wrapper(*args, **kwargs):
        # redirect output to `null`
        with open(os.devnull, "w") as devnull:
            old_out = sys.stdout
            sys.stdout = devnull
            try:
                return f(*args, **kwargs)
            # restore to default
            finally:
                sys.stdout = old_out

    return wrapper

def validate_method(method, supported_methods):
    """Raises a `ValueError` if a method is not supported.

    Parameters
    ----------
    method : str
        The method specified by the user.
    supported_methods : list of str
        The methods supported.  All method names must be lowercase.

    Returns
    -------
    method : str
        Will return the method name if it is supported.
    """
    method = method.lower()
    if method in supported_methods:
        return method
    raise ValueError(
        "method '{}' is not supported; "
        "must be one of {}".format(method, supported_methods)
    )

def centroid_quadratic(data, mask=None):
    """Computes the quadratic estimate of the centroid in a 2d-array.

    This method will fit a simple 2D second-order polynomial
    $P(x, y) = a + bx + cy + dx^2 + exy + fy^2$
    to the 3x3 patch of pixels centered on the brightest pixel within
    the image.  This function approximates the core of the Point
    Spread Function (PSF) using a bivariate quadratic function, and returns
    the maximum (x, y) coordinate of the function using linear algebra.

    For the motivation and the details around this technique, please refer
    to Vakili, M., & Hogg, D. W. 2016, ArXiv, 1610.05873.

    Caveat: if the brightest pixel falls on the edge of the data array, the fit
    will tend to fail or be inaccurate.

    Parameters
    ----------
    data : 2D array
        The 2D input array representing the pixel values of the image.
    mask : array_like (bool), optional
        A boolean mask, with the same shape as `data`, where a **True** value
        indicates the corresponding element of data is masked.

    Returns
    -------
    column, row : tuple
        The coordinates of the centroid in column and row.  If the fit failed,
        then (NaN, NaN) will be returned.
    """
    if isinstance(data, u.Quantity):
        data = data.value
    # Step 1: identify the patch of 3x3 pixels (z_)
    # that is centered on the brightest pixel (xx, yy)
    if mask is not None:
        data = data * mask
    arg_data_max = np.nanargmax(data)
    yy, xx = np.unravel_index(arg_data_max, data.shape)
    # Make sure the 3x3 patch does not leave the TPF bounds
    if yy < 1:
        yy = 1
    if xx < 1:
        xx = 1
    if yy > (data.shape[0] - 2):
        yy = data.shape[0] - 2
    if xx > (data.shape[1] - 2):
        xx = data.shape[1] - 2

    z_ = data[yy - 1 : yy + 2, xx - 1 : xx + 2]

    # Next, we will fit the coefficients of the bivariate quadratic with the
    # help of a design matrix (A) as defined by Eqn 20 in Vakili & Hogg
    # (arxiv:1610.05873). The design matrix contains a
    # column of ones followed by pixel coordinates: x, y, x**2, xy, y**2.
    A = np.array(
        [
            [1, -1, -1, 1, 1, 1],
            [1, 0, -1, 0, 0, 1],
            [1, 1, -1, 1, -1, 1],
            [1, -1, 0, 1, 0, 0],
            [1, 0, 0, 0, 0, 0],
            [1, 1, 0, 1, 0, 0],
            [1, -1, 1, 1, -1, 1],
            [1, 0, 1, 0, 0, 1],
            [1, 1, 1, 1, 1, 1],
        ]
    )
    # We also pre-compute $(A^t A)^-1 A^t$, cf. Eqn 21 in Vakili & Hogg.
    At = A.transpose()
    # In Python 3 this can become `Aprime = np.linalg.inv(At @ A) @ At`
    Aprime = np.matmul(np.linalg.inv(np.matmul(At, A)), At)

    # Step 2: fit the polynomial $P = a + bx + cy + dx^2 + exy + fy^2$
    # following Equation 21 in Vakili & Hogg.
    # In Python 3 this can become `Aprime @ z_.flatten()`
    a, b, c, d, e, f = np.matmul(Aprime, z_.flatten())

    # Step 3: analytically find the function maximum,
    # following https://en.wikipedia.org/wiki/Quadratic_function
    det = 4 * d * f - e ** 2
    if abs(det) < 1e-6:
        return np.nan, np.nan  # No solution
    xm = -(2 * f * b - c * e) / det
    ym = -(2 * d * c - b * e) / det
    return xx + xm, yy + ym

def _query_solar_system_objects(
    ra, dec, times, radius=0.1, location="kepler", cache=True, show_progress=True
):
    """Returns a list of asteroids/comets given a position and time.

    This function relies on The Virtual Observatory Sky Body Tracker (SkyBot)
    service which can be found at http://vo.imcce.fr/webservices/skybot/

    Parameters
    ----------
    ra : float
        Right Ascension in degrees.
    dec : float
        Declination in degrees.
    times : array of float
        Times in Julian Date.
    radius : float
        Search radius in degrees.
    location : str
        Spacecraft location. Options include `'kepler'` and `'tess'`.
    cache : bool
        Whether to cache the search result. Default is True.
    show_progress : bool
        Whether to display a progress bar during the download. Default is True.

    Returns
    -------
    result : `pandas.DataFrame`
        DataFrame containing the list of known solar system objects at the
        requested time and location.
    """
    # We import pandas locally, because it takes quite a bit of time to import,
    # and it is only required for this specific feature.
    import pandas as pd

    if (location.lower() == "kepler") or (location.lower() == "k2"):
        location = "C55"
    elif location.lower() == "tess":
        location = "C57"

    url = "http://vo.imcce.fr/webservices/skybot/skybotconesearch_query.php?"
    url += "-mime=text&"
    url += "-ra={}&".format(ra)
    url += "-dec={}&".format(dec)
    url += "-bd={}&".format(radius)
    url += "-loc={}&".format(location)

    df = None
    times = np.atleast_1d(times)
    for time in tqdm(times, desc="Querying for SSOs", disable=~show_progress):
        url_queried = url + "EPOCH={}".format(time)
        response = download_file(url_queried, cache=cache, show_progress=show_progress)
        if open(response).read(10) == "# Flag: -1":  # error code detected?
            raise IOError(
                "SkyBot Solar System query failed.\n"
                "URL used:\n" + url_queried + "\n"
                "Response received:\n" + open(response).read()
            )
        res = pd.read_csv(response, delimiter="|", skiprows=2)
        if len(res) > 0:
            res["epoch"] = time
            res.rename(
                {"# Num ": "Num", " Name ": "Name", " Class ": "Class", " Mv ": "Mv"},
                inplace=True,
                axis="columns",
            )
            res = res[["Num", "Name", "Class", "Mv", "epoch"]].reset_index(drop=True)
            if df is None:
                df = res
            else:
                df = pd.concat([df, res])
    if df is not None:
        df.reset_index(drop=True)
    return df

def show_citation_instructions():
    """Show citation instructions."""
    from . import PACKAGEDIR, __citation__

    # To make installing Lightkurve easier, ipython is an optional dependency,
    # because we can assume it is installed when notebook-specific features are called
    try:
        from IPython.display import HTML

        ipython_installed = True
    except ModuleNotFoundError:
        ipython_installed = False

    if not is_notebook() or not ipython_installed:
        print(__citation__)
    else:
        from pathlib import Path  # local import to speed up `import lightkurve`
        import astroquery  # local import to speed up `import lightkurve`

        templatefile = Path(PACKAGEDIR, "data", "show_citation_instructions.html")
        template = open(templatefile, "r").read()
        template = template.replace("LIGHTKURVE_CITATION", __citation__)
        template = template.replace("ASTROPY_CITATION", astropy.__citation__)
        template = template.replace("ASTROQUERY_CITATION", astroquery.__citation__)
        return HTML(template)

def _get_notebook_environment():
    """Returns 'jupyter', 'colab', or 'terminal'.

    One can detect whether or not a piece of Python is running by executing
    `get_ipython().__class__`, which returns the following result:

        * Jupyter notebook: `ipykernel.zmqshell.ZMQInteractiveShell`
        * Google colab: `google.colab._shell.Shell`
        * IPython terminal: `IPython.terminal.interactiveshell.TerminalInteractiveShell`
        * Python terminal: `NameError: name 'get_ipython' is not defined`
    """
    try:
        ipy = str(type(get_ipython())).lower()
        if "zmqshell" in ipy:
            return "jupyter"
        if "colab" in ipy:
            return "colab"
    except NameError:
        pass  # get_ipython() is not a builtin
    return "terminal"

def is_notebook():
    """Returns `True` if we are running in a notebook."""
    return _get_notebook_environment() in ["jupyter", "colab"]

def remote_jupyter_proxy_url(port):
    """
    Callable to configure Bokeh's show method when a proxy must be
    configured.    If port is None we're asking about the URL
    for the origin header.
    """
    base_url = os.environ['LK_JUPYTERHUB_EXTERNAL_URL']
    host = urllib.parse.urlparse(base_url).netloc

    # If port is None we're asking for the URL origin
    # so return the public hostname.
    if port is None:
        return host

    service_url_path = os.environ['JUPYTERHUB_SERVICE_PREFIX']
    proxy_url_path = 'proxy/%d' % port

    user_url = urllib.parse.urljoin(base_url, service_url_path)
    full_url = urllib.parse.urljoin(user_url, proxy_url_path)
    return full_url

def finalize_notebook_url(notebook_url):
    """Based on `notebook_url` and the environment, compute a final value for
    notebook_url to be passed on to bokeh enabling transparent operation on JupyterHub.

    See Bokeh instructions here:
    https://docs.bokeh.org/en/latest/docs/user_guide/output/jupyter.html

    This handles two aspects of Bokeh made tricky by JupyterHub, firstly
    accessing the random Bokeh server port while behind a proxy, and second not
    triggering CORS restrictions while accessing a second server.

    A key aspect of the computed URL is the externally visible DNS name of the
    JupyterHub, so for the case of TIKE we might have:

    export LK_JUPYTERHUB_EXTERNAL_URL="https://timeseries.science.stsci.edu"

    If LK_JUPYTERHUB_EXTERNAL_URL is implicitly defined by the hub environment,
    JupyterHub users can nominally ignore the notebook_url parameter and
    Lightkurve should "just work" as if the local default URL localhost:8888
    was sufficient.

    For example remote_jupyter_proxy_url(25346) would return the URL
    "https://test.timeseries.science.stsci.edu/user/homer@stsci.edu/proxy/24356"

    which is essentially HUB + USER_SESSION + BOKEH_PORT_IN_SESSION

    The function result should be identical to past behavior unless the definition
    of LK_JUPYTERHUB_EXTERNAL_URL indicates JupyterHub is in use.  In this case the
    use of remote_jupyter_proxy_url is activated.   This effectively makes it the
    JupyterHub default instead of localhost:8888.
    """
    if notebook_url is not None:
        return notebook_url
    elif os.environ.get("LK_JUPYTERHUB_EXTERNAL_URL"):
        return remote_jupyter_proxy_url
    else:
        return "localhost:8888"


import inspect
import re
from abc import ABC, abstractmethod
from functools import partial
from itertools import chain
from os import remove
from tempfile import NamedTemporaryFile
from typing import Mapping, Optional, Sequence, Tuple

from mypy.api import run, run_dmypy

from assert_typecheck.util import normalize_type

class BaseMypyRunner(ABC):
    @abstractmethod
    def run(self, args: Sequence[str], temp_file_name: str, target_name: str) -> Tuple[str, str, int]:
        ...

    def _assert_mypy_typechecks(self, obj, *, python_version: Optional[str] = None, platform: Optional[str] = None,
                                other_options: Sequence[str] = (), aliases: Optional[Mapping] = None, ):
        filename = inspect.getfile(obj)
        mod_source, source_start = inspect.findsource(obj)
        source_end = len(inspect.getblock(mod_source[source_start:])) + source_start

        mod_source = list(mod_source)
        for line_index in chain(range(source_start), range(source_end, len(mod_source))):
            mod_source[line_index] = mod_source[line_index].rstrip() + '# type: ignore\n'

        if aliases:
            aliases = {normalize_type(k): normalize_type(v) for k, v in aliases.items()}

            def repl(match):
                return aliases[match.group(0)]

            repl_pattern = re.compile(
                r'(?<![a-zA-Z_])' + '|'.join(re.escape(k) for k in aliases.keys()) + r'(?![a-zA-Z_])')

            for line_index in range(source_start, source_end):
                mod_source[line_index] = repl_pattern.sub(repl, mod_source[line_index])

        indent = re.match(r'\s*', mod_source[source_start])[0]  # type: ignore[index]
        mod_source.insert(source_end, f'{indent}import typing; __any: typing.Any = ...;{obj.__name__}(*__any)\n')

        args = [*other_options, '--show-error-context', '--show-error-codes', '--no-error-summary']

        if python_version:
            args.append('--python-version')
            args.append(python_version)

        if platform:
            args.append('--platform')
            args.append(platform)

        with NamedTemporaryFile(mode="w", delete=False) as file:
            file.writelines(mod_source)
        try:
            out, err, exit_code = self.run(args, file.name, filename)
        finally:
            remove(file.name)

        if exit_code == 0:
            return

        if err:
            raise RuntimeError(err)

        raise AssertionError(out)

    def assert_mypy_typechecks(self, obj=None, **kwargs):
        if obj is None:
            return partial(self._assert_mypy_typechecks, **kwargs)
        return self._assert_mypy_typechecks(obj, **kwargs)

class MypyRunner(BaseMypyRunner):
    def run(self, args: Sequence[str], temp_file_name: str, target_name: str) -> Tuple[str, str, int]:
        args = [*args, '--shadow-file', target_name, temp_file_name, target_name]
        return run(args)

_global_mypy_runner = MypyRunner()

assert_mypy_typechecks = _global_mypy_runner.assert_mypy_typechecks

class DMyPyRunner(BaseMypyRunner):
    def __init__(self, python_version: Optional[str] = None, platform: Optional[str] = None,
                 other_options: Sequence[str] = ()):
        self.python_version = python_version
        self.platform = platform
        self.other_options = other_options

    def _assert_mypy_typechecks(self, obj, *, python_version: Optional[str] = None, platform: Optional[str] = None,
                                other_options: Sequence[str] = (), aliases: Optional[Mapping] = None, ):
        other_options = tuple(other_options)
        if (python_version != self.python_version
                or platform != self.platform
                or other_options != self.other_options):
            return assert_mypy_typechecks(obj, python_version=python_version, platform=platform,
                                          other_options=other_options, aliases=aliases)
        return super()._assert_mypy_typechecks(obj, python_version=python_version, platform=platform,
                                               other_options=other_options, aliases=aliases)

    def run(self, args: Sequence[str], temp_file_name: str, target_name: str) -> Tuple[str, str, int]:
        args = ['run', '--', *args, temp_file_name]
        raw_results = run_dmypy(args)
        # because we don't shadow the file, we un-alias the target name ourselves
        out, err, exit_code = raw_results
        out = out.replace(temp_file_name, target_name)
        return out, err, exit_code

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        run_dmypy(['stop'])


from PIL import Image, ImageDraw, ImageFont
import requests
from os import getcwd
from io import BytesIO
from json import load, dump
from base.resource_manager import ResourceManager

rm = ResourceManager()

DATA_PATH = rm.genpath('data')
IMAGES_PATH = rm.genpath('images/artifacts')

def create_image(set,  images_list ):
        font = ImageFont.truetype(rm.genpath('misc')+'font.otf', 45)
        with open(f'{getcwd()}/template_image.json','r') as f:
            template =  load(f)
        new = Image.new(mode='RGBA',size=(template['dimensions']['width'],template['dimensions']['height']))
        images = {}
        for c, i in enumerate(images_list,1):
            images[str(c)] = i

        for image in images:
            url = images[image]
            x_pos = template[image]['x']
            y_pos = template[image]['y']
            r = requests.get(url).content
            paste_ = Image.open(BytesIO(r))
            new.paste(paste_, (x_pos,y_pos))

        ImageDraw.Draw(new).text((460,500), text=set.replace(' ','\n',99), font=font, fill=(255,255,255))
        new.save(IMAGES_PATH+set+'.png',format='PNG')
        print('image created for', set)

artifacts = {}
with open(DATA_PATH+'/artifacts.json','r') as f:
    artifacts = load(f)

for arti in artifacts:
    images = [i['img'][:i['img'].find('/revision')] for i in artifacts[arti]['pieces']]
    create_image(arti, images)
    artifacts[arti]['file'] = arti+'.png'

with open(DATA_PATH+'/artifacts_filed.json', 'w') as f:
    dump(artifacts, f, indent=1)


"""
Reading Simple GemPy model in Subsurface
========================================
"""

import sys

sys.path.insert(0, 'subsurface/')
import pooch
import subsurface as ss
import subsurface.reader.read_netcdf

# %% md
# Single surface
# --------------

# Pull gempy model
model_files = pooch.retrieve(
    url="https://github.com/cgre-aachen/gempy_data/raw/master/"
        "data/subsurface/example1.zip",
    known_hash="fb79a63eeb874cf0cdca557106c62c67eace23811db5935e57c3448fed7f8978",
    processor=pooch.Unzip()
)

# # %%
fname, = [i for i in model_files if "meshes.nc" in i]
dataset = ss.reader.read_netcdf.read_unstruct(fname)

# %%
obj = ss.TriSurf(dataset)
print(obj.mesh.points_attributes_to_dict)

mesh = ss.visualization.to_pyvista_mesh(obj)
ss.visualization.pv_plot([mesh])

# %% md
# Four Layers
# -----------

# Pull gempy model
model_files_2 = pooch.retrieve(
    url="https://github.com/cgre-aachen/gempy_data/raw/master/data/gempy_models/Kim.zip",
    known_hash="f530a88351ed0e38673c6937161c59a2f69df92202e14c1e5d5729ed5d72a323",
    processor=pooch.Unzip()
)

# %% md
# Triangular meshes
# +++++++++++++++++

# %%
fname, = [i for i in model_files_2 if "meshes.nc" in i]
gempy_tri_mesh_unstruct =  ss.reader.read_netcdf.read_unstruct(fname)
tri_surf = ss.TriSurf(gempy_tri_mesh_unstruct)
mesh = ss.visualization.to_pyvista_mesh(tri_surf)
ss.visualization.pv_plot([mesh])

# %% md
# Regular grid
# ++++++++++++

fname, = [i for i in model_files_2 if "regular_grid.nc" in i]
gempy_struct = ss.reader.read_netcdf.read_struct(fname)
regular_grid = ss.StructuredGrid(gempy_struct)

# %%
pyvista_mesh = ss.visualization.to_pyvista_grid(regular_grid,
                                                data_set_name='property_matrix',
                                                attribute_slice={'Properties': 'id'}
                                                )

ss.visualization.pv_plot([pyvista_mesh])

# %%

pyvista_mesh = ss.visualization.to_pyvista_grid(regular_grid,
                                                data_set_name='block_matrix',
                                                attribute_slice={'Properties': 'id',
                                                                 'Features': 'Default series'})

ss.visualization.pv_plot([pyvista_mesh])


import numpy as np
from sklearn.gaussian_process import GaussianProcessRegressor

import inspect

def clean_kwargs(func, kwargs):
    """Checks if the kwargs are valid for the function"""
    cleaned_kwargs = {}
    sig = inspect.signature(func)
    if not any(p.kind == p.VAR_KEYWORD for p in sig.parameters.values()):
        for key, val in kwargs.items():
            if key in sig.parameters.keys():
                cleaned_kwargs[key] = val
    return cleaned_kwargs

class DistributionCentricGPR(GaussianProcessRegressor):
    """Distribution Centric Gaussian Process Regressor

    Note, this is a wrapper around the sklearn GaussianProcessRegressor and
    requires the exact same (incl. hyperparameters) kernel for all distillation steps.

    Parameters
    ----------
    alpha : float
        The alpha for the distribution centric GPR (i.e. the precomputed alpha for all steps).

    alphas : array-like, shape (num_distillations,)
        The alphas for the distribution centric GPR (i.e. list of alphas for each step).

    **kwargs : dict
        The usual kwargs for the GaussianProcessRegressor.
    """

    def __init__(
        self,
        kernel=None,
        alpha=[1e-10],
        alphas=None,
        optimizer="fmin_l_bfgs_b",
        n_restarts_optimizer=0,
        normalize_y=False,
        copy_X_train=True,
        random_state=None,
        **kwargs,
    ):
        if alphas is not None:
            self.alphas = np.asarray(alphas)
            assert np.all(self.alphas > 0), "alphas must be positive"
            self.num_distillations = len(self.alphas)
            self.alpha = 1 / (np.sum(1 / self.alphas))
        else:
            self.alphas = None
            self.alpha = alpha
            self.num_distillations = None
        super().__init__(
            kernel=kernel,
            alpha=self.alpha,
            optimizer=optimizer,
            n_restarts_optimizer=n_restarts_optimizer,
            normalize_y=normalize_y,
            copy_X_train=copy_X_train,
            random_state=random_state,
        )


import numpy as np
import matplotlib.pyplot as plt
import scipy.sparse as sp
from matplotlib import cm, colors
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cm as cmx
import os
from utility import *

plt.rcParams.update({'font.size':20})

class cdata:

    def __init__(self, year_start, year_end, pressure):

        self.pressure = pressure
        domain = os.path.join(os.path.join(os.getcwd(), "ARGO"), "compact"+str(pressure))
        if os.path.exists(domain):
            time, lat, long, temp, pres = np.array([]), np.array([]), np.array([]), np.array([]), np.array([])
            time_list, lat_list, long_list, temp_list, pres_list, time_dic = [], [], [], [], [], {}

            for year in range(year_start, year_end + 1):
                loc_time, loc_lat, loc_long, loc_temp, loc_pres = \
                    np.load(os.path.join(domain, str(year)+".npy"), allow_pickle=True)
                time = np.hstack((time, loc_time))
                lat = np.hstack((lat, loc_lat))
                long = np.hstack((long, loc_long))
                temp = np.hstack((temp, loc_temp))
                pres = np.hstack((pres, loc_pres))
            current_indice = 0
            while current_indice < time.shape[0]:
                old_day = int(time[current_indice])
                new_indice = current_indice
                current_day = int(time[current_indice])
                while current_day == old_day and new_indice < time.shape[0]:
                    new_indice += 1
                    if new_indice > time.shape[0]:
                        current_day, old_day = int(time[new_indice-1]), current_day
                    else:
                        current_day, old_day = int(time[new_indice-1]), current_day
                #time_dic[old_day] = len(time_dic)
                time_list.append(time[current_indice:new_indice])
                lat_list.append(lat[current_indice:new_indice])
                long_list.append(long[current_indice:new_indice])
                temp_list.append(temp[current_indice:new_indice])
                pres_list.append(pres[current_indice:new_indice])
                current_indice = new_indice

            time_anchor = np.min(time)
            for i in range(len(time_list)):
                time_list[i] -= time_anchor

            self.time_anchor = time_anchor
            self.time = time
            self.lat = lat
            self.long = long
            self.temp = temp
            self.pres = pres
            self.xyz = cart((self.long + 180) * np.pi / 180, (self.lat + 90) * np.pi / 180)
            self.true_dim = self.temp.shape[0]
            self.pressure = pressure
            self.year_start = year_start
            self.year_end = year_end

            self.time_list = time_list
            self.lat_list = lat_list
            self.long_list = long_list
            self.temp_list = temp_list
            self.pres_list = pres_list
            #self.time_dic = time_dic

        else:
            print("The data has not been compacted.")

    def spatial_plot(self, indice, llon=-180, llat=-70, ulon=180, ulat=70):

        fig, axes = plt.subplots(ncols=1, nrows=1, figsize=(25, 12))
        map = Basemap(projection='cyl', llcrnrlon=llon, llcrnrlat=llat, urcrnrlon=ulon, urcrnrlat=ulat, ax=axes)
        map.drawcoastlines()
        if len(indice) == 2:
            day_start = indice[0]
            day_end = indice[1]
        elif len(indice) == 1:
            day_start = indice[0]
            day_end = indice[0]
        else:
            print("Error; indice must have only one or two indexes")
        temp_plot = np.array([])
        lat_plot = np.array([])
        long_plot = np.array([])
        for day in range(day_start, day_end + 1):
            try:
                temp_plot = np.hstack((temp_plot, self.temp_list[day]))
                lat_plot = np.hstack((lat_plot, self.lat_list[day]))
                long_plot = np.hstack((long_plot, self.long_list[day]))
            except:
                donothing = 1

        x, y = map(long_plot,  lat_plot)
        jet = plt.get_cmap('jet', 2000)
        cnorm = colors.Normalize(vmin=np.min(temp_plot), vmax=np.max(temp_plot))
        scalarMap = cmx.ScalarMappable(norm=cnorm, cmap=jet)
        im = map.scatter(x, y, marker='D', color=scalarMap.to_rgba(temp_plot))
        plt.colorbar(mappable=scalarMap, ax=axes)
        axes.set_title("ARGO data between day " + str(day_start) + " and " + str(day_end) + " (included) at pressure "\
                       + str(self.pressure) + ".")
        map.fillcontinents(color="k")
        plt.show()


import torch
import torch.nn as nn
import losses
import math
import argparse
import numpy as np
import os
from tqdm import tqdm
import torch.nn.functional as F
from utils import init_weights
import torchvision
from utils import *
import torch.utils.model_zoo as model_zoo
from torch import nn
class ArcMarginProduct(nn.Module):
    r"""Implement of large margin arc distance: :
        Args:
            in_features: size of each input sample
            out_features: size of each output sample
            s: norm of input feature
            m: margin
            cos(theta + m)
        """
    def __init__(self, in_features, out_features, s=30.0, m=0.50, easy_margin=False):
        super(ArcMarginProduct, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.s = s
        self.m = m
        self.weight = nn.Parameter(torch.FloatTensor(out_features, in_features))
        nn.init.xavier_uniform_(self.weight)

        self.easy_margin = easy_margin
        self.cos_m = math.cos(m)
        self.sin_m = math.sin(m)
        self.th = math.cos(math.pi - m)
        self.mm = math.sin(math.pi - m) * m

    def forward(self, input, label):
        # --------------------------- cos(theta) & phi(theta) ---------------------------
        cosine = F.linear(F.normalize(input), F.normalize(self.weight))
        sine = torch.sqrt((1.0 - torch.pow(cosine, 2)).clamp(0, 1))
        phi = cosine * self.cos_m - sine * self.sin_m
        if self.easy_margin:
            phi = torch.where(cosine > 0, phi, cosine)
        else:
            phi = torch.where(cosine > self.th, phi, cosine - self.mm)
        # --------------------------- convert label to one-hot ---------------------------
        # one_hot = torch.zeros(cosine.size(), requires_grad=True, device='cuda')
        one_hot = torch.zeros(cosine.size(), device='cuda')
        one_hot.scatter_(1, label.view(-1, 1).long(), 1)
        # -------------torch.where(out_i = {x_i if condition_i else y_i) -------------
        output = (one_hot * phi) + ((1.0 - one_hot) * cosine)  # you can use torch.where if your torch.__version__ is 0.4
        output *= self.s
        # print(output)

        return output

class DSAN(nn.Module):
    def __init__(self, num_classes=31, bottle_neck=True,max_iter=1000):
        super(DSAN, self).__init__()
        self.feature_layers = ResBase50()
        self.lmmd_loss = losses.ori_LMMD_loss(class_num=num_classes)
        self.bottle_neck = bottle_neck
        if bottle_neck:
            bottleneck_dim = 256
            self.bottle = nn.Linear(2048, 256)
            self.cls_fc = nn.Linear(256, num_classes)
        else:
            bottleneck_dim = 2048
            self.cls_fc = nn.Linear(2048, num_classes)
        self.num_class = num_classes
        self.max_iter = max_iter
        self.iter_num = 0
        self.ad_net = AdversarialNetwork(bottleneck_dim*num_classes, 1024, max_iter=self.max_iter).cuda()
    # def init_cate_dict(self,source_loader,target_loader,num_classes,cate_len):
    #     with torch.no_grad():
    #         source_cate_dict = {idx:[] for idx in range(num_classes)}
    #         iter_data = iter(source_loader)
    #         dict_counter = 0
    #         dict_index_set = set()
    #         while True:
    #             data_source, label_source = next(iter_data)
    #             data_source,label_source = data_source.to(self.device),label_source.to(self.device)
    #             source,source_stages = self.base_network(data_source)
    #             if self.use_bottleneck:
    #                 source = self.bottleneck_layer(source)
    #             source_clf = self.classifier_layer(source)
    #             #B,D
    #             source = source[source_clf.argmax(-1)==label_source]
    #             label_source = label_source[source_clf.argmax(-1)==label_source]
    #             for source_f,class_idx in zip(source[:],label_source.int().tolist()):
    #                 if len(source_cate_dict[class_idx])<cate_len:
    #                     source_cate_dict[class_idx].append(F.normalize(source_f.detach(),dim=-1))
    #                 if len(source_cate_dict[class_idx])>=cate_len and class_idx not in dict_index_set:
    #                     dict_index_set.add(class_idx)
    #                     dict_counter += 1
    #             if dict_counter >= num_classes:
    #                 break
    #         source_cate_dict = torch.stack([torch.stack(v) for k,v in source_cate_dict.items()])

    #         target_cate_dict = {idx:[] for idx in range(num_classes)}
    #         iter_data = iter(target_loader)
    #         dict_counter = 0
    #         dict_index_set = set()
    #         thres = 0.9
    #         while True:
    #             data_target, _ = next(iter_data)
    #             data_target = data_target.to(self.device)
    #             target,target_stages = self.base_network(data_target)
    #             if self.use_bottleneck:
    #                 target = self.bottleneck_layer(target)
    #             target_clf = self.classifier_layer(target)
    #             #B,D
    #             label_target = target_clf.argmax(-1)
    #             target = target[label_target>thres]
    #             label_target = label_target[label_target>thres]
    #             for target_f,class_idx in zip(target[:],label_target.int().tolist()):
    #                 if len(target_cate_dict[class_idx])<cate_len:
    #                     target_cate_dict[class_idx].append(F.normalize(target_f.detach(),dim=-1))
    #                 if len(target_cate_dict[class_idx])>=cate_len and class_idx not in dict_index_set:
    #                     dict_index_set.add(class_idx)
    #                     dict_counter += 1
    #             if dict_counter >= num_classes:
    #                 break
    #             thres -= 0.01
    #         target_cate_dict = torch.stack([torch.stack(v) for k,v in target_cate_dict.items()])
    #         cate_dict = torch.concat([source_cate_dict,target_cate_dict],dim=1)
    #         idx = torch.randperm(cate_dict.shape[1])
    #         cate_dict = cate_dict[:,idx]
    #         return cate_dict.to(self.device)

    # def update_cate_dict(self,source_clf,source_label,source_feature,target_clf,target_feature):
    #     with torch.no_grad():
    #         new_cate_dict = torch.zeros_like(self.cate_dict)
    #         source_feature = source_feature[source_clf.argmax(-1)==source_label]
    #         source_label = source_label[source_clf.argmax(-1)==source_label]
    #         class_idx_set = set()
    #         for source_f,class_idx in zip(source_feature[:],source_label.int().tolist()):
    #             if class_idx not in class_idx_set:
    #                 class_idx_set.add(class_idx)
    #                 new_cate_dict[:,1:] = self.cate_dict[:,:-1].detach()
    #                 new_cate_dict[:,0] = F.normalize(source_f.detach(),dim=-1)
    #         target_label = target_clf[target_clf.argmax(-1)>0.8].argmax(-1)
    #         target_feature = target_feature[target_clf.argmax(-1)>0.8]
    #         for target_f,class_idx in zip(target_feature[:],target_label.int().tolist()):
    #             if class_idx not in class_idx_set:
    #                 class_idx_set.add(class_idx)
    #                 new_cate_dict[:,1:] = self.cate_dict[:,:-1].detach()
    #                 new_cate_dict[:,0] = F.normalize(target_f.detach(),dim=-1)

    #         return new_cate_dict.to(self.device)

    # def get_logits_thres(self,max_thres=0.8,min_thres=0.2):
    #     p = self.iter_num / self.max_iter
    #     return max_thres - (max_thres - min_thres) * p

    # def step(self):
    #     self.iter_num+=1

    def forward(self, source, target, s_label):
        source = self.feature_layers(source)
        if self.bottle_neck:
            source = self.bottle(source)
        s_pred = self.cls_fc(source)

        target = self.feature_layers(target)
        if self.bottle_neck:
            target = self.bottle(target)
        t_pred = self.cls_fc(target)
        t_logits = torch.nn.functional.softmax(t_pred,dim=1)

        # entropy = None
        # features = torch.cat((source, target), dim=0)
        # outputs = torch.cat((s_pred,t_pred), dim=0)
        # softmax_out = nn.Softmax(dim=1)(outputs)
        # eff = calc_coeff(self.iter_num, max_iter=self.max_iter)
        # cdan_loss = losses.CDAN([features, softmax_out], self.ad_net, entropy, eff)

        # logits_thres = self.get_logits_thres()
        # t_logits_max = t_logits.max(-1)
        # t_logits_filter_bool = t_logits_max.values>logits_thres
        # t_logits_filter_index = torch.arange(0,t_logits.size()[0])[t_logits_filter_bool]
        # t_logits = t_logits[t_logits_filter_index]
        # target = target[t_logits_filter_index]
        loss_lmmd = self.lmmd_loss.get_loss(source, target, s_label,t_logits)
        # ad_net = utils.AdversarialNetwork(args.bottleneck_dim*class_num, 1024, max_iter=args.max_iter).cuda()
        # entropy = loss.Entropy(softmax_out)
        # self.step()
        return s_pred, loss_lmmd

    def predict(self, x):
        x = self.feature_layers(x)
        if self.bottle_neck:
            x = self.bottle(x)
        return self.cls_fc(x)

class VGG16Base(nn.Module):
  def __init__(self):
    super(VGG16Base, self).__init__()
    model_vgg = torchvision.models.vgg16(pretrained=True)
    self.features = model_vgg.features
    self.classifier = nn.Sequential()
    for i in range(6):
        self.classifier.add_module("classifier"+str(i), model_vgg.classifier[i])
    self.feature_layers = nn.Sequential(self.features, self.classifier)
    self.in_features = 4096

  def forward(self, x):
    x = self.features(x)
    x = x.view(x.size(0), -1)
    x = self.classifier(x)
    return x

class ResBase18(nn.Module):
    def __init__(self):
        super(ResBase18, self).__init__()
        model_resnet = torchvision.models.resnet18(pretrained=True)
        self.conv1 = model_resnet.conv1
        self.bn1 = model_resnet.bn1
        self.relu = model_resnet.relu
        self.maxpool = model_resnet.maxpool
        self.layer1 = model_resnet.layer1
        self.layer2 = model_resnet.layer2
        self.layer3 = model_resnet.layer3
        self.layer4 = model_resnet.layer4
        self.avgpool = model_resnet.avgpool
        self.in_features = model_resnet.fc.in_features

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        return x

class ResBase34(nn.Module):
    def __init__(self):
        super(ResBase34, self).__init__()
        model_resnet = torchvision.models.resnet34(pretrained=True)
        self.conv1 = model_resnet.conv1
        self.bn1 = model_resnet.bn1
        self.relu = model_resnet.relu
        self.maxpool = model_resnet.maxpool
        self.layer1 = model_resnet.layer1
        self.layer2 = model_resnet.layer2
        self.layer3 = model_resnet.layer3
        self.layer4 = model_resnet.layer4
        self.avgpool = model_resnet.avgpool
        self.in_features = model_resnet.fc.in_features

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        return x

class ResBase50(nn.Module):
    def __init__(self):
        super(ResBase50, self).__init__()
        model_resnet50 = torchvision.models.resnet50(pretrained=True)
        self.conv1 = model_resnet50.conv1
        self.bn1 = model_resnet50.bn1
        self.relu = model_resnet50.relu
        self.maxpool = model_resnet50.maxpool
        self.layer1 = model_resnet50.layer1
        self.layer2 = model_resnet50.layer2
        self.layer3 = model_resnet50.layer3
        self.layer4 = model_resnet50.layer4
        self.avgpool = model_resnet50.avgpool
        self.in_features = model_resnet50.fc.in_features

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        return x

class ResBase101(nn.Module):
    def __init__(self):
        super(ResBase101, self).__init__()
        model_resnet101 = torchvision.models.resnet101(pretrained=True)
        self.conv1 = model_resnet101.conv1
        self.bn1 = model_resnet101.bn1
        self.relu = model_resnet101.relu
        self.maxpool = model_resnet101.maxpool
        self.layer1 = model_resnet101.layer1
        self.layer2 = model_resnet101.layer2
        self.layer3 = model_resnet101.layer3
        self.layer4 = model_resnet101.layer4
        self.avgpool = model_resnet101.avgpool
        self.in_features = model_resnet101.fc.in_features

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        return x

class ResClassifier(nn.Module):
    def __init__(self, class_num, feature_dim,bottleneck=True,bottleneck_dim=256):
        super(ResClassifier, self).__init__()
        self.bottleneck = bottleneck
        if bottleneck:
            self.bottleneck = nn.Linear(feature_dim, bottleneck_dim)
            self.fc = nn.Linear(bottleneck_dim, class_num)
        else:
            bottleneck_dim = feature_dim
            self.fc = nn.Linear(bottleneck_dim, class_num)
        # self.bottleneck.apply(init_weights)
        # self.fc.apply(init_weights)

    def forward(self, x):
        if self.bottleneck:
            x = self.bottleneck(x)
        y = self.fc(x)
        return x,y

class AdversarialNetwork(nn.Module):
  def __init__(self, in_feature, hidden_size, max_iter=10000):
    super(AdversarialNetwork, self).__init__()
    self.ad_layer1 = nn.Linear(in_feature, hidden_size)
    self.ad_layer2 = nn.Linear(hidden_size, hidden_size)
    self.ad_layer3 = nn.Linear(hidden_size, 1)
    self.relu1 = nn.ReLU()
    self.relu2 = nn.ReLU()
    self.dropout1 = nn.Dropout(0.5)
    self.dropout2 = nn.Dropout(0.5)
    self.sigmoid = nn.Sigmoid()
    self.apply(init_weights)
    self.iter_num = 0
    self.alpha = 10
    self.low = 0.0
    self.high = 1.0
    self.max_iter = max_iter

  def forward(self, x):
    if self.training:
        self.iter_num += 1
    coeff = calc_coeff(self.iter_num, self.high, self.low, self.alpha, self.max_iter)
    x = x * 1.0
    x.register_hook(grl_hook(coeff))
    x = self.ad_layer1(x)
    x = self.relu1(x)
    y = self.ad_layer3(x)
    y = self.sigmoid(y)
    return y

  def output_num(self):
    return 1
  def get_parameters(self):
    return [{"params":self.parameters(), "lr_mult":10, 'decay_mult':2}]

model_urls = {
    'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
    'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
    'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
    'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
    'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
}

def conv3x3(in_planes, out_planes, stride=1):
    """3x3 convolution with padding"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                     padding=1, bias=False)

class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(BasicBlock, self).__init__()
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = nn.BatchNorm2d(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = nn.BatchNorm2d(planes)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
                               padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)
        self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(planes * 4)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

class ResNet(nn.Module):

    def __init__(self, block, layers, num_classes=1000):
        self.inplanes = 64
        super(ResNet, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        self.avgpool = nn.AvgPool2d(7, stride=1)
        self.baselayer = [self.conv1, self.bn1, self.layer1, self.layer2, self.layer3, self.layer4]
        self.fc = nn.Linear(512 * block.expansion, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)

        return x

def resnet50(pretrained=False, **kwargs):
    """Constructs a ResNet-50 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
    return model

def resnet101(pretrained=False,**kwargs):
    """Constructs a ResNet-50 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 4, 23, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
    return model


"""Add many to many relationship user-permission

Revision ID: c90db4410eab
Revises: 8f0af5b4181a
Create Date: 2023-08-21 21:48:25.175618

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'c90db4410eab'
down_revision = '8f0af5b4181a'
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('user_permission',
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('permission_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['permission_id'], ['permissions.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('user_id', 'permission_id')
    )
    op.drop_constraint('permissions_owner_id_fkey', 'permissions', type_='foreignkey')
    op.drop_column('permissions', 'test')
    op.drop_column('permissions', 'owner_id')
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('permissions', sa.Column('owner_id', sa.INTEGER(), autoincrement=False, nullable=True))
    op.add_column('permissions', sa.Column('test', sa.VARCHAR(), autoincrement=False, nullable=True))
    op.create_foreign_key('permissions_owner_id_fkey', 'permissions', 'users', ['owner_id'], ['id'])
    op.drop_table('user_permission')
    # ### end Alembic commands ###


import nltk
import csv
import re
import stemming

from nltk.corpus import stopwords,wordnet
from nltk.stem import PorterStemmer, WordNetLemmatizer
from nltk.tokenize import word_tokenize

def main():

	stop_words = stopwords.words('english') + ['advertisement','support']
	print(stop_words)
	#stop_words.append('said')
	#stop_words.append('would')
	#stop_words.append('s')
	#raise NotImplementedError
	ps  = PorterStemmer()
	wnl = WordNetLemmatizer()
	for value in range(1,101):
		text = ""
		with open("./NHL/NHL_File_" + str(value)+ ".txt",'r') as f:
			print("Running")
			for line in f:
				#print(para)
				#para = ["runner","running","run"]
				line = line.lower()
				line = re.sub(r"[^A-Za-z]+",' ',line)
				word_tokens = word_tokenize(line)
				#word_tokens = ["runner","running","run"]
				filtered_sentence = []
				stemmed_words = ""
				for w in word_tokens:
					if w not in stop_words:
						filtered_sentence.append(w)
				for w in filtered_sentence:
					if w.endswith('e') or w.endswith('s') or w.endswith('y') or w.endswith('l'):
						#stemmed_words.append(w +' : '+wnl.lemmatize(w))
						stemmed_words = stemmed_words + wnl.lemmatize(w) + " "
					else:
						#stemmed_words.append(w+' : '+ps.stem(w))
						stemmed_words = stemmed_words + ps.stem(w) + " "
				text = text + stemmed_words
			#print(text)
			text_file = open("./NHL_STEM/NHL_STEM_" +str(value) +".txt", "w")
			text_file.write(text)
			text_file.close()
			#raise NotImplementedError

if __name__ == "__main__":
	main()


# Эта задача внешне похожа не предыдущую. У нас так же есть горка и скромные и наглые дети, которые ведут себя так же как в прошлой задаче. Н
# о теперь на площадку пришли очень наглые дети, которые встают сразу в начало очереди.

# На вход подается несколько строк. В первой строке - количество строк, которое вам нужно будет прочитать. Во всех остальных может быть:

# пара вида `s N`, `i N` или `c N`, где символ s означает скромного ребенка, i – наглого, c –очень наглого, а N – номер ребенка.
# символ `+`, который означает, что ребенок, который стоял первым в очереди, скатился с горки.
# Напишите программу, котора выведет номера скатившихся детей.

# Sample Input 1:

# 5
# s 1
# c 2
# i 3
# +
# +
# Sample Output 1:

# 2
# 3
# Sample Input 2:

# 5
# s 1
# s 2
# +
# i 3
# +
# Sample Output 2:

# 1
# 2

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
        self.prev = None

    def __str__(self):
        return self.val
kid_count = 1
n = int(input())
first_kid = ListNode(input().split(sep=' ')[1])
tail_kid = first_kid
mid_kid = first_kid
head_kid = first_kid

for _ in range(n-1):
    a = input()
    if a == '+':
        print(int(head_kid.val))
        head_kid = head_kid.next
        if kid_count % 2 == 0:
            mid_kid = mid_kid.next
        kid_count -= 1
    else:
        b = a.split(sep=' ')
        char = b[0]
        num = b[1]
        kid = ListNode(num)

        if char == 's':
            kid.prev = tail_kid
            tail_kid.next = kid
            tail_kid = kid
            if (kid_count + 1) % 2 == 1:
                mid_kid = mid_kid.next
        elif char == 'i':
            kid.prev = mid_kid
            kid.next = mid_kid.next
            if mid_kid.next:
                mid_kid.next.prev = kid
            mid_kid.next = kid
            if kid_count % 2 == 0:
                mid_kid = mid_kid.next
            if kid.next is None:
                tail_kid = kid
        elif char == 'c':
            kid.next = head_kid
            head_kid.prev = kid
            head_kid = kid
            if kid_count % 2 == 1:
                mid_kid = mid_kid.prev

        kid_count += 1


from typing import List, Optional
from fastapi import FastAPI
import wikipedia
from wikipedia import WikipediaPage
import requests
import urllib
from settings import GITHUB_TOKEN
from schema import WikiResponse, GithubRepo, Response

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/search")
async def search(q: str = None):
    wiki = fetch_wiki_article(q)
    # sof = fetch_stackoverflow_answers(parsed_query)
    repos = fetch_github_repos(urllib.parse.quote(q))
    res = Response(
        query=q,
        wiki=wiki,
        repos=repos)
    return res

def fetch_wiki_article(query: str) -> Optional[WikiResponse]:
    try:
        wp: WikipediaPage = wikipedia.page(query)
        return WikiResponse(
            url=wp.url,
            title=wp.title,
            summary=wp.summary
        )
    except wikipedia.exceptions.DisambiguationError as e:
        print(e)
        wps = wikipedia.search(query)
        wp = wps[0]
        if wp:
            return WikiResponse(
                url=wp.url,
                title=wp.title,
                summary=wp.summary
            )
        return None
    except wikipedia.exceptions.PageError as e:
        print(e)
        return None

def fetch_github_repos(query: str) -> Optional[List[GithubRepo]]:
    url = f"https://api.github.com/search/repositories?q={query}+in:description&page=1"
    headers = {"Authorization": f"token {GITHUB_TOKEN}"}
    res = requests.get(url, headers)

    repos = res.json()["items"]
    if len(repos) == 0:
        return None
    return list(map(
        lambda repo: GithubRepo(
            url = repo.get("html_url", None),
            description = repo.get("description", ""),
            updated_at = repo.get("updated_at", ""),
            star = repo.get("stargazers_count", 0)
        ), repos))


import re

import w3af.core.controllers.output_manager as om

from w3af.core.data.parsers.doc.cookie_parser import parse_cookie
from w3af.core.data.misc.encoding import smart_unicode

LINK_HEADER_RE = re.compile('<(.*?)>.*')

def extract_link_from_header_simple(http_response, header_name, header_value):
    """
    Extract links from HTTP response headers which have the header value set to
    a link.

    Example headers we can parse:
        x-pingback: http://w3af.org/xmlrpc.php
        location: /foo/

    :param http_response: The http response object
    :param header_name: The http response header name
    :param header_value: The http response header value (where the URL lives)
    :return: Yield URL instances
    :see: https://github.com/andresriancho/w3af/issues/9493
    """
    if not header_value:
        raise StopIteration

    try:
        yield http_response.get_url().url_join(header_value)
    except ValueError:
        msg = ('The application sent a "%s" header that w3af'
               ' failed to correctly parse as an URL, the header'
               ' value was: "%s"')
        om.out.debug(msg % (header_name, header_value))

def extract_link_from_link_header(http_response, header_name, header_value):
    """
    Extract links from HTTP response headers which have the header value set to
    a "wordpress link"

    Example headers we can parse:
        link: <http://w3af.org/?p=4758>; rel=shortlink

    :param http_response: The http response object
    :param header_name: The http response header name
    :param header_value: The http response header value (where the URL lives)
    :return: Yield URL instances
    :see: https://github.com/andresriancho/w3af/issues/9493
    """
    re_match = LINK_HEADER_RE.search(header_value)
    if re_match:
        try:
            url_str = re_match.group(1)
        except IndexError:
            raise StopIteration

        if not url_str:
            raise StopIteration

        try:
            yield http_response.get_url().url_join(url_str)
        except ValueError:
            msg = ('The application sent a "%s" header that w3af'
                   ' failed to correctly parse as an URL, the header'
                   ' value was: "%s"')
            om.out.debug(msg % (header_name, header_value))

def extract_link_from_set_cookie_header(http_response, header_name, header_value):
    """
    Extract links from the "path" key of a cookie

    Example headers we can parse:
        set-cookie: __cfduid=...; path=/; domain=.w3af.org; HttpOnly

    :param http_response: The http response object
    :param header_name: The http response header name
    :param header_value: The http response header value (where the URL lives)
    :return: Yield URL instances
    :see: https://github.com/andresriancho/w3af/issues/9493
    """
    try:
        cookie = parse_cookie(header_value)
    except:
        raise StopIteration

    for key in cookie.keys():
        try:
            path = cookie[key]['path']
        except KeyError:
            continue

        if path:
            try:
                yield http_response.get_url().url_join(path)
            except ValueError:
                msg = ('The application sent a "%s" header that w3af'
                       ' failed to correctly parse as an URL, the header'
                       ' value was: "%s"')
                om.out.debug(msg % (header_name, header_value))

URL_HEADERS = {extract_link_from_header_simple: {'location',
                                                 'uri',
                                                 'content-location',
                                                 'x-pingback'},
               extract_link_from_link_header: {'link'},
               extract_link_from_set_cookie_header: {'set-cookie'}}

def headers_url_generator(resp, fuzzable_req):
    """
    Yields tuples containing:
        * Newly found URL
        * The FuzzableRequest instance passed as parameter
        * The HTTPResponse generated by the FuzzableRequest
        * Boolean indicating if we trust this reference or not

    The newly found URLs are extracted from the http response headers such
    as "Location".

    :param resp: HTTP response object
    :param fuzzable_req: The HTTP request that generated the response
    """
    resp_headers = resp.get_headers()

    for parser, header_names in URL_HEADERS.iteritems():
        for header_name in header_names:

            header_value, _ = resp_headers.iget(header_name, None)
            if header_value is not None:

                header_value = smart_unicode(header_value,
                                             encoding=resp.charset)

                for ref in parser(resp, header_name, header_value):
                    yield ref, fuzzable_req, resp, False


#
# @lc app=leetcode id=557 lang=python3
#
# [557] Reverse Words in a String III
#
# https://leetcode.com/problems/reverse-words-in-a-string-iii/description/
#
# algorithms
# Easy (72.83%)
# Total Accepted:    263K
# Total Submissions: 360.9K
# Testcase Example:  `"Let's take LeetCode contest"`
#
# Given a string s, reverse the order of characters in each word within a
# sentence while still preserving whitespace and initial word order.
#
#
# Example 1:
# Input: s = "Let's take LeetCode contest"
# Output: "s'teL ekat edoCteeL tsetnoc"
# Example 2:
# Input: s = "God Ding"
# Output: "doG gniD"
#
#
# Constraints:
#
#
# 1 <= s.length <= 5 * 10^4
# s contains printable ASCII characters.
# s does not contain any leading or trailing spaces.
# There is at least one word in s.
# All the words in s are separated by a single space.
#
#
#
class Solution:
    def reverseWords(self, s: str) -> str:
        output = ''
        last_index = 0
        for i in range(len(s)):
            if s[i] == ' ':
                for j in range(i - 1, last_index - 1, -1):
                    output += s[j]
                output += ' '
                last_index = i + 1
        if last_index != len(s):
            for j in range(len(s) - 1, last_index - 1, -1):
                output += s[j]

        return output


""" Corrected CC between APs and presynaptic events
Author: Drew B. Headley

"""
import sys

sys.path.append(".")  # have to do this for relative imports to work consistently
import numpy as np
import pandas as pd
import pdb
from .load_spike_h5 import load_spike_h5
from .load_spike_aux_h5 import load_spike_aux_h5
from .cc_ptpt import cc_ptpt

def cc_presyn_phdep_files(presyn_fname, ap_fname, rhym_ser, step_len, win, **kwargs):
    """
    Uses the files for the preynaptic spikes and phase of an afferent rhythm,
    calculate a phase stratified cross-correlation between presynaptic spikes
    and action potentials.

    Parameters
    ----------
    presyn_fname : string
        file path for presynaptic spike events h5
    ap_fname : numpy array
        file path for action potentials
    rhym_ser : numpy array
        inhibitory rhythm
    step_len : numeric
        the bin size for the CC in units of seconds
    win : [int, int] (default is [-10, 10])
        a 2 element list-like specifying the number of bins for either edge
        of the cross correlation function
    kwargs: arguments for cc_ptpt

    Returns
    ----------
    cc : dictionary
        cross correlation for each phase. Keys are 't' for trough and 'p' for peak

    Examples
    ----------

    """
    ph_mask = rhym_ser >= 0

    # load action potential events
    spk_pts = load_spike_h5(ap_fname)

    # load presynaptic events into
    pre_pts = load_spike_aux_h5(presyn_fname)

    # create spike series and keep only those with the right phase
    p_inds = np.where(ph_mask)[0]
    t_inds = np.where(~ph_mask)[0]
    # find set of presynaptic spikes in troughs and peaks
    pre_pts_t = pre_pts[np.isin(pre_pts, t_inds)]
    pre_pts_p = pre_pts[np.isin(pre_pts, p_inds)]

    # calculate cross-correlation
    cc = {}
    cc["t"] = cc_ptpt(spk_pts, pre_pts_t, step_len, win, **kwargs)
    cc["p"] = cc_ptpt(spk_pts, pre_pts_p, step_len, win, **kwargs)

    return cc

if __name__ == "__main__":
    from prettytable import PrettyTable
    import pdb

    print("Testing presynaptic spikes with cc_presyn_phdep_files.py")

    samps_per_ms = 10
    sim_win = [0, 200]
    cc_win = [-15, 15]  # multiply by step to get window in milliseconds
    step = 2 * samps_per_ms  # number of simulation steps for creating the dendritic
    # event occurrence series
    t_ser = np.linspace(
        sim_win[0], sim_win[1], samps_per_ms * 1000 * sim_win[1]
    )  # seconds
    sin_ser = np.sin(t_ser * 16 * 2 * np.pi)

    pre_fpath_test = "Z:\\DendOscSub\\output_clust_16Hz_conc\\exc_stim_aux_spikes2.h5"
    ap_fpath_test = "Z:\\DendOscSub\\output_clust_16Hz_conc\\spikes.h5"

    # pdb.set_trace()
    cc_test = cc_presyn_phdep_files(
        pre_fpath_test, ap_fpath_test, sin_ser, step, cc_win
    )

    # use prettytable to print the results with each row a cc lag
    tbl = PrettyTable()
    tbl.field_names = ["Lag (ms)", "Trough", "Peak"]
    for lags, t_cc, p_cc in zip(
        cc_test["t"]["lags"], cc_test["t"]["values"], cc_test["p"]["values"]
    ):
        tbl.add_row([lags, t_cc, p_cc])
    print(tbl)


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

import pandas as pd

pd.set_option("display.max_rows", 500)
pd.set_option("display.max_columns", 500)
pd.set_option("display.width", 1000)

df = pd.DataFrame({"Yes": [50, 21], "No": [131, 2]})
print(df)
df = pd.DataFrame({"Bob": ["I liked it.", "It was awful."], "Sue": ["Pretty good.", "Bland."]})
print(df)
df = pd.DataFrame(
    {"Bob": ["I liked it.", "It was awful."], "Sue": ["Pretty good.", "Bland."]},
    index=["Product A", "Product B"],
)
print(df)

df = pd.Series([1, 2, 3, 4, 5])
print(df)

df = pd.Series([30, 35, 40], index=["2015 Sales", "2016 Sales", "2017 Sales"], name="Product A")
print(df)

wine_reviews = pd.read_csv("winemag-data-130k-v2.csv")
print(wine_reviews.shape)
print(wine_reviews.head())

wine_reviews = pd.read_csv("winemag-data-130k-v2.csv", index_col=0)
print(wine_reviews.head())


import pyupbit
import os
from tqdm import tqdm
import time
import datetime
import argparse

def calc_count(interval) :
    interval_count_min = \
        {
            'minute1': 1440,
            'minute3': 480,
            'minute5': 288,
            'minute10': 144,
            'minute15': 96,
            'minute30': 48,
            'minute60': 24,
            'minute240': 6
        }
    interval_count_year = \
        {
            'day': 365,
            'week': 52,
            'month': 12
        }

    if interval in interval_count_min :
        count = interval_count_min[interval]
        interval_term = 'min'
    else :
        count = interval_count_year[interval]
        interval_term = 'year'

    return count, interval_term

def calc_last_day(year, month):
    next_month = datetime.date(int(year), month, 1).replace(day=28) + datetime.timedelta(days=4)
    return next_month - datetime.timedelta(days=next_month.day)

def int_to_str(num) :
    if len(str(num)) < 2:
        num = "0" + str(num)
    else:
        num = str(num)

    return num

def coin_data(ticker, interval, count, date_y, date_m='01', date_d='01'):
    time.sleep(0.25)

    to = f'{date_y}-{date_m}-{date_d} 00:00'
    df = pyupbit.get_ohlcv(ticker=ticker, interval=interval, to=to, count=count)

    try:
        save_path = "./data/" + str(date_y) + "_" + date_m
        save_name = to.split(' ')[0]
        os.makedirs(save_path, exist_ok=True)
        df.to_csv(save_path + '/' + str(save_name) + '.csv')

    except:
        print("None data : ", to)

def minutes_interval(ticker, interval, count, years) :
    over = False
    for year in years:
        print("current year : " + year)
        start_month = 10 if year == '2017' else 1

        for month in tqdm(range(start_month, 13)):
            last_day = calc_last_day(year, month)
            month = int_to_str(month)

            for day in range(1, last_day.day + 1):
                day = int_to_str(day)

                if str(datetime.date.today()) == f'{year}-{month}-{day}':
                    over = True
                    break

                coin_data(ticker, interval, count, year, date_m=month, date_d=day)

            if over == True:
                break

def year_interval(ticker, interval, count, years) :
    for year in years :
        print("current year : " + year)
        coin_data(ticker, interval, count, year)

if __name__ == '__main__' :

    parser = argparse.ArgumentParser()
    parser.add_argument('--ticker', type=str, required=True,
                        help='name of coin')
    parser.add_argument('--interval', type=str, required=True,
                        help='interval')

    args = parser.parse_args()

    ticker = args.ticker
    interval = args.interval
    count, interval_term = calc_count(interval)

    current_year = datetime.datetime.today().year
    years = [str(year) for year in range(2017, current_year + 1)]

    if interval_term == 'min' :
        minutes_interval(ticker, interval, count, years)

    else :
        year_interval(ticker, interval, count, years)


import pytest

from .conftest import run_pipeline, CONFIGS_DIR

@pytest.mark.forked
def test_bare_config():
    out = run_pipeline(
        "bare_config",
        gets=["a"],
        args=[f"@{CONFIGS_DIR / 'bare.toml'}"],
    )
    assert out.strip() == "a = 1"

    out = run_pipeline(
        "bare_config",
        gets=["a"],
    )
    assert out.strip() == "a = 10"

@pytest.mark.forked
def test_process_with_config():
    out = run_pipeline(
        "process_with_config",
        gets=["Process.envs.x", "Process.envs.y"],
        args=[f"@{CONFIGS_DIR / 'process_with_config.toml'}"],
    )
    assert "Process.envs.x = 1" in out
    assert "Process.envs.y = 2" in out


# -*- coding: utf-8 -*-
#This part is to update the gui-config.json with the decoded SSR URL.
import json

def updateConfig(newConfig):
    #Load file
    with open('gui-config.json') as json_data_file:
        data = json.load(json_data_file)

    #Begin update for the configuration
    for keys, values in newConfig.items():
        for config in data['configs']:
            config[keys] = values

    #Write file
    with open('gui-config.json', 'w') as outfile:
        json.dump(data, outfile, indent = 2)


import sys
from cx_Freeze import setup, Executable

# Chemin de votre fichier principal Python (main.py)
main_script = 'main.py'

# Options de configuration pour l'exécutable
build_exe_options = {
    'packages': ['tkinter', 'gui', 'PIL', ],  # Liste des packages utilisés par votre application
    'excludes': [],  # Liste des modules à exclure
    'include_files': [('assets', 'assets')]  # Liste des fichiers supplémentaires requis (images, etc.)
}

# Création de l'exécutable
base = None
if sys.platform == 'win32':
    base = 'Win32GUI'  # Si vous voulez une application sans console sur Windows

executable = Executable(script=main_script, base=base)

# Configuration de la construction
setup(
    name='projet_football',
    version='1.0',
    description='application permettant la création de schéma de comoosition de football',
    options={'build_exe': build_exe_options},
    executables=[executable]
)