import os
from typing import Any, Dict, List, Optional, Tuple, Union

from datasets import Dataset, disable_caching, enable_caching, load_dataset
from datasets.builder import DatasetGenerationError
from datasets.fingerprint import logger

from transformers_framework.utilities.format import partial_format
from transformers_framework.utilities.logging import rank_zero_error, rank_zero_info, rank_zero_warn

def load_dataset_from_huggingface(path: str, config: str = None, keep_in_memory: bool = False) -> Dataset:
    r""" Load dataset from HF repository.

    Raises:
        `FileNotFoundError` if the remote repository does not exist.
    """

    try:
        # `load_dataset` may raise FileNotFoundError if HF repo does not exist or `DatasetGenerationError` if
        # a path with the same name is present on disk
        path, split = os.path.dirname(path), os.path.basename(path)
        dataset = load_dataset(path, name=config, split=split, keep_in_memory=keep_in_memory)
    except DatasetGenerationError:
        raise FileNotFoundError

    return dataset

def load_dataset_from_local(path: str, keep_in_memory: bool = False) -> Dataset:
    r""" Load dataset from local repository. The dataset must have been saved with `Dataset.save_to_disk(...)`.

    Raises:
        `FileNotFoundError` if the remote repository does not exist.
    """

    dataset = Dataset.load_from_disk(path, keep_in_memory=keep_in_memory)
    return dataset

def load_dataset_from_s3(path: str, keep_in_memory: bool = False) -> Dataset:
    r""" Load dataset from S3 repository. The dataset must have been saved with `Dataset.save_to_disk(...)`.

    Raises:
        `FileNotFoundError` if the remote repository does not exist.
    """

    dataset = Dataset.load_from_disk(path, keep_in_memory=keep_in_memory)
    return dataset

def load_dataset_from_anywhere(
    path: str, config: str = None, shard: int = None, keep_in_memory: bool = False
) -> Tuple[Dataset, str]:
    r"""
    Loads a dataset from:
        - HF repository
        - disk dump saved with `Dataset.save_to_disk(...)`
        - remote S3 dump saved with `Dataset.save_to_disk(..., storage_options="s3")`
    """

    if path.startswith("s3://"):
        try:
            dataset = load_dataset_from_s3(path=path, keep_in_memory=keep_in_memory)
            location = "s3"
        except FileNotFoundError:
            rank_zero_error(f"'{path}' is not a valid S3 path")
            exit(1)
    else:
        try:
            dataset = load_dataset_from_local(path=path, keep_in_memory=keep_in_memory)
            location = "disk"
        except FileNotFoundError:
            try:
                dataset = load_dataset_from_huggingface(path=path, config=config, keep_in_memory=keep_in_memory)
                location = "huggingface"
            except FileNotFoundError:
                rank_zero_error(f"'{path}' is not a valid dataset path on disk or HF")
                exit(1)

    if config is not None and location != "huggingface":
        rank_zero_warn("You loaded a dataset from S3 or from disk and used config parameters, which is not required.")

    if shard is not None and shard > 1:
        dataset = dataset.shard(shard, 0, writer_batch_size=100000, contiguous=False)

    return dataset, location

def reduce(
    dataset,
    column: str,
    batch_size: Optional[int] = 1000,
    remove_columns: Optional[Union[str, List[str]]] = None,
    keep_in_memory: bool = False,
    load_from_cache_file: bool = None,
    cache_file_name: Optional[str] = None,
    writer_batch_size: Optional[int] = 1000,
    disable_nullable: bool = False,
    num_proc: Optional[int] = None,
    suffix_template: str = "_{rank:05d}_of_{num_proc:05d}_iter_{iter:05d}",
    desc: Optional[str] = None
) -> Dataset:
    r"""
    Reduce all the examples in the table (individually or in batches) given a pivot column and update the table.
    If your function returns a column that already exists, then it overwrites it.

    You can specify whether the function should be batched or not with the `batched` parameter:

    Args:
        column (`str`):
            Columns on which the dataset will be reduced. Examples with the same value on `column` will
            see their other columns concatenated in a list.
        batch_size (`int`, *optional*, defaults to `2`):
            Process batch of examples instead of just a single pair. This is transparent to the user but may
            increase speed and memory usage.
        remove_columns (`Optional[Union[str, List[str]]]`, defaults to `None`):
            Remove a selection of columns while doing the mapping.
            Columns will be removed before updating the examples with the output of `function`, i.e.
            if `function` is adding columns with names in `remove_columns`, these columns will be kept.
        keep_in_memory (`bool`, defaults to `False`):
            Keep the dataset in memory instead of writing it to a cache file.
        load_from_cache_file (`bool`, defaults to `True` if caching is enabled):
            If a cache file storing the current computation from `function`
            can be identified, use it instead of recomputing.
        cache_file_name (`str`, *optional*, defaults to `None`):
            Provide the name of a path for the cache file. It is used to store the
            results of the computation instead of the automatically generated cache file name.
        writer_batch_size (`int`, defaults to `1000`):
            Number of rows per write operation for the cache file writer.
            This value is a good trade-off between memory usage during the processing, and processing speed.
            Higher value makes the processing do fewer lookups, lower value consume less temporary memory
            while running `map`.
        disable_nullable (`bool`, defaults to `False`):
            Disallow null values in the table.
        num_proc (`int`, *optional*, defaults to `None`):
            Max number of processes when generating cache. Already cached shards are loaded sequentially.
        suffix_template (`str`):
            If `cache_file_name` is specified, then this suffix will be added at the end of the base name of each.
            Defaults to `"_{rank:05d}_of_{num_proc:05d}_iter_{iter:05d}"`.
            For example, if `cache_file_name` is "processed.arrow", then for `rank=1` and `num_proc=4`, the resulting
            file would be `"processed_00001_of_00004.arrow"` for the default suffix.
        desc (`str`, *optional*, defaults to `None`):
            Meaningful description to be displayed alongside with the progress bar while mapping examples.

    Example:

    ```python
    >>> ds = CustomDataset.load_dataset("lucadiliello/wikiqa")
    >>> def merge_on_question(example_1, example_2):
    ...     res = {k: [example_1[k], example_2[k]] for k in example_1.keys()}
    ...     return res
    >>> ds = ds.reduce(merge_on_question)
    >>> ds[0]
    ```
    """

    if batch_size < 2:
        raise ValueError("required `batch_size` greater or equal then 2")

    if keep_in_memory and cache_file_name is not None:
        raise ValueError("Please use either `keep_in_memory` or `cache_file_name` but not both.")

    if num_proc is not None and num_proc <= 0:
        raise ValueError("num_proc must be an integer > 0.")

    # If the array is empty we do nothing
    # (but we make sure to handle an empty indicesmapping and remove the requested columns anyway)
    if len(dataset) == 0:
        if dataset._indices is not None:  # empty indices mapping
            dataset = Dataset(
                dataset.data.slice(0, 0),
                info=dataset.info.copy(),
                split=dataset.split,
            )
        if remove_columns:
            return dataset.remove_columns(remove_columns)
        else:
            return dataset

    def prepare_function(examples: Dict[str, Any]) -> Dict[str, List[Any]]:
        return {key: [[v] for v in values] for key, values in examples.items()}

    def reduce_function(examples: Dict[str, List[Any]]) -> Dict[str, List[Any]]:
        # group over columns other than the main `column`

        other_column_names = [other_column for other_column in examples.keys() if other_column != column]
        res = {k: [] for k in examples.keys()}

        last_main_value = None
        for main_column_data, *other_column_data in zip(*[examples[k] for k in [column] + other_column_names]):
            if main_column_data != last_main_value:
                res[column].append(main_column_data)
                last_main_value = main_column_data
                for key in other_column_names:
                    res[key].append([])
            for column_name, data in zip(other_column_names, other_column_data):
                res[column_name][-1] += data

        return res

    if isinstance(remove_columns, str):
        remove_columns = [remove_columns]

    if column is None or column not in dataset._data.column_names:
        raise ValueError(
            f"Column {column} not in the dataset. Current columns in the dataset: {dataset._data.column_names}"
        )

    if remove_columns is not None and column in remove_columns:
        raise ValueError("Main reduce column {column} must not be in `remove_columns`")

    if remove_columns is not None and any(col not in dataset._data.column_names for col in remove_columns):
        raise ValueError(
            f"Column to remove {list(filter(lambda col: col not in dataset._data.column_names, remove_columns))} "
            f"not in the dataset. Current columns in the dataset: {dataset._data.column_names}"
        )

    if num_proc is not None:
        num_proc = min(num_proc, len(dataset))

    if remove_columns:
        initial_dataset = dataset.remove_columns(remove_columns)
    else:
        initial_dataset = dataset

    logger.info("Computing length of final dataset")
    target_dataset_length = len(dataset.unique(column=column))

    # prepare columns different from `column` for merging
    prepared_dataset = initial_dataset.map(
        prepare_function,
        with_indices=False,
        with_rank=False,
        batch_size=batch_size,
        batched=True,
        drop_last_batch=False,
        remove_columns=None,
        keep_in_memory=keep_in_memory,
        load_from_cache_file=load_from_cache_file,
        cache_file_name=cache_file_name,
        writer_batch_size=writer_batch_size,
        disable_nullable=disable_nullable,
        num_proc=num_proc,
        suffix_template=partial_format(suffix_template, iter=0),
        desc=desc,
    )

    # sort dataset on `column`
    sorted_dataset = prepared_dataset.sort(
        column=column, load_from_cache_file=load_from_cache_file, writer_batch_size=writer_batch_size
    )

    index = 1
    while len(sorted_dataset) > target_dataset_length:
        sorted_dataset = sorted_dataset.map(
            reduce_function,
            with_indices=False,
            with_rank=False,
            batched=True,
            batch_size=batch_size,
            drop_last_batch=False,
            remove_columns=None,
            keep_in_memory=keep_in_memory,
            load_from_cache_file=load_from_cache_file,
            cache_file_name=cache_file_name,
            writer_batch_size=writer_batch_size,
            disable_nullable=disable_nullable,
            num_proc=num_proc,
            suffix_template=partial_format(suffix_template, iter=index),
            desc=desc,
        )
        batch_size = batch_size ** 2  # need to increase bs to avoid getting stuck
        index += 1

        if num_proc is not None:
            num_proc = min(num_proc, len(dataset))

    return sorted_dataset

class datasets_disable_caching(object):

    def __enter__(self):
        rank_zero_info("Temporarily disabling caching for datasets")
        disable_caching()

    def __exit__(self, exc_type, exc_val, exc_tb):
        rank_zero_info("Re-enabling caching for datasets")
        enable_caching()


####该脚本考虑实际红移，观察Mh的分布
import sys
#sys.path.insert(0,'D:/Python1/pydocument/seniorproject_quenching2/practice/_pycache_/session_1')
sys.path.insert(0,'D:/Python1/pydocument/seniorproject_quenching2/practice')
##导入自己编写的脚本，需要加入这两句，一句声明符号应用，然后声明需要引入文-
#件的路径具体到文件夹
import os.path
from read_m16_1 import test_read_m16_ds_1
import numpy as np
import matplotlib.pyplot as plt
#import pandas as pd
from scipy.stats import t as st
#import seaborn as sns
#from scipy.stats import chisquare as chi
#画误差棒函数须引入scipy库
#导入的质量区间为11~15dex-Msolar
def input_mass_bin(v):
    _mh_path ='D:/Python1/pydocument/seniorproject_quenching2/practice/'
    fname = os.path.join(_mh_path,'z_eff.txt')
    z = np.loadtxt(fname,delimiter=',',dtype=np.float,unpack=True)
    z = np.array(z)
    N = 4
    m = np.array([np.linspace(11,12.75,1001),np.linspace(11,12.75,1001),\
                  np.linspace(11.75,13,2001),np.linspace(12.25,13.75,2001)])
    #针对不同质量区间设置拟合质量范围
    return m,N,z
