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

' a module for some simple but important processes '

__author__ = 'Zitong Lu'

import nibabel as nib
import numpy as np
import os
import math
from scipy.stats import spearmanr, pearsonr, kendalltau, ttest_1samp, ttest_rel
import sys

# get package abspath
package_root = os.path.dirname(os.path.abspath(__file__))

' a function for zeroing the value close to zero '

def limtozero(x):

    """
    zero the value close to zero

    Parameters
    ----------
    x : float

    Returns
    -------
    0
    """

    if x < 1e-15:
        x = 0

    return x

' a function for getting the affine of the fMRI-img '

def get_affine(file_name):

    """
    get the affine of the fMRI-img

    Parameters
    ----------
    file_name : string
        The filename of a sample fMRI-img in your experiment

    Returns
    -------
    affine : array
        The position information of the fMRI-image array data in a reference space.
    """

    if file_name is "":

        return "Invalid input"

    img = nib.load(file_name)

    return img.affine

' a function for Fisher-Z transform of a RDM'

def fisherz_rdm(rdm):

    """
    Fisher-Z transform of a RDM

    Parameters
    ----------
    rdm : array or list [n_cons, n_cons]
        A representational dissimilarity matrix.

    Returns
    -------
    newrdm : array or list [n_cons, n_cons]
        A representational dissimilarity matrix after Fisher-Z transform.
    """

    if len(np.shape(rdm)) != 2 or np.shape(rdm)[0] != np.shape(rdm)[1]:

        return "Invalid input!"

    ncons = np.shape(rdm)[0]

    newrdm = np.zeros([ncons, ncons], dtype=np.float)

    # ignore the diagonal
    for i in range(ncons):
        for j in range(ncons):
            if i != j:
                newrdm[i, j] = 0.5*np.log((1+rdm[i, j])/(1-rdm[i, j]))

    return newrdm

' a function for FWE-correction for fMRI RSA results '

def fwe_correct(p, p_threshold):

    """
    FWE correction for fMRI RSA results

    Parameters
    ----------
    p : array
        The p-value map (3-D).
    p_threshold: string
        The p threshold.

    Returns
    -------
    correctp : array.
        The FDR corrected p-value map.
    """

    px = np.shape(p)[0]
    py = np.shape(p)[1]
    pz = np.shape(p)[2]

    n = 0

    for i in range(px):
        for j in range(py):
            for k in range(pz):

                if (math.isnan(p[i, j, k]) == False):
                    n = n + 1

    fwep = p*n

    print("finished FWE correct")

    return fwep

' a function for FDR-correction for fMRI RSA results '

def fdr_correct(p, p_threshold):

    """
    FDR correction for fMRI RSA results

    Parameters
    ----------
    p : array
        The p-value map (3-D).
    p_threshold: string
        The p threshold.

    Returns
    -------
    correctp : array.
        The FDR corrected p-value map.
    """

    px = np.shape(p)[0]
    py = np.shape(p)[1]
    pz = np.shape(p)[2]

    n = 0

    for i in range(px):
        for j in range(py):
            for k in range(pz):

                if (math.isnan(p[i, j, k]) == False):

                    n = n + 1

    fdrp = np.full([px, py, pz], np.nan)

    pcluster = np.zeros([n], dtype=np.float)

    m = 0

    for i in range(px):
        for j in range(py):
            for k in range(pz):

                if (math.isnan(p[i, j, k]) == False):
                    pcluster[m] = p[i, j, k]
                    m = m + 1

    index = np.argsort(pcluster)

    for l in range(n):
        pcluster[index[l]] = float(pcluster[index[l]]*n/(l+1))

    """for l in range(n - 1):
        if pcluster[index[-l - 1]] < pcluster[index[-l - 2]]:
            pcluster[index[-l - 2]] = pcluster[index[-l - 1]]"""

    m = 0

    for i in range(px):
        for j in range(py):
            for k in range(pz):

                if (math.isnan(p[i, j, k]) == False) and (p[i, j, k] < p_threshold):
                    fdrp[i, j, k] = pcluster[m]
                    m = m + 1

    print("finished FDR correct")

    return fdrp

"""def fdr_correct(p, size=[60, 60, 60], n=64, type="sphere"):

    x = size[0]
    y = size[1]
    z = size[2]

    px = np.shape(p)[0]
    py = np.shape(p)[1]
    pz = np.shape(p)[2]

    if type == "cube":

        n = float(n*px*py*pz/(x*y*z))

        nq = 1
        ni = 1

        while nq < n:
            ni = ni + 1
            nq = ni*ni*ni

        n = nq

        print(n, ni)

        fdrp = np.full([px, py, pz], np.nan)

        for i in range(px-ni+1):
            for j in range(py-ni+1):
                for k in range(pz-ni+1):

                    pcluster = p[i:i+ni, j:j+ni, k:k+ni]
                    pcluster = np.reshape(pcluster, [n])

                    index = np.argsort(pcluster)

                    for l in range(n):
                        pcluster[index[l]] = float(pcluster[index[l]]*n/(l+1))

                    for l in range(n-1):
                        if pcluster[index[-l-1]] < pcluster[index[-l-2]]:
                            pcluster[index[-l-2]] = pcluster[index[-l-1]]

                    newpcluster = np.full([n], np.nan)

                    for l in range(n):
                        newpcluster[l] = pcluster[index[l]]

                    fdrp[i:i+ni, j:j+ni, k:k+ni] = np.reshape(newpcluster, [ni, ni, ni])

        print("finished FDR correct")

        return fdrp

    elif type == "sphere":

        ni = int(nr * px * py * pz / (x * y * z)) + 1

        c0 = [int(ni), int(ni), int(ni)]
        n0 = 0
        for i in range(int(2 * ni + 1)):
            for j in range(int(2 * ni + 1)):
                for k in range(int(2 * ni + 1)):
                    dist = np.square(i - c0[0]) + np.square(j - c0[1]) + np.square(k - c0[2])
                    if dist <= ni * ni:
                        # print(i, j)
                        n0 = n0 + 1

        n = n0
        # print(c0)
        # print(n0)
        print(n, ni)

        fdrp = np.full([px, py, pz], np.nan)

        for i in range(px - 2 * ni):
            for j in range(py - 2 * ni):
                for k in range(pz - 2 * ni):

                    nindex = 0

                    pcluster = np.zeros([n], dtype=np.float)

                    for lx in range(2 * ni + 1):
                        for ly in range(2 * ni + 1):
                            for lz in range(2 * ni + 1):
                                dist = np.square(lx - c0[0]) + np.square(ly - c0[1]) + np.square(lz - c0[2])
                                if dist <= ni * ni:
                                    pcluster[nindex] = p[i + lx, j + ly, k + lz]
                                    nindex = nindex + 1

                    index = np.argsort(pcluster)

                    for l in range(n):
                        pcluster[index[l]] = float(pcluster[index[l]] * n / (l + 1))

                    for l in range(n - 1):
                        if pcluster[index[-l - 1]] < pcluster[index[-l - 2]]:
                            pcluster[index[-l - 2]] = pcluster[index[-l - 1]]

                    newpcluster = np.full([n], np.nan)

                    for l in range(n):
                        newpcluster[l] = pcluster[index[l]]

                    nindex = 0

                    for lx in range(2 * ni + 1):
                        for ly in range(2 * ni + 1):
                            for lz in range(2 * ni + 1):
                                dist = np.square(lx - c0[0]) + np.square(ly - c0[1]) + np.square(lz - c0[2])
                                if dist <= ni * ni:
                                    fdrp[i + lx, j + ly, k + lz] = newpcluster[nindex]
                                    nindex = nindex + 1

        print("finished FDR correct")

        return fdrp"""

' a function for fMRI RSA results correction by threshold '

def correct_by_threshold(img, threshold):

    """
    correct the fMRI RSA results by threshold

    Parameters
    ----------
    img : array
        A 3-D array of the fMRI RSA results.
        The shape of img should be [nx, ny, nz]. nx, ny, nz represent the shape of the fMRI-img.
    threshold : int
        The number of voxels used in correction.
        If threshold=n, only the similarity clusters consisting more than n voxels will be visualized.

    Returns
    -------
    img : array
        A 3-D array of the fMRI RSA results after correction.
        The shape of img should be [nx, ny, nz]. nx, ny, nz represent the shape of the fMRI-img.
    """

    if len(np.shape(img)) != 3:

        return "Invalid input"

    sx = np.shape(img)[0]
    sy = np.shape(img)[1]
    sz = np.shape(img)[2]

    nsmall = 1

    while nsmall*nsmall*nsmall < threshold:
        nsmall = nsmall + 1

    nlarge = nsmall + 2

    for i in range(sx-nlarge+1):
        for j in range(sy-nlarge+1):
            for k in range(sz-nlarge+1):

                listlarge = list(np.reshape(img[i:i+nlarge, j:j+nlarge, k:k+nlarge], [nlarge*nlarge*nlarge]))
                print(listlarge.count(0))

                if listlarge.count(0) < nlarge*nlarge*nlarge:

                    index1 = 0

                    for l in range(nlarge):
                        for m in range(nlarge):

                            if img[i + l, j + m, k] == 0:
                                index1 = index1 + 1

                            if img[i + l, j + m, k + nlarge - 1] == 0:
                                index1 = index1 + 1

                    for l in range(nlarge-1):
                        for m in range(nlarge-2):

                            if img[i + l, j, k + m] == 0:
                                index1 = index1 + 1

                            if img[i, j + l + 1, k + m] == 0:
                                index1 = index1 + 1

                            if img[i + nlarge - 1, j + l, k + m] == 0:
                                index1 = index1 + 1

                            if img[i + l + 1, j + nlarge - 1, k + m] == 0:
                                index1 = index1 + 1

                    nex = nlarge * nlarge * nlarge - nsmall * nsmall * nsmall
                    print("index1:"+str(index1))

                    if index1 == nex:
                        print("**************************")
                        unit = img[i+1:i+1+nsmall, j+1:j+1+nsmall, k+1:k+1+nsmall]
                        unit = np.reshape(unit, [nsmall*nsmall*nsmall])
                        list_internal = list(unit)
                        index2 = nsmall*nsmall*nsmall-list_internal.count(0)
                        print(index1, index2)

                        if index2 < threshold:
                            img[i+1:i+1+nsmall, j]

                            for l in range(nsmall):
                                for m in range(nsmall):
                                    for p in range(nsmall):
                                        img[i+1:i+1+nsmall, j+1:j+1+nsmall, k+1:k+1+nsmall] = np.zeros([nsmall, nsmall, nsmall])

    print("finished correction")

    return img

' a function for getting ch2.nii.gz '

def get_bg_ch2():

    """
    get ch2.nii.gz

    Returns
    -------
    path : string
        The absolute file path of 'ch2.nii.gz'
    """

    return os.path.join(package_root, 'template/ch2.nii.gz')

' a function for getting ch2bet.nii.gz '

def get_bg_ch2bet():

    """
    get ch2bet.nii.gz

    Returns
    -------
    path : string
        The absolute file path of 'ch2bet.nii.gz'
    """

    return os.path.join(package_root, 'template/ch2bet.nii.gz')

