import glob
import logging
import os
import time

import numpy as np
from astropy.io import fits
from astropy.stats import biweight_location
from multiprocess import Process, Queue
# Then use system installed version instead
from ppxf.ppxf import ppxf
from printStatus import printStatus

from gistPipeline.auxiliary import _auxiliary
from gistPipeline.prepareTemplates import _prepareTemplates

# Physical constants
C = 299792.458  # speed of light in km/s

"""
PURPOSE:
  This module performs the extraction of non-parametric star-formation histories
  by full-spectral fitting.  Basically, it acts as an interface between pipeline
  and the pPXF routine from Cappellari & Emsellem 2004
  (ui.adsabs.harvard.edu/?#abs/2004PASP..116..138C;
  ui.adsabs.harvard.edu/?#abs/2017MNRAS.466..798C).
"""

def robust_sigma(y, zero=False):
     """
     Biweight estimate of the scale (standard deviation).
     Implements the approach described in
     "Understanding Robust and Exploratory Data Analysis"
     Hoaglin, Mosteller, Tukey ed., 1983, Chapter 12B, pg. 417
     Added for sigma-clipping method
     """
     y = np.ravel(y)
     d = y if zero else y - np.median(y)

     mad = np.median(np.abs(d))
     u2 = (d/(9.0*mad))**2  # c = 9
     good = u2 < 1.0
     u1 = 1.0 - u2[good]
     num = y.size * ((d[good]*u1**2)**2).sum()
     den = (u1*(1.0 - 5.0*u2[good])).sum()
     sigma = np.sqrt(num/(den*(den - 1.0)))  # see note in above reference

     return sigma

def workerPPXF(inQueue, outQueue):
    """
    Defines the worker process of the parallelisation with multiprocessing.Queue
    and multiprocessing.Process.
    """

    for (
        templates,
        galaxy,
        noise,
        velscale,
        start,
        goodPixels_sfh,
        mom,
        offset,
        degree,
        mdeg,
        regul_err,
        doclean,
        fixed,
        velscale_ratio,
        npix,
        ncomb,
        nbins,
        i,
        optimal_template_in,
    ) in iter(inQueue.get,'STOP'):
        (
            sol,
            w_row,
            bestfit,
            optimal_template,
            mc_results,
            formal_error,
            spectral_mask
        ) = run_ppxf(templates,
            galaxy,
            noise,
            velscale,
            start,
            goodPixels_sfh,
            mom,
            offset,
            degree,
            mdeg,
            regul_err,
            doclean,
            fixed,
            velscale_ratio,
            npix,
            ncomb,
            nbins,
            i,
            optimal_template_in,
        )

        outQueue.put(
            (
                i,
                sol,
                w_row,
                bestfit,
                optimal_template,
                mc_results,
                formal_error,
                spectral_mask,
            )
        )

def run_ppxf_firsttime(
    templates,
    log_bin_data,
    log_bin_error,
    velscale,
    start,
    goodPixels,
    nmoments,
    offset,
    degree,
    mdeg,
    regul_err,
    doclean,
    fixed,
    velscale_ratio,
    npix,
    ncomb,
    nbins,
    i,
    optimal_template_in,
):
    """
    Calls the penalised Pixel-Fitting routine from Cappellari & Emsellem 2004
    (ui.adsabs.harvard.edu/?#abs/2004PASP..116..138C;
    ui.adsabs.harvard.edu/?#abs/2017MNRAS.466..798C), in order to determine the
    non-parametric star-formation histories.
    """
        # Call PPXF for first time to get optimal template
    print("Running pPXF for the first time")
    logging.info("Using the new 3-step pPXF implementation")
    pp = ppxf(
        templates,
        log_bin_data,
        log_bin_error,
        velscale,
        start,
        goodpixels=goodPixels,
        plot=False,
        quiet=True,
        moments=nmoments,
        degree=-1,
        vsyst=offset,
        mdegree=mdeg,
        regul = 1./regul_err,
        fixed=fixed,
        velscale_ratio=velscale_ratio,
    )

    normalized_weights = pp.weights / np.sum( pp.weights )
    optimal_template   = np.zeros( templates.shape[0] )
    for j in range(0, templates.shape[1]):
        optimal_template = optimal_template + templates[:,j]*normalized_weights[j]

    return optimal_template

def run_ppxf(
    templates,
    log_bin_data,
    log_bin_error,
    velscale,
    start,
    goodPixels,
    nmoments,
    offset,
    degree,
    mdeg,
    regul_err,
    doclean,
    fixed,
    velscale_ratio,
    npix,
    ncomb,
    nbins,
    i,
    optimal_template_in,
):

    """
    Calls the penalised Pixel-Fitting routine from Cappellari & Emsellem 2004
    (ui.adsabs.harvard.edu/?#abs/2004PASP..116..138C;
    ui.adsabs.harvard.edu/?#abs/2017MNRAS.466..798C), in order to determine the
    non-parametric star-formation histories.
    """
    printStatus.progressBar(i, nbins, barLength=50)

    try:

        if len(optimal_template_in) > 1:
            # First Call PPXF - do fit and estimate noise
            # use fake noise for first iteration
            fake_noise=np.full_like(log_bin_data, 1.0)

            pp_step1 = ppxf(
                optimal_template_in,
                log_bin_data,
                fake_noise,
                velscale,
                start,
                goodpixels=goodPixels,
                plot=False,
                quiet=True,
                moments=nmoments,
                degree=-1,
                vsyst=offset,
                mdegree=mdeg,
                fixed=fixed,
                velscale_ratio=velscale_ratio,
            )
            # Find a proper estimate of the noise
            #noise_orig = biweight_location(log_bin_error[goodPixels])
            #goodpixels is one shorter than log_bin_error
            noise_orig = np.mean(log_bin_error[goodPixels])
            noise_est = robust_sigma(pp_step1.galaxy[goodPixels]-pp_step1.bestfit[goodPixels])

            # Calculate the new noise, and the sigma of the distribution.
            noise_new = log_bin_error*(noise_est/noise_orig)
            noise_new_std = robust_sigma(noise_new)

            # A temporary fix for the noise issue where a single high S/N spaxel causes clipping of the entire spectrum
            noise_new[np.where(noise_new <= noise_est-noise_new_std)] = noise_est

            ################ 2 ##################
            # Second Call PPXF - use best-fitting template, determine outliers
            if doclean == True:
                pp_step2 = ppxf(
                    optimal_template_in,
                    log_bin_data,
                    noise_new,
                    velscale,
                    start,
                    goodpixels=goodPixels,
                    plot=False,
                    quiet=True,
                    moments=nmoments,
                    degree=-1,
                    vsyst=offset,
                    mdegree=mdeg,
                    fixed=fixed,
                    velscale_ratio=velscale_ratio,
                    clean=True,
                )

                # update goodpixels
                goodPixels = pp_step2.goodpixels

                # repeat noise scaling # Find a proper estimate of the noise
                noise_orig = biweight_location(log_bin_error[goodPixels])
                noise_est = robust_sigma(pp_step1.galaxy[goodPixels]-pp_step2.bestfit[goodPixels])

                # Calculate the new noise, and the sigma of the distribution.
                noise_new = log_bin_error*(noise_est/noise_orig)
                noise_new_std = robust_sigma(noise_new)

                # A temporary fix for the noise issue where a single high S/N spaxel causes clipping of the entire spectrum
                noise_new[np.where(noise_new <= noise_est-noise_new_std)] = noise_est

            ################ 3 ##################
            # Third Call PPXF - use all templates, get best-fit
            pp = ppxf(
                templates,
                log_bin_data,
                noise_new,
                velscale,
                start,
                goodpixels=goodPixels,
                plot=False,
                quiet=True,
                moments=nmoments,
                degree=-1,
                vsyst=offset,
                mdegree=mdeg,
                regul = 1./regul_err,
                fixed=fixed,
                velscale_ratio=velscale_ratio,
            )

        #update goodpixels again
        goodPixels = pp.goodpixels

        #make spectral mask
        spectral_mask = np.full_like(log_bin_data, 0.0)
        spectral_mask[goodPixels] = 1.0

        # Make the unconvolved optimal stellar template
        normalized_weights = pp.weights / np.sum( pp.weights )
        optimal_template   = np.zeros( templates.shape[0] )
        for j in range(0, templates.shape[1]):
            optimal_template = optimal_template + templates[:,j]*normalized_weights[j]

        # Correct the formal errors assuming that the fit is good
        formal_error = pp.error * np.sqrt(pp.chi2)

        weights = pp.weights.reshape(templates.shape[1:])/pp.weights.sum()
        w_row   = np.reshape(weights, ncomb)

        # # Do MC-Simulations - Amelia - this is not currently implemented. Add back in later.
        # sol_MC     = np.zeros((nsims,nmoments))
        mc_results = np.zeros(nmoments)
        #
        # for o in range(0, nsims):
        #     # Add noise to bestfit:
        #     #   - Draw random numbers from normal distribution with mean of 0 and sigma of 1 (np.random.normal(0,1,npix)
        #     #   - standard deviation( (galaxy spectrum - bestfit)[goodpix] )
        #     noisy_bestfit = pp.bestfit  +  np.random.normal(0, 1, len(log_bin_data)) * np.std( log_bin_data[goodPixels] - pp.bestfit[goodPixels] )
        #
        #     mc = ppxf(templates, noisy_bestfit, log_bin_error, velscale, start, goodpixels=goodPixels, plot=False, \
        #             quiet=True, moments=nmoments, degree=-1, mdegree=mdeg, velscale_ratio=velscale_ratio, vsyst=offset, bias=0.0)
        #     sol_MC[o,:] = mc.sol[:]
        #
        # if nsims != 0:
        #     mc_results = np.nanstd( sol_MC, axis=0 )
        # print(pp.sol[:])

        return(pp.sol[:], w_row, pp.bestfit, optimal_template, mc_results, formal_error, spectral_mask) # AMELIA: do I neeed the pp.reddening?

    except:
        return( np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan )

# ## *****************************************************************************
# #        noise_i = noise_i * np.sqrt(  / len(goodPixels) )
# #        regul_err =
#
#         pp = ppxf(templates, galaxy_i, noise_i, velscale, start, goodpixels=goodPixels, plot=False, quiet=True,\
#               moments=nmom, degree=-1, vsyst=dv, mdegree=mdeg, regul=1./regul_err, fixed=fixed, velscale_ratio=velscale_ratio)
#
# #        if i == 0:
# #            print()
# #            print( i, pp.chi2 )
# #            print( len( goodPixels ) )
# #            print( np.sqrt(2 * len(goodPixels)) )
# #            print()
#
#         weights = pp.weights.reshape(templates.shape[1:])/pp.weights.sum()
#         w_row   = np.reshape(weights, ncomb)
#
#         # Correct the formal errors assuming that the fit is good
#         formal_error = pp.error * np.sqrt(pp.chi2)
#
#         return(pp.sol, w_row, pp.bestfit, formal_error)
#
#     except:
#         return(np.nan, np.nan, np.nan, np.nan)

def mean_agemetalalpha(w_row, ageGrid, metalGrid, alphaGrid, nbins):
    """
    Calculate the mean age, metallicity and alpha enhancement in each bin.
    """
    mean = np.zeros( (nbins,3) ); mean[:,:] = np.nan

    for i in range( nbins ):
        mean[i,0] = np.sum(w_row[i] * ageGrid.ravel())   / np.sum(w_row[i])
        mean[i,1] = np.sum(w_row[i] * metalGrid.ravel()) / np.sum(w_row[i])
        mean[i,2] = np.sum(w_row[i] * alphaGrid.ravel()) / np.sum(w_row[i])

    return(mean)