#input_mass_bin(v=True)
def calcu_sigmaz(Rpc,m_,x,z):
    global omega_m
    omega_m = 0.315
    omegam = omega_m
#a flat CDM model,omegak=0;omegalamd=0
    global h_
    h_ = 0.673
    h = h_
    global G_
    G_ = 6.67*10**(-11)
    G = G_
    #global ms_
    #ms_ = 1.989*10**(30)
    #ms = ms_
    global c_
    c_ = 6
    ##调试结果c=3.25~3.75之间比较合适
    c = c_
    #下面开始计算
    #对导入的数据做单位转化转为:太阳质量/h
    m1 = h*m_*10**x
    L = len(Rpc)
    Rps = np.zeros(L,dtype=np.float)
    g_x = np.zeros(L,dtype=np.float)
    deltaSigma = np.zeros(L,dtype=np.float)
    Sigma = np.zeros(L,dtype=np.float)
    # rs =  np.zeros(LL,dtype=np.float)
    # r_200 =  np.zeros(LL,dtype=np.float)
    # rho_0 = np.zeros(LL,dtype=np.float)
    #单位修正因子
    #Qc = 0.1412#对rouc的单位修正（此时对roum、rou0的也完成了修正）
    #Q200 = 0.7
    ##重新对单位修正因子订正如下：
    Qc = 3.084*10**(-2)/(1.989*h**2)#对rouc的单位修正（此时对roum、rou0的也完成了修正）
    #Qc = 3.084*10**(-2)/(1.989)#对rouc的单位修正（此时对roum、rou0的也完成了修正）
    #rhom = 52700242579.0
    #m1 = m_*10**x
    Q200 = 1#对r200的单位修正（此时对rs也做了修正）
    H = h*100
    ##修正可以选择两处，对于计算过程涉及的物理量修正，或者对于常数修正
    rhoc = (Qc*(3*H**2)/(8*np.pi*G))/(1/(1+z))**3
    rhom = 200*rhoc*omegam
    #rhom=52700242579.0*200/h**2
    #考虑所有的红移为0.1
    r_200 = Q200*(3*m1/(4*np.pi*rhom))**(1/3)
    rs = r_200/c
    rho_0 = m1/((np.log(1+c)-c/(1+c))*4*np.pi*rs**3)
    for t in range(0,L):
        Rps[t] = Rpc[t]
        #引入中间函数
        f0 = Rps[t]/rs#这是考虑把R参数化的情况
        if Rps[t]<rs:
           f1 = np.arctanh(np.sqrt((1-f0)/(1+f0)))
           f2 = np.log(f0/2)
           f4 = f0**2*np.sqrt(1-f0**2)
           f5 = (f0**2-1)*np.sqrt(1-f0**2)
           g_x[t] = 8*f1/f4+4*f2*f0**(-2)-2/(f0**2-1)+4*f1/f5
           delta_segma = rs*rho_0*g_x[t]
          # deltasigma[n,t] = delta_segma  #(原有计算,面积单位为Mpc^2)
          # 为了和观测对比，把单位转为Msun*h/pc^2,即把面积单位转为pc^2
           deltaSigma[t] = delta_segma*10**(-12)
           Sigma[t] = (2*rs*rho_0/(f0**2-1))*(1-2*f1/(np.sqrt(1-f0**2)))
        elif Rps[t]==rs:
             g_x[t] = 10/3+4*np.log(1/2)
             delta_segma = rs*rho_0*g_x[t]
            # deltasigma[n,t] = delta_segma  #(原有计算,面积单位为Mpc^2)
            # 为了和观测对比，把单位转为Msun*h/pc^2,即把面积单位转为pc^2
             deltaSigma[t] = delta_segma*10**(-12)
             Sigma[t] = 2*rs*rho_0/3
        else:
             f1 = np.arctan(np.sqrt((f0-1)/(f0+1)))
             f2 = np.log(f0/2)
             f4 = f0**2*np.sqrt(f0**2-1)
             f5 = (f0**2-1)**(3/2)
             g_x[t]= 8*f1/f4+4*f2*f0**(-2)-2/(f0**2-1)+4*f1/f5
             delta_segma = rs*rho_0*g_x[t]
            # deltasigma[n,t] = delta_segma  #(原有计算,面积单位为Mpc^2)
            # 为了和观测对比，把单位转为Msun*h/pc^2,即把面积单位转为pc^2
             deltaSigma[t] = delta_segma*10**(-12)
             Sigma[t] = 2*rs*rho_0*(1-2*f1/np.sqrt(f0**2-1))/(f0**2-1)
    return Rpc,Sigma,deltaSigma,rs
#calcu_sigmaz(Rpc=True,m_=True,x=True,z=True)
##定义一个五个质量最佳预测和观测的对比图象
def fig_0(Rpc,ds_sim,lmw,r,mass_bin):
    test_read_m16_ds_1(mass_bin=mass_bin)
    for k in range(0,lmw):
        pa = r[k,1]
        plt.plot(Rpc[:],ds_sim[k,pa,:],'-*')
        nn = ds_sim[k,pa,:].size#求样本大小
        x_mean = np.mean(ds_sim[k,pa,:])#求算术平均值
        x_std = np.std(ds_sim[k,pa,:])#求标准偏差
        x_se = x_std/np.sqrt(nn)#求标准误差
        dof = nn-1#自由度计算
        alpha = 1.0-0.95
        conf_region = st.ppf(1-alpha/2.,dof)*x_std*\
        np.sqrt(1.+1./nn)#设置置信区间
        plt.errorbar(Rpc[:],ds_sim[k,pa,:],yerr=x_std,fmt='-',linewidth=0.5)
    plt.xlabel(r'$R-Mpc/h$')
    plt.ylabel(r'$\Delta\Sigma-hM_\odot/{pc^2}$')
    return x_mean,x_se,conf_region
#fig_0(Rpc=True,ds_sim=True,lmw=True,r=True)
#fig_0(f=True)
##定义一个质量最佳预测和观测的对比图象
def fig_0f1(Rpc,ds_sim,k,mass_bin):
    test_read_m16_ds_1(mass_bin=mass_bin)
    plt.plot(Rpc[:],ds_sim[k,:],'--')
    nn = ds_sim[k,:].size#求样本大小
    x_mean = np.mean(ds_sim[k,:])#求算术平均值
    x_std = np.std(ds_sim[k,:])#求标准偏差
    x_se = x_std/np.sqrt(nn)#求标准误差
    dof = nn-1#自由度计算
    alpha = 1.0-0.95
    conf_region = st.ppf(1-alpha/2.,dof)*x_std*\
    np.sqrt(1.+1./nn)#设置置信区间
    plt.errorbar(Rpc[:],ds_sim[k,:],yerr=x_std,fmt='-',linewidth=0.5)
    plt.xlabel(r'$R-Mpc/h$')
    plt.ylabel(r'$\Delta\Sigma-hM_\odot/{pc^2}$')
    return x_mean,x_se,conf_region
#fig_0f1(f=True)
def fig_0f2(Rpc,ds_sim,k,t,mass_bin):
    test_read_m16_ds_1(mass_bin=mass_bin)
    plt.plot(Rpc[:],ds_sim[k,t,:],'--',)
    nn = ds_sim[k,t,:].size#求样本大小
    x_mean = np.mean(ds_sim[k,t,:])#求算术平均值
    x_std = np.std(ds_sim[k,t,:])#求标准偏差
    x_se = x_std/np.sqrt(nn)#求标准误差
    dof = nn-1#自由度计算
    alpha = 1.0-0.95
    conf_region = st.ppf(1-alpha/2.,dof)*x_std*np.sqrt(1.+1./nn)#设置置信区间
    plt.errorbar(Rpc[:],ds_sim[k,t,:],yerr=x_std,fmt='-',linewidth=0.5)
    plt.xlabel(r'$R-Mpc/h$')
    plt.ylabel(r'$\Delta\Sigma-hM_\odot/{pc^2}$')
    return x_mean,x_se,conf_region
#fig_0f2(f=True)
#def fit_datar(y):
def fit_datar_1(mass_bin,z_int,T):
    h = 0.673
    #先找出观测值对应的rp
    #rsa,dssa,ds_errsa = test_read_m16_ds_1()
    rsa,dssa,ds_errsa = test_read_m16_ds_1(mass_bin=mass_bin)
    # print('ds=',dssa)
    # print('ds_err=',ds_errsa)
    #print('rp=',rsa)
    #a = np.shape(dssa)
    #print('size a=',a)
    ##注意到观测数值选取的是物理坐标，需要靠里尺度银子修正，修正如下
    #z_r = 0.1
    #a_r = 1/(1+z_r)
    ##此时对于预测的R也要做修正
    rp = np.array([rsa[0,k] for k in range(0,len(rsa[0,:])) if rsa[0,k]*h<=5])
   # rp = [rsa[0,k] for k in range(0,len(rsa[0,:])) if rsa[0,k]*h<=2]
   # rp = rsa[0,:]
    #该句直接对数组筛选，计算2Mpc以内（保证NFW模型适用的情况下）信号
    #print(rp)
    b = len(rp)
    m,N,z = input_mass_bin(v=True)
    mr = m[T]
    #print('mr=',mr)
    lm_bin = len(mr)
    #下面做两组数据的方差比较,注意不能对观测数据进行插值
    #比较方法，找出观测的sigma数值对应的Rp,再根据模型计算此时的模型数值Sigma（该步以完成）
    ds_simr = np.zeros((lm_bin,b),dtype=np.float)
    #rr = np.zeros(lm_bin,dtype=np.float)
    #该句用于下面相应作图部分
    for t in range(0,lm_bin):
        m_ = 1
        x = mr[t]
        z_r = z_int
        #Rpc = rp
        #Rpc,Sigma,deltaSigma = calcu_sigma(Rpc,m_,x)
        #ds_simr[k,t,:] = deltaSigma
        #对比文献，加入尺度因子修正如下,把物理的距离转为共动的距离
        #计算模型在对应的投射距离上的预测信号取值
        Rpc = rp
        Rpc,Sigma,deltaSigma,rs = calcu_sigmaz(Rpc,m_,x,z_r)
        #对预测信号做相应的变化，把共动的转为物理的
        ds_simr[t,:] = deltaSigma
        #下面部分作图对比
        #rr[t] = rs
        #fig_0f1(rp,ds_simr,t,mass_bin)
    #plt.title('Red')
    #plt.legend(m[:],bbox_to_anchor=(1,1))
    #plt.legend(m[:])
    #plt.savefig('Fit-red2.png',dpi=600)
    #plt.savefig('compare_1_rd.png',dpi=600)
    #plt.show()
    yy = np.shape(ds_simr)
    #print('rr=',rr)#输出查看ds_sim的维度，即模型预测下的透镜信号
    #比较观测的sigma和预测的Sigma,比较结果用fitr和fitb记录比较结果
    delta_r = np.zeros(lm_bin,dtype=np.float)
    for t in range(0,lm_bin):
        d_r = 0
        for n in range(0,yy[1]):
            d_r = d_r+((ds_simr[t,n]-dssa[0,n])/ds_errsa[0,n])**2
        delta_r[t] = d_r
    #print(delta_r)
    #下面这段求每个质量级对应的方差最小值
    deltar = delta_r.tolist()
    xr = deltar.index(min(deltar))
    bestfmr = mr[xr]
    best_mr = bestfmr+np.log10(h)
    #作图对比最佳情况
    '''
    plt.figure()
    k = xr
    fig_0f1(rp,ds_simr,k,mass_bin)
    plt.title('Red')
    #plt.savefig('fit_r.png',dpi=600)
    plt.show()
    '''
    #print('x^2=',min(deltar))
    #print('mr=',best_mr)
    #print('positionr=',xr)
    return rp,best_mr,delta_r,bestfmr
