import numpy as np
import pandas as pd
import pickle
import pystan as ps
from scipy.stats import bernoulli, uniform, norm, beta, gamma, binom
from scipy.special import expit
import arviz as ar
import sys
from utilities.plotUtils import *
from utilities.utilityFunctions import unpickle_object

def create_model_code(priors):
    """

    """
    data_block = 'data {{\nint<lower=0> N;\nint<lower=0,upper=1> y[N];\nint<lower=0,upper=1> x1[N];\n}}\n'
    transformed_data_block = 'transformed data {{}}\n'
    transformed_parameters_block  = 'transformed parameters {{\nreal<upper=0> beta1;\nbeta1 = -1*beta1_pos;\n}}\n'
    generated_quantities_block = 'generated quantities {{}}\n'
    if priors['se'].split('(')[0]=='fixed':
        se = float(priors['se'].split('(')[1].split(')')[0])
        if priors['sp'].split('(')[0]=='fixed': # se_prior=='fixed*' & sp_prior=='fixed*'
            sp = float(priors['sp'].split('(')[1].split(')')[0])
            parameters_block = 'parameters {{\nreal beta0;\nreal<lower=0> beta1_pos;\n}}\n'
            model_block = 'model {{\nbeta0 ~ {0};\nbeta1_pos ~ {1};\nfor(i in 1:N) {{\nreal p;\nreal se;\nreal sp;\nse = {2};\nsp = {3};\np = inv_logit(beta0 + beta1*x1[i]);\ny[i] ~ bernoulli(se*p + (1-sp)*(1-p));\n}}\n}}\n'.format(priors['beta0'], priors['beta1'], se, sp)
        else: # se_prior=='fixed*' & sp_rior!='fixed*'
            parameters_block = 'parameters {{\nreal beta0;\nreal<lower=0> beta1_pos;\nreal<lower=0,upper=1> sp;\n}}\n'
            model_block = 'model {{\nbeta0 ~ {0};\nbeta1_pos ~ {1};\nsp ~ {2};\nfor(i in 1:N) {{\nreal p;\nreal se;\nse = {3};\np = inv_logit(beta0 + beta1*x1[i]);\ny[i] ~ bernoulli(se*p + (1-sp)*(1-p));\n}}\n}}\n'.format(priors['beta0'], priors['beta1'], priors['sp'], se)
    else:
        if priors['sp'].split('(')[0]=='fixed': # se_prior!='fixed*' & sp_prior=='fixed*'
            sp = float(priors['sp'].split('(')[1].split(')')[0])
            parameters_block = 'parameters {{\nreal beta0;\nreal<lower=0> beta1_pos;\nreal<lower=0,upper=1> se;\n}}\n'
            model_block = 'model {{\nbeta0 ~ {0};\nbeta1_pos ~ {1};\nse ~ {2}\nfor(i in 1:N) {{\nreal p;\nreal sp;\nsp = {3};\np = inv_logit(beta0 + beta1*x1[i]);\ny[i] ~ bernoulli(se*p + (1-sp)*(1-p));\n}}\n}}\n'.format(priors['beta0'], priors['beta1'], priors['se'], sp)
        else: # se_prior!='fixed*' & sp_prior!='fixed*'
            parameters_block = 'parameters {{\nreal beta0;\nreal<lower=0> beta1_pos;\nreal<lower=0,upper=1> se;\nreal<lower=0,upper=1> sp;\n}}\n'
            model_block = 'model {{\nbeta0 ~ {0};\nbeta1_pos ~ {1};\nse ~ {2};\nsp ~ {3};\nfor(i in 1:N) {{\nreal p;\np = inv_logit(beta0 + beta1*x1[i]);\ny[i] ~ bernoulli(se*p + (1-sp)*(1-p));\n}}\n}}\n'.format(priors['beta0'], priors['beta1'], priors['se'], priors['sp'])

    model_code = data_block + transformed_data_block + parameters_block + transformed_parameters_block + model_block +  generated_quantities_block
    model_code = model_code.replace('{{','{').replace('}}','}')

    return model_code

def create_stan_model(code):
    model = ps.StanModel(model_code=code)
    return model

def save_stan_model(model, save_path):
    with open(save_path, 'wb') as f:
        pickle.dump(model, f, protocol=pickle.HIGHEST_PROTOCOL)

def sample_from_model(model, data, n_burnin, n_samples, n_chains):
    fit = model.sampling(data=data, iter=n_samples+n_burnin, chains=n_chains, warmup=n_burnin)
    return fit

# def sample_from_model(compiled_model_path, n_burnin, n_samples, n_chains):
#     # unpickle compiled model
#     model = unpickle_object(compiled_model_path)
#     code = model.model_code
#     # sample from model
#     fit = sample_from_model(model, stan_data, n_burnin, n_samples, n_chains)
#     idata = ar.from_pystan(fit)

#     del(model) # delete model

#     return {
#         'fit': idata,
#         'code': code,
#     }

class SimulatedData:
    def __init__(self, N, vax_prob, beta0, beta1, se, sp):
        self.N = N
        self.vax_prob = vax_prob
        self.beta0 = beta0
        self.beta1 = beta1
        self.se = se
        self.sp = sp

    def generate_ve_study_data(self):
        x1 = bernoulli.rvs(self.vax_prob, size=self.N) # vaccinated (1)/unvaccinated (0)
        X = pd.DataFrame({'vax': x1,})
        beta_vec = np.array([self.beta0, self.beta1])
        X['prob_td'] = X.apply(lambda row: expit(np.dot(np.array([1, row['vax']]), beta_vec)), axis=1)
        # X['test_id'] = test_id
        X['test_result'] = -1
        X['test_result'] = X['prob_td'].apply(lambda p: bernoulli.rvs(self.se*p + (1-self.sp)*(1-p)))

        self.study_data = X

    def generate_test_validation_data(self, n, prob):
        return binom.rvs(n, prob)

    def create_stan_data_se_sp_validation_one_dx_test(self, n_sens, n_spec):
        VALIDATION_ONE_DX_TEST_NEEDED_PARAMS = ['N', 'x1', 'testResults', 'y_spec', 'n_spec', 'y_sens', 'n_sens']
        self.n_sens = n_sens
        self.n_spec = n_spec
        self.y_sens = self.generate_test_validation_data(self.n_sens, self.se)
        self.y_spec = self.generate_test_validation_data(self.n_spec, self.sp)

        self.stan_data = {
            'N': self.N,
            'x1': np.array(self.study_data['vax']),
            'testResults': np.array(self.study_data['test_result']),
            'y_spec': self.y_spec,
            'n_spec': self.n_spec,
            'y_sens': self.y_sens,
            'n_sens': self.n_sens,
        }

    def create_stan_data_se_sp_fixed_one_dx_test(self, se, sp):
        self.stan_data = {
            'N': self.N,
            'x1': np.array(self.study_data['vax']),
            'testResults': np.array(self.study_data['test_result']),
            'se': se,
            'sp': sp,
        }

class StudyData:
    def __init__(self, vax_prob, beta0, beta1):
        self.vax_prob = vax_prob
        self.beta0 = beta0
        self.beta1 = beta1

    def generate_validation_data(self, n, prob):
        return binom.rvs(n, prob)

    def generate_one_test_data(self, N, se, sp, test_id):
        x1 = bernoulli.rvs(self.vax_prob, size=N) # vaccinated (1)/unvaccinated (0)
        X = pd.DataFrame({'vax': x1,})
        beta_vec = np.array([self.beta0, self.beta1])
        X['prob_td'] = X.apply(lambda row: expit(np.dot(np.array([1, row['vax']]), beta_vec)), axis=1)
        X['test_id'] = test_id
        X['test_result'] = -1
        X['test_result'] = X['prob_td'].apply(lambda p: bernoulli.rvs(se*p + (1-sp)*(1-p)))

        data = {
            f'N': N,
            f'se': se,
            f'sp': sp,
            f'X': X
        }

        return data

def plot_centered_error_bars(ax, x, y, true_y, lower, upper, label=None):
    ax.errorbar(x, y-true_y, yerr=(y-lower, upper-y), fmt='o', label=label) # error is (lower, upper) = (mean-lower, upper-mean)
    return ax

# def get_command_line_arguments(args):
def get_command_line_arguments():
    """
    Get command line arguments. Generally
    """
    args = sys.argv
    args = args[1:]
    argument_dict = {}
    for arg in args:
        name = arg.split('=')[0][2:]
        val = arg.split('=')[1]
        argument_dict[name] = val
    return argument_dict

def compute_hdi(idata, param, prob=0.95):
    hdi = ar.hdi(idata, hdi_prob=prob).to_dataframe()
    interval = [hdi[param][0], hdi[param][1]]

    x = idata.to_dataframe()[('posterior', f'{param}')]
    prop = np.sum((x>=interval[0]) & (x<=interval[1]))/len(x)
    return (interval, prop)

def compute_centered_credible_interval(samples, val, step, prob=0.95):
    """
    Computes X% credible interval where the credible interval is centered on user supplied value.
    """
    prop = 0
    ii = 1
    while prop<prob:
        interval = [val-ii*step, val+ii*step]
        prop = compute_probability_of_interval(samples, interval)
        ii += 1
    return (interval, prop)

def compute_lower_credible_interval(samples, step, prob=0.95):
    prop = 0
    ii = 1
    min_val = samples.min()
    while prop<prob:
        interval = [min_val, val+ii*step]
        prop = compute_probability_of_interval(samples, interval)
        ii += 1
    return (interval, prop)

def compute_upper_credible_interval(samples, step, prob=0.95):
    prop = 0
    ii = 1
    max_val = samples.max()
    while prop<prob:
        interval = [max_val-ii*step, max_val]
        prop = compute_probability_of_interval(samples, interval)
        ii += 1
    return (interval, prop)

def compute_probability_of_interval(samples, interval):
    samples = samples.sort_values().reset_index(drop=True)
    mask = ((samples>=interval[0]) & (samples<=interval[1]))
    return np.sum(mask)/len(samples)

def hdi_from_models(list_of_models, param):
    ci_df = []
    for model in list_of_models:
        idata = ar.from_pystan(model.fit)
        interval, prob = compute_hdi(idata, param) # change this line if you want a different CI
        true = model.true_params[param]
        ci_df.append([param, true, interval[0], interval[1], interval[1]-interval[0], prob])

    ci_df = pd.DataFrame(ci_df, \
                        columns=['param','true','lower','upper','width','prob']
                        )
    return ci_df

def centered_credible_interval_plot(ax, ci_df):
    """

    """
    true, lower, upper = ci_df['true'], ci_df['lower'], ci_df['upper']
    index = np.arange(1,len(lower)+1)
    for ii in range(len(index)):
        tt=ax.vlines(index[ii],
                      ymin=lower[ii]-true[ii],
                      ymax=upper[ii]-true[ii],
                      color='r',
                      lw=2,
                      ls='-',
#                       label=f'{true[ii]}'
                     )

    tt=ax.axhline(0,
                   color='k',
                   ls='--'
                  )
    # return ax

def generate_prior(stan_prior):
    size = 1000
    low, up = 0.0001, 0.9999
    name = stan_prior.split('(')[0]
    param1, param2 = float(stan_prior.split('(')[1].split(',')[0]), float(stan_prior.split('(')[1].split(',')[1][:-1])
    if name=='normal':
        ppf = lambda xx: norm.ppf(xx, loc=param1, scale=param2)
        pdf = lambda xx: norm.pdf(xx, loc=param1, scale=param2)
    elif name=='beta':
        ppf = lambda xx: beta.ppf(xx, a=param1, b=param2)
        pdf = lambda xx: beta.pdf(xx, a=param1, b=param2)
    elif name=='gamma':
        ppf = lambda xx: gamma.ppf(xx, a=param1, scale=1/param2)
        pdf = lambda xx: gamma.pdf(xx, a=param1, scale=1/param2)
    elif name=='uniform':
        ppf = lambda xx: gamma.ppf(xx, a=param1, scale=param2-param1)
        pdf = lambda xx: gamma.pdf(xx, a=param1, scale=param2-param1)

    prior = np.zeros((size, 2))
    prior[:,0] = np.linspace(ppf(low), ppf(up), size)
    prior[:,1] = pdf(prior[:,0])

    return prior

# def posterior_plot(model, param):
def posterior_plot(idata, param, true_val, prior_name,
                   mean=False, lower=False, upper=False,
                  ):

    """

    """
    fig, axs = fig_setup(1,1)

    # true value as vertical line
    # true = model.true_params[param]
    tt=axs[0].axvline(x=true_val, color='k', linestyle='--',
                label=f"true {param}={true_val}"
                ) #

    # prior
    prior = generate_prior(prior_name)
    if param=='beta1':
        prior[:,0] = (-1)*prior[:,0]
    tt=axs[0].plot(prior[:,0], prior[:,1],
                lw=3,
                color='r',
                label=f'prior {prior_name}'
                )

    # posterior samples
    nbins = 100
    x = idata.to_dataframe()[('posterior', f'{param}')]
    n, bins, patches =axs[0].hist(x,
                        bins=nbins,
                        density=True,
                        label='posterior samples',
    #                      label=f'{param}'
                        )
    max_count = np.max(n)

    # credible intervals
    # HDI
    interval, prob = compute_hdi(idata, param)
    prob = np.sum((x>=interval[0]) & (x<=interval[1]))/len(x)
    tt=axs[0].hlines(y=max_count/10,
                    xmin=interval[0],
                    xmax=interval[1],
                    lw=2,
                    label=f'HDI ({prob:0.4})'
                    )

    # CI equal-tailed around mean
    if mean is True:
        val = x.mean()
        interval, prob = compute_centered_credible_interval(x, val, 0.0005)
        tt=axs[0].hlines(y=max_count/12,
                        xmin=interval[0],
                        xmax=interval[1],
                        lw=2,
                        label=f'mean CI ({prob:0.4})'
                        )

    # lower CI
    if lower is True:
        interval, prob = compute_lower_credible_interval(x, 0.0005)
        tt=axs[0].hlines(y=max_count/14,
                        xmin=interval[0],
                        xmax=interval[1],
                        lw=2,
                        label=f'lower ({prob:0.4})'
                        )

    # upper CI
    if upper is True:
        interval, prob = compute_upper_credible_interval(x, 0.05)
        tt=axs[0].hlines(y=max_count/16,
                        xmin=interval[0],
                        xmax=interval[1],
                        lw=2,
                        label=f'upper ({prob:0.4})'
                        )

    tt=axs[0].set_xlim(x.min(), x.max())
    tt=set_title_axes_labels(axs[0],None,f'{param}','density')

    return (fig, axs)