' a function for getting HarvardOxford-cort-maxprob-thr0-1mm.nii.gz '

def get_HOcort():
    """
    get HarvardOxford-cort-maxprob-thr0-1mm.nii.gz

    Returns
    -------
    path : string
        The absolute file path of 'HarvardOxford-cort-maxprob-thr0-1mm.nii.gz'
    """

    return os.path.join(package_root, 'template/HarvardOxford-cort-maxprob-thr0-1mm.nii.gz')

' a function for filtering the data by a ROI mask '

def datamask(fmri_data, mask_data):

    """
    filter the data by a ROI mask

    Parameters
    ----------
    fmri_data : array
        The fMRI data.
        The shape of fmri_data is [nx, ny, nz]. nx, ny, nz represent the size of the fMRI data.
    mask_data : array
        The mask data.
        The shape of mask_data is [nx, ny, nz]. nx, ny, nz represent the size of the fMRI data.

    Returns
    -------
    newfmri_data : array
        The new fMRI data.
        The shape of newfmri_data is [nx, ny, nz]. nx, ny, nz represent the size of the fMRI data.
    """

    if len(np.shape(fmri_data)) != 3 or len(np.shape(mask_data)) != 3:

        return "Invalid input"

    nx, ny, nz = fmri_data.shape

    newfmri_data = np.full([nx, ny, nz], np.nan)

    for i in range(nx):
        for j in range(ny):
            for k in range(nz):

                if (mask_data[i, j, k] != 0) and (math.isnan(mask_data[i, j, k]) is False):
                    newfmri_data[i, j, k] = fmri_data[i, j, k]

    return newfmri_data

' a function for project the position of a point in matrix coordinate system to the position in MNI coordinate system '

def position_to_mni(point, affine):

    """
    project the position in matrix coordinate system to the position in MNI coordinate system

    Parameters
    ----------
    point : list or array
        The position in matrix coordinate system.
    affine : array or list
        The position information of the fMRI-image array data in a reference space.

    Returns
    -------
    newpoint : array
        The position in MNI coordinate system.
    """

    i = point[0]
    j = point[1]
    k = point[2]

    x = affine[0, 3] + i * affine[0, 0] - affine[0, 0]
    y = affine[1, 3] + j * affine[1, 1] - affine[1, 1]
    z = affine[2, 3] + k * affine[2, 2] - affine[2, 2]

    newpoint = np.array([x, y, z])

    return newpoint

' a function for convert data of MNI template to your data template '

def mask_to(mask, size, affine, filename=None):

    """
    convert mask data of certain template to your data template

    Parameters
    ----------
    mask : string
        The file path+filename for the mask of certain template.
    size : array or list [nx, ny, nz]
        The size of the fMRI-img in your experiments.
    affine : array or list
        The position information of the fMRI-image array data in a reference space.
    filename : string. Default is None - 'newmask.nii'.
        The file path+filename for the mask for your data template .nii file.

    Notes
    -----
    A result .nii file of new mask will be generated at the corresponding address of filename.
    """

    data = nib.load(mask).get_fdata()

    nx = data.shape[0]
    ny = data.shape[1]
    nz = data.shape[2]

    print("******")
    print(nx, ny, nz)

    maskaffine = nib.load(mask).affine

    print(maskaffine)
    print(maskaffine[0, 0], maskaffine[1, 1], maskaffine[2, 2])

    newdata = np.zeros(size, dtype=np.float)

    for i in range(nx):
        for j in range(ny):
            for k in range(nz):
                if data[i, j, k] != 0 or math.isnan(data[i, j, k]) is False:
                    mx = maskaffine[0, 3]+(i-1)*maskaffine[0, 0]
                    my = maskaffine[1, 3]+(j-1)*maskaffine[1, 1]
                    mz = maskaffine[2, 3]+(k-1)*maskaffine[2, 2]
                    x = int(float((mx-affine[0, 3])/affine[0, 0]))+1
                    y = int(float((my-affine[1, 3])/affine[1, 1]))+1
                    z = int(float((mz-affine[2, 3])/affine[2, 2]))+1
                    if x < size[0] and y < size[1] and z < size[2]:
                        newdata[x, y, z] = data[i, j, k]

    file = nib.Nifti1Image(newdata, affine)

    if filename == None:
        filename = "newmask.nii"
    else:
        q = ".nii" in filename

        if q == True:
            filename = filename
        else:
            filename = filename+".nii"

    nib.save(file, filename)

    return 0

' a function for permutation test '

def permutation_test(v1, v2, iter=5000):

    """
    Conduct Permutation test

    Parameters
    ----------
    v1 : array
        Vector 1.
    v2 : array
        Vector 2.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    p : float
        The permutation test result, p-value.
    """

    if len(v1) != len(v2):

        return "Invalid input"

    # permutation test

    diff = abs(np.average(v1) - np.average(v2))
    v = np.hstack((v1, v2))
    nv = v.shape[0]
    ni = 0

    for i in range(iter):
        vshuffle = np.random.permutation(v)
        vshuffle1 = vshuffle[:int(nv/2)]
        vshuffle2 = vshuffle[int(nv/2):]
        diff_i = np.average(vshuffle1) - np.average(vshuffle2)

        if diff_i >= diff:
            ni = ni + 1

    # permunitation test p-value
    p = np.float64(ni/iter)

    return p

' a function for permutation test for correlation coefficients '

def permutation_corr(v1, v2, method="spearman", iter=5000):

    """
    Conduct Permutation test for correlation coefficients

    Parameters
    ----------
    v1 : array
        Vector 1.
    v2 : array
        Vector 2.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    p : float
        The permutation test result, p-value.
    """

    if len(v1) != len(v2):

        return "Invalid input"

    # permutation test

    if method == "spearman":

        rtest = spearmanr(v1, v2)[0]

        ni = 0

        for i in range(iter):
            v1shuffle = np.random.permutation(v1)
            v2shuffle = np.random.permutation(v2)
            rperm = spearmanr(v1shuffle, v2shuffle)[0]

            if rperm>rtest:
                ni = ni + 1

    if method == "pearson":
        print(iter)
        rtest = pearsonr(v1, v2)[0]

        ni = 0

        for i in range(iter):
            v1shuffle = np.random.permutation(v1)
            v2shuffle = np.random.permutation(v2)
            rperm = pearsonr(v1shuffle, v2shuffle)[0]

            if rperm>rtest:
                ni = ni + 1

    if method == "kendalltau":

        rtest = kendalltau(v1, v2)[0]

        ni = 0

        for i in range(iter):
            v1shuffle = np.random.permutation(v1)
            v2shuffle = np.random.permutation(v2)
            rperm = kendalltau(v1shuffle, v2shuffle)[0]

            if rperm>rtest:
                ni = ni + 1

    p = np.float64((ni+1)/(iter+1))

    return p

' a function for getting the cluster-index information '

def get_cluster_index_1d_1sided(m):

    """
    Get cluster-index information from a vector

    Parameters
    ----------
    m : array
        A significant vector.
        The values in m should be 0 or 1, which represent significant point or not significant point.

    Returns
    -------
    p : float
        The permutation test result, p-value.
    """

    x = np.shape(m)[0]
    b = np.zeros([x+2])
    b[1:x+1] = m

    index_m = np.zeros([x])

    index_n = 0
    for i in range(x):
        if b[i+1] == 1 and b[i] == 0 and b[i+2] == 1:
            index_n = index_n + 1
        if b[i+1] == 1:
            if b[i] != 0 or b[i+2] != 0:
                index_m[i] = index_n

    return index_m, index_n

' a function for getting the cluster-index information '

def get_cluster_index_2d_1sided(m):

    """
    Get cluster-index information from a matrix

    Parameters
    ----------
    m : array
        A significant matrix.
        The values in m should be 0 or 1, which represent significant point or not significant point.

    Returns
    -------
    p : float
        The permutation test result, p-value.
    """

    x, y = np.shape(m)
    b = np.zeros([x+2, y+2])
    b[1:x+1, 1:y+1] = m

    index_m = np.zeros([x, y])

    index_n = 0
    for i in range(x):
        for j in range(y):
            ii = i + 1
            jj = j + 1
            if b[ii, jj] == 1 and (b[ii-1, jj]+b[ii+1, jj]+b[ii, jj-1]+b[ii, jj+1]) != 0:
                min_index = index_n + 1
                if b[ii - 1, jj] == 1:
                    min_index = np.min([min_index, index_m[i - 1, j]])
                if b[ii, jj - 1] == 1:
                    min_index = np.min([min_index, index_m[i, j - 1]])
                k1 = 0
                while b[ii, jj - k1] == 1:
                    index_m[i, j - k1] = min_index
                    k1 = k1 + 1
                k2 = 0
                while b[ii - k2, jj] == 1:
                    index_m[i - k2, j] = min_index
                    k2 = k2 + 1
                k = 0
                while b[ii, jj + k] == 1:
                    index_m[i, j + k] = min_index
                    k = k + 1
                k = 0
                while b[ii + k, jj] == 1:
                    index_m[i + k, j] = min_index
                    k = k + 1
                if b[ii, jj - 1] != 1:
                    index_n = index_n + 1
                    k = 0
                    m = 0
                    while b[ii, jj + k] == 1:
                        if b[ii - 1, jj + k] == 1:
                            m = 1
                        k = k + 1
                    if m == 1:
                        index_n = index_n - 1

    return index_m, index_n

' a function for 1-sample & 1-sided cluster based permutation test for 2-D results '

def clusterbased_permutation_2d_1samp_1sided(results, level=0, p_threshold=0.05, iter=5000):

    """
    1-sample & 1-sided cluster based permutation test for 2-D results

    Parameters
    ----------
    results : array
        A result matrix.
        The shape of results should be [n_subs, x1, x2]. n_subs represents the number of subjects.
    level : float. Default is 0.
        A expected value in null hypothesis. (Here, results > level)
    p_threshold : float. Default is 0.05.
        The threshold of p-values.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    p : float
        The permutation test result, p-value.
    """

    nsubs, x1, x2 = np.shape(results)

    ps = np.zeros([x1, x2])
    ts = np.zeros([x1, x2])
    for t1 in range(x1):
        for t2 in range(x2):
            ts[t1, t2], p = ttest_1samp(results[:, t1, t2], level, alternative='greater')
            if p < p_threshold and ts[t1, t2] > 0:
                ps[t1, t2] = 1
            else:
                ps[t1, t2] = 0

    cluster_index, cluster_n = get_cluster_index_2d_1sided(ps)
    print(cluster_n)

    if cluster_n != 0:
        cluster_ts = np.zeros([cluster_n])
        for i in range(cluster_n):
            for t1 in range(x1):
                for t2 in range(x2):
                    if cluster_index[t1, t2] == i + 1:
                        cluster_ts[i] = cluster_ts[i] + ts[t1, t2]

        permu_ts = np.zeros([iter])
        chance = np.full([nsubs], level)
        for i in range(iter):
            permu_cluster_ts = np.zeros([cluster_n])
            for j in range(cluster_n):
                for t1 in range(x1):
                    for t2 in range(x2):
                        if cluster_index[t1, t2] == j + 1:
                            v = np.hstack((results[:, t1, t2], chance))
                            vshuffle = np.random.permutation(v)
                            v1 = vshuffle[:nsubs]
                            v2 = vshuffle[nsubs:]
                            permu_cluster_ts[j] = permu_cluster_ts[j] + ttest_rel(v1, v2, alternative="greater")[0]
            permu_ts[i] = np.max(permu_cluster_ts)

        for i in range(cluster_n):
            index = 0
            for j in range(iter):
                if cluster_ts[i] > permu_ts[j]:
                    index = index + 1
            if index < iter * (1-p_threshold):
                for t1 in range(x1):
                    for t2 in range(x2):
                        if cluster_index[t1, t2] == i + 1:
                            ps[t1, t2] = 0

    return ps