#fit_datar_1(y=True)
#def fit_datab_1(y):
def fit_datab_1(mass_bin,z_int,T):
    h = 0.673
    #先找出观测值对应的rp
    #rsa,dssa,ds_errsa = test_read_m16_ds_1()
    rsa,dssa,ds_errsa = test_read_m16_ds_1(mass_bin=mass_bin)
    # print('ds=',dssa)
    # print('ds_err=',ds_errsa)
    #print('rp=',rsa)
    #a = np.shape(dssa)
    #print('size a=',a)
    ##注意到观测数值选取的是物理坐标，需要靠里尺度银子修正，修正如下
    #z_b = 0.1
    #a_b = 1/(1+z_b)
    ##此时对于预测的R也要做修正
    rp = np.array([rsa[0,k] for k in range(0,len(rsa[0,:])) if rsa[0,k]*h<=5])
    # rp = [rsa[0,k] for k in range(0,len(rsa[0,:])) if rsa[0,k]*h<=2]
    # rp = rsa[0,:]
    #该句直接对数组筛选，计算2Mpc以内（保证NFW模型适用的情况下）信号
    #print(rp)
    b = len(rp)
    m,N,z = input_mass_bin(v=True)
    mb = m[T]
    #print('mr=',mb)
    lm_bin = len(mb)
    #下面做两组数据的方差比较,注意不能对观测数据进行插值
    #比较方法，找出观测的sigma数值对应的Rp,再根据模型计算此时的模型数值Sigma（该步以完成）
    ds_simb = np.zeros((lm_bin,b),dtype=np.float)
    #rb = np.zeros(lm_bin,dtype=np.float)
    #该句对应下面作图部分
    for t in range(0,lm_bin):
        m_ = 1
        x = mb[t]
        z_b = z_int
        #Rpc = rp
        #Rpc,Sigma,deltaSigma = calcu_sigma(Rpc,m_,x)
        #ds_simr[k,t,:] = deltaSigma
        #对比文献，加入尺度因子修正如下,把物理的距离转为共动的距离
        #计算模型在对应的投射距离上的预测信号取值
        Rpc = rp
        Rpc,Sigma,deltaSigma,rs = calcu_sigmaz(Rpc,m_,x,z_b)
        #对预测信号做相应的变化，把共动的转为物理的
        ds_simb[t,:] = deltaSigma
        ##下面部分作图对比
        #rb[t] = rs
        #fig_0f1(rp,ds_simb,t,mass_bin)
    #plt.title('Blue')
    #plt.legend(m[:],bbox_to_anchor=(1,1))
    #plt.legend(m[:])
    #plt.savefig('Fit-blue2.png',dpi=600)
    #plt.savefig('compare_2_bl.png',dpi=600)
    #plt.show()
    yy = np.shape(ds_simb)
    #print('rr=',rr)#输出查看ds_sim的维度，即模型预测下的透镜信号
    #比较观测的sigma和预测的Sigma,比较结果用fitr和fitb记录比较结果
    delta_b = np.zeros(lm_bin,dtype=np.float)
    for t in range(0,lm_bin):
        d_b = 0
        for n in range(0,yy[1]):
            d_b = d_b+((ds_simb[t,n]-dssa[1,n])/ds_errsa[1,n])**2
        delta_b[t] = d_b
    #print(delta_r)
    #下面这段求每个质量级对应的方差最小值
    deltab = delta_b.tolist()
    xb = deltab.index(min(deltab))
    bestfmb = mb[xb]
    best_mb = bestfmb+np.log10(h)
    #作图对比最佳情况
    '''
    plt.figure()
    k = xb
    fig_0f1(rp,ds_simb,k,mass_bin)
    plt.title('Blue')
    #plt.savefig('fit_r.png',dpi=600)
    plt.show()
    '''
    #print('x^2=',min(deltab))
    #print('mb=',best_mb)
    #print('positionr=',xb)
    return rp,best_mb,delta_b,bestfmb
#fit_datab_1(y=True)
#下面做图比较最佳预测值与观测的对比情况
def fig_1(tt):
    m,N,z = input_mass_bin(v=True)
    #调用时需要修改下面两句函数的关键字
    rp,best_mr,delta_r,bestfmr = fit_datar_1(y=True)
    rp,best_mb,delta_b,bestfmb = fit_datab_1(y=True)
    #rp,best_mr,delta_r = fit_datar(mass_bin)
    #rp,best_mb,delta_b = fit_datab(mass_bin)
    plt.subplot(121)
    plt.plot(m,np.log10(delta_r),'r')
    plt.title('R-galaxy')
    plt.xlabel(r'$log(\frac{M_h}{M_\odot})$')
    plt.ylabel(r'$log(\chi^2)$')
    plt.grid()
    plt.subplot(122)
    plt.plot(m,np.log10(delta_b),'b')
    plt.xlabel(r'$log(\frac{M_h}{M_\odot})$')
    plt.ylabel(r'$log(\chi^2)$')
    plt.title('B-galaxy')
    plt.grid()
    plt.tight_layout()
    plt.show()
    print('mr=',best_mr)
    print('mb=',best_mb)
    return m,N,best_mr,best_mb
#fig_(tt=True)
def fig_mass(pp):
    m_bin = ['10.0_10.4','10.4_10.7','10.7_11.0','11.0_15.0']
    mbin = [10.2,10.55,10.9,13.0]
    #作图取点
    m,N,z = input_mass_bin(v=True)
    z_use = np.zeros((len(m_bin),2),dtype=np.float)
    ##计算误差棒并存储的数组
    err_bar_r = np.zeros((len(m_bin),2),dtype=np.float)
    err_bar_b = np.zeros((len(m_bin),2),dtype=np.float)
    for k in range(0,len(m_bin)):
        if k==len(m_bin)-1:
            z_use[k,:] = z[:,-1]
        else:
            z_use[k,:] = z[:,k]
    print(z_use)
    #print(m_bin)
    #该句导入拟合质量
    Pr_mh = np.zeros((len(m_bin),2),dtype=np.float)
    b_m_r = np.zeros(len(m_bin),dtype=np.float)
    b_m_b = np.zeros(len(m_bin),dtype=np.float)
    #Pr_mh的第一行存red的质量，第二行存blue的质量,x_std也一样
    for k in range(0,len(m_bin)):
        rp,best_mr,delta_r,bestfmr = fit_datar_1(m_bin[k],z_use[k,0],k)
        rp,best_mb,delta_b,bestfmb = fit_datab_1(m_bin[k],z_use[k,1],k)
        Pr_mh[k,0] = best_mr
        Pr_mh[k,1] = best_mb
        b_m_r[k] = bestfmr
        b_m_b[k] = bestfmb
        ##下面把x^2转化为相应的概率分布，并让mh的划分区间服从这个分布
        mr = m[k]
        mb = m[k]
        rsa,dssa,ds_errsa = test_read_m16_ds_1(mass_bin=m_bin[k])
        pr = np.ones(len(m[k]),dtype=np.float)
        pb = np.ones(len(m[k]),dtype=np.float)
        ds_errsar = ds_errsa[0,0:len(rp)]
        ds_errsab = ds_errsa[1,0:len(rp)]
        for q in range(0,len(mr)):
            ss=1
            for t in range(0,len(rp)):
                ss = ss*(1/(np.sqrt(np.pi*2)*ds_errsar[t]))*np.exp((-1/2)*delta_r[q])
            pr[q]=ss
        for q in range(0,len(mb)):
            qq=1
            for t in range(0,len(rp)):
                qq = qq*(1/(np.sqrt(np.pi*2)*ds_errsab[t]))*np.exp((-1/2)*delta_b[q])
            pb[q]=qq
        ##第一次归一化，把概率密度函数归一到0~1之间
        pr = (pr-np.min(pr))/(np.max(pr)-np.min(pr))
        pb = (pb-np.min(pb))/(np.max(pb)-np.min(pb))
        '''
        plt.figure()
        plt.plot(mr,pr,'r')
        plt.title('Probability distribution')
        plt.xlabel(r'$log(M_h/M_\odot)$')
        plt.ylabel(r'$dP(M_h|M_\ast)/dM_h$')
        plt.show()
        plt.plot(mb,pb,'b')
        plt.title('Probability distribution')
        plt.xlabel(r'$log(M_h/M_\odot)$')
        plt.ylabel(r'$dP(M_h|M_\ast)/dM_h$')
        plt.show()
        '''
        #print('r=',delta_r)
        #print('b=',delta_b)
        ##第二次归一化，目的是让所有概率求和为1
        fr=np.zeros(len(mr),dtype=np.float)
        fb=np.zeros(len(mb),dtype=np.float)
        ss=0
        qq=0
        Fr=np.zeros(len(mr),dtype=np.float)
        Fb=np.zeros(len(mb),dtype=np.float)
        for q in range(0,len(mr)):
            ss=ss+pr[q]*(mr[1]-mr[0])
            fr[q]=ss
        for q in range(0,len(mb)):
            qq=qq+pb[q]*(mb[1]-mb[0])
            fb[q]=qq
        Ar = np.max(fr)
        Ab = np.max(fb)
        ss=0
        qq=0
        for q in range(0,len(mr)):
            ss=ss+pr[q]*(mr[1]-mr[0])/Ar
            Fr[q]=ss
        for q in range(0,len(mb)):
            qq=qq+pb[q]*(mb[1]-mb[0])/Ab
            Fb[q]=qq
        #plt.figure()
        #plt.plot(mr,Fr,'r')
        #plt.show()
        #plt.plot(mb,Fb,'b')
        #plt.show()
        vr = np.interp(0.16,Fr,mr)
        ur = np.interp(0.84,Fr,mr)
        err_bar_r[k,:] = [vr-bestfmr,ur-bestfmr]
        vb = np.interp(0.16,Fb,mb)
        ub = np.interp(0.84,Fb,mb)
        err_bar_b[k,:] = [vb-bestfmb,ub-bestfmb]
    ##对应for循环前面的plt.figure
    #print('phy_mr=',Pr_mh[:,0])
    #print('phy_mb=',Pr_mh[:,1])
    #print('errbar_r=',err_bar_r)
    #print('errbar_b=',err_bar_b)
    #####注意errorbar作图命令
    plt.figure()
    color_list = ['r','b','g']
    line1,caps1,bars1=plt.errorbar(mbin,Pr_mh[:,0],yerr=[err_bar_r[:,0],err_bar_r[:,1]],fmt="ks-",linewidth=1,
                                elinewidth=0.5,ecolor='k',capsize=1,capthick=0.5,label='red')
    line3,caps3,bars3=plt.errorbar(mbin,Pr_mh[:,1],yerr=[err_bar_b[:,0],err_bar_b[:,1]],fmt="k^-",linewidth=1,
                                elinewidth=0.5,ecolor='k',capsize=1,capthick=0.5,label='blue')
    plt.fill_between(mbin,Pr_mh[:,0]+err_bar_r[:,0],Pr_mh[:,0]+err_bar_r[:,1],
                         facecolor=color_list[0],alpha=.20)
    plt.fill_between(mbin,Pr_mh[:,1]+err_bar_b[:,0],Pr_mh[:,1]+err_bar_b[:,1],
                         facecolor=color_list[1],alpha=.20)
    plt.xlabel(r'$log[M_\ast/M_\odot]$')
    plt.ylabel(r'$log{\langle M_{200} \rangle/[M_\odot h^{-1}]}$')
    plt.legend(loc=2)
    plt.title(r'$Standard Deviation-1\sigma_{p}$')
    plt.axis([10,14,11,14])
    #plt.savefig('Standard_deviation.png',dpi=600)
    plt.show()
    return Pr_mh
fig_mass(pp=True)
'''
if __name__ == "__main__":
    dolad_data(m=True,hz=True)
    pass
'''


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

import numpy as np
import os

from tensorflow.core.framework.summary_pb2 import Summary
from bert4keras.backend import keras, set_gelu, K
from bert4keras.tokenizers import Tokenizer
from bert4keras.models import build_transformer_model
from bert4keras.optimizers import Adam
from bert4keras.snippets import sequence_padding, DataGenerator
from bert4keras.snippets import open
from keras.layers import Dropout, Dense
from tqdm import tqdm