def bernoulli_confidence_interval(p_hat, n, alpha=0.05):
    z_val = norm.ppf(1-(alpha/2))
#     print(z_val)
    se = np.sqrt(p_hat*(1-p_hat)/n)
    return [p_hat-z_val*se, p_hat+z_val*se]

def create_ci_plots(df, param, outpath, sort=False):
    # subset to single parameter
    ci_df = df[df['param']==param]

    # sort and reset index dataframe
    if sort is True:
        ci_df.sort_values('true', inplace=True) # sort
    ci_df.reset_index(inplace=True, drop=True) # reset index

    # compute proportion of CIs capturing true parameter value
    prop_ci_capturing_true = np.sum((ci_df['true']>=ci_df['lower']) & (ci_df['true']<=ci_df['upper']))/ci_df.shape[0]
    interval = bernoulli_confidence_interval(prop_ci_capturing_true, ci_df.shape[0])
#     interval

    # plot
    fig, axs = fig_setup(1, 1, w=20)
    tt=centered_credible_interval_plot(axs[0], ci_df)
    tt=set_title_axes_labels(axs[0],
                        f'{param}\nproportion of CIs capturing true = {prop_ci_capturing_true} ([{interval[0]:.3}, {interval[1]:.3}])',
                        'iteration',
                        'CI centered on true'
                        )
    plt.tight_layout()
    tt=axs[0].set_xlim([-1,201])
    finalize(axs[0])
    plt.savefig(outpath,
                dpi=300
            )

def sample_from_prior(stan_prior):
    name = stan_prior.split('(')[0]
    if name!='fixed':
        param1, param2 = float(stan_prior.split('(')[1].split('-')[0]), float(stan_prior.split('(')[1].split('-')[1][:-1])
    else:
        sample = float(stan_prior.split('(')[1][:-1])

    if name=='normal':
        sample = norm.rvs(loc=param1, scale=param2)
    elif name=='beta':
        sample = beta.rvs(a=param1, b=param2)
    elif name=='gamma':
        sample = gamma.rvs(a=param1, scale=1/param2)
    elif name=='uniform':
        sample = uniform.rvs(a=param1, scale=param2-param1)

    return sample

class MCMCSamples:
    """This class is for plotting and analyzing the results of MCMC samples"""
    def __init__(self, data, mcmc_samples, true_param_vals):
        """
        Params:
            - data (DataFrame) : has to have 'test_result' and 'vax' column. vax=0 (1) should be unvaccinated (vaccinated) and test_result=0 (1) should be test negative (positive)
            - mcmc_samples : output of arviz.from_pystan
            - true_param_vals : dict containing true param values
        """
        self.data = data
        self.mcmc_samples = mcmc_samples
        self.true_param_vals = true_param_vals

    def compute_test_result_summaries(self):
        tmp = self.data.groupby(['vax', 'test_result'])['test_result'].count()
        self.num_unvax_testNeg = tmp[0][0]
        self.num_unvax_testPos = tmp[0][1]
        self.num_vax_testNeg = tmp[1][0]
        self.num_vax_testPos = tmp[1][1]

    def compute_uncorrected_point_estimates(self):
        """
        Compute naive odds ratio which is (n_vp / n_vn) / (n_up / n_un)
        """
        vax_odds = self.num_vax_testPos/self.num_vax_testNeg
        unvax_odds = self.num_unvax_testPos/self.num_unvax_testNeg

        # return vax_odds/unvax_odds
        self.uncorrected_odds_ratio = vax_odds/unvax_odds
        self.uncorrected_ve_pt_estimate = 1 - self.uncorrected_odds_ratio

    def compute_corrected_point_estimates(self):
        """
        Compute corrected odds ratio ala Endo et al. equation 3 and Marc's derivation.
        """
        nu = self.num_unvax_testNeg + self.num_unvax_testPos
        nv = self.num_vax_testNeg + self.num_vax_testPos
        se, sp = self.true_param_vals['se'], self.true_param_vals['sp']
        vax_odds = (self.num_vax_testPos - (1-sp)*nv)/(self.num_vax_testNeg - (1-se)*nv)
        unvax_odds = (self.num_unvax_testPos - (1-sp)*nu)/(self.num_unvax_testNeg - (1-se)*nu)

        # return vax_odds/unvax_odds
        self.corrected_odds_ratio = vax_odds/unvax_odds
        self.corrected_ve_pt_estimate = 1 - self.corrected_odds_ratio

    def posterior_samples_plot(self, param, ax, smooth=0):
        nbins = 100
        samples = self.mcmc_samples.to_dataframe()[('posterior', f'{param}')]
        n, bins = np.histogram(counts, bins=100, density=True)
        n, bins, patches = ax.hist(
            samples,
            bins=nbins,
            density=True,
            label='posterior samples',
            # label=f'{param}'
        )
        tt = ax.axvline(
            x=self.true_param_vals[param],
            color='k',
            linestyle='--',
            label=f"true {param}={self.true_param_vals[param]:.3f}"
        )
        # return ax

    # def credible_interval_df(self, prob=0.95):

    def centered_ci_plot(self, param=None):
        if param is None:
            for param in self.true_param_vals.keys():
                samples = self.mcmc_samples.to_dataframe()[('posterior', f'{param}')]
                compute_centered_credible_interval(samples, val, 0.0005, prob)

def generate_study_data(beta0, beta1, vax_prob, N, se, sp, test_id):
        x1 = bernoulli.rvs(vax_prob, size=N) # vaccinated (1)/unvaccinated (0)
        X = pd.DataFrame({'vax': x1,})
        beta_vec = np.array([beta0, beta1])
        X['prob_td'] = X.apply(lambda row: expit(np.dot(np.array([1, row['vax']]), beta_vec)), axis=1)
        X['test_id'] = test_id
        X['test_result'] = -1
        X['test_result'] = X['prob_td'].apply(lambda p: bernoulli.rvs(se*p + (1-sp)*(1-p)))

        data = {
            'beta0': beta0,
            'beta1': beta1,
            'vax_prob': vax_prob,
            'N': N,
            'se': se,
            'sp': sp,
            'X': X
        }

        return data


# -*- coding: utf-8 -*-
#from ez_setup import use_setuptools
#use_setuptools()
__revision__ = "$Id$"
import sys
import os
from setuptools import setup, find_packages
import glob
import imp

common = imp.load_source('common', os.path.join('src', 'pypath', 'common.py'))
__version__ = common.__version__

def which(exe):
    '''
    Checks if executable is available.
    Source:
    http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
    '''
    def is_exe(fpath):
        return os.path.exists(fpath) and os.access(fpath, os.X_OK)

    def ext_candidates(fpath):
        yield fpath
        for ext in os.environ.get("PATHEXT", "").split(os.pathsep):
            yield fpath + ext

    fpath, fname = os.path.split(exe)
    if fpath:
        if is_exe(exe):
            return True
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, exe)
            for candidate in ext_candidates(exe_file):
                if is_exe(candidate):
                    return True
    return False

def query_yes_no(question, default="yes"):
    """
    From http://stackoverflow.com/questions/3041986/python-command-line-yes-no-input

    Ask a yes/no question via raw_input() and return their answer.

    "question" is a string that is presented to the user.
    "default" is the presumed answer if the user just hits <Enter>.
        It must be "yes" (the default), "no" or None (meaning
        an answer is required of the user).

    The "answer" return value is True for "yes" or False for "no".
    """
    valid = {"yes": True, "y": True, "ye": True,
             "no": False, "n": False}
    if default is None:
        prompt = " [y/n] "
    elif default == "yes":
        prompt = " [Y/n] "
    elif default == "no":
        prompt = " [y/N] "
    else:
        raise ValueError("invalid default answer: '%s'" % default)
    while True:
        sys.stdout.write(question + prompt)
        choice = raw_input().lower()
        if default is not None and choice == '':
            return valid[default]
        elif choice in valid:
            return valid[choice]
        else:
            sys.stdout.write("Please respond with 'yes' or 'no' "
                             "(or 'y' or 'n').\n")

metainfo = {
    'authors': {
    'Türei':('Dénes Türei','denes@ebi.ac.uk'),
    },
    'version': __version__,
    'license': 'LGPL',
    'download_url': ['http://pypath.omnipathdb.org'],
    'url': ['http://pypath.omnipathdb.org'],
    'description': 'Work with molecular networks in Python igraph',
    'platforms': ['Linux', 'Unix', 'MacOSX', 'Windows'],
    'keywords': ['protein', 'mRNA', 'DNA', 'signaling',
                 'SignaLink', 'Signor', 'InnateDB', 'IntAct', 'Reactome',
                 'MPPI', 'NCI-PID', 'DIP', 'MatrixDB', 'PANTHER',
                 'PhosphoSite', 'PhosphoPoint', 'DEPOD', 'SPIKE', 'KEGG',
                 'Autophagy', 'ARN', 'NRF2', 'NRF2ome', 'Guide to Pharmacology',
                 'regulation',
                 'phosphorylation', 'kinase', 'phosphatase',
                 'dephosphorylation', 'directed graph'],
    'classifiers': [
    'Development Status :: 2 - Pre-Alpha',
    'Intended Audience :: Developers',
    'Intended Audience :: Science/Research',
    'License :: GNU Library or Lesser General Public License (LGPL)',
    'Operating System :: OS Independent',
    'Programming Language :: Python',
    'Topic :: Software Development :: Libraries :: Python Modules',
    'Topic :: Scientific/Engineering :: Bio-Informatics',
    'Topic :: Scientific/Engineering :: Information Analysis',
    'Topic :: Scientific/Engineering :: Mathematics']
}

#package_data = [
    #'pypath/data/alzpw-ppi.csv',
    #'pypath/data/arn_014.csv',
    #'pypath/data/arn.csv',
    #'pypath/data/arn_lr01.csv',
    #'pypath/data/ca1.csv',
    #'pypath/data/cancer_gene_census.csv',
    #'pypath/data/cell-map-edge-attributes.txt',
    #'pypath/data/dd_refs.csv',
    #'pypath/data/depod-refs.csv',
    #'pypath/data/dip_human_core_processed.csv',
    #'pypath/data/entrez_uniprot.csv',
    #'pypath/data/gdsc.sif',
    #'pypath/data/gold_standard.csv',
    #'pypath/data/gold_standard.xlsx',
    #'pypath/data/innatedb.csv',
    #'pypath/data/intact_filtered.csv',
    #'pypath/data/krishnadev_atg_1.tab',
    #'pypath/data/krishnadev_atg.tab',
    #'pypath/data/krishnadev_vegeredmeny.csv',
    #'pypath/data/kshirsagar_atg_1.tab',
    #'pypath/data/kshirsagar_atg.tab',
    #'pypath/data/kshirsagar_vegeredmeny.csv',
    #'pypath/data/macrophage-strict.csv',
    #'pypath/data/matrixdb_core.csv',
    #'pypath/data/mppi_human_rep.csv',
    #'pypath/data/nci-pid-strict.csv',
    #'pypath/data/netpath_refs.csv',
    #'pypath/data/nrf2ome.csv',
    #'pypath/data/phosphopoint.csv',
    #'pypath/data/phosphosite_human_hc.csv',
    #'pypath/data/phosphosite_human_noref.csv',
    #'pypath/data/salmonella_atg.tar.gz',
    #'pypath/data/sec_ac.txt',
    #'pypath/data/shlecker_atg_1.tab',
    #'pypath/data/shlecker_vegeredmeny.csv',
    #'pypath/data/signor_ppi.tsv',
    #'pypath/data/slk01human.csv',
    #'pypath/data/spike_hc.csv',
    #'pypath/data/swissprot2.csv',
    #'pypath/data/swissprot-gsymbol-name.csv',
    #'pypath/data/trembl2.csv',
    #'pypath/data/uniprot-all-human.tab',
    #'pypath/data/intogene_cancerdrivers.tsv'
#]

with open('README.rst') as f:
    readme = f.read()
with open('HISTORY.rst') as f:
    history = f.read()

# choosing module for mysql access:
deps = [
    'python-igraph',
    'beautifulsoup4',
    'configparser',
    'pyopenssl',
    'ndg-httpsclient',
    'chembl_webresource_client',
    'pyasn1',
    'numpy',
    'scipy',
    'matplotlib',
    'fisher',
    'statsmodels',
    'pycurl',
    'lxml',
    'xlrd',
    'httplib2',
    'future'
]

#mysql = 'pymysql'
#if which('mysql') and which('mysql_config'):
    #mysql_alt = query_yes_no('Looks like MySQL is installed on your system. \n'\
        #'Do you want to use MySQL-python instead of pymysql?')
    #if mysql_alt:
        #mysql = 'MySQL-python'

#deps.append(mysql)

setup(
    name = 'pypath',
    version = __version__,
    maintainer = metainfo['authors']['Türei'][0],
    maintainer_email = metainfo['authors']['Türei'][1],
    author = metainfo['authors']['Türei'][0],
    author_email = metainfo['authors']['Türei'][1],
    long_description = readme + '\n\n' + history,
    keywords = metainfo['keywords'],
    description = metainfo['description'],
    license = metainfo['license'],
    platforms = metainfo['platforms'],
    url = metainfo['url'],
    download_url = metainfo['download_url'],
    classifiers = metainfo['classifiers'],
    # package installation
    package_dir = {'':'src'},
    packages = list(set(find_packages() + ['pypath', 'pypath.data'])),
    include_package_data = True,
    install_requires = deps
)


from m05_my_lab_util import read_file, print_profiles, show_profile

profile_path = r"c:\tmp\m04_lab_profiles.txt"

profile_dict = dict()
sorted_list = list()

profile_dict = read_file(profile_path)

sorted_list = []
for name, profile in profile_dict.items():
    sorted_list.append([name, profile])
print_profiles("  Unsorted List   ", sorted_list)