' a function for 1-sided cluster based permutation test for 2-D results '

def clusterbased_permutation_2d_1sided(results1, results2, p_threshold=0.05, iter=5000):

    """
    1-sided cluster based permutation test for 2-D results

    Parameters
    ----------
    results1 : array
        A result matrix under condition1.
        The shape of results should be [n_subs, x1, x2]. n_subs represents the number of subjects.
    results2 : array
        A result matrix under condition2.
        The shape of results should be [n_subs, x1, x2]. n_subs represents the number of subjects. (results1 > results2)
    p_threshold : float. Default is 0.05.
        The threshold of p-values.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    p : float
        The permutation test result, p-value.
    """

    nsubs1, x11, x12 = np.shape(results1)
    nsubs2, x21, x22 = np.shape(results2)

    if nsubs1 != nsubs2 and x11 != x21 and x12 != x22:

        return "Invalid input!"

    nsubs = nsubs1
    x1 = x11
    x2 = x12

    ps = np.zeros([x1, x2])
    ts = np.zeros([x1, x2])
    for t1 in range(x1):
        for t2 in range(x2):
            ts[t1, t2], p = ttest_rel(results1[:, t1, t2], results2[:, t1, t2], alternative="greater")
            if p < p_threshold and ts[t1, t2] > 0:
                ps[t1, t2] = 1
            else:
                ps[t1, t2] = 0

    cluster_index, cluster_n = get_cluster_index_2d_1sided(ps)
    print(cluster_n)

    if cluster_n != 0:
        cluster_ts = np.zeros([cluster_n])
        for i in range(cluster_n):
            for t1 in range(x1):
                for t2 in range(x2):
                    if cluster_index[t1, t2] == i + 1:
                        cluster_ts[i] = cluster_ts[i] + ts[t1, t2]

        permu_ts = np.zeros([iter])
        for i in range(iter):
            permu_cluster_ts = np.zeros([cluster_n])
            for j in range(cluster_n):
                for t1 in range(x1):
                    for t2 in range(x2):
                        if cluster_index[t1, t2] == j + 1:
                            v = np.hstack((results1[:, t1, t2], results2[:, t1, t2]))
                            vshuffle = np.random.permutation(v)
                            v1 = vshuffle[:nsubs]
                            v2 = vshuffle[nsubs:]
                            permu_cluster_ts[j] = permu_cluster_ts[j] + ttest_rel(v1, v2, alternative="greater")[0]
            permu_ts[i] = np.max(permu_cluster_ts)

        for i in range(cluster_n):
            index = 0
            for j in range(iter):
                if cluster_ts[i] > permu_ts[j]:
                    index = index + 1
            if index < iter * (1 - p_threshold):
                for t1 in range(x1):
                    for t2 in range(x2):
                        if cluster_index[t1, t2] == i + 1:
                            ps[t1, t2] = 0

    return ps

' a function for showing the progress bar '

def show_progressbar(str, cur, total=100):

    percent = '{:.2%}'.format(cur / total)
    sys.stdout.write('\r')
    sys.stdout.write(str + ": [%-100s] %s" % ('=' * int(cur), percent))
    sys.stdout.flush()


# python3

class Bracket:
    def __init__(self, sign, index):
        self.sign = sign
        self.index = index

    def get_sign(self):
        return self.sign

    def get_index(self):
        return self.index

class Stack:
    def __init__(self):
        self.__storage = []

    def is_empty(self):
        return len(self.__storage) == 0

    def push(self, p):
        self.__storage.append(p)

    def pop(self):
        return self.__storage.pop()

def bracket_matches(closed):
    if closed == ']':
        return '['
    if closed == '}':
        return '{'
    if closed == ')':
        return '('
    return False

if __name__ == "__main__":
    text = input('Enter your code. ')

    brackets_stack = Stack()
    error_index = -1

    for i, symbol in enumerate(text):
        if symbol == '(' or symbol == '[' or symbol == '{':
            brackets_stack.push(Bracket(symbol, i))
            pass

        if symbol == ')' or symbol == ']' or symbol == '}':
            if brackets_stack.is_empty():
                error_index = i + 1
                break

            bracket = brackets_stack.pop()
            if bracket_matches(symbol) != bracket.get_sign():
                error_index = bracket.get_index() + 1
                break
            pass

    if not brackets_stack.is_empty():
        error_index = brackets_stack.pop().get_index() + 1

    print(['Success', error_index][error_index > -1])


# Sreela Kodali - MNIST on Keras
# June 21st, 2017

from __future__ import print_function
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.datasets import mnist
from keras.optimizers import RMSprop, SGD
from keras.regularizers import l1, l2
import numpy
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
numpy.random.seed(7)

# Part 1: Load Data
#load data
(xtrain, ytrain), (xtest, ytest) = mnist.load_data()

# reshape dataset from 28x28 images to sets of 784 pixels
xtrain = xtrain.reshape(60000, 784)
xtest = xtest.reshape(10000, 784)

# casting each array
xtrain = xtrain.astype('float32')
xtest = xtest.astype('float32')

# making array values from 0 to 1
xtrain /= 255
xtest /= 255

#printing n for each data set
print(xtrain.shape[0], 'train samples')
print(xtest.shape[0], 'test samples')

# converting class vectors to binary class matrices
ytrain = keras.utils.to_categorical(ytrain, 10)
ytest = keras.utils.to_categorical(ytest, 10)

# CONTROL PARAMETERS
l_r = 0.5
p = 0.6
d = 0.0
n_nodesL1 = 25
n_nodesL2 = 15
n_nodesL3 = 10
drate = 0.05
reg = 0.01
#kernel_regularizer=l2(reg))

#creating sgd optimizer
opt = SGD(lr=l_r, momentum=p, decay=d, nesterov=False)

# Part 2: Define Model
model = Sequential()
model.add(Dense(n_nodesL1, input_shape=(784,),  activation='relu'))
model.add(Dropout(drate))
model.add(Dense(n_nodesL2, activation='relu', bias_regularizer=l1(reg)))
model.add(Dropout(drate))
model.add(Dense(n_nodesL3, activation='sigmoid'))

# Part 3: Compile Model
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])

#setting up variables for forloop to fit/evaluate models with different epochs
epoch = [3, 6, 9, 12, 15]
trainP = numpy.zeros(len(epoch))
testP = numpy.zeros(len(epoch))
i = 0

#fit model and evaluate train and test performance for x epochs
for x in epoch:
	print('---EPOCH #:', x, '---')
	model.fit(xtrain, ytrain, epochs=x, batch_size=10, verbose=0, validation_data=(xtrain, ytrain))
	trainPerformance = model.evaluate(xtrain, ytrain, verbose=0)
	#print('Epoch #:', x, 'Train loss:', trainPerformance[0])
	print('Epoch #:', x, 'Train accuracy:', trainPerformance[1])
	trainP[i] = trainPerformance[1]

	testPerformance = model.evaluate(xtest, ytest, verbose=0)
	#print('Epoch #:', x, 'Test loss:', testPerformance[0])
	print('Epoch #:', x, 'Test accuracy:', testPerformance[1])
	testP[i] = testPerformance[1]
	i += 1

#plot train and test accuracy by # of epochs
fig = plt.figure()
line1, = plt.plot(epoch, trainP, label='Train')
line2, = plt.plot(epoch, testP, label='Test')
plt.xlabel('# of Epochs')
plt.ylabel('Accuracy')
plt.legend(handles=[line1, line2])
plt.show()


import socket

class ServerStub(object):
    def __init__(self, mycode):
        self.mycode = mycode

    def convert(self, num, obj):
        """根据类型编号转换类型"""
        if num == "1":
            obj = int(obj)
        if num == "2":
            obj = float(obj)
        if num == "3":
            obj = str(obj)
        return obj

    def unpack(self, data):
        """3.服务端的RPC Proxy组件把通过网络接收到的数据包按照相应格式进行拆包解码，获取方法名和参数"""
        msg = data.decode("utf-8")
        # 格式应该是"格式：func:函数名@params:类型编号-参数,类型编号2-参数2..."
        array = msg.split("@")
        func = array[0].split(":")[1]
        if len(array) > 1:
            args = list()
            for item in array[1].split(":")[1].split(","):
                temps = item.split("-")
                # 类型转换
                args.append(self.convert(temps[0], temps[1]))
            return (func, tuple(args))  # (func,args)
        return (func, )

    def pack(self, result):
        """打包：把方法和参数拼接成自定义的协议"""
        # 格式："data:返回值"
        return f"data:{result}".encode("utf-8")

    def exec(self, func, args=None):
        """4.服务端的RPC Proxy组件根据方法名和参数进行本地调用"""
        # 如果没有这个方法则返回None
        func = getattr(self.mycode, func, None)
        if args:
            return func(*args)  # 解包
        else:
            return func()  # 无参函数

    def handle(self, client_socket, client_addr):
        while True:
            # 获取客户端发送的数据包
            data = client_socket.recv(2048)
            if data:
                try:
                    data = self.unpack(data)  # 解包
                    if len(data) == 1:
                        data = self.exec(data[0])  # 执行无参函数
                    elif len(data) > 1:
                        data = self.exec(data[0], data[1])  # 执行带参函数
                    else:
                        data = "RPC Server Error Code:500"
                except Exception as ex:
                    data = "RPC Server Function Error"
                    print(ex)
                # 6.服务端的RPC Proxy组件将返回值打包编码成自定义的协议数据包，并通过网络发送给客户端的RPC Proxy组件
                data = self.pack(data)  # 把函数执行结果按指定协议打包
                # 把处理过的数据发送给客户端
                client_socket.send(data)
            else:
                print(f"客户端：{client_addr}已断开\n")
                break


import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import re
import requests
import html
import csv
import numpy as np
import os
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument("--window-size=1920x1080")

# Nom du fichier de sauvegarde
saveAs = 'liens_france'

# URL à scrapper
urlToScrap = 'https://www.opinionsystem.fr/fr-fr/search'

# Chromedriver doit être installé et placé à la racine du projet
driver = webdriver.Chrome(executable_path = os.path.join(os.getcwd(), 'chromedriver'), chrome_options = chrome_options)
driver.implicitly_wait(3)
driver.get(urlToScrap)

links = []