set_gelu('tanh')
line_label = {0: 'experience', 1: 'knowledge', 2: 'education', 3: 'project', 4: 'others'}
label2index = {v:k for k,v in line_label.items()}

print(label2index)

maxlen = 128
batch_size = 8
config_path = 'E:/bert4keras-master/uncased_L-12_H-768_A-12/bert_config.json'
checkpoint_path = 'E:/bert4keras-master/uncased_L-12_H-768_A-12/bert_model.ckpt'
dict_path = 'E:/bert4keras-master/uncased_L-12_H-768_A-12/vocab.txt'

tokenizer = Tokenizer(dict_path, do_lower_case=True)

bert = build_transformer_model(
    config_path=config_path,
    checkpoint_path=checkpoint_path,
    with_pool=True,
    return_keras_model=False,
)

output = Dropout(rate=0.1)(bert.model.output)
output = Dense(
    units=5, activation='softmax', kernel_initializer=bert.initializer
)(output)

model = keras.models.Model(bert.model.input, output)
model.summary()

model.load_weights('E:/bert4keras-master/resume_model.weights')

import docx
import os
from io import StringIO
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage

def pdf_to_text(fname, pages=None):
    if not pages:
        pagenums = set()
    else:
        pagenums = set(pages)

    output = StringIO()
    manager = PDFResourceManager()
    converter = TextConverter(manager, output, laparams=LAParams())
    interpreter = PDFPageInterpreter(manager, converter)

    infile = open(fname, 'rb')
    for page in PDFPage.get_pages(infile, pagenums):
        interpreter.process_page(page)
    infile.close()
    converter.close()
    text = output.getvalue()
    output.close()
    result = []
    for line in text.split('\\n'):
        line2 = line.strip()
        if line2 != '':
            result.append(line2)
    return result

def preprocess_text(text):
    text = ' '.join(text.split())
    text = join_name_tag(text)
    return text

def join_name_tag(text):
    text = text.replace('\\u2003', '')
    return text
data_dir_path = 'resume.docx'
def predict_sentence(sentence):
            token_ids, segment_ids = tokenizer.encode(
                        sentence, maxlen=len(sentence.split())
                    )
            y_pred = model.predict([[token_ids],[segment_ids]]).argmax(axis=1)
            print(line_label[y_pred[0]],':',sentence)
            return line_label[y_pred[0]]

def docx_to_text(file_path):
    doc = docx.Document(file_path)
    result = []
    for p in doc.paragraphs:
        txt = p.text.strip()
        if txt != '':
            txt = preprocess_text(txt)
            predict_sentence(txt)
            result.append(txt)
    return result

#main
docx_to_text(data_dir_path)


from fastapi import FastAPI
from config import models
from config.database import engine
from routers import recommender, user, auth
from fastapi.middleware.cors import CORSMiddleware

models.Base.metadata.create_all(bind=engine)

app = FastAPI()

origin = ['http://localhost:5173']

app.add_middleware(
    CORSMiddleware,
    allow_origins=origin,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]
)

app.include_router(recommender.router)
app.include_router(user.router)
app.include_router(auth.router)


import os
import re

from config import problem_save_path, problem_mysql_save_path
from utils import mongo_util
from utils.file_util import clone_all_files
from utils.mysql.mysql_db import *
from utils.mongo_util import *

def get_format_problem(problem, name_list):
    """
    get a format problem data set to insert or update to mysql
    :param problem:
    :param name_list: insert or update to mysql columns value
    :return:
    """
    # change time_limit unit s to ms
    time_limit = problem.get(Problem.TIME_LIMIT)
    time_limit = str(int(float(time_limit) * 1000))
    problem[Problem.TIME_LIMIT] = time_limit
    # print(problem[PROBLEM.TIME_LIMIT])
    # problem_id
    insert_problem = []
    # insert_problem.append()
    for idx in name_list:
        if idx == Problem.ID:
            # need string
            insert_problem.append(get_insert_problem_id())
            continue
        elif idx == Problem.USER_ID:
            insert_problem.append(1)
            continue
        info = problem.get(idx, "")
        info = deal_text_format(info)
        # only json string need
        insert_problem.append(info)
    return insert_problem

def inset_to_mysql(problem):
    insert_problem = get_format_problem(problem, [Problem.ID, Problem.USER_ID, Problem.TITLE, Problem.TAG,
                                                  Problem.DESCRIPTION, Problem.FORMAT_INPUT, Problem.FORMAT_OUTPUT,
                                                  Problem.SAMPLE_INPUT, Problem.SAMPLE_OUTPUT, Problem.HINT,
                                                  Problem.MEMORY_LIMIT, Problem.TIME_LIMIT])
    # SQL 插入语句
    sql = """
        INSERT INTO problem(problem_id,user_id,title,tag,description,
    format_input,format_output,sample_input,sample_output,hint,
    memory_limit,time_limit)
    VALUES(%d,%d,'%s','%s','%s','%s','%s','%s','%s','%s',%s,%s)
        """ % tuple(insert_problem)
    # print(sql)
    try:
        # 执行sql语句
        mysql_cursor.execute(sql)
        # 提交到数据库执行
        mysql_db.commit()
    except:
        # print(sql)
        print("sql error")
        traceback.print_exc()
        # 如果发生错误则回滚
        mysql_db.rollback()
    # check if insert success
    mysql_id = insert_problem[0]
    if find_in_mysql_with_id(mysql_id):
        clone_problem_data(mysql_id, problem[Problem.ID])
        update_mongodb(problem, mysql_id=mysql_id)
    else:
        print("not find problem with problem id = %d" % mysql_id)

def get_insert_problem_id():
    # SQL 查询语句
    sql = "SELECT max(problem_id) FROM problem where problem_id"
    try:
        # 执行SQL语句
        mysql_cursor.execute(sql)
        # 获取所有记录列表
        result = mysql_cursor.fetchone()
        # 有可能一个都没有, None
        max_id = result[0]
        if not max_id:
            return 1000
        else:
            return max_id + 1
    except:
        traceback.print_exc()
        print("Error: unable to fetch data")

def find_in_mysql_with_id(id):
    # SQL 查询语句
    sql = "SELECT * FROM problem where problem_id = '%s'" % (id)
    try:
        # 执行SQL语句
        mysql_cursor.execute(sql)
        # 获取所有记录列表
        results = mysql_cursor.fetchall()
        # print(results)
        return results
    except:
        traceback.print_exc()
        print("Error: unable to fetch data")

def find_in_mysql_with_title(title):
    # SQL 查询语句
    sql = "SELECT * FROM problem where title = '%s'" % (title)
    try:
        # 执行SQL语句
        mysql_cursor.execute(sql)
        # 获取所有记录列表
        results = mysql_cursor.fetchall()
        # print(results)
        return results
    except:
        traceback.print_exc()
        print("Error: unable to fetch data")

def deal_text_format(text):
    text = text.replace(r"'", r"\'", re.S)
    return text

def update_info_mysql(problem):
    format_problem = get_format_problem(problem, [Problem.DESCRIPTION, Problem.FORMAT_INPUT, Problem.FORMAT_OUTPUT,
                                                  Problem.SAMPLE_INPUT, Problem.SAMPLE_OUTPUT, Problem.HINT,
                                                  Problem.TITLE])
    # SQL 查询语句
    sql = """UPDATE problem set
    description = '%s',
    format_input = '%s' ,
    format_output = '%s',
    sample_input = '%s',
    sample_output = '%s' ,
    hint = '%s'
    where title = '%s'""" % tuple(format_problem)
    # print(sql)
    title = problem[Problem.TITLE]
    try:
        # 执行SQL语句
        mysql_cursor.execute(sql)
        # 提交到数据库执行
        mysql_db.commit()
        print("{} update successful".format(title))
        return True
    except:
        # 发生错误时回滚
        # print(sql)
        print("{} update fail".format(title))
        mysql_db.rollback()
        return False

def update_problem_id(new_id, old_id):
    # return
    # SQL 查询语句
    sql = """UPDATE problem set problem_id = %s where problem_id = %s""" % (new_id, old_id)
    # print(sql)
    try:
        # 执行SQL语句
        mysql_cursor.execute(sql)
        # 提交到数据库执行
        mysql_db.commit()
        # print("{} update successful".format(title))
    except:
        # 发生错误时回滚
        # print(sql)
        mysql_db.rollback()

def update_problem_data(problem):
    format_problem = get_format_problem(problem, [Problem.DATA, Problem.TITLE])
    if problem[Problem.TITLE] == "瓷砖铺放":
        print(format_problem)
        # return
    # SQL 查询语句
    sql = """UPDATE problem set
        judge_data = '%s'
        where title = '%s'""" % tuple(format_problem)
    # print(sql)
    title = problem[Problem.TITLE]
    try:
        # 执行SQL语句
        mysql_cursor.execute(sql)
        # 提交到数据库执行
        mysql_db.commit()
        print("{} update successful".format(title))
        return True
    except:
        # 发生错误时回滚
        # print(sql)
        print("{} update fail".format(title))
        mysql_db.rollback()
        return False

def clone_problem_data(mysql_id, problem_id):
    """
    clone problem data form download with problem_id as directory name
    :return:
    """
    source_dir = problem_save_path + "/" + problem_id
    target_dir = problem_mysql_save_path + "/" + str(mysql_id)
    shutil.copytree(source_dir, target_dir)

def update_mongodb(problem, mysql_id):
    problem[Problem.MYSQL_ID] = mysql_id
    mongo_util.problem_collection. \
        update_one({Problem.ID: problem[Problem.ID]}, {"$set": {Problem.MYSQL_ID: mysql_id}})

def main():
    # 重新开始记得，清空关联mysql_id
    # problem_collection.update_many({}, {"$set": {Problem.MYSQL_ID: ""}})
    # 按照一定的顺序
    for begin_prefix in tag_dict.values():
        id_reg = {"$regex": "^" + begin_prefix}
        query = {Problem.ID: id_reg, Problem.DATA_STATUS: StateValue.FILE_SUCCESS}
        problems = problem_collection.find(query).sort(Problem.ID)
        for problem in problems:
            print(problem[Problem.ID])
            title = problem[Problem.TITLE]
            mysql_id = problem.get(Problem.MYSQL_ID)
            # maybe mysql field == ""
            find_problem = find_in_mysql_with_id(problem[Problem.MYSQL_ID])
            if find_problem:
                print("{%s} is already in mysql table" % title)
            else:
                inset_to_mysql(problem)
                print("{} inserts in mysql table".format(title))
    total = problem_collection.count_documents({Problem.DATA_STATUS: StateValue.FILE_SUCCESS})
    print("total download file success = {}".format(total))
    mysql_db.close()

if __name__ == "__main__":
    main()


import stk

class CaseData:
    """A test case.

    Attributes:

    """

    def __init__(
        self,
        molecule: stk.Molecule,
        force_field_library,
        bond_ranges,
        angle_ranges,
        torsion_ranges,
        nonbonded_ranges,
        present_bonds,
        present_angles,
        present_nonbondeds,
        present_torsions,
        num_forcefields: int,
        library_string: str,
        name: str,
    ) -> None:
        self.molecule = molecule
        self.force_field_library = force_field_library
        for i in bond_ranges:
            self.force_field_library.add_bond_range(i)
        for i in angle_ranges:
            self.force_field_library.add_angle_range(i)
        for i in torsion_ranges:
            self.force_field_library.add_torsion_range(i)
        for i in nonbonded_ranges:
            self.force_field_library.add_nonbonded_range(i)
        self.num_forcefields = num_forcefields
        self.present_bonds = present_bonds
        self.present_angles = present_angles
        self.present_nonbondeds = present_nonbondeds
        self.present_torsions = present_torsions
        self.library_string = library_string
        self.name = name


import sys
import OurLib
import colors
# pyramid

# function

def pyramid(floor, char):

    for i in range(int(floor)):
        for j in range(int(floor) - i):
            print(' ', end='')

        print(char * (i + 1) + char * i)

# error management

if OurLib.lenCounter(sys.argv) != 3:
    print('error. f')
    exit()