# sorting of dictionary of dictionaries
sorted_list = list(sorted(profile_dict.items(),
                          key=lambda k_v: k_v[1]["name"]))
print_profiles("  Sorted by Name  ", sorted_list)

sorted_list = list(sorted(profile_dict.items(),
                          key=lambda k_v: k_v[1]["location"]))
print_profiles("Sorted by Location", sorted_list)

sorted_list = list(sorted(profile_dict.items(),
                          key=lambda k_v: k_v[1]["employer"]))
print_profiles("Sorted by Employer", sorted_list)

while True:
    name_to_find = input("\nEnter name: ")
    if len(name_to_find) == 0:
        break

    show_profile(name_to_find, profile_dict)


from random import randint

def rand_num():
    return randint(1,1000)

#check if their input was a valid number
def is_number(stuff):
    try:
        if stuff.isdigit():
            if int(stuff)>=1 and int(stuff)<=1000:
                return True
        else:
            return False

    except (ValueError, TypeError):
        return False

#comparing their guess with the random number
def check(actual,user_guess):
    if actual == user_guess:
        print("You got it!")
        return True
    elif actual< user_guess:
        print("Your guess was too high.")
        return False
    else:
        print("Your guess was too low.")
        return False

x=rand_num()
y=False
while not y:
    z=input("Guess a number(integer) between 1 and 1000.")
    if is_number(z):
        y= check(x,int(z))


from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponse, HttpResponseRedirect
from django.core.files import File
from urllib.request import urlopen
from tempfile import NamedTemporaryFile

from carimage.models import TeslaImage

'''
Tesla uses an image generator which take a few args and return car image
Here is site which allows to play with the args:
https://observablehq.com/@slickplaid/model-3-configurator

The tesla URL is https://static-assets.tesla.com/configurator/compositor

Exemple of valid URL
Model 3
https://static-assets.tesla.com/configurator/compositor?&options=$PMNG,$W39B,$DV4W,$MT304,$IN3PB&view=STUD_3QTR&model=m3&size=200&bkba_opt=1&version=0.0.25
Model S
https://static-assets.tesla.com/configurator/compositor?&options=$WTAS,$PPMR,$MTS03&view=STUD_3QTR_V2&model=ms&size=300&bkba_opt=1&version=v0027d202004163351
Model X
https://static-assets.tesla.com/configurator/compositor?&options=$WT20,$PPMR,$MTX03&view=STUD_3QTR_V2&model=mx&size=300&bkba_opt=1&version=v0027d202004163351

Here are relevant options:

    Query Parameters:
        options - choose what options & accessories to display
        view - choose angle of view of the car
        model - which car to show
        size - photo size
        bkba_opt - background
        version - unknown (value is always 0.0.25)

BKBA_OPT Values

    0 - white background
    1 - transparent background

VIEW Values

    STUD_3QTR - 3-quarter front view
    STUD_SEAT - interior
    STUD_SIDE - side view
    STUD_REAR - shows trunk & spoiler
    STUD_WHEEL - side view of front half of car

OPTION Values

All of these options are one value, comma-separated.
Color (choose one)

    $PBSB - Solid Black
    $PPMR - Red Multi-Coat
    $PMNG - Midnight Silver Metallic (Mid-Night Gray :D)
    $PPSB - Deep Blue Metallic
    $PPSW - Pearl White Multi-Coat
    $PMSS - Silver Metallic
    $PMBL - Obsidian Black

Wheel (choose one)

    $W38B - 18" Aero Wheels
    $W39B - 19" Sport Wheels
    $W32B - 20" Sport Wheels (doesn't work for STUD_SIDE)

Spoiler

    $SLR1 - Carbon-fiber spoiler

AWD/RWD (choose one)

    $DV2W - RWD
    $DV4W - AWD

Suspension (choose one)

    $MT302 - Shows with RWD, highest
    $MT303 - Shows with AWD, low
    $MT304 - Shows with Performance, lowest

Interior (choose one)

    $PFP31 - White interior
    $IN3PB - Black interior (for STUD_SEAT)
    $IN3PW - White interior (for STUD_SEAT)

'''

'''
Here is the info returned by my car.
'AD15,MDL3,PBSB,RENA,BT37,ID3W,RF3G,S3PB,DRLH,DV2W,W39B,APF0,COUS,BC3B,CH07,PC30,FC3P,FG31,GLFR,
HL31,HM31,IL31,LTPB,MR31,FM3B,RS3H,SA3P,STCP,SC04,SU3C,T3CA,TW00,TM00,UT3P,WR00,AU3P,APH3,AF00,ZCST,MI00,CDM0'

See for explanations, bit he signals that codes are not reliable anymore
https://tesla-api.timdorr.com/vehicle/optioncodes
'''

def CreateImageFile(image):
    img_temp = NamedTemporaryFile(delete=True)
    # get from tesla and save
    img_temp.write(urlopen(image.image_url).read())
    img_temp.flush()
    image.image_file.save(f"image_{image.pk}", File(img_temp))
    # save in the db
    image.save()

# for url parse, see https://docs.djangoproject.com/en/3.0/topics/http/urls/
# color comes from codes as it seems correct and is the string we want (at least on my car)
# wheel and carmodel comes from car info, not from car info codes due to reliability
def CarImageFromTesla(request, color, wheel, CarModel):
    size = "400"

    # Assume 19 as I can only identify 18 reliably as it is my car wheels
    wheelToUse = "W39B"
    if wheel == "Pinwheel18":
        wheelToUse = "W38B"

    # Assume model 3 as I have no idea of the params for other cars
    if CarModel == "modelx":
        CarModelToUse = "mx"
        url = "https://static-assets.tesla.com/configurator/compositor?&options=$WT20,$" + color + ",$MTX03&view=STUD_3QTR_V2&model=" + CarModelToUse + "&size=" + size + "&bkba_opt=1&version=v0027d202004163351"
    else:
        if CarModel == "models" or CarModel == "models2":
            CarModelToUse = "ms"
            url = "https://static-assets.tesla.com/configurator/compositor?&options=$WTAS,$" + color + ",$MTS03&view=STUD_3QTR_V2&model=" + CarModelToUse + "&size=" + size + "&bkba_opt=1&version=v0027d202004163351"
        else:
            if CarModel == "modely":
                CarModelToUse = "my"
                url = "https://static-assets.tesla.com/configurator/compositor?&options=$WY19B,$" + color + ",$DV4W,$MTY03,$INYPB&view=STUD_3QTR&model=" + CarModelToUse + "&size=" + size + "&bkba_opt=1&version=v0027d202004163351"
            else:  # model 3
                CarModelToUse = "m3"
                url = "https://static-assets.tesla.com/configurator/compositor?&options=$" + color + ",$" + wheelToUse + ",$DV4W,$MT303,$IN3PB&view=STUD_3QTR&model=" + CarModelToUse + "&size=" + size + "&bkba_opt=1&version=0.0.25"

    # Get the image from cache, if there is a problem, redirect to tesla site
    # code inpired from https://stackoverflow.com/questions/16381241/django-save-image-from-url-and-connect-with-imagefield
    willNeedFileCreation = False
    try:
        try:
            # get from cache
            image = TeslaImage.objects.get(image_url=url)
        except ObjectDoesNotExist:
            # add it with image from tesla
            image = TeslaImage()
            image.image_url = url
            # save in the db in order that primary key is init
            image.save()
            willNeedFileCreation = True
        # check if a file with image is present. Will be absent on initial
        # and also when image dir is cleaned
        if image.image_url and not image.image_file:
            willNeedFileCreation = True
        if willNeedFileCreation:
            CreateImageFile(image)
        # Try to return cache entry
        try:
            return HttpResponse(image.image_file, content_type="image/png")
        except Exception:
            CreateImageFile(image)  # image was surely missing, seems that test on image.image_file fails
            return HttpResponse(image.image_file, content_type="image/png")
    except Exception:
        # go to tesla site
        return HttpResponseRedirect(url)

# valid url example:
# http://127.0.0.1:8000/carimage/PBSB/Pinwheel18/model3


print('-=-' * 15, '\033[31mSomando Valores\033[m', '-=-' * 15)

print('\033[31mAviso: para Parar digite "999"!\033[0m (O número "999" será desconsidarado na soma)')

soma = num = 0
while True:
    num = int(input('Digite um número a ser somado: '))
    if num == 999:
        break
    soma += num

print(f'A soma de todos os valores foi de \033[34m{soma}\033[0m.')


#!/usr/bin/python2.7

import dendropy
from dendropy.calculate import treecompare
import numpy
import os
import random
import scipy.stats
import subprocess
import sys

n_species_trees = int(sys.argv[1])

catapillar_newick = "((((s0,s1),s2),s3),s4);"
catapillar_tree = dendropy.Tree.get(data = catapillar_newick, schema = "newick", rooting = "force-rooted")

biopy_tree_filename = "biopy.species.tree"
complete_tree_filename = "simulated.species.tree"

biopy_cmd = ["sample_species_tree", "-o", biopy_tree_filename, "-p", "i,3,0.2", "-b", "10.0", "-d", "0.0", "-n", "1000", "5"]

random.seed()
numpy.random.seed()

for rep_i in range(n_species_trees):
	rep_folder = "rep-%02d" % (rep_i)
	if not os.path.exists(rep_folder):
		os.mkdir(rep_folder)

	biopy_tree_path = os.path.join(rep_folder, biopy_tree_filename)

	subprocess.check_call(biopy_cmd, cwd = rep_folder)
	biopy_trees = dendropy.TreeList.get(path = biopy_tree_path, schema = "nexus", rooting = "force-rooted", taxon_namespace = catapillar_tree.taxon_namespace)

	is_catapillar = False
	for bt in biopy_trees:
		print(bt)
		print(catapillar_tree)
		print(treecompare.symmetric_difference(catapillar_tree, bt))
		if treecompare.symmetric_difference(catapillar_tree, bt) == 0:
			species_tree = bt

	complete_tree_path = os.path.join(rep_folder, complete_tree_filename)
	species_tree.write(path = complete_tree_path, schema = "nexus", suppress_rooting = True, suppress_annotations = False)


# 스마트 물류 414

n, k = map(int, input().split())
line = input()
check = set()
res = 0

for i in range(n):
    if line[i] == "P":
        for j in range(max(0, i - k), min(n, i + k + 1)):
            if line[j] == "H" and j not in check:
                check.add(j)
                res += 1
                break

print(res)


from PIL import Image
import numpy as np
import tensorflow as tf
import os

data_dir = 'test'
model_path = 'model/image_model'

def read_data(data_dir):
    datas = []
    labels = []
    fpaths = []
    for name in os.listdir(data_dir):
        fpath = os.path.join(data_dir, name)
        fpaths.append(fpath)

        image = Image.open(fpath)
        print(image.size, image.format, image.mode)
        #data = np.asarray(image, dtype=np.float32)/255.0
        data = np.array(image)/255.0

        label = int(name.split('_')[0])
        datas.append(data)
        labels.append(label)

    datas = np.array(datas)
    labels = np.array(labels)
    print("shape of datas: {}\tshape of labels: {}".format(datas.shape, labels.shape))
    return fpaths, datas, labels

fpaths, datas, labels = read_data(data_dir)

# 计算有多少类图片
num_classes = len(set(labels))

#定义placeholder，存放输入和标签
datas_placeholder = tf.placeholder(tf.float32, [None, 32, 32, 3])
labels_placeholder = tf.placeholder(tf.int32, [None])
#存放Dropout参数的容器， 训练时为0.25，测试时为0
dropout_placeholder = tf.placeholder(tf.float32)

# 定义卷积层, 20个卷积核, 卷积核大小为5，用Relu激活
conv0 = tf.layers.conv2d(datas_placeholder, 20, 5, activation=tf.nn.relu)
# 定义max-pooling层，pooling窗口为2x2，步长为2x2
pool0 = tf.layers.max_pooling2d(conv0, [2, 2], [2, 2])

# 定义卷积层, 40个卷积核, 卷积核大小为4，用Relu激活
conv1 = tf.layers.conv2d(pool0, 40, 4, activation=tf.nn.relu)
# 定义max-pooling层，pooling窗口为2x2，步长为2x2
pool1 = tf.layers.max_pooling2d(conv1, [2, 2], [2, 2])

# 定义卷积层, 60个卷积核, 卷积核大小为3，用Relu激活
conv2 = tf.layers.conv2d(pool1, 60, 3, activation=tf.nn.relu)
# 定义max-pooling层，pooling窗口为2x2，步长为2x2
pool2 = tf.layers.max_pooling2d(conv2, [2, 2], [2, 2])

# 将3维特征转换为1维向量
flatten = tf.layers.flatten(pool2)

# 全连接层，转换为长度为100的特征向量
fc = tf.layers.dense(flatten, 400, activation=tf.nn.relu)

# 加上DropOut，防止过拟合
dropout_fc = tf.layers.dropout(fc, dropout_placeholder)

# 未激活的输出层
logits = tf.layers.dense(dropout_fc, num_classes)

predicted_labels = tf.arg_max(logits, 1)

# 利用交叉熵定义损失
losses = tf.nn.softmax_cross_entropy_with_logits(
    labels=tf.one_hot(labels_placeholder, num_classes),
    logits=logits
)
# 平均损失
mean_loss = tf.reduce_mean(losses)

# 定义优化器，指定要优化的损失函数
optimizer = tf.train.AdamOptimizer(learning_rate=1e-2).minimize(losses)

# 用于保存和载入模型
#saver = tf.train.import_meta_graph('model/image_model.meta')
saver = tf.train.Saver()

with tf.Session() as sess:
    print('测试模式')
    saver.restore(sess, model_path)
    print('从{}载入模型 '.format(model_path))
    #label和名称的对照关系
    label_name_dict = {
        0:'巴士',
        1:'出租车',
        2:'货车',
        3:'家用轿车',
        4:'面包车',
        5:'吉普车',
        6:'运动型多功能车',
        7:'重型货车',
        8:'赛车',
        9:'消防车'
    }

    test_feed_dict = {
        datas_placeholder:datas,
        labels_placeholder:labels,
        dropout_placeholder:0
    }

    predicted_labels_val = sess.run(predicted_labels, feed_dict=test_feed_dict)
    #真实label和模型预测label
    for fpath, real_label, predicted_label in zip(fpaths, labels, predicted_labels_val):
        real_label_name = label_name_dict[real_label]
        predicted_label_name = label_name_dict[predicted_label]
        print('{}\t{} => {}'.format(fpath, real_label_name, predicted_label_name))

    correct_number = 0
    for fpath, real_label, predicted_label in zip(fpaths, labels, predicted_labels_val):
        if real_label == predicted_label:
            correct_number += 1
    correct_rate = correct_number/200
    print('正确率：{:.2%}'.format(correct_rate))