def save_sfh(
    mean_result,
    ppxf_result,
    w_row,
    mc_results,
    formal_error,
    logAge_grid,
    metal_grid,
    alpha_grid,
    ppxf_bestfit,
    logLam,
    goodPixels,
    velscale,
    logLam1,
    ncomb,
    nAges,
    nMetal,
    nAlpha,
    npix,
    config,
    spectral_mask,
    optimal_template_comb,
):
    """ Save all results to disk. """

    # ========================
    # SAVE KINEMATICS
    outfits_sfh = (
        os.path.join(config["GENERAL"]["OUTPUT"], config["GENERAL"]["RUN_ID"])
        + "_sfh.fits"
    )
    printStatus.running("Writing: " + config["GENERAL"]["RUN_ID"] + "_sfh.fits")

    # Primary HDU
    priHDU = fits.PrimaryHDU()

    # Table HDU with stellar kinematics
    cols = []
    cols.append(fits.Column(name="AGE", format="D", array=mean_result[:, 0]))
    cols.append(fits.Column(name="METAL", format="D", array=mean_result[:, 1]))
    cols.append(fits.Column(name="ALPHA", format="D", array=mean_result[:, 2]))

    if config["SFH"]["FIXED"] == False:
        cols.append(fits.Column(name="V", format="D", array=kin[:, 0]))
        cols.append(fits.Column(name="SIGMA", format="D", array=kin[:, 1]))
        if np.any(kin[:, 2]) != 0:
            cols.append(fits.Column(name="H3", format="D", array=kin[:, 2]))
        if np.any(kin[:, 3]) != 0:
            cols.append(fits.Column(name="H4", format="D", array=kin[:, 3]))
        if np.any(kin[:, 4]) != 0:
            cols.append(fits.Column(name="H5", format="D", array=kin[:, 4]))
        if np.any(kin[:, 5]) != 0:
            cols.append(fits.Column(name="H6", format="D", array=kin[:, 5]))

        cols.append(
            fits.Column(name="FORM_ERR_V", format="D", array=formal_error[:, 0])
        )
        cols.append(
            fits.Column(name="FORM_ERR_SIGMA", format="D", array=formal_error[:, 1])
        )
        if np.any(formal_error[:, 2]) != 0:
            cols.append(
                fits.Column(name="FORM_ERR_H3", format="D", array=formal_error[:, 2])
            )
        if np.any(formal_error[:, 3]) != 0:
            cols.append(
                fits.Column(name="FORM_ERR_H4", format="D", array=formal_error[:, 3])
            )
        if np.any(formal_error[:, 4]) != 0:
            cols.append(
                fits.Column(name="FORM_ERR_H5", format="D", array=formal_error[:, 4])
            )
        if np.any(formal_error[:, 5]) != 0:
            cols.append(
                fits.Column(name="FORM_ERR_H6", format="D", array=formal_error[:, 5])
            )

    dataHDU = fits.BinTableHDU.from_columns(fits.ColDefs(cols))
    dataHDU.name = "SFH"

    # Create HDU list and write to file
    priHDU = _auxiliary.saveConfigToHeader(priHDU, config["SFH"])
    dataHDU = _auxiliary.saveConfigToHeader(dataHDU, config["SFH"])
    HDUList = fits.HDUList([priHDU, dataHDU])
    HDUList.writeto(outfits_sfh, overwrite=True)

    printStatus.updateDone("Writing: " + config["GENERAL"]["RUN_ID"] + "_sfh.fits")
    logging.info("Wrote: " + outfits_sfh)

    # ========================
    # SAVE WEIGHTS AND GRID
    outfits_sfh = (
        os.path.join(config["GENERAL"]["OUTPUT"], config["GENERAL"]["RUN_ID"])
        + "_sfh-weights.fits"
    )
    printStatus.running("Writing: " + config["GENERAL"]["RUN_ID"] + "_sfh-weights.fits")

    # Primary HDU
    priHDU = fits.PrimaryHDU()

    # Table HDU with weights
    cols = []
    cols.append(
        fits.Column(name="WEIGHTS", format=str(w_row.shape[1]) + "D", array=w_row)
    )
    dataHDU = fits.BinTableHDU.from_columns(fits.ColDefs(cols))
    dataHDU.name = "WEIGHTS"

    logAge_row = np.reshape(logAge_grid, ncomb)
    metal_row = np.reshape(metal_grid, ncomb)
    alpha_row = np.reshape(alpha_grid, ncomb)

    # Table HDU with grids
    cols = []
    cols.append(fits.Column(name="LOGAGE", format="D", array=logAge_row))
    cols.append(fits.Column(name="METAL", format="D", array=metal_row))
    cols.append(fits.Column(name="ALPHA", format="D", array=alpha_row))
    gridHDU = fits.BinTableHDU.from_columns(fits.ColDefs(cols))
    gridHDU.name = "GRID"

    # Create HDU list and write to file
    priHDU = _auxiliary.saveConfigToHeader(priHDU, config["SFH"])
    dataHDU = _auxiliary.saveConfigToHeader(dataHDU, config["SFH"])
    gridHDU = _auxiliary.saveConfigToHeader(gridHDU, config["SFH"])
    HDUList = fits.HDUList([priHDU, dataHDU, gridHDU])
    HDUList.writeto(outfits_sfh, overwrite=True)

    fits.setval(outfits_sfh, "NAGES", value=nAges)
    fits.setval(outfits_sfh, "NMETAL", value=nMetal)
    fits.setval(outfits_sfh, "NALPHA", value=nAlpha)

    printStatus.updateDone(
        "Writing: " + config["GENERAL"]["RUN_ID"] + "_sfh-weights.fits"
    )
    logging.info("Wrote: " + outfits_sfh)

    # ========================
    # SAVE BESTFIT
    outfits_sfh = (
        os.path.join(config["GENERAL"]["OUTPUT"], config["GENERAL"]["RUN_ID"])
        + "_sfh-bestfit.fits"
    )
    printStatus.running("Writing: " + config["GENERAL"]["RUN_ID"] + "_sfh-bestfit.fits")

    # Primary HDU
    priHDU = fits.PrimaryHDU()

    # Table HDU with SFH bestfit
    cols = []
    cols.append( fits.Column(name='BESTFIT', format=str(npix)+'D', array=ppxf_bestfit ))

    dataHDU = fits.BinTableHDU.from_columns(fits.ColDefs(cols))
    dataHDU.name = "BESTFIT"

    # Table HDU with SFH logLam
    cols = []

    cols.append( fits.Column(name='LOGLAM', format='D', array=logLam ))

    logLamHDU = fits.BinTableHDU.from_columns(fits.ColDefs(cols))
    logLamHDU.name = "LOGLAM"

    # Table HDU with SFH goodpixels
    cols = []
    cols.append(fits.Column(name="GOODPIX", format="J", array=goodPixels))
    goodpixHDU = fits.BinTableHDU.from_columns(fits.ColDefs(cols))
    goodpixHDU.name = "GOODPIX"

    # Create HDU list and write to file
    priHDU = _auxiliary.saveConfigToHeader(priHDU, config["SFH"])
    dataHDU = _auxiliary.saveConfigToHeader(dataHDU, config["SFH"])
    logLamHDU = _auxiliary.saveConfigToHeader(logLamHDU, config["SFH"])
    goodpixHDU = _auxiliary.saveConfigToHeader(goodpixHDU, config["SFH"])
    HDUList = fits.HDUList([priHDU, dataHDU, logLamHDU, goodpixHDU])
    HDUList.writeto(outfits_sfh, overwrite=True)

    fits.setval(outfits_sfh, "VELSCALE", value=velscale)
    fits.setval(outfits_sfh, "CRPIX1", value=1.0)
    fits.setval(outfits_sfh, "CRVAL1", value=logLam1[0])
    fits.setval(outfits_sfh, "CDELT1", value=logLam1[1] - logLam1[0])

    printStatus.updateDone(
        "Writing: " + config["GENERAL"]["RUN_ID"] + "_sfh-bestfit.fits"
    )
    logging.info("Wrote: " + outfits_sfh)

