from email.quoprimime import quote
import pandas as pd
import requests
from bs4 import BeautifulSoup
from time import sleep
import csv

def request(msg, slp=1):
    '''A wrapper to make robust https requests.'''
    status_code = 500  # Want to get a status-code of 200
    while status_code != 200:
        sleep(slp)  # Don't ping the server too often
        try:
            r = requests.get(msg)
            status_code = r.status_code
            if status_code != 200:
                print("Server Error! Response Code %i. Retrying..." % (r.status_code))
        except:
            print("An exception has occurred, probably a momentory loss of connection. Waiting one seconds...")
            sleep(10)
    return r

def getDadosJogo(linkJogo,idJogo):

    #urlBusca = 'https://www.ludopedia.com.br/jogo/'
    #nomeJogoBusca = str(nomeJogo).replace(' ','-')
    #urlBuscaPagina = urlBusca+nomeJogoBusca
    urlBuscaPagina = linkJogo
    r = request(urlBuscaPagina)
    columnsDF=["id",
            "jogoAno",
            "jogoIdade",
            "jogoTempJogo",
            "jogoQtJogadores",
            "jogoMiolosDesigner",
            "jogoMiolosArtista",
            "jogoMiolosEditora",
            "jogoStatTem",
            "jogoStatQuer",
            "jogoStatTeve",
            "jogoStatFavorito",
            "jogoResumoTexto"]
    #dfJogo = pd.DataFrame(columns=columnsDF,index=range(1))

    soup = BeautifulSoup(r.text, 'lxml')

    detalhes = soup.find_all("div", attrs={'class':'jogo-top-main'})
    jogoAnoFull = detalhes[0].find_all("span", attrs={'class':'text-xs'})

    jogoAno = jogoAnoFull[0].string.replace('(','').replace(')','')

    jogoDetalhesFull = detalhes[0].find_all("ul", attrs={'class':'list-inline'})
    jogoDetalhesLista = jogoDetalhesFull[0].find_all("li")
    jogoIdade = jogoDetalhesLista[0].text
    jogoTempJogo = jogoDetalhesLista[1].text
    jogoQtJogadores = jogoDetalhesLista[2].text

    jogoMiolos = detalhes[0].find_all("span", attrs={'class':'info-span text-sm'})
    jogoMiolosDesigner = jogoMiolos[0].find_next("a").text
    jogoMiolosArtista = jogoMiolos[1].find_next("a").text
    jogoMiolosEditora = jogoMiolos[2].find_next("a").text

    jogoStatsPossuem =  detalhes[0].find_all("button")
    for jogoStat in jogoStatsPossuem:
        if jogoStat.attrs.get("data-tipo") == "fl_tem":
            sTemp = jogoStat.string
            jogoStatTem = sTemp[sTemp.find('(')+1:sTemp.find(')')]
        if jogoStat.attrs.get("data-tipo") == "fl_quer":
            sTemp = jogoStat.string
            jogoStatQuer = sTemp[sTemp.find('(')+1:sTemp.find(')')]
        if jogoStat.attrs.get("data-tipo") == "fl_teve":
            sTemp = jogoStat.string
            jogoStatTeve = sTemp[sTemp.find('(')+1:sTemp.find(')')]
        if jogoStat.attrs.get("data-tipo") == "fl_favorito":
            sTemp = jogoStat.string
            jogoStatFavorito = sTemp[sTemp.find('(')+1:sTemp.find(')')]

    jogoResumo = soup.find_all("div", attrs={'class':'bloco-sm-content bloco-sm-content-open'})
    jogoResumoTexto = jogoResumo[0].text.replace("\n","").replace("\t",'').replace("\r",'')

    tempDataframe = pd.DataFrame([[idJogo,
                            jogoAno,
                            jogoIdade,
                            jogoTempJogo,
                            jogoQtJogadores,
                            jogoMiolosDesigner,
                            jogoMiolosArtista,
                            jogoMiolosEditora,
                            jogoStatTem,
                            jogoStatQuer,
                            jogoStatTeve,
                            jogoStatFavorito,
                            jogoResumoTexto]],columns=columnsDF)
    #dfJogo.iloc[1, :] = tempDataframe
    print(tempDataframe)
    return tempDataframe

def geraDetalheJogos():
    #----------------- lendo os jogos baixados
    file = '003_storage/000_transient/ludopedia_listaJogos.csv'
    csvDetalheJogos = '003_storage/000_transient/ludopedia_DetalhesJogos.csv'

    df = pd.read_csv(file,sep=';',quoting=csv.QUOTE_NONNUMERIC)
    linkJogo = df['url']
    print('DF size :'+str(df.size))
    print('DF shape :'+str(df.shape))

    dfJogoAll = pd.DataFrame(columns=["id",
                                "jogoAno",
                                "jogoIdade",
                                "jogoTempJogo",
                                "jogoQtJogadores",
                                "jogoMiolosDesigner",
                                "jogoMiolosArtista",
                                "jogoMiolosEditora",
                                "jogoStatTem",
                                "jogoStatQuer",
                                "jogoStatTeve",
                                "jogoStatFavorito",
                                "jogoResumoTexto"])

    #for idJogo in  idsJogos:
    for i in range(len(df)):
        dfJogo = getDadosJogo(linkJogo[i],i+1)
        dfJogoAll = pd.concat([dfJogoAll,dfJogo],axis=0)
        if i == 0:
            #criando o arquivo inicial
            # com o header
            dfJogo.to_csv(csvDetalheJogos, sep=';',quoting=csv.QUOTE_NONNUMERIC)
        else:
            dfJogo.to_csv(csvDetalheJogos,mode='a', header=False, sep=';',quoting=csv.QUOTE_NONNUMERIC)

#geraDetalheJogos()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/3/3 16:31
# @Author  : Vincent.G.Woo
# @Site    :
# @File    : blogtest2.py
# @Software: PyCharm Community Edition
# @Python_Version:
# @Software_Version:

import sys
from blog import Ui_MainWindow
from PyQt5 import QtWidgets

class mywindow(QtWidgets.QWidget, Ui_MainWindow):
    global mStr

    def __init__(self):
        super(mywindow, self).__init__()
        self.setupUi(self)
        self.pushButton_3.clicked.connect(self.addText)

    def getText(self):
        global mStr
        mStr = self.textEdit.toHtml()

    def setText(self):
        global mStr
        self.textBrowser.append(mStr)

    def addText(self):
        self.getText()
        self.setText()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    myshow = mywindow()
    myshow.show()
    sys.exit(app.exec_())


import numpy as np
from scipy import stats
# from expon_cusum import Lv,F1
# import matplotlib.pyplot as plt

### This script is used to compare the thresholds for CuSum & WL-Cusum

## From first commit
class F1(object):
    def __init__(self,c,mu_1=1,sig_1=1):
        self.c = c
        self.mu_1 = mu_1
        self.dist = stats.norm(0,sig_1)
    def pdf(self,y,nu,t):
        # Require: t > nu
        assert(not isinstance(y,np.ndarray))
        return self.dist.pdf(y-self.mu_1*np.exp(self.c*(t-nu)))
    def logpdf(self,y,nu,t):
        # Require: t > nu
        assert(not isinstance(y,np.ndarray))
        return self.dist.logpdf(y-self.mu_1*np.exp(self.c*(t-nu)))
    def rvs(self,size,nu,t):
        # Require: t > nu
        return self.dist.rvs(size=size) + self.mu_1*np.exp(self.c*(np.arange(t,t+size)-nu))
    def logpdf_arr(self,y):
        assert(isinstance(y,np.ndarray))
        ind = np.arange(float(len(y)))
        return np.sum(self.dist.logpdf(y-self.mu_1*np.exp(self.c*ind)))
def gfun(y,fc,f0):
    llr = np.zeros(len(y))
    for k in range(len(y)):
        llr[k] = fc.logpdf_arr(y[k:]) - np.sum(f0.logpdf(y[k:]))
    # print(llr)
    return (np.argmax(llr),np.amax(llr))
def Lv(y, f0, c, t):
    max_k, max_llr = gfun(y,F1(c,mu_1=f0.mean(),sig_1=np.sqrt(f0.var())),f0)
    # print(t,max_k)
    return max_llr

c = 0.4
f0 = stats.norm(0.1,100)
# f1 = F1(c,mu_1=f0.mean(),sig_1=np.sqrt(f0.var()))

b_1 = np.exp(np.linspace(np.log(.75),np.log(1.05),5))
b_2 = np.exp(np.linspace(np.log(.22),np.log(.32),5))
b_3 = np.exp(np.linspace(np.log(2),np.log(4.8),5))

N = 10000

mtfa_1 = np.zeros(len(b_1))
mtfa_2 = np.zeros(len(b_2))
mtfa_3 = np.zeros(len(b_3))
# add_1 = np.zeros(len(b_1))
# add_2 = np.zeros(len(b_2))
# add_3 = np.zeros(len(b_3))
W_1 = 25
W_2 = 50

for j in range(N):
    for i in range(len(b_1)):
        t = W_1
        y = f0.rvs(size=2000)
        while True:
            try:
                y[t]
            except IndexError:
                y = np.append(y,f0.rvs(size=2000))
            v_1 = Lv(y[max(1,t-W_1+1):t+1], f0, c, t)
            if (v_1 > b_1[i]):
                mtfa_1[i] = mtfa_1[i] + t - W_1 + 1
                break
            t = t + 1
        # print(mtfa_1[i])

    for i in range(len(b_2)):
        t = W_2
        y = f0.rvs(size=2000)
        while True:
            try:
                y[t]
            except IndexError:
                y = np.append(y,f0.rvs(size=2000))
            v_2 = Lv(y[max(1,t-W_2+1):t+1], f0, c, t)
            if (v_2 > b_2[i]):
                mtfa_2[i] = mtfa_2[i] + t - W_2 + 1
                break
            t = t + 1
        # print(mtfa_2[i])

    # No WL
    for i in range(len(b_3)):
        t = 0
        y = f0.rvs(size=2000)
        while True:
            try:
                y[t]
            except IndexError:
                y = np.append(y,f0.rvs(size=2000))
            v_3 = Lv(y[:t+1], f0, c, t)
            if (v_3 > b_3[i]):
                mtfa_3[i] = mtfa_3[i] + t + 1
                break
            t = t + 1

    # for i in range(len(b_1)):
    #     t = W_1
    #     y = f0.rvs(size=W_1)
    #     while True:
    #         try:
    #             y[t]
    #         except IndexError:
    #             y = np.append(y,f1.rvs(size=W_1,nu=W_1-1,t=t))
    #         v_1 = Lv(y[max(1,t-W_1+1):t+1], f0, c, t)
    #         if (v_1 > b_1[i]):
    #             add_1[i] = add_1[i] + t - W_1 + 1
    #             break
    #         t = t + 1
    #
    # for i in range(len(b_2)):
    #     t = W_2
    #     y = f0.rvs(size=W_2)
    #     while True:
    #         try:
    #             y[t]
    #         except IndexError:
    #             y = np.append(y,f1.rvs(size=W_2,nu=W_2-1,t=t))
    #         v_2 = Lv(y[max(1,t-W_2+1):t+1], f0, c, t)
    #         if (v_2 > b_2[i]):
    #             add_2[i] = add_2[i] + t - W_2 + 1
    #             break
    #         t = t + 1
    #
    # for i in range(len(b_3)):
    #     t = W_3
    #     y = f0.rvs(size=W_3)
    #     while True:
    #         try:
    #             y[t]
    #         except IndexError:
    #             y = np.append(y,f1.rvs(size=W_3,nu=W_3-1,t=t))
    #         v_3 = Lv(y[max(1,t-W_3+1):t+1], f0, c, t)
    #         if (v_3 > b_3[i]):
    #             add_3[i] = add_3[i] + t - W_3 + 1
    #             break
    #         t = t + 1

    if j % 10 == 9:
        print("Num of Iterations:", (j+1))
        print("mtfa_1:", mtfa_1 / (j+1))
        print("mtfa_2:", mtfa_2 / (j+1))
        print("mtfa_3:", mtfa_3 / (j+1))
        print("add_1:", add_1 / (j+1))
        print("add_2:", add_2 / (j+1))
        print("add_3:", add_3 / (j+1))
        # with open('./icp_cusum_t1.npy', 'wb') as f:
        #     np.save(f, mtfa_1 / (j+1))
        #     np.save(f, mtfa_2 / (j+1))
        #     np.save(f, mtfa_3 / (j+1))
        #     np.save(f, add_1 / (j+1))
        #     np.save(f, add_2 / (j+1))
        #     np.save(f, add_3 / (j+1))