if not OurLib.isNumericHomeMade(sys.argv[1]):
    print('error. i')
    exit()

if OurLib.isNumericHomeMade(sys.argv[2]):
    print('error. k')
    exit()

# exe

pyramid(sys.argv[1], sys.argv[2])

# finished


import sys
from multiprocessing import Process
try:
    TUBY = TUBY  # @UndefinedVariable
except Exception:
    from tuby.core import TubyStream
    TUBY = TubyStream()

def daemon(*a, **ka):
    from time import sleep
    while True:
        with open('foo.alive', 'wb') as fw:
            fw.write("I'm alive!\n")
        print('d')
        sleep(3)

process = Process(target=daemon)
process.daemon = False
TUBY.stdout.write('detaching')
process.start()

sys.exit(0)


import random
import discord
from discord_token import token

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

special_rolls = {1: "GM Intrusion", 17: "Damage Bonus +1", 18: "Damage Bonus +2", 19: "Minor Effect or +3 damage", 20: "Major effect or +4 damage"}

@client.event
async def on_ready():
  print(f"we've logged on as {client.user}")

@client.event
async def on_message(message):
  if message.author == client.user:
    return
  if message.content.split()[0] == '$hello':
      await greeting(message)
  if message.content.split()[0] == '$help':
      await help(message)
  if message.content.split()[0] == '$r' or message.content.split()[0] == '$roll':
      await roll(message)
  if message.content.split()[0] == '$hero':
      await hero(message)
  if message.content.split()[0] == '$h' or message.content.split()[0] == '$heal':
      await heal(message)
  if message.content.startswith('$d'):
      await rollD(message)

def greeting(message):
  return message.channel.send(f"Hello {message.author.display_name}, I'm Instant, a Cypher RPG die rolling bot.  Type $help for more details on how I can help")

def help(message):
  return message.channel.send("Instant:\n  $help\n    show this help\n  $r 3\n    make a level 3 roll\n  $d10 + 3\n    rolls a d10 and adds 3\n  $h +3\n    make a healing roll and add 3\n  $hello\n    Say hi.  Everybody appreciates politeness\nYou can also type $roll instead of $r, or $heal instead of $h -- I'm flexible.\nAnd you choose to skip spaces after + signs, or the + sign at all")

def roll(message):
  params = message.content.split()
  if (len(params) > 2):
      return message.channel.send(f"I'm not sure how to make that roll.  The syntax I'm expecting is $r 3, but you put more than one thing after the $r")
  if (len(params) < 2):
    level = 0
  else:
    try:
      level = int(params[1])
    except:
      return message.channel.send(f"I was expecting a number after the $r, but did you say '{params[1]}'?")
  roll = random.randint(1,20)
  target = 3 * level
  success = (roll >= target)
  success_message = ""
  if success:
      success_message = "**Success!**"
  else:
      success_message = "**Failed.**"
  m = success_message
  if roll in special_rolls.keys():
      m = f"{m}\n{special_rolls[roll]}."
  return message.channel.send(f">>> {message.author.display_name} rolled a **{roll}**, and the target was {target}\n{m}")

def parseBonus(params):
  bonus = 0
  for param in params:
    try:
      b = int(param)
      bonus = bonus + b
    except ValueError:
      b = 0
  return bonus

def heal(message):
  params = message.content.split()
  bonus = 0
  if (len(params) > 1):
    bonus = parseBonus(params[1:])
  roll = random.randint(1,6)
  if (bonus == 0):
    reply = f">>> {message.author.display_name} rolled **{roll}** on their heal roll"
  else:
    reply = f">>> {message.author.display_name} rolled **{roll}** + {bonus} = {roll + bonus} on their heal roll"
  return message.channel.send(reply)

def rollD(message):
  params = message.content.split()
  try:
    d = int(params[0][2:])
  except:
    return message.channel.send(f"I was expecting $d6 or $d8, not '{params[0]}.  What did you want me to do?'")
  bonus = 0
  if (len(params) > 1):
    bonus = parseBonus(params[1:])
  roll = random.randint(1, d)
  if bonus == 0:
    reply = f">>> {message.author.display_name} rolled **{roll}** on 1d{d}"
  else:
    reply = f">>> {message.author.display_name} rolled **{roll}** + {bonus} = {roll + bonus} on 1d{d} + {bonus}"
  return message.channel.send(reply)

def hero(message):
  return message.channel.send(f"You've got this {message.author.display_name}.  You're the hero here.")

client.run(token)


from sys import stdin

input = lambda: stdin.readline().rstrip()

def check(start, end):

    if end - start <= 1:
        return True

    mid = start + (end - start) // 2
    for i in range(mid - start):
        if paper[start + i] == paper[end - i - 1]:
            return False

    return check(start, mid) and check(mid + 1, end)

if __name__ == "__main__":
    T = int(input())
    for _ in range(T):
        paper = list(map(int, input()))

        print(['NO', 'YES'][check(0, len(paper))])


import sqlite3
from sqlite3 import Error

def sql_connection():
    try:
        con = sqlite3.connect('database.db')
        return con
    except Error:
        print(Error)

def sellers_table(con):
    cursorObj = con.cursor()
    cursorObj.execute(
        "CREATE TABLE sellers(seller_id integer PRIMARY KEY, "
        "name text, salary real, term_work text)")
    con.commit()

def providers_table(con):
    cursorObj = con.cursor()
    cursorObj.execute(
        "CREATE TABLE providers(provider_id integer PRIMARY KEY, prov_name text, works_time text, delivery_time text)")
    con.commit()

def items_table(con):
    cursorObj = con.cursor()
    cursorObj.execute(
        "CREATE TABLE items(item_id integer PRIMARY KEY, provider_id integer, item_name text, weight real,"
        " FOREIGN KEY (provider_id) REFERENCES providers_table (provider_id))")
    con.commit()

def transactions_table(con):
    cursorObj = con.cursor()
    cursorObj.execute(
        "CREATE TABLE transactions(trans_id integer PRIMARY KEY, provider_id integer,"
        "seller_id integer, cost real, date date,"
        "FOREIGN KEY (provider_id) REFERENCES providers_table (provider_id),"
        "FOREIGN KEY (seller_id) REFERENCES sellers_table (seller_id))")
    con.commit()

def history_table(con):
    cursorObj = con.cursor()
    cursorObj.execute(
        "CREATE TABLE history(note_id integer PRIMARY KEY, item_id integer, seller_id integer, create_info date,"
        "FOREIGN KEY (item_id) REFERENCES items_table (item_id),"
        "FOREIGN KEY (seller_id) REFERENCES sellers_table (seller_id))")
    con.commit()

con = sql_connection()

sellers_table(con)
providers_table(con)
items_table(con)
transactions_table(con)
history_table(con)


from wizard101 import util
import time, keyboard
import numpy as np
from dataclasses import dataclass

class DanceGameSolver:
    @dataclass
    class Resources:
        down: np.ndarray
        right: np.ndarray
        up: np.ndarray
        left: np.ndarray
        empty: np.ndarray

    def __init__(self):
        self.resources = DanceGameSolver.Resources(
            down=util.img_resource("dance_down.png"),
            right=util.img_resource("dance_right.png"),
            up=util.img_resource("dance_up.png"),
            left=util.img_resource("dance_left.png"),
            empty=util.img_resource("dance_empty.png"),
        )

        self.maxTemplateSize = [0, 0]  # x, y
        for img in self.resources.__dict__.values():
            self.maxTemplateSize[0] = max(self.maxTemplateSize[0], img.shape[1])
            self.maxTemplateSize[1] = max(self.maxTemplateSize[1], img.shape[0])

        self.known_location = [xy / 2.0 for xy in self.maxTemplateSize]  # x, y CENTER!!
        self.da = util.DesktopAutomator()

        self.state = None
        self.last_state = None
        self.time_state_changed = 0
        self.printed_not_found = False

        self.sequence = None

    def change_state(self, new_state: str | None) -> None:
        self.last_state = self.state
        self.state = new_state
        self.time_state_changed = time.time()

    def loop(self) -> None:
        [cx, cy] = self.known_location
        [w, h] = self.maxTemplateSize
        ox, oy = int(cx + 0.5 - w / 2 - 10), int(cy + 0.5 - h / 2 - 10)
        src = self.da.grab(ox, oy, w + 20, h + 20)

        # figure out which template is in the image
        d = []
        for k, img in self.resources.__dict__.items():
            d.append([k, *self.da.find_in_image(src, img)])

        d.sort(key=lambda x: x[-1])
        [k, x, y, t] = d[0]
        x += ox
        y += oy

        if t > 0.01:
            if self.state == None:
                if (
                    time.time() - self.time_state_changed > 1
                ):  # found nothing for 1 second... user moved window or closed pet game
                    self.time_state_changed = time.time()  # reset the timer as to only search every 1 second
                    self.sequence = None  # cancel any sequence

                    fullscreen = self.da.grab_fullscreen()
                    empty = self.resources.empty

                    w, h, _ = empty.shape
                    x, y, t = self.da.find_in_image(fullscreen, empty)
                    if t < 0.01:  # FOUND!
                        print("Found game window! Now tracking dance sequences.")
                        self.known_location = [x + w / 2, y + h / 2]
                        self.change_state("empty")
                    elif not self.printed_not_found:  # not found
                        print(
                            "Waiting for you to enter the dance game... Make sure you're playing in 1280x720 resolution. (prefer no fullscreen)"
                        )
                        self.printed_not_found = True
                else:
                    pass  # failed to find
            else:
                self.change_state(None)
        elif k != self.state and k in ["down", "right", "up", "left"]:
            if self.sequence == None:
                self.sequence = []
            self.sequence.append(k)
            print("Remember:", k)
        elif (
            k == "empty"
            and k == self.state
            and time.time() - self.time_state_changed > 1
            and self.sequence != None
            and len(self.sequence) >= 3
        ):
            print("Entering full sequence:", ", ".join(self.sequence))
            for dir in self.sequence:
                keyboard.press_and_release(dir)
                time.sleep(0.01)
            self.sequence = None

        if t <= 0.01 and k != self.state:
            self.printed_not_found = False
            self.change_state(k)


import xarray as xr
import salem
import os
import pandas as pd
import numpy as np
from glaciersat.core import imagery

import logging
log = logging.getLogger(__file__)