def extractStarFormationHistories(config):
    """
    Starts the computation of non-parametric star-formation histories with
    pPXF.  A spectral template library sorted in a three-dimensional grid of
    age, metallicity, and alpha-enhancement is loaded.  Emission-subtracted
    spectra are used for the fit. An according emission-line mask is
    constructed. The stellar kinematics can or cannot be fixed to those obtained
    with a run of unregularized pPXF and the analysis started.  Results are
    saved to disk and the plotting routines called.
    """

    # Read LSF information
    LSF_Data, LSF_Templates = _auxiliary.getLSF(config, "SFH")

    # Prepare template library
    velscale = fits.open(
        os.path.join(config["GENERAL"]["OUTPUT"], config["GENERAL"]["RUN_ID"])
        + "_BinSpectra.fits"
    )[0].header["VELSCALE"]
    velscale_ratio = 2

    (
        templates,
        lamRange_temp,
        logLam_template,
        ntemplates,
        logAge_grid,
        metal_grid,
        alpha_grid,
        ncomb,
        nAges,
        nMetal,
        nAlpha,
    ) = _prepareTemplates.prepareTemplates_Module(
        config,
        config['SFH']['LMIN'],
        config['SFH']['LMAX'],
        velscale/velscale_ratio,
        LSF_Data,
        LSF_Templates,
        'SFH',
        sortInGrid=True,
    )
    templates = templates.reshape( (templates.shape[0], ntemplates) )

    # Read spectra
    if (
        os.path.isfile(
            os.path.join(config["GENERAL"]["OUTPUT"], config["GENERAL"]["RUN_ID"])
            + "_ppxf-bestfit-emlines_BIN.fits"
        )
        == True
    ):
        logging.info(
            "Using emission-subtracted spectra at "
            + os.path.join(config["GENERAL"]["OUTPUT"], config["GENERAL"]["RUN_ID"])
            + "__ppxf-bestfit-emlines_BIN.fits"
        )
        printStatus.done("Using emission-subtracted spectra")

        hdu = fits.open(
            os.path.join(config['GENERAL']['OUTPUT'],
            config['GENERAL']['RUN_ID'])+'_gas-cleaned_'+config['GAS']['LEVEL']+'.fits'
        )
        # Adding a bit in to also load the BinSpectra.fits to grab the error spectrum, even if using the cleaned gas specrum
        # But sometimes this isn't always the right shape. So really, you want the error saved to the _gas_cleaned_BIN.fits hdu
        #hdu2 = fits.open(os.path.join(config['GENERAL']['OUTPUT'],config['GENERAL']['RUN_ID'])+'_BinSpectra.fits')

    else:
        logging.info(
            "Using regular spectra without any emission-correction at "
            + os.path.join(config["GENERAL"]["OUTPUT"], config["GENERAL"]["RUN_ID"])
            + "_BinSpectra.fits"
        )
        printStatus.done("Using regular spectra without any emission-correction")
        hdu = fits.open(
            os.path.join(config["GENERAL"]["OUTPUT"], config["GENERAL"]["RUN_ID"])
            + "_BinSpectra.fits"
        )

    galaxy = np.array( hdu[1].data.SPEC )
    logLam = hdu[2].data.LOGLAM
    idx_lam = np.where( np.logical_and( np.exp(logLam) > config['SFH']['LMIN'], np.exp(logLam) < config['SFH']['LMAX'] ) )[0]
    galaxy = galaxy[:,idx_lam]
    #galaxy = galaxy/np.median(galaxy) # Amelia added to normalise normalize flux. Do we use this again?
    logLam = logLam[idx_lam]
    nbins = galaxy.shape[0]
    npix = galaxy.shape[1]
    ubins = np.arange(0, nbins)
    noise = np.full(npix, config['SFH']['NOISE'])
    dv = (np.log(lamRange_temp[0]) - logLam[0])*C
    #bin_err = np.array( hdu2[1].data.ESPEC.T ) #This will almost certainly not work, as galaxy array isn't transposed
    bin_err = np.array( hdu[1].data.ESPEC.T ) #This will almost certainly not work, as galaxy array isn't transposed. Does this still need to be transposed?
    bin_data = np.array( hdu[1].data.SPEC.T ) # Amelia this doens't bode well
    bin_data = bin_data[idx_lam,:]
    bin_err = bin_err[idx_lam,:]
    # Last preparatory steps
    offset = (logLam_template[0] - logLam[0])*C
    #noise = np.ones((npix,nbins))
    noise = bin_err # is actual noise, not variance
    nsims = config['SFH']['MC_PPXF']

    # Implementation of switch FIXED
    # Do fix kinematics to those obtained previously
    if config["SFH"]["FIXED"] == True:
        logging.info("Stellar kinematics are FIXED to the results obtained before.")
        # Set fixed option to True
        fixed = [True] * config["KIN"]["MOM"]

        # Read PPXF results
        ppxf_data = fits.open(
            os.path.join(config["GENERAL"]["OUTPUT"], config["GENERAL"]["RUN_ID"])
            + "_kin.fits"
        )[1].data
        start = np.zeros((nbins, config["KIN"]["MOM"]))
        for i in range(nbins):
            start[i, :] = np.array(ppxf_data[i][: config["KIN"]["MOM"]])

    # Do *NOT* fix kinematics to those obtained previously
    elif config["SFH"]["FIXED"] == False:
        logging.info(
            "Stellar kinematics are NOT FIXED to the results obtained before but extracted simultaneously with the stellar population properties."
        )
        # Set fixed option to False and use initial guess from Config-file
        fixed = None
        start = np.zeros((nbins, 2))
        for i in range(nbins):
            start[i, :] = np.array([0.0, config["KIN"]["SIGMA"]])

    # Define goodpixels
    goodPixels_sfh = _auxiliary.spectralMasking(config, config['SFH']['SPEC_MASK'], logLam)

    # Define output arrays
    ppxf_result = np.zeros((nbins,6    ))
    w_row = np.zeros((nbins,ncomb))
    ppxf_bestfit = np.zeros((nbins,npix))
    optimal_template = np.zeros((nbins,templates.shape[0]))
    mc_results = np.zeros((nbins,6))
    formal_error = np.zeros((nbins,6))
    spectral_mask = np.zeros((nbins,bin_data.shape[0]))

    # ====================
    # Run PPXF once on combined mean spectrum to get a single optimal template
    comb_spec = np.nanmean(bin_data[:,:],axis=1)
    comb_espec = np.nanmean(bin_err[:,:],axis=1)
    #comb_spec = comb_spec/np.nanmedian(comb_spec) # Amelia added to mormalise normalize spectrum
    #comb_espec = comb_espec/np.nanmedian(comb_espec) # and the error spectrum
    optimal_template_init = [0]

    optimal_template_out = run_ppxf_firsttime(
        templates,
        comb_spec ,
        comb_espec,
        velscale,
        start[0,:],
        goodPixels_sfh,
        config['SFH']['MOM'],
        offset,-1,
        config['SFH']['MDEG'],
        config['SFH']['REGUL_ERR'],
        config["SFH"]["DOCLEAN"],
        fixed,
        velscale_ratio,
        ncomb,
        nsims,
        nbins,
        0,
        optimal_template_init,
    )

    # now define the optimal template that we'll use throughout
    optimal_template_comb = optimal_template_out

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

    # ====================
    # Run PPXF
    start_time = time.time()
    if config["GENERAL"]["PARALLEL"] == True:
        printStatus.running("Running PPXF in parallel mode")
        logging.info("Running PPXF in parallel mode")

        # Create Queues
        inQueue = Queue()
        outQueue = Queue()

        # Create worker processes
        ps = [
            Process(target=workerPPXF, args=(inQueue, outQueue))
            for _ in range(config["GENERAL"]["NCPU"])
        ]

        # Start worker processes
        for p in ps:
            p.start()

        # Fill the queue
        for i in range(nbins):
            inQueue.put(
                (
                    templates,
                    bin_data[:,i],
                    noise[:,i],
                    velscale,
                    start[i,:],
                    goodPixels_sfh,
                    config['SFH']['MOM'],
                    offset,
                    -1,
                    config['SFH']['MDEG'],
                    config['SFH']['REGUL_ERR'],
                    config["SFH"]["DOCLEAN"],
                    fixed,
                    velscale_ratio,
                    npix,
                    ncomb,
                    nbins,
                    i,
                    optimal_template_comb,
                )
            )

        # now get the results with indices
        ppxf_tmp = [outQueue.get() for _ in range(nbins)]

        # send stop signal to stop iteration
        for _ in range(config["GENERAL"]["NCPU"]):
            inQueue.put("STOP")

        # stop processes
        for p in ps:
            p.join()

        # Get output
        index = np.zeros(nbins)
        for i in range(0, nbins):

            index[i] = ppxf_tmp[i][0]
            ppxf_result[i,:config['SFH']['MOM']] = ppxf_tmp[i][1]
            w_row[i,:] = ppxf_tmp[i][2]
            ppxf_bestfit[i,:] = ppxf_tmp[i][3]
            optimal_template[i,:] = ppxf_tmp[i][4]
            mc_results[i,:config['SFH']['MOM']] = ppxf_tmp[i][5]
            formal_error[i,:config['SFH']['MOM']] = ppxf_tmp[i][6]
            spectral_mask[i,:] = ppxf_tmp[i][7]

        # Sort output
        argidx = np.argsort( index )
        ppxf_result = ppxf_result[argidx,:]
        w_row = w_row[argidx,:]
        ppxf_bestfit = ppxf_bestfit[argidx,:]
        optimal_template = optimal_template[argidx,:]
        mc_results = mc_results[argidx,:]
        formal_error = formal_error[argidx,:]
        spectral_mask = spectral_mask[argidx,:]

        printStatus.updateDone("Running PPXF in parallel mode", progressbar=True)

    if config['GENERAL']['PARALLEL'] == False: # Amelia you haven't tested this yet. Come back to.
        printStatus.running("Running PPXF in serial mode")
        logging.info("Running PPXF in serial mode")
        for i in range(nbins):
            (
                kin[i,:config['SFH']['MOM']],
                w_row[i,:],
                bestfit[i,:],
                formal_error[i,:config['SFH']['MOM']],
            ) = run_ppxf(
                templates,
                galaxy[i,:],
                noise,
                velscale,
                start[i,:],
                goodPixels_sfh,
                config['SFH']['MOM'],
                dv,
                -1,
                config['SFH']['MDEG'],
                config['SFH']['REGUL_ERR'],
                config["KIN"]["DOCLEAN"],
                fixed,
                velscale_ratio,
                npix,
                ncomb,
                nbins,
                i,
                optimal_template_in,
            )
        printStatus.updateDone("Running PPXF in serial mode", progressbar=True)

    print(
        "             Running PPXF on %s spectra took %.2fs using %i cores"
        % (nbins, time.time() - start_time, config["GENERAL"]["NCPU"])
    )
    logging.info(
        "Running PPXF on %s spectra took %.2fs using %i cores"
        % (nbins, time.time() - start_time, config["GENERAL"]["NCPU"])
    )

    # Check for exceptions which occurred during the analysis
    idx_error = np.where( np.isnan( ppxf_result[:,0] ) == True )[0]

    if len(idx_error) != 0:
        printStatus.warning(
            "There was a problem in the analysis of the spectra with the following BINID's: "
        )
        print("             " + str(idx_error))
        logging.warning(
            "There was a problem in the analysis of the spectra with the following BINID's: "
            + str(idx_error)
        )
    else:
        print("             " + "There were no problems in the analysis.")
        logging.info("There were no problems in the analysis.")
    print("")

    # Calculate mean age, metallicity and alpha
    mean_results = mean_agemetalalpha(
        w_row, 10**logAge_grid, metal_grid, alpha_grid, nbins
    )

    # Save to file

    save_sfh(
        mean_results,
        ppxf_result,
        w_row,
        mc_results,
        formal_error,
        logAge_grid,
        metal_grid,
        alpha_grid,
        ppxf_bestfit,
        logLam,
        goodPixels_sfh,
        velscale,
        logLam,
        ncomb,
        nAges,
        nMetal,
        nAlpha,
        npix,
        config,
        spectral_mask,
        optimal_template_comb,
    )

    # Return
    return None


import numpy as np
import torch
import torch.nn as nn
from torch import nn

class InvLM(nn.Module):
    def __init__(self, input_dim=10, output_shape=(1, 28, 28), channel=1):
        super(InvLM, self).__init__()
        self.output_shape = output_shape
        self.fc = nn.Linear(input_dim, np.prod(output_shape))

    def forward(self, x):
        batch_size = x.shape[0]
        x = x.reshape(batch_size, -1)
        return self.fc(x).reshape((batch_size,) + self.output_shape)

class InvCNN(nn.Module):
    def __init__(self, input_dim=40, output_shape=None, channel=3):
        super(InvCNN, self).__init__()
        self.ct1 = nn.ConvTranspose2d(input_dim, 1024, (4, 4), stride=(1, 1))
        self.bn1 = torch.nn.BatchNorm2d(
            1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True
        )
        self.ct2 = nn.ConvTranspose2d(1024, 512, (4, 4), stride=(2, 2), padding=(1, 1))
        self.bn2 = torch.nn.BatchNorm2d(
            512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True
        )
        self.ct3 = nn.ConvTranspose2d(512, 256, (4, 4), stride=(2, 2), padding=(1, 1))
        self.bn3 = torch.nn.BatchNorm2d(
            256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True
        )
        self.ct4 = nn.ConvTranspose2d(256, 128, (4, 4), stride=(2, 2), padding=(1, 1))
        self.bn4 = torch.nn.BatchNorm2d(
            128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True
        )
        self.ct5 = nn.ConvTranspose2d(
            128, channel, (4, 4), stride=(2, 2), padding=(1, 1)
        )

    def forward(self, x):
        x = self.ct1(x)
        x = self.bn1(x)
        x = torch.tanh(x)
        x = self.ct2(x)
        x = self.bn2(x)
        x = torch.tanh(x)
        x = self.ct3(x)
        x = self.bn3(x)
        x = torch.tanh(x)
        x = self.ct4(x)
        x = self.bn4(x)
        x = torch.tanh(x)
        x = self.ct5(x)
        x = torch.tanh(x)
        return x

def get_invmodel_class(invmodel_type):
    if invmodel_type == "InvCNN":
        model_class = InvCNN
    elif invmodel_type == "InvLM":
        model_class = InvLM
    else:
        raise NotImplementedError(f"{invmodel_type} is not supported.")
    return model_class


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from mathutils import Quaternion
import csv
from collections import namedtuple
import numpy as np
from compare_traj.msg_to_traj import Msg_to_Traj
from compare_traj.blender_to_traj import Blender_to_Traj
from scipy.spatial.transform import Rotation as R