import sys
import json
from datetime import datetime, timedelta
from pathlib import Path
from textwrap import dedent

import numpy as np
import tweepy

def th(n):
    """Ordinal of an integer, eg "1st", "2nd" etc"""
    return str(n) + (
        "th" if 4 <= n % 100 <= 20 else {1: "st", 2: "nd", 3: "rd"}.get(n % 10, "th")
    )

def fmt(a):
    """Dedent and replace single newline characters with a space"""
    return '\n\n'.join(line.replace('\n', ' ') for line in dedent(a).split('\n\n'))

def stats():
    stats = json.loads(Path("latest_nsw_stats.json").read_text())
    today = stats['today']  # The date of the last update - should be today
    R_eff = stats['R_eff']
    u_R_eff = stats['u_R_eff']
    today = datetime.fromisoformat(today)
    today = f'{today.strftime("%B")} {th(today.day)}'
    return today, R_eff, u_R_eff

def tweet_1_text():
    today, R_eff, u_R_eff = stats()
    COMMENT_TEXT = f"""\
    NSW R_eff as of {today} with daily cases and restrictions. Latest estimate:
    R_eff = {R_eff:.2f} ± {u_R_eff:.2f}

    Cases shown on a linear scale (first image) and log scale (second image).

    More info https://chrisbillington.net/COVID_NSW.html"""
    return fmt(COMMENT_TEXT)

def tweet_2_text():
    today, R_eff, u_R_eff = stats()
    COMMENT_TEXT = f"""\
    NSW R_eff as of {today} with daily cases and restrictions. Latest estimate:
    R_eff = {R_eff:.2f} ± {u_R_eff:.2f}

    Plus SIR model projection.

    (Cases shown on a log scale)

    More info https://chrisbillington.net/COVID_NSW.html

    #covid19nsw #covidsydney"""
    return fmt(COMMENT_TEXT)

def tweet_3_text():
    stats = json.loads(Path("latest_nsw_stats.json").read_text())
    R_eff_sydney = stats['R_eff_sydney']
    u_R_eff_sydney = stats['u_R_eff_sydney']
    R_eff_not_sydney = stats['R_eff_not_sydney']
    u_R_eff_not_sydney = stats['u_R_eff_not_sydney']

    COMMENT_TEXT = f"""\
    R_eff in Greater Sydney vs rest of New South Wales:

    Greater Sydney: R_eff = {R_eff_sydney:.02f} ± {u_R_eff_sydney:.02f}
    NSW excluding Greater Sydney: R_eff = {R_eff_not_sydney:.02f} ± {u_R_eff_not_sydney:.02f}

    (Cases shown on a log scale)

    (note region-specific data is several days old, does not include today's cases)

    #covid19nsw #covidsydney"""
    return dedent(COMMENT_TEXT)

def tweet_4_text():
    stats = json.loads(Path("latest_nsw_stats.json").read_text())
    R_eff_concern = stats['R_eff_concern']
    u_R_eff_concern = stats['u_R_eff_concern']
    R_eff_others = stats['R_eff_others']
    u_R_eff_others = stats['u_R_eff_others']

    COMMENT_TEXT = f"""\
    R_eff in LGAs of concern* vs the rest of NSW (*all of Penrith included):

    LGAs of concern: R_eff = {R_eff_concern:.02f} ± {u_R_eff_concern:.02f}
    Rest of NSW: R_eff = {R_eff_others:.02f} ± {u_R_eff_others:.02f}

    (Cases shown on a log scale)

    (note LGA data is several days old, does not include today's cases)

    #covid19nsw #covidsydney"""
    return dedent(COMMENT_TEXT)

def tweet_5_text():
    stats = json.loads(Path("latest_nsw_stats.json").read_text())
    R_eff_hunter = stats['R_eff_hunter']
    u_R_eff_hunter = stats['u_R_eff_hunter']
    R_eff_illawarra = stats['R_eff_illawarra']
    u_R_eff_illawarra = stats['u_R_eff_illawarra']
    R_eff_wnsw = stats['R_eff_wnsw']
    u_R_eff_wnsw = stats['u_R_eff_wnsw']

    COMMENT_TEXT = f"""\
    The Hunter region: R_eff = {R_eff_hunter:.02f} ± {u_R_eff_hunter:.02f}

    Illawarra region: R_eff = {R_eff_illawarra:.02f} ± {u_R_eff_illawarra:.02f}

    Western New South Wales: R_eff = {R_eff_wnsw:.02f} ± {u_R_eff_wnsw:.02f}

    (Cases shown on a log scale)

    (note region-specific data is several days old, does not include today's cases)

    #covid19nsw #covidsydney"""
    return dedent(COMMENT_TEXT)

def tweet_6_text():
    stats = json.loads(Path("latest_nsw_stats.json").read_text())

    proj_lines = [
        "day  cases  68% range",
    ]
    for proj in stats['projection'][1:8]:
        presser_date = datetime.fromisoformat(proj['date']) + timedelta(1)
        cases = proj['cases']
        lower = proj['lower']
        upper = proj['upper']
        weekday = presser_date.strftime("%a:")
        cases_str = f"{cases:.0f}".rjust(5)
        range_str = f"{lower:.0f}—{upper:.0f}".rjust(10)
        proj_lines.append(" ".join([weekday, cases_str, range_str]))

    proj_lines = "\n    ".join(proj_lines)

    doubling_time = 5 * np.log(2) / np.log(stats['R_eff'])

    doubling_or_halving = "Doubling" if doubling_time > 0 else "Halving"

    COMMENT_TEXT = f"""\
    Cases in NSW if the current trend continues:

    {proj_lines}

    {doubling_or_halving} time is {abs(doubling_time):.1f} days."""

    return dedent(COMMENT_TEXT)

if __name__ == '__main__':
    api_key = sys.argv[1]
    api_secret_key = sys.argv[2]
    access_token = sys.argv[3]
    access_token_secret = sys.argv[4]

    auth = tweepy.OAuthHandler(api_key, api_secret_key)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth, wait_on_rate_limit=True)

    # Upload images
    linear = api.media_upload("COVID_NSW_linear.png")
    vax_linear = api.media_upload("COVID_NSW_vax_linear.png")
    log = api.media_upload("COVID_NSW.png")
    vax_log = api.media_upload("COVID_NSW_vax.png")
    concern = api.media_upload("COVID_NSW_LGA_concern.png")
    others = api.media_upload("COVID_NSW_LGA_others.png")
    sydney = api.media_upload("COVID_NSW_sydney.png")
    not_sydney = api.media_upload("COVID_NSW_not_sydney.png")
    hunter = api.media_upload("COVID_NSW_hunter.png")
    illawarra = api.media_upload("COVID_NSW_illawarra.png")
    wnsw = api.media_upload("COVID_NSW_wnsw.png")

    # Post tweets with images
    tweet_1 = api.update_status(
        status=tweet_1_text(),
        media_ids=[linear.media_id, log.media_id],
    )

    # tweet_2 = api.update_status(
    #     status=tweet_2_text(),
    #     media_ids=[log.media_id, vax_log.media_id],
    #     in_reply_to_status_id=tweet_1.id,
    #     auto_populate_reply_metadata=True,
    # )

    # tweet_3 = api.update_status(
    #     status=tweet_3_text(),
    #     media_ids=[sydney.media_id, not_sydney.media_id],
    #     in_reply_to_status_id=tweet_2.id,
    #     auto_populate_reply_metadata=True,
    # )

    # tweet_4 = api.update_status(
    #     status=tweet_4_text(),
    #     media_ids=[concern.media_id, others.media_id],
    #     in_reply_to_status_id=tweet_3.id,
    #     auto_populate_reply_metadata=True,
    # )

    # tweet_5 = api.update_status(
    #     status=tweet_5_text(),
    #     media_ids=[hunter.media_id, illawarra.media_id, wnsw.media_id],
    #     in_reply_to_status_id=tweet_4.id,
    #     auto_populate_reply_metadata=True,
    # )

    # tweet_6 = api.update_status(
    #     status=tweet_6_text(),
    #     in_reply_to_status_id=tweet_2.id,
    #     auto_populate_reply_metadata=True,
    # )


from utils.types import strict_types
from empleos import Empleo
from trabajador import Trabajador

class Empresa:
    @strict_types
    def __init__(self, nombre: str, tipo: Empleo):
        self.nombre = nombre
        self.tipo = tipo
        self.empleados = []

    @strict_types
    def contratar(self, empleado: Trabajador):
        empleo_switch = {
            "PROGRAMACION": "programador",
            "DISENO": "diseñador",
            "CONSTRUCCION": "obrero"
        }
        empleo = empleo_switch.get(Empleo.tostr(self.tipo))

        if empleado.empleo != self.tipo:
            raise Exception("{} no pasó la entrevista, no es un {}".format(empleado.nombre, empleo))
        else:
            self.empleados.append(empleado)

    def trabajar_jornada(self):
        for trabajador in self.empleados:
            trabajador.trabajar()


def getMean(a,b,c,d,e,f,g,h):
    sm = a+b+c+d+e+f+g+h
    return sm
    mean = sm/8
    return mean

def getMode(a,b,c,d,e,f,g,h):
    lis = [a,b,c,d,e,f,g,h]
    for x  in lis:
        x = int(x)
    mode = max(lis)
    return mode
def outputName(a):
    print("Hi",a)
def replacein(phrase):
    word = ""
    for letter in phrase:
        if letter.lower() in "aeiou":
            word = word+"g"
        else:
            word = word+letter
    return word
print(replacein(input("Enter phrase: ")))


# -*- coding:utf-8 -*-

"""
时间:2021年10月03日
作者:幻非
"""

import os
import tkinter

player = tkinter.Tk()
player.title("定时关机")
mw, mh = player.maxsize()
player.geometry('300x200+%d+%d' % ((mw - 500) / 2, (mh - 300) / 2))  # 窗口居中
player.resizable(False, False)  # 锁定窗口大小

def start():
    if status.get() == 1:
        time = int(text.get()) * 60
        close(time)
    elif status.get() == 2:
        time = int(text.get()) * 3600
        close(time)

# 计时关机
def close(self):
    if self < 315360000:
        shutdown = "shutdown -s -t " + str(self)
        if os.system(shutdown) != 0:
            os.system('shutdown -a')
            os.system(shutdown)
    else:
        print("超出范围")

# 结束关机
def end():
    os.system('shutdown -a')

Frame1 = tkinter.Frame(player)
Frame1.pack(side="top", pady=25)

Frame2 = tkinter.Frame(player)
Frame2.pack()

text = tkinter.Entry(Frame1, width=5, font=('', '40'), justify='center')
text.pack(side="right")

status = tkinter.IntVar()
status.set(2)
tkinter.Radiobutton(Frame1, text="分钟", variable=status, value=1, font=('', '18')).pack()
tkinter.Radiobutton(Frame1, text="小时", variable=status, value=2, font=('', '18')).pack()

btn_random_video = tkinter.Button(Frame2, text="开始计时", font=('', '18'), command=start)
btn_random_video.pack(side="left", padx=5)
btn_random_video = tkinter.Button(Frame2, text="关闭计时", font=('', '18'), command=end)
btn_random_video.pack(side="right", padx=5)

player.mainloop()


#!/usr/bin/env python3
# sql_searcher_indexer_v_bat.py
# Last Updated 202101050930

import SQL_Searcher_Class

if __name__ == "__main__":
    # Parameters and settings
    root_folder = r"V:\Business Analysis Team"
    index_file_name = "sql_file_index_v_bat.txt"
    output_file_name = ""

    # Create the object
    tool = SQL_Searcher_Class.SQL_Searcher(root_folder, index_file_name, output_file_name)

    # Create index
    tool.create_index()


name = input("Enter Your Name: ")
print(f"Welcome {name} to this Advanture!")

user_input = input("You are lost in a Jungle and now there are Two ways ahead left and right. Type left or right: ").lower()
if user_input == "left":
    q1 = input("Okay theres a river ahead how you want to cross it by swim or bridge. Type bridge or swim: ").lower()
    if q1 == "swim":
        print("You are drowned. You Lost!")
    elif q1 == "bridge":
        print("The bridge break you Lost!")
    else:
        print("Invalid input you lost!")

elif user_input == "right":
    q2 = input("Now there is a mountain ahead. How you want to cross it. Type cave or climb:").lower()
    if q2 == "cave":
        print("You Lost Yourself and Die. You Lost!")
    elif q2 == "climb":
        print("Congrats You find the civilization ahead. You Won!!")
    else:
        print("Invalid input you lost!")
else:
    print("Invalid input you lost!")


from tqdm import tqdm
from time import sleep

class Progress():

    def __init__(self, secs, prefix):
        self.secs = secs
        self.prefix = prefix

    def bar(self):
        with tqdm(total=100,
                  ncols=65,
                  bar_format='{l_bar}{bar}|',
                  desc=self.prefix) as pbar:
            for i in range(100):
                pbar.update(1)
                sleep(self.secs)


from http.server import HTTPServer
from threading import Thread
from typing import Any, Dict

def pretty_print(clause: Dict[str, Any]) -> str:
    """
    Print a logical formula back to TPTP language.

    :param clause: a logical clause to print
    :returns: a TPTP string
    """
    res = f"cnf({clause['label']}, {clause['role']}, "
    res += clause["literals"]
    res += (
        f", inference({clause['inference_rule']}, [], ["
        + ", ".join(clause["inference_parents"])
        + "])"
    )
    return res + ")."