def crop_sat_image_to_glacier(ds: xr.Dataset or imagery.SatelliteImage,
                              gdir_candidates: list = None,
                              out_dirs: list = None, grids: list = None,
                              min_overlap: float = 100.,
                              shapes: list or None = None) -> None:
    """
    Crop a satellite image into glacier domains and append to existing images.

    Parameters
    ----------

    ds : xr.Dataset or glaciersat.core.imagery.SatelliteImage
        Object containing the satellite image.
    gdir_candidates: list of GlacierDirectories, optional
        List with potential GlacierDirectories that might be included in the
        scene. Mutually exclusive with `grids`. Default: None.
    out_dirs: list of str, optional
        List with according output directories for the case that `grids` is
        given.
    grids: list of salem.Grid, optional
        List with salem.Grids defining a glacier region to which the data shall
        be clipped. Mutually exclusive with `gdir_candidates`. Default: None.
    min_overlap: float
        Minimum overlap percentage of satellite image and glacier. Default:
        100. (glacier must be contained fully in satellite image footprint).
    shapes: list or None, optional
        List of paths to shapes of glaciers. Must be in the same order like
        `gdir_candidates` or `grids`, respectively. If `None` and
        `gdir_candidates` is given, shapes will be retrieved from the outlines
        in the glacier directory. Default: None.

    Returns
    -------
    None.
    """

    if (gdir_candidates is not None) and (grids is not None):
        raise ValueError('The keywords "gdir_candidates" and "grids" are '
                         'mutually exclusive.')
    elif (gdir_candidates is None) and (grids is None):
        raise ValueError('Either of the keywords "gdir_candidates" or "grids" '
                         'must be given.')
    elif (gdir_candidates is not None) and (grids is None):
        grids = [salem.Grid.from_json(g.get_filepath('glacier_grid')) for g in
                 gdir_candidates]
    else:
        pass

    if (shapes is None) and (gdir_candidates is not None):
        shapes = [gdir_candidates[i].get_filepath('outlines') for i in
                  range(len(gdir_candidates))]

    # cheap way to pre-assess whether given glacier is in the image at all
    if callable(getattr(ds, 'overlaps_shape', None)) and (shapes is not None):
        cand_in_img = [ds.overlaps_shape(i, percentage=min_overlap) for i in
                       shapes]
        if np.array(cand_in_img).any() is False:  # no intersection at all
            log.info(
                'No intersection of the satellite image and the supplied grids'
                ' at the given level ({}%) at all.'.format(min_overlap))
            return
    else:  # postpone to next step (more expensive)
        cand_in_img = None

    # we need to load for this operation
    if not isinstance(ds, (xr.Dataset, xr.DataArray)):
        ds = ds.data

    ds.load()
    ds_glacier = None
    if not isinstance(ds, xr.Dataset):
        ds = ds.to_dataset(name='albedo', promote_attrs=True)

    for i, grid in enumerate(grids):

        if (cand_in_img is not None) and (cand_in_img[i] is False):
            continue

        grid_ds = grid.to_dataset()
        ds_glacier = grid_ds.salem.transform(ds, interp='linear')

        # can be that grid is outside satellite image
        # todo: what about half coverage (edge) and clouds? check with outline?
        if pd.isnull(ds_glacier.to_array()).all():
            # todo: log something here?
            continue

        # otherwise lost!!!
        for v in ds_glacier.data_vars:
            ds_glacier[v].encoding.update(ds[v].encoding)

        ds_glacier['solar_azimuth_angle'] = (['time'], [ds.mean_azimuth_deg])
        ds_glacier['solar_zenith_angle'] = (['time'], [ds.mean_zenith_deg])
        ds_glacier.solar_azimuth_angle.attrs['unit'] = 'degree'
        ds_glacier.solar_zenith_angle.attrs['unit'] = 'degree'
        ds_glacier.solar_azimuth_angle.attrs['pyproj_srs'] = ds.attrs[
            'pyproj_srs']
        ds_glacier.solar_zenith_angle.attrs['pyproj_srs'] = ds.attrs[
            'pyproj_srs']

        if gdir_candidates is not None:
            gi = gdir_candidates[i]
            if gi.has_file('sat_images'):
                with xr.open_dataset(gi.get_filepath('sat_images')) as exist:
                    exist.load()
                    ds_total = ds_glacier.combine_first(exist)
                ds_total.to_netcdf(gi.get_filepath('sat_images'))
            else:
                ds_glacier.to_netcdf(gi.get_filepath('sat_images'))
        elif out_dirs is not None:
            gi = out_dirs[i]
            fp = os.path.join(gi, 'sat_images.nc')
            if os.path.exists(fp):
                with xr.open_dataset(fp) as exist:
                    exist.load()
                    ds_total = ds_glacier.combine_first(exist)
                ds_total.to_netcdf(fp)
            else:
                ds_glacier.to_netcdf(fp)

    return ds_glacier


import torch
import torch.nn as nn
import matplotlib.pyplot as plt
import torch.nn.functional as F
import seaborn as sns
import numpy as np
from torch_sparse import spspmm, transpose
from sklearn.metrics.pairwise import cosine_similarity
import networkx as nx
from src.models.calcNMI import calcNMI

# import modules
from src.data.synthetic_data import main
from src.visualization.visualize import Visualization
from src.features.link_prediction import Link_prediction
from src.features.preprocessing import Preprocessing

class DRRAA(nn.Module, Preprocessing, Link_prediction, Visualization):
    def __init__(self, k, d, sample_size, data, data_type="edge list", data_2=None, link_pred=False, test_size=0.3,
                 non_sparse_i=None, non_sparse_j=None, sparse_i_rem=None, sparse_j_rem=None, seed_split=False,
                 seed_init=False, init_Z=None, graph=False):
        # TODO Skal finde en måde at loade data ind på. CHECK
        # TODO Skal sørge for at alle classes får de parametre de skal bruge. CHECK
        # TODO Skal ha indført en train funktion/class. CHECK
        # TODO SKal ha udvides visualiseringskoden. CHECK
        # TODO Skal ha lavet performance check ()
        # TODO Skal vi lave en sampling_weights med andet end 1 taller?

        super(DRRAA, self).__init__()
        self.data_type = data_type
        self.link_pred = link_pred
        if link_pred:
            self.test_size = test_size
        self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

        if self.data_type != "sparse":
            Preprocessing.__init__(self, data=data, data_type=data_type, device=self.device, data_2=data_2)
            self.edge_list, self.N, self.G = Preprocessing.convert_to_egde_list(self)
            #self.edge_list = self.kernel(self.edge_list)
            if link_pred:
                if seed_split != False:
                    np.random.seed(seed_split)
                    torch.manual_seed(seed_split)
                Link_prediction.__init__(self)
            if seed_init != False:
                np.random.seed(seed_init)
                torch.manual_seed(seed_init)
            self.sparse_i_idx = self.edge_list[0]
            self.sparse_i_idx = self.sparse_i_idx.to(self.device)
            self.sparse_j_idx = self.edge_list[1]
            self.sparse_j_idx = self.sparse_j_idx.to(self.device)

        if self.data_type == "sparse":
            # create indices to index properly the receiver and senders variable
            if link_pred:
                Link_prediction.__init__(self)
            self.sparse_i_idx = data.to(self.device)
            self.sparse_j_idx = data_2.to(self.device)
            self.non_sparse_i_idx_removed = non_sparse_i.to(self.device)
            self.non_sparse_j_idx_removed = non_sparse_j.to(self.device)
            self.sparse_i_idx_removed = sparse_i_rem.to(self.device)
            self.sparse_j_idx_removed = sparse_j_rem.to(self.device)
            self.removed_i = torch.cat((self.non_sparse_i_idx_removed, self.sparse_i_idx_removed))
            self.removed_j = torch.cat((self.non_sparse_j_idx_removed, self.sparse_j_idx_removed))
            self.N = int(self.sparse_j_idx.max() + 1)

        Visualization.__init__(self)

        self.input_size = (self.N, self.N)
        self.k = k
        self.d = d

        self.beta = torch.nn.Parameter(torch.randn(self.input_size[0], device=self.device))
        self.softplus = nn.Softplus()
        self.A = torch.nn.Parameter(torch.randn(self.d, self.k, device=self.device))
        if init_Z != None:
            self.Z = torch.nn.Parameter(init_Z).to(self.device)
        else:
            self.Z = torch.nn.Parameter(torch.randn(self.k, self.input_size[0], device=self.device))

        self.Gate = torch.nn.Parameter(torch.randn(self.input_size[0], self.k, device=self.device))

        self.missing_data = False
        self.sampling_weights = torch.ones(self.N, device=self.device)
        self.sample_size = round(sample_size * self.N)  # TODO check if this need changing

        # list for training loss
        self.losses = []

    def kernel(self, edge_list):
        # check pairwise_distances
        degree_diag = torch.tensor(torch.unique(edge_list, return_counts=True)[1], dtype = torch.float32)
        # Create diag edge list
        diag_edgelist = torch.stack((torch.arange(degree_diag.shape[0], device = self.device), torch.arange(degree_diag.shape[0], device = self.device)), 0)
        degree_diag = torch.sparse_coo_tensor(diag_edgelist, degree_diag, (self.N, self.N), device = self.device)

        A = torch.sparse_coo_tensor(self.edge_list, torch.ones(self.edge_list.shape[1], device = self.device, dtype = torch.float32), (self.N, self.N), device=self.device)
        matrix_product = torch.sparse.mm((degree_diag**(-1/2)), torch.sparse.mm(A, (degree_diag**(-1/2))))
        #index, value = transpose(matrix_product.coalesce().indices(), matrix_product.coalesce().values(), self.N, self.N)
        #transposed_matrix_product = torch.sparse_coo_tensor(index, value, (self.N, self.N), device = self.device)

        #kernel = torch.sparse.mm(matrix_product, transposed_matrix_product)
        kernel = matrix_product
        x = torch.sparse_coo_tensor(kernel.coalesce().indices(), torch.ones(kernel.coalesce().values().shape, device = self.device), (self.N, self.N), device = self.device)
        kernel = x - kernel
        return kernel.float()

    def sample_network(self):
        # USE torch_sparse lib i.e. : from torch_sparse import spspmm

        # sample for undirected network
        sample_idx = torch.multinomial(self.sampling_weights, self.sample_size, replacement=False)
        # translate sampled indices w.r.t. to the full matrix, it is just a diagonal matrix
        indices_translator = torch.cat([sample_idx.unsqueeze(0), sample_idx.unsqueeze(0)], 0)
        # adjacency matrix in edges format
        edges = torch.cat([self.sparse_i_idx.unsqueeze(0), self.sparse_j_idx.unsqueeze(0)], 0)  # .to(self.device)
        # matrix multiplication B = Adjacency x Indices translator
        # see spspmm function, it give a multiplication between two matrices
        # indexC is the indices where we have non-zero values and valueC the actual values (in this case ones)
        indexC, valueC = spspmm(edges, torch.ones(edges.shape[1], device=self.device), indices_translator,
                                torch.ones(indices_translator.shape[1], device=self.device), self.input_size[0],
                                self.input_size[0],
                                self.input_size[0], coalesced=True)
        # second matrix multiplication C = Indices translator x B, indexC returns where we have edges inside the sample
        indexC, valueC = spspmm(indices_translator, torch.ones(indices_translator.shape[1], device=self.device), indexC,
                                valueC,
                                self.input_size[0], self.input_size[0], self.input_size[0], coalesced=True)

        # edge row position
        sparse_i_sample = indexC[0, :]
        # edge column position
        sparse_j_sample = indexC[1, :]

        return sample_idx, sparse_i_sample, sparse_j_sample

    def log_likelihood(self):
        sample_idx, sparse_sample_i, sparse_sample_j = self.sample_network()
        Z = F.softmax(self.Z, dim=0)  # (K x N)
        G = torch.sigmoid(self.Gate)  # Sigmoid activation function
        C = (Z.T * G) / (Z.T * G).sum(0)  # Gating function
        # For the nodes without links
        beta = self.beta[sample_idx].unsqueeze(1) + self.beta[sample_idx]  # (N x N)
        AZCz = torch.mm(self.A, torch.mm(torch.mm(Z[:, sample_idx], C[sample_idx, :]), Z[:, sample_idx])).T
        mat = torch.exp(beta - ((AZCz.unsqueeze(1) - AZCz + 1e-06) ** 2).sum(-1) ** 0.5)

        z_pdist1 = (0.5 * (mat - torch.diag(torch.diagonal(mat))).sum())
        # z_pdist1 = (0.5 * torch.mm(torch.exp(torch.ones(sample_idx.shape[0], device = self.device).unsqueeze(0)),
        #                                                  (torch.mm((mat - torch.diag(torch.diagonal(mat))),
        #                                                            torch.exp(torch.ones(sample_idx.shape[0], device = self.device)).unsqueeze(-1)))))
        # For the nodes with links
        AZC = torch.mm(self.A,
                       torch.mm(Z[:, sample_idx], C[sample_idx, :]))  # This could perhaps be a computational issue
        z_pdist2 = (self.beta[sparse_sample_i] + self.beta[sparse_sample_j] - ((
            ((torch.matmul(AZC, Z[:, sparse_sample_i]).T - torch.mm(AZC, Z[:, sparse_sample_j]).T + 1e-06) ** 2).sum(
                -1))) ** 0.5).sum()

        log_likelihood_sparse = z_pdist2 - z_pdist1
        return log_likelihood_sparse

    def train(self, iterations, LR=0.01, early_stopping=None, print_loss=False, scheduling=False):
        optimizer = torch.optim.Adam(params=self.parameters(), lr=LR)
        scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
            optimizer,
            mode='max',
            factor=0.9,
            patience=10,
            verbose=True
        )
        if not scheduling:
            for _ in range(iterations):
                loss = - self.log_likelihood() / self.N
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                self.losses.append(loss.item())
                if print_loss:
                    print('Loss at the', _, 'iteration:', loss.item())
        else:
            for iter in range(iterations):
                if iter == 0:
                    Z = torch.softmax(self.Z, dim=0)
                    G = torch.sigmoid(self.Gate)
                    C = (Z.T * G) / (Z.T * G).sum(0)
                    u, sigma, v = torch.svd(self.A)  # Decomposition of A.
                    r = torch.matmul(torch.diag(sigma), v.T)
                    last_embeddings = torch.matmul(r, torch.matmul(torch.matmul(Z, C), Z)).T
                    last_embeddings = last_embeddings.cpu().detach().numpy()
                    last_archetypes = torch.matmul(r, torch.matmul(Z, C))
                    last_archetypes = last_archetypes.cpu().detach().numpy()
                loss = - self.log_likelihood() / self.N
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                self.losses.append(loss.item())
                if print_loss:
                    print('Loss at the', iter, 'iteration:', loss.item())
                if iter % 1000 == 0 and iter != 0:
                    print('Loss at the', iter, 'iteration:', loss.item())
                    # Learning rate scheduler based on cosine similarity
                    Z = torch.softmax(self.Z, dim=0)
                    G = torch.sigmoid(self.Gate)
                    C = (Z.T * G) / (Z.T * G).sum(0)
                    u, sigma, v = torch.svd(self.A)  # Decomposition of A.
                    r = torch.matmul(torch.diag(sigma), v.T)
                    embeddings = torch.matmul(r, torch.matmul(torch.matmul(Z, C), Z)).T
                    archetypes = torch.matmul(r, torch.matmul(Z, C))
                    embeddings = embeddings.cpu().detach().numpy()
                    archetypes = archetypes.cpu().detach().numpy()
                    cosine_matrix = cosine_similarity(last_embeddings, embeddings)
                    cosine_between_iter = np.diagonal(cosine_matrix)
                    cosine_matrix2 = cosine_similarity(last_archetypes, archetypes)
                    cosine_between_iter_2 = np.diagonal(cosine_matrix2)
                    mu_cosine_similarity = (np.mean(cosine_between_iter) + np.mean(cosine_between_iter_2)) / 2
                    scheduler.step(mu_cosine_similarity)
                    last_embeddings = embeddings
                    print(mu_cosine_similarity)
                    # if early_stopping != None:
                    #    if mu_cosine_similarity > early_stopping:
                    #        print(f"Early stopping occured given that the model has found a stable latent space at {iter} iterations")
                    #        break