class VSLAM_to_Traj:
    def __init__(self, pathin):
        self.pathin = pathin

    def csv_to_pose(self):
        camPose_list = []
        with open(self.pathin) as cam_csv:
            camdata = csv.reader(cam_csv)
            headers = next(camdata)
            Row = namedtuple('Row', headers)
            for r in camdata:
                row = Row(*r)
                for temp in row:
                    camPose_list.append(float(temp))

        pose = np.array(camPose_list).reshape(-1, 9)
        if 'noisy' in self.pathin:
            pose[:, 2] = -pose[:, 2]

        return pose

    def get_trajectory(self, pose_wc):
        trans_wc = pose_wc[:, :3]
        quat_wc = pose_wc[:, 3:]
        # quat_wc[:,[0,1,2,3]] = quat_wc[:,[3,0,1,2]]
        pose_w = []
        for i, _ in enumerate(pose_wc):
            q = Quaternion(quat_wc[i])
            rot = np.array(q.to_matrix())
            # q = R.from_quat(quat_wc[i])
            # rot = np.array(R.as_matrix(q))
            # pose_w.append(rot)
            pose_w.append(rot @ trans_wc[i])
        return np.array(pose_w)

    def cal_baseline(self, traj):
        diff_list = np.diff(traj, axis=0)
        dis_seq = []
        for i in range(len(diff_list)):
            dis_seq.append(np.linalg.norm(diff_list[i]))
            # print('d{order} = {num:.2f}m'.format(order=i, num=dis_list[i-1]))
        return np.array(dis_seq)

    def cal_velocity(self,traj):
        diff_list = np.diff(traj, axis=0)
        return diff_list * 30

    def plot_pose(self, pose_w, display3D=False):
        fig = plt.figure()

        if display3D:
            ax = fig.gca(projection='3d')
            ax.plot(pose_w[:, 0], pose_w[:, 1], pose_w[:, 2], "o-", label='pose points')
            ax.scatter(pose_w[0, 0], pose_w[0, 1], pose_w[0, 2], color="red")
            ax.scatter(pose_w[10, 0], pose_w[10, 1], pose_w[10, 2], color="green")
            ax.scatter(pose_w[-1, 0], pose_w[-1, 1], pose_w[-1, 2], color="purple")
            ax.set_zlabel('Z')
        else:
            ax = fig.gca()
            ax.plot(pose_w[:, 0], pose_w[:, 1], "o-", label='pose points')

            ax.plot(pose_w[0, 0], pose_w[0, 1], "X", color="red")
            ax.plot(pose_w[10, 0], pose_w[10, 1], "X", color="green")
            ax.plot(pose_w[-1, 0], pose_w[-1, 1], "X", color="purple")

        ax.legend()  # 画一条空间曲线
        ax.set_xlabel('X')
        ax.set_ylabel('Y')
        # ax.set_xlim(-5, 12)
        # ax.set_ylim(-6, 12)
        # ax.set_zlim(-6, 6)
        # fig.colorbar(surf, shrink=0.5, aspect=5)
        plt.show()

    def plot_baseline(self, baseline):
        # baseline = np.delete(baseline, 0)

        fig = plt.figure()
        ax1 = fig.add_subplot(121)
        ax1.plot(baseline, color='tab:red', label='ground truth')
        ax1.set_xlabel('frame', fontsize=20)
        ax1.set_ylabel('baseline', fontsize=20)
        ax1.set_title('compared OpenVSLAM and Ground Truth', fontsize=20)
        ax1.tick_params(axis='x', rotation=0, labelsize=12)
        ax1.tick_params(axis='y', rotation=0)
        ax1.grid(alpha=.4)
        ax1.grid(True)

        # # # Plot Line2 (Right Y Axis)
        ax2 = fig.add_subplot(122)
        counts, bins = np.histogram(baseline)
        edges = np.delete(bins, 0)
        res = edges.dot(counts) / len(baseline)

        ax2.hist(bins[:-1], bins, weights=counts, facecolor='tab:red', alpha=0.75)
        print('average baseline:={:.3f}m'.format(res))
        ax2.grid(alpha=.4)
        ax2.grid(True)

        # # # ax2 (right Y axis)
        ax2.set_ylabel("frequency", color='tab:blue', fontsize=20)
        ax2.tick_params(axis='y', labelcolor='tab:blue')
        ax2.set_xlabel('baseline/m', fontsize=20)
        # plt.ylim(0, 1)
        ax2.set_title("Distribution of Baseline", fontsize=22)
        # plt.tight_layout()
        plt.show()

if __name__ == '__main__':
    # path0 = "/home/taungdrier/Desktop/FAUbox/Archiv_OpenVSLAM/VSLAM_results/eval_uni_EQT_2400_15102020/"
    # filename = "eval_uni_EQT_2400_15102020.msg"
    folderpath = '../compare_traj/saved_data/from_msg_data/'
    # posecsv = "uni_EQT_traj.csv"
    posecsv = "uni_EQT_kf_traj.csv"
    fullposecsv = "frame_trajectory.csv"
    # m2t = Msg_to_Traj(path0 + filename)
    # load_data = np.load(folderpath + 'saved_eval_uni_EQT_2400_15102020.npz')
    # kf_pose_cw = load_data['keyframe_pose_cw']
    # zw = kf_pose_cw[kf_pose_cw[:, 0].argsort()]  # 按第'1'列排序
    # pose_cw = m2t.get_trajectory(zw[:, 2:])
    # v2t = VSLAM_to_Traj(path0 + posecsv)

    v2t = VSLAM_to_Traj(fullposecsv)
    full_pose = v2t.csv_to_pose()
    real_traj = v2t.get_trajectory(full_pose[:, 2:])
    # to regular view transformation
    real_traj[:, [1, 2]] = real_traj[:, [2, 1]]
    real_traj[:, 1] = -real_traj[:, 1]
    full_baseline = v2t.cal_baseline(real_traj)

    # PLOT
    v2t.plot_pose(real_traj, display3D=True)
    # v_traj = v2t.cal_velocity(real_traj)
    # v2t.plot_pose(v_traj, display3D=True)
    # v2t.plot_baseline(30 *full_baseline)


from scripts.helper import get_account, deploy_mocks
from brownie import Fundraiser, network, config

def deploy_fundraiser(account, price_feed_address):
  print(f'Deploying from account {account} with price_feed_address {price_feed_address} ...')
  fundraiser = Fundraiser.deploy(price_feed_address, {"from": account}, publish_source=config["networks"][network.show_active()].get("verify"),)
  print(f"Contract deployed to {fundraiser.address}")
  return fundraiser

def main():
  if network.show_active() != "polygon-test":
    sys.exit("Only polygon-test network is supported.")
  account = get_account()
  price_feed_address = config["networks"][network.show_active()]["matic_usd_price_feed"]
  fundraiser = deploy_fundraiser(account, price_feed_address)


import matplotlib.pyplot as plt
import math
import random

#initialize theta and R
currentTheta = 0
currentR = 0
#set up polar plt
ax = plt.subplot(111, projection="polar")
ax.set_ylim([0,30])
#enable interactive mode so the plot doesn't close and open and is non-blocking
plt.ion()
plt.show()
#just for demonstration
countingUp = True
while(1):
    #***demo stuff***
    #currentTheta = random.randrange(0,359)
    currentTheta += 5
    #currentR = random.randrange(0,10)
    if(currentR == 30):
        countingUp = False
    elif(currentR == 0):
        countingUp = True
    if(countingUp):
        currentR += 0.75
    else:
        currentR -= 0.75
    #***demo stuff end***
    #set theta and r
    theta = [0,math.radians(currentTheta)]
    r = [0,currentR]
    #clear plot and prep for redraw
    ax.cla()
    #set limit on polar plot range
    ax.set_ylim([0,30])
    #tell the user what the range is at the moment in the bottom left corner
    ax.annotate('Range: {:0.2f}'.format(currentR) + " Feet",
            xy=(currentTheta, currentR),  # theta, radius
            xytext=(0.05, 0.05),    # fraction, fraction
            textcoords='figure fraction',
    #        arrowprops=dict(facecolor='black', shrink=0.05),
            horizontalalignment='left',
            verticalalignment='bottom',
            )
    #plot first a red diamond and then a red line, both for position
    ax.plot(theta, r,'^r')
    ax.plot(theta, r,'-r')
    #give the plot a moment to breathe and do its processing
    plt.pause(0.05)


import gitlab

gitlab_client = gitlab.Gitlab.from_config("global", [".python-gitlab.cfg"])
project_ids = []
long_living_branches = []

if __name__ == "__main__":
    for project_id in project_ids:
        g_project = gitlab_client.projects.get(project_id)
        g_jobs = g_project.jobs.list(all=True)

        g_filtered_jobs = list(filter(
            lambda job: job.ref in long_living_branches and job.stage == "static-analysis" and job.status == "success",
            g_jobs))

        job_ids = ""
        for g_job in g_filtered_jobs:
            job_ids += (str(g_job.id) + ",")

            with open("data/" + str(project_id) + "/" + str(g_job.id) + ".log", "wb") as file:
                g_job.trace(streamed=True, action=file.write)

        with open("data/" + str(project_id) + "/job_ids.txt", "w") as file:
            file.write(job_ids[:-1])


from __future__ import print_function

import gevent

from opentracing.mocktracer import MockTracer
from opentracing.scope_managers.gevent import GeventScopeManager
from ..testcase import OpenTracingTestCase

class TestGevent(OpenTracingTestCase):
    def setUp(self):
        self.tracer = MockTracer(GeventScopeManager())

    def test_main(self):
        # Start a Span and let the callback-chain
        # finish it when the task is done
        with self.tracer.start_active_span('one', finish_on_close=False):
            self.submit()

        gevent.wait()

        spans = self.tracer.finished_spans()
        self.assertEqual(len(spans), 1)
        self.assertEqual(spans[0].operation_name, 'one')

        for i in range(1, 4):
            self.assertEqual(spans[0].tags.get('key%s' % i, None), str(i))

    def submit(self):
        span = self.tracer.scope_manager.active.span

        def task1():
            with self.tracer.scope_manager.activate(span, False):
                span.set_tag('key1', '1')

                def task2():
                    with self.tracer.scope_manager.activate(span, False):
                        span.set_tag('key2', '2')

                        def task3():
                            with self.tracer.scope_manager.activate(span,
                                                                    True):
                                span.set_tag('key3', '3')

                        gevent.spawn(task3)

                gevent.spawn(task2)

        gevent.spawn(task1)


#!/usr/bin/env python

# Script maps a hotkey(Ctrl-Alt-F) to a workaround (https://hex-rays.com/products/ida/news/7_2/the_mac_rundown/)
# which fixes the broken tail calls. Provides another hotkey(Ctrl-Alt-E) to overwrite the function end address,
# allowing IDA to decompile properly

import ida_kernwin
import ida_auto
import ida_funcs

CONTEXT_MENU_PATH = 'iOS Fix/'

class NoRetFix(ida_kernwin.action_handler_t):
    def __init__(self):
        ida_kernwin.action_handler_t.__init__(self)

    def activate(self, ctx):
        func_ea = ctx.cur_value
        if func_ea != BADADDR:
            func = ida_funcs.get_func(func_ea)
            if func is not None and (func.flags & FUNC_NORET) != 0:
                func.flags &= ~FUNC_NORET
                ida_funcs.update_func(func)
                ida_auto.reanalyze_callers(func.start_ea, False)
                print(f'[+] Fixed: {ida_funcs.get_func_name(func_ea)}@{hex(func_ea)}')
                return 1
        return 0

    def update(self, ctx):
        return ida_kernwin.AST_ENABLE_ALWAYS

class EndEAFix(ida_kernwin.action_handler_t):
    def __init__(self):
        ida_kernwin.action_handler_t.__init__(self)

    def activate(self, ctx):
        addr = ctx.cur_ea
        while ida_funcs.get_func(addr) == None:
            addr -= 4
        found_func = ida_funcs.get_func(addr)
        found_func_name = ida_funcs.get_func_name(found_func.start_ea)
        print(f"[+] Found function {found_func_name}")
        found_func.end_ea = ctx.cur_ea + 4 # put cursor at the last instruction
        print(f"[+] End EA for {found_func_name} set to {hex(ctx.cur_ea + 4)}")
        return 1

    def update(self, ctx):
        return ida_kernwin.AST_ENABLE_ALWAYS

class ContextHooks(ida_kernwin.UI_Hooks):
    def finish_populating_widget_popup(self, form, popup):
        tft = ida_kernwin.get_widget_type(form)
        if tft == ida_kernwin.BWN_DISASM:
            ida_kernwin.attach_action_to_popup(
                form,
                popup,
                'ios:noretfix',
                CONTEXT_MENU_PATH,
                ida_kernwin.SETMENU_INS,
            )
            ida_kernwin.attach_action_to_popup(
                form,
                popup,
                'ios:endeafix',
                CONTEXT_MENU_PATH,
                ida_kernwin.SETMENU_INS,
            )
        elif tft == ida_kernwin.BWN_PSEUDOCODE:
            pass

noretfix_desc = ida_kernwin.action_desc_t('ios:noretfix', 'Fix function broken tail', NoRetFix(), 'Ctrl+Alt+F')
endeafix_desc = ida_kernwin.action_desc_t('ios:endeafix', 'Fix end address', EndEAFix(), 'Ctrl+Alt+E')
ida_kernwin.register_action(noretfix_desc)
ida_kernwin.register_action(endeafix_desc)
hooks = ContextHooks()
hooks.hook()


import pytest
from recon.recognizer import *

def test_base_recognizer(test_texts):
    recognizer = EntityRecognizer()

    with pytest.raises(NotImplementedError):
        recognizer.labels
    with pytest.raises(NotImplementedError):
        recognizer.predict(test_texts)

def test_spacy_recognizer(nlp, test_texts):
    ruler = nlp.create_pipe("entity_ruler")
    ruler.add_patterns(
        [
            {"label": "SKILL", "pattern": "Machine learning"},
            {"label": "SKILL", "pattern": "researched"},
            {"label": "SKILL", "pattern": "AI"},
            {"label": "JOB_ROLE", "pattern": "Software Engineer"},
        ]
    )

    nlp.add_pipe(ruler)

    recognizer = SpacyEntityRecognizer(nlp)
    assert recognizer.labels == ["JOB_ROLE", "SKILL"]

    examples = list(recognizer.predict(test_texts))

    assert examples[0].text == test_texts[0]
    assert len(examples[0].spans) == 3
    assert len(examples[1].spans) == 2