###


from colorama import Fore, Style
from typing import Tuple
import numpy as np
from colorama import Fore, Style

import time
print(Fore.BLUE + "\nLoading tensorflow..." + Style.RESET_ALL)
start = time.perf_counter()

from tensorflow import keras
from keras import Model, Sequential, layers, regularizers, optimizers, models
from keras.callbacks import EarlyStopping

end = time.perf_counter()
print(f"\n✅ tensorflow loaded ({round(end - start, 2)} secs)")

def initialize_model(X: np.ndarray) -> Model:
    """
    Initialize the Neural Network with random weights
    """
    print(Fore.BLUE + "\nInitialize model..." + Style.RESET_ALL)

    model = models.Sequential()

    input_shape = (13, 130, 1)

    ### First Convolution, MaxPooling, and Normalization
    model.add(layers.Conv2D(32, (3,3), activation='relu', padding='same', input_shape=input_shape))
    model.add(layers.MaxPool2D(pool_size=(3,3), strides=(2, 2), padding='same'))
    model.add(layers.BatchNormalization())
    model.add(layers.Dropout(0.5))

    ### Second Convolution & MaxPooling
    model.add(layers.Conv2D(32, (2,2), activation='relu', padding='same'))
    model.add(layers.MaxPool2D(pool_size=(3,3), strides=(2, 2), padding='same'))
    model.add(layers.BatchNormalization())
    model.add(layers.Dropout(0.55))

    ### Third Convolution & MaxPooling
    model.add(layers.Conv2D(32, (2,2), activation='relu', padding='same'))
    model.add(layers.MaxPool2D(pool_size=(2,2), strides=(2, 2), padding='same'))
    model.add(layers.BatchNormalization())
    model.add(layers.Dropout(0.55))

    ### Flattening
    model.add(layers.Flatten())

    ### One more Dense Layer
    model.add(layers.Dense(64, activation='relu'))

    ### A Dropout layer to avoid overfitting
    model.add(layers.Dropout(0.5))

    ### Last layer - Classification Layer with 8 outputs for the 8 genres
    model.add(layers.Dense(8, activation='softmax'))

    print("\n✅ model initialized")

    return model

def compile_model(model: Model, learning_rate: float) -> Model:
    """
    Compile the Neural Network
    """

    ### Model compilation
    optimizer = optimizers.Adam(learning_rate=0.0001)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])

    print("\n✅ model compiled")

    return model

def train_model(model: Model,
                X: np.ndarray,
                y: np.ndarray,
                batch_size=32,
                patience=10,
                validation_split=0.3,
                validation_data=None) -> Tuple[Model, dict]:
    """
    Fit model and return a the tuple (fitted_model, history)
    """

    print(Fore.BLUE + "\nTrain model..." + Style.RESET_ALL)

    #Setup early stopping
    es = EarlyStopping(patience = patience, monitor='accuracy')

    #Training the data
    history = model.fit(X,
              y,
              validation_split=validation_split,
              validation_data=validation_data,
              batch_size = batch_size,
              epochs = 50,
              callbacks = [es],
              verbose = 1)

    print(f"\n✅ model trained ({len(X)} rows)")

    return model, history

def evaluate_model(model: Model,
                   X: np.ndarray,
                   y: np.ndarray,
                   batch_size=64) -> Tuple[Model, dict]:
    """
    Evaluate trained model performance on dataset
    """

    print(Fore.BLUE + f"\nEvaluate model on {len(X)} rows..." + Style.RESET_ALL)

    if model is None:
        print(f"\n❌ no model to evaluate")
        return None

    metrics = model.evaluate(
        x=X,
        y=y,
        batch_size=batch_size,
        verbose=1,
        # callbacks=None,
        return_dict=True)

    accuracy = metrics["accuracy"]
    # mae = metrics["mae"]

    # print(f"\n✅ model evaluated: loss {round(loss, 2)} mae {round(mae, 2)}")

    print(f"\n✅ model evaluated: accuracy {round(accuracy, 2)}")

    return metrics


"""
Author:    alberto.suarez@uam.es

Coauthors: joseantonio.alvarezo@estudiante.uam.es
           franciscojavier.saez@estudiante.uam.es

"""

import warnings
from abc import ABC, abstractmethod
from typing import Callable, Optional, Union, Type
from sklearn.gaussian_process.kernels import RBF

import matplotlib.pyplot as plt
import numpy as np
import scipy as sp

# needed for default implementation of set_params
from sklearn.base import BaseEstimator, TransformerMixin