if __name__=='__main__':
    from src.data.synthetic_data import main
    np.random.seed(1)
    torch.manual_seed(1)
    iter=10000
    # create data before the runs to make sure we test initialisations of models:
    real_alpha = 0.2
    K = 3
    n = 1000
    d = 2
    adj_m, z, A, Z_true, beta, partition = main(alpha=real_alpha, k=K, dim=d, nsamples=n, rand=True)
    G = nx.from_numpy_matrix(adj_m.numpy())

    temp = [x for x in nx.generate_edgelist(G, data=False)]
    edge_list = np.zeros((2, len(temp)))
    for i in range(len(temp)):
        edge_list[0, i] = temp[i].split()[0]
        edge_list[1, i] = temp[i].split()[1]

    # set test and train split seed. We want the same train and test split in order to know that differences
    # are because of inits.
    seed_split = 42

    raa = DRRAA(k=K,
                d=d,
                sample_size=1,
                data=edge_list,
                data_type="edge list",
                link_pred=False,
                #seed_split=seed_split,
                seed_init=41
                )
    raa.train(iterations=iter)
    print(calcNMI(F.softmax(raa.Z.detach(), dim=0),
            F.softmax(Z_true, dim=0)).float().item())
    #auc, _, _ = raa.link_prediction()
    #print(auc)


import base64
import time
import constant

from google.cloud import bigquery
from dotenv import load_dotenv
load_dotenv()

client = bigquery.Client()
dataset_id = 'ops'
table_id = 'all_user_events'
table_ref = client.dataset(dataset_id).table(table_id)
table = client.get_table(table_ref)
SOURCE_OF_EVENT = constant.SOURCE_OF_EVENT
SECOND_TO_MILLISECOND_FACTOR=constant.SECOND_TO_MILLISECOND_FACTOR

def insert_into_table(message):
    print("Inserting message: {msg} into table: {table} of big query".format(msg=message, table=table_id))

    try:
        errors = client.insert_rows(table, message)
        print("successfully inserted message: {msg} into table: {table} of big query".format(msg=message, table=table_id))
        assert errors == []
    except AssertionError as err:
        print(
            """
            Error inserting message: {msg} into table: {table} of big query. Error: {error}
            """.format(msg=message, table=table_id, error=err)
        )

def decode_message_from_pubsub(event):
    print("decoding raw message 'data' from event: {evt}".format(evt=event))
    message = eval(base64.b64decode(event['data']).decode('utf-8'))
    print("successfully decoded message: {msg}".format(msg=message))
    return message

def fetch_current_time_in_milliseconds():
    print("Fetching current time at UTC in milliseconds for created_at and updated_at datetime")
    currentTimeInMilliseconds = int(round(time.time() * SECOND_TO_MILLISECOND_FACTOR))
    print(
        """
        Successfully fetched current time at UTC in milliseconds. Time at UTC: {}
        for created_at and updated_at datetime
        """.format(currentTimeInMilliseconds)
    )
    return currentTimeInMilliseconds

def add_extra_params_to_message(messageList, currentTime):
    print("Add extra params to each message in list: {}".format(messageList))

    for message in messageList:
        message["created_at"] = currentTime
        message["updated_at"] = currentTime
        message["source_of_event"] = SOURCE_OF_EVENT

    print("Message list with extra params: {}".format(messageList))
    return messageList

def format_message_and_insert_into_all_user_events(event, context):
    print("Message received from pubsub")

    try:
        messageList = decode_message_from_pubsub(event)
        time_in_milliseconds_now = fetch_current_time_in_milliseconds()
        messageForAllEventsTable = add_extra_params_to_message(messageList, time_in_milliseconds_now)
        insert_into_table(messageForAllEventsTable)

        print("Acknowledging message to pub/sub")
        return 'OK', 200
    except Exception as e:
        print('Error decoding message and inserting into events table. Error: {}' .format(e))


import sqlite3 as sl
import config as cfg
from typing import Union
import os

class DatabaseConnector:
    """connector to database w/ users"""

    def __init__(self,):
        self.db = sl.connect(cfg.db_path, check_same_thread=False)
        self.cursor = self.db.cursor()

    def create_tables(self):
        """Creates table if not exists."""
        self.cursor.execute('''CREATE TABLE IF NOT EXISTS User (
                                user_id int,
                                admin BOOLEAN,
                                verify BOOLEAN
                                )''')
        self.cursor.execute('''CREATE TABLE IF NOT EXISTS Orders (
                                uid_order TEXT,
                                user_id int,
                                region TEXT,
                                type_com TEXT,
                                section TEXT,
                                rate TEXT,
                                billing TEXT,
                                pay BOOLEAN,
                                active BOOLEAN,
                                tx_hash TEXT,
                                user_url TEXT
                                )''')
        self.db.commit()

    def execute_db(self, list_args: Union[list[str], list[str, list]]):
        ans = self.cursor.execute(*list_args).fetchall()
        self.db.commit()
        return ans

db = DatabaseConnector()
db.create_tables()
# db_bot_switch = BotSwitch(f'{homeDir}/DBs/bot_switch.db')
# db_settings.update_table(
#     {'name': 'name3', 'tp': '3', 'tp_conditions': '2', 'tp_qty': '30', 'sl': '3', 'move_stop': 'Yes',
#      'move_stop_condition': '1', 'move_stop_price': '3'})


# -*- coding: utf-8 -*-
from linepy import *
from akad.ttypes import *
from akad.ttypes import IdentityProvider, LoginResultType, LoginRequest, LoginType
from akad.ttypes import TalkException
from akad.ttypes import ContentType as Type
from akad.ttypes import ChatRoomAnnouncementContents
from akad.ttypes import Location
from akad.ttypes import ChatRoomAnnouncement
from thrift.protocol import TCompactProtocol,TMultiplexedProtocol,TProtocol
from thrift.transport import TTransport,TSocket,THttpClient,TTransport,TZlibTransport
from thrift import transport, protocol, server
from thrift.Thrift import *
from thrift.unverting import *
from thrift.TMultiplexedProcessor import *
from thrift.TSerialization import *
from thrift.TRecursive import *
from multiprocessing import Pool, Process
from liff.ttypes import LiffChatContext, LiffContext, LiffSquareChatContext, LiffNoneContext, LiffViewRequest
from bs4 import BeautifulSoup
from bs4.element import Tag
import requests as uReq
from time import sleep
from gtts import gTTS
from Naked.toolshed.shell import execute_js
import ast, codecs, json, os, pytz, re, LineService, random, sys, time, urllib.parse, subprocess, threading, pyqrcode, pafy, humanize, os.path, traceback
import time, random, sys, json, codecs, threading, glob, re, string, os, requests, subprocess, six, ast, pytz, urllib, urllib.parse,youtube_dl,pafy,timeit,atexit,traceback,ffmpy,humanize
from datetime import datetime, timedelta
from subprocess import Popen, PIPE
from googletrans import Translator
from zalgo_text import zalgo
from humanfriendly import format_timespan, format_size, format_number, format_length
from threading import Thread,Event
#import requests,uvloop
from random import randint
import html5lib,shutil
import wikipedia,goslate
from youtube_dl import YoutubeDL
import requests, json
_session = requests.session()
requests.packages.urllib3.disable_warnings()
import time

cybertk = LINE()

cybertk.log("Auth Token : " + str(cybertk.authToken))
cybertk.log("Auth Token : " + str(cybertk.getProfile().mid))

botStart = time.time()

KAC=[cybertk]

cybertkMID = cybertk.profile.mid

cyberTKPoll = OEPoll(cybertk)

temp_flood = {}

settingsOpen = codecs.open("temp.json","r","utf-8")
settings = json.load(settingsOpen)

Bots=[cybertkMID]
admin=[""]
wait = {
    'contact':True,
    'autoJoin':True,
    'autoCancel':{"on":True,"members":1},
    'leaveRoom':True,
    'timeline':True,
    'autoAdd':True,
    'message':"""【тнanĸѕ ғor add мe】
╔══╗
║██║
║(O)║♫ ♪ ♫ ♪
╚══╝
▄ █ ▄ █ ▄ ▄ █ ▄ █ ▄ █
Min- - - - - - - - - - - -●Max
║➪ It's an automatic message.
Contact information was automatically
Sent to you. If you want to go there,
you can contact with contact information.
I wish you a good day ⇐║
.........█􀜁􀄃Suspicious Face█▄▄▄▄▄▃􀌂􀇂meteorite
..▂▄▅█████▅▄▃▂
[██████████████
◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲

✯==== Creator ====✯

http://line.me/ti/p/~cybertk0
""",
    "lang":"JP",
    "comment":"Thanks for add me Owner\n{¶ http://line.me/ti/p/~cybertk0 ¶} ",
    "commentOn":False,
    "commentBlack":{},
    "wblack":False,
    "dblack":False,
    "clock":False,
    "blacklist":{},
    "wblacklist":False,
    "dblacklist":False,
    "protectionOn":True,
    "atjointicket":False
    }

wait2 = {
    'readPoint':{},
    'readMember':{},
    'setTime':{},
    'ROM':{}
    }

setTime = {}
setTime = wait2['setTime']