# On récupère l'element input avec l'id `query`
webElement = driver.find_element_by_id('query')
# On injecte une valeur à l'input
webElement.send_keys('immobilier')

# On récupère le button du formulaire de recherche et on clique dessus
driver.find_element_by_xpath('/html/body/div[1]/section[2]/div/div/div/div/div/div/div[1]/div[3]/form/div[3]/button').click()

# Il y a 1100 résultats au total et 9 fiches par pages, soit 120 pages.
nb_pages = 120
num_page = 0

# Rafrachir toutes les pages
while num_page < nb_pages :
    driver.find_element_by_xpath('/html/body/div[1]/section[3]/div[2]/div/div/div/button').click()
    num_page += 1

# 3 car la première card commence à la div 3 du container
num_card = 3
# 1070 cards au total
total_card = 1070

while num_card < total_card :
    ## On récupère l'element lien de la card
    linkElement = driver.find_element_by_xpath('/html/body/div[1]/section[3]/div[1]/div/div['+str(num_card)+']/a')
    ## On extrait l'url texte de l'element lien
    link = linkElement.get_attribute('href')
    ## On ajoute le lien à la liste
    links.append(link)
    num_card += 1

driver.quit()

## On sauvarde les liens dans un ficher
script_dir = os.path.dirname(__file__)
file_path = os.path.join(script_dir, saveAs)
np.save(file_path, links)


import random
import statistics
import time
import sys

def _mutacao(pais, geneSet,avalia_aptidao):
    indice = random.randrange(0, len(pais.Genes)) #funçao random.range(start,stop,step)
    geneFilho = pais.Genes[:]
    novoGene, alternar = random.sample(geneSet, 2) #atribuição multipla de variaveis
    geneFilho[indice] = alternar \
        if novoGene == geneFilho[indice] \
            else novoGene
    aptidao = avalia_aptidao(geneFilho)
    return Cromossomo(geneFilho, aptidao)

def _gerar_pais(tamanho, geneSet, avalia_aptidao):
    genes = []
    while len(genes) < tamanho:
        tamanhoAmostra = min(tamanho - len(genes), len(geneSet))
        genes.extend(random.sample(geneSet, tamanhoAmostra))
    aptidao = avalia_aptidao(genes)
    return Cromossomo(genes, aptidao)

def _obter_melhora(novo_filho, gerar_pais):
    melhorPai = gerar_pais()
    yield melhorPai
    while True:
        filho = novo_filho(melhorPai)
        if melhorPai.Aptidao > filho.Aptidao:
            continue
        if not filho.Aptidao > melhorPai.Aptidao:
            melhorPai = filho
            continue
        yield filho
        melhorPai = filho

def oMelhor(avaliaAptidao, tamanhoAlvo, aptidaoOtima, geneSet, tela):
    random.seed()

    def fnMutacao(pais):
        return _mutacao(pais, geneSet, avaliaAptidao)
    def fnGerarPais():
        return _gerar_pais(tamanhoAlvo, geneSet, avaliaAptidao)

    for melhora  in _obter_melhora(fnMutacao, fnGerarPais):
        tela(melhora)
        if not aptidaoOtima > melhora.Aptidao:
            return melhora

class Cromossomo:
    def __init__(self, genes, aptidao,):
        self.Genes = genes
        self.Aptidao = aptidao

class Benchmark:
    @staticmethod
    def run(funcao):
        cronometragem = []
        stdout = sys.stdout
        for i in range(100):
            sys.stdout = None
            iniciaTempo = time.time()
            funcao()
            segundos = time.time() - iniciaTempo
            sys.stdout = stdout
            cronometragem.append(segundos)
            media = statistics.mean(cronometragem)
            if i < 10 or i % 10 == 9:
                print("{} {:3.2f} {:3.2f}".format(
                    1 + i, media,
                    statistics.stdev(cronometragem,media)
                    if i > 1 else 0))


cities = ['Aa', 'Ba', 'Ca', 'Da', 'Ea']
capatilized_cities = []
for city in cities:
    print(city)
print("done")

for city in cities:
    capatilized_cities.append(city.upper())
    print(capatilized_cities)

# Indentation make the difference how they are printed
print(capatilized_cities)

# Create a new list out of existing list
names = ["Joey Tribbiani", "Monica Geller", "Chandler Bing", "Phoebe Buffay"]
usernames = []
for name in names:
    usernames.append(name.lower().replace(" ", "_"))
print(usernames)

# Create a new list out of existing list using range function
usernames = ["Joy Tribbiani", "Mons Geller", "Chandan Bing", "Photo Buffay"]
for i in range(len(usernames)):
    usernames[i] = usernames[i].lower().replace(" ", "_")
print(usernames)

# Count the xml tags in a string
tokens = ['<greeting>', 'Hello World!', '</greeting>']
count = 0

# write your for loop here
for token in tokens:
    if (token[0] == '<' and token[-1] == '>'):
        count += 1
print(count)

# Write an HTML code from given string
items = ['first string', 'second string']
html_str = "
\n"  # "\ n" is the character that marks the end of the line, it does
# the characters that are after it in html_str are on the next line

# write your code here
for item in items:
    html_str += "
{}
\n".format(item)
html_str += '
'
print(html_str)

# building dictionaries using for loop
book_title = ['great', 'expectations', 'the', 'adventures', 'of', 'sherlock', 'holmes',
              'the', 'great', 'gasby', 'hamlet', 'adventures', 'of', 'huckleberry', 'fin']
word_counter = {}
for word in book_title:
    if word not in word_counter:
        word_counter[word] = 1
    else:
        word_counter[word] += 1
print(word_counter)

# building dictionaries using get method
book_title1 = ['great', 'expectations', 'the', 'adventures', 'of', 'sherlock', 'holmes',
               'the', 'great', 'gasby', 'hamlet', 'adventures', 'of', 'huckleberry', 'fin']
word_counter1 = {}

for word in book_title1:
    word_counter1[word] = word_counter1.get(word, 0) + 1
print(word_counter1)

# accessing dictionaries using for loop

cast = {
    "Jerry Seinfeld": "Jerry Seinfeld",
    "Julia Louis-Dreyfus": "Elaine Benes",
    "Jason Alexander": "George Costanza",
    "Michael Richards": "Cosmo Kramer"
}

# only access keys
for key in cast:
    print(key)

# access both keys and values
for key, value in cast.items():
    print("Actor : {}, Role : {}".format(key, value))

# You would like to count the number of fruits in your basket.
# In order to do this, you have the following dictionary and list of
# fruits.  Use the dictionary and list to count the total number
# of fruits, but you do not want to count the other items in your basket.

result = 0
basket_items = {'apples': 4, 'oranges': 19, 'kites': 3, 'sandwiches': 8}
fruits = ['apples', 'oranges', 'pears', 'peaches', 'grapes', 'bananas']

for object, count in basket_items.items():
    if object in fruits:
        result += count

print("There are {} fruits in the basket.".format(result))

# Only to get updated dictionaries
result = 0
basket_items = {'apples': 4, 'oranges': 19, 'kites': 3, 'sandwiches': 8}
fruits = ['apples', 'oranges', 'pears', 'peaches', 'grapes', 'bananas']

# Iterate through the dictionary
for fruit in basket_items:
    if fruit in fruits:
        basket_items[fruit] += 1
# if the key is in the list of fruits, add the value (number of fruits) to result

print(basket_items)


from sys import path
path.insert(0, "../Funktioner")
from floatInput import *

# Definiera funktioner
def encrypt(key, string):
	# Beräkna rutnät
	matrix = []
	for i in range(len(key)):
		matrix.append([])

	# Sätt in strängen
	xCoord = 0
	for i in range(len(string)):
		matrix[xCoord].append(string[i])
		xCoord += 1
		if xCoord == len(key):
			xCoord = 0

	# Nyckel till array
	keyArray = []
	for i in range(len(key)):
		keyArray.append(key[i])

	# Ordna kolumnerna
	reverse = []
	for nuttin in range(len(key)):
		for i in range(len(keyArray)):
			if keyArray[i] == max(keyArray):
				reverse.append(matrix.pop(i))
				keyArray.pop(i)
				break
	sortedArray = reverse[::-1]

	# Rutnät till sträng
	outputString = ""
	for i in range(len(sortedArray)):
		for j in range(len(sortedArray[i])):
			outputString += sortedArray[i][j]

	return(outputString)

# Ta input
#nyckel = input("Nyckel: ")
#sträng = input("Sträng: ")

# Output
"""print()
print("Nyckel: " + nyckel)
print("Krypterad: " + encrypt(nyckel, encrypt(nyckel, sträng)))
print("Ursprunglig: " + sträng)"""

decrypt("KODA", encrypt("KODA", "VISESIFINALEN"))


favourite_languages = {
    'jen' : 'python',
    'sarah' : 'c',
    'edward' : 'ruby',
    'phil' : 'python',
}

# Pollsters: People we want to take the poll
# poll_takers: People who have taken the poll.
pollsters = ['jen','phil','sarah','erin','rosie']
poll_takers = favourite_languages.keys()

for pollster in pollsters:
    if pollster in poll_takers:
        print(pollster.title() + ", thank you for taking the poll.")
    else:
        print(pollster.title() + ", please take the poll!")


from concurrent.futures import ProcessPoolExecutor
from functools import partial
import numpy as np
import os
from util import audio
from configs.hparams import hparams

def build_from_path(in_dir, base_dir, out_dir, hparams, num_workers=1, tqdm=lambda x: x):
  '''Preprocesses the LJ Speech dataset from a given input path into a given output directory.

    Args:
      in_dir: The directory where you have downloaded the LJ Speech dataset
      out_dir: The directory to write the output into
      num_workers: Optional number of worker processes to parallelize across
      tqdm: You can optionally pass tqdm to get a nice progress bar

    Returns:
      A list of tuples describing the training examples. This should be written to train.txt
  '''

  # We use ProcessPoolExecutor to parallize across processes. This is just an optimization and you
  # can omit it and just call _process_utterance on each input if you want.
  train_dir = os.path.join(base_dir, 'training_{}'.format(out_dir))
  valid_dir = os.path.join(base_dir, 'valid_{}'.format(out_dir))
  os.makedirs(train_dir, exist_ok=True)
  os.makedirs(valid_dir, exist_ok=True)
  executor = ProcessPoolExecutor(max_workers=num_workers)
  futures = []
  futures_val = []
  index = 1
  with open(os.path.join(in_dir, 'script', 'script.txt'), encoding='utf-8', errors='ignore') as f:  # script_punc_num.txt, encoding='cp949'
    for line in f:
      parts = line.rstrip('\n').split('\t')
      # index = line[1:5]
      #if parts[0][0] != 'f':
      wav_path = os.path.join(in_dir, 'wav', '%s.wav' % parts[0])  # without [0])
      # test_list = ['4030', '4048', '4053', '4078', '4089', '4092', '4093', '4096', '4117', '4120', '4122', '4130', '4142', '4156', '4201', '4213', '4227', '4309', '4319', '4323']
      # if os.path.exists(wav_path):
        # if int(index)>4000 and not (index in test_list):
          # text = line[6:-1]  # parts[2]
          # futures_val.append(executor.submit(partial(_process_utterance, valid_dir, index, wav_path, text)))
        # elif int(index)<=4000:
          # text = line[6:-1]  # parts[2]
          # futures.append(executor.submit(partial(_process_utterance, train_dir, index, wav_path, text)))
      if os.path.exists(wav_path):
        if int(index)%100==0:
          text = parts[1]  # line[6:-1]  #
          futures_val.append(executor.submit(partial(_process_utterance, valid_dir, index, wav_path, text)))
        else:
          text = parts[1]  # line[6:-1]  #
          futures.append(executor.submit(partial(_process_utterance, train_dir, index, wav_path, text)))
      index += 1
  # return [future.result() for future in tqdm(futures)]
    write_metadata([future.result() for future in tqdm(futures)], train_dir, 'train.txt')
    write_metadata([future.result() for future in tqdm(futures_val)], valid_dir, 'valid.txt')