def tptp2python(literals: str) -> str:
    """
    Transform a TPTP CNF formula literals to look like Python code.

    :param literals: literals of a TPTP-formatted first-order formula in CNF
    :return: (hopefully syntactically correct) Python Boolean-valued expression
    """
    return (
        literals.replace("==", "^^")
        .replace("!=", "^^^")
        .replace("=", "==")
        .replace("^^^", "!=")
        .replace("^^", "==")
        .replace("$false", "False")
        .replace("as", "__as")
    )

def start_server_in_a_thread(server: HTTPServer) -> None:
    """
    Start a given HTTP server as a daemon.

    :param server: HTTP server
    """
    thread = Thread(target=server.serve_forever)
    thread.daemon = True
    thread.start()


"""Serializers for our common models.

Serializers convert our data base objects to json and back again (if
needed).

  """

from fsdviz.common.models import (
    CWT,
    Agency,
    CompositeFinClip,
    FishTag,
    Grid10,
    Jurisdiction,
    Lake,
    LatLonFlag,
    ManagementUnit,
    Mark,
    PhysChemMark,
    Species,
    StateProvince,
    Strain,
    StrainRaw,
)
from rest_framework import serializers

class ModelColorSerializer(serializers.ModelSerializer):
    """An abstract base class for serializers for objects that have an
    optional colour attribute.  Colours only need be returned on
    lookup-api endpoints - not every endpoint. Colour is returned if
    color=True is passed to the serializer."""

    def __init__(self, *args, **kwargs):
        color = kwargs.pop("color", False)
        super(ModelColorSerializer, self).__init__(*args, **kwargs)
        if color is False:
            self.fields.pop("color")

class AgencySerializer(ModelColorSerializer):
    class Meta:
        model = Agency
        fields = ("abbrev", "agency_name", "color")

class LakeSerializer(ModelColorSerializer):
    class Meta:
        model = Lake
        fields = ("abbrev", "lake_name", "color")
        lookup_field = "abbrev"

class StateProvinceSerializer(ModelColorSerializer):
    class Meta:
        model = StateProvince
        fields = ("id", "abbrev", "name", "country", "description", "color")

class JurisdictionSerializer(ModelColorSerializer):

    lake = LakeSerializer()
    stateprov = StateProvinceSerializer()

    # lake = serializers.ReadOnlyField(source='lake.lake_name')
    # stateprov = serializers.ReadOnlyField(source='stateprov.name')

    class Meta:
        model = Jurisdiction
        fields = ("id", "slug", "name", "lake", "stateprov", "description", "color")

    @staticmethod
    def setup_eager_loading(queryset):
        """Perform necessary eager loading of data."""
        queryset = queryset.select_related("lake", "stateprov")
        return queryset

class ManagementUnitSerializer(ModelColorSerializer):

    lake = LakeSerializer()

    class Meta:
        model = ManagementUnit
        fields = (
            "id",
            "label",
            "description",
            "lake",
            "mu_type",
            "slug",
            "primary",
            "color",
        )

    @staticmethod
    def setup_eager_loading(queryset):
        """Perform necessary eager loading of data."""
        queryset = queryset.select_related("lake")
        return queryset

class SimpleSpeciesSerializer(serializers.ModelSerializer):
    """A simple serializer for species objects that contains just the
    abbreviation and common name. This serializer is intended to be used
    as a nested-serializer.
    """

    class Meta:
        model = Species
        fields = (
            "abbrev",
            "common_name",
        )
        lookup_field = "abbrev"

class SpeciesSerializer(ModelColorSerializer):
    class Meta:
        model = Species
        fields = (
            "abbrev",
            "common_name",
            "scientific_name",
            "species_code",
            "speciescommon",
            "color",
        )
        lookup_field = "abbrev"

class StrainSpeciesSerializer(ModelColorSerializer):
    """A serialicer for strain objects that also includes attributes of
    the related species.  There can only be one related species
    object for each strain.
    """

    strain_species = SpeciesSerializer(many=False)

    class Meta:
        model = Strain
        fields = (
            "id",
            "strain_code",
            "strain_label",
            "strain_species",
            "slug",
            "color",
        )

    @staticmethod
    def setup_eager_loading(queryset):
        """Perform necessary eager loading of data."""
        queryset = queryset.select_related("strain_species")
        return queryset

class StrainSerializer(ModelColorSerializer):
    """A simplified serializer for strain objects - used in the strainRaw
    Serializers.  This version does *not* include the species serializer as
    it is already included in the StrainRawSerializer.
    """

    class Meta:
        model = Strain
        fields = ("id", "strain_code", "strain_label", "slug", "color")

class StrainRawSerializer(ModelColorSerializer):
    """A serializer for Strain Raw objects. Includes nested serializers
    for both species and strain objects.  Uses the simplified strain
    serializer that does not include species.  StrainRaw is
    effectively an association table between species and strain -
    there can only be one species and one strain fro each record.
    """

    species = SpeciesSerializer(many=False)
    strain = StrainSerializer(many=False)

    class Meta:
        model = StrainRaw
        fields = ("id", "raw_strain", "description", "species", "strain", "color")

    @staticmethod
    def setup_eager_loading(queryset):
        """Perform necessary eager loading of data."""
        queryset = queryset.select_related("species", "strain")
        return queryset

class CWTSerializer(serializers.ModelSerializer):
    class Meta:
        model = CWT
        fields = (
            "cwt_number",
            "tag_type",
            "manufacturer",
            "slug",
            # "tag_reused",
            # "multiple_species",
            # "multiple_strains",
            # "multiple_yearclasses",
            # "multiple_makers",
            # "multiple_agencies",
            # "multiple_lakes",
            # "multiple_grid10s",
        )

class Grid10Serializer(serializers.ModelSerializer):

    lake = LakeSerializer()

    #    lake = serializers.HyperlinkedRelatedField(
    #        view_name='common_api:lake-detail', lookup_field='abbrev',
    #        read_only=True)

    class Meta:
        model = Grid10
        fields = ("id", "grid", "lake", "centroid", "slug")

    @staticmethod
    def setup_eager_loading(queryset):
        """Perform necessary eager loading of data."""
        queryset = queryset.select_related("lake")
        return queryset

class LatLonFlagSerializer(serializers.ModelSerializer):
    class Meta:
        model = LatLonFlag
        fields = ("value", "description")

class MarkSerializer(serializers.ModelSerializer):
    class Meta:
        model = Mark
        fields = ("clip_code", "mark_code", "mark_type", "description")

class CompositeFinClipSerializer(ModelColorSerializer):
    class Meta:
        model = CompositeFinClip
        fields = ("clip_code", "description", "color")

class FishTagSerializer(ModelColorSerializer):
    class Meta:
        model = FishTag
        fields = ("tag_code", "tag_type", "tag_colour", "description", "color")

class PhysChemMarkSerializer(ModelColorSerializer):
    class Meta:
        model = PhysChemMark
        fields = ("mark_code", "mark_type", "description", "color")


"""
Iterável -> str, range, etc (__iter__)
Iterador -> quem sabe entregar um valor por vez
next -> me entregue o próximo valor
iter -> me entregue seu iterador
"""

# numeros = range(0, 100, 8)

# for numro in numeros:
#     print(numro)

# for letra in texto:  ("!!!for equivale a esta operação!!!")

texto = 'Daniel'
iterator = iter(texto)  # texto.__iter__()       # Interável:

print(next(iterator))      # print(iterator.__next__())   # Iterator:

while True:
    try:
        letra = next(iterator)
        print(letra)
    except StopIteration:
        break


from hexalattice.hexalattice import *
import numpy as np
from fonctions_frontieres import *
from fonctions_plot import *

# ---------PARAMETRES--------------------

rho = 0.3       # Densite
kappa = 0.5     # Freezing parameter
iterations = 5 # Number of iterations

# ----------RESEAU -------------------
N=7# Taille de la grille
hex_centers, _ = create_hex_grid(nx=N,          # Création du résau
                                 ny=N,
                                 do_plot=False)

x_hex_coords = hex_centers[:, 0]
y_hex_coords = hex_centers[:, 1]

color_vapor = np.full((N ** 2,3), [1, 0, 0]) # ROUGE pour la vapeur
color_ice = np.full((N ** 2,3), [0, 0, 1]) # BLEU pour la glace
color_quasi = np.full((N ** 2,3), [0, 1, 0]) # VERT pour quasi-liquid

colors_init = color_ice + color_vapor + color_quasi

# --------------------------- MASK INITIAL --------------------
mask_tot = np.full((N ** 2,4), [0, 0, 0, rho])    # Mask totale a=(0 ou 1 si dans cristal) b : boundary mass (quasi-liquid)
                                    # c : cristal mass (ice) d : diffusive mass (vapor)

mask_tot[int((len(mask_tot) / 2)-N/2)] = [1, 0, 1, 0]       # On fixe au milieu une cellule gelée
centre = int((len(mask_tot) / 2)-N/2 )
# ----------------------FONCTIONS ÉVOLUTION---------------------------

def freezing(mask0, N, centre, kappa) :
    ## b0, d0 = masques initiaux
    # centre = index
    mask1 = mask0
    idxa,idxb,idxc,idxd,idxe,idxf,idxg = alentours_idx(N,centre)

    idx = [idxb,idxc,idxd,idxe,idxf,idxg]
    for i in range(len(idx)) :
        mask1[idx[i],1] = mask0[idx[i], 1] + ((1-kappa)*mask0[idx[i], 3])

        mask1[idx[i],2]= mask0[idx[i], 1] + (kappa*mask0[idx[i], 3])

        mask1[idx[i],3] = 0

    return mask1

# -----------------------UDPDATE MASK ------------------------------

for t in range(15):

    mask_change = mask_tot
    for i in range(len(mask_tot)):
        case = mask_tot[i]
        if i%N == 0 or (i+1)%N == 0 or i < N or i > N*(N-1):
            continue

        elif case[0]==1:
            continue

        elif prox_crystal(mask_tot,i) != 0: # est a proximité du cristal, condition réfléchie
            new_value = somme_vap_voisin_cristal(mask_tot, i, N) #Laplacien condition réfléchie
            mask_change[i,3] = new_value

        else:
            ele = alentours(mask_tot,i) #
            new_value = somme_vap(ele)
            mask_change[i,3] = new_value
    mask_tot = mask_change

# ---------------------PLOT RESULTS------------------------------------

""""
TUTO PLOT :
    - Pour  plot tout : plot_total(mask_tot, color_ice, color_vapor, color_quasi, x_hex_coords, y_hex_coords, N)

    POUR UNE SEULE PHASE AJOUTER plt.show()

    - Pour plot glace : plot_ice(mask_tot, color_ice, x_hex_coords, y_hex_coords, N)
    - Pour plot vapeur : plot_vapeur(mask_tot, color_vapor, x_hex_coords, y_hex_coords, N)
    - pour plot QLL : plot_quasi(mask_tot, color_quasi, x_hex_coords, y_hex_coords, N)
"""
plot_total(mask_tot, color_ice, color_vapor, color_quasi, x_hex_coords, y_hex_coords, N)


T = int(input())
for ttt in range(T):
    string = list(input().strip())
    stack = []
    for i in string:
        if i.isdigit():
            stack.append(i)
        else:
            a = stack.pop()
            b = stack.pop()
            s = b+i+a
            res = eval(s)
            stack.append(str(res))
    print(stack[0])


import sqlite3
from contextlib import contextmanager
from config import SQLITE_DB_PATH
import functools
from typing import Tuple

@contextmanager
def sqlite_connection():
    conn = sqlite3.connect(SQLITE_DB_PATH)
    c = conn.cursor()
    try:
        yield c
    finally:
        conn.close()

@functools.lru_cache(maxsize=10)
def fetch_article(article_id: int, c: sqlite3.Cursor) -> Tuple[str, str]:
    query = "SELECT title, published FROM articles WHERE id = ?;"
    c.execute(query, (article_id,))
    res = c.fetchone()
    title = res[0]
    date_published = res[1].split("T")[0]
    return title, date_published

def fetch_article_split_text(article_id: int, position: int, c: sqlite3.Cursor) -> str:
    query = "SELECT text FROM articles_split WHERE article_id = ? AND " "position = ?;"
    c.execute(query, (article_id, position))
    text = c.fetchone()[0]
    return text


import os
import pandas as pd
import numpy as np
import requests
import gdown

from sklearn.model_selection import train_test_split

from sklearn_pandas import DataFrameMapper

_val_idx = {"B": 1, "C": 2, "D": 3}

def cut_val_band(df, band, val):
  return df[df[band] <= val]

def cut_flag(df, attr, value):
  chunk_size = df[df[attr] == value].shape[0]
  print(f"{chunk_size} values cut off.")

  return df[df[attr] != value]

def cut_all_val_errs(df, dataset_name, val):
  if dataset_name == 'sdss':
    return  _cut_all_val_errs(df, val, s='err_')
  else:
    return _cut_all_val_errs(df, val, p='Err')

def _cut_all_val_errs(df, val, s='', p=''):
  df = df[df[f"{s}u{p}"] <= val]
  df = df[df[f"{s}g{p}"] <= val]
  df = df[df[f"{s}r{p}"] <= val]
  df = df[df[f"{s}i{p}"] <= val]
  df = df[df[f"{s}z{p}"] <= val]

  return df

def remove_col_df(dataframe, col_to_remove):
  for c in col_to_remove:
    if c in dataframe.columns:
      dataframe.drop(columns=[c], axis=1,inplace=True)

def ugriz_errs_split(x, chunks=2):
  dim = x.shape[1]
  pad = int(dim / chunks)
  if chunks == 2:
    return x.iloc[:, :pad], x.iloc[:, pad:pad*2], [[]], [[]]
  elif chunks == 3:
    return x.iloc[:, :pad], x.iloc[:, pad:pad*2], x.iloc[:, pad*2:pad*3], [[]]
  else:
    return x.iloc[:, :pad], x.iloc[:, pad:pad*2], x.iloc[:, pad*2:pad*3], x.iloc[:, pad*3:pad*4]

def filter_col(dataframe):
  #remove_col_df(dataframe, ('ID', '#ID', 'redshiftErr','objid', 'specobjid', 'class'))
  remove_col_df(dataframe, ('ID', '#ID', 'redshiftErr', 'specobjid', 'class', 'clean'))

  if 'objid' in dataframe.columns:
    objid = dataframe.pop('objid')
    size = len(dataframe.columns)
    dataframe.insert(size-1, 'objid', objid)

  return dataframe