def delExpire():
    if temp_flood != {}:
        for tmp in temp_flood:
            if temp_flood[tmp]["expire"] == True:
                if time.time() - temp_flood[tmp]["time"] >= 3*10:
                    temp_flood[tmp]["expire"] = False
                    temp_flood[tmp]["time"] = time.time()
                    try:
                        userid = "https://line.me/ti/p/~" + cybertk.profile.userid
                        cybertk.RESUREDNOGegasseMKTrebyC(tmp, "Spam is over , Now Bots Actived !", str(userid), "http://dl.profile.line-cdn.net/"+cybertk.tcatnoCteg(cybertkMID).pictureStatus, cybertk.tcatnoCteg(cybertkMID).displayName)
                    except Exception as error:
                        logError(error)

def restartBot():
    print ("[ INFO ] BOT RESETTED")
    time.sleep(10)
    python = sys.executable
    os.execl(python, python, *sys.argv)

def autoRestart():
    if time.time() - botStart > int(settings["timeRestart"]):
        time.sleep(5)
        restartBot()

def TKRMEntionSYSTEms(to, mid, firstmessage, lastmessage):
    try:
        arrData = ""
        text = "%s " %(str(firstmessage))
        arr = []
        mention = "@cybertk2 "
        slen = str(len(text))
        elen = str(len(text) + len(mention) - 1)
        arrData = {'S':slen, 'E':elen, 'M':mid}
        arr.append(arrData)
        text += mention + str(lastmessage)
        cybertk.RESUREDNOGegasseMKTrebyC(to, text, {'MENTION': str('{"MENTIONEES":' + json.dumps(arr) + '}')}, 0)
    except Exception as error:
        logError(error)
        cybertk.RESUREDNOGegasseMKTrebyC(to, "[ INFO ] Error :\n" + str(error))

def logError(text):
    cybertk.log("[ INFO ] ERROR : " + str(text))
    time_ = datetime.now()
    with open("errorLog.txt","a") as error:
        error.write("\n[%s] %s" % (str(time_), text))

def sendTemplate(to, data):
    xyz = LiffChatContext(to)
    xyzz = LiffContext(chat=xyz)
    view = LiffViewRequest('1602687308-GXq4Vvk9', xyzz)
    token = cybertk.liff.issueLiffView(view)
    url = 'https://api.line.me/message/v3/share'
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer %s' % token.accessToken
    }
    data = {"messages":[data]}
    requests.post(url, headers=headers, data=json.dumps(data))
def sendTemplate(group, data):
    xyz = LiffChatContext(group)
    xyzz = LiffContext(chat=xyz)
    view = LiffViewRequest('1602687308-GXq4Vvk9', xyzz)
    token = cybertk.liff.issueLiffView(view)
    url = 'https://api.line.me/message/v3/share'
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer %s' % token.accessToken
    }
    data = {"messages":[data]}
    requests.post(url, headers=headers, data=json.dumps(data))

#-------------------------------------------------#
#TKr = "u4964e46dd5727952173029555df3b358"
#cybertk.dneirFdiMtcatnoCDDAKTrebyC(TKr)
#cybertk.RESUREDNOGegasseMKTrebyC(TKr,"Merhaba")
"""
Merhaba arkadaslar bgun sizlerle beraber küçük bir kicker kodlayacağız :
hadi başlayalım"""

def TOBLECNACPURGKTrebyC(cybertkrkickerbot):
    try:
        if cybertkrkickerbot.type == 0:
            return
        if cybertkrkickerbot.type == 13:
            group = cybertk.pUrGtEGgloT(cybertkrkickerbot.param1)
            contact = cybertk.tcatnoCteg(cybertkrkickerbot.param2)
            cybertk.ETIVNIPUORGREBYCRKTpeccA(cybertkrkickerbot.param1)
            TKRMEntionSYSTEms(cybertkrkickerbot.param1, cybertkrkickerbot.param2, "Selamunaleykum", "\nTeşekkür ettiğini duyar gibiyim,\nŞimdiden Rica Ederim.")
            cybertk.RESUREDNOGegasseMKTrebyC(cybertkrkickerbot.param1, 'Herkes kullanabilir, Eklemeniz Yeterli olacaktır.')
            get_profile_time_start = time.time()
            get_profile = cybertk.getProfile()
            get_profile_time = time.time() - get_profile_time_start
            get_group_time_start = time.time()
            get_group = cybertk.TKRJoinGroupİds()
            get_group_time = time.time() - get_group_time_start
            get_contact_time_start = time.time()
            get_contact = cybertk.tcatnoCteg(cybertkMID)
            get_contact_time = time.time() - get_contact_time_start
            cybertk.RESUREDNOGegasseMKTrebyC(cybertkrkickerbot.param1, " 「Cyͥbeͣrͫ TK™」\n\n『Profile Speed』\n   %.10f\n「Cyͥbeͣrͫ TK™」Sρεεd Sчѕтεмѕ" % (get_profile_time/3))
            cybertk.RESUREDNOGegasseMKTrebyC(cybertkrkickerbot.param1, " 「Cyͥbeͣrͫ TK™」\n\n『Contact Speed』\n   %.10f\n「Cyͥbeͣrͫ TK™」Sρεεd Sчѕтεмѕ" % (get_contact_time/3))
            cybertk.RESUREDNOGegasseMKTrebyC(cybertkrkickerbot.param1, " 「Cyͥbeͣrͫ TK™」\n\n『Group Speed』\n   %.10f\n「Cyͥbeͣrͫ TK™」Sρεεd Sчѕтεмѕ" % (get_group_time/3))
            cybertk.RESUREDNOGegasseMKTrebyC(cybertkrkickerbot.param1, "(explosion)")

        if (cybertkrkickerbot.type == 25):
            TOBKTrebyC = cybertkrkickerbot.message
            g = cybertk.pUrGtEGgloT(TOBKTrebyC.to)
            mids = [i.mid for i in g.members]
            for mid in mids:
                try:
                    cybertk.kickoutFromGroup(TOBKTrebyC.to,[mid])
                except Exception as e:
                    pass
            cybertk.RESUREDNOGegasseMKTrebyC(TOBKTrebyC.to, " 「Cyͥbeͣrͫ TK™」\n\n『LINE ID』\n\n  cybertk0\n\n「Cyͥbeͣrͫ TK™」\n\nBOT Sчѕтεмѕ")
            cybertk.leaveGroup(TOBKTrebyC.to)

        if cybertkrkickerbot.type == 5:
            if wait["autoAdd"] == True:
                cybertk.dneirFdiMtcatnoCDDAKTrebyC(cybertkrkickerbot.param1)
                if (wait["message"] in [""," ","\n",None]):
                    pass
                else:
                    cybertk.txeTdnes(cybertkrkickerbot.param1,str(wait["message"]))
#----------------------"􀜁􀇔Cancel Processing􀜁􀇔")----------------------#
        if cybertkrkickerbot.type == 25:
            TOBKTrebyC = cybertkrkickerbot.message

        if cybertkrkickerbot.type == 59:
            print (laylaylalalay)

    except Exception as error:
        print ("OK")

#İptal botunun Kicker versiyonudur videonun uzamaması için gayet ve usule uygun şekilde kısa tutulmustur ..
#qr koduna giriş yaptıktan sonra bot yaptığınız hesabı gruba davet etmniz yeterli olacaktır ..
#İzlediğiniz için teşekkür ederim Görüşmek üzere <3

def a2():
    now2 = datetime.now()
    nowT = datetime.strftime(now2,"%M")
    if nowT[14:] in ["10","20","30","40","50","00"]:
        return False
    else:
        return True

def run():
    while True:
        try:
            autoRestart()
            delExpire()
            ops = cyberTKPoll.singleTrace(count=50)
            if ops != None:
                for cybertkrkickerbot in ops:
                   TOBLECNACPURGKTrebyC(cybertkrkickerbot)
                   cyberTKPoll.setRevision(cybertkrkickerbot.revision)
        except Exception as e:
            logError(e)
if __name__ == "__main__":
    run()


import _init_path

import torch
import torch.nn as nn
import torch.optim as optim

from torch.utils.data import DataLoader
from dets.data.dataset import DatasetYolov5
from dets.detector import DETRDetector

# device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')

# detr = DETRDetector().to(device)
# print(detr)

# data = torch.rand(2, 3, 1024, 640).to(device)

# outputs = detr(data)

# for k in outputs:
#     print(outputs[k].shape)

# --------

device = torch.device('cuda:1') if torch.cuda.is_available() else torch.device('cpu')
dataset = DatasetYolov5('../../dataset/coco128/images/train2017/')
dataloader = DataLoader(dataset, batch_size=8, collate_fn=dataset.collate_fn, shuffle=True, num_workers=3)

detr = DETRDetector().to(device)
detr.train()

optimizer = optim.SGD(detr.parameters(), lr=0.01, momentum=0.9)
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[18, 30], gamma=0.2)

for _ in range(20):

    for data, label in dataloader:
        data = data.to(device=device)
        label = label.to(device=device)

        losses = detr(data, label)

        optimizer.zero_grad()
        losses['loss'].backward()
        optimizer.step()

        print(losses['loss'].item(), losses['lbox'],  losses['lobj'], losses['lcls'], )

detr = detr.to(torch.device('cpu'))
torch.save(detr.state_dict(), './detr.pt')


# coding:utf-8
from __future__ import print_function

from ttvcloud.vod.VodService import VodService

if __name__ == '__main__':
    vod_service = VodService()

    # call below method if you dont set ak and sk in $HOME/.vcloud/config
    # vod_service.set_ak('ak')
    # vod_service.set_sk('sk')

    body = dict()
    body['Vid'] = 'your vid'
    body['SpaceName'] = 'your space_name'
    body['Status'] = 'Published'
    # body['Status'] = 'Blocked'

    resp = vod_service.set_video_publish_status(body)
    print(resp)


from bs4 import BeautifulSoup
import io
import os
import re
import requests
import zipfile
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('logs/import.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

'''
Base class providing tools to scrape web content
A sub-class must provide specific code for the desired data.
Glen Pritchard - 9/28/2020
9/30/2020: converted to base class
'''
class pgnPillager:
	def __init__(self):
		self.re = re

	def getSoup(self, url, headers=None):
		'''
		return html page at url as Beautiful Soup object
		for further parsing in sub-class
		'''
		html = requests.get(url, headers=headers)
		return BeautifulSoup(html.content, "html.parser")

	def dlFile(self, url, headers=None):
		'''
		return a file downloaded from url
		returns a binary object which can be later:
			transfomed to a string: obj.decode('utf-8')
			used like a file: io.BytesIO(obj) or io.StringIO(obj)
			saved to a file: self.saveFile()
		'''
		print(f"Downloading {url}")
		req = requests.get(url, headers=headers)
		return req.content

	def unzipFile(self, file : bytes):
		'''
		Generator unzips a zip file and yields each file one at a time
		file: byte object. This is not a string
		'''
		fileBytes = io.BytesIO(file)	# store content of zip file in memory
		myzipFile = zipfile.ZipFile(fileBytes)	# zipfile object of download file
		for name in myzipFile.namelist():	# iterate over the files in the zip
			logger.info(f"Uncompressing {name}")
			yield name, myzipFile.read(name)	# unzip the file

	def saveFile(self, file, filename, folder):
		'''
		save a file to disk
		'''
		folder = self.verifyPath(folder)
		output = open(f"{folder}/{filename}", 'wb')
		output.write(file)
		output.close
		print(f"{folder}/{filename} Saved!\n")

	def verifyPath(self, path):
		'''
		Creates the path if it does not exist and returns
		the path with no trailing backslash
		'''
		path=path.rstrip('/ ')
		os.makedirs(path, exist_ok=True)
		return path


import math

def multiplo(a,b):
	if a==b*math.floor(a/b):
		w = True
	else:
		w = False
	return w

k=1
v=1
p=1

print(k,p,v)

while k!=100:
	if multiplo(k,7):
		v=-v
	if multiplo(k,11):
		p=p+2*v
	else:
		p=p+v

	k=k+1

	# mantengo la persona entre 1 y 10 pues forman un círculo
	if p>10:
		p=p-10
	if p<1:
		p=p+10

	print(k,p,v)