def _process_utterance(out_dir, index, wav_path, text):
  wav = audio.load_wav(wav_path)
  spectrogram = audio.spectrogram(wav).astype(np.float32)
  n_frames = spectrogram.shape[1]
  mel_spectrogram = audio.melspectrogram(wav).astype(np.float32)
  spectrogram_filename = 'selvas-spec-%04d.npy' % int(index)
  mel_filename = 'selvas-mel-%04d.npy' % int(index)
  np.save(os.path.join(out_dir, spectrogram_filename), spectrogram.T, allow_pickle=False)
  np.save(os.path.join(out_dir, mel_filename), mel_spectrogram.T, allow_pickle=False)
  return (spectrogram_filename, mel_filename, n_frames, text)

def write_metadata(metadata, out_dir, domain):
  with open(os.path.join(out_dir, domain), 'w', encoding='utf-8') as f:
    for m in metadata:
      f.write('|'.join([str(x) for x in m]) + '\n')
  frames = sum([m[2] for m in metadata])
  hours = frames * hparams.frame_shift_ms / (3600 * 1000)
  print('Wrote %d utterances, %d frames (%.2f hours)' % (len(metadata), frames, hours))
  print('Max input length:  %d' % max(len(m[3]) for m in metadata))
  print('Max output length: %d' % max(m[2] for m in metadata))
  print('Min input length:  %d' % min(len(m[3]) for m in metadata))
  print('Min output length: %d' % min(m[2] for m in metadata))


from lux.extensions.ui import *

def add_css(all):
    css = all.css
    vars = all.variables
    vars.list_image_width = 140
    vars.list_image_xs_height = 150

    css('.media-list > .media',
        css(' > a',
            css(' .post-body',
                margin_left=px(vars.list_image_width+20)),
            css(' h3',
                font_weight='normal',
                color=vars.colors.gray_dark),
            text_decoration='none',
            color=vars.colors.gray))

    css('.post-image',
        width=px(vars.list_image_width),
        max_height=px(vars.list_image_width),
        float='left',
        height='auto')

    css('.post-image-xs',
        max_height=px(vars.list_image_xs_height),
        max_width=pc(90),
        width='auto')

    sphinx(all)

def sphinx(all):
    css = all.css
    vars = all.variables

    vars.headerlink.color = vars.colors.gray_lighter
    vars.headerlink.color_hover = vars.colors.gray_light

    for n in range(1, 7):
        css('h%d:hover > a.headerlink' % n,
            visibility='visible')
    css('dt:hover > a.headerlink', visibility='visible')

    css('a.headerlink',
        css(':hover',
            color=vars.headerlink.color_hover),
        padding=spacing(0, 4),
        color=vars.headerlink.color,
        text_decoration='none',
        visibility='hidden')

    css('.viewcode-link',
        float='right')

    css('div.viewcode-block:target',
        margin=spacing(-1, -10),
        padding=spacing(0, 10))

    css('table.docutils.field-list th',
        padding=spacing(0, 20, 0, 0))


import matplotlib.pyplot as plt
from controller.database import Database
from datetime import datetime

#data = Database.delete_table('mytable')

data = Database.find('mytable')

time = [datetime.fromtimestamp(float(d["time"])).strftime("%I:%M:%S") for d in data]
angx = [d["angx"] for d in data]
angy = [d["angy"] for d in data]
head = [d["head"] for d in data]

throttle = [d["throttle"] for d in data]
pitch = [d["pitch"] for d in data]
roll = [d["roll"] for d in data]
yaw = [d["yaw"] for d in data]

engine1 = [d["engine1"] for d in data]
engine2 = [d["engine2"] for d in data]
engine3 = [d["engine3"] for d in data]
engine4 = [d["engine4"] for d in data]

fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, sharex=True)

ax1.plot(time,angx, color="r", label="roll")
ax1.plot(time,angy, color="b", label="pitch")
ax1.plot(time,head, color="g", label="heading")

ax2.plot(time,throttle, color="r", label="cnt_throttle")
ax2.plot(time,pitch, color="b", label="cnt_pitch")
ax2.plot(time,roll, color="g", label="cnt_roll")
ax2.plot(time,yaw, color="m", label="cnt_yaw")

ax3.plot(time,engine1, color="r", label="engine1")
ax3.plot(time,engine2, color="b", label="engine2")
ax3.plot(time,engine3, color="g", label="engine3")
ax3.plot(time,engine4, color="m", label="engine4")

ax1.legend()
ax2.legend()
ax3.legend()

plt.show()


from splinter import Browser
from bs4 import BeautifulSoup
import requests
import pandas as pd

def init_browser():
    executable_path = {"executable_path": "/usr/local/bin/chromedriver"}
    return Browser("chrome", **executable_path, headless=False)

def scrape_info():
    browser = init_browser()
    main_dict = {}

## NASA Mars News
    url = 'https://mars.nasa.gov/news/'
    browser.visit(url)

    html = browser.html
    soup = BeautifulSoup(html, "html.parser")

    main_dict["title"] = soup.find("article").find("div",class_='content_title').a.text
    main_dict["news_p"] = soup.find("article").find("div",class_='article_teaser_body').text

    #close  browser
    browser.quit()

## JPL Mars Space Images - Featured Image
    main_url = 'https://www.jpl.nasa.gov'
    query_url = main_url + '/spaceimages/?search=&category=Mars'
    browser.visit(query_url)

    html = browser.html
    soup = BeautifulSoup(html, 'html.parser')

    #find the image
    article_style = soup.find("article")['style']
    #extract the url from the background img style
    featured_image_link = article_style.split("'")[1].split("'")[0]

    main_dict["featured_image_url"] = main_url + featured_image_link

    #close  browser
    browser.quit()

## Mars Weather
    url = 'https://twitter.com/marswxreport?lang=en'
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')

    results = soup.find_all('div', class_="js-tweet-text-container")
    result = results[0]

    main_dict["mars_weather"] = result.find('p').text

## Mars Facts
    url = 'https://space-facts.com/mars/'

    mars_facts = pd.read_html(url)
    mars_facts_df = mars_facts[0]
    mars_facts_df = mars_facts_df.rename(columns={0:'fact type',1:'fact'})

    main_dict["mars_facts"] = mars_facts_df.to_dict('records')

    ## Mars Hemisphere
    base_url = 'https://astrogeology.usgs.gov'
    url = base_url + '/search/results?q=hemisphere+enhanced&k1=target&v1=Mars'
    browser.visit(url)

    html = browser.html
    soup = BeautifulSoup(html, 'html.parser')
    results = soup.find_all('a', class_="itemLink")

    hemi_img_url = {}
    hemisphere_image_urls = []

    for result in results:
        try:
            title = result.find('h3').text
            image_link = base_url + result['href']

            if (title and image_link):
                hemi_img_url = {
                    'title': title,
                    'image_url': image_link
                }
                hemisphere_image_urls.append(hemi_img_url)

        except AttributeError:
            print('x')

    main_dict["mars_hemispheres"] = hemisphere_image_urls

    return main_dict


from collections import namedtuple
from unittest import TestCase

from src.cap2.sequencia import Baralho

class TestSequencia(TestCase):

    baralho = Baralho()
    Carta = namedtuple('carta', ['valores', 'naipe'])

    def teste_len(self):
        self.assertEqual(len(self.baralho), 56)

    def teste_getitem(self):
        self.assertEqual(self.baralho[1], self.Carta('Paus', '2'))

if __name__ == '__main__':
    TestSequencia()


import gc

class Node:
    def __init__(self, key, value):
        self.next = None
        self.previous = None
        self.value = value
        self.key = key

class DLList:
    def __init__(self):
        self.head = None
        self.tail = None

    def push_front(self, key,value):
        node = Node(key,value)
        node.next = self.head

        if self.head:
            self.head = node
            self.head.previous = node
            return node
        else:
            self.head = self.tail = node
            return node

    def push_back(self, key, value):
        node = Node(key, value)
        node.previous = self.tail

        if not self.tail:
            self.head = node
            self.tail = node
            return node
        else:
            self.tail.next = node
            self.tail = node
            return node

    def pop_front(self):
        if not self.head:
            return
        else:
            node = self.head
            node.next.previous = None
            self.head = node.next
            node.next = None
            gc.collect()
            return node

    def pop_back(self):
        if not self.tail:
            return
        else:
            node = self.tail
            node.previous.next = None
            self.tail = node.previous
            node.previous = None
            gc.collect()
            return node

    def pop_node(self, node):
        if not node.previous:
            self.head = node.next
        else:
            node.previous.next = node.next

        if not node.next:
            self.tail = node.previous
        else:
            node.next.previous = node.previous
        gc.collect()

    def traverse(self):
        tmp = self.head
        res = []
        while tmp:
            res.append(tmp.value)
            tmp = tmp.next
        return res


#!/usr/bin/env python

"""Higher-level DB functions used across multiple svn projects of DESDM.

Modified more often than the lower-level despydb
"""

import re
import sys
import copy
import time
import socket
from collections import OrderedDict

import despydb.desdbi as desdbi
import despydmdb.dmdb_defs as dmdbdefs
import despymisc.miscutils as miscutils