from django.shortcuts import render, HttpResponse,redirect
from .models import Order, Product
from django.db.models import Sum

def index(request):
    context = {
        "all_products": Product.objects.all()
    }
    return render(request, "index.html", context)

def checkout(request):
    last=Order.objects.last()
    price=last.total_price
    full_order = Order.objects.aggregate(Sum('quantity_ordered'))['quantity_ordered__sum']
    full_price = Order.objects.aggregate(Sum('total_price'))['total_price__sum']
    context = {
        'orders':full_order,
        'total':full_price,
        'bill':price,
    }
    return render(request, "checkout.html",context)

def buy(request):
    if request.method =='POST':
        quantity_from_form = int(request.POST['quantity'])
        price_from_form = float(request.POST['price'])
        total_charge = quantity_from_form * price_from_form
        print(request.POST)
        print("Charging credit card...")
        Order.objects.create(quantity_ordered=quantity_from_form, total_price=total_charge)
        return redirect('/checkout')
    return redirect('/')


from random import random

# IMPORTANT QUESTION
class Solution:
    def findKthNumber(self, n: int, k: int) -> int:
        elevens = []
        lmax = len(str(n))
        for num in range(1, n+1):
            elevens.append(self.convert(num, lmax))
        elevens.sort()
        elven = elevens[k-1]
        ans = ""
        for x in elven:
            if x == "A":
                ans += "9"
            elif 9 >= int(x) >= 1:
                ans += str(int(x)-1)
            elif x == "0":
                break
        return int(ans)

    def convert(self, num, lmax):
        ten = str(num)
        eleven = ""
        lcur = len(ten)
        for x in range(lmax):
            if x < lcur:
                eleven += self.switch(ten[x])
            else:
                eleven += "0"
        return eleven

    def switch(self, char):
        if int(char) < 9:
            return str(int(char)+1)
        else:
            return "A"

sol = Solution()
#rint(sol.findKthNumber(13, 13))


from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from bank.models import *

# Compound Interest views here.

@login_required
def compound_interest_view(request):
    user1 = Interest_Bank_Account.objects.get(user=request.user)
    user2 = Current_Bank_Account.objects.get(user=request.user)
    interest_amount = user1.amount
    current_amount = user2.amount

    Current_Bank_Account.objects.filter(user=request.user).update(
        amount=interest_amount + current_amount
    )
    Interest_Bank_Account.objects.filter(user=request.user).update(
        amount=0
    )
    return redirect('dashboard')

@login_required
def compound_affiliate_bonus_view(request):
    user1 = Affiliate_Bank_Account.objects.get(user=request.user)
    user2 = Current_Bank_Account.objects.get(user = request.user)
    affiliate_amount = user1.amount
    current_amount = user2.amount

    Current_Bank_Account.objects.filter(user = request.user).update(
        amount = affiliate_amount + current_amount
    )
    Affiliate_Bank_Account.objects.filter(user = request.user).update(
        amount = 0
    )
    return redirect('dashboard')


"""jayi URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/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.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from core import views
from django.views.generic import RedirectView
urlpatterns = [
    path('admin/', admin.site.urls),
    path('login/', views.login_user),
    path('login/submit',views.submit_login),
                  path ( 'login/cadastrar', views.cadastrar_usuario ),
                  path ( 'login/voltar', views.voltar ),
                  path ( 'login/cadastrar/voltar', views.voltar ),
                  path ( 'login/cadastrar/submit', views.cadastrar_confirmar ),
                  path('inicio/online',views.online),

                  path('inicio/logout/',views.logout_user),
    path('inicio/',views.inicio),
    path('inicio/jogadas/', views.jogadas),
    path('inicio/jogar/', views.jogar),
    path('inicio/jogar/pule/', views.ver_pule),
    path('inicio/jogar/submit', views.jogar_valor),
    path('inicio/resultado/',views.resultado),
    path('inicio/girar/',views.girar),
    path('inicio/dinheiro/',views.dinheiro),
    path ('inicio/resultado/conferir/', views.conferir_resultado ),
    path('inicio/jogar/teste/', views.teste),
    path('inicio/testando2', views.teste_script),
    path('inicio/seudinheiro', views.dinheiro_rodada),
    path ('inicio/conteiner', views.conteiner ),
    path ('inicio/avaliacao', views.avaliacao ),
    path ( 'inicio/comojogar', views.comojogar ),
    path ( 'inicio/submit', views.submit_avaliacao ),
    path ( 'teste', views.imagem ),
    path ( '', RedirectView.as_view ( url = 'inicio/' ) )
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys

def create_model(neuron1, neuron2):
    # create model
    model = Sequential()
    model.add(Dense(neuron1, input_dim=8, kernel_initializer='uniform', activation='linear'))
    model.add(Dense(neuron2, input_dim=neuron1, kernel_initializer='uniform', activation='linear'))
    model.add(Dense(1, activation='sigmoid'))

    # compile the model
    adam = Adam(lr=0.001)
    model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
    return model

def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Mon Dec 21 11:59:19 2020

@author: 100jjad

Add two sparse matrix with same dimensions

..........Sajjad Soheyli.........Series.....02.............Exercise....03...........

"""
#Get two Matrix From User

R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))
N1=0
N2=0
N3=0

matrix1 = []
matrix2 = []

for i in range(R):
    print("Enter row" ,i," inputs of First Matrix : ")
    a =[]
    for j in range(C):
         a.append(int(input()))
    matrix1.append(a)

print("\n")

for i in range(R):
    print("Enter row" ,i," inputs of Second Matrix : ")
    a =[]
    for j in range(C):
         a.append(int(input()))
    matrix2.append(a)

#sum of two Matrix  By Usual Method

Matrix_Sum = [[0 for x in range(R)] for y in range(C)]

for i in range(R):
    for j in range(C):
        Matrix_Sum[i][j] = matrix1[i][j] + matrix2[i][j]

print("Sum of two Sparse Matrix By Usual Method : \n" , Matrix_Sum)

#Create Sparse Matrix

Sparse_Matrix1 = []
Sparse_Matrix1.append([R,C,0])

Sparse_Matrix2 = []
Sparse_Matrix2.append([R,C,0])

Sparse_Matrix_Sum = []
Sparse_Matrix_Sum.append([R,C,0])

for i in range(0 , R):
    for j in range(0 , C):
        if matrix1[i][j]!=0:
            N1+=1
            Sparse_Matrix1.append([i+1 , j+1 , matrix1[i][j]])

Sparse_Matrix1[0][2]=N1
print("\n\n Sparse_Matrix1 : \n",Sparse_Matrix1 , "\n\n\n")

for i in range(0 , R):
    for j in range(0 , C):
        if matrix2[i][j]!=0:
            N2+=1
            Sparse_Matrix2.append([i+1 , j+1 , matrix2[i][j]])

Sparse_Matrix2[0][2]=N2
print("\n\n Sparse_Matrix2 : \n",Sparse_Matrix2 , "\n\n\n")

for i in range(0 , R):
    for j in range(0 , C):
        if Matrix_Sum[i][j]!=0:
            N3+=1
            Sparse_Matrix_Sum.append([i+1 , j+1 , Matrix_Sum[i][j]])

Sparse_Matrix_Sum[0][2]=N3
print("\n\n Sparse_Matrix_Sum : \n",Sparse_Matrix_Sum , "\n\n\n")

#sum of two Matrix  By Unusual Method

Unusual_Matrix_Sum = [[0 for x in range(R)] for y in range(C)]
"""
for i in range(1 , N1):
    for j in range(1 , N2):
        if Sparse_Matrix1[i][0] == Sparse_Matrix2[j][0]  & Sparse_Matrix1[i][1] == Sparse_Matrix2[j][1]:
            row = Sparse_Matrix1[i][0] -1
            col = Sparse_Matrix1[i][1] -1
            temp = Sparse_Matrix1[i][2] + Sparse_Matrix2[j][2]
            Unusual_Matrix_Sum[row][col] += temp
            temp = 0
            break
"""

for i in range(1 , N1+1):
    row = Sparse_Matrix1[i][0] -1
    col = Sparse_Matrix1[i][1] -1
    Unusual_Matrix_Sum[row][col] += Sparse_Matrix1[i][2]

for i in range(1 , N2+1):
    row = Sparse_Matrix2[i][0] -1
    col = Sparse_Matrix2[i][1] -1
    Unusual_Matrix_Sum[row][col] += Sparse_Matrix2[i][2]

print("\n\nSum of two Sparse Matrix By Unusual Method : \n" , Unusual_Matrix_Sum)


#-*-coding:utf8 -*-

def repeat_msg(msg,repeat=3):
    for i in range(repeat):
        print(msg)

if __name__=='__main__':
    repeat_msg('Hello')
    print("---------------")
    repeat_msg('Hello',2)
    print("---------------")
    repeat_msg('Hi',repeat=5)# repeat_msg('Hi',5)
                             #키워드 문자

def circle_area(radius, pi):
    area = pi*(radius**2)
    return area
if __name__ == "__main__":
    print("반지름:",3,"면적:",circle_area(3,3.14))
    print("반지름:",3, "면적:",circle_area(3,pi=3.14))
    print("반지름:",3,"면적:",circle_area(pi=3.14,radius=3))


import torch
import torch.nn as nn
import numpy as np
from tqdm import tqdm

def evaluate_ood(data_loader, mode, model, device):
    model.eval()
    outPred = []
    outGround = []
    outConf = []
    nih = False
    bxent = nn.BCELoss(reduction = 'mean')

    progress_bar = tqdm(data_loader)

    for i, (images,labels) in enumerate(progress_bar):

        if type(labels) == list:
            labels = torch.stack(labels).float().transpose(0,1).to(device)
        else:
            nih = True

        bs, c, h, w = images.size()
        varInput = images.view(-1, c, h, w)

        outGround.append(labels.cpu().numpy())

        if mode == 'confidence':
            #with torch.no_grad():

            T = 1000  #this hyperparameter can also be experimeted
            epsilon = 0.001

            model.zero_grad()
            varInput.requires_grad_()

            pred, _ = model(varInput)

            if nih:
                labels = torch.zeros(pred.shape[0], pred.shape[1]).to(device)

            pred = pred / T

            loss = bxent(pred, labels)
            loss.backward()

            varInput = varInput - epsilon * torch.sign(varInput.grad)
            #pred, _ = model(varInput)
            preds, confidence = model(varInput)
            confidence = torch.sigmoid(confidence)
            confidence = confidence.data.cpu().numpy()
            outPred.append(preds.data.cpu().numpy())
            outConf.append(confidence)

        elif mode == 'confidence_scaling':
            epsilon = 0.001  ##value needs to be determined (noise magnitude)

            model.zero_grad()
            varInput.requires_grad_()

            _,confidence = model(varInput)
            confidence = torch.sigmoid(confidence)

            loss = torch.mean(-torch.log(confidence))
            loss.backward()

            varInput = varInput - epsilon * torch.sign(varInput.grad)

            preds,confidence = model(varInput)
            confidence = torch.sigmoid(confidence)
            confidence = confidence.data.cpu().numpy()
            outPred.append(preds.data.cpu().numpy())
            outConf.append(confidence)

        elif mode == 'baseline':
            #uses the non-augmented model architecture
            with torch.no_grad():
                pred, _ = model(varInput)
                pred = pred.cpu().numpy()
                outPred.append(pred)

        elif mode == 'odin':
            #uses the non-augmented model architecture same as baseline
            T = 1000  #this hyperparameter can also be experimeted
            epsilon = 0.001

            model.zero_grad()
            varInput.requires_grad_()

            pred, _ = model(varInput)

            if nih:
                labels = torch.zeros(pred.shape[0], pred.shape[1]).to(device)

            pred = pred / T

            loss = bxent(pred, labels)
            loss.backward()

            varInput = varInput - epsilon * torch.sign(varInput.grad)
            pred, _ = model(varInput)

            #might need to take Sigmoid layer out from the model class for ODIN

            pred = pred.data.cpu().numpy()
            outPred.append(pred)

    outPred = np.concatenate(outPred)
    outGround = np.concatenate(outGround)
    outConf = None if mode == "odin" or mode == "baseline" else np.concatenate(outConf)
    return outPred, outGround, outConf