def filter_negative_data(dataframe, dataset):
  if dataset == 'sdss':
    return _filter_negative_data(dataframe, s='err_')
  else:
    return _filter_negative_data(dataframe, p='Err')

def _filter_negative_data(dataframe, s='', p=''):
  orig = dataframe.shape[0]
  dataframe = dataframe[dataframe.u > 0]
  dataframe = dataframe[dataframe.g > 0]
  dataframe = dataframe[dataframe.r > 0]
  dataframe = dataframe[dataframe.i > 0]
  dataframe = dataframe[dataframe.z > 0]

  dataframe = dataframe[dataframe[f"{s}u{p}"] > 0]
  dataframe = dataframe[dataframe[f"{s}g{p}"] > 0]
  dataframe = dataframe[dataframe[f"{s}r{p}"] > 0]
  dataframe = dataframe[dataframe[f"{s}i{p}"] > 0]
  dataframe = dataframe[dataframe[f"{s}z{p}"] > 0]

  clean = dataframe.shape[0]
  print(f"Negative data removed: {orig-clean}.")

  return dataframe

def build_dataset(dataframe, num_features, scaler, include_id=False):
  x = dataframe.iloc[:, :-1]
  y = dataframe[['redshift']]

  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
  x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.2, random_state=42)

  if include_id:
    x_train = x_train.iloc[:, :-1]
    x_val = x_val.iloc[:, :-1]
    x_test_ids = x_test.iloc[:, -1]

  chunks = 2
  if num_features > 10:
    chunks = num_features / 5

  if 5 < num_features < 16:
    x_train_ugriz, x_train_errs, x_train_experrs, _ = ugriz_errs_split(x_train, chunks)
    x_val_ugriz, x_val_errs, x_val_experrs, _ = ugriz_errs_split(x_val, chunks)
    x_test_ugriz, x_test_errs, x_test_experrs, _ = ugriz_errs_split(x_test, chunks)

    if scaler != None:
      mapper = DataFrameMapper([(x_train_ugriz.columns, scaler)])
      x_train_ugriz_s = mapper.fit_transform(x_train_ugriz)
      x_train_ugriz = pd.DataFrame(x_train_ugriz_s, index=x_train_ugriz.index, columns=x_train_ugriz.columns)

      x_val_ugriz_s = mapper.transform(x_val_ugriz)
      x_val_ugriz = pd.DataFrame(x_val_ugriz_s, index=x_val_ugriz.index, columns=x_val_ugriz.columns)

      x_test_ugriz_s = mapper.transform(x_test_ugriz)
      x_test_ugriz = pd.DataFrame(x_test_ugriz_s, index=x_test_ugriz.index, columns=x_test_ugriz.columns)

      if chunks == 2:
        x_train = pd.concat([x_train_ugriz, x_train_errs], axis=1)
        x_val = pd.concat([x_val_ugriz, x_val_errs], axis=1)
        x_test = pd.concat([x_test_ugriz, x_test_errs], axis=1)
      else:
        x_train = pd.concat([x_train_ugriz, x_train_errs, x_train_experrs], axis=1)
        x_val = pd.concat([x_val_ugriz, x_val_errs, x_val_experrs], axis=1)
        x_test = pd.concat([x_test_ugriz, x_test_errs, x_test_experrs], axis=1)

  elif num_features > 15:
    x_train_ugriz, x_train_errs, x_train_experrs, x_train_expmags = ugriz_errs_split(x_train, chunks)
    x_val_ugriz, x_val_errs, x_val_experrs, x_val_expmags = ugriz_errs_split(x_val, chunks)
    x_test_ugriz, x_test_errs, x_test_experrs, x_test_expmags = ugriz_errs_split(x_test, chunks)

    if scaler != None:
      mapper = DataFrameMapper([(x_train_ugriz.columns, scaler)])
      x_train_ugriz_s = mapper.fit_transform(x_train_ugriz)
      x_train_ugriz = pd.DataFrame(x_train_ugriz_s, index=x_train_ugriz.index, columns=x_train_ugriz.columns)

      x_val_ugriz_s = mapper.transform(x_val_ugriz)
      x_val_ugriz = pd.DataFrame(x_val_ugriz_s, index=x_val_ugriz.index, columns=x_val_ugriz.columns)

      x_test_ugriz_s = mapper.transform(x_test_ugriz)
      x_test_ugriz = pd.DataFrame(x_test_ugriz_s, index=x_test_ugriz.index, columns=x_test_ugriz.columns)

      x_train = pd.concat([x_train_ugriz, x_train_errs, x_train_experrs, x_train_expmags], axis=1)
      x_val = pd.concat([x_val_ugriz, x_val_errs, x_val_experrs, x_val_expmags], axis=1)
      x_test = pd.concat([x_test_ugriz, x_test_errs, x_test_experrs, x_test_expmags], axis=1)

  else:
    if scaler != None:
      mapper = DataFrameMapper([(x_train.columns, scaler)])
      x_train_s = mapper.fit_transform(x_train)
      x_train = pd.DataFrame(x_train_s, index=x_train.index, columns=x_train.columns)

      x_val_s = mapper.transform(x_val)
      x_val = pd.DataFrame(x_val_s, index=x_val.index, columns=x_val.columns)

      x_test_s = mapper.transform(x_test)
      x_test = pd.DataFrame(x_test_s, index=x_test.index, columns=x_test.columns)

  if include_id:
    x_test = pd.concat([x_test, x_test_ids], axis=1)

  return x_train, y_train, x_test, y_test, x_val, y_val, scaler

def build_dataset_coin_data(df_train, df_val, num_features, scaler, transform_test):
  all_train_data = df_train.to_numpy()
  x = all_train_data[:,0:(num_features)]
  y = all_train_data[:,-1]

  all_val_data = df_val.to_numpy()

  x_test = all_val_data[:, :-1]
  y_test = all_val_data[:, -1]

  x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=42)
  x_test_experrs = None

  chunks = 2
  if num_features > 10:
    chunks = num_features / 5

  if 5 < num_features < 16:
    x_train_ugriz, x_train_errs, x_train_experrs, _ = ugriz_errs_split(x_train, chunks)
    x_val_ugriz, x_val_errs, x_val_experrs, _ = ugriz_errs_split(x_val, chunks)
    if transform_test:
      x_test_ugriz, x_test_errs, x_test_experrs, _ = ugriz_errs_split(x_test, chunks)
    else:
      x_test_ugriz, x_test_errs, _, _ = ugriz_errs_split(x_test, 2)

    if scaler != None:
      x_train_ugriz = scaler.fit_transform(x_train_ugriz)
      x_val_ugriz = scaler.transform(x_val_ugriz)
      x_test_ugriz = scaler.transform(x_test_ugriz)

      if chunks == 2:
        x_train = np.hstack((x_train_ugriz, x_train_errs))
        x_val = np.hstack((x_val_ugriz, x_val_errs))
        x_test = np.hstack((x_test_ugriz, x_test_errs))
      else:
        x_train = np.hstack((x_train_ugriz, x_train_errs, x_train_experrs))
        x_val = np.hstack((x_val_ugriz, x_val_errs, x_val_experrs))
        if transform_test:
          x_test = np.hstack((x_test_ugriz, x_test_errs, x_test_experrs))
        else:
          x_test = np.hstack((x_test_ugriz, x_test_errs))

  elif num_features > 15:
    x_train_ugriz, x_train_errs, x_train_experrs, x_train_expmags = ugriz_errs_split(x_train, chunks)
    x_val_ugriz, x_val_errs, x_val_experrs, x_val_expmags = ugriz_errs_split(x_val, chunks)
    x_test_ugriz, x_test_errs, _, _ = ugriz_errs_split(x_test, 2)

    if scaler != None:
      x_train_ugriz = scaler.fit_transform(x_train_ugriz)
      x_val_ugriz = scaler.transform(x_val_ugriz)
      x_test_ugriz = scaler.transform(x_test_ugriz)

      x_train = np.hstack((x_train_ugriz, x_train_errs, x_train_experrs, x_train_expmags))
      x_val = np.hstack((x_val_ugriz, x_val_errs, x_val_experrs, x_val_expmags))
      x_test = np.hstack((x_test_ugriz, x_test_errs))

  else:
    if scaler != None:
      x_train = scaler.fit_transform(x_train)
      x_val = scaler.transform(x_val)
      x_test = scaler.transform(x_test)

  return x_train, y_train, x_test, y_test, x_val, y_val, scaler

def load_dataframe(dataset_name, coin_val):
  pd.set_option("max_columns", None)
  if dataset_name == 'teddy' or dataset_name == 'happy':
    if coin_val:
      train = pd.read_csv(f"{dataset_name}_train_data", comment='#', delim_whitespace=True, names=['ID', 'u', 'g', 'r', 'i', 'z', 'uErr', 'gErr', 'rErr', 'iErr', 'zErr', 'redshift', 'redshiftErr'])
      test  = pd.read_csv(f"{dataset_name}_val_data_{_val_idx[coin_val]}", comment='#', delim_whitespace=True, names=['ID', 'u', 'g', 'r', 'i', 'z', 'uErr', 'gErr', 'rErr', 'iErr', 'zErr', 'redshift', 'redshiftErr'])
      return train, test
    else:
      data = pd.read_csv(f"{dataset_name}_train_data", comment='#', delim_whitespace=True, names=['ID','u','g','r','i','z','uErr','gErr','rErr','iErr','zErr','redshift','redshiftErr'])
      return data, None

  if dataset_name == 'kaggle' or dataset_name == 'kaggle_bkp':
    return pd.read_csv('kaggle_train_data.csv'), None

  if dataset_name == 'sdss':
    return pd.read_csv('sdss_train_data.csv', comment="#"), None

  return None, None

def download_data(dataset_name, coin_val):
  if dataset_name == 'teddy':
    if coin_val:
      download_teddy(data_chunk=coin_val)
    else:
      download_teddy()

  if dataset_name == 'happy':
    if coin_val:
      download_happy(data_chunk=coin_val)
    else:
      download_happy()

  if dataset_name == 'kaggle':
    if os.path.isfile('kaggle_train_data.csv'):
      print("Dataset Found!")
    else:
      download_kaggle()

  if dataset_name == 'kaggle_bkp':
    if os.path.isfile('kaggle_train_data.csv'):
      print("Dataset Found!")
    else:
      download_kaggle_alternative()

  if dataset_name == 'sdss':
    if os.path.isfile('sdss_train_data.csv'):
      print("Dataset Found!")
    else:
      download_sdss()

def download_teddy(data_chunk='A'):
  data_file = 'teddy_train_data'
  if data_chunk != 'A':
    data_file = f"teddy_val_data_{_val_idx[data_chunk]}"

  if os.path.isfile(data_file):
    print("Dataset Found!")
  else:
    os.system(f"wget https://raw.githubusercontent.com/COINtoolbox/photoz_catalogues/master/Teddy/forTemplateBased/teddyT_{data_chunk}.cat")
    if data_chunk == 'A':
      os.system(f"mv teddyT_{data_chunk}.cat teddy_train_data")
    else:
      os.system(f"mv teddyT_{data_chunk}.cat teddy_val_data_{_val_idx[data_chunk]}")

def download_happy(data_chunk='A'):
  data_file = 'happy_train_data'
  if data_chunk != 'A':
    data_file = f"happy_val_data_{_val_idx[data_chunk]}"

  if os.path.isfile(data_file):
    print("Dataset Found!")
  else:
    os.system(f"wget https://raw.githubusercontent.com/COINtoolbox/photoz_catalogues/master/Happy/forTemplateBased/happyT_{data_chunk}")
    if data_chunk == 'A':
      os.system(f"mv happyT_{data_chunk} happy_train_data")
    else:
      os.system(f"mv happyT_{data_chunk} happy_val_data_{_val_idx[data_chunk]}")

def download_kaggle():
  os.system('wget -O archieve.zip "https://storage.googleapis.com/kaggle-competitions-data/kaggle-v2/3617/105309/bundle/archive.zip?GoogleAccessId=web-data@kaggle-161607.iam.gserviceaccount.com&Expires=1589667788&Signature=PqUjFhjNMhS9DEBnk3Yxzo%2BXnKS9c3v1XbmW%2F2dVcromNwDnELmnVSAtGD%2BFi%2FiNay801TWg84P0N6qCq8D5P%2BcOj92zcBB0%2BjPX8yIKvvwud3mA%2FCW%2FLAXTCbgGHrNKUUSIAMhkwro41Nzi%2F4lP0pIpCugQr44bWnCmh57zmt5CNJDb63ZS7CiZyZGsNg6YebXTAzR3yuNbdtMd3pvmQJwv%2FPedtyoR4PhloelwqE3fxVx2pt9AdZzE%2FU%2BRVyoFCnj1EiQBq8oDES2TEoJde9rTCIMWM6OtFPa%2Bq3e%2BqDC9lzgkywTPJ4hjmRA4VmFgPWb57uOX7T2QZuHnnIG87g%3D%3D&response-content-disposition=attachment%3B+filename%3Dphotometric-redshift-estimation-2013.zip"')
  os.system('unzip -u archieve.zip')
  os.system('unzip -u train.zip')
  os.system('mv train.csv kaggle_train_data.csv')

def download_sdss_alternative():
  id = '1i5fFrVlVkfRvFMBYzYbE_whLfSTz5fYe'
  url = f"https://drive.google.com/uc?id={id}"
  output = 'sdss_train_data.csv'
  gdown.download(url, output, quiet=False)

def download_sdss():
  url = f"https://zenodo.org/record/4752020/files/sdss_train_data.csv?download=1"
  output = 'sdss_train_data.csv'
  gdown.download(url, output, quiet=False)

def download_kaggle_alternative():
  def download_file_from_google_drive(id):
    URL = "https://docs.google.com/uc?export=download"

    session = requests.Session()

    response = session.get(URL, params={'id': id}, stream=True)
    token = get_confirm_token(response)

    if token:
      params = {'id': id, 'confirm': token}
      response = session.get(URL, params=params, stream=True)

    save_response_content(response)

  def get_confirm_token(response):
    for key, value in response.cookies.items():
      if key.startswith('download_warning'):
        return value

    return None

  def save_response_content(response):
    CHUNK_SIZE = 32768

    with open("kaggle_train_data.csv", "wb") as f:
      for chunk in response.iter_content(CHUNK_SIZE):
        if chunk:  # filter out keep-alive new chunks
          f.write(chunk)

  download_file_from_google_drive('1xCQzmusNSt65zQnjsrnddLhuCB5WdYcl')