class DesDmDbi (desdbi.DesDbi):
    """Adds DB functions used across various DM projects.
    """

    def __init__(self, desfile=None, section=None):
        desdbi.DesDbi.__init__(self, desfile, section, retry=True)

    def exec_sql_expression(self, expression):
        """Execute an SQL expression or expressions.

        Construct and execute an SQL statement from a string containing an SQL
        expression or a list of such strings.  Return a sequence containing a
        result for each column.
        """
        if isinstance(expression, list):
            s = ','.join(expression)
        else:
            s = expression

        stmt = self.get_expr_exec_format() % s
        cursor = self.cursor()
        cursor.execute(stmt)
        res = cursor.fetchone()
        cursor.close()
        return res

    def get_expr_exec_format(self):
        """Return a format string for a statement to execute SQL expressions.

        The returned format string contains a single unnamed python subsitution
        string that expects a string containing the expressions to be executed.
        Once the expressions have been substituted into the string, the
        resulting SQL statement may be executed.

        Examples:
            expression:      con.get_expr_exec_format()
            oracle result:   SELECT %s FROM DUAL
            postgres result: SELECT %s

            expression:      con.get_expr_exec_format() % 'func1(), func2()'
            oracle result:   SELECT func1(), func2() FROM DUAL
            postgres result: SELECT func1(), func2()
        """
        return self.con.get_expr_exec_format()

    def get_metadata(self):
        sql = "select * from ops_metadata"
        curs = self.cursor()
        curs.execute(sql)
        desc = [d[0].lower() for d in curs.description]

        result = OrderedDict()
        for line in curs:
            d = dict(list(zip(desc, line)))
            headername = d['file_header_name'].lower()
            columnname = d['column_name'].lower()
            if headername not in result:
                result[headername] = OrderedDict()
            if columnname not in result[headername]:
                result[headername][columnname] = d
            else:
                raise Exception("Found duplicate row in metadata (%s, %s)" % (headername, columnname))

        curs.close()
        return result

    def get_all_filetype_metadata(self):
        """Provide a complete set of filetype metadata required during a run.

        Gets a dictionary of dictionaries or string=value pairs representing
        data from the OPS_METADATA, OPS_FILETYPE, and OPS_FILETYPE_METADATA tables.
        """
        sql = """select f.filetype, f.metadata_table, f.filetype_mgmt,
                    nvl(fm.file_hdu, 'primary') file_hdu,
                    fm.status, fm.derived,
                    fm.file_header_name, m.column_name
                from OPS_METADATA m, OPS_FILETYPE f, OPS_FILETYPE_METADATA fm
                where m.file_header_name=fm.file_header_name
                    and f.filetype=fm.filetype
                    and fm.status != 'I'
                """
        curs = self.cursor()
        curs.execute(sql)
        desc = [d[0].lower() for d in curs.description]

        result = OrderedDict()
        for row in curs:
            info = dict(list(zip(desc, row)))
            ptr = result
            ftype = info['filetype'].lower()
            if ftype not in result:
                result[ftype] = OrderedDict({'hdus': OrderedDict()})
                if info['metadata_table'] is not None:
                    result[ftype]['metadata_table'] = info['metadata_table'].lower()
                if info['filetype_mgmt'] is not None:
                    result[ftype]['filetype_mgmt'] = info['filetype_mgmt']

            if info['file_hdu'].lower() not in result[ftype]['hdus']:
                result[ftype]['hdus'][info['file_hdu'].lower()] = OrderedDict()

            ptr = result[ftype]['hdus'][info['file_hdu'].lower()]
            if info['status'].lower() not in ptr:
                ptr[info['status'].lower()] = OrderedDict()

            ptr = ptr[info['status'].lower()]
            if info['derived'].lower() not in ptr:
                ptr[info['derived'].lower()] = OrderedDict()

            ptr[info['derived'].lower()][info['file_header_name'].lower()] = info['column_name'].lower()

        curs.close()

        return result

    def get_site_info(self):
        """Return contents of ops_site and ops_site_val tables.
        """
        # assumes foreign key constraints so cannot have site in ops_site_val that isn't in ops_site

        site_info = self.query_results_dict('select * from ops_site', 'name')

        sql = "select name,key,val from ops_site_val"
        curs = self.cursor()
        curs.execute(sql)
        for (name, key, val) in curs:
            site_info[name][key] = val
        return site_info

    def get_archive_info(self):
        """Return contents of ops_archive and ops_archive_val tables.
        """
        # assumes foreign key constraints so cannot have archive in ops_archive_val that isn't in ops_archive

        archive_info = self.query_results_dict('select * from ops_archive', 'name')

        sql = "select name,key,val from ops_archive_val"
        curs = self.cursor()
        curs.execute(sql)
        for (name, key, val) in curs:
            archive_info[name][key] = val
        return archive_info

    def get_archive_transfer_info(self):
        """Return contents of ops_archive_transfer and ops_archive_transfer_val tables.
        """
        archive_transfer = OrderedDict()
        sql = "select src,dst,transfer from ops_archive_transfer"
        curs = self.cursor()
        curs.execute(sql)
        for row in curs:
            if row[0] not in archive_transfer:
                archive_transfer[row[0]] = OrderedDict()
            archive_transfer[row[0]][row[1]] = OrderedDict({'transfer': row[2]})

        sql = "select src,dst,key,val from ops_archive_transfer_val"
        curs = self.cursor()
        curs.execute(sql)
        for row in curs:
            if row[0] not in archive_transfer:
                miscutils.fwdebug(
                    0, 'DESDBI_DEBUG', "WARNING: found info in ops_archive_transfer_val for src archive %s which is not in ops_archive_transfer" % row[0])
                archive_transfer[row[0]] = OrderedDict()
            if row[1] not in archive_transfer[row[0]]:
                miscutils.fwdebug(
                    0, 'DESDBI_DEBUG', "WARNING: found info in ops_archive_transfer_val for dst archive %s which is not in ops_archive_transfer" % row[1])
                archive_transfer[row[0]][row[1]] = OrderedDict()
            archive_transfer[row[0]][row[1]][row[2]] = row[3]
        return archive_transfer

    def get_job_file_mvmt_info(self):
        """Return contents of ops_job_file_mvmt and ops_job_file_mvmt_val tables.
        """
        # [site][home][target][key] = [val]  where req key is mvmtclass

        sql = "select site,home_archive,target_archive,mvmtclass from ops_job_file_mvmt"
        curs = self.cursor()
        curs.execute(sql)
        info = OrderedDict()
        for (site, home, target, mvmt) in curs:
            if home is None:
                home = 'no_archive'

            if target is None:
                target = 'no_archive'

            if site not in info:
                info[site] = OrderedDict()
            if home not in info[site]:
                info[site][home] = OrderedDict()
            info[site][home][target] = OrderedDict({'mvmtclass': mvmt})

        sql = "select site,home_archive,target_archive,key,val from ops_job_file_mvmt_val"
        curs = self.cursor()
        curs.execute(sql)
        for (site, home, target, key, val) in curs:
            if home is None:
                home = 'no_archive'

            if target is None:
                target = 'no_archive'

            if (site not in info or
                home not in info[site] or
                    target not in info[site][home]):
                miscutils.fwdie("Error: found info in ops_job_file_mvmt_val (%s, %s, %s, %s, %s) which is not in ops_job_file_mvmt" % (
                    site, home, target, key, val), 1)
            info[site][home][target][key] = val
        return info

    def load_artifact_gtt(self, filelist):
        """Insert file artifact information into global temp table.
        """
        # filelist is list of file dictionaries
        # returns artifact GTT table name

        parsemask = miscutils.CU_PARSE_FILENAME | miscutils.CU_PARSE_EXTENSION

        # make sure table is empty before loading it
        self.empty_gtt(dmdbdefs.DB_GTT_ARTIFACT)

        colmap = [dmdbdefs.DB_COL_FILENAME, dmdbdefs.DB_COL_COMPRESSION,
                  dmdbdefs.DB_COL_MD5SUM, dmdbdefs.DB_COL_FILESIZE]
        rows = []
        for file in filelist:
            miscutils.fwdebug(3, 'DESDBI_DEBUG', "file = %s" % file)
            fname = None
            comp = None
            md5sum = None
            filesize = None
            if (dmdbdefs.DB_COL_FILENAME in file or dmdbdefs.DB_COL_FILENAME.lower() in file):
                if dmdbdefs.DB_COL_COMPRESSION in file:
                    fname = file[dmdbdefs.DB_COL_FILENAME]
                    comp = file[dmdbdefs.DB_COL_COMPRESSION]
                elif dmdbdefs.DB_COL_COMPRESSION.lower() in file:
                    fname = file[dmdbdefs.DB_COL_FILENAME.lower()]
                    comp = file[dmdbdefs.DB_COL_COMPRESSION.lower()]
                elif dmdbdefs.DB_COL_FILENAME in file:
                    (fname, comp) = miscutils.parse_fullname(file[dmdbdefs.DB_COL_FILENAME], parsemask)
                else:
                    (fname, comp) = miscutils.parse_fullname(
                        file[dmdbdefs.DB_COL_FILENAME.lower()], parsemask)
                miscutils.fwdebug(3, 'DESDBI_DEBUG', "fname=%s, comp=%s" % (fname, comp))
            elif 'fullname' in file:
                (fname, comp) = miscutils.parse_fullname(file['fullname'], parsemask)
                miscutils.fwdebug(3, 'DESDBI_DEBUG', "parse_fullname: fname=%s, comp=%s" % (fname, comp))
            else:
                miscutils.fwdebug(3, 'DESDBI_DEBUG', "file=%s" % (file))
                raise ValueError("Invalid entry filelist (%s)" % file)

            if dmdbdefs.DB_COL_FILESIZE in file:
                filesize = file[dmdbdefs.DB_COL_FILESIZE]
            elif dmdbdefs.DB_COL_FILESIZE.lower() in file:
                filesize = file[dmdbdefs.DB_COL_FILESIZE.lower()]

            if dmdbdefs.DB_COL_MD5SUM in file:
                md5sum = file[dmdbdefs.DB_COL_MD5SUM]
            elif dmdbdefs.DB_COL_MD5SUM.lower() in file:
                md5sum = file[dmdbdefs.DB_COL_MD5SUM.lower()]

            miscutils.fwdebug(3, 'DESDBI_DEBUG', "row: fname=%s, comp=%s, filesize=%s, md5sum=%s" %
                              (fname, comp, filesize, md5sum))
            rows.append({dmdbdefs.DB_COL_FILENAME: fname, dmdbdefs.DB_COL_COMPRESSION: comp,
                         dmdbdefs.DB_COL_FILESIZE: filesize, dmdbdefs.DB_COL_MD5SUM: md5sum})

        self.insert_many(dmdbdefs.DB_GTT_ARTIFACT, colmap, rows)
        return dmdbdefs.DB_GTT_ARTIFACT

    def load_filename_gtt(self, filelist):
        """Insert filenames into filename global temp table.

        To use in join for later query.
        """
        # returns filename GTT table name

        # make sure table is empty before loading it
        self.empty_gtt(dmdbdefs.DB_GTT_FILENAME)

        colmap = [dmdbdefs.DB_COL_FILENAME, dmdbdefs.DB_COL_COMPRESSION]
        rows = []
        for file in filelist:
            fname = None
            comp = None
            if isinstance(file, str):
                (fname, comp) = miscutils.parse_fullname(
                    file, miscutils.CU_PARSE_FILENAME | miscutils.CU_PARSE_EXTENSION)
            elif isinstance(file, dict) and (dmdbdefs.DB_COL_FILENAME in file or dmdbdefs.DB_COL_FILENAME.lower() in file):
                if dmdbdefs.DB_COL_COMPRESSION in file:
                    fname = file[dmdbdefs.DB_COL_FILENAME]
                    comp = file[dmdbdefs.DB_COL_COMPRESSION]
                elif dmdbdefs.DB_COL_COMPRESSION.lower() in file:
                    fname = file[dmdbdefs.DB_COL_FILENAME.lower()]
                    comp = file[dmdbdefs.DB_COL_COMPRESSION.lower()]
                elif dmdbdefs.DB_COL_FILENAME in file:
                    (fname, comp) = miscutils.parse_fullname(
                        file[dmdbdefs.DB_COL_FILENAME], miscutils.CU_PARSE_FILENAME | miscutils.CU_PARSE_EXTENSION)
                else:
                    (fname, comp) = miscutils.parse_fullname(
                        file[dmdbdefs.DB_COL_FILENAME.lower()], miscutils.CU_PARSE_FILENAME | miscutils.CU_PARSE_EXTENSION)
            else:
                raise ValueError("Invalid entry filelist (%s)" % file)
            rows.append({dmdbdefs.DB_COL_FILENAME: fname, dmdbdefs.DB_COL_COMPRESSION: comp})
        self.insert_many(dmdbdefs.DB_GTT_FILENAME, colmap, rows)
        return dmdbdefs.DB_GTT_FILENAME

    def load_id_gtt(self, idlist):
        self.empty_gtt(dmdbdefs.DB_GTT_ID)
        colmap = [dmdbdefs.DB_COL_ID]
        rows = []
        for desfid in idlist:
            if isinstance(desfid, int):
                rows.append({dmdbdefs.DB_COL_ID: desfid})
            else:
                raise ValueError("invalid entry idlist (%s)" % str(desfid))
        self.insert_many(dmdbdefs.DB_GTT_ID, colmap, rows)
        return dmdbdefs.DB_GTT_ID

    def empty_gtt(self, tablename):
        """Clean out temp table if one wants separate commit/rollback control.
        """
        # could be changed to generic empty table function, for now wanted safety check
        if 'gtt' not in tablename.lower():
            raise ValueError("Invalid table name for a global temp table (missing GTT)")

        sql = "delete from %s" % tablename
        curs = self.cursor()
        curs.execute(sql)
        curs.close()

    def create_task(self, name, info_table,
                    parent_task_id=None, root_task_id=None, i_am_root=False,
                    label=None, do_begin=False, do_commit=False):
        """Insert a row into the task table and return task id.
        """
        row = {'name': name, 'info_table': info_table}

        row['id'] = self.get_seq_next_value('task_seq') # get task id

        if parent_task_id is not None:
            row['parent_task_id'] = int(parent_task_id)

        if i_am_root:
            row['root_task_id'] = row['id']
        elif root_task_id is not None:
            row['root_task_id'] = int(root_task_id)

        if label is not None:
            row['label'] = label

        self.basic_insert_row('task', row)

        if do_begin:
            self.begin_task(row['id'])

        if do_commit:
            self.commit()

        return row['id']

    def begin_task(self, task_id, do_commit=False):
        """Update a row in the task table with beginning of task info.
        """
        updatevals = {'start_time': self.get_current_timestamp_str(),
                      'exec_host': socket.gethostname()}
        wherevals = {'id': task_id} # get task id

        self.basic_update_row('task', updatevals, wherevals)
        if do_commit:
            self.commit()

    def end_task(self, task_id, status, do_commit=False):
        """Update a row in the task table with end of task info.
        """
        wherevals = {}
        wherevals['id'] = task_id

        updatevals = {}
        updatevals['end_time'] = self.get_current_timestamp_str()
        updatevals['status'] = status

        self.basic_update_row('task', updatevals, wherevals)
        if do_commit:
            self.commit()

    def get_datafile_metadata(self, filetype):
        """Gets a dictionary of all datafile (such as XML or fits table data files) metadata for the given filetype.

        Returns a list: [target_table_name,metadata]
        """
        TABLE = 0
        HDU = 1
        ATTRIBUTE = 2
        POSITION = 3
        COLUMN = 4
        DATATYPE = 5
        FORMAT = 6

        bindstr = self.get_named_bind_string("afiletype")
        sql = """select table_name, hdu, lower(attribute_name), position, lower(column_name), datafile_datatype, data_format
                from OPS_DATAFILE_TABLE df, OPS_DATAFILE_METADATA md
                where df.filetype = md.filetype and current_flag=1 and lower(df.filetype) = lower(""" + bindstr + """)
                order by md.attribute_name, md.POSITION"""
        result = OrderedDict()
        curs = self.cursor()
        curs.execute(sql, {"afiletype": filetype})

        tablename = None
        for row in curs:
            if tablename == None:
                tablename = row[TABLE]
            if row[HDU] not in list(result.keys()):
                result[row[HDU]] = {}
            if row[ATTRIBUTE] not in list(result[row[HDU]].keys()):
                result[row[HDU]][row[ATTRIBUTE]] = {}
                result[row[HDU]][row[ATTRIBUTE]]['datatype'] = row[DATATYPE]
                result[row[HDU]][row[ATTRIBUTE]]['format'] = row[FORMAT]
                result[row[HDU]][row[ATTRIBUTE]]['columns'] = []
            if len(result[row[HDU]][row[ATTRIBUTE]]['columns']) == row[POSITION]:
                result[row[HDU]][row[ATTRIBUTE]]['columns'].append(row[COLUMN])
            else:
                result[row[HDU]][row[ATTRIBUTE]]['columns'][row[POSITION]] = row[COLUMN]
        curs.close()
        if tablename is None:
            raise ValueError('Invalid filetype - missing entries in datafile tables')
        return [tablename, result]