from __future__ import (unicode_literals, print_function, absolute_import,
                        division)

import logging
# Py2-3 compatibility
try:
    import queue
except ImportError:
    import Queue as queue
import os
import random
import string
import subprocess
import sys
import threading
from time import sleep

from six import reraise as raise_
from six.moves import zip_longest
from dfs_sdk import scaffold

DVOT_REPO = 'http://github.com/Datera/dvot'
ASSETS = os.path.join(
        os.path.dirname(
            os.path.dirname(os.path.abspath(__file__))), 'assets')
LOCKS = {}

class Parallel(object):

    """
    A helper class that makes it simpler to run tasks multiple times
    in parallel.  If you have multiple tasks you want to run in parallel
    you need to encapsulate them in a single function that accepts a variety
    of arguments.
    """

    def __init__(self, funcs, args_list=None, kwargs_list=None, max_workers=5,
                 timeout=3600):
        """

        :param funcs: A list of functions to be used by the workers
        :param args_list: A list of tuples of arguments required by each
                          function in `funcs`
        :param kwargs_list: A list of dictionaries of kwargs accepted
                            by each function in `funcs`
        :param max_workers: The maximum number of simultaneous threads
        """
        self.logger = logging.getLogger(__name__)
        if not self.logger.handlers:
            self.logger.addHandler(logging.NullHandler())
        self.funcs = funcs
        self.args_list = args_list if args_list else []
        self.kwargs_list = kwargs_list if kwargs_list else []
        self.max_workers = max_workers
        self.queue = queue.Queue()
        self.exceptions = queue.Queue()
        self.threads = []
        self.timeout = timeout
        self.keep_running = True

    @staticmethod
    def _set_current_thread_name_from_func_name(func):
        """ Renames the current thread to reflect the name of func """
        orig_thread_number = threading.current_thread().name.split('-')[-1]
        threading.current_thread().name = "Parallel-" + \
            func.__module__ + '.' + func.__name__ + "-" + orig_thread_number

    def _wrapped(self):
        threading.current_thread().name = "Parallel-Worker-" + \
            threading.current_thread().name
        while self.keep_running:
            try:
                func, args, kwargs = self.queue.get(block=False)
            except queue.Empty:
                break
            self.logger.debug(
                "Running {} with args: {} and kwargs {} with thread {}".format(
                    func, args, kwargs, threading.current_thread()))
            try:
                # Rename this thread to reflect the function we're running
                orig_name = threading.current_thread().name
                self._set_current_thread_name_from_func_name(func)
                # Call the function:
                func(*args, **kwargs)
                # Reset this thread name to its original (e.g. "Thread-9")
                threading.current_thread().name = orig_name
            except Exception:
                self.keep_running = False
                self.logger.exception("Exception occurred in thread {}".format(
                    threading.current_thread()))
                self.exceptions.put(sys.exc_info())

            self.queue.task_done()

    def run_threads(self):
        """
        Call this function to start the worker threads.  They will continue
        running until all args/kwargs are consumed.  This is a blocking call.
        """
        try:
            for func, args, kwargs in zip_longest(
                    self.funcs, self.args_list, self.kwargs_list,
                    fillvalue={}):
                # Flag a common (and confusing) user error:
                if isinstance(args, str) or isinstance(args, unicode):
                    msg = "args_list must be list of lists not list of strings"
                    raise ValueError(msg)
                self.queue.put((func, args, kwargs))

            for _ in xrange(self.max_workers):
                thread = threading.Thread(target=self._wrapped)
                thread.setDaemon(True)
                thread.start()
                self.threads.append(thread)

            if (len(self.funcs) < len(self.args_list) or
                    len(self.funcs) < len(self.kwargs_list)):
                raise ValueError(
                    "List of functions passed into a Parallel object must "
                    "be longer or equal in length to the list of args "
                    "and/or kwargs passed to the object.  {}, {}, {"
                    "}".format(self.funcs, self.args_list, self.kwargs_list))

            while self.queue.unfinished_tasks:
                # Check if exception has been generated by a thread and raise
                # if found one is found
                try:
                    exc = self.exceptions.get(block=False)
                    self.keep_running = False
                    raise_(*exc)
                except queue.Empty:
                    pass
                sleep(0.2)

        # Ensure all threads will exit regardless of the current
        # state of the main thread
        finally:

            try:
                exc = self.exceptions.get(block=False)
                self.keep_running = False
                # Join all threads to ensure we don't continue
                # without all threads stopping
                for thread in self.threads:
                    thread.join(self.timeout)
                raise_(*exc)
            except queue.Empty:
                pass

def exe(cmd, fail_ok=False):
    cmd = '{{ {}; }} 2>/dev/null'.format(cmd)
    dprint("Running command:", cmd)
    try:
        # Redirect stderr
        return subprocess.check_output(cmd, shell=True)
    except subprocess.CalledProcessError as e:
        if fail_ok:
            dprint(e)
            return None
        raise EnvironmentError(
            "Encountered error running command: {}, error : {}".format(cmd, e))

def rand_file_name(directory):
    return os.path.join(directory, ''.join([
        random.choice(string.ascii_letters) for _ in range(10)]))

def dprint(*args, **kwargs):
    if scaffold.VERBOSE:
        print(*args, **kwargs)

def locker(func):
    def _wrapper(*args, **kwargs):
        n = func.__name__
        if n not in LOCKS:
            LOCKS[n] = threading.Lock()
        with LOCKS[n]:
            return func(*args, **kwargs)
    return _wrapper


import os
import re

import invoke
from invoke import task

def oneline_command(string):
    return re.sub("\\s+", " ", string).strip()

def run_invoke_cmd(context, cmd):
    return context.run(cmd, env=None, hide=False, warn=False, pty=False, echo=True)

@task
def clean(context):
    find_command = oneline_command(
        """
        find
            .
            -type f \\(
                -name '*.script'
            \\)
            -or -type d \\(
                -name '*.dSYM' -or
                -name 'Sandbox' -or
                -name 'Output' -or
                -name 'output'
            \\)
            -not -path "**Expected**"
            -not -path "**Input**"
        """
    )

    find_result = run_invoke_cmd(context, find_command)
    find_result_stdout = find_result.stdout.strip()
    echo_command = oneline_command(
        """echo {find_result} | xargs rm -rfv""".format(find_result=find_result_stdout)
    )

    run_invoke_cmd(context, echo_command)

@task
def sphinx(context):
    cwd = os.getcwd()
    run_invoke_cmd(
        context,
        oneline_command(
            f"""
            cd docs &&
                SPHINXOPTS="-W --keep-going -n" make html latexpdf &&
                cp build/latex/sphinx-latex-reqspec-template.pdf {cwd}/Template.pdf &&
                open build/latex/sphinx-latex-reqspec-template.pdf
            """
        ),
    )

@task
def test_unit(context):
    command = oneline_command(
        """
        pytest --capture=no
        """
    )

    run_invoke_cmd(context, command)

@task
def export_pip_requirements(context):
    run_invoke_cmd(context, "poetry export -f requirements.txt > requirements.txt")

@task
def lint_black_diff(context):
    command = oneline_command(
        """
        black . --color 2>&1
        """
    )
    result = run_invoke_cmd(context, command)

    # black always exits with 0, so we handle the output.
    if "reformatted" in result.stdout:
        print("invoke: black found issues")
        result.exited = 1
        raise invoke.exceptions.UnexpectedExit(result)

@task
def lint_pylint(context):
    command = oneline_command(
        """
        pylint --rcfile=.pylint.ini docs/ tasks.py
        """
    )
    try:
        run_invoke_cmd(context, command)
    except invoke.exceptions.UnexpectedExit as exc:
        # pylink doesn't show an error message when exit code != 0, so we do.
        print("invoke: pylint exited with error code {}".format(exc.result.exited))
        raise exc

@task(lint_pylint, lint_black_diff)
def lint(_):
    pass

@task(lint)
def test(_):
    pass


def getCount(arr):
    letters = []
    for line in arr:
        for c in line:
            if (not (c in letters)):
                letters.append(c)
    return len(letters)

with open("input.txt") as f:
    lines = f.read().split("\n\n")
    sum = 0
    for i in range(len(lines)):
        lines[i] = lines[i].split("\n")
        sum += getCount(lines[i])
    print(sum)


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

# In[6]:

import matplotlib.pyplot as plt
import numpy as np

x=np.arange(-11,11,0.1)
y=1/(1+np.exp(-x))

plt.plot(x,y)
plt.show()

# In[8]:

def relu(x):
    return np.maximum(0,x)

# In[10]:

relu(-1)

# In[11]:

x=np.arange(-11,10,0.1)
y=relu(x)
plt.plot(x,y)


__author__ = 'Tornovskiy Konstantin'

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-cs', '--check-csp', help="Check installed Crypto PRO CSP version", action='store_true')
parser.add_argument('-v', '--get-version', help="Get dictionary with Hostname(IP) and Crypto PRO CSP version", action='store_true')
parser.add_argument('-f', '--write-file', help="Write output to file", action='store_true')
parser.add_argument('-r', '--range', help='Machine IP range')
args = parser.parse_args()

if args.range:
    print('Check CSP on range - ' + args.range)
else:
    print('Enter some range')
if args.check_csp:
    print('Check is enabled')


# Single Inheritance Vs Composition
from time import sleep
inherit_digram="""Single Inheritance
                |``````````````|
                |    Parent    |
                |,,,,,,,,,,,,,,|
                        |
                        |
                |``````````````|
                |      Son     |
                |,,,,,,,,,,,,,,|
"""

class Parent():
    def __init__(self):
        print("Creating Parent Object")

    def parent_implicit(self):
        print("Parent Implicit Method")

    def override(self):
        print("Parent Override Method")

    def altered(self):
        print("Parent Altered Method")

class Child(Parent):
    def __init__(self):
        super(Child, self).__init__()
        print("Creating Child Object")

    def child_implicit(self):
        print("Child Implicit Method")

    def override(self):
        print("Child Override Method")

    def altered(self):
        print("Child Altered Method Befor Parent ")
        super(Child, self).altered()
        print("Child Altered Method After Parent ")

def main():
    dad = Parent()
    son = Child()

    print("-" * 40)

    dad.parent_implicit()
    son.child_implicit()
    son.parent_implicit()

    print("-" * 40)

    dad.override()
    son.override()

    print("-" * 40)

    dad.altered()
    son.altered()

    print("-" * 40)

    dad = son

if __name__ == '__main__':
    print(inherit_digram)
    sleep(2)
    main()


import discord
import os
import random

client = discord.Client()

sad_words = [
    "sad", "depressed", "unhappy", "angry", "miserable", "depressief",
    "ik wil dood"
]

starter_encouragements = [
    "Cheer up!", "Hang in there.", "You are a great person / bot!"
]

hello_statements = [
    "Hallo", "Goedendag", "Fawaka Dushi", "Sup bruh", "Hi", "Fock off, jk"
]

poiesz_statements = [
    "Poiesz is een fantastisch mooie winkel opgericht in het altijd walgelijke Friesland!",
    "Poiesz is een kut winkel opgericht in die homo provincie hiernaast!",
    "Poiesz is kut man!", "Ga leren googlen fso slet"
]

jt_statements = [
    "Ik ben Jan Theo en ik denk dat ik een gangster ben",
    "Ik ben Jan Theo en ik hou van roze! #gay",
    "Ik weet niet wie ik ben, misschien je moeder?",
    "Ik ben je bro of nee toch niet",
    "Kan jij mijn profielfoto zien? Nee? Dan mag ik je niet"
]

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

# quote command

#end quote