class RandomFeaturesSampler(ABC, BaseEstimator, TransformerMixin):
    """Base class for random feature samplers."""

    def __init__(self, n_random_features: int) -> None:
        self.n_random_features = n_random_features
        self.w = None

    def _initialize_w(self, n_features: int) -> np.ndarray:
        pass

    @abstractmethod
    def fit(self, X: np.ndarray) -> np.ndarray:
        """Initialize w's for the random features.
        This should be implemented for each kernel."""
        pass

    def fit_transform(
        self, X: np.ndarray, X_prime: Optional[np.ndarray] = None
    ) -> np.ndarray:
        """Initialize  w's (fit) & compute random features (transform)."""
        n_features = np.shape(X)[1]
        self.fit(n_features)
        return self.transform(X)

    def transform(self, X: np.ndarray) -> np.ndarray:
        """Compute the random features.
        Assumes that the vector of w's has been initialized

        Parameters
        ----------
        X:
            Data matrix of shape (n_instances, n_features).

        Returns
        -------
        random_features:
            Array of shape (n_instances, n_random_features).
        """
        if self.w is None:
            raise ValueError("Use fit_transform to initialize w.")

        n_instances, n_features = np.shape(X)

        if np.shape(self.w)[1] != n_features:
            raise ValueError("Different # of features for X and w.")

        random_features = np.empty((n_instances, self.n_random_features))
        random_features[:, ::2] = np.cos(X @ self.w.T)
        random_features[:, 1::2] = np.sin(X @ self.w.T)

        norm_factor = np.sqrt(self.n_random_features // 2)
        random_features = random_features / norm_factor

        return random_features

class RandomFeaturesSamplerRBF(RandomFeaturesSampler):
    """Random Fourier Features for the RBF kernel."""

    def __init__(self, sigma: float = 1.0, n_random_features: int = 100) -> None:

        self.sigma = sigma
        # Initialize number of random features in parent class
        super().__init__(n_random_features)

    def fit(self, n_features: int = 100) -> np.ndarray:
        """Initialize the w's for the random features."""
        w_mean = np.zeros(n_features)
        w_cov_matrix = self.sigma**2 * np.identity(n_features)

        rng = np.random.default_rng()
        self.w = rng.multivariate_normal(
            w_mean,
            w_cov_matrix,
            self.n_random_features // 2,
        )

class RandomFeaturesSamplerMatern(RandomFeaturesSampler):
    """Random Fourier Features for the Matern kernel."""

    def __init__(
        self, scale: float = 1.0, nu: float = 1.0, n_random_features: int = 100
    ) -> None:
        """The Fourier transform of the Matérn kernel is a
        Student's t distribution with twice the degrees of freedom.
        Ref. Chapter 4 of
        Carl Edward Rasmussen and Christopher K. I. Williams. 2005.
        Gaussian Processes for Machine Learning
        (Adaptive Computation and Machine Learning). The MIT Press.
        There is probably a mistake with the scale factor.
        """

        # Changed variables since they CANNNOT be modified due to sklearn GridSearch
        # self.nu = 2.0 * nu
        self.nu = nu
        # Changed name from original's author name due to incompatibility with sklearn
        self.scale = scale

        super().__init__(n_random_features)
        self.w = None

    def fit(self, n_features: int) -> np.ndarray:
        """Compute w's for random Matérn features."""
        # Scale of the Fourier tranform of the kernel
        w_mean = np.zeros(n_features)
        w_cov_matrix = self.scale**2 * np.identity(n_features)

        self.w = random_multivariate_student_t(
            w_mean,
            w_cov_matrix,
            self.nu,
            self.n_random_features // 2,
        )

def random_multivariate_student_t(
    mean: np.ndarray,
    cov_matrix: np.ndarray,
    degrees_of_freedom: float,
    n_samples: int,
) -> np.ndarray:
    """Generate samples from a multivariate Student's t.
    https://en.wikipedia.org/wiki/Multivariate_t-distribution#Definition
    """

    # Dimensions of multivariate Student's t distribution.
    D = len(mean)

    rng = np.random.default_rng()
    x = rng.chisquare(degrees_of_freedom, n_samples) / degrees_of_freedom

    Z = rng.multivariate_normal(
        np.zeros(D),
        cov_matrix,
        n_samples,
    )

    X = mean + Z / np.sqrt(x)[:, np.newaxis]
    return X

class NystroemFeaturesSampler(BaseEstimator, TransformerMixin):
    """Sample Nystroem features."""

    def __init__(
        self,
        kernel: Callable[[np.ndarray, np.ndarray], np.ndarray] = RBF(),
        n_random_features: int = 100,
    ) -> None:
        # _kernel -> kernel due to SKlearn compatibility
        self.kernel = kernel
        self.component_indices_ = None
        # J
        self._X_reduced = None
        # W
        self._reduced_kernel_matrix = None
        # (W+)^1/2
        self._sqrtm_pinv_reduced_kernel_matrix = None
        # Needed for compatibility
        self.n_random_features = n_random_features

    def fit(
        self, X: np.ndarray, y: Optional[np.ndarray] = None  # Compatibility
    ) -> np.ndarray:
        """Precompute auxiliary quantities for Nystroem features."""
        n_instances = len(X)
        # Check dimensions to avoid selecting more features than
        # possible
        n_random_features = self.n_random_features
        if self.n_random_features > n_instances:
            n_random_features = n_instances

        # Sample subset of training instances.
        rng = np.random.default_rng()
        self.component_indices_ = rng.choice(
            range(n_instances),
            size=n_random_features,
            replace=False,
        )

        self._X_reduced = X[self.component_indices_, :]

        # Compute reduced kernel matrix.
        self._reduced_kernel_matrix = self.kernel(self._X_reduced, self._X_reduced)
        self._reduced_kernel_matrix = (
            self._reduced_kernel_matrix + self._reduced_kernel_matrix.T
        ) / 2.0  # enforce symmetry of kernel matrix

        # Compute auxiliary quantities.
        self._sqrtm_pinv_reduced_kernel_matrix = sp.linalg.sqrtm(
            np.linalg.pinv(self._reduced_kernel_matrix, rcond=1.0e-6, hermitian=True)
        )

        # Check that complex part is negligible and eliminate it
        if np.iscomplexobj(self._sqrtm_pinv_reduced_kernel_matrix):
            threshold_imaginary_part = 1.0e-6
            max_imaginary_part = np.max(
                np.abs(np.imag(self._sqrtm_pinv_reduced_kernel_matrix))
            )
            # if max_imaginary_part > threshold_imaginary_part:
            # warnings.warn("Maximum imaginary part is {}".format(max_imaginary_part))

            self._sqrtm_pinv_reduced_kernel_matrix = np.real(
                self._sqrtm_pinv_reduced_kernel_matrix
            )

    def approximate_kernel_matrix(
        self,
        X: np.ndarray,
        # Needed for sklearn compatibility
        X_prime: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """Approximate the kernel matrix using Nystroem features."""

        X_features = self.fit_transform(X)
        return X_features @ X_features.T

    def fit_transform(
        self,
        X: np.ndarray,
        X_prime: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """Compute Nyström features."""
        self.fit(X)
        return self.transform(X)

    def transform(self, X_prime: np.ndarray) -> np.ndarray:
        """Compute Nystroem features with precomputed quantities."""
        J = self.kernel(X_prime, self._X_reduced)
        return J @ self._sqrtm_pinv_reduced_kernel_matrix

def demo_kernel_approximation_features(
    X: np.ndarray,
    kernel: Callable[[np.ndarray, np.ndarray], np.ndarray],
    features_sampler_class: Union[
        Type[RandomFeaturesSampler], Type[NystroemFeaturesSampler]
    ],
    features_sampler_kwargs: np.ndarray,
    n_features: np.array,
) -> None:
    """Kernel approximation using random sampled features.
    Either RFF or Nyström features."""
    n_plots = len(n_features) + 1
    fig, axes = plt.subplots(1, n_plots)
    fig.set_size_inches(15, 4)
    font = {"fontname": "arial", "fontsize": 18}

    kernel_matrix = kernel(X, X)
    axes[0].imshow(kernel_matrix, cmap=plt.cm.Blues)
    axes[0].set_title("Exact kernel", **font)
    axes[0].set_xticks([])
    axes[0].set_yticks([])

    for n_f, ax in zip(n_features, axes[1:]):
        print("# of features = ", n_f)

        features_sampler = features_sampler_class(
            **features_sampler_kwargs, n_random_features=n_f
        )

        X_features = features_sampler.fit_transform(X)
        kernel_matrix_approx = X_features @ X_features.T

        ax.imshow(kernel_matrix_approx, cmap=plt.cm.Blues)

        err_approx = kernel_matrix - kernel_matrix_approx
        err_mean = np.mean(np.abs(err_approx))
        err_max = np.max(np.abs(err_approx))

        ax.set_xlabel(
            "err (mean) = {:.4f} \n err (max) = {:.4f}".format(err_mean, err_max),
            **font
        )

        ax.set_title("{} features".format(n_f), **font)

        ax.set_xticks([])
        ax.set_yticks([])
        plt.tight_layout()
    plt.show()

def plot_kernel_error(
    X: np.ndarray,
    kernel: Callable[[np.ndarray, np.ndarray], np.ndarray],
    features_sampler_class: Union[
        Type[RandomFeaturesSampler], Type[NystroemFeaturesSampler]
    ],
    features_sampler_kwargs: np.ndarray,
    n_features: np.ndarray,
    kernel_name: str,
) -> None:
    # Initialize variables
    kernel_matrix = kernel(X, X)
    errors = np.array([], dtype=float)

    for n_f in n_features:
        # Create sampler
        features_sampler = features_sampler_class(
            **features_sampler_kwargs, n_random_features=n_f
        )

        # Compute approximation
        X_features = features_sampler.fit_transform(X)
        kernel_matrix_approx = X_features @ X_features.T

        # Compute error
        mean_error = np.mean(np.abs(kernel_matrix - kernel_matrix_approx))
        errors = np.append(errors, mean_error)

    # Plotting
    _, ax = plt.subplots(1, 1, figsize=(10, 6))
    font = {"fontname": "arial", "fontsize": 18}
    ax.plot(n_features, errors, color="deepskyblue", label=r"kernel error")
    ax.set_title("{} kernel error study".format(kernel_name), **font)

if __name__ == "__main__":

    """
    import matplotlib.pyplot as plt
    import numpy as np
    from sklearn import datasets, svm
    from sklearn.kernel_approximation import RBFSampler
    from sklearn.metrics.pairwise import rbf_kernel

    import kernel_approximation as ka

    # A not so simple 2 D problem
    X, Y = datasets.make_moons(n_samples=100, noise=0.3, random_state=0)

    # Compute grid of points for plotting the decision regions
    grid_x, grid_y = np.meshgrid(
        np.linspace(-3, 3, 50),
        np.linspace(-3, 3, 50),
    )

    grid_X = np.c_[grid_x.ravel(), grid_y.ravel()]

    gamma = 0.5
    # Kernel matrix
    def kernel(X, Y):
        return rbf_kernel(X, Y, gamma=gamma)

    n_nystroem_features = 20

    nystroem_sampler = NystroemFeaturesSampler(kernel)
    nystroem_features = nystroem_sampler.fit_transform(X)
    nystroem_features_grid = nystroem_sampler.transform(grid_X)

    clf = svm.SVC(kernel='linear')
    # clf = svm.NuSVC(gamma='auto')
    clf.fit(nystroem_features, Y)

    """
    import matplotlib.pyplot as plt
    import numpy as np
    from sklearn import datasets
    from sklearn.gaussian_process.kernels import Matern
    from sklearn.kernel_approximation import RBFSampler
    from sklearn.metrics.pairwise import rbf_kernel

    # 3-D data
    n_instances = 1000
    X, t = datasets.make_s_curve(n_instances, noise=0.1)
    X = X[np.argsort(t)]

    # 2-D data
    # X, y = datasets.make_moons(n_samples=400, noise=.05)
    # X = X[np.argsort(y)]

    # Reshape if necessary
    if X.ndim == 1:
        X = X[:, np.newaxis]

    # Kernel parameters
    sigma = 1.0
    gamma = 1.0 / (2.0 * sigma**2)

    n_nystroem_features = [10, 100, 1000]

    # Kernel function
    def kernel(X, Y):
        return rbf_kernel(X, Y, gamma=gamma)

    nystroem_features = NystroemFeaturesSampler(kernel)

    demo_kernel_approximation_features(
        X,
        kernel,
        nystroem_features,
        n_nystroem_features,
    )


import requests
from urllib.parse import urlencode
import re
from requests import codes
import os
from hashlib import md5

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Host': 'p3-tt.bytecdn.cn',
    'Upgrade-Insecure-Requests': 1,
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
}

def get_page(offset):
    params = {
        'aid': '24',
        'offset': offset,
        'format': 'json',
        # 'keyword': '街拍',
        'autoload': 'true',
        'count': '20',
        'cur_tab': '1',
        'from': 'search_tab',
        'pd': 'synthesis'
    }
    base_url = 'https://www.toutiao.com/api/search/content/?keyword=%E8%A5%BF%E5%AE%89'
    url = base_url + urlencode(params)
    try:
        resp = requests.get(url)
        print(url)
        if 200 == resp.status_code:
            print(resp.json())
            return resp.json()
    except requests.ConnectionError:
        return None

items = []

def get_images(json):
    if json.get('data'):
        data = json.get('data')
        for item in data:
            title = item.get('title')
            images = item.get('image_list')

            if title is None or images is None:
                continue

            for image in images:
                origin_image = re.sub("list/300x196/", "origin/", image.get('url'))

                print(image.get('url'))
                print(origin_image)
                print("---")

                # 返回一个生成器
                map = {
                    # 'image': origin_image,
                    'image': origin_image,
                    'title': title
                }

                items.append(map)

def save_image(item):
    # 获取文件系统分隔符
    img_path = '/Users/zzzz/Desktop/image' + os.path.sep + item.get('title')
    if not os.path.exists(img_path):
        os.makedirs(img_path)
    try:
        resp = requests.get(item.get('image'), headers)
        if codes.ok == resp.status_code:
            file_path = img_path + os.path.sep + '{file_name}.{file_suffix}'.format(
                file_name=md5(resp.content).hexdigest(),
                file_suffix='jpg')
            if not os.path.exists(file_path):

                with open(file_path, 'wb') as f:
                    f.write(resp.content)
                print('Downloaded image path is %s' % file_path)
            else:
                print('Already Downloaded', file_path)
    except requests.ConnectionError:
        print('Failed to Save Image，item %s' % item)

def main(offset):

    for offset in range(0,100,20):
        json = get_page(offset)
        get_images(json)
        for item in items:
            save_image(item)

main(0)


import numpy as np
import pandas as pd
import statsmodels.api as sm
from pandas.tseries.offsets import MonthEnd
from prettytable import PrettyTable, MARKDOWN
from sklearn.kernel_ridge import KernelRidge
from sklearn.linear_model import LassoCV, RidgeCV
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from regression.regression_models import *
from utils.utils import Dataset, get_dataset

def get_regressors():
    return [
        'Lineare',
        'Lineare robusto (Huber)',
        'Lineare avanzato (Cook)',
        'Ridge',
        'Lasso',
        'Polinomiale (grado 2)',
        'Polinomiale (grado 3)',
        'Random Forest',
        'Gradient Boosting',
        'SVR (Kernel lineare)',
        'SVR (Kernel polinomiale)',
        'SVR (Kernel RBF)',
        'KernelRidge'
    ]

def get_regressors_smaller():
    return [
        'Lineare',
        'Lineare robusto (Huber)',
        'Lineare avanzato (Cook)',
        'Ridge',
        'Lasso',
        'Polinomiale (grado 2)',
        'Polinomiale (grado 3)',
        'Random Forest',
        'Gradient Boosting',
        'SVR (lineare)',
        'SVR (polinomiale)',
        'SVR (RBF)',
        'KernelRidge'
    ]

def avg(scores):
    return '%0.2f' % (sum(scores) / len(scores))

def make_avg(predictor, X, y, n=500):
    r2_scores, rmse_scores = [], []
    for _ in range(n):
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, shuffle=True)
        predictor.fit(X_train, y_train)
        r2_scores.append(predictor.score(X_test, y_test))
        rmse_scores.append(mean_squared_error(y_test, predictor.predict(X_test), squared=False))
    return avg(r2_scores), avg(rmse_scores)

def apply_linear_regression(X, y):
    return make_avg(get_linear_model(), X, y)

def apply_cook_regression(X, y):
    m = sm.OLS(y, sm.add_constant(X)).fit()
    infl = m.get_influence()
    soglia = 4 / len(X)
    (a, p) = infl.cooks_distance
    mask = a < soglia
    return make_avg(get_linear_model(), X[mask], y[mask])

def apply_huber_regression(X, y):
    return make_avg(get_huber_model(), X, y)

def apply_lasso_regression(X, y):
    lassoregcv = LassoCV(n_alphas=100, random_state=1)
    model = make_pipeline(StandardScaler(with_mean=False), lassoregcv)
    return make_avg(model, X, y)

def apply_ridge_regression(X, y):
    alpha_range = 10. ** np.arange(-2, 5)
    ridgereg = RidgeCV(alphas=alpha_range, scoring='neg_mean_squared_error')
    model = make_pipeline(StandardScaler(with_mean=False), ridgereg)
    return make_avg(model, X, y)

def apply_poly_regression(X, y):
    return make_avg(get_polynomial_model(), X, y)

def apply_poly_3_regression(X, y):
    m = make_pipeline(PolynomialFeatures(degree=3, include_bias=False), linear_model.LinearRegression(n_jobs=-1))
    return make_avg(m, X, y)

def apply_kridge_regression(X, y):
    kr = make_pipeline(StandardScaler(with_mean=False),
                       KernelRidge(alpha=1, kernel='poly', degree=2, gamma=1, coef0=1))
    return make_avg(kr, X, y)

def apply_random_forest_regression(X, y):
    return make_avg(get_random_forest_model(), X, y)

def apply_gradient_boosting_regression(X, y):
    return make_avg(get_gradient_boosting_model(), X, y)

def apply_svr_linear_regression(X, y):
    return make_avg(get_svr_linear_model(), X, y)

def apply_svr_polynomial_regression(X, y):
    return make_avg(get_svr_polynomial_model(), X, y)

def apply_rbf_polynomial_regression(X, y):
    return make_avg(get_svr_rbf_model(), X, y)

def evaluate(X, y):
    lr_score, lr_rmse = apply_linear_regression(X, y)
    huber_score, huber_rmse = apply_huber_regression(X, y)
    cook_score, cook_rmse = apply_cook_regression(X, y)

    ridge_score, ridge_rmse = apply_ridge_regression(X, y)
    lasso_score, lasso_rmse = apply_lasso_regression(X, y)
    kridge_score, kridge_rmse = apply_kridge_regression(X, y)

    poly_score, poly_rmse = apply_poly_regression(X, y)
    poly3_score, poly3_rmse = apply_poly_3_regression(X, y)
    rf_score, rf_rmse = apply_random_forest_regression(X, y)
    gb_score, gb_rmse = apply_gradient_boosting_regression(X, y)
    svr_lin_score, svr_lin_rmse = apply_svr_linear_regression(X, y)
    svr_poly_score, svr_poly_rmse = apply_svr_polynomial_regression(X, y)
    svr_rbf_score, svr_rbf_rmse = apply_rbf_polynomial_regression(X, y)

    r2_scores = [lr_score, huber_score, cook_score, ridge_score, lasso_score, poly_score, poly3_score, rf_score,
                 gb_score, svr_lin_score, svr_poly_score,
                 svr_rbf_score, kridge_score]
    rmse_scores = [lr_rmse, huber_rmse, cook_rmse, ridge_rmse, lasso_rmse, poly_rmse, poly3_rmse, rf_rmse, gb_rmse,
                   svr_lin_rmse, svr_poly_rmse,
                   svr_rbf_rmse, kridge_rmse]

    return r2_scores, rmse_scores

def print_annual_results(results, title):
    table = PrettyTable()
    # table.title = title
    table.add_column('Modello di regressione', get_regressors())
    table.add_column('R²', results[0])
    table.add_column('RMSE (µg/m³)', results[1])
    table.align = 'l'
    table.set_style(MARKDOWN)
    print(table)

def print_monthly_results(month_results, title):
    def construct_table(i):
        t = PrettyTable()
        # t.title = '{} | '.format('R²' if i == 0 else 'RMSE (µg/m³)') + title
        t.add_column('Modello', get_regressors_smaller())
        for res in month_results:
            if any(res['results'][i]):
                t.add_column(res['period'], res['results'][i])
        t.align = 'l'
        return t

    # R²
    table = construct_table(0)
    table.set_style(MARKDOWN)
    print(table)

    # RMSE
    table = construct_table(1)
    table.set_style(MARKDOWN)
    print(table)

def annual_summary(dataset, station, chemical):
    X = dataset['airqino_{}'.format(chemical)].values.reshape((-1, 1))
    y = dataset['arpat_{}'.format(chemical)].values
    results = evaluate(X, y)
    print_annual_results(results,
                         title='All year | {n} | {c} (n={l})'.format(c=chemical.upper(), n=station,
                                                                     l=str(len(dataset))))

def monthly_summary(dataset, station, chemical, is_24h=False):
    if chemical == 'no2':
        date_range = pd.date_range('2020-01-01', '2020-12-31', freq='MS')
    else:
        date_range = pd.date_range('2020-09-01', '2021-08-31', freq='MS')
    month_results = []
    for month in date_range:
        month_str = month.strftime('%b')
        month_start = month.strftime('%Y-%m-%d')
        month_end = (month + MonthEnd(1)).strftime('%Y-%m-%d')

        month_dataset = dataset.loc[month_start: month_end]
        threshold = 5 if is_24h else 70
        if month_dataset.empty or len(month_dataset.index) < threshold:
            month_results.append({
                'results': [[0 for _ in get_regressors()], [0 for _ in get_regressors()]],
                'period': month_str,
                'values_count': str(len(month_dataset.index))
            })
            continue

        X = month_dataset['airqino_{}'.format(chemical)].values.reshape((-1, 1))
        y = month_dataset['arpat_{}'.format(chemical)].values

        results = evaluate(X, y)
        month_results.append({'results': results, 'period': month_str, 'values_count': str(len(y))})

    print_monthly_results(month_results, title='Monthly results | {d} | {c}'.format(c=chemical.upper(), d=station))

if __name__ == '__main__':
    # SMART16 - NO2
    # dataset = get_dataset(Dataset.SMART16_NO2).loc['2020-01-18': '2020-12-31']
    # annual_summary(dataset=dataset, station='SMART16-CAPANNORI', chemical='no2')
    # monthly_summary(dataset=dataset, station='SMART16-CAPANNORI', chemical='no2')

    # SMART16_new - PM2.5
    # annual_summary(dataset=get_dataset(Dataset.SMART16_NEW_PM), station='SMART16_new-CAPANNORI', chemical='pm2.5')
    # monthly_summary(dataset=get_dataset(Dataset.SMART16_NEW_PM), station='SMART16_new-CAPANNORI', chemical='pm2.5')

    # SMART16_new - PM10
    # annual_summary(dataset=get_dataset(Dataset.SMART16_NEW_PM), station='SMART16_new-CAPANNORI', chemical='pm10')
    # monthly_summary(dataset=get_dataset(Dataset.SMART16_NEW_PM), station='SMART16_new-CAPANNORI', chemical='pm10')

    # SMART16_new - PM2.5 - 8H
    # annual_summary(get_dataset(Dataset.SMART16_NEW_PM_12H), 'SMART16_new-CAPANNORI-8h', 'pm2.5')
    # monthly_summary(get_dataset(Dataset.SMART16_NEW_PM_12H), 'SMART16_new-CAPANNORI-12h', 'pm2.5', is_24h=True)

    # SMART16_new - PM10 - 8H
    # annual_summary(get_dataset(Dataset.SMART16_NEW_PM_12H), 'SMART16_new-CAPANNORI-8h', 'pm10')
    monthly_summary(get_dataset(Dataset.SMART16_NEW_PM_12H), 'SMART16_new-CAPANNORI-8h', 'pm10', is_24h=True)


import sys
from global_executor import run_executor
from pepper_cmd_classes import Sonar, Dialogue
sys.path.append('tablet/scripts')
from modim_classes import CleanScreen

if __name__ == "__main__":

	print("Starting monitor, to stop robot send KeyboardInterrupt signal.")
	while True:
		try:
			sonar = Sonar()
			location = sonar.listen(personHere = False)
			if location == 'front':
				executor_status = run_executor()
				if executor_status == -1:
					print("Interaction with user completed successfully.")
			else:
				dialogue = Dialogue()
				dialogue.say("Who is behind me? If you came to me, come please in front so I could see you.")
				print("Person was detected by rear sonar.")
		except KeyboardInterrupt:
			print("Interrupted")
			CleanScreen()
			sys.exit(0)

Master degree/Elective in AI 2/playground/main.py


# use open cv to show new images from AirSim

from PythonClient import *
import cv2
import time
import sys

client = AirSimClient('127.0.0.1')

# get depth image
result = client.setImageTypeForCamera(0, AirSimImageType.Depth)

time.sleep(1) # give it time to render
help = False

while True:
    # because this method returns std::vector<uint8>, msgpack decides to encode it as a string unfortunately.
    result = client.getImageForCamera(0, AirSimImageType.Depth)
    if (result == "\0"):
        if (not help):
            help = True
            print("Please press '1' in the AirSim view to enable the Depth camera view")
    else:
        rawImage = np.fromstring(result, np.int8)
        png = cv2.imdecode(rawImage, cv2.IMREAD_UNCHANGED)
        cv2.imshow("Traffic", png)

    key = cv2.waitKey(1) & 0xFF;
    if (key == 27 or key == ord('q') or key == ord('x')):
        break;


# -*- coding: utf-8 -*-
#Cluedo
#Initialisation tableau

profil = [('Colonel Moutarde', 'CCTGGAGGGTGGCCCCACCGGCCGAGACAGCGAGCATATGCAGGAAGCGGCAGGAATAAGGAAAA'), ('Mlle Rose', 'CTCCTGATGCTCCTCGCTTGGTGGTTTGAGTGGACCTCCCAGGCCAGTGCCGGGCCCCTCATAGGAGAGGADN'), ('Mme Pervenche', 'AAGCTCGGGAGGTGGCCAGGCGGCAGGAAGGCGCACCCCCCCAGTACTCCGCGCGCCGGGACAGAATGCCADN'), ('M. Leblanc', 'CTGCAGGAACTTCTTCTGGAAGTACTTCTCCTCCTGCAAATAAAACCTCACCCATGAATGCTCACGCAAG')]

def enquete (adn) :

    # Initialisation variable
    data1 = "CATA"
    data2 = "ATGC"
    datall1 = "CATATGC"
    datall2 = "ATGCATA"

    for i in range (0,len(profil)):
        if profil[i][1].count(data1) != 0 and profil[i][1].count(data2) != 0 and (profil[i][1].count(datall1) + profil[i][1].count(datall2)) == 0:
            print (profil[i][0] + " est coupable")
            return
        else:
            print (profil[i][0] + " est innocent")

cremier = enquete(profil)


# @Time : 2020/3/3 14:04
# @Author : Xylia_Yang
# @Description :二叉搜索树与双向链表(返回双向链表的头节点)

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def Convert(self, pRootOfTree):
        if not pRootOfTree:
            return None

        self.lastNode=None
        self.converCore(pRootOfTree)

        pHead=self.lastNode
        while pHead.left:
            pHead=pHead.left
        return pHead

    def converCore(self,pNode):
        if not pNode:
            return

        if pNode.left:
            self.converCore(pNode.left)

        pNode.left=self.lastNode

        if  self.lastNode:
            self.lastNode.right=pNode

        self.lastNode=pNode

        if pNode.right:
            self.converCore(pNode.right)


import os
from PIL import Image
import torchvision
import torch, cv2, math, random
import numpy as np

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
device_id = [0]
IMG_EXTENSIONS = ['jpg', 'jpeg', 'png', 'ppm', 'bmp', 'pgm']

def mkdir(path):
    if not os.path.exists(path):
        os.makedirs(path)

def pil_loader(path, mode='RGB'):
    """
    open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
    :param path: image path
    :return: PIL.Image
    """
    assert _is_image_file(path), "%s is not an image" % path
    with open(path, 'rb') as f:
        with Image.open(f) as img:
            return img.convert(mode)

def calculate_RF(model):
    layers = getLayers(model)
    r = 1
    for layer in layers[::-1]:
        if isinstance(layer, torch.nn.Conv2d):
            kernel = layer.kernel_size[0]
            padding = layer.padding[0]
            stride = layer.stride[0]
            r = stride * r + (kernel - stride)
    return r

def getLayers(model):
    """
    get each layer's name and its module
    :param model:
    :return: each layer's name and its module
    """
    layers = []

    def unfoldLayer(model):
        """
        unfold each layer
        :param model: the given model or a single layer
        :param root: root name
        :return:
        """

        # get all layers of the model
        layer_list = list(model.named_children())
        for item in layer_list:
            module = item[1]
            sublayer = list(module.named_children())
            sublayer_num = len(sublayer)

            # if current layer contains sublayers, add current layer name on its sublayers
            if sublayer_num == 0:
                layers.append(module)
            # if current layer contains sublayers, unfold them
            elif isinstance(module, torch.nn.Module):
                unfoldLayer(module)

    unfoldLayer(model)
    return layers

def load_as_tensor(path, mode='RGB'):
    """
    Load image to tensor
    :param path: image path
    :param mode: 'Y' returns 1 channel tensor, 'RGB' returns 3 channels, 'RGBA' returns 4 channels, 'YCbCr' returns 3 channels
    :return: 3D tensor
    """
    if mode != 'Y':
        return PIL2Tensor(pil_loader(path, mode=mode))
    else:
        return PIL2Tensor(pil_loader(path, mode='YCbCr'))[:1]

def PIL2Tensor(pil_image):
    return torchvision.transforms.functional.to_tensor(pil_image)

def Tensor2PIL(tensor_image, mode='RGB'):
    if len(tensor_image.size()) == 4 and tensor_image.size()[0] == 1:
        tensor_image = tensor_image.view(tensor_image.size()[1:])
    return torchvision.transforms.functional.to_pil_image(tensor_image.detach().cpu(), mode=mode)

def _is_image_file(filename):
    """
    judge if the file is an image file
    :param filename: path
    :return: bool of judgement
    """
    filename_lower = filename.lower()
    return any(filename_lower.endswith(ext) for ext in IMG_EXTENSIONS)

def image_files(path):
    """
    return list of images in the path
    :param path: path to Data Folder, absolute path
    :return: 1D list of image files absolute path
    """
    abs_path = os.path.abspath(path)
    image_files = os.listdir(abs_path)
    for i in range(len(image_files)):
        if (not os.path.isdir(image_files[i])) and (_is_image_file(image_files[i])):
            image_files[i] = os.path.join(abs_path, image_files[i])
    return image_files

def split_to_batches(l, n):
    for i in range(0, len(l), n):
        yield l[i:i + n]

def _sigmoid_to_tanh(x):
    """
    range [0, 1] to range [-1, 1]
    :param x: tensor type
    :return: tensor
    """
    return (x - 0.5) * 2.

def _tanh_to_sigmoid(x):
    """
    range [-1, 1] to range [0, 1]
    :param x:
    :return:
    """
    return x * 0.5 + 0.5

def _add_batch_one(tensor):
    """
    Return a tensor with size (1, ) + tensor.size
    :param tensor: 2D or 3D tensor
    :return: 3D or 4D tensor
    """
    return tensor.view((1, ) + tensor.size())

def _remove_batch(tensor):
    """
    Return a tensor with size tensor.size()[1:]
    :param tensor: 3D or 4D tensor
    :return: 2D or 3D tensor
    """
    return tensor.view(tensor.size()[1:])

def mod_crop(tensor, scale=4):
    B, C, H, W = tensor.shape
    return tensor[:, :, :H-H % scale, :W-W % scale]

def isotropic_gaussian_kernel(l, sigma, epsilon=1e-5):
    ax = np.arange(-l // 2 + 1., l // 2 + 1.)
    xx, yy = np.meshgrid(ax, ax)
    kernel = np.exp(-(xx ** 2 + yy ** 2) / (2. * (sigma + epsilon) ** 2))
    return kernel / np.sum(kernel)

def prepare_images(hr_path, scale=4):
    hr_pil = Image.open(hr_path)
    sizex, sizey = hr_pil.size
    hr_pil = hr_pil.crop((0, 0, sizex - sizex % scale, sizey - sizey % scale))
    sizex, sizey = hr_pil.size
    lr_pil = hr_pil.resize((sizex // scale, sizey // scale), Image.BICUBIC)
    return lr_pil, hr_pil

def same_seeds(seed = 2022):
    random.seed(seed)
    np.random.seed(seed)  # 保证后续使用random函数时，产生固定的随机数
    torch.manual_seed(seed)  # 固定随机种子（CPU）
    # if torch.cuda.is_available():  # 固定随机种子（GPU)
    torch.cuda.manual_seed(seed)  # 为当前GPU设置
    torch.cuda.manual_seed_all(seed)  # 为所有GPU设置

    torch.use_deterministic_algorithms(True)
    torch.backends.cudnn.enabled = False
    torch.backends.cudnn.benchmark = False  # GPU、网络结构固定，可设置为True
    torch.backends.cudnn.deterministic = True  # 固定网络结构
    os.environ['PYTHONHASHSEED'] = str(seed)
    os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8'

def post_process(input):
    out = torch.clamp(input, min=0., max=1.)
    out = np.moveaxis(torch.squeeze(out,0).detach().cpu().numpy(), 0, 2)
    out = out * 255
    out = out.astype(np.uint8)
    return out

def calculate_psnr(img1, img2):
    # img1 and img2 have range [0, 255]
    img1 = img1.astype(np.float64)
    img2 = img2.astype(np.float64)
    mse = np.mean((img1 - img2) ** 2)
    if mse == 0:
        return float("inf")
    return 20 * math.log10(255.0 / math.sqrt(mse))

def ssim(img1, img2):
    C1 = (0.01 * 255) ** 2
    C2 = (0.03 * 255) ** 2

    img1 = img1.astype(np.float64)
    img2 = img2.astype(np.float64)
    kernel = cv2.getGaussianKernel(11, 1.5)
    window = np.outer(kernel, kernel.transpose())

    mu1 = cv2.filter2D(img1, -1, window)[5:-5, 5:-5]  # valid
    mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5]
    mu1_sq = mu1 ** 2
    mu2_sq = mu2 ** 2
    mu1_mu2 = mu1 * mu2
    sigma1_sq = cv2.filter2D(img1 ** 2, -1, window)[5:-5, 5:-5] - mu1_sq
    sigma2_sq = cv2.filter2D(img2 ** 2, -1, window)[5:-5, 5:-5] - mu2_sq
    sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2

    ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / (
        (mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2)
    )
    return ssim_map.mean()

def calculate_ssim(img1, img2):
    """calculate SSIM
    the same outputs as MATLAB's
    img1, img2: [0, 255]
    """
    if not img1.shape == img2.shape:
        raise ValueError("Input images must have the same dimensions.")
    if img1.ndim == 2:
        return ssim(img1, img2)
    elif img1.ndim == 3:
        if img1.shape[2] == 3:
            ssims = []
            for i in range(3):
                ssims.append(ssim(img1, img2))
            return np.array(ssims).mean()
        elif img1.shape[2] == 1:
            return ssim(np.squeeze(img1), np.squeeze(img2))
    else:
        raise ValueError("Wrong input image dimensions.")

# def calculate_psnr(img1, img2, border=0):
#     # img1 and img2 have range [0, 255]
#     #img1 = img1.squeeze()
#     #img2 = img2.squeeze()
#     if not img1.shape == img2.shape:
#         raise ValueError('Input images must have the same dimensions.')
#     h, w = img1.shape[:2]
#     img1 = img1[border:h-border, border:w-border]
#     img2 = img2[border:h-border, border:w-border]
#
#     img1 = img1.astype(np.float64)
#     img2 = img2.astype(np.float64)
#     mse = np.mean((img1 - img2)**2)
#     if mse == 0:
#         return float('inf')
#     return 20 * math.log10(255.0 / math.sqrt(mse))
#
#
# # --------------------------------------------
# # SSIM
# # --------------------------------------------
# def calculate_ssim(img1, img2, border=0):
#     '''calculate SSIM
#     the same outputs as MATLAB's
#     img1, img2: [0, 255]
#     '''
#     #img1 = img1.squeeze()
#     #img2 = img2.squeeze()
#     if not img1.shape == img2.shape:
#         raise ValueError('Input images must have the same dimensions.')
#     h, w = img1.shape[:2]
#     img1 = img1[border:h-border, border:w-border]
#     img2 = img2[border:h-border, border:w-border]
#
#     if img1.ndim == 2:
#         return ssim(img1, img2)
#     elif img1.ndim == 3:
#         if img1.shape[2] == 3:
#             ssims = []
#             for i in range(3):
#                 ssims.append(ssim(img1[:,:,i], img2[:,:,i]))
#             return np.array(ssims).mean()
#         elif img1.shape[2] == 1:
#             return ssim(np.squeeze(img1), np.squeeze(img2))
#     else:
#         raise ValueError('Wrong input image dimensions.')
#
#
# def ssim(img1, img2):
#     C1 = (0.01 * 255)**2
#     C2 = (0.03 * 255)**2
#
#     img1 = img1.astype(np.float64)
#     img2 = img2.astype(np.float64)
#     kernel = cv2.getGaussianKernel(11, 1.5)
#     window = np.outer(kernel, kernel.transpose())
#
#     mu1 = cv2.filter2D(img1, -1, window)[5:-5, 5:-5]  # valid
#     mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5]
#     mu1_sq = mu1**2
#     mu2_sq = mu2**2
#     mu1_mu2 = mu1 * mu2
#     sigma1_sq = cv2.filter2D(img1**2, -1, window)[5:-5, 5:-5] - mu1_sq
#     sigma2_sq = cv2.filter2D(img2**2, -1, window)[5:-5, 5:-5] - mu2_sq
#     sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2
#
#     ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) *
#                                                             (sigma1_sq + sigma2_sq + C2))
#     return ssim_map.mean()

def cv2_to_pil(img):
    image = Image.fromarray(cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_BGR2RGB))
    return image

def pil_to_cv2(img):
    image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
    return image


import math

def getDigit(number, placeValue):
    return math.floor(abs(number) / pow(10, placeValue)) % 10

def digitCount(number):
    if number == 0: return 1
    return math.floor(math.log10(abs(number))) + 1

def mostDigits(arrayInput):
    maxDigits = 0
    for number in arrayInput:
        maxDigits = max(maxDigits, digitCount(number))
    return maxDigits

def radixSort(arrayInput):
    maxDigitCount = int(mostDigits(arrayInput))

    for iteration in range(maxDigitCount):
        digitsBucket = [[] for i in range(10)]

        for index in range(len(arrayInput)):
            digit = int(getDigit(arrayInput[index], iteration))
            digitsBucket[digit].append(arrayInput[index])

        output = []
        for listElement in digitsBucket:
            if type(listElement) == int:
                output.append(listElement)
            else:
                output.extend(listElement)

        arrayInput = output

    return arrayInput

print(radixSort([99, 44, 6, 2, 421, 5, 63, 8447, 283, 4, 0]))
# a  = [1,2,3,4,5, [6,7]]
# output = []
# for i in a:
#     if type(i) == int:
#         output.append(i)
#     else:
#         output.extend(i)

# for i in output:


'''# {{{
Created on 28 янв. 2019 г.

@author: BuYn
'''# }}}
#import block# {{{
import unittest
from model.globalsvar import *
from presenter.gstodo import GSTodo
# }}}
class Test(unittest.TestCase):# {{{

    @classmethod #setUpClass# {{{
    def setUpClass(self):
        print("*"*33,"*"*33)
        self.gs = GSTodo(filename = 'testToDoProgresDaylis')
#        https://docs.google.com/spreadsheets/d/15EjV7CfotRunq831Ca1yWXtsI04DcVS_ukX0fHs6O7k/edit#gid=0
        # use creds to create a client to interact with the Google Drive API
        print ("file opened")
        print("*"*33,"*"*33)
        #self.gs.sheet_main.update_acell('A1', 'Bingo!')
        # }}}

    @classmethod #tearDownClass# {{{
    def tearDownClass(cls):
        print("*"*33,"*"*33)
        print("tear down module")
        print("*"*33,"*"*33)
# }}}

    def setUp(self):# {{{
        i ="set up"
        print("*"*33,i,"*"*33)
# }}}

    def tearDown(self):# {{{
        i = "tear Down"
        print("*"*33,i,"*"*33)
# }}}

    def test_init(self):# {{{
        self.assertIsNotNone( self.gs.sheet_main)
        self.assertIsNotNone( self.gs.sheet_main.sheet)
        self.assertIsNotNone( self.gs.sheet_calc.sheet)
        print(self.gs.sheet_main.sheet)
        # }}}

    def test_compileNewLogString(self):# {{{
        result = self.gs.compileNewLogString(name = "Подготока ОС", time = 12)
        self.assertEqual(
                result
            , ["2/6/2019 6:13:35","","",12,"Подготока ОС","","","","","",
                "=NOW() - A21","=A21-Blans!$O$1",0.07778,"","",
                "=INT(A21)"])
        #   sheet.insertRowBefore(21);
        #21, 1)("Main")"E4"
        #(21, 3).setValue(value)
        #(21, 4).setValue(time)
        #(21, 5).setValue(name)
        #(21,11).("=NOW() - A21");
        #(21,12)("=A21-Blans!$O$1");
        #(21,13)("Calc")("h13")
        #(21,16)("=INT(A21)").setNumberFormat("M/d/yyyy");
        # }}}
    # }}}
if __name__ == "__main__":# {{{
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
    # }}}


# coding:utf-8 #

import re
import codecs
import sys
import os

def mode1(f_i,label):
    fi=codecs.open(f_i,"r","utf-8")
    f_o_prefix=(os.path.splitext(f_i))[0]
    f_o=f_o_prefix+r"_loc.txt"
    fo=codecs.open(f_o,"w","utf-8")
    context=fi.readlines()
    for line in context:
        result=line.split()
        line_len=len(result)
        fo.write(line.strip()+" ")
        i=0
        for token in result:
            pair=re.match("(.*?)/(.*)",token)
            if pair:
                word=pair.group(1).replace("[","").replace(" ","")
                pos=pair.group(2)
                pos_modify=re.match("(.*)\].*",pos)
                if pos_modify:
                    pos=pos_modify.group(1)
                if pos.startswith(label):
                    fo.write(str(i)+" ")
            i=i+1
        fo.write(str(line_len)+"\n")
    fo.close()
    fi.close()

def mode2(f_i):
    fi=codecs.open(f_i,"r","utf-8")
    f_o_prefix=(os.path.splitext(f_i))[0]
    f_o=f_o_prefix+r"_wp.txt"
    fo=codecs.open(f_o,"w","utf-8")
    context=fi.readlines()
    for line in context:
        result=line.split()
        for token in result:
            pair=re.match("(.*?)/(.*)",token)
            if pair:
                word=pair.group(1).replace("[","").replace(" ","")
                pos=pair.group(2)
                pos_modify=re.match("(.*)\].*",pos)
                if pos_modify:
                    pos=pos_modify.group(1)
                fo.write(word+" "+pos+"\n")
        fo.write("\n")
    fo.close()
    fi.close()

def mode3(f_i):
    fi=codecs.open(f_i,"r","utf-8")#输入文件是mode1的输出文件
    f_o_prefix=(os.path.splitext(f_i))[0]
    f_o_lb=f_o_prefix+r"_lb.txt"
    f_o_rb=f_o_prefix+r"_rb.txt"
    f_o_context=f_o_prefix+r"_context.txt"
    fo_lb=codecs.open(f_o_lb,"w","utf-8")
    fo_rb=codecs.open(f_o_rb,"w","utf-8")
    fo_context=codecs.open(f_o_context,"w","utf-8")
    context=fi.readlines()
    for line in context:
        result=line.strip().split()
        line_len=int(result[-1])
        begin=0
        end=line_len-1
        #word_field=result[0:line_len]
        loc_field=result[line_len:-1]
        for loc in loc_field:
            left_border=result[max(begin,int(loc)-1)].replace("[","").replace(" ","")
            fo_lb.write(left_border+"\n")
            right_border=result[min(int(loc)+1,end)].replace("[","").replace(" ","")
            fo_rb.write(right_border+"\n")
            ner_context=" ".join(result[max(0,int(loc)-5):min(int(loc)+6,end+1)])
            fo_context.write(ner_context+"\n")
    fo_lb.close()
    fo_rb.close()
    fo_context.close()
    fi.close()

def mode4(f_i):
    fi=codecs.open(f_i,"r","utf-8")
    f_o_prefix=(os.path.splitext(f_i))[0]
    f_o=f_o_prefix+r"_ntag.txt"
    fo=codecs.open(f_o,"w","utf-8")
    context=fi.readlines()
    for line in context:
        end=len(line.strip())-1
        i=0
        for char in line.strip():
            if end==0:
                fo.write(char+" S\n")
            elif i==0:
                fo.write(char+" B\n")
            elif i==end:
                fo.write(char+" E\n")
            else:
                fo.write(char+" M\n")
            i=i+1
    fo.close()
    fi.close()

###bash###
#
#   cat [fi] | sort | uniq -c | sort -k1r | grep -v '\]' | grep -v '/[mtxw]' | grep -v ' [[:digit:]] ' | grep -v '/n[stz]' | sed 's/^[ \t]*//g' > [fo]
#   cat [fi] | sed 's/^[ \t]*//g' | cut -d ' ' -f 2 | cut -d / -f 1 > [fo]
#
###bash###

if __name__=='__main__':
    program=os.path.basename(sys.argv[0])
    f_i,parameter,label=sys.argv[1:4]
    if parameter=="1":
        mode1(f_i,label)
    elif parameter=="2":
        mode2(f_i)
    elif parameter=="3":
        mode3(f_i)
    elif parameter=="4":
        mode4(f_i)
    else:
        pass


'''
914. 卡牌分组
给定一副牌，每张牌上都写着一个整数。

此时，你需要选定一个数字 X，使我们可以将整副牌按下述规则分成 1 组或更多组：

每组都有 X 张牌。
组内所有的牌上都写着相同的整数。
仅当你可选的 X >= 2 时返回 true。

思路：统计各张牌的数量，满足各个数量有大于1的公约数
'''
class Solution:
    def hasGroupsSizeX(self, deck:list) -> bool:
        count = {}
        for x in deck:
            if count.get(x) is None:
                count[x] = 1
            else:
                count[x] = count[x] + 1

        l = list(count.values())

        if len(l) == 1:
            if l[0] > 1:
                return True
            else:
                return False
        l.sort()
        base = self.commonBase(l[0], l[1])
        if base is None:
            return False

        for i in range(len(l)):
            if l[i] % base != 0:
                return False

        return True

    def commonBase(self, x, y):
        start = min(x, y)
        end = max(x, y)
        for i in range(2, start + 1):
            if  x % i == 0 and y % i == 0:
                return i

        return None

print(Solution().hasGroupsSizeX([1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3]))


from data_objects.random_cycler import RandomCycler
from data_objects.utterance import Utterance
from pathlib import Path

"""Minimally altered code from https://github.com/Trebolium/Real-Time-Voice-Cloning/tree/master/encoder/data_objects"""

# Contains the set of utterances of a single speaker
class Speaker:
    def __init__(self, root: Path):
        self.root = root
        self.name = root.name
        self.utterances = None
        self.utterance_cycler = None

    def _load_utterances(self, num_feats):
        """ Utterance stores mel and wav paths, but does not make them npy objects until random_partial() is called"""
        spkr_uttrs_list = [f.name[:-4] for f in self.root.glob('*.npy')]
        self.utterances = [Utterance(self.root.joinpath(f+'.npy'), f+'.wav') for f in spkr_uttrs_list]
        self.utterance_cycler = RandomCycler(self.utterances)

    def random_partial(self, count, n_frames, num_feats):
        """
        Samples a batch of <count> unique partial utterances from the disk in a way that all
        utterances come up at least once every two cycles and in a random order every time.

        :param count: The number of partial utterances to sample from the set of utterances from
        that speaker. Utterances are guaranteed not to be repeated if <count> is not larger than
        the number of utterances available.
        :param n_frames: The number of frames in the partial utterance.
        :return: A list of tuples (utterance, frames, range) where utterance is an Utterance,
        frames are the frames of the partial utterances and range is the range of the partial
        utterance with regard to the complete utterance.
        """
        if self.utterances is None:
            self._load_utterances(num_feats)

        utterances = self.utterance_cycler.sample(count)
        """Utterance random__partial returns: (splice of numpy, (start/end values))"""
        a = [(u,) + u.random_partial(n_frames, num_feats) for u in utterances]

        """ a is a list of tuples (uttr objects, spliced uttr_features, start/end coords), size of count"""
        return a


title = ['部长', '课长', '系长', '主任']
data = [
    {'name':'高桥', 'position' : '主任'},
    {'name':'铃木', 'position' : '系长'},
    {'name':'佐藤', 'position' : '部长'},
    {'name':'加藤', 'position' : '课长'},
]

data.sort(key=lambda x: title.index(x['position']))
print(data)


from random import randint, choice
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%d/%m/%Y")
file_name = "math_game_log.txt"

# Functions

def append_wrong_answer(answer,operation,file_name=file_name):
    with open(file_name, 'a') as f:
        f.write(f'\tExpression: {operation}, User Input: {answer}, '\
            f'Expected: {eval(operation)}\n')

def check_if_int(answer):
    '''Checks if the input is an integer. '''
    try:
        int(answer)
    except ValueError:
        print('Your answer cannot contain anything other than numbers. Please try again: ')
        return False

    else:
        return True

def addition(a, b):
    operation = f"{a} + {b}"
    answer = input(operation + ' = ')

    while check_if_int(answer) == False:
        answer = input ('\n'+operation + ' = ')

    if eval(operation) == int(answer):
        return('Excellent!')

    else:
        append_wrong_answer(answer, operation)
        return(f'Incorrect. The correct answer is {eval(operation)}.')

def subtraction(a, b):
    if difficulty == 'easy':
        while b>a:
            x1,x2 = difficulty_lvls['easy'][0][0], difficulty_lvls['easy'][0][-1]
            a, b= randint(x1, x2), randint(x1, x2)

    operation = f"{a} - {b}"
    answer = input (operation + ' = ')

    while check_if_int(answer) == False:
        answer = input ('\n'+operation + ' = ')

    if eval(operation) == int(answer):
        return(f'Correct! Well done, {name}.')
    else:
        append_wrong_answer(answer, operation)
        return(f'Incorrect. The correct answer is {eval(operation)}.')

def multiplication(a, b):
    operation = f"{a} * {b}"
    answer = input(operation + ' = ')

    while check_if_int(answer) == False:
        answer = input ('\n'+operation + ' = ')
    if eval(operation) == int(answer):
        return(f'Good job, {name}!')
    else:
        append_wrong_answer(answer, operation)
        return(f'Incorrect. The correct answer is {eval(operation)}.')

#                           *Actual game:*

"""For the dictionary below:
The first element of each list is the smallest possible number to be used
the second is the largest possible number. The first list is for the addition and
subtraction questionss, the second is for the multiplication questions."""

difficulty_lvls = {
    'easy': [[5, 30], [2, 11]],
    'intermediate': [[10, 50], [3, 12]],
    'difficult':[[12,100],[4, 17]],
    'expert': [[30, 2000], [6, 30]],
    }

name = input('Please enter your name: ').title()
difficulty = input("Difficulty level (easy, intermediate, difficult, expert): ")
while difficulty not in difficulty_lvls:
    difficulty = input("\nYou chose a non-existent level."\
        "Please try again(easy, intermediate, difficult, expert): ")

#     while False
# while difficulty in difficulty_lvls == False:
    # difficulty = input("\nYou chose a non-existent level."\
    #     "Please try again(easy, intermediate, difficult, expert): ")

with open(file_name) as f:
    contents = f.read()

with open(file_name, 'a') as f:
    if current_time not in contents:
        f.write(current_time + ': \n')
    f.write('\n'+name+':')
    f.write('\n\t'+difficulty.title()+'\n')

lst = ['addition', 'subtraction', 'multiplication']

greeting = (f"\nHello, {name}. You'll be given a chance to exit this program "\
" and level up after every 5 attempts. Let's begin! \n")
print(greeting)

i = 0
while True:

    if i%5==0:
        d=difficulty
        x1,x2=difficulty_lvls[d][0][0], difficulty_lvls[d][0][-1]
        y1,y2=difficulty_lvls[d][-1][0], difficulty_lvls[d][-1][-1]
# The issue here is that randint does not accept ranges
    a1,a2 = randint(x1, x2), randint(x1, x2)
    m1,m2 = randint(y1, y2), randint(y1, y2)

    '''Randomizer'''
    chosen = choice(lst)

    if chosen == lst[0]:
        print(addition(a1, a2))
    elif chosen == lst[1]:
        print(subtraction(a1, a2))
    else:
        print(multiplication(m1, m2))

    i+=1

    if i %5 == 0:
        print('\n')
        answer = input(f'Would you like to continue, {name}? (yes/no) ')
        if answer == 'no':
            break
# Here we ask if the user wants to go on to the next level. We must first
# check if the user is not currently at the last level as there is no lvl to lvl up to.
        if difficulty in list(difficulty_lvls.keys())[:-1]:
            lvl_up = input("Would you like to advance to the next level? (yes/no) ")
            if lvl_up == 'yes':
                next_index = list(difficulty_lvls.keys()).index(difficulty)+1
                difficulty = list(difficulty_lvls.keys())[next_index]
                print(f"\nNew difficulty level: {difficulty.title()}\n")
                with open(file_name, 'a') as f:
                    f.write('\n\t'+difficulty.title()+'\n')


#!/usr/bin/python
import argparse
import json
import os
from os import listdir, path
import copy
from pathlib import Path
import re
import pandas as pd
import numpy as np
from bidict import bidict
from scipy import sparse
from sklearn.model_selection import cross_validate, StratifiedKFold
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MultiLabelBinarizer

from echr.utils.folders import make_build_folder
from echr.utils.logger import getlogger
from echr.utils.cli import TAB
from rich.markdown import Markdown
from rich.console import Console
from rich.table import Table
from rich.progress import (
    Progress,
    BarColumn,
    TimeRemainingColumn,
)
from rich.panel import Panel
from rich.tree import Tree
from echr_experiments.config import ROUND_DIGITS, \
                                    SEED, \
                                    MULTICLASS_DESC_OUTPUT_FILE, \
                                    MULTICLASS_OUTPUT_FILE, \
                                    MULTICLASS_ARTICLES, \
                                    MULTICLASS_FLAVORS, \
                                    K_FOLD, \
                                    AS_TIME_SERIES, \
                                    MULTICLASS_CLASSIFIERS, \
                                    DEFAULT_FEATURE_THRESHOLD
from echr_experiments.format import format_filter_output, format_method_output
from echr_experiments.data import load_ECHR_instance, generate_datasets_descriptors
from echr_experiments.scorers import make_scorers, process_score, calculate_average_cm
from echr_experiments.utils import update_classifier_result, \
                                   update_dataset_filter_result, \
                                   update_dataset_metadata, \
                                   update_dataset_result, \
                                   update_article_desc

seed = SEED
result_file = MULTICLASS_OUTPUT_FILE
articles = MULTICLASS_ARTICLES
flavors = MULTICLASS_FLAVORS
k_fold = K_FOLD
as_time_series = AS_TIME_SERIES
feature_threshold = DEFAULT_FEATURE_THRESHOLD
classifiers = MULTICLASS_CLASSIFIERS

import warnings
from pandas.core.common import SettingWithCopyWarning

warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)

log = getlogger()

def generate_outcomes_data(y_file, outcome_to_id, filter_threshold=100):
    # Generate hot-one outcome matrix
    with open(y_file) as file:
        f = lambda x: {x.split(':')[0]:x.split(':')[1]}
        outcomes = file.readlines()
        outcomes = pd.DataFrame(outcomes)
        outcomes[0] = outcomes[0].apply(lambda x: x.strip().split())
        outcomes['caseid'] = outcomes[0].apply(lambda x: x[0])
        outcomes[0] = outcomes[0].apply(lambda x: x[1])

    return outcomes

def map_outcome(art, x):
            if x[f'{art}:1'] == 1:
                return 1
            elif x[f'{art}:0'] == 1:
                return 0
            else:
                return -1

def load_dataset(X_file, min_threshold=0):
    with open(X_file) as file:
        f = lambda x: {x.split(':')[0]:x.split(':')[1]}
        X = file.readlines()
        X = pd.DataFrame(X)
        X[0] = X[0].apply(lambda x: sorted(x.strip().split()))
        #X['caseid'] = X[0].apply(lambda x: x[0])
        #X[0] = X[0].apply(lambda x: x[1:])

        # Generate hot-one outcome matrix
        mlb = MultiLabelBinarizer(sparse_output=True)
        X = X.join(
                    pd.DataFrame.sparse.from_spmatrix(
                        mlb.fit_transform(X.pop(0)),
                        index=X.index,
                        columns=mlb.classes_))
        return X

def run(console, build, force):
    __console = console
    global print
    print = __console.print

    outcomes_path = 'data/input/datasets/'
    raw_outcome_file = Path(outcomes_path) / 'outcomes.txt'
    outcome_matrix_file = Path(outcomes_path) / 'outcomes_matrix.csv'

    print(Markdown("- **Prepare outcome matrix**"))
    OUTCOME_TO_ID =  'data/input/datasets/outcomes_variables.json'
    with open(OUTCOME_TO_ID, 'r') as f:
        outcome_to_id = json.load(f)

    if True: #not os.path.isfile(outcome_matrix_file) or force:
        print(TAB + '> Generate the outcome matrix [green][DONE]')
        outcomes_matrix = generate_outcomes_data(raw_outcome_file, outcome_to_id, filter_threshold=100)
        outcomes_matrix.to_csv(outcome_matrix_file)
    else:
        print(TAB + '> Load the outcome matrix [green][DONE]')
    outcome_matrix = pd.read_csv(outcome_matrix_file)

    # Class encoding
    le = LabelEncoder()
    le.fit(outcomes_matrix[0])
    mapping_class = dict(zip(le.classes_, le.transform(le.classes_)))
    outcomes_matrix['decision'] = le.transform(outcomes_matrix[0])
    outcomes_matrix['article'] = outcomes_matrix[0].apply(lambda x: x.split(':')[0])
    CM = []  # Confusion matrices

    count = outcomes_matrix['article'].value_counts()
    count = json.loads(count.to_json())

    count = {k:v for k,v in count.items() if v > 100}
    articles_to_keep = list(count.keys())

    outcomes_matrix = outcomes_matrix[outcomes_matrix['article'].isin(articles_to_keep)]
    le = LabelEncoder()
    le.fit(outcomes_matrix[0])
    mapping_class = dict(zip(le.classes_, le.transform(le.classes_)))

    c_outcomes = bidict(outcome_to_id)
    metadata = json.loads(outcomes_matrix['article'].value_counts().to_json())
    metadata = {k:{'Size': v, 'Article': c_outcomes.inverse[int(k)] } for k,v in metadata.items()} # c_outcomes.inverse[int(k)]
    for art, mdata in metadata.items():
        mdata['Violation'] = outcomes_matrix[outcomes_matrix[0] == f'{art}:1'].shape[0]
        mdata['No-Violation'] = outcomes_matrix[outcomes_matrix[0] == f'{art}:0'].shape[0]
        mdata['Prevalence'] = mdata['Violation'] / (mdata['Violation'] + mdata['No-Violation'])

    update_article_desc('Multiclass', metadata, MULTICLASS_DESC_OUTPUT_FILE)
    print(TAB + '> Update dataset description [green][DONE]')

    print(Markdown("- **Experiment summary**"))
    FLAVORS = {'Descriptive only': 'descriptive.txt', 'Bag-of-Words only': 'BoW.txt', 'Descriptive and Bag-of-Words': 'descriptive+BoW.txt'}
    print(f"  | Flavors: {len(FLAVORS)}")
    print(f"  | Methods: {len(classifiers)}")
    print(f"  = {len(FLAVORS) * len(classifiers)} cross-validation procedures")
    print(f"  = Take some :coffee: or :tea: and  relax")

    try:
        f = open (result_file, "r")
        exp_results = json.loads(f.read())
        print(TAB + '> Load existing results [green][DONE]')
    except Exception as e:
        exp_results = {}
        print(TAB + '> No previous results [green][DONE]')

    table = Table(title="Cross-Validation Summary")

    table.add_column("Flavor", style="cyan", no_wrap=True)
    table.add_column("Method", justify="right", style="blue")
    table.add_column("Status", justify="right", style="green")

    for i, flavor in enumerate(FLAVORS.keys()):
            for k, method in enumerate(classifiers.keys()):
                dataset_name = f'Multiclass - {flavor}'
                status = exp_results.get(dataset_name, {}).get('methods', {}).get(method, None)
                status = '[green]DONE' if status else None
                table.add_row(flavor if k == 0 else None, method, status)

    print(table)

    for flavor, features_file in FLAVORS.items():
        print(Panel(f'[bold yellow] Cross-Validation Flavor {flavor.upper()}'), justify="center")

        print(Markdown("- **Prepare dataset**"))
        dataset_path = Path(outcomes_path) / features_file
        X = load_dataset(dataset_path)
        X = pd.DataFrame(X)
        X = X[X.index.isin(outcomes_matrix.index)]
        print(TAB + '> Load the dataset [green][DONE]')

        if flavor != 'Bag-of-Words':
            # Remove '0:'
            to_drop = [e for e in X.columns if e.startswith('0:')]
            X.drop(columns=to_drop, inplace=True)
            print(TAB + '> Drop unecessary columns [green][DONE]')

        print(Markdown(f"- **Cross-Validate**"))
        y = outcomes_matrix['decision']

        o = {'name': f'Multiclass - {flavor}'}
        dataset_name = o['name']

        if dataset_name not in exp_results:
            exp_results[dataset_name] = {}

        #format_filter_output(dataset_name, o)
        update_dataset_result(dataset_name, o, result_file)
        update_dataset_filter_result(dataset_name, o, result_file)

        metadata = exp_results.get(dataset_name, {}).get('filter', {})
        metadata['size'] = metadata.get('size', int(y.shape[0]))
        #metadata['violation'] = metadata.get('violation', int(y_art.sum()))
        #metadata['non_violation'] = metadata['size'] - metadata['violation']
        #metadata['prevalence'] = float(metadata['violation']) / metadata['size']

        update_dataset_metadata(dataset_name, metadata, result_file)
        print(TAB + '> Generate dataset metadata [green][DONE]')

        #if flavor == 'Descriptive only':
        #    update_article_desc(art, metadata, MULTICLASS_DESC_OUTPUT_FILE)
        #    print(TAB + '> Update dataset description [green][DONE]')

        CM = []
        for classifier_name, classifier in classifiers.items():
                    print(TAB + f'> [bold]{classifier_name}')
                    if exp_results.get(dataset_name, {}).get('methods', {}).get(classifier_name, None):
                        print(TAB + ' тоб Cross-Validation results already exist. [green][SKIP]')
                    else:
                        try:
                            scoring = make_scorers(multiclass=True, CM=CM)
                            cv = TimeSeriesSplit(n_splits=k_fold) if as_time_series \
                                else StratifiedKFold(n_splits=k_fold) #, random_state=seed)
                            #scores = cross_validate(classifier, X_art.to_numpy(), y_art.to_numpy(),
                            scores = cross_validate(classifier, X, y,
                                cv=cv,
                                scoring=scoring,
                                return_train_score=True,
                                verbose=10,
                                n_jobs=None, error_score='raise')
                            classifier_output = process_score(scores, scoring, seed, multilabel=True)
                            cm = calculate_average_cm(CM, train_score=True)
                            classifier_output['confusion_matrix'] = cm
                            classifier_output['confusion_matrix']['class_labels'] = list(le.classes_) #list(outcomes_matrix[0].value_counts().index) #list(le.classes_)
                            #format_method_output(classifier_name, classifier_output)
                            update_classifier_result(
                                dataset_name,
                                classifier_name,
                                classifier_output,
                                result_file
                            )
                            pass
                        except Exception as e:
                            print(e)

def main(args):
    console = Console(record=True)
    run(console, args.build, args.force)

def parse_args(parser):
    args = parser.parse_args()
    return args

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Multiclass experiments')
    parser.add_argument('--build', type=str, default="./build/echr_database/")
    parser.add_argument('-f', '--force', action='store_true')
    args = parse_args(parser)

    main(args)


""" question - Implement a function that receives the ages dict and returns the average age."""

"""here I am creating a new function and its name is average_num and in function  i passed an input_dictionary
as parameter """

def average_num(input_dictionary):
    """ Here I print the input dictionary because when it prints, and I see the dictionary"""
    print("input is this", input_dictionary)
    """here i create a variable  total = 0"""
    total = 0
    """Here i am using for loop function to go inside the dictionary's values with the help of ' i ' i is only go
      in values which is integers so give value like 1,2,3,4 etc"""
    for i in input_dictionary.values():
        """Now here I am using the total variable in for loop to calculate the sum of ages of dictionary
        here first of all the value of total =0 then i=9 total assigns the i value and add the next value of i
        like this i calculate the sum of dictionary but for getting average i also needed length of dictionary"""
        total += i
        """Now i came outside the loop and calculate the length in new variable name is length"""
    length = len(input_dictionary)
    """here i have the sum of dictionary and the length of dictionary
    the formula to getting average = sum/ length
    so here i take a new variable and calculate the average value """
    avg_val = total / length
    """here i return the value to get the output means the average value"""
    return avg_val

ages = {
    "Peter": 10,
    "Isabel": 11,
    "Anna": 9,
    "Thomas": 10,
    "Bob": 10,
    "Joseph": 11,
    "Maria": 12,
    "Gabriel": 10,
}
"""Here I called function and assign the value of function in a variable"""
average_of_dictionary = average_num(input_dictionary=ages)
"""here I am calling the variable in which function was assigned"""
print(average_of_dictionary)


# -*- coding: utf-8 -*-
"""
Created on Tue Oct  5 00:38:24 2021

@author: Marcos Martilotta
"""
import os

def archivos_png(directorio):
    lista_archivos = os.listdir(directorio)
    lista_png = []
    for archivo in lista_archivos:
        if archivo[-3:] == 'png':
            lista_png.append(archivo)
            print(lista_png)

    return lista_png

if __name__ == '__main__':
    print(archivos_png('../Data/ordenar/imgs'))


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import string
import random
import time
path = "D:\chromedriver.exe"
driver = webdriver.Chrome(path)
print("Opening up the browser")
driver.get("https://prnt.sc/")
print("Preparing cookies...")
driver.find_element_by_css_selector('.css-47sehv').click()
time.sleep(2)
while True:
    rastgele = ''.join(random.choices(string.ascii_lowercase + string.digits, k = 6))
    site = str(f'http://prnt.sc/{rastgele}')
    time.sleep(1)
    print("Accessing this site: "+ site)
    driver.get(site)
    time.sleep(0.5)
    print("Locating image...")
    time.sleep(2)
    filename = 'result'+rastgele+'.png'
    try:
        x = driver.find_element_by_xpath('//*[@id="screenshot-image"]')
        print("Downloading image...")
        print(x)
        with open(filename, 'wb') as file:
            file.write(driver.find_element_by_xpath('//*[@id="screenshot-image"]').screenshot_as_png)
            print("File successfully downloaded! File name : " + filename)
    except:
        print("Image not found, going to the next link")


'''
# class variables
#Example_01

class Circle:
    pi = 3.1416

    def __init__(self,radius):
        self.radius =radius

    def circle_circumference(self):
        return 2*Circle.pi*self.radius

circle1 = Circle(5)
circle2 = Circle(7)

print(circle1.circle_circumference())

#Class variable (Example_02)
# if all same discount offer
class Headphone:

    discount_percent = 15 # class varibale = discount

    def __init__(self,brand_name,model_name,price):

        self.brand_name = brand_name
        self.model_name = model_name
        self.price = price

    def apply_discount(self):
        offer_price = (Headphone.discount_percent/100)*self.price
        return self.price - offer_price

h1 = Headphone('Hp','r44',5000)
h2 = Headphone('Asus','tt33',8900)

print(h1.apply_discount())

'''

#Class variable (Example_03)
# if all different  discount offer

class Headphone:
    discount_percent = 15

    def __init__(self,brand_name,model_name,price):

        self.brand_name = brand_name
        self.model_name = model_name
        self.price = price

    def apply_discount(self):
        #offer_price = (Headphone.discount_percent/100) * self.price
        '''
       first it looking it on function variable,it can't this ,after that will looking for
       class varible ,if match it, it will taken this and run

        '''
        offer_pirce = (self.discount_percent/100)*self.price
        return self.price - offer_pirce

h1 = Headphone('Dell','hrr33',78000)
h2 = Headphone('Asus','a88',99900)
h1.discount_percent = 10 # if we change now the discount_percent ,it will work.
print(h1.apply_discount())


from app import db
from app.models import \
    MoneroTransactions, MoneroWalletFee
from app.generalfunctions import floating_decimals
from sqlalchemy import func

def getlastestfee():

    """
    THis will query the last 10 withdrawls...get the average fee
    :return:
    """

    getratings = db.session.query(func.avg(MoneroTransactions.fee))
    getratings = getratings.filter(MoneroTransactions.category == 2)
    getratings = getratings.order_by(MoneroTransactions.created.desc())
    avgrate = getratings.limit(10)
    x = (avgrate[0][0])
    y = floating_decimals(x, 8)

    thefee = db.session.query(MoneroWalletFee).filter_by(id=1).first()
    thefee.amount = y
    db.session.add(thefee)
    db.session.commit()
getlastestfee()


"""
Team Activity
Week 04
Purpose: Determine how fast an object will fall using the formula:
v(t) = sqrt(mg/c) * (1 - exp((-sqrt(mgc)/m)*t))
"""
import math
print("To calculate how fast an object will fall, enter these informations:")
#input mass (in kg)
m = float(input("Mass (in kg): "))
#input acceleration due to gravity
g = float(input("Gravity (in m/s^2, 9.8 for Earth, 24 for Jupiter): "))
#input time (in seconds)
t = float(input("Time (in seconds): "))
#input density of fluid
p = float(input("Density of the fluid (in kg/m^3, 1.3 for air, 1000 for water): "))
#input cross sectional area of projectile (in square meters)
A = float(input("Cross sectional area (in m^2): "))
#input drag constan
C = float(input("Drag constant (0.5 for sphere, 1.1 for cylinder): "))
#Compute the value for the inner value c
c = (1 / 2) * p * A * C
#show value of c
print(f"The inner value of c is: {c:.3f}")
#Compute and display the overall velocity
v = math.sqrt(m * g / c) * (1 - math.exp((-1*(math.sqrt(m * g * c) / m ))*t))

print(f"The velocity after 10.0 seconds is : {v:.3f}")

#Stretch 1
"""
.75kg Alexa speaker
g = 9.8
csa = 25.51

To calculate how fast an object will fall, enter these informations:
Mass (in kg): 0.75
Gravity (in m/s^2, 9.8 for Earth, 24 for Jupiter): 9.8
Time (in seconds): 10
Density of the fluid (in kg/m^3, 1.3 for air, 1000 for water): 1.3
Cross sectional area (in m^2): 25.51
Drag constant (0.5 for sphere, 1.1 for cylinder): 0.5
The inner value of c is: 8.291
The velocity after 10.0 seconds is : 0.942

Boulder = 68kg
Mass (in kg): 68
Gravity (in m/s^2, 9.8 for Earth, 24 for Jupiter): 9.8
Time (in seconds): 10
Density of the fluid (in kg/m^3, 1.3 for air, 1000 for water): 1.3
Cross sectional area (in m^2): 0.676
Drag constant (0.5 for sphere, 1.1 for cylinder): 0.5
The inner value of c is: 0.220
The velocity after 10.0 seconds is : 45.781
"""

#Stretch 2
"""
Alexa Speaker :
Earth V = 0.942
Jupiter V = 1.473

Boulder :

Earth V = 45.781
Jupiter V = 80.865

"""

#Stretch 3

#Terminal Velocity =

print("\n\nTHE TERMINAL VELOCITY IS : " + str(math.sqrt(m * g / c)))

Programming Building Blocks/Week 2/04Teach.py


import random
import math
def weight(alpha1,beta1,gamma1):
    x1= (alpha1 << 1)& mask
    y1= (beta1  << 1)& mask
    z1= (gamma1  << 1)& mask
    not_x=(x1^mask)
    eq=((not_x)^y1)&(not_x^z1)
    s=eq&(alpha1^beta1^gamma1^(y1))
    if(s==0):
        h=bin((~eq)& mask)
        wt=(h[1:].count("1"))
        return math.pow(2,-wt)
    else:
        return -200

def store(da,db,dc,wt):
    alpha.append(da)
    beta.append(db)
    gamma.append(dc)
    prob.append(wt)
    return da,db,dc

def PDDT(n,p_thres,k,wt,da,db,dc):
    if(n==k):
        store(da,db,dc,wt)
        return
    for x in range(0,2):
        for y in range(0,2):
            for z in range(0,2):
                new_da= (da | (x<<k) & mask)
                new_db= (db | (y<<k) & mask)
                new_dc= (dc | (z<<k) & mask)
                wt= weight(new_da,new_db,new_dc)
                if(wt >= p_thres or ((x==1 and y==1 and z==1) and (k+1)!=n)):
                    PDDT(n,p_thres,k+1,wt,new_da,new_db,new_dc)

mask = 2 ** 16 - 1
alpha,beta,gamma,prob=[],[],[],[]

PDDT(16,0.07,0,1,0,0,0)
for i in range(len(alpha)):
	  print(alpha[i])
print(len(prob))
for i in range(len(beta)):
	  print(beta[i])
print(len(prob))
for i in range(len(gamma)):
	  print(gamma[i])
for i in range(len(prob)):
	  print(prob[i])
print(len(prob))

#print(len(prob))
#print(len(alpha))
#print(len(beta))
#print(len(gamma))


nums=[2,2,3,4,4,3,3]
val=5

if val in nums:
    a=nums.count(val)
    for i in range(a):
       nums.remove(val)
    print(nums)
else:
    print(f"No {val} found in the list")


from graphviz import Digraph as DotGraph

MAX_EDGES = 2000

def gshow(g, attr=None, file_name=None, view=False):
    if g is None: return

    """ shows a networx DiGraph g
        using graphviz
        it could become slow on large graphs (above MAX_EDGES)
    """
    ecount = g.number_of_edges()
    if ecount > MAX_EDGES:
        print('GRAPH TOO BIG TO SHOW, EDGES=', ecount)
        return
    dot = DotGraph()
    for e in gen_dot_edges(g, attr=attr):
        f, t, w = e
        dot.edge(f, t, label=w)
    print('generated:', file_name, 'edges:', ecount)
    dot.render(file_name, view=view)

def gen_dot_edges(g, attr=None):
    for e in g.edges():
        f, t = e
        if not attr:
            w = ''
        else:
            w = g[f][t].get(attr)
            if not w: w = ''
        if not isinstance(f, str): f = "#" + str(f)
        if not isinstance(t, str): t = "#" + str(t)
        f = f.replace(':', '.')
        t = t.replace(':', '.')
        w = str(w)
        yield (f, t, w)

def xshow(gs, attr=None, file_name=None, view=False):
    """ shows a sequence of (possibly originating from the same,
       via a transformation) displayed together, left to right
    """
    dot = DotGraph()
    ecount = 0
    for i, g in enumerate(gs):
        k = g.number_of_edges()
        ecount += k
        if ecount > MAX_EDGES:
            print('GRAPH CHAIN TOO BIG TO SHOW, EDGES=', ecount)
            return
        for f, t, w in gen_dot_edges(g, attr=attr):
            mark = "@" + str(i)
            dot.edge(f + mark, t + mark, label=w)
    print('generated:', file_name, 'edges:', ecount)
    dot.render(file_name, view=view)


username = input()

cmd_input = input().split()

while cmd_input[0] != "Sign":
    args = cmd_input
    cmd = args[0].lower()

    if cmd == "case":
        type = args[1]
        if type == "lower":
            username = username.lower()
            print(username)
        elif type == "upper":
            username = username.upper()
            print(username)

    elif cmd == "reverse":
        start_index = int(args[1])
        end_index = int(args[2])
        if 0 <= start_index < end_index < len(username):

            res = username[start_index:end_index+1]
            print(res[::-1])

    elif cmd == "cut":
        substring = args[1]

        if substring not in username:
            print(f"The word {username} doesn't contain {substring}.")
        else:
            username = username.replace(substring, "", 1)
            print(username)

    elif cmd == "replace":
        char = args[1]
        username = username.replace(char, "*")
        print(username)

    elif cmd == "check":
        char = args[1]
        if char in username:
            print("Valid")
        else:
            print(f"Your username must contain {char}.")

    cmd_input = input().split()