#### Embedded simple test
if __name__ == '__main__':
    dbh = DesDmDbi()
    print('dbh = ', dbh)
    if dbh.is_postgres():
        print('Connected to postgres DB')
    elif dbh.is_oracle():
        print('Connected to oracle DB')
    print('which_services_file = ', dbh.which_services_file())
    print('which_services_section = ', dbh.which_services_section())

    print(dbh.get_column_names('exposure'))

    cursor = dbh.cursor()
    cursor.execute('SELECT count(*) from exposure')
    row = cursor.fetchone()
    print('Number exposures:', row[0])
    cursor.close()
    #dbh.commit()
    dbh.close()


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

def lastOfUs(amount, step) :
	guys = [i for i in range(1, amount+1)]
	while len(guys) > 1 :
		fakeStep = step
		while fakeStep > len(guys) : fakeStep -= len(guys)
		guys = guys[fakeStep:] + guys[:fakeStep-1]
		#print(guys)
	return guys[0]

a = int(input("Enter amount of guys: "))
c = int(input("Enter step: "))
print("The last guy is {}".format(lastOfUs(a,c) if a > 0 else "nobody"))


''' things to remember. if you want to:
delete - you must traverse
on delete - smallest child becomes the parent
>= flows right '''

class BinarySearchTree:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

    ''' on insert, it adds the input value to the BST
        it follows the rules of the order of elements
        in order to find your insertion spot, you need to traverse'''

    # Insert the given value into the tree
    def insert(self, value):
        if value < self.value:
            if self.left == None:
                self.left = BinarySearchTree(value)
            else:
                self.left.insert(value)
        else:
            if self.right == None:
                self.right = BinarySearchTree(value)
            else:
                self.right.insert(value)

    ''' 'contains' will search the BST for the input value
        a boolean will be returned to confirm the value exists or not
        Traverse the tree by starting at the root
        On finding the first instance of the value - stop (return true)
        if the search reaches a node that doesn't have children - then the value is not found (reutrn false)'''

    # Return True if the tree contains the value
    # False if it does not
    def contains(self, target):
        if self.value == target:
            return True
        else:

            if target < self.value:
                if self.left == None:
                    return False
                else:
                    return self.left.contains(target)
            else:
                if self.right == None:
                    return False
                else:
                    return self.right.contains(target)

    ''' 'get max' will return max value in the BST
         search moves right until it can go no further'''

    # Return the maximum value found in the tree
    def get_max(self):
        if self.right == None:
            return self.value
        else:
            return self.right.get_max()

    ''' 'for each' traverses every node in the tree
         executes the passed-in callback func on each node '''

    # Call the function `cb` on the value of each node
    # You may use a recursive or iterative approach
    def for_each(self, cb):

        cb(self.value)

        if self.right != None:
            self.right.for_each(cb)

        if self.left != None:
            self.left.for_each(cb)


import csv
from numpy.linalg import norm
from scipy import *
from pylab import plot, show, legend,xlim,ylim,savefig,title,xlabel,ylabel,clf, loglog
from matplotlib2tikz import save as tikz_save

#wdir = "../../../../data/raw/DBSWanew100s/o3/10/10.0/"
wdir = "../../../../data/raw/trackleadsola10new/o3/"
sdir = "../../../../data/postprocessing/DBSWfront/o3/10/10.0/"

if not os.path.exists(sdir):
    os.makedirs(sdir)

#wdir = "../../../data/Joe/dbh/o"+order+"/"
#sdir = "../../../../written/exportpic/dambreak/ex/"

#gap = 50
gap = 10

h0 = 0.1
g = 9.81

s = wdir + "outlast.txt"
with open(s,'r') as file1:
    readfile = csv.reader(file1, delimiter = ',', quotechar='|', quoting=csv.QUOTE_MINIMAL)

    h = []
    u = []
    x = []
    j = -1
    for row in readfile:
        if (j >= 0):
            dx =float(row[0])
            dt =float(row[1])
            t =float(row[2])
            x.append(float(row[4]))
            h.append(float(row[5]))
            u.append(float(row[7]))

        j = j + 1
    x = array(x)
    u = array(u)
    h = array(h)

xbeg = int((0)/dx)
xend = int((1000)/dx)
xt =x[xbeg:xend:gap]
ht =h[xbeg:xend:gap]

"""
n = len(xt)
s = sdir + "hDBSW.dat"
with open(s,'w') as file1:
    for i in range(n):
        s ="%3.8f%5s%1.15f\n" %(xt[i] + 200," ",ht[i])
        file1.write(s)
"""

n = len(xt)
s = sdir + "hDB.dat"
with open(s,'w') as file1:
    for i in range(n):
        s ="%3.8f%5s%1.15f\n" %(xt[i]," ",ht[i])
        file1.write(s)


import multiprocessing as multi
import concurrent.futures

from .windows import get_window_offsets

from tqdm import tqdm

_EXEC_DICT = {'mpool': multi.Pool,
              'processes': concurrent.futures.ProcessPoolExecutor,
              'threads': concurrent.futures.ThreadPoolExecutor}