# Standard text commands
    if message.content.startswith('!commands'):
        await message.channel.send(
            'Dit zijn alle commands die je kan uitvoeren: !hello, !jt, !twss, !msn, !maker, !cytorick, !wappie, !corona, !of, !age, !poiesz, !corona'
        )

    if message.content.startswith('!hello'):
        await message.channel.send(random.choice(hello_statements))

    if message.content.startswith('!jt'):
        await message.channel.send(random.choice(jt_statements))

    if message.content.startswith('!twss'):
        await message.channel.send('That`s what she said!')

    if message.content.startswith('!msn'):
        await message.channel.send('Mooie schoenen, neuken?')

    if message.content.startswith('!maker'):
        await message.channel.send(
            'Ik ben gemaakt door cytorick, een geweldige programmeur!')

    if message.content.startswith('!cytorick'):
        await message.channel.send(
            'Cytorick is een lichtelijk narcistisch persoon!')

    if message.content.startswith('!wappie'):
        await message.channel.send(
            '"Van de honderd lampen in de bovenkamer branden er een paar niet. Niet het meest snuggere type" aldus Miertje'
        )

    if message.content.startswith('!of'):
        await message.channel.send(
            'Ik leef in de cloud en ben niet in staat om een OnlyFans te onderhouden! Dus nee ik heb geen OnlyFans!'
        )

    if message.content.startswith('!age'):
        await message.channel.send(
            'Een leeftijd heb ik niet! Ik ben ontwikkeld op 15-02-2021...')

    if message.content.startswith('!poiesz'):
        await message.channel.send(random.choice(poiesz_statements))

    if message.content.startswith('!corona'):
        await message.channel.send(
            'Pff hier vraag je me wat... Corona is een prima te doen biertje maar is sinds 2020 ook de benaming van een erg besmettelijk virus... Idk waarom je dit niet weet maar ja ik ben maar een bot en ik oordeel niet... :wink: '
        )

# The end

client.run(os.getenv('TOKEN'))


# Developed for the LSST System Integration, Test and Commissioning Team.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the LICENSE file at the top-level directory of this distribution
# for details of code ownership.
#
# Use of this source code is governed by a 3-clause BSD-style
# license that can be found in the LICENSE file.

import asyncio
import math

from lsst_efd_client import EfdClient

from .. import utils

__all__ = ('main')

async def run(opts):
    efd = EfdClient(opts.location)
    cscs = utils.CSC.get_from_source(opts.sut)

    summary_state = 5  # STANDBY
    time_window = 10.0  # seconds
    time_format = '%Y-%m-%dT%H:%M:%S.%f'

    print("#############################################################")
    print("#                      STANDBY Report                       #")
    print("#############################################################")

    if opts.index_auto:
        top_n = 3
    else:
        top_n = 1

    for csc in cscs:
        ss_df = await efd.select_top_n(csc.efd_topic("logevent_summaryState"),
                                       ["private_sndStamp", "summaryState"],
                                       top_n, csc.index)

        if opts.index_auto:
            ss_df = ss_df.iloc[[2]]

        sv_df = await efd.select_top_n(csc.efd_topic("logevent_settingVersions"),
                                       ["private_sndStamp",
                                        "recommendedSettingsLabels",
                                        "recommendedSettingsVersion"],
                                       1, csc.index)

        sov_df = await efd.select_top_n(csc.efd_topic("logevent_softwareVersions"),
                                        "*",
                                        1, csc.index)

        print("-------------------------------------------------------------")
        print(f"CSC: {csc.full_name}")
        try:
            ss_df = utils.convert_timestamps(ss_df, ["private_sndStamp"])
            if ss_df.summaryState[0] != summary_state:
                print("CSC not in STANDBY State")
            else:
                print("CSC in STANDBY State")
                print(f"Time of Summary State: {ss_df.private_sndStamp[0].strftime(time_format)}")
        except (AttributeError, KeyError):
            print("summaryState event not present")
        try:
            sov_df = utils.convert_timestamps(sov_df, ["private_sndStamp"])
            delta = utils.time_delta(utils.get_now(), sov_df.private_sndStamp.values[0])
            print("softwareVersions present")
            print(f"Publication time gap: {delta:.1f} seconds")
            utils.check_correct_value(opts.xml, sov_df["xmlVersion"][0], "XML version")
            utils.check_correct_value(opts.sal, sov_df["salVersion"][0], "SAL version")
            utils.check_not_empty(sov_df["cscVersion"][0], "CSC version")
        except (AttributeError, KeyError):
            print("softwareVersions event not present")
        if csc.name not in utils.NON_CONFIG_CSCS:
            try:
                sv_df = utils.convert_timestamps(sv_df, ["private_sndStamp"])
                if sv_df.size:
                    delta = utils.time_delta(ss_df.private_sndStamp.values[0],
                                             sv_df.private_sndStamp.values[0])
                    if math.fabs(delta) > time_window:
                        print(f"Large delay in settingVersions publish: {delta:.1f} seconds")
                    rsl = sv_df.recommendedSettingsLabels.values[0]
                    rsv = sv_df.recommendedSettingsVersion.values[0]
                    if rsl == "":
                        print("Recommended Settings Labels is empty")
                    else:
                        print(f"Recommended Settings Labels: {rsl}")
                    print(f"Recommended Settings Version: {rsv}")
                else:
                    print("settingVersions event not present")
            except (AttributeError, KeyError):
                print("settingVersions event not present")

def main():
    parser = utils.create_parser()
    args = parser.parse_args()

    asyncio.run(run(args))


"""
Read file into texts and calls.
It's ok if you don't understand how to read files
"""
import csv
with open('texts.csv', 'r') as f:
    reader = csv.reader(f)
    texts = list(reader)

with open('calls.csv', 'r') as f:
    reader = csv.reader(f)
    calls = list(reader)

"""
TASK 2: Which telephone number spent the longest time on the phone
during the period? Don't forget that time spent answering a call is
also time spent on the phone.
Print a message:
"<telephone number> spent the longest time, <total time> seconds, on the phone during
September 2016.".
"""

"""
Returns a dictionary of calls in a given period O(n)
"""
def calls_in_period(calls, period):
    # Below was a suggestion from reviewer feedback
    call_dict = {}
    for call in calls:
        if call[0] not in call_dict.keys():
            call_dict[call[0]] = int(call[3])
        else:
            call_dict[call[0]] += int(call[3])
        if call[1] not in call_dict.keys():
            call_dict[call[1]] = int(call[3])
        else:
            call_dict[call[1]] += int(call[3])
    return call_dict

"""
Retuns the number and time in seconds of the longest time
by a given number on calls in a given period O(n)

Parameters: str, format '09-2016' for September 2016
"""
def longest_call_in_period(calls, period):
    dict = calls_in_period(calls, period)
    maxValue = 0
    maxKey = ''
    # Worse case dict could hold n
    for key in dict:
        if dict[key] > maxValue:
            maxValue = dict[key]
            maxKey = key
    return maxKey, maxValue

def test():
    result = longest_call_in_period(calls, '09-2016')
    message = '{} spent the longest time, {} seconds, on the phone during September 2016.'.format(result[0], result[1])
    print(message)

def tests():
    test_cases = [['97424 22395', '90365 06212', '01-09-2026 06:03:22', '1'],
    ['94489 72078', '92415 91418', '01-09-2016 06:05:35', '2'],
    ['78993 92058','92411 96415','30-09-2016 23:14:19', '3'],
    ['78993 92058','92411 96415','30-08-2016 23:14:19', '3'],
    ['94489 72078','92411 96415','30-09-2016 23:14:19', '4']]

    # Test total durations
    assert(calls_in_period(test_cases, '09-2016')['97424 22395'] == 0)
    assert(calls_in_period(test_cases, '09-2016')['78993 92058'] == 3)
    assert(calls_in_period(test_cases, '09-2016')['94489 72078'] == 6)

    result = longest_call_in_period(test_cases, '09-2016')
    message = '{} spent the longest time, {} seconds, on the phone during September 2016.'.format(result[0], result[1])
    assert(message == '92411 96415 spent the longest time, 7 seconds, on the phone during September 2016.')

test()


import numpy as np
import matplotlib.pyplot as plt

def round_dot(a):
    return [round(a[0], 2), round(a[1], 2)]

def f(x):
    return x**4 - 20*x**3 + 7

def dichotomy(a, b, func, k, iter=None):
    if iter is None:
        iter = []

    x1 = (b + a - 0.1)/2
    x2 = (b + a + 0.1)/2

    if not k:
        iter.append(round_dot([(a + b) / 2, func((a + b) / 2)]))
        return iter
    elif func(x1) <= func(x2):
        iter.append(round_dot([x1, f(x1)]))
        return dichotomy(a, x2, func, k-1, iter)
    elif func(x1) > func(x2):
        iter.append(round_dot([x2, f(x2)]))
        return dichotomy(x1, b, func, k-1, iter)

def data_vis(a, b, f, k):
    data0x = []
    data0y = []
    t = a
    while t+5 < b:
        data0x.append(t)
        data0y.append(f(t))
        t += 0.2

    data = np.transpose(dichotomy(a, b, f, k))
    x = data[0]
    y = data[1]
    fig = plt.figure()
    ax0 = fig.add_subplot()
    ax0.scatter(data0x, data0y)
    ax0.scatter(x, y)
    plt.show()

def dot_vis(dots):
    for i in dots:
        print(i)

dot_vis(dichotomy(5, 25, f, 7))
data_vis(5, 25, f, 7)


import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from matplotlib.colors import LinearSegmentedColormap

import shap

from ..explainer import Explainer

class XDeepShapImageExplainer(Explainer):

    def __init__(self, predict_proba, class_names, n_segment, segment):
        """Init function.

        # Arguments
            predict_proba: Function. Classifier prediction probability function.
            class_names: List. A list of class names, ordered according to whatever the classifier is using.
            n_segment: Integer. Number of segments in the image.
            segment: Array. An array with 2 dimensions, segments_slic of the image.
        """
        Explainer.__init__(self, predict_proba, class_names)
        self.n_segment = n_segment
        self.segment = segment

    @staticmethod
    def _mask_image(zs, segmentation, image, background=None):
        """Mask image function for shap."""
        if background is None:
            background = image.mean((0, 1))
        out = np.zeros((zs.shape[0], image.shape[0], image.shape[1], image.shape[2]))
        for i in range(zs.shape[0]):
            out[i, :, :, :] = image
            for j in range(zs.shape[1]):
                if zs[i, j] == 0:
                    out[i][segmentation == j, :] = background
        return out

    def set_parameters(self, n_segment, segment):
        """Parameter setter for shap_image. For more detail, please check https://github.com/slundberg/shap/blob/master/shap/explainers/kernel.py.

        # Arguments
            n_segment: Integer. number of segments in the image.
            segment: Array. An array with 2 dimensions, segments_slic of the image.
        """
        self.n_segment = n_segment
        self.segment = segment

    def explain(self, instance, top_labels=None, labels=(1,), **kwargs):
        """Generate explanation for a prediction using certain method.

        # Arguments
            instance: Array. An image to be explained.
            top_labels: Integer. Number of labels you care about.
            labels: Tuple. Labels you care about, if top_labels is not none, it will be replaced by the predicted top labels.
            **kwarg: Parameters setter. For more detail, please check https://github.com/slundberg/shap/blob/master/shap/explainers/kernel.py.
        """
        Explainer.explain(self, instance, top_labels=top_labels, labels=labels)

        def f(z):
            masked = self._mask_image(z, self.segment, self.instance, 255)
            return self.predict_proba(masked)

        self.explainer = shap.KernelExplainer(f, np.zeros((1, self.n_segment)), **kwargs)
        self.explanation = self.explainer.shap_values(np.ones((1, self.n_segment)), **kwargs)

    def __fill_segmentation(self, values, segmentation):
        """Fill segmentation

        # Arguments
            values: Array, weights.
            segmentation: Array. An array with 2 dimensions, segments_slic of the image.

        # Return
            out
        """
        out = np.zeros(segmentation.shape)
        for i in range(len(values)):
            out[segmentation == i] = values[i]
        return out

    def show_explanation(self, deprocess=None):
        """Visualization of explanation of shap.

        # Arguments
            deprocess: Function. A function to deprocess the image.
        """
        Explainer.show_explanation(self)
        shap_values = self.explanation
        labels = self.labels
        segments_slic = self.segment
        img = self.instance

        if deprocess is not None:
            img = deprocess(img)
        img = Image.fromarray(np.uint8(img*255))
        colors = []
        for l in np.linspace(1, 0, 100):
            colors.append((245 / 255, 39 / 255, 87 / 255, l))
        for l in np.linspace(0, 1, 100):
            colors.append((24 / 255, 196 / 255, 93 / 255, l))
        colormap = LinearSegmentedColormap.from_list("shap", colors)
        # Plot our explanations
        fig, axes = plt.subplots(nrows=1, ncols=len(labels)+1, figsize=(12, 4))
        inds = labels
        axes[0].imshow(img)
        axes[0].axis('off')
        max_val = np.max([np.max(np.abs(shap_values[i][:, :-1])) for i in range(len(shap_values))])
        assert hasattr(labels, '__len__')
        for i in range(len(labels)):
            m = self.__fill_segmentation(shap_values[inds[i]][0], segments_slic)
            axes[i + 1].set_title(self.class_names[labels[i]])
            axes[i + 1].imshow(img, alpha=0.5)
            axes[i + 1].imshow(m, cmap=colormap, vmin=-max_val, vmax=max_val)
            axes[i + 1].axis('off')
        plt.show()