def dump_test_set(x_test, y_test):
  df_test = pd.concat([x_test, y_test], axis=1).reindex(x_test.index)
  df_test.to_csv('test_dataset.csv')

def load_test_dataframe(dataset):
  return pd.read_csv(dataset)

def empty():
  return pd.DataFrame()


# coding: utf-8

'''
Created on 26 марта 2016 г.

@author: keen
'''

import numpy

class DecisionStamp:
    def __init__(self,model,features_weight,class_max,p0,p1):
        self.model = model
        self.features_weight = features_weight
        self.class_max = class_max
        self.p0 = p0
        self.p1 = p1

    def stamp_sign(self,x,sample = True):
        if sample:
            x = x[:,self.features_weight]
        return numpy.sign(self.model.predict(x))

    def predict_proba(self,x,sample = True):
        res = numpy.zeros((x.shape[0],self.class_max + 1))
        sgns = self.stamp_sign(x,sample)
        res[sgns < 0] = self.p0
        res[sgns >=0] = self.p1

        return res


__author__ = '''\n'''.join(['Diederik van Liere (dvanliere@gmail.com)', ])
__author__email = 'dvanliere at gmail dot com'
__date__ = '2010-10-21'
__version__ = '0.1'

'''
This file provides mapper between language name and locale language name and
Wikipedia acronym.
Gothic and Birmese are not yet supported, see rows 450 and 554.
'''

LANGUAGES = {
'English':'en',
'German':'de',
'French':'fr',
'Italian':'it',
'Polish':'pl',
'Japanese':'ja',
'Spanish':'es',
'Dutch':'nl',
'Portuguese':'pt',
'Russian':'ru',
'Swedish':'sv',
'Chinese':'zh',
'Catalan':'ca',
'Norwegian':'no',
'Bokmål':'no',
'Finnish':'fi',
'Ukrainian':'uk',
'Hungarian':'hu',
'Czech':'cs',
'Romanian':'ro',
'Turkish':'tr',
'Korean':'ko',
'Vietnamese':'vi',
'Danish':'da',
'Arabic':'ar',
'Esperanto':'eo',
'Serbian':'sr',
'Indonesian':'id',
'Lithuanian':'lt',
'Volapük':'vo',
'Slovak':'sk',
'Hebrew':'he',
'Bulgarian':'bg',
'Persian':'fa',
'Slovenian':'sl',
'Waray-Waray':'war',
'Croatian':'hr',
'Estonian':'et',
'Malay':'ms',
'Newar':'new',
'Nepal Bhasa':'new',
'Simple English':'simple',
'Galician':'gl',
'Thai':'th',
'Aromanian':'roa-rup',
'Nynorsk':'nn',
'Basque':'eu',
'Hindi':'hi',
'Greek':'el',
'Haitian':'ht',
'Latin':'la',
'Telugu':'te',
'Georgian':'ka',
'Cebuano':'ceb',
'Macedonian':'mk',
'Azeri':'az',
'Tagalog':'tl',
'Breton':'br',
'Serbo-Croatian':'sh',
'Marathi':'mr',
'Luxembourgish':'lb',
'Javanese':'jv',
'Latvian':'lv',
'Bosnian':'bs',
'Icelandic':'is',
'Welsh':'cy',
'Belarusian':'be-x-old',
'Taraškievica':'be-x-old',
'Piedmontese':'pms',
'Albanian':'sq',
'Tamil':'ta',
'Bishnupriya Manipuri':'bpy',
'Belarusian':'be',
'Aragonese':'an',
'Occitan':'oc',
'Bengali':'bn',
'Swahili':'sw',
'Ido':'io',
'Ripuarian':'ksh',
'Lombard':'lmo',
'West Frisian':'fy',
'Gujarati':'gu',
'Low Saxon':'nds',
'Afrikaans':'af',
'Sicilian':'scn',
'Quechua':'qu',
'Kurdish':'ku',
'Urdu':'ur',
'Sundanese':'su',
'Malayalam':'ml',
'Cantonese':'zh-yue',
'Asturian':'ast',
'Neapolitan':'nap',
'Samogitian':'bat-smg',
'Walloon':'wa',
'Chuvash':'cv',
'Irish':'ga',
'Armenian':'hy',
'Yoruba':'yo',
'Kannada':'kn',
'Tajik':'tg',
'Tarantino':'roa-tara',
'Venetian':'vec',
'Western Panjabi':'pnb',
'Nepali':'ne',
'Scottish Gaelic':'gd',
'Yiddish':'yi',
'Min Nan':'zh-min-nan',
'Uzbek':'uz',
'Tatar':'tt',
'Kapampangan':'pam',
'Ossetian':'os',
'Sakha':'sah',
'Alemannic':'als',
'Maori':'mi',
'Egyptian Arabic':'arz',
'Kazakh':'kk',
'Nahuatl':'nah',
'Limburgian':'li',
'Upper Sorbian':'hsb',
'Gilaki':'glk',
'Corsican':'co',
'Gan':'gan',
'Amharic':'am',
'Mongolian':'mn',
'Interlingua':'ia',
'Central Bicolano':'bcl',
'Võro':'fiu-vro',
'Dutch Low Saxon':'nds-nl',
'Faroese':'fo',
'Turkmen':'tk',
'Scots':'sco',
'West Flemish':'vls',
'Sinhalese':'si',
'Sanskrit':'sa',
'Bavarian':'bar',
'Burmese':'my',
'Manx':'gv',
'Divehi':'dv',
'Norman':'nrm',
'Pangasinan':'pag',
'Romansh':'rm',
'Banyumasan':'map-bms',
'Zazaki':'diq',
'Sorani':'ckb',
'Northern Sami':'se',
'Mazandarani':'mzn',
'Wu':'wuu',
'Uyghur':'ug',
'Friulian':'fur',
'Ligurian':'lij',
'Maltese':'mt',
'Bihari':'bh',
'Novial':'nov',
'Malagasy':'mg',
'Kashubian':'csb',
'Ilokano':'ilo',
'Sardinian':'sc',
'Classical Chinese':'zh-classical',
'Khmer':'km',
'Ladino':'lad',
'Pali':'pi',
'Anglo-Saxon':'ang',
'Zamboanga Chavacano':'cbk-zam',
'Tibetan':'bo',
'Fiji Hindi':'hif',
'Franco-Provençal':'frp',
'Arpitan':'frp',
'Hakka':'hak',
'Cornish':'kw',
'Punjabi':'pa',
'Pashto':'ps',
'Kalmyk':'xal',
'Silesian':'szl',
'Pennsylvania German':'pdc',
'Hawaiian':'haw',
'Saterland Frisian':'stq',
'Interlingue':'ie',
'Navajo':'nv',
'Fijian':'fj',
'Crimean Tatar':'crh',
'Komi':'kv',
'Tongan':'to',
'Acehnese':'ace',
'Somali':'so',
'Erzya':'myv',
'Guarani':'gn',
'Karachay-Balkar':'krc',
'Extremaduran':'ext',
'Lingala':'ln',
'Kirghiz':'ky',
'Meadow Mari':'mhr',
'Assyrian Neo-Aramaic':'arc',
'Emilian-Romagnol':'eml',
'Lojban':'jbo',
'Picard':'pcd',
'Aymara':'ay',
'Wolof':'wo',
'Tumbuka':'tum',
'Kabyle':'kab',
'Bashkir':'ba',
'North Frisian':'frr',
'Tahitian':'ty',
'Tok Pisin':'tpi',
'Papiamentu':'pap',
'Zealandic':'zea',
'Sranan':'srn',
'Greenlandic':'kl',
'Udmurt':'udm',
'Chechen':'ce',
'Igbo':'ig',
'Komi-Permyak':'koi',
'Oriya':'or',
'Lower Sorbian':'dsb',
'Kongo':'kg',
'Lao':'lo',
'Abkhazian':'ab',
'Moksha':'mdf',
'Romani':'rmy',
'Hill Mari':'mrj',
'Banjar':'bjn',
'Old Church Slavonic':'cu',
'Mirandese':'mwl',
'Karakalpak':'kaa',
'Samoan':'sm',
'Moldovan':'mo',
'Tetum':'tet',
'Avar':'av',
'Kashmiri':'ks',
'Gothic':'got',
'Sindhi':'sd',
'Bambara':'bm',
'Nauruan':'na',
'Norfolk':'pih',
'Pontic':'pnt',
'Inuktitut':'iu',
'Inupiak':'ik',
'Bislama':'bi',
'Cherokee':'chr',
'Assamese':'as',
'Min Dong':'cdo',
'Ewe':'ee',
'Swati':'ss',
'Oromo':'om',
'Zhuang':'za',
'Zulu':'zu',
'Tigrinya':'ti',
'Venda':'ve',
'Tsonga':'ts',
'Hausa':'ha',
'Dzongkha':'dz',
'Sango':'sg',
'Chamorro':'ch',
'Cree':'cr',
'Xhosa':'xh',
'Akan':'ak',
'Sesotho':'st',
'Kinyarwanda':'rw',
'Tswana':'tn',
'Kikuyu':'ki',
'Buryat':'bxr',
'Buginese':'bug',
'Chichewa':'ny',
'Lak':'lbe',
'Twi':'tw',
'Shona':'sn',
'Kirundi':'rn',
'Fula':'ff',
'Cheyenne':'chy',
'Luganda':'lg',
'Ndonga':'ng',
'Sichuan Yi':'ii',
'Choctaw':'cho',
'Marshallese':'mh',
'Afar':'aa',
'Kuanyama':'kj',
'Hiri Motu':'ho',
'Muscogee':'mus',
'Kanuri':'kr',
'Herero':'hz',
'English':'en',
'Deutsch':'de',
'Français':'fr',
'Italiano':'it',
'Polski':'pl',
'日本語':'ja',
'Español':'es',
'Nederlands':'nl',
'Português':'pt',
'Русский':'ru',
'Svenska':'sv',
'中文':'zh',
'Català':'ca',
'Norsk':'no',
'Bokmål':'no',
'Suomi':'fi',
'Українська':'uk',
'Magyar':'hu',
'Čeština':'cs',
'Română':'ro',
'Türkçe':'tr',
'한국어':'ko',
'Tiếng Việt':'vi',
'Dansk':'da',
'العربية':'ar',
'Esperanto':'eo',
'Српски':'sr',
'Srpski':'sr',
'Bahasa Indonesia':'id',
'Lietuvių':'lt',
'Volapük':'vo',
'Slovenčina':'sk',
'עברית':'he',
'Български':'bg',
'فارسی':'fa',
'Slovenščina':'sl',
'Winaray':'war',
'Hrvatski':'hr',
'Eesti':'et',
'Bahasa Melayu':'ms',
'नेपाल भाषा':'new',
'Simple English':'simple',
'Galego':'gl',
'ไทย':'th',
'Armãneashce':'roa-rup',
'Nynorsk':'nn',
'Euskara':'eu',
'हिन्दी':'hi',
'Ελληνικά':'el',
'Krèyol ayisyen':'ht',
'Latina':'la',
'తెలుగు':'te',
'ქართული':'ka',
'Sinugboanong Binisaya':'ceb',
'Македонски':'mk',
'Azərbaycan':'az',
'Tagalog':'tl',
'Brezhoneg':'br',
'Srpskohrvatski':'sh',
'Српскохрватски':'sh',
'मराठी':'mr',
'Lëtzebuergesch':'lb',
'Basa Jawa':'jv',
'Latviešu':'lv',
'Bosanski':'bs',
'Íslenska':'is',
'Cymraeg':'cy',
'Беларуская':'be-x-old',
'тарашкевіца':'be-x-old',
'Piemontèis':'pms',
'Shqip':'sq',
'தமிழ்':'ta',
'ইমার ঠার':'bpy',
'বিষ্ণুপ্রিয়া মণিপুরী':'bpy',
'Беларуская':'be',
'Aragonés':'an',
'Occitan':'oc',
'বাংলা':'bn',
'Kiswahili':'sw',
'Ido':'io',
'Ripoarisch':'ksh',
'Lumbaart':'lmo',
'Frysk':'fy',
'ગુજરાતી':'gu',
'Plattdüütsch':'nds',
'Afrikaans':'af',
'Sicilianu':'scn',
'Runa Simi':'qu',
'Kurdî':'ku',
'كوردی':'ku',
'اردو':'ur',
'Basa Sunda':'su',
'മലയാളം':'ml',
'粵語':'zh-yue',
'Asturianu':'ast',
'Nnapulitano':'nap',
'Žemaitėška':'bat-smg',
'Walon':'wa',
'Чăваш':'cv',
'Gaeilge':'ga',
'Հայերեն':'hy',
'Yorùbá':'yo',
'ಕನ್ನಡ':'kn',
'Тоҷикӣ':'tg',
'Tarandíne':'roa-tara',
'Vèneto':'vec',
'شاہ مکھی پنجابی':'pnb',
'Shāhmukhī Pañjābī':'pnb',
'नेपाली':'ne',
'Gàidhlig':'gd',
'ייִדיש':'yi',
'Bân-lâm-gú':'zh-min-nan',
'O‘zbek':'uz',
'Tatarça':'tt',
'Татарча':'tt',
'Kapampangan':'pam',
'Иронау':'os',
'Саха тыла':'sah',
'Saxa Tyla':'sah',
'Alemannisch':'als',
'Māori':'mi',
'مصرى':'arz',
'Maṣrī':'arz',
'Қазақша':'kk',
'Nāhuatl':'nah',
'Limburgs':'li',
'Hornjoserbsce':'hsb',
'گیلکی':'glk',
'Corsu':'co',
'贛語':'gan',
'አማርኛ':'am',
'Монгол':'mn',
'Interlingua':'ia',
'Bikol':'bcl',
'Võro':'fiu-vro',
'Nedersaksisch':'nds-nl',
'Føroyskt':'fo',
'تركمن ':'tk',
'Туркмен':'tk',
'Scots':'sco',
'West-Vlams':'vls',
'සිංහල':'si',
'संस्कृतम्':'sa',
'Boarisch':'bar',
'မ္ရန္‌မာစာ':'my', #Needs fix
'Gaelg':'gv',
'ދިވެހިބަސް':'dv',
'Nouormand':'nrm',
'Normaund':'nrm',
'Pangasinan':'pag',
'Rumantsch':'rm',
'Basa Banyumasan':'map-bms',
'Zazaki':'diq',
'Soranî':'ckb',
'کوردی':'ckb',
'Sámegiella':'se',
'مَزِروني':'mzn',
'吴语':'wuu',
'Oyghurque':'ug',
'Furlan':'fur',
'Líguru':'lij',
'Malti':'mt',
'भोजपुरी':'bh',
'Novial':'nov',
'Malagasy':'mg',
'Kaszëbsczi':'csb',
'Ilokano':'ilo',
'Sardu':'sc',
'古文':'zh-classical',
'文言文':'zh-classical',
'ភាសាខ្មែរ':'km',
'Dzhudezmo':'lad',
'पाऴि':'pi',
'Englisc':'ang',
'Chavacano de Zamboanga':'cbk-zam',
'བོད་སྐད':'bo',
'Fiji Hindi':'hif',
'Arpitan':'frp',
'Hak-kâ-fa':'hak',
'客家話':'hak',
'Kernewek':'kw',
'Karnuack':'kw',
'ਪੰਜਾਬੀ':'pa',
'پښتو':'ps',
'Хальмг':'xal',
'Ślůnski':'szl',
'Deitsch':'pdc',
'Hawai`i':'haw',
'Seeltersk':'stq',
'Interlingue':'ie',
'Diné bizaad':'nv',
'Na Vosa Vakaviti':'fj',
'Qırımtatarca':'crh',
'Коми':'kv',
'faka Tonga':'to',
'Bahsa Acèh':'ace',
'Soomaaliga':'so',
'Эрзянь':'myv',
'Erzjanj Kelj':'myv',
"Avañe'ẽ":'gn',
'Къарачай-Малкъар':'krc',
'Qarachay-Malqar':'krc',
'Estremeñu':'ext',
'Lingala':'ln',
'Кыргызча':'ky',
'Олык Марий':'mhr',
'Olyk Marij':'mhr',
'ܐܪܡܝܐ':'arc',
'Emiliàn e rumagnòl':'eml',
'Lojban':'jbo',
'Picard':'pcd',
'Aymar':'ay',
'Wolof':'wo',
'chiTumbuka':'tum',
'Taqbaylit':'kab',
'Башҡорт':'ba',
'Frasch':'frr',
'Reo Mā`ohi':'ty',
'Tok Pisin':'tpi',
'Papiamentu':'pap',
'Zeêuws':'zea',
'Sranantongo':'srn',
'Kalaallisut':'kl',
'Удмурт кыл':'udm',
'Нохчийн':'ce',
'Igbo':'ig',
'Перем Коми':'koi',
'Perem Komi':'koi',
'ଓଡ଼ିଆ':'or',
'Dolnoserbski':'dsb',
'KiKongo':'kg',
'ລາວ':'lo',
'Аҧсуа':'ab',
'Мокшень':'mdf',
'Mokshanj Kälj':'mdf',
'romani - रोमानी':'rmy',
'Кырык Мары':'mrj',
'Kyryk Mary':'mrj',
'Bahasa Banjar':'bjn',
'Словѣньскъ':'cu',
'Páigina Percipal':'mwl',
'Qaraqalpaqsha':'kaa',
'Gagana Samoa':'sm',
'Молдовеняскэ':'mo',
'Tetun':'tet',
'Авар':'av',
'कश्मीरी':'ks',
'كشميري':'ks',
'𐌲𐌿𐍄𐌹𐍃𐌺':'got',   #Needs fix
'سنڌي، سندھی ، सिन्ध':'sd',
'Bamanankan':'bm',
'dorerin Naoero':'na',
'Norfuk':'pih',
'Ποντιακά':'pnt',
'ᐃᓄᒃᑎᑐᑦ':'iu',
'Iñupiak':'ik',
'Bislama':'bi',
'ᏣᎳᎩ':'chr',
'অসমীয়া':'as',
'Mìng-dĕ̤ng-ngṳ̄':'cdo',
'Eʋegbe':'ee',
'SiSwati':'ss',
'Oromoo':'om',
'Cuengh':'za',
'isiZulu':'zu',
'ትግርኛ':'ti',
'Tshivenda':'ve',
'Xitsonga':'ts',
'هَوُسَ':'ha',
'ཇོང་ཁ':'dz',
'Sängö':'sg',
'Chamoru':'ch',
'Nehiyaw':'cr',
'isiXhosa':'xh',
'Akana':'ak',
'Sesotho':'st',
'Ikinyarwanda':'rw',
'Setswana':'tn',
'Gĩkũyũ':'ki',
'Буряад':'bxr',
'Basa Ugi':'bug',
'Chi-Chewa':'ny',
'Лакку':'lbe',
'Twi':'tw',
'chiShona':'sn',
'Kirundi':'rn',
'Fulfulde':'ff',
'Tsetsêhestâhese':'chy',
'Luganda':'lg',
'Oshiwambo':'ng',
'ꆇꉙ':'ii',
'Choctaw':'cho',
'Ebon':'mh',
'Afar':'aa',
'Kuanyama':'kj',
'Hiri Motu':'ho',
'Muskogee':'mus',
'Kanuri':'kr',
'Otsiherero':'hz',
}