class ParallelTask(object):

    """
    A class for parallel tasks over a DataArray with returned results for each chunk

    Args:
        data (DataArray): The ``xarray.DataArray`` to process.
        row_chunks (Optional[int]): The row chunk size to process in parallel.
        col_chunks (Optional[int]): The column chunk size to process in parallel.
        padding (Optional[tuple]): Padding for each window. ``padding`` should be given as a tuple
            of (left pad, bottom pad, right pad, top pad). If ``padding`` is given, the returned list will contain
            a tuple of ``rasterio.windows.Window`` objects as (w1, w2), where w1 contains the normal window offsets
            and w2 contains the padded window offsets.
        scheduler (Optional[str]): The parallel task scheduler to use. Choices are ['processes', 'threads', 'mpool'].

            mpool: process pool of workers using ``multiprocessing.Pool``
            processes: process pool of workers using ``concurrent.futures``
            threads: thread pool of workers using ``concurrent.futures``
        n_workers (Optional[int]): The number of parallel workers for ``scheduler``.
        n_chunks (Optional[int]): The chunk size of windows. If not given, equal to ``n_workers`` x 50.

    Example:
        >>> import geowombat as gw
        >>> from geowombat.core.parallel import ParallelTask
        >>>
        >>> def user_func(*args):
        >>>     data, num_workers = list(itertools.chain(*args))
        >>>     results = data.data.sum().compute(scheduler='threads', num_workers=num_workers)
        >>>     return results
        >>>
        >>> # Process 8 windows in parallel using threads
        >>> # Process 4 dask chunks in parallel using threads
        >>> # 32 total workers are needed
        >>> with gw.open('image.tif') as src:
        >>>     pt = ParallelTask(src, n_workers=8)
        >>>     res = pt.map(user_func, 4)
    """

    def __init__(self,
                 data,
                 row_chunks=None,
                 col_chunks=None,
                 padding=None,
                 scheduler='threads',
                 n_workers=1,
                 n_chunks=None):

        self.data = data
        self.padding = padding
        self.n = None
        self.scheduler = scheduler
        self.executor = _EXEC_DICT[scheduler]
        self.n_workers = n_workers
        self.n_chunks = n_chunks

        self.windows = None
        self.n_windows = None

        if not isinstance(self.n_chunks, int):
            self.n_chunks = self.n_workers * 50

        self._setup(row_chunks, col_chunks)

    def _setup(self, row_chunks, col_chunks):

        rchunksize = row_chunks if isinstance(row_chunks, int) else self.data.gw.row_chunks
        cchunksize = col_chunks if isinstance(col_chunks, int) else self.data.gw.col_chunks

        self.windows = get_window_offsets(self.data.gw.nrows,
                                          self.data.gw.ncols,
                                          rchunksize,
                                          cchunksize,
                                          return_as='list',
                                          padding=self.padding)

        self.n_windows = len(self.windows)

    def map(self, func, *args):

        """
        Maps a function over a DataArray

        Args:
            func (func): The function to apply to the ``data`` chunks.

        Returns:
            ``list``: Results for each data chunk.
        """

        results = []

        # Iterate over the windows in chunks
        for wchunk in range(0, self.n_windows, self.n_chunks):

            window_slice = self.windows[wchunk:wchunk + self.n_chunks]
            n_windows_slice = len(window_slice)

            if self.padding:

                # Read the padded window
                if len(self.data.shape) == 2:
                    data_gen = ((self.data[w[1].row_off:w[1].row_off + w[1].height, w[1].col_off:w[1].col_off + w[1].width], *args) for widx, w in enumerate(window_slice))
                elif len(self.data.shape) == 3:
                    data_gen = ((self.data[:, w[1].row_off:w[1].row_off + w[1].height, w[1].col_off:w[1].col_off + w[1].width], *args) for widx, w in enumerate(window_slice))
                else:
                    data_gen = ((self.data[:, :, w[1].row_off:w[1].row_off + w[1].height, w[1].col_off:w[1].col_off + w[1].width], *args) for widx, w in enumerate(window_slice))

            else:

                if len(self.data.shape) == 2:
                    data_gen = ((self.data[w.row_off:w.row_off + w.height, w.col_off:w.col_off + w.width], *args) for widx, w in enumerate(window_slice))
                elif len(self.data.shape) == 3:
                    data_gen = ((self.data[:, w.row_off:w.row_off + w.height, w.col_off:w.col_off + w.width], *args) for widx, w in enumerate(window_slice))
                else:
                    data_gen = ((self.data[:, :, w.row_off:w.row_off + w.height, w.col_off:w.col_off + w.width], *args) for widx, w in enumerate(window_slice))

            if self.n_workers == 1:

                for result in tqdm(map(func, data_gen), total=n_windows_slice):
                    results.append(result)

            else:

                with self.executor(self.n_workers) as executor:

                    if self.scheduler == 'mpool':

                        for result in tqdm(executor.imap_unordered(func, data_gen), total=n_windows_slice):
                            results.append(result)

                    else:

                        for result in tqdm(executor.map(func, data_gen), total=n_windows_slice):
                            results.append(result)

        return results


import torch
from torch import nn
import torch.nn.functional as F
from torch.hub import load_state_dict_from_url
import numpy as np

from ..registry import HEADS

@HEADS.register_module
class LaneCls(nn.Module):
    def __init__(self, dim, cfg=None):
        super(LaneCls, self).__init__()
        self.cfg = cfg
        chan = cfg.featuremap_out_channel
        self.pool = torch.nn.Conv2d(chan, 8, 1)
        self.dim = dim
        self.total_dim = np.prod(dim)
        self.cls = torch.nn.Sequential(
            torch.nn.Linear(1800, 2048),
            torch.nn.ReLU(),
            torch.nn.Linear(2048, self.total_dim),
        )

    def forward(self, x):
        x = self.pool(x).view(-1, 1800)
        cls = self.cls(x).view(-1, *self.dim)
        output = {'cls': cls}
        return output


from flask import Flask
from flask import render_template
from flask import request
from flask import redirect  # 重定向
from flask_sqlalchemy import SQLAlchemy
import os

# 查找当前的py文件的绝对路径,再去掉最后一级
BASE_DIR = os.path.dirname(os.path.abspath(__name__))
# 拼接之后就是上传文件保存的绝对路径
UPLOAD_DIR = os.path.join(BASE_DIR, 'static', 'upload')

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://zx-nb:123456@49.234.54.195/flask1907'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

db = SQLAlchemy(app)

class student(db.Model):
    __tablename__ = 'student'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), unique=True)
    gender = db.Column(db.String(10), default='male')
    chinese = db.Column(db.Float, default=0.0)
    math = db.Column(db.Float, default=0.0)

@app.route('/')
def home():
    students = student.query.all()
    return render_template('base.html', student=students)

@app.route("/modify", methods=("POST", "GET"))
def modify():
    global students
    if request.method == "POST":
        uid = int(request.form["uid"])
        user = student.query.get(uid)
        user.gender = request.form["gender"]
        user.chinese = float(request.form["chinese"])
        user.math = float(request.form["math"])
        db.session.add(user)
        db.session.commit()

        # 拿到的上传图片
        upload_img = request.files["avatar"]
        # 拼接路径
        filepath = os.path.join(UPLOAD_DIR, user.name)
        # 保存图片
        print(filepath)
        upload_img.save(filepath)
        return redirect('/')

    else:
        # get请求获得参数是request.args
        uid = int(request.args['uid'])
        user = student.query.get(uid)
        return render_template('modify.html', user=user)

@app.route('/info')
def info():
    uid = int(request.args['uid'])
    user = student.query.get(uid)
    return render_template("info.html", user=user)

@app.route('/top5')
def top5():
    chinese_math = db.desc(student.chinese + student.math)
    user = student.query.order_by(chinese_math).limit(5)
    return render_template('top5.html', user=user)

if __name__ == '__main__':
    app.run()


# Question 6
"""
TO DO:
"""
# Created input prompt to save integer as variable sentence
sentence = input("Please enter a sentence: ")
# Make a list of the words in the sentence.
sentenceList = sentence.split()
# Create an empty list for converted sentence
finalList = []
# Create an empty string to place words of converted sentence
finalSentence = ''

# for a number (i) select every second element with a range from zero to length of sentence list.
for i in range(0, len(sentenceList), 2):
    # Add every second work to empyt list finalList
    finalList.append(sentenceList[i])
    # Add the first element of finalList to finalSentence
    finalSentence = finalList[0]
# for a number (a) select every element in finalList except the first
for a in range(1, len(finalList)):
    # Concatinate on a space (' ') and the next word in the list
    finalSentence = finalSentence+' '+finalList[a]

# Print the final sentence.
print(finalSentence)


N, C = map(int, input().split())
D = []
for i in range(C):
    D.append(list(map(int, input().split())))

c_arr = []
for i in range(N):
    c_arr.append(list(map(int, input().split())))

c_arr1 = []
c_arr2 = []
c_arr3 = []

for i in range(N):
    for j in range(N):
        if (i+j)%3 == 0:
            c_arr1.append(c_arr[i][j])
        if (i+j)%3 == 1:
            c_arr2.append(c_arr[i][j])
        if (i+j)%3 == 2:
            c_arr3.append(c_arr[i][j])

tmp = 1000*500*500

def cost(arr, aft):
    sum = 0
    for i in range(len(arr)):
        sum += D[arr[i]-1][aft]
    return sum

ans_arr1 = []
ans_arr2 = []
ans_arr3 = []

for l in range(C):
    ans_arr1.append(cost(c_arr1, l))
for l in range(C):
    ans_arr2.append(cost(c_arr2, l))
for l in range(C):
    ans_arr3.append(cost(c_arr3, l))

ans_arr = []
for l in range(C):
    for m in range(C):
        for n in range(C):
            if l == n or n == m or m == l:
                pass
            else:
                ans_arr.append(ans_arr1[l]+ans_arr2[m]+ans_arr3[n])

print(min(ans_arr))


from __future__ import print_function, division, absolute_import
from .lstd import LSTDLearner
from ..testutil import MockPolicy, MockPolicyFeatureModule

import unittest
import scipy
from numpy.testing import assert_array_almost_equal, assert_almost_equal

from pybrain.datasets import ReinforcementDataSet
from librl.policies.boltzmann import BoltzmanPolicy

class LSTDLearnerTestCase(unittest.TestCase):
    def setUp(self):
        self.policy = MockPolicy({}, [0, 0])
        self.module = MockPolicyFeatureModule(self.policy)
        self.learner = LSTDLearner(module=self.module,
                                   cssinitial=1,
                                   cssdecay=1, # css means critic step size
                                   assinitial=1,
                                   assdecay=1, # ass means actor steps size
                                   rdecay=1, # reward decay weight
                                   maxcriticnorm=100, # maximum critic norm
                                   tracestepsize=0.9, # trace stepsize
                                   parambound = None # bound for the parameters
                                   )

    @unittest.skip('ignore the test before lstd algorithm is finalized')
    def testCritic(self):
        assert_array_almost_equal([[1, 0], [0, 1]], self.learner.A)
        assert_array_almost_equal([0, 0], self.learner.b)

        self.learner.A = scipy.array([[2, 0],
                                      [0, 4]], dtype=float)
        self.learner.b = scipy.array([2, 3], dtype=float)
        self.learner.z = scipy.array([-1, 1], dtype=float)
        self.learner.k = 1
        self.learner.critic(1, scipy.array([1, 2]), 2, scipy.array([4, 3]))
        # r = -inv(A) * b
        assert_array_almost_equal([-1, -0.75], self.learner.r)
        # featureDifference = [3, 1], gamma = 0.5
        # A += 0.5 * ( [-1] * [3, 1] - [[2, 0] )
        #              [ 1]             [0, 4]]
        assert_array_almost_equal(0.5, self.learner.gamma())
        assert_array_almost_equal([[-0.5, -0.5],
                                   [1.5, 2.5]], self.learner.A)