from tkinter import Entry

from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response

from api.models import Restaurant,Account

from utils.decorators import user_login_required

@api_view()
def get_all_restaurants(request):
    restaurants=Restaurant.objects.all()
    # print(books)
    return Response({
        'success':True,
        'data': [
            {
                'restaurant_id':restaurant.restaurant_id,
                # 'account': restaurant.account,
                'name': restaurant.name,
                'address': restaurant.address,
                'phone': restaurant.phone,
                'business_hours': restaurant.business_hours,
                'resume': restaurant.resume,
                'license_id': restaurant.license_id,
                'lat': restaurant.lat,
                'lon': restaurant.lon,

            }
        for restaurant in restaurants
        ]

    })

# @api_view()
# def get_a_restaurant(request):
#     data=request.data
#
#     restaurant_id=data.get('restaurant_id')
#     restaurants = Restaurant.objects.filter(restaurant_id=restaurant_id)
#     if not restaurants.exists():
#         return Response({'success':False, 'message':'沒有此餐廳'}, status=status.HTTP_404_NOT_FOUND)
#     return Response({
#         'success': True,
#         'data':[
#             {
#                 'name': restaurant.name,
#                 'address': restaurant.address,
#                 'phone': restaurant.phone,
#                 'business_hours': restaurant.business_hours,
#                 'resume': restaurant.resume,
#             }
#             for restaurant in restaurants
#         ]
#     })

@api_view()
def get_a_restaurant(request,pk):
    # data = request.query_params
    # pk = data.get('post_id')
    try:
        restaurant = Restaurant.objects.get(pk=pk)
    except:
        return Response({'success': False, 'message': '查無資料'}, status=status.HTTP_404_NOT_FOUND)

    return Response({

        'success': True,
        'data': {
            'restaurant_id': restaurant.restaurant_id,
            # 'account': restaurant.account,
            'name': restaurant.name,
            'address': restaurant.address,
            'phone': restaurant.phone,
            'business_hours': restaurant.business_hours,
            'resume': restaurant.resume,
            'license_id': restaurant.license_id,
            'lat': restaurant.lat,
            'lon': restaurant.lon,
        }
    })

@api_view(['POST'])
def add_information(request):
    data = request.data
    # try:
    account=Account.objects.get(pk=data['account'])
    Restaurant.objects.create(account=account,name=data['name'], address=data['address'], phone=data['phone'],business_hours=data['business_hours'],resume=data['resume'],license_id=data['license_id'],lat=data['lat'],lon=data['lon'])

    # except:
    #     return Response({'success': False, "message": '新增失敗'}, status=status.HTTP_400_BAD_REQUEST)
    #
    # return Response({'success': True, 'message': '新增成功'})

@api_view()
def search_restaurant(request):
    data = request.query_params
    search = data['search']
    # try:
    restaurants = Restaurant.objects.filter(name__contains=search)
    # except:
    #     return Response({'success': False, 'message': '查無資料'}, status=status.HTTP_404_NOT_FOUND)

    return Response({

        'success': True,
        'data': [
            {
                'restaurant_id': restaurant.restaurant_id,
                # 'account': restaurant.account,
                'name': restaurant.name,
                'address': restaurant.address,
                'phone': restaurant.phone,
                'business_hours': restaurant.business_hours,
                'resume': restaurant.resume,
                'license_id': restaurant.license_id,
                'lat': restaurant.lat,
                'lon': restaurant.lon,
            }
            for restaurant in restaurants
        ]
    })


import os, sys, xlrd, xlwt
import pandas as pd
import numpy as np
import argparse
from multiprocessing import Pool

#set up parameters
parser = argparse.ArgumentParser(description='manual to this script')
parser.add_argument('--permutation-time', type=int, default=1000)
parser.add_argument('--cluster', type=int, help='Input the number of the cluster')
parser.add_argument('--geneset', type=str, help='Input geneset for permutation test in AD known geneset coexpression network')
parser.add_argument('--fitlength-distributiongenes', type=str, help='Input genes fitted the length distribution as the specific geneset')
parser.add_argument('--correlation-method', type=str, help='the correlation method used for network construction: pearson or spearman')
parser.add_argument('--correlation-threshold', type=float, help='Input the correlation threshold for PPI pairs')
args = parser.parse_args()

os.chdir('~/spatial_temporal_analysis/')
geneset_list=pd.read_table(args.geneset,header=None)
geneset_list=list(geneset_list.iloc[:, 0])
geneset_num=len(geneset_list)

# calculate fraction of co-expression pairs of geneset according to different windows in the same cluster
file_name='~/spatial_temporal_analysis/net_thresholdcor_PPI/'+'net_coexp_threshold'+str(args.correlation_threshold)+'_'+args.correlation_method+'_cluster'+str(args.cluster)+'.txt'
tmp_data = pd.read_table(file_name, sep='\t')
all_geneset_data = tmp_data[(tmp_data['gene1'].isin(geneset_list)) | (tmp_data['gene2'].isin(geneset_list))]
num_all_cluster_pairs = len(all_geneset_data['cor'])
if args.cluster != 5:
  start = 1
  geneset_fraction_win_list = []
else:
  start = 2
  geneset_fraction_win_list = ['NA']
for j in range(start,12):
  spatio_temperal = 'cluster'+str(args.cluster)+'_win'+str(j)
  tmp_data_temp = all_geneset_data[all_geneset_data['spatio-temperal'] == spatio_temperal]
  num_tmp_data_temp = len(tmp_data_temp['cor'])
  geneset_fraction_win_list.append(num_tmp_data_temp/num_all_cluster_pairs)

# generate random seed genes
fitlength_distribution_genes=pd.read_table(args.fitlength_distributiongenes)
random_gene_list = list(set(fitlength_distribution_genes['entrezgene_id']))
random_gene_list = list(set(random_gene_list) - set(geneset_list))

def permutation_test(permutation_time):
  m=permutation_time
  random_seeds = list(np.random.choice(random_gene_list, geneset_num, replace=False))
  all_random_data = tmp_data[(tmp_data['gene1'].isin(random_seeds)) | (tmp_data['gene2'].isin(random_seeds))]
  num_all_cluster_pairs = len(all_random_data['cor'])
  if args.cluster == 5:
    random_fraction_win_list = ['NA']
  else:
    random_fraction_win_list = []
  for win in range(start,12):
    spatio_temperal = 'cluster'+str(args.cluster)+'_win'+str(win)
    tmp_data_temp = all_random_data[all_random_data['spatio-temperal'] == spatio_temperal]
    num_tmp_data_temp = len(tmp_data_temp['cor'])
    random_fraction_win_list.append(num_tmp_data_temp/num_all_cluster_pairs)
  return(random_fraction_win_list)

if __name__ == '__main__':
  cpu_num=os.cpu_count()
  pool = Pool(cpu_num)
  jobs = []
  jobs_res = []
  for permutation_time in range(args.permutation_time):
    jobs.append(pool.apply_async(permutation_test, args=(permutation_time,)))
  pool.close()
  pool.join()
  for permu in jobs:
    jobs_res.append(permu.get())
  jobs_res = pd.DataFrame(jobs_res)
  jobs_res.columns = ['win'+ str(win) for win in range(1,12)]
  if args.cluster == 5:
    cluster_res = ['cluster'+str(args.cluster),'NA']
  else:
    cluster_res = ['cluster'+str(args.cluster)]
  for j in range(start,12):
    permu_win_list = list(jobs_res['win'+str(j)])
    #significance test
    permu_win_pvalue=(len([item for item in permu_win_list if item > geneset_fraction_win_list[j-1]])+1)/(args.permutation_time+1)
    cluster_res.append(permu_win_pvalue)

  #output the permutation results
  outfile='~/spatial_temporal_analysis/net_thresholdcor_PPI_fractiontest/fraction_permutationtest_'+'net_coexp_threshold'+str(args.correlation_threshold)+'_'+args.correlation_method+'_'+str(args.permutation_time)+'.txt'
  res_out=str(cluster_res).replace('[','').replace(']','').replace("'",'').replace(', ','\t')
  f=open(outfile,'a')
  f.write(res_out)
  f.write('\n')
  f.close()


from datetime import datetime, timedelta
from os import path, remove
from urllib import request, error
from myDevices.utils.logger import exception, info, warn, error, debug
from threading import Thread
from time import sleep
from random import randint
from socket import error as socket_error
from myDevices.os.daemon import Daemon
from myDevices.os.threadpool import ThreadPool

defaultUrl = "https://updates.mydevices.com/test/10MB.zip"
download_path = "/etc/myDevices/test"
mb = 1024*1024
#in seconds download Rate = 24 hours
defaultDownloadRate = 24*60*60

class DownloadSpeed():
    def __init__(self, config):
        self.downloadSpeed = None
        self.uploadSpeed = None
        self.testTime = None
        self.isRunning = False
        self.Start()
        self.config = config
        #add a random delay to the start of download
        self.delay = randint(0,100)
    def Start(self):
        #thread = Thread(target = self.Test)
        #thread.start()
        ThreadPool.Submit(self.Test)
    def Test(self):
        if self.isRunning:
            return False
        self.isRunning = True
        sleep(1)
        self.TestDownload()
        sleep(1)
        self.TestUpload()
        self.testTime = datetime.now()
        self.isRunning = False
        return True
    def TestDownload(self):
        try:
            a = datetime.now()
            info('Excuting regular download test for network speed')
            url = self.config.cloudConfig.DownloadSpeedTestUrl if 'DownloadSpeedTestUrl' in self.config.cloudConfig else defaultUrl
            debug(url + ' ' + download_path)
            request.urlretrieve(url, download_path)
            request.urlcleanup()
            b = datetime.now()
            c = b - a
            if path.exists(download_path):
                size = path.getsize(download_path)/mb
                self.downloadSpeed = size/c.total_seconds()
                remove(download_path)
                return True
        except socket_error as serr:
                error ('TestDownload:' + str(serr))
                ret = False
                Daemon.OnFailure('cloud', serr.errno)
                return
        except:
            exception('TestDownload Failed')
        return False
    def TestUpload(self):
        debug('Network Speed TestUpload - Not implemented')
        self.uploadSpeed = 0
    def IsDownloadTime(self):
        if self.testTime is None:
            return True
        downloadRate = int(self.config.cloudConfig.DownloadSpeedTestRate) if 'DownloadSpeedTestRate' in self.config.cloudConfig else defaultDownloadRate
        if self.testTime +timedelta(seconds=downloadRate+self.delay ) < datetime.now():
            return True
        return False
    def getDownloadSpeed(self):
        if self.IsDownloadTime():
            self.Start()
        return self.downloadSpeed
    def getUploadSpeed(self):
        return self.uploadSpeed

def Test():
    from myDevices.utils.config import Config
    testDownload = DownloadSpeed(Config('/etc/myDevices/AppSettings.ini'))
    speed = testDownload.getDownloadSpeed()
    print('Download speed 1: ' + str(speed))
    sleep(20)
    speed = testDownload.getDownloadSpeed()
    print('Download speed 2: ' + str(speed))
    sleep(20)
    speed = testDownload.getDownloadSpeed()
    print('Download speed 3: ' + str(speed))