from nilearn.input_data import NiftiLabelsMasker, NiftiMasker
from nilearn import image
import os
from nilearn import datasets
from nilearn.connectome import ConnectivityMeasure
from nilearn import plotting
import numpy as np
from graph import *
from numpy.linalg import eig
from nilearn.input_data import NiftiMapsMasker

def matrice_degré(G):
    """permet de calculer la matrice de degrés à partir du graph G"""
    matrice_d=np.zeros(np.shape(G))

    for i in range(len(G)):
        for j in range(len(G[0])) :
            if i!=j:

                matrice_d[i,i]+=G[i,j]

    return(matrice_d)

def matrice_binaire(matrice,threshold):
    """conversion de matrice en matrice binaire selon le threshold choisi"""
    for i in range(len(matrice)):
        for j in range(len(matrice[0])):
            if matrice[i][j]>threshold:
                matrice[i][j]=1
            else:
                matrice[i][j]=0

    return matrice

### Changement de répertoire de travail+téléchangement des données IRMf pour rest et active

os.chdir("E:/COURS-IMT/CODEV_Recherche/Données_Analysées/fmriprep/sub-02CB/func")
data_rest="sub-02CB_task-rest_run-1_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz"
#data_active="??????"

###Telechangement de l'atlas

path_Glasser="E:/COURS-IMT/CODEV_Recherche/Ressources/glasser/Glasser_masker.nii.gz"
glasser_atlas=image.load_img(path_Glasser)

###Création des connectomes et des masker pour les états rest et active

connectome_measure = ConnectivityMeasure(kind='correlation')
glassermasker = NiftiLabelsMasker(labels_img=path_Glasser)
glassermasker.fit()
#connectome_measure_active = ConnectivityMeasure(kind='correlation')
connectome_measure_rest = ConnectivityMeasure(kind='correlation')

###Extraction des times series

time_series_rest = []
#time_series_active = []
time_series_rest.append(glassermasker.fit_transform(data_rest))
#time_series_active.append(glassermasker.fit_transform(data_active))

### calcul de la matrice de corrélation+ Moyenne

correlation_matrices_rest = connectome_measure_rest.fit_transform(time_series_rest)
correlation_matrices_rest = connectome_measure_rest.mean_
# correlation_matrices_active = connectome_measure_active.fit_transform(time_series_active)
# correlation_matrices_active = connectome_measure_active.mean_
np.fill_diagonal(correlation_matrices_rest, 0)
#np.fill_diagonal(correlation_matrices_active, 0)

###Conversion en matrice binaire

correlation_matrices_rest=matrice_binaire(correlation_matrices_rest,0.25)
#correlation_matrices_active=matrice_binaire(correlation_matrices_active,0.3)

###Afficher les connectomes

coordinates = plotting.find_parcellation_cut_coords(labels_img=path_Glasser)
plotting.plot_connectome(correlation_matrices_rest, coordinates,
                         edge_threshold="80%")

plotting.show()
### afficher la matrice de correlation

plotting.plot_matrix(correlation_matrices_rest,vmax=0.8, vmin=-0.8)
#plotting.plot_matrix(correlation_matrices_active, vmax=0.8, vmin=-0.8)

plotting.show()

###Calcul de la matrice Laplacienne et affichage des valeurs propres
matrice_deg=matrice_degré(correlation_matrices_rest)
L=matrice_deg-correlation_matrices_rest
D,V=eig(L)

print(sorted(D))


import pandas as pd
import networkx as nx
import plotly.express as px

def cooccurrence_edgelist(df, on):
    entity = 'faculty' if 'student' in on else 'student'
    edgelist = pd.merge(df, df, on=on, how='inner')
    edgelist = edgelist[edgelist[entity + '_x'] != edgelist[entity + '_y']]
    edgelist['pair'] = edgelist[[entity + '_x', entity + '_y']].values.tolist()
    edgelist['pair'] = edgelist['pair'].apply(lambda x: sorted(x))
    edgelist[['source', 'target']] = pd.DataFrame(edgelist.pair.tolist(), index=edgelist.index)
    edgelist['relationship'] = edgelist.apply(
        lambda x: 'Co-Advised' if (x['edge_type_x'] == 'Advisor') and (x.edge_type_y == 'Advisor') else 'Co-Committee',
        axis=1)
    edgelist = edgelist.groupby(['source', 'target', 'relationship']).size().reset_index(name='weight')
    return edgelist

color_scale = px.colors.qualitative.Light24

def get_color(i, n=len(color_scale)):
    return color_scale[i - 1] if i < n else 'rgb(1,1,1)'

def get_nx_graph(edge_file, node_file, create_using=nx.DiGraph):
    g = nx.from_pandas_edgelist(
        edge_file,
        'source',
        'target',
        edge_attr=['weight', 'relationship'],
        create_using=create_using
    )

    # print('')
    # print([edge for edge in g.edges if 'Manuela Veloso' in edge])

    node_attr = node_file.set_index('id').to_dict('index')
    nx.set_node_attributes(g, node_attr)
    return g

def set_node_degree(G, name='degree'):
    degree = G.degree
    for v in G:
        G.nodes[v][name] = degree[v]

def set_node_pagerank(G, name='pagerank', **kwargs):
    pr = nx.pagerank_scipy(G, **kwargs)
    for v in G:
        G.nodes[v][name] = pr[v]

def set_node_community(G, name='community'):
    # communities = sorted(nx.community.greedy_modularity_communities(G), key=len, reverse=True)
    # communities = sorted(next(nx.community.girvan_newman(G)), key=len, reverse=True)
    # communities = nx.community.label_propagation_communities(G)
    communities = nx.community.asyn_lpa_communities(G, weight='weight')
    # print(communities)
    communities = sorted(communities, key=len, reverse=True)
    # print(communities)

    # degrees = nx.get_node_attributes(G, 'degree')
    # degrees = G.degree
    '''Add community to node attributes'''
    i = 0
    for v_c in communities:
        valid_community = False
        if len(v_c) > 2:
            valid_community = True
            for v in v_c:
                # Add 1 to save 0 for external edges
                G.nodes[v][name] = i + 1
                G.nodes[v][name + '_color'] = get_color(i, min(len(communities), len(color_scale)))
        else:
            for v in v_c:
                G.nodes[v][name] = 0
                G.nodes[v][name + '_color'] = 'rgb(1,1,1)'
        if valid_community:
            i = i + 1

def set_edge_community(G, name='community'):
    '''Find internal edges and add their community to their attributes'''
    for v, w, n in G.edges:
        if G.nodes[v][name] == G.nodes[w][name]:
            # Internal edge, mark with community
            G.edges[v, w, n][name] = G.nodes[v][name]
            G.edges[v, w, n][name + '_color'] = G.nodes[v][name + '_color']
        else:
            # External edge, mark as 0
            G.edges[v, w, n][name] = 0
            G.edges[v, w, n][name + '_color'] = 'grey'

def add_zero_degree_nodes(g, node_df, name_prefix):
    node_attr = node_df.set_index('id').to_dict('index')
    for node in node_attr:
        if node not in g:
            g.add_node(node)
            attr = node_attr[node]
            attr[name_prefix + 'degree'] = 0
            attr[name_prefix + 'pagerank'] = 0
            attr[name_prefix + 'community'] = 0
            attr[name_prefix + 'community_color'] = 'rgb(1,1,1)'
            nx.set_node_attributes(g, {node: attr})

    return g

def create_node_edge_frame(G):
    node_attr = {node: G.nodes[node] for node in G.nodes}
    edge_attr = {edge: G.edges[edge[0], edge[1], edge[2]] for edge in G.edges}

    node_frame = pd.DataFrame.from_dict(node_attr, orient='index').rename_axis('id').reset_index()
    edge_frame = pd.DataFrame.from_dict(edge_attr, orient='index').rename_axis(['source', 'target', 'other']).reset_index()

    return node_frame, edge_frame

def get_node_edge_frame(node_df, edgelist_df, name_prefix=''):
    G = get_nx_graph(edgelist_df, node_df, create_using=nx.MultiGraph)

    # Set node and edge communities
    set_node_degree(G, name=name_prefix + 'degree')
    set_node_pagerank(G, name=name_prefix + 'pagerank')
    set_node_community(G, name=name_prefix + 'community')
    set_edge_community(G, name=name_prefix + 'community')

    G = add_zero_degree_nodes(G, node_df, name_prefix)

    # print(G.edges('Manuela Veloso'))

    # print(edgelist_df[edgelist_df['source'] == 'Manuela Veloso'])
    # Create node and edge_frame
    node_frame, edge_frame = create_node_edge_frame(G)
    node_frame['display'] = 'element'

    # print(edge_frame[edge_frame['source'] == 'Manuela Veloso'])

    return node_frame, edge_frame