from tkinter import Tk, Label, Button
from tkinter.ttk import Combobox

R3 = range(3)
BUTTONS, MOVES = [[ None for _ in R3] for _ in R3], {"N":0}
position, bconfig = {"anchor":"center", "x":125}, {"highlightbackground":"darkgray", "highlightthickness":1}

def win() -> bool:
    return BUTTONS[2][0] == BUTTONS[1][1] == BUTTONS[0][2] or 1 == len(set([BUTTONS[_][_] for _ in R3])) \
            or any(True if 1 in [len(set(row)), len(set([BUTTONS[_][r] for _ in R3]))] else False for r, row in enumerate(BUTTONS))

def next_move(border, row, column):

    lbl = Label(border, text=player.get(), width=3, **bconfig)
    BUTTONS[row][column].destroy()
    MOVES["N"] += 1

    lbl.grid(row=row, column=column)
    BUTTONS[row][column] = lbl["text"]

    if 5 <= MOVES["N"]:
        if win():
            Label(root, text=f"{player.get()} won!", **bconfig).place(y=190, **position)
    player.set("O" if "X" == player.get() else "X")

root = Tk()
root.title("oldlady's game")
root.geometry("250x250")

player = Combobox(root, values=["X", "O"], width=3)
player.place(y=50, **position)

border = Label(root, highlightbackground="black", highlightthickness=1)
border.place(y=125, **position)

for row in R3:
    for column in R3:
        def move(border=border, row:int=row, column:int=column) -> None:
            next_move(border, row, column)

        btn = Button(border, command=move, width=3)
        btn.grid(row=row, column=column)
        BUTTONS[row][column] = btn

root.mainloop()


import datetime as dt

today = dt.datetime.today().date()
offset = max(1, (today.weekday() + 6) % 7 - 3)
timedelta = dt.timedelta(offset)
recent_working_day = (today - timedelta).strftime("%d%m%y")

zip_file_dir = [r"C:\Users\pavithra\Documents\fintuple-automation-projects\BseBhavCopyNI\bhav_copy_ni\zip_files"]
csv_file_dir = [r"C:\Users\pavithra\Documents\fintuple-automation-projects\BseBhavCopyNI\bhav_copy_ni\csv_files"]

file_extensions = [".zip", ".csv"]

zip_file_name = ["nse_" + recent_working_day]
csv_file_name = ["bhav_copy_" + recent_working_day]

nse_bhav_copy_url = ["https://www.bseindia.com/download/BhavCopy/Equity/EQ_ISINCODE_"]


currency_names_list = [
["FIVE THOUSAND"],
["ONE THOUSAND"],
["FIVE HUNDRED"],
["ONE HUNDRED"],
["FIFTY"],
["TWENTY"],
["TEN"],
["FIVE"],
["TWO"],
["ONE"]
]

labels = [
"Cash in Five Thousand Rupees",
"Cash in One Thousand Rupees",
"Cash in Five Hundred Rupees",
"Cash in One Hundred Rupees",
"Cash in Fifty Rupees",
"Cash in Twenty Rupees",
"Cash in Ten Rupees",
"Cash in Five Rupees",
"Cash in Two Rupees",
"Cash in One Rupees"
]

remaining_cash = [
["FIVE THOUSAND", 5000000],
["ONE THOUSAND", 1000000],
["FIVE HUNDRED", 500000],
["ONE HUNDRED", 1000000],
["FIFTY", 500000],
["TWENTY", 20000],
["TEN", 10000],
["FIVE", 5000],
["TWO", 2000],
["ONE", 1000]
]

def check_cash_register(price, cash, currency_names_list=remaining_cash):
    status = get_status(price, cash, currency_names_list)
    change = get_change(price, cash, currency_names_list, status)
    return {
        "status": status,
        "change": change
    }

def get_status(price, cash, currency_names_list):
    cash_in_drawer_list = currency_names_list
    cinda = []
    cash_in_drawer = 0
    for item in cash_in_drawer_list:
        cinda.append(item[1])
        cash_in_drawer += item[1]
    ac = [5000, 1000, 500, 100, 50, 20, 10, 5, 2, 1]
    status = ""
    total_change = float(cash) - float(price)
    if total_change > cash_in_drawer:
        status = "INSUFFICIENT_FUNDS"
    elif total_change == cash_in_drawer:
        status = "CLOSED"
    else:
        status = "OPEN"
        i = 0
        while i < len(cinda):
            if total_change >= ac[i] and cinda[i] >= ac[i]:
                total_change -= ac[i]
                cinda[i] -= ac[i]
                total_change = float(format(total_change, ".2f"))
                cinda[i] = float(format(cinda[i], ".2f"))
            else:
                i += 1

        if total_change != 0:
            status = "INSUFFICIENT_FUNDS"

    return status

def get_change(price, cash, currency_names_list, status):
    total_change = float(cash) - float(price)
    changeArray = []
    currentCurrencyName = ""
    currentCurrency = 0
    change = []
    ac = [
    ["FIVE THOUSAND", 5000],
    ["ONE THOUSAND", 1000],
    ["FIVE HUNDRED", 500],
    ["ONE HUNDRED", 100],
    ["FIFTY", 50],
    ["TWENTY", 20],
    ["TEN", 10],
    ["FIVE", 5],
    ["TWO", 2],
    ["ONE", 1]
    ]
    cash_in_drawer_list = currency_names_list
    i = 0
    j = 0
    while i < len(ac):
        if total_change >= ac[i][1] and cash_in_drawer_list[i][1] >= ac[i][1]:
            total_change -= ac[i][1]
            cash_in_drawer_list[i][1] -= ac[i][1]
            total_change = float(format(total_change, ".2f"))
            cash_in_drawer_list[i][1] = float(format(cash_in_drawer_list[i][1], ".2f"))
            j += 1
        else:
            if j != 0:
                currentCurrencyName = ac[i][0]
                currentCurrency = ac[i][1] * j
                if status != "INSUFFICIENT_FUNDS":
                    change = [currentCurrencyName, currentCurrency]
                    changeArray.append(change)
            j = 0
            i += 1
    return changeArray


import os
import unittest
import numpy as np
import math
import tempfile
import struct

from neurogen import encoder
from neurogen import mesh as ngmesh
from neurogen import info as nginfo
from neurogen import volume as ngvol

dir_path = os.path.dirname(os.path.realpath(__file__))

class TestEncodingDecoding(unittest.TestCase):
    def test_encode_decode(self):
        vertices = np.loadtxt(os.path.join(dir_path, 'test_data/sphere_vertices.npy')).astype(np.uint32)
        faces = np.loadtxt(os.path.join(dir_path, 'test_data/sphere_faces.npy')).astype(np.uint32)

        buffer = encoder.encode_vertices_faces(vertices, faces, compression_level=5)
        vertices_decoded, faces_decoded = encoder.decode_buffer(buffer)

        self.assertTrue(
            np.array_equal(
                np.sort(vertices[faces].flat),
                np.sort(vertices_decoded[faces_decoded].flat)
            )
        )

    def test_info_file_specification(self):
        size, radius = 100, 20
        volume = np.zeros((size,size,size)).astype('uint8')
        volshape = volume.shape
        x0, y0, z0 = int(np.floor(volshape[0]/2)), \
                    int(np.floor(volshape[1]/2)), int(np.floor(volshape[2]/2))
        for x in range(x0-radius, x0+radius+1):
            for y in range(y0-radius, y0+radius+1):
                for z in range(z0-radius, z0+radius+1):
                    deb = radius - abs(x0-x) - abs(y0-y) - abs(z0-z)
                    if (deb)>=0:
                        volume[x,y,z] = 1

        vol_dtype = volume.dtype

        with tempfile.TemporaryDirectory() as temp_dir:
            info_dict = nginfo.info_image(directory=str(temp_dir),
                                          dtype=vol_dtype,
                                          chunk_size=[64,64,64],
                                          size=volume.shape)
            scales = info_dict['scales']
            num_scales = len(scales)
            self.assertTrue(info_dict['scales'][-1]['size'] == [1,1,1])

            encodevolume = ngvol.generate_recursive_chunked_representation(volume=volume,
                                                                           info=info_dict,
                                                                           dtype=vol_dtype,
                                                                           directory=temp_dir,
                                                                           blurring_method='average')
            encoded_images = os.path.join(temp_dir, str(num_scales-1))

            for encoded_base in os.listdir(encoded_images):
                split = encoded_base.split("_")
                split = [list(map(int, im.split("-"))) for im in split]
                x1, x0 = split[0][1], split[0][0]
                y1, y0 = split[1][1], split[1][0]
                z1, z0 = split[2][1], split[2][0]

                encoded_fullpath = os.path.join(encoded_images,encoded_base)
                with open(encoded_fullpath, 'rb') as enc_image:
                    decoded_image = ngvol.decode_volume(enc_image.read(), dtype=vol_dtype, shape=(x1-x0, y1-y0, z1-z0))
                    self.assertTrue((volume[x0:x1, y0:y1, z0:z1] == decoded_image).all())

            for i in range(num_scales):
                scale_key = scales[i]['key']
                scale_dir = os.path.join(str(temp_dir), scale_key)
                scale_size = scales[i]['size']
                num_directories = len(os.listdir(scale_dir))
                xfiles = np.ceil(scale_size[0]/64)
                yfiles = np.ceil(scale_size[1]/64)
                zfiles = np.ceil(scale_size[2]/64)
                self.assertTrue(xfiles*yfiles*zfiles == num_directories)

    def test_mesh_generation(self):
        vertices = np.loadtxt(os.path.join(dir_path, 'test_data/sphere_vertices.npy')).astype(np.uint32)
        faces = np.loadtxt(os.path.join(dir_path, 'test_data/sphere_faces.npy')).astype(np.uint32)

        # temp_dir = tempfile.TemporaryDirectory()
        with tempfile.TemporaryDirectory() as temp_dir:
            offset_check = 0
            ngmesh.fulloctree_decomposition(vertices=vertices,
                                          faces=faces,
                                          num_lods=3,
                                          segment_id=1,
                                          directory=str(temp_dir))

            manifest = os.path.join(temp_dir, "meshdir", "1.index")
            eof = os.path.getsize(manifest)
            with open(manifest, 'rb') as manifest_file:
                chunkshapes = list(struct.unpack_from("<3f",manifest_file.read(12)))
                gridorigin = list(struct.unpack_from("<3f",manifest_file.read(12), offset=0))
                num_lods = int(struct.unpack("<I",manifest_file.read(4))[0])
                lod_scales = list((struct.unpack("<"+ str(num_lods)+ "f",manifest_file.read(num_lods*4))))

                self.assertTrue(((vertices.max(axis=0)-vertices.min(axis=0))/(num_lods) == chunkshapes).all())
                self.assertTrue((vertices.min(axis=0)==gridorigin).all())
                self.assertTrue(len(lod_scales)==num_lods)

                for num in range(num_lods):
                    vertex_offsets = list((struct.unpack("<3f", manifest_file.read(12))))
                num_fragments_per_lod = list((struct.unpack("<"+ str(num_lods)+ "I",manifest_file.read(num_lods*4))))
                for i in range(0, num_lods):
                    frags_inthisLOD = int(num_fragments_per_lod[i])
                    if i != (num_lods-1):
                        self.assertTrue(frags_inthisLOD%8==0)
                    fragment_positions_x = list(struct.unpack_from("<" + str(frags_inthisLOD) + "I", manifest_file.read(frags_inthisLOD*4)))
                    fragment_positions_y = list(struct.unpack_from("<" + str(frags_inthisLOD) + "I", manifest_file.read(frags_inthisLOD*4)))
                    fragment_positions_z = list(struct.unpack_from("<" + str(frags_inthisLOD) + "I", manifest_file.read(frags_inthisLOD*4)))
                    fragment_offsets = list(struct.unpack_from("<" + str(frags_inthisLOD)+"I", manifest_file.read(frags_inthisLOD*4)))
                    offset_check = offset_check + sum(fragment_offsets)

                self.assertTrue(eof==manifest_file.tell())

            fragment = os.path.join(temp_dir, "meshdir", "1")
            fragment_file_size = os.path.getsize(fragment)
            self.assertTrue(offset_check==fragment_file_size)

if __name__ == '__main__':
    unittest.main()


"""
kernel_medodids contains the class KernelMedoids which is used to run the kernel algorithm based on the paper:
    Randomized Clustered Nystrom for Large-Scale Kernel Machines

KernelMedoids can be initialized from GAM with the desired arguments

The fit function is called from GAM

The fit function calls cluster which is then completes the kernel algorithm

The algorithm utilizes nystroms and the vaex framework to improve efficiency

After the kernel algorithm is complete, it needs to call another clustering algorithm to complete the clustering

Currently that algorithm is set to be the parallel medoids algorithm

"""

import sklearn
import vaex
from gam_package.preprocessor.preprocessor import load_data, setArguments
import csv
import logging
from collections import Counter

import matplotlib.pylab as plt
import numpy as np
from numpy import sqrt, sum, matmul
from math import exp
from scipy.sparse.linalg import svds
from timeit import default_timer
import math
from numpy.linalg import svd

from gam_package.medoids_algorithms.k_medoids import KMedoids
from gam_package.distance_functions.kendall_tau_distance import mergeSortDistance
from gam_package.distance_functions.spearman_distance import spearman_squared_distance
from gam_package.distance_functions.euclidean_distance import euclidean_distance

from gam_package.medoids_algorithms.parallel_medoids import ParallelMedoids
from gam_package.plot_functions.plot import parallelPlot, radarPlot, facetedRadarPlot, silhouetteAnalysis
from gam_package.medoids_algorithms.ranked_medoids import RankedMedoids
from gam_package.medoids_algorithms.bandit_pam import BanditPAM

import matplotlib.pyplot as plt
import pandas as pd

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.cluster import KMeans

class KernelMedoids:

    def __init__(self, n_clusters=1, max_iter=100, tol=0.0001, attributions_path="data/mushrooms.csv",
                 CLUSTER_NUM=3, TARGET_DIM=6, SKETCH_SIZE=60, SIGMA=1, dataset=None):

        self.attributions_path = attributions_path
        self.n_clusters = n_clusters
        self.max_iter = max_iter
        self.tol = tol
        self.centers = None
        self.members = None

        ## Parse parameters from command line arguments
        self.CLUSTER_NUM = CLUSTER_NUM
        self.TARGET_DIM = CLUSTER_NUM * 2
        self.SKETCH_SIZE = TARGET_DIM * 10
        self.SIGMA = SIGMA
        self.dataset = dataset

        print("####################################")
        print("cluster number = ", CLUSTER_NUM)
        print("target dimension = ", TARGET_DIM)
        print("sketch size = ", SKETCH_SIZE)
        print("sigma = ", SIGMA)

        # import os
        # cwd = os.getcwd()  # Get the current working directory (cwd)
        # files = os.listdir(cwd)  # Get all the files in that directory
        # print("Files in %r: %s" % (cwd, files))

        self.applyOneOverSquareRoot_vfunc = np.vectorize(self.applyOneOverSquareRoot)

    def fit(self, X=None, plotit=False, verbose=True, attributions_path=None):

        ## Loads data
        # self.data = vaex.from_csv(self.attributions_path, copy_index = True)

        self.args = setArguments(self.dataset)
        total_data, total_labels, sigma, feature_labels = load_data(self.args)
        # total_data = total_data[np.random.choice(range(len(total_data)), size=args.sample_size, replace=False)]

        ## Parse the data to get labels and features
        # RDD[(Int, Array[Double])]
        # label_vector_rdd = df.rdd.map(pair= > (pair[0], pair[1]) )
        self.total_data = total_data
        x = total_data
        self.sigma = sigma
        self.feature_labels = feature_labels

        if total_labels is None:
            y = np.array(range(x.shape[0]))
        else:
            y = np.array(total_labels)
        label_vector_rdd = vaex.from_arrays(x=x, y=y)

        self.data = (total_data, total_labels, sigma, feature_labels)

        ## Perform kernel k-means with Nystrom approximation
        ## (Array[String], Array[String])
        self.cluster(label_vector_rdd, self.CLUSTER_NUM, self.TARGET_DIM, self.SKETCH_SIZE, self.SIGMA)

        return self.n, self.total_data, self.feature_labels, self.duration
        print("")

    '''
     * Compute the RBF kernel function of two vectors.
     def rbf(x1: Array[Double], x2: Array[Double], sigma: Double): Double
     '''

    def rbf(self, x1, x2, sigma):
        ## squared l2 distance between x1 and x2
        # dist = zip(x1, x2).map(lambda pair: pair[0] - pair[1]).map(lambda x: x * x).sum
        dist = zip(x1, x2)
        dist2 = list(map(lambda pair: pair[0] - pair[1], dist))
        dist3 = list(map(lambda x: x * x, dist2))
        sum_distance = sum(dist3)

        ## RBF kernel function is exp( - ||x1-x2||^2 / 2 / sigma^2 )
        return exp(-1 * sum_distance / (2 * sigma * sigma))

    '''
         * The Nystrom method for approximating the RBF kernel matrix.
         * Let K be n-by-n kernel matrix. The Nystrom method approximates
         * K by C * W^{-1}_l * C^T, where we set l = c/2.
         * The Nystrom feature vectors are the rows of C * W^{-1/2}_l.
         *
         * Input
         *  label_vector_rdd: RDD of labels and raw input feature vectors
         *  c: sketch size (k < s < c/2)
         *  sigma: kernel width parameter
         *
         * Output
         *  RDD of (lable, Nystrom feature vector) pairs
         *  the dimension of Nystrom feature vector is (c/
         return RDD[(Int, Vector)]
         '''

    def applyOneOverSquareRoot(self, s):
        return 1 / math.sqrt(s)

    '''
         * Locally compute the W matrix of Nystrom and its factorization W_{l}^{-1} = U * U^T.
         def nystrom_w_mat(data_samples: Array[Array[Double]], sigma: Double): DenseMatrix[Double]
         '''

    def nystrom_w_mat(self, data_samples, sigma):
        c = data_samples.shape[0]

        l = math.ceil(c * 0.5)

        ## Compute the W matrix of Nystrom method
        # w_mat = DenseMatrix.zeros[Double](c, c)
        w_mat = np.array([[0] * c] * c)
        # data_samplesX = data_samples.evaluate(data_samples.x)

        for j in range(c):
            for i in range(c):
                w_mat[i, j] = self.rbf(data_samples[i], data_samples[j], sigma)

        ## Compute matrix U such that U * U^T = W_{l}^{-1}
        U, S, V = svd(w_mat, full_matrices=False)

        # u_mat = U(::, 0 until l) # get matrix where only the first l values of every column is returned
        # u_mat = U[0:l,:]
        u_mat = U[:, 0:l]

        # s_arr = S(0 until l).toArray.map(lambda s: 1 / math.sqrt(s))
        s_arr = S[0:l]

        # s_arr.map(lambda s: 1 / math.sqrt(s))

        # s_arr = self.applyOneOverSquareRoot(s_arr)
        s_arr = self.applyOneOverSquareRoot_vfunc(s_arr)

        for j in range(0, l):
            # u_mat(::, j) *= s_arr(j)
            u_mat[:, j] = u_mat[:, j] * s_arr[j]
        return u_mat

    '''
     * Compute the RBF kernel functions of a vector and a collection of vectors.
     def rbf(x1: Array[Double], x2: Array[Array[Double]], sigma: Double): Array[Double]
     '''

    def rbf2(self, x1, x2, sigma):
        n = x2.shape[0]
        # print("n: ", n)
        kernel_arr = [0] * n
        # print("kernel_arr: ", kernel_arr)
        sigma_sq = 2 * sigma * sigma
        # print("sigma_sq: ", sigma_sq)
        for i in range(0, n):
            ## squared l2 distance between x1 and x2(i)
            # dist = zip(x1,x2[i])
            # dist2 = list(map(lambda pair: pair[0] - pair[1], dist))
            # dist3 = list(map(lambda x: x * x, dist2))
            # sum_distance = sum(dist3)
            # print("i: ", i)
            sum_distance = sqrt(sum((x1 - x2[i]) ** 2))
            # print("sum_distance: ", sum_distance)
            ## RBF kernel function
            kernel_arr[i] = exp(-sum_distance / sigma_sq)
            # print("kernel_arr[i]: ", kernel_arr[i])
        # print("kernel_arr: ", kernel_arr)
        # print("kernel_arr.shape: ", kernel_arr.shape)
        return kernel_arr

    def applyRBF2(self, row):
        # print("line 207, type(row): ", type(row))
        return self.rbf2(row, self.data_samples, self.sigma)

    def nystrom(self, label_vector_rdd, c, sigma):
        n = label_vector_rdd.shape[0]
        self.n = n

        ## Randomly sample about c points from the dataset
        frac = c / n
        # data_samples = label_vector_rdd.sample(false, frac).map(pair= > pair._2).collect
        # broadcast_samples = sc.broadcast(data_samples)
        # RDD[(Int, Array[Double])]
        data_samples = label_vector_rdd.sample(frac=frac)
        # data_samples = data_samples.x
        data_samples = data_samples.evaluate(data_samples.x)

        ## Compute the C matrix of the Nystrom method
        # rbf_fun = (x1: Array[Double], x2: Array[Array[Double]])

        # c_mat_rdd = label_vector_rdd
        # .map(pair => (pair._1, broadcast_rbf.value(pair._2, broadcast_samples.value)))
        # .map(pair => (pair._1, new DenseVector(pair._2)))

        self.data_samples = data_samples
        self.sigma = sigma
        c_mat_rddX = label_vector_rdd.apply(self.applyRBF2, arguments=[label_vector_rdd.x])

        # c_mat_rddX = label_vector_rdd.evaluate(c_mat_rddX)

        x = label_vector_rdd.evaluate(c_mat_rddX)
        y = label_vector_rdd.evaluate(label_vector_rdd.y)
        c_mat_rdd = vaex.from_arrays(x=x, y=y)

        ## Compute the W matrix of the Nystrom method
        ## decompose W as: U * U^T \approx W^{-1}
        u_mat = self.nystrom_w_mat(data_samples, sigma)

        print("")
        ## Compute the features extracted by Nystrom
        ## feature matrix is C * W^{-1/2}_{c/2}
        # nystrom_rdd = c_mat_rdd.map(lambda row: row.t * u_mat).map(lambda row: pair._2.t)
        print("")
        self.u_mat = u_mat
        nystrom_rddX = c_mat_rdd.apply(self.multiplyRowUMat, arguments=[c_mat_rdd.x])  # returned only x values

        x2 = c_mat_rdd.evaluate(nystrom_rddX)
        nystrom_rdd = vaex.from_arrays(x=x2, y=y)

        return nystrom_rdd

    def multiplyRowUMat(self, row):
        retVal = matmul(row, self.u_mat)
        # print("retVal: ", retVal)
        return retVal

    def multiplyRowVMat(self, row):
        retVal = matmul(self.v_mat, row)
        # print("multiplyRowVMat: ", retVal)
        return retVal

    '''
     Kernel k-means clustering with Nystrom approximation.
     Input
     *  label_vector_rdd: RDD of labels and raw input feature vectors
     *  k: cluster number
     *  s: target dimension of extracted feature vectors
     *  c: sketch size (k < s < c/2)
     *  sigma: kernel width parameter
     *
     * Output
     *  labels: Array of (true label, predicted label) pairs
     *  time: Array of the elapsed time of Nystrom, PCA, and k-means, respectively
     return (Array[String], Array[String])
    '''

    def cluster(self, label_vector_rdd, k, s, c, sigma):
        ## max number of iterations (can be tuned)
        MAX_ITER = 100

        # label_vector_rdd

        ## Extract features by the Nystrom method
        t0 = default_timer()

        # nystrom_rdd: RDD[(Int, Vector)]
        nystrom_rdd = self.nystrom(label_vector_rdd, c, sigma)
        t1 = default_timer() - t0

        ##label_vector_rdd.unpersist()
        print("####################################")
        print("Nystrom method costs  ", t1, "  seconds.")
        print("####################################")

        ## Extract s principal components from the Nystrom features
        ## The V matrix stored in a local dense matrix
        t2 = default_timer()
        # mat = new RowMatrix(nystrom_rdd.map(pair => pair._2))
        mat = nystrom_rdd.evaluate(nystrom_rdd.x)

        # svd: SingularValueDecomposition[RowMatrix, Matrix] = mat.computeSVD(s, computeU = false)
        U, S, Vt = svds(mat, k=s)
        print("")
        # v_mat: Matrix = svd.V.transpose
        # v_mat = V.transpose
        v_mat = Vt

        # nystrom_pca_rdd: RDD[(Int, Vector)] = nystrom_rdd
        #         .map(pair => (pair._1, broadcast_v_mat.value.multiply(pair._2)))
        #         .map(pair => (pair._1, Vectors.dense(pair._2.toArray)))

        self.v_mat = v_mat

        # nystrom_pca_rdd = nystrom_rdd.apply(, arguments[])

        nystrom_pca_rddX = nystrom_rdd.apply(self.multiplyRowVMat, arguments=[nystrom_rdd.x])

        x = nystrom_rdd.evaluate(nystrom_pca_rddX)
        # x = nystrom_pca_rddX
        y = nystrom_rdd.evaluate(nystrom_rdd.y)
        nystrom_pca_rdd = vaex.from_arrays(x=x, y=y)
        t3 = default_timer() - t2

        print("####################################")
        print("PCA costs  ", t3, "  seconds.")
        print("####################################")

        print('The scikit-learn version is {}.'.format(sklearn.__version__))

        ## K-means clustering over the extracted features
        t4 = default_timer()
        # feature_rdd: RDD[Vector] = nystrom_pca_rdd.map(pair => pair._2)
        feature_rdd = x
        # clusters = KMeans.train(feature_rdd, k, MAX_ITER)

        # kmedoids = KMedoids(n_clusters=k, max_iter=self.max_iter)
        # centers, members, duration = kmedoids.fit(feature_rdd)
        # banditPAM = BanditPAM(data=self.data)
        # n, total_data, feature_labels, duration = banditPAM.fit(X=feature_rdd, verbose=False)

        clusters = ParallelMedoids(attributions_path=self.args.attributions_path)
        n, dfp, mlist, duration = clusters.fit(X=feature_rdd, verbose=False, n_clusters=k)

        self.centers = clusters.centers
        self.members = clusters.members

        t5 = default_timer() - t4
        self.duration = default_timer() - t0
        print("####################################")
        print("K-means clustering costs  ", t5, "  seconds.")
        print("####################################")

        ## Predict labels
        # labels: Array[String] = nystrom_pca_rdd
        #         .map(pair => (pair._1, clusters.predict(pair._2)))
        #         .map(pair => pair._1.toString + " " + pair._2.toString)
        #         .collect()

        # return (labels, time)

    # /mnt/c/Users/charm/PycharmProjects/SparkKernelKMeans/data

# '''

if __name__ == '__main__':
    kernelMedoids = KernelMedoids(max_iter=1, dataset="mushrooms")
    n, total_data, feature_labels, duration = kernelMedoids.fit()
    print("centers: ", kernelMedoids.centers)
    print("members: ", kernelMedoids.members)


import numpy as np

wfs = ['Montage', 'Cybershake', 'Epig', 'Random', 'LIGO']
alphas = [0.2, 0.4, 0.6, 0.8]
algos = ['DeepWS', 'SCS', 'PSO', 'ICPCP']

for wf in wfs:
	savename = "./Cost-" + wf
	f = open(savename, "w")
	f.truncate()
	f.close()
	f = open(savename, "a")

	print('alphavalue', 'DeepWS_cost', 'DeepWS_penalty', 'SCS_cost',
	      'SCS_penalty', 'PSO_cost', 'PSO_penalty', 'IC-PCP_cost',
	      'IC-PCP_penalty', file=f)

	for alpha in alphas:
		filename = "../PlayOutput/" + wf + '-' + str(alpha) + '-Deadline.txt'
		fr = open(filename, 'r')
		lines = fr.readlines()
		dl = []
		for line in lines:
			line = line.rstrip('\n')
			dl.append(float(line))
		fr.close()

		print(alpha, ' ', end='', file=f)
		for algo in algos:
			filename = "../PlayOutput/" + wf + '-' + str(alpha) + '-' + algo + '.txt'
			fr = open(filename, 'r')
			lines = fr.readlines()
			times = []
			costs = []
			for line in lines:
				line = line.rstrip('\n').split(' ')
				times.append(float(line[0]))
				costs.append(float(line[1]))
			fr.close()

			pen = 0
			for i in range(len(times)):
				if times[i] > dl[i]: # 超时
					# pen = pen + (times[i] - dl[i]) * 4
					pen = pen + (times[i] - dl[i]) * 20

			oricost = np.mean(costs)
			if pen / len(times) > oricost * 0.3:
				pen = oricost * 0.3
			else:
				pen = pen / len(times)

			print(oricost, ' ',  pen, ' ',  end='', file=f)

		print("", file=f)


from django.conf.urls import url

from .views import ActivationView, CompanyRegistrationView, RegistrationView, activation_complete, registration_complete

urlpatterns = [
    url(r"^activate/complete/$", activation_complete, name="registration_activation_complete"),
    url(r"^activate/(?P<activation_key>\w+)/$", ActivationView.as_view(), name="registration_activate"),
    url(r"^register/$", RegistrationView.as_view(), name="registration_register"),
    url(r"^register/company/$", CompanyRegistrationView.as_view(), name="registration_register_company"),
    url(r"^register/complete/$", registration_complete, name="registration_complete"),
]


import itertools
from collections import OrderedDict
import numpy as np
import cv2

import torch
import torch.nn as nn
from torch.nn import Sequential as Seq, Linear, ReLU
import torch.nn.functional as F
from torch_geometric.data import Data, Batch

from . import base_networks
from . import graph_construction as gc
from . import constants
from .util import utilities as util_

class DeleteNet(nn.Module):

    def __init__(self, config):
        super(DeleteNet, self).__init__()
        self.node_encoder = base_networks.NodeEncoder(config['node_encoder_config'])
        self.bg_fusion_module = base_networks.LinearEncoder(config['bg_fusion_module_config'])

    def forward(self, graph):
        """DeleteNet forward pass.

        Note: Assume that the graph contains the background node as the first node.

        Args:
            graph: a torch_geometric.Data instance with attributes:
                - rgb: a [N, 256, h, w] torch.FloatTensor of ResnNet50+FPN rgb image features
                - depth: a [N, 3, h, w] torch.FloatTensor. XYZ image
                - mask: a [N, h, w] torch.FloatTensor of values in {0, 1}
                - orig_masks: a [N, H, W] torch.FloatTensor of values in {0, 1}. Original image size.
                - crop_indices: a [N, 4] torch.LongTensor. xmin, ymin, xmax, ymax.

        Returns:
            a [N] torch.FloatTensor of delete score logits. The first logit (background) is always low,
                so BG is never deleted.
        """
        encodings = self.node_encoder(graph)  # dictionary
        concat_features = torch.cat([encodings[key] for key in encodings], dim=1)  # [N, \sum_i d_i]

        bg_feature = concat_features[0:1]  # [1, \sum_i d_i]
        node_features = concat_features[1:]  # [N-1, \sum_i d_i]
        node_minus_bg_features = node_features - bg_feature  # [N-1, \sum_i d_i]

        node_delete_logits = self.bg_fusion_module(node_minus_bg_features)  # [N-1, 1]
        delete_logits = torch.cat([torch.ones((1, 1), device=constants.DEVICE) * -100,
                                   node_delete_logits], dim=0)
        return delete_logits[:,0]

class DeleteNetWrapper(base_networks.NetworkWrapper):

    def setup(self):

        if 'deletenet_model' in self.config:
            self.model = self.config['deletenet_model']
        else:
            self.model = DeleteNet(self.config)
        self.model.to(self.device)

    def get_new_potential_masks(self, masks, fg_mask):
        """Compute new potential masks.

        See if any connected components of fg_mask _setminus_ mask can be
            considered as a new mask. Concatenate them to masks.

        Args:
            masks: a [N, H, W] torch.Tensor with values in {0, 1}.
            fg_mask: a [H, W] torch.Tensor with values in {0, 1}.

        Returns:
            a [N + delta, H, W] np.ndarray of new masks. delta = #new_masks.
        """
        occupied_mask = masks.sum(dim=0) > 0.5

        fg_mask = fg_mask.cpu().numpy().astype(np.uint8)
        fg_mask[occupied_mask.cpu().numpy()] = 0
        fg_mask = cv2.erode(fg_mask, np.ones((3,3)), iterations=1)

        nc, components = cv2.connectedComponents(fg_mask, connectivity=8)
        components = torch.from_numpy(components).float().to(constants.DEVICE)
        for j in range(1, nc):
            mask = components == j
            component_size = mask.sum().float()
            if component_size > self.config['min_pixels_thresh']:
                masks = torch.cat([masks, mask[None].float()], dim=0)

        return masks

    def delete_scores(self, graph):
        """Compute delete scores for each node in the graph.

        Args:
            graph: a torch_geometric.Data instance

        Returns:
            a [N] torch.Tensor with values in [0, 1]
        """
        return torch.sigmoid(self.model(graph))


import os

import gymnasium as gym
from stable_baselines3.common.callbacks import CheckpointCallback
from stable_baselines3.ppo import PPO

import config
from model import TrackmaniaInputCallback

if os.path.exists("trackmanai_ppo_offline.zip"):
    model = PPO.load("trackmanai_ppo_offline", env=gym.make('TrackmaniaPreTraining-v1'), verbose=1,
                     tensorboard_log=config.tensorboard_log_path)
    reset_timesteps = False
else:
    model = PPO("MultiInputPolicy", 'TrackmaniaPreTraining-v1', verbose=1,
                tensorboard_log=config.tensorboard_log_path)
    reset_timesteps = True

# model.learning_rate = 1.0
# model.policy.optimizer = dadaptation.DAdaptAdam(model.policy.parameters(), lr=model.learning_rate)

while True:
    model.learn(total_timesteps=10000, log_interval=1, reset_num_timesteps=reset_timesteps,
                tb_log_name='trackmania_pretraining',
                callback=[
                    CheckpointCallback(save_freq=5000, save_path=config.checkpoint_path, name_prefix='trackmania_v1',
                                       save_replay_buffer=True, save_vecnormalize=True),
                    TrackmaniaInputCallback()])
    model.save("trackmanai_ppo_offline")
    reset_timeswdswasdteps = False


n = int(input("Enter the number of rows: "))
m = (2 * n) - 2
for i in range(0, n):
    for j in range(0, m):
        print(end=" ")
    m = m - 1  # decrementing m after each loop
    for j in range(0, i + 1):
        # printing full Triangle pyramid using stars
        print("* ", end=' ')
    print(" ")


import cv2
from imutils.object_detection import non_max_suppression
import numpy as np
from operator import itemgetter

NEWH, NEWW = 320, 320
MIN_CONFIDENCE = 0.5

def box_in_box(b, outer_box) :
    x1, y1, x2, y2 = outer_box
    return (x1-5) < b[0] < (x2) and (x1) < b[2] < (x2+5) and (y1-5) < b[1] < (y2) and (y1) < b[3]< (y2+5)

def mser_regs(gray_img) :
    mser = cv2.MSER_create(8,80, 8000)
    msers, bboxes = mser.detectRegions(gray_img)
    return msers, bboxes

def east(image, H, W) :
    # define the two output layer names for the EAST detector model that
    # we are interested -- the first is the output probabilities and the
    # second can be used to derive the bounding box coordinates of text
    layerNames = ["feature_fusion/Conv_7/Sigmoid", "feature_fusion/concat_3"]
    net = cv2.dnn.readNet("frozen_east_text_detection.pb")

    # construct a blob from the image and then perform a forward pass of
    # the model to obtain the two output layer sets
    blob = cv2.dnn.blobFromImage(image, 1.0, (W, H), (123.68, 116.78, 103.94), swapRB=True, crop=False)
    net.setInput(blob)
    (scores, geometry) = net.forward(layerNames)

    # grab the number of rows and columns from the scores volume, then
    # initialize our set of bounding box rectangles and corresponding
    # confidence scores
    (numRows, numCols) = scores.shape[2:4]
    rects = []
    confidences = []

    # loop over the number of rows
    for y in range(0, numRows):
        # extract the scores (probabilities), followed by the geometrical
        # data used to derive potential bounding box coordinates that
        # surround text
        scoresData = scores[0, 0, y]
        xData0 = geometry[0, 0, y]
        xData1 = geometry[0, 1, y]
        xData2 = geometry[0, 2, y]
        xData3 = geometry[0, 3, y]
        anglesData = geometry[0, 4, y]

        # loop over the number of columns
        for x in range(0, numCols):
            # if our score does not have sufficient probability, ignore it
            if scoresData[x] < MIN_CONFIDENCE:
                continue

            # compute the offset factor as our resulting feature maps will
            # be 4x smaller than the input image
            (offsetX, offsetY) = (x * 4.0, y * 4.0)

            # extract the rotation angle for the prediction and then
            # compute the sin and cosine
            angle = anglesData[x]
            cos = np.cos(angle)
            sin = np.sin(angle)

            # use the geometry volume to derive the width and height of
            # the bounding box
            h = xData0[x] + xData2[x]
            w = xData1[x] + xData3[x]

            # compute both the starting and ending (x, y)-coordinates for
            # the text prediction bounding box
            endX = int(offsetX + (cos * xData1[x]) + (sin * xData2[x]))
            endY = int(offsetY - (sin * xData1[x]) + (cos * xData2[x]))
            startX = int(endX - w)
            startY = int(endY - h)

            # add the bounding box coordinates and probability score to
            # our respective lists
            rects.append((startX, startY, endX, endY))
            confidences.append(scoresData[x])

    # apply non-maxima suppression to suppress weak, overlapping bounding
    # boxes
    bboxes = non_max_suppression(np.array(rects), probs=confidences)
    return bboxes

def non_max_suppression_fast(boxes, overlapThresh = 0.99):
    # if there are no boxes, return an empty list
    boxes = np.array(boxes)
    if len(boxes) == 0:
        return []
	# if the bounding boxes integers, convert them to floats --
	# this is important since we'll be doing a bunch of divisions
    if boxes.dtype.kind == "i":
        boxes = boxes.astype("float")
	# initialize the list of picked indexes
    pick = []
	# grab the coordinates of the bounding boxes
    x1 = boxes[:,0]
    y1 = boxes[:,1]
    x2 = boxes[:,2]
    y2 = boxes[:,3]
	# compute the area of the bounding boxes and sort the bounding
	# boxes by the bottom-right y-coordinate of the bounding box
    area = (x2 - x1 + 1) * (y2 - y1 + 1)
    idxs = np.argsort(y2)
	# keep looping while some indexes still remain in the indexes
	# list
    while len(idxs) > 0:
		# grab the last index in the indexes list and add the
		# index value to the list of picked indexes
        last = len(idxs) - 1
        i = idxs[last]
        pick.append(i)
		# find the largest (x, y) coordinates for the start of
		# the bounding box and the smallest (x, y) coordinates
		# for the end of the bounding box
        xx1 = np.maximum(x1[i], x1[idxs[:last]])
        yy1 = np.maximum(y1[i], y1[idxs[:last]])
        xx2 = np.minimum(x2[i], x2[idxs[:last]])
        yy2 = np.minimum(y2[i], y2[idxs[:last]])
		# compute the width and height of the bounding box
        w = np.maximum(0, xx2 - xx1 + 1)
        h = np.maximum(0, yy2 - yy1 + 1)
		# compute the ratio of overlap
        overlap = (w * h) / area[idxs[:last]]
		# delete all indexes from the index list that have
        idxs = np.delete(idxs, np.concatenate(([last], np.where(overlap > overlapThresh)[0])))
	# return only the bounding boxes that were picked using the
	# integer data type
    return boxes[pick].astype("int")

def rescale(boxes, rW, rH) :
    return [[int(startX * rW), int(startY * rH), int(endX * rW), int(endY * rH)] for (startX, startY, endX, endY) in boxes]

def link_letters_to_words(mser_boxes, bboxes) :
    letters = [[] for i in range(len(bboxes))]
    for mser_box in mser_boxes :
        for i in range(len(bboxes)) :
            if box_in_box(mser_box, bboxes[i]) :
                letters[i].append(mser_box)
                break
    return letters

def word_seq(bboxes) :
    bboxes = sorted(bboxes, key = itemgetter(1))
    j, sorted_bboxes = 0, []
    for i in range(len(bboxes) - 1) :
        if abs(bboxes[i + 1][1] - bboxes[i][1]) > 5 :
            sorted_bboxes += sorted(bboxes[j:i+1], key = itemgetter(0))
            j = i + 1
    sorted_bboxes += sorted(bboxes[j:], key = itemgetter(0))
    return sorted_bboxes

def get_bboxes(orig_img, gray_img):
    image = orig_img.copy()
    (H, W) = image.shape[:2]

    rW = W / float(NEWW)
    rH = H / float(NEWH)

    image = cv2.resize(image, (NEWW, NEWH))
    (H, W) = image.shape[:2]

    bboxes = east(image, H, W)
    msers = mser_regs(gray_img)
    mser_boxes = msers[1]
    mser_boxes = [[b[0], b[1], b[0] + b[2] - 1, b[1] + b[3] - 1] for b in mser_boxes]
    mser_boxes = non_max_suppression_fast(mser_boxes)

    ordered_words = word_seq(rescale(bboxes, rW, rH))
    letters = [word_seq(l) for l in link_letters_to_words(mser_boxes, ordered_words)]

    for (startX, startY, endX, endY) in ordered_words:
        cv2.rectangle(orig_img, (startX, startY), (endX, endY), (0, 255, 0), 2)
#    for _ in letters:
 #       for b in _ :
  #          cv2.rectangle(orig_img, (b[0], b[1]), (b[2], b[3]), (255,0,0), 1)

    cv2.imshow("Text Detection", orig_img)
    cv2.waitKey(0)

    letter_imgs = [[orig_img[b[1] : b[3] , b[0] : b[2] ] for b in _] for _ in letters]
    word_imgs = [orig_img[b[1] : b[3], b[0] : b[2]] for b in ordered_words]

    return letter_imgs, word_imgs


from os import strerror

counters = {chr(ch): 0 for ch in range(ord('a'), ord('z') + 1)}
file_name = input("Enter the name of the file to analyze: ")
try:
    file = open(file_name, "rt")
    for line in file:
        for char in line:
            if char.isalpha():
                counters[char.lower()] += 1
    file.close()
    file = open(file_name + '.hist', 'wt')
    # Note: we have used a lambda to access the directory items and set reverse to True to obtain a valid order..
    for char in sorted(counters.keys(), key=lambda x: counters[x], reverse=True):
        c = counters[char]
        if c > 0:
            file.write(char + ' -> ' + str(c) + '\n')
    file.close()
except IOError as e:
    print("An I/O error occurred: ", strerror(e.errno))

MISCELANEOS/Trabajando con archivos reales/test1.py


import math

def calc_box_height(input):
    x = 1
    while x**2 < input:
        x += 2

    return x

# 0 is the bottom, move clockwise
def calc_box_side(box_height, input):
    for i in range(4):
        max = (box_height**2)-(box_height*i) + i + 1
        min = max - box_height
        if i is 3:
            min += 1
        row = range(min, max)
        if input in row:
            return list(row) if i % 2 is 0 else list(reversed(row))

def solve(input):
    box_height = calc_box_height(input)
    box_side = calc_box_side(box_height, input)
    if box_side is None:
        return 0

    d_center = math.floor(box_height/2)
    pos = box_side.index(input)
    d_axis = abs(d_center - pos)
    return d_axis + d_center

grade_sheet = [
    ('Test 1', 1, 0),
    ('Test 2', 12, 3),
    ('Test 3', 23, 2),
    ('Test 4', 1024, 31)
]

for name, input, expected in grade_sheet:
    solution = solve(input)
    if expected is not solution:
        print('[x] %s failed: got %s' % (name, solution))
    else:
        print('[] %s passed' % name)

input = 325489
print('Solution: %s' % solve(input))


import pandas as pd
import requests  # urlopen
from bs4 import BeautifulSoup
import re
import time
from random import randrange

v_title = []
v_link = []
v_nota = []

for page in range(1, 100):

    time.sleep(randrange(1, 3))

    url = 'https://torrentfilmes.net/page/' + str(page)
    headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'}
    print('Getting Url: ' + url)
    r = requests.get(url, headers=headers)

    if r.status_code == 200:
        html = r.text
        soup = BeautifulSoup(html, 'html.parser')
        t = soup.find("div", {"class": "listagem"})

        for li in t.findAll('div', attrs={'class': 'item'}):

            a = li.find('a')

            nota = li.find('div', attrs={'class': 'nota'})

            try:
                title = a['title']
            except Exception as e:
                title = ''

            try:
                link  = a['href']
            except Exception as e:
                link = ''

            try:
                nota = re.sub(r"\s+", " ", nota.get_text()).strip()
            except Exception as e:
                nota = ''

            if title != '':
                v_title.append(title)
                v_link.append(link)
                v_nota.append(nota)

    else:
        print(r.status_code)

df = pd.DataFrame({'Title': v_title, 'Link': v_link, 'Nota': v_nota})
#df.to_csv('filmes_olx.csv', index=False, encoding='utf-8')


import tkinter as tk
from newwindow import NewWindow

class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title("Main Window")
        self.geometry("400x300")

        self.button = tk.Button(self, text="Open New Window", command=self.open_new_window)
        self.button.pack(pady=20)

    def open_new_window(self):
        new_window = NewWindow(self)
        new_window.grab_set()


import torch
import torch.nn as nn

def mesh_grid(B, H, W, D):
    # batches not implented
    x = torch.arange(H)
    y = torch.arange(W)
    z = torch.arange(D)
    mesh = torch.stack(torch.meshgrid(x, y, z)[::-1], 0)
    mesh = mesh.unsqueeze(0)
    return mesh.repeat([B,1,1,1,1])

def norm_grid(v_grid):
    _, _, H, W, D = v_grid.size()

    # scale grid to [-1,1]
    v_grid_norm = torch.zeros_like(v_grid)
    v_grid_norm[:, 0, :, :] = 2.0 * v_grid[:, 0, :, :] / (D - 1) - 1.0
    v_grid_norm[:, 1, :, :] = 2.0 * v_grid[:, 1, :, :] / (H - 1) - 1.0
    v_grid_norm[:, 2, :, :] = 2.0 * v_grid[:, 2, :, :] / (W - 1) - 1.0
    return v_grid_norm.permute(0, 2, 3, 4, 1)

def flow_warp(img2, flow12, pad='border', mode='bilinear'):
    B, _, H, W, D = flow12.size()
    flow12 = torch.flip(flow12, [1])
    base_grid = mesh_grid(B, H, W, D).type_as(img2)  # B2HW

    v_grid = norm_grid(base_grid + flow12)  # BHW2
    im1_recons = nn.functional.grid_sample(
        img2, v_grid, mode=mode, padding_mode=pad, align_corners=True)

    return im1_recons

def get_occu_mask_bidirection(flow12, flow21, scale=0.01, bias=0.5):
    flow21_warped = flow_warp(flow21, flow12, pad='zeros')
    flow12_diff = flow12 + flow21_warped
    mag = (flow12 * flow12).sum(1, keepdim=True) + \
          (flow21_warped * flow21_warped).sum(1, keepdim=True)
    occ_thresh = scale * mag + bias
    occ = (flow12_diff * flow12_diff).sum(1, keepdim=True) > occ_thresh
    return occ.float()


import os
import yaml
import pandas as pd
from glob import glob
import argparse

raise NotImplementedError("Need to rewrite for new folder naming strategies.")

parser = argparse.\
    ArgumentParser(description='Aggregate CV folds to get summary statistics/')

parser.add_argument('--folder', type=str, help="Folder containing the fold results as results.")

args = parser.parse_args()

# Read in results for each fold
fold_folders = glob(os.path.join(args.tune_dir, 'fold_*/'))

results = []
for folder_path in fold_folders:

    # path/fold_IDX/ -> fold_IDX
    folder_name = folder_path.split('/')[-2]
    fold_idx = int(folder_name.split('_')[1])

    # load results fpath
    results_fpath = os.path.join(folder_path, 'results.yaml')
    with open(results_fpath, 'r') as file:
        res = yaml.safe_load(file)
    res['fold'] = fold_idx

    results.append(res)

# format results to pandas
results = pd.DataFrame(results)
results = results.set_index('fold')
results = results[['val_c_index', 'val_loss',
                   'train_c_index', 'train_loss',
                   'runtime']]  # reorder columns

# compute fold summary statistis
results_summary = results.agg(['mean', 'std', 'min', 'max', 'median']).T

# write output string
output = ''
output += '{} cross-validation results\n'.format(args.subtype)
output += 'Validation c-index avg = {:1.2f}, std={:1.2f}'.\
          format(100 * results_summary.loc['val_c_index', 'mean'],
                 100 * results_summary.loc['val_c_index', 'std'])
output += '\n\n\n'

output += 'Individual fold results'
output += str(results)
output += '\n\nFold summary statistics'
output += str(results_summary)

print(output)

save_fpath = os.path.join(args.folder, 'results_agg.txt')
with open(save_fpath, 'w') as f:
    f.write(output)


import glfw
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np

gLeftButton = False
gRightButton = False

gFov = 150.
gWidth = 800
gHeight = 800

gCamwith = -0.
gCamHeight = 0.
gPointX = 0.
gPointY = 0.

gXpos = 0
gYpos = 0

varr = np.array([[0., 0., 0.]], 'float32')
narr = np.array([[0., 0., 0.]], 'float32')
tarr = np.array([[0., 0., 0.]], 'float32')
qarr = np.array([[0., 0., 0.]], 'float32')
marr = np.array([[0., 0., 0.]], 'float32')

gtoggle = [True, False]

def drawFrame():
    glBegin(GL_LINES)
    glColor3ub(255, 0, 0)
    glVertex3fv(np.array([-25,0.,0.]))
    glVertex3fv(np.array([25.,0.,0.]))
    glColor3ub(0, 255, 0)
    glVertex3fv(np.array([0.,0.,-25]))
    glVertex3fv(np.array([0.,0.,25.]))
    glEnd()

def drawGrid():
	glBegin(GL_LINES)
	for i in range(-100, 101):
		if i == 0:
			continue

		if i % 10 == 0:
			glColor3ub(100, 100, 100)
		else :
			glColor3ub(50, 50, 50)
		glVertex3fv(np.array([-25., 0., i/4]))
		glVertex3fv(np.array([25., 0., i/4]))

		glVertex3fv(np.array([i/4, 0., -25.]))
		glVertex3fv(np.array([i/4, 0., 25.]))

	glEnd()

def createVertexArraySeparate():
	global tarr, qarr, marr, varr, narr

	return tarr

def glDrawArray():
    global varr, narr, tarr, qarr, marr, gtoggle

    glEnableClientState(GL_VERTEX_ARRAY)
    glEnableClientState(GL_NORMAL_ARRAY)

    #draw trianble mesh
    glNormalPointer(GL_FLOAT, 6*tarr.itemsize, tarr)
    glVertexPointer(3, GL_FLOAT, 6*tarr.itemsize, ctypes.c_void_p(tarr.ctypes.data + 3*tarr.itemsize))
    glDrawArrays(GL_TRIANGLES, 0, int(tarr.size/6))

    #draw quad mesh
    glNormalPointer(GL_FLOAT, 6*qarr.itemsize, qarr)
    glVertexPointer(3, GL_FLOAT, 6*qarr.itemsize, ctypes.c_void_p(qarr.ctypes.data + 3*qarr.itemsize))
    glDrawArrays(GL_QUADS, 0, int(qarr.size/6))

    #draw polygon mesh
    glNormalPointer(GL_FLOAT, 6*marr.itemsize, marr)
    glVertexPointer(3, GL_FLOAT, 6*marr.itemsize, ctypes.c_void_p(marr.ctypes.data + 3*marr.itemsize))
    glDrawArrays(GL_POLYGON, 0, int(marr.size/6))

def render():
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glEnable(GL_DEPTH_TEST)

    #set Toggle wireframe / solid by pressing Z key
    if gtoggle[0]:
    	glPolygonMode( GL_FRONT_AND_BACK, GL_LINE)
    else :
    	glPolygonMode( GL_FRONT_AND_BACK, GL_FILL)
    glLoadIdentity()
    gluLookAt(0, 0, 8, 0,0,0, 0,.1,0)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()

    gluPerspective(gFov, gWidth/gHeight, 5, 1000)
    glTranslatef(0., 0., -500)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glTranslatef(gPointX, 0, 0)
    glTranslatef(0, gPointY, 0)
    glRotatef(gCamHeight, 1, 0, 0)
    glRotatef(gCamwith, 0, 1, 0)

    drawFrame()
    drawGrid()

    glEnable(GL_LIGHTING)   # try to uncomment: no lighting
    glEnable(GL_LIGHT0)
    glEnable(GL_LIGHT1)
    glEnable(GL_LIGHT2)
    glEnable(GL_LIGHT3)
    glEnable(GL_LIGHT4)

    glEnable(GL_NORMALIZE)  # try to uncomment: lighting will be incorrect if you scale the object
    # glEnable(GL_RESCALE_NORMAL)

    # Red light position
    ambientLightColor = (.1,.1,.1,1.)

    RedlightPos = (0.,np.sqrt(10**3),0.,1.)    # try to change 4th element to 0. or 1.
    RedColor = (1.,0.2,0.2,1.)
    glLightfv(GL_LIGHT0, GL_POSITION, RedlightPos)
	# light intensity for each color channel
    glLightfv(GL_LIGHT0, GL_DIFFUSE, RedColor)
    glLightfv(GL_LIGHT0, GL_SPECULAR, RedColor)
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightColor)
    #Green light position

    GreenlightPos = (-10.,-10.,10.,1.)    # try to change 4th element to 0. or 1.
    GreenlightColor = (0.2,1.,0.2,1.)
    glLightfv(GL_LIGHT1, GL_POSITION, GreenlightPos)

    # light intensity for each color channel
    glLightfv(GL_LIGHT1, GL_DIFFUSE, GreenlightColor)
    glLightfv(GL_LIGHT1, GL_SPECULAR, GreenlightColor)
    glLightfv(GL_LIGHT1, GL_AMBIENT, ambientLightColor)
    #BLue light position

    BluelightPos = (10.,-10.,-10.,1.)    # try to change 4th element to 0. or 1.
    BluelightColor = (0.2,0.2,1.,1.)
    glLightfv(GL_LIGHT2, GL_POSITION, BluelightPos)

   	# light intensity for each color channel
    glLightfv(GL_LIGHT2, GL_DIFFUSE, BluelightColor)
    glLightfv(GL_LIGHT2, GL_SPECULAR, BluelightColor)
    glLightfv(GL_LIGHT2, GL_AMBIENT, ambientLightColor)

    #White Light position 1
    WhitelightPos = (1000.,0.,1000.,1.)
    WhitelightColor = (0.75, 0.75, 0.75, 0.1)
    glLightfv(GL_LIGHT3, GL_POSITION, WhitelightPos)

	# light intensity for each color channel
    glLightfv(GL_LIGHT3, GL_DIFFUSE, WhitelightColor)
    glLightfv(GL_LIGHT3, GL_SPECULAR, WhitelightColor)
    glLightfv(GL_LIGHT3, GL_AMBIENT, ambientLightColor)

	#White Light position 2
    WhitelightPos = (-1000.,0.,-1000.,1.)
    WhitelightColor = (0.75, 0.75, 0.75, 0.1)
    glLightfv(GL_LIGHT4, GL_POSITION, WhitelightPos)

	# light intensity for each color channel
    glLightfv(GL_LIGHT4, GL_DIFFUSE, WhitelightColor)
    glLightfv(GL_LIGHT4, GL_SPECULAR, WhitelightColor)
    glLightfv(GL_LIGHT4, GL_AMBIENT, ambientLightColor)

    glColor3ub(255, 255, 255)
    glDrawArray()

    glDisable(GL_LIGHTING)

def mouse_button_callback(window, button, action, mods):
	global gLeftButton, gRightButton
	if action == glfw.PRESS or action == glfw.REPEAT:
		if button == glfw.MOUSE_BUTTON_LEFT:
			gLeftButton = True
		elif button == glfw.MOUSE_BUTTON_RIGHT:
			gRightButton = True
	else :
		gLeftButton = False
		gRightButton = False

def cursor_position_callback(window, xoffset, yoffset):
	global gCamwith, gCamHeight, gLeftButton, gRightButton
	global gXpos, gYpos, gPointX, gPointY
	changedX = gXpos - xoffset
	changedY = gYpos - yoffset

	if gLeftButton == True and gRightButton == False:
		gCamwith -= 5 *np.radians(changedX)
		gCamHeight -= 5 * np.radians(changedY)

	if gRightButton == True and gLeftButton == False:
		gPointX -= 0.005*changedX
		gPointY += 0.005*changedY

	gXpos = xoffset
	gYpos = yoffset

def scroll_callback(window, xoffset, yoffset):
    global gFov
    gFov -= yoffset

def key_callback(window, key, scancode, action, mods):
	global gtoggle

	if action==glfw.PRESS or action==glfw.REPEAT:
		if key == glfw.KEY_Z:
			#Set toggle switch for glPolygon
			gtoggle[0] = not gtoggle[0]
		elif key == glfw.KEY_S:
			gtoggle[1] = not gtoggle[1]

def drop_callback(window, paths):
	global varr, narr, tarr, qarr, marr

	varr = np.array([[0., 0., 0.]], 'float32')
	narr = np.array([[0., 0., 0.]], 'float32')
	tarr = np.array([[0., 0., 0.]], 'float32')
	qarr = np.array([[0., 0., 0.]], 'float32')
	marr = np.array([[0., 0., 0.]], 'float32')

	#Get paths to read obj files and  file name
	paths = str(paths)
	file_name = list(paths.split('/'))[-1][:-2]

	#Open obj file
	f = open(paths[2:-2], 'r')
	count3F, count4F, countMF = 0, 0, 0

	while True:
		line = f.readline()
		if not line:
			break

		input_list = list(line.split())
		if len(input_list) < 2:
			continue

		if input_list[0] == "v":
			varr = np.append(varr, np.array([[float(input_list[1]), float(input_list[2]), float(input_list[3])]], 'float32'), axis=0)
		elif input_list[0] == "vn":
			narr = np.append(narr, np.array([[float(input_list[1]), float(input_list[2]), float(input_list[3])]], 'float32'), axis=0)
		elif input_list[0] == "f":
			if input_list[len(input_list)-1] == '\n':
				input_list = np.delete(input_list, len(input_list)-1,0)

			#Count number of faces to print
			if len(input_list) == 4:
				count3F += 1
				for i in range(1,4):
					sli = input_list[i].split('/')
					if len(sli) < 3:
						tarr = np.append(tarr, np.array([varr[int(sli[0])]], 'float32'), axis=0)
					else :
						tarr = np.append(tarr, np.array([narr[int(sli[2])]], 'float32'), axis=0)
					tarr = np.append(tarr, np.array([varr[int(sli[0])]], 'float32'), axis=0)

			elif len(input_list) == 5:
				count4F += 1
				for i in range(1,5):
					sli = input_list[i].split('/')
					if len(sli) < 3:
						qarr = np.append(qarr, np.array([varr[int(sli[0])]], 'float32'), axis=0)
					else :
						qarr = np.append(qarr, np.array([narr[int(sli[2])]], 'float32'), axis=0)
					qarr = np.append(qarr, np.array([varr[int(sli[0])]], 'float32'), axis=0)

			if len(input_list) > 5:
				countMF += 1
				for i in range(32):
					marr = np.append(marr, np.array([narr[sli[i][1]]], 'float32'), axis=0)
					marr = np.append(marr, np.array([varr[sli[i][0]]], 'float32'), axis=0)

	f.close()
	tarr = np.delete(tarr, [0], axis=0)
	qarr = np.delete(qarr, [0], axis=0)
	marr = np.delete(marr, [0], axis=0)

	#Print out the information of the obj file to console
	print()
	print("=================================================================")
	print("File name : " + file_name)
	print("Total number of faces : ", (count3F+count4F+countMF))
	print("Number of faces with 3 vertics : ", (count3F))
	print("Number of faces with 4 vertics : ", (count4F))
	print("Number of faces with more than 4 vertics : ", (countMF))
	print("=================================================================")

def size_callback(window, width, height):
    global gWidth, gHeight
    gWidth = width
    gHeight = height
    glViewport(0, 0, gWidth, gHeight)

def main():
    global gVertexArrayIndexed, gIndexArray

    if not glfw.init():
        return
    window = glfw.create_window(480,480,'2016025969', None,None)
    if not window:
        glfw.terminate()
        return
    glfw.make_context_current(window)

    glfw.set_mouse_button_callback(window, mouse_button_callback)
    glfw.set_cursor_pos_callback(window, cursor_position_callback)
    glfw.set_scroll_callback(window, scroll_callback)
    glfw.set_key_callback(window, key_callback)
    glfw.set_framebuffer_size_callback(window, size_callback)
    glfw.set_drop_callback(window, drop_callback)

    glfw.swap_interval(1)
    tarr = createVertexArraySeparate()

    while not glfw.window_should_close(window):
        glfw.poll_events()
        render()
        glfw.swap_buffers(window)

    glfw.terminate()

if __name__ == "__main__":
    main()


import os
from flask import Flask, request
import pandas as pd

import sqlite3
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db

'''
The API will receive requests to retrieve the data was predicted (report).
The API will connect to the database to synchronize it to cloud database (Firebase)
'''

app = Flask(__name__)
APP_ROUTE = os.path.dirname(os.path.abspath(__file__))

@app.route("/get_data", methods= ['GET'])
def get_data():
    r = request.args.get("data")
    q=request.args.get('report')
    if q=='report':
        conn = sqlite3.connect("informations.db")
        cur = conn.cursor()

        query="select * from CyberBullying"
        d=pd.read_sql(query,conn)
        # Saving the data to an excel sheet.
        d.to_csv('report.csv')

        conn.commit()
        conn.close()

    # logging to firebase
    if r=='sync':
        # Connect to the sqlite database
        conn = sqlite3.connect("informations.db")
        cur = conn.cursor()

        query="select * from CyberBullying"
        d=pd.read_sql(query,conn)

        # logging in using private key
        cred = credentials.Certificate('project-htu-firebase-adminsdk-nzuna-2a1efd2349.json')
        firebase_admin.initialize_app(cred, {'databaseURL' : 'https://project-htu-default-rtdb.firebaseio.com/', 'httpTimeout' : 30})
        print('logged in to firebase')

        # Add the data to the database
        for i in range(0, 29):
            # Date
            ref1 = f"{d.iloc[i, 0]}/DATE"
            root = db.reference(ref1)

            # writing
            x = {'DATE': f'{d.iloc[i, 2]}'}
            root.set(x)

            # DETECT
            ref2 = f"{d.iloc[i, 0]}/DETECT"
            root = db.reference(ref2)

            # writing
            x = {'DETECT': f'{d.iloc[i, 3]}'}
            root.set(x)

        conn.commit()
        conn.close()

    return 'Done'

if __name__ == "__main__":
    app.run(debug=False, host= '0.0.0.0', port= 5000)


# Module containing functions for implementation of network diffusion model
# Created May 18, 2018
import numpy as np
from scipy import linalg
from scipy import stats

def symmetric_normalized_laplacian(matrix):
    """Returns the symmetric normalized network Laplacian. Input should be a square matrix"""

    # Compute the degree of each node in the network corresponding to the matrix given
    delta = np.sum(matrix, axis=1)  # row sum = node degree

    # Initialise the vector that will hold the inverse square root of the node degrees
    n = len(delta)  # number of nodes
    delta_inv_sqrt = np.zeros(n)

    # Fill the vector
    if delta.any() == 0:  # Check for unconnected nodes, this prevents a division by zero error
        for i in range(0, n):
            if delta[i] == 0:
                delta_inv_sqrt[i] = 0  # set the inverse square root of the degree of each unconnected node to zero
            else:
                delta_inv_sqrt[i] = 1.0 / (np.sqrt(delta[i]))
    else:
        delta_inv_sqrt = 1.0 / (np.sqrt(delta))  # skip checking and looping through if all elements are non-zero

    # Compute the network Laplacian, L
    diag_inv_sqrt = np.diag(delta_inv_sqrt)  # create diagonal matrix equal to the degree matrix^(-1/2)
    laplacian = np.eye(n) - diag_inv_sqrt.dot(matrix).dot(diag_inv_sqrt)

    return laplacian

def functional_connectivity(laplacian, beta, time):
    """This function returns a sequence of functional connectivity matrices over time, generated via a network
    diffusion process informed by the laplacian of the structural connectivity network and a decay rate beta.
    The model is from "Network diffusion accurately models the relationship between structural and functional brain
    connectivity networks", Abdelnour et al, 2014. The time vector gives the diffusion time"""

    # Initialise the array holding the sequence of functional connectivity matrices
    n = len(laplacian)  # number of brain regions/network nodes
    functional_conn = np.zeros((n, n, len(time)))

    # Fill the array at each time step
    for i in range(0, len(time)):
        functional_conn[:, :, i] = linalg.expm(-1*beta*laplacian*time[i])

    return functional_conn

def find_t_critical(modeled_functional, true_functional, model_time_dimension=None, true_time_dimension=None):
    """This function returns scalars informing on the optimisation of the similarity between the input arrays. The
    measure of similarity used here is the Pearson correlation coefficient between matrices. If the input arrays are
    sequences of matrices over time, the dimension of each the array that specifies time should be input as an integer
    for model_time_dimension and true_time_dimension. The function returns:

    t_critical_modeled and t_critical_true - the time in each original time scale at which the Pearson correlation
                                            coefficient is maximized between the modeled and true functional
                                            connectivity matrices given.
    pearson_max - the maximum Pearson correlation coefficient computed between any modeled and true functional matrix
                    pair
    p_value - the p-value associated with the pearson_max value
    model_index - the time dimension index of the modeled functional matrix in the modeled_functional array that gives
                    pearson_max
    true_index - the time dimension index of the true functional matrix in the true_functional array that gives
                    pearson_max
    """

    # Initialise the vectors
    Pearson_coefficients = []
    p_values = []

    # Check for each time dimension case and then compute the desired statistics
    if model_time_dimension is None and true_time_dimension is None:  # neither have a time dimension
        if len(modeled_functional.shape) != 2 or len(true_functional.shape) != 2:
            raise (ValueError('The input arrays must be 2D!'))
        r = stats.pearsonr(modeled_functional.ravel(), true_functional.ravel())
        Pearson_coefficients.append(r[0])  # correlation coefficient between matrices
        p_values.append(r[1])  # p-value for correlation coefficient between these two matrices
        t_critical_model = None  # no time dimension
        t_critical_true = None  # no time dimension
        pearson_max = max(Pearson_coefficients)
        index = Pearson_coefficients.index(pearson_max)
        p_value = p_values[index]
        model_index = None  # single model matrix
        true_index = None  # single true matrix

    elif model_time_dimension is None:  # no modeled matrix time dimension
        if len(modeled_functional.shape) != 2:
            raise (ValueError('The modeled_functional array must be 2D!'))
        if len(true_functional.shape) != 3:
            raise (ValueError('The true_functional array must be 3D!'))
        if true_time_dimension not in range(3):  # i.e. it's not 0, 1, or 2
            raise (ValueError('The true_time_dimension must be either 0, 1, or 2!'))
        # Determine the length of the true functional time vector
        true_time_length = true_functional.shape[true_time_dimension]
        # Loop through the time steps and compare the single modeled matrix and each true functional matrix
        for i in range(true_time_length):
            # select true functional matrix at time step i depending on which dimension is time
            if true_time_dimension == 0:
                true_functional_i = true_functional[i, :, :]
            elif true_time_dimension == 1:
                true_functional_i = true_functional[:, i, :]
            else:  # true_time_dimension == 2
                true_functional_i = true_functional[:, :, i]
            r = stats.pearsonr(modeled_functional.ravel(), true_functional_i.ravel())
            Pearson_coefficients.append(r[0])
            p_values.append(r[1])
        # Compute the max correlation coefficient and the p-value, times, and indicies associated with it
        pearson_max = max(Pearson_coefficients)
        index = Pearson_coefficients.index(pearson_max)
        p_value = p_values[index]
        model_index = None  # single model matrix
        true_index = index
        t_critical_model = None  # no time dimension
        if true_time_dimension == 0:
            t_critical_true = true_functional[true_index, 0, 0]
        elif true_time_dimension == 1:
            t_critical_true = true_functional[0, true_index, 0]
        else:  # true_time_dimension == 2
            t_critical_true = true_functional[0, 0, true_index]

    elif true_time_dimension is None:  # no true matrix time dimension
        if len(true_functional.shape) != 2:
            raise (ValueError('The true_functional array must be 2D!'))
        if len(modeled_functional.shape) != 3:
            raise (ValueError('The modeled_functional array must be 3D!'))
        if model_time_dimension not in range(3):  # i.e. it's not 0, 1, or 2
            raise (ValueError('The model_time_dimension must be either 0, 1, or 2!'))
        # Determine the length of the model functional time vector
        model_time_length = modeled_functional.shape[model_time_dimension]
        # Loop through the time steps and compare the single true matrix and each modeled functional matrix
        for i in range(model_time_length):
            # select the modeled functional matrix at time step i depending on which dimension is time
            if model_time_dimension == 0:
                modeled_functional_i = modeled_functional[i, :, :]
            elif model_time_dimension == 1:
                modeled_functional_i = modeled_functional[:, i, :]
            else:  # model_time_dimension == 2
                modeled_functional_i = modeled_functional[:, :, i]
            r = stats.pearsonr(modeled_functional_i.ravel(), true_functional.ravel())
            Pearson_coefficients.append(r[0])
            p_values.append(r[1])
        # Compute the max correlation coefficient and the p-value, times, and indicies associated with it
        pearson_max = max(Pearson_coefficients)
        index = Pearson_coefficients.index(pearson_max)
        p_value = p_values[index]
        model_index = index  # single model matrix
        true_index = None
        t_critical_true = None  # no time dimension
        if model_time_dimension == 0:
            t_critical_model = modeled_functional[model_index, 0, 0]
        elif model_time_dimension == 1:
            t_critical_model = modeled_functional[0, model_index, 0]
        else:  # model_time_dimension == 2
            t_critical_model = modeled_functional[0, 0, model_index]

    else:  # both input arrays have a time dimension
        if model_time_dimension not in range(3):  # i.e. it's not 0, 1, or 2
            raise (ValueError('The model_time_dimension must be either 0, 1, or 2!'))
        if true_time_dimension not in range(3):  # i.e. it's not 0, 1, or 2
            raise (ValueError('The true_time_dimension must be either 0, 1, or 2!'))
        # Determine the length of the time vectors
        model_time_length = modeled_functional.shape[model_time_dimension]
        true_time_length = true_functional.shape[true_time_dimension]
        # Loop through the time steps and compare each possible pair of matrices
        # Loop through modeled functional time steps
        for i in range(model_time_length):
            if model_time_dimension == 0:
                modeled_functional_i = modeled_functional[i, :, :]
            elif model_time_dimension == 1:
                modeled_functional_i = modeled_functional[:, i, :]
            else:  # model_time_dimension == 2
                modeled_functional_i = modeled_functional[:, :, i]
            # Loop through true functional time steps
            for j in range(true_time_length):
                if true_time_dimension == 0:
                    true_functional_j = true_functional[j, :, :]
                elif true_time_dimension == 1:
                    true_functional_j = true_functional[:, j, :]
                else:  # true_time_dimension == 2
                    true_functional_j = true_functional[:, :, j]
                r = stats.pearsonr(modeled_functional_i.ravel(), true_functional_j.ravel())
                Pearson_coefficients.append(r[0])
                p_values.append(r[1])
        # Compute the max correlation coefficient and the p-value, times, and indicies associated with it
        pearson_max = max(Pearson_coefficients)
        index = Pearson_coefficients.index(pearson_max)
        p_value = p_values[index]
        # Reshape to determine loop indicies
        loop_shape_Pearson_coefficients = np.reshape(Pearson_coefficients, (model_time_length, true_time_length))
        index_reshape = np.argwhere(loop_shape_Pearson_coefficients == pearson_max)
        model_index = int(index_reshape.ravel()[0])  # if multiple elements in index array, use the first one
        true_index = int(index_reshape.ravel()[1])  # if multiple elements in index array, use the first one
        # Find modeled time at which coefficient is maximised
        if model_time_dimension == 0:
            t_critical_model = modeled_functional[model_index, 0, 0]
        elif model_time_dimension == 1:
            t_critical_model = modeled_functional[0, model_index, 0]
        else:  # model_time_dimension == 2
            t_critical_model = modeled_functional[0, 0, model_index]
        # Find true time at which coefficient is maximised
        if true_time_dimension == 0:
            t_critical_true = true_functional[true_index, 0, 0]
        elif true_time_dimension == 1:
            t_critical_true = true_functional[0, true_index, 0]
        else:  # true_time_dimension == 2
            t_critical_true = true_functional[0, 0, true_index]

    return t_critical_model, t_critical_true,  pearson_max, p_value, model_index, true_index

def distance_between_regions(region_positions):
    """This function returns a symmetric matrix of the Euclidean distance (Frobenius norm) between regions.
    Input is an array of each regions centre position in 3D in some co-ordinate system"""

    # Initialise vector for storing distances
    n = len(region_positions)  # number of regions
    half_distances = np.zeros((n, n))
    # Fill vector
    for i in range(n):
        for j in range(0, i+1):  # only compute half of matrix as it's symmetric
            half_distances[i, j] = np.linalg.norm(region_positions[i, :] - region_positions[j, :])
    # Fill the other side of the matrix
    distances = half_distances + half_distances.T

    return distances

def backward_difference_derivative(signal, time):
    """Returns the discrete time derivative of the signal. The derivative is computed via backward difference over
    each time step"""

    # Initialise vector for storing the derivative
    n = len(time)
    signal_derivative = np.zeros(n)
    # Set the derivative at time step=0 to be equal to the derivative at time step=1
    signal_derivative[0] = (signal[1] - signal[0])/(time[1] - time[0])
    # Compute the derivatives at the rest of the time steps
    for i in range(1, n):
        signal_derivative[i] = (signal[i] - signal[i-1])/(time[i] - time[i-1])

    return signal_derivative


import pandas as pd
import numpy as np
import spacy
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from spacy.lang.en.stop_words import STOP_WORDS
from string import punctuation
from spacy.displacy import render

stop_word = list(STOP_WORDS)
print(stop_word[:10])

# load English pre train small model
nlp = spacy.load("en_core_web_sm")

df1 = pd.read_csv("sentiment labelled sentences/amazon_cells_labelled.txt",sep="\t", header=None)
df2 = pd.read_csv("sentiment labelled sentences/imdb_labelled.txt",sep="\t",header=None)
df3 = pd.read_csv("sentiment labelled sentences/yelp_labelled.txt", sep= "\t",header= None)

df = pd.concat([df1,df2,df3],axis=0)

# Rename the column name
df.rename({0:"Review",1:"Sentiment"},axis=1,inplace=True)

# Now Clean the data

def clean_data(text):
    doc = nlp(text)
    list_tokens = []
    clean_tokens = []

    for token in doc:
        if token.lemma_ != "-PRON-":
            tem_token = token.lemma_.lower().strip()
        else:
            tem_token = token.lower()
        list_tokens.append(tem_token)

    for token in list_tokens:
        if token not in stop_word and token not in punctuation:
            clean_tokens.append(token)

    return clean_tokens

# now we are doing vectorization

tfidf = TfidfVectorizer(tokenizer= clean_data)

l_svm = LinearSVC()

# Split data into x and y
x = df["Review"]
y = df["Sentiment"]

# Now split data into train test split

X_train,X_test, y_train,y_test = train_test_split(x,y, test_size=0.2,random_state=2023)

pipe_line = Pipeline([("tfidf",tfidf),("classifier",l_svm)])
pipe_line.fit(X_train,y_train)
Pipeline(memory= None,
        steps = [("tfidf",TfidfVectorizer(analyzer="word", binary=False, decode_error="strict",
                                          encoding = "utf-8", input = "content",
                                         lowercase = True, max_df = 1.0, max_features = None, min_df = 1, ngram_range = (1,1),
                                         norm = "l2", preprocessor = None, smooth_idf = True, stop_words = None, strip_accents = None,
                                         sublinear_tf = False, token_pattern = "(?u)\\u\\w\\w+\\b",
                                         use_idf = True, vocabulary = None)),
                ("clf",LinearSVC(C = 1.0, class_weight=None, dual= True, fit_intercept=True, intercept_scaling=1,
                                loss = "squared_hinge", max_iter=1000, multi_class="ovr", penalty = 'l2',random_state=None,
                                tol=0.0001, verbose=0))],verbose=False)
y_pred = pipe_line.predict(X_test)

print(accuracy_score(y_test,y_pred))

print(classification_report(y_pred,y_test))

print(pipe_line.predict(["wow, This is amazing not lesson"])[0])

###################
import pickle
model = open("model.sav","wb")
pickle.dump(pipe_line,model)
model = pickle.load(open("model.sav","rb"))
print(model.predict(["my name is khan"]))


"""
This file is auto generator by CodeGenerator. Don't modify it directly, instead alter tushare_api.tmpl of it.

Tushare anns接口
获取上市公司公告数据及原文文本，数据从2000年开始。
数据接口-另类数据-上市公司公告原文  https://tushare.pro/document/2?doc_id=176

@author: rmfish
"""
import pandas as pd
import tushare as ts
from sqlalchemy import Integer, String, Float, Column, create_engine
from sqlalchemy.orm import sessionmaker

from tutake.api.base_dao import Base
from tutake.api.process import DataProcess
from tutake.api.process_report import ProcessException
from tutake.api.ts.anns_ext import *
from tutake.api.ts.tushare_dao import TushareDAO, create_shared_engine
from tutake.api.ts.tushare_api import TushareAPI
from tutake.api.ts.tushare_base import TuShareBase
from tutake.utils.config import TutakeConfig
from tutake.utils.utils import project_root

class TushareAnns(Base):
    __tablename__ = "tushare_anns"
    id = Column(Integer, primary_key=True, autoincrement=True)
    ts_code = Column(String, index=True, comment='股票代码')
    ann_date = Column(String, index=True, comment='公告日期')
    ann_type = Column(String, comment='公告类型')
    title = Column(String, comment='公告标题')
    content = Column(String, comment='公告内容')
    pub_time = Column(String, comment='公告发布时间')
    src_url = Column(String, comment='pdf原文URL')
    filepath = Column(String, comment='pdf原文')

class Anns(TushareDAO, TuShareBase, DataProcess):
    instance = None

    def __new__(cls, *args, **kwargs):
        if cls.instance is None:
            cls.instance = super().__new__(cls)
        return cls.instance

    def __init__(self, config):
        self.engine = create_shared_engine(config.get_data_sqlite_driver_url('tushare_report.db'),
                                           connect_args={
                                               'check_same_thread': False,
                                               'timeout': config.get_sqlite_timeout()
                                           })
        session_factory = sessionmaker()
        session_factory.configure(bind=self.engine)
        TushareAnns.__table__.create(bind=self.engine, checkfirst=True)

        query_fields = ['ts_code', 'ann_date', 'start_date', 'end_date', 'limit', 'offset']
        entity_fields = ["ts_code", "ann_date", "ann_type", "title", "content", "pub_time", "src_url", "filepath"]
        TushareDAO.__init__(self, self.engine, session_factory, TushareAnns, 'tushare_report.db', 'tushare_anns',
                            query_fields, entity_fields, config)
        DataProcess.__init__(self, "anns", config)
        TuShareBase.__init__(self, "anns", config, 5000)
        self.api = TushareAPI(config)

    def columns_meta(self):
        return [{
            "name": "ts_code",
            "type": "String",
            "comment": "股票代码"
        }, {
            "name": "ann_date",
            "type": "String",
            "comment": "公告日期"
        }, {
            "name": "ann_type",
            "type": "String",
            "comment": "公告类型"
        }, {
            "name": "title",
            "type": "String",
            "comment": "公告标题"
        }, {
            "name": "content",
            "type": "String",
            "comment": "公告内容"
        }, {
            "name": "pub_time",
            "type": "String",
            "comment": "公告发布时间"
        }, {
            "name": "src_url",
            "type": "String",
            "comment": "pdf原文URL"
        }, {
            "name": "filepath",
            "type": "String",
            "comment": "pdf原文"
        }]

    def anns(self, fields='', **kwargs):
        """
        获取上市公司公告数据及原文文本，数据从2000年开始。
        | Arguments:
        | ts_code(str):   股票代码
        | ann_date(str):   公告日期
        | start_date(str):   公告开始日期
        | end_date(str):   公告结束日期
        | limit(int):   单次返回数据长度
        | offset(int):   请求数据的开始位移量

        :return: DataFrame
         ts_code(str)  股票代码
         ann_date(str)  公告日期
         ann_type(str)  公告类型
         title(str)  公告标题
         content(str)  公告内容
         pub_time(str)  公告发布时间
         src_url(str)  pdf原文URL
         filepath(str)  pdf原文

        """
        return super().query(fields, **kwargs)

    def process(self):
        """
        同步历史数据
        :return:
        """
        return super()._process(self.fetch_and_append)

    def fetch_and_append(self, **kwargs):
        """
        获取tushare数据并append到数据库中
        :return: 数量行数
        """
        init_args = {"ts_code": "", "ann_date": "", "start_date": "", "end_date": "", "limit": "", "offset": ""}
        if len(kwargs.keys()) == 0:
            kwargs = init_args
        # 初始化offset和limit
        if not kwargs.get("limit"):
            kwargs['limit'] = self.default_limit()
        init_offset = 0
        offset = 0
        if kwargs.get('offset'):
            offset = int(kwargs['offset'])
            init_offset = offset

        kwargs = {key: kwargs[key] for key in kwargs.keys() & init_args.keys()}

        def fetch_save(offset_val=0):
            try:
                kwargs['offset'] = str(offset_val)
                self.logger.debug("Invoke pro.anns with args: {}".format(kwargs))
                res = self.tushare_query('anns', fields=self.entity_fields, **kwargs)
                res.to_sql('tushare_anns', con=self.engine, if_exists='append', index=False, index_label=['ts_code'])
                return res
            except Exception as err:
                raise ProcessException(kwargs, err)

        df = fetch_save(offset)
        offset += df.shape[0]
        while kwargs['limit'] != "" and str(df.shape[0]) == kwargs['limit']:
            df = fetch_save(offset)
            offset += df.shape[0]
        return offset - init_offset

setattr(Anns, 'default_limit', default_limit_ext)
setattr(Anns, 'default_cron_express', default_cron_express_ext)
setattr(Anns, 'default_order_by', default_order_by_ext)
setattr(Anns, 'prepare', prepare_ext)
setattr(Anns, 'query_parameters', query_parameters_ext)
setattr(Anns, 'param_loop_process', param_loop_process_ext)

if __name__ == '__main__':
    pd.set_option('display.max_columns', 50)    # 显示列数
    pd.set_option('display.width', 100)
    config = TutakeConfig(project_root())
    pro = ts.pro_api(config.get_tushare_token())
    print(pro.anns())

    api = Anns(config)
    api.process()    # 同步增量数据
    print(api.anns())    # 数据查询接口


import glfw
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np

global key_input
key_input = []

def render():
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()

    # draw coordinate system: x in red, y in green, z in blue
    glBegin(GL_LINES)
    glColor3ub(255, 0, 0)
    glVertex2fv(np.array([0.,0.]))
    glVertex2fv(np.array([1.,0.]))
    glColor3ub(0, 255, 0)
    glVertex2fv(np.array([0.,0.]))
    glVertex2fv(np.array([0.,1.]))
    glEnd()

    # draw triangle
    glColor3ub(255, 255, 255)
    global key_input
    for i in range(len(key_input)):
        #glLoadIdentity()
        nM = np.identity(4)
        if chr(key_input[i]) == 'Q':
            nM[0][3] = -0.1
        elif chr(key_input[i]) == 'E':
            nM[0][3] = 0.1
        elif chr(key_input[i]) == 'A':
            th = np.radians(10)
            nM[:3,:3] = [[np.cos(th), -np.sin(th), 0],
                        [np.sin(th), np.cos(th), 0],
                        [0, 0, 0]]
        elif chr(key_input[i]) == 'D':
            th = np.radians(10)
            nM[:3,:3] = [[np.cos(th), np.sin(th), 0],
                        [-np.sin(th), np.cos(th), 0],
                        [0, 0, 0]]
        elif chr(key_input[i]) == '1':
            key_input = []
            glLoadIdentity()
            break
        glMultMatrixf(nM.T)
    drawTriangle()

def drawTriangle():
    glBegin(GL_TRIANGLES)
    glVertex2fv(np.array([.0,.5]))
    glVertex2fv(np.array([.0,.0]))
    glVertex2fv(np.array([.5,.0]))
    glEnd()

def key_callback(window, key, scancode, action, mods):
    # rotate the camera when 1 or 3 key is pressed or repeated
    if action==glfw.PRESS:
        global key_input
        key_input.insert(0, key)

def main():
    if not glfw.init():
        return
    window = glfw.create_window(480, 480, '2016025969', None,None)
    if not window:
        glfw.terminate()
        return
    glfw.make_context_current(window)
    glfw.set_key_callback(window, key_callback)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        render()
        glfw.swap_buffers(window)

    glfw.terminate()

if __name__ == "__main__":
    main()


from tkinter import *
from functools import partial

alphs = [
    ['a', 'j', 's'],
    ['b', 'k', 't'],
    ['c', 'l', 'u'],
    ['d', 'm', 'v'],
    ['e', 'n', 'w'],
    ['f', 'o', 'x'],
    ['g', 'p', 'y'],
    ['h', 'q', 'z'],
    ['i', 'r']
]
# coding=utf-8
data = [
    [],
    [
        'Testing 1 & 1',
        'Testing 1 & 2',
        'Testing 1 & 3',
        'Testing 1 & 4',
        'Testing 1 & 5'
    ],
    [
        'Testing 2 & 1',
        'Testing 2 & 2',
        'Testing 2 & 3',
        'Testing 2 & 4',
        'Testing 2 & 5'
    ],
    [
        'Testing 3 & 1',
        'Testing 3 & 2',
        'Testing 3 & 3',
        'Testing 3 & 4',
        'Testing 3 & 5'
    ],
    [
        'Testing 4 & 1',
        'Testing 4 & 2',
        'Testing 4 & 3',
        'Testing 4 & 4',
        'Testing 4 & 5'
    ],
    [
        'Testing 5 & 1',
        'Testing 5 & 2',
        'Testing 5 & 3',
        'Testing 5 & 4',
        'Testing 5 & 5'
    ],
    [
        'Testing 6 & 1',
        'Testing 6 & 2',
        'Testing 6 & 3',
        'Testing 6 & 4',
        'Testing 6 & 5'
    ],
    [
        'Testing 7 & 1',
        'Testing 7 & 2',
        'Testing 7 & 3',
        'Testing 7 & 4',
        'Testing 7 & 5'
    ],
    [
        'Testing 8 & 1',
        'Testing 8 & 2',
        'Testing 8 & 3',
        'Testing 8 & 4',
        'Testing 8 & 5'
    ],
    [
        'Testing 9 & 1',
        'Testing 9 & 2',
        'Testing 9 & 3',
        'Testing 9 & 4',
        'Testing 9 & 5'
    ]
]

result = 0

def calculatePoint(self):
    global result
    result = 0
    name = txtEntry.get()
    for n in name.lower():
        i = 0
        for a in alphs:
            i += 1
            if n in a:
                result += i

    test = 0
    while result >= 10:
        moreResult = 0
        for i in str(result):
            moreResult += int(i)
        result = moreResult

    lblPoint.config(text=str(result))
    lblPoint.grid(row=1, column=2, sticky=S, ipadx=38, pady=50)
    root.update()

def resultGuess(num):
    global result
    if result > 0:
        txtResult.delete('1.0', END)
        txtResult.insert(INSERT, data[result][num])

root = Tk()
root.title('Guessing')

frameHeader = Frame(root, bg='green')
frameHeader.grid(row=0, column=0)
title = Label(frameHeader, text='Guessing by name', font=("Courier", 44), bg='green', fg='#fff')
title.grid(row=0, column=0, columnspan=10, padx=50, pady=10)

frameBody = Frame(root, bg='#fff')
frameBody.grid(row=1, column=0)
textLbl = Label(frameBody, text='Name: ', font=("Courier", 20), bg='#fff')
textLbl.grid(row=1, column=0, sticky=NW, ipadx=50, pady=50)
txtEntry = Entry(frameBody, font=("Courier", 20), bg='#fff')
txtEntry.bind('<Return>', calculatePoint)
txtEntry.grid(row=1, column=1, sticky=S, ipadx=20, pady=50)
lblPoint = Label(frameBody, text='', font=("Courier", 20), bg='#fff')
lblPoint.grid(row=1, column=2, sticky=S, ipadx=46, pady=50)

wrapper = Frame(root, bg='#fff')
wrapper.grid(row=2, column=0)
frameFooter = Frame(wrapper, bg='#fff')
frameFooter.grid(row=2, column=0, padx=63)

btnFirst = Button(frameFooter, text="Character", font=("Courier", 14), command=partial(resultGuess, 0))
btnFirst.grid(row=2, column=0, sticky=W, padx=10, pady=15)
btnSecond = Button(frameFooter, text="Individual", font=("Courier", 14), command=partial(resultGuess, 1))
btnSecond.grid(row=2, column=1, sticky=W, padx=10, pady=15)
btnThird = Button(frameFooter, text="Love", font=("Courier", 14), command=partial(resultGuess, 2))
btnThird.grid(row=2, column=2, sticky=W, padx=10, pady=15)
btnFourth = Button(frameFooter, text="Work", font=("Courier", 14), command=partial(resultGuess, 3))
btnFourth.grid(row=2, column=3, sticky=W, padx=10, pady=15)
btnFifth = Button(frameFooter, text="Money", font=("Courier", 14), command=partial(resultGuess, 4))
btnFifth.grid(row=2, column=4, sticky=W, padx=10, pady=15)

wrappBottom = Frame(root, bg='#fff')
wrappBottom.grid(row=3, column=0)
resultWrap = Frame(wrappBottom, bg='#fff')
resultWrap.grid(row=3, column=0, padx=10, pady=10)

txtResult = Text(resultWrap, bg='#eaeaea')
txtResult.grid(row=3, column=0)

root.mainloop()


import time

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding, rsa

"""
This is wrapper around rsa class of cryptography.hazmat.primitives.asymmetric and provides the functionality of
generating RSA key-pair, serializing the public key, decrypting a cipher using the private key and refreshing
the kye-pair.
"""

class RSA:
    def __init__(self):
        self._private_key = None
        self._public_key = None
        self.generation_time = 0
        self.expire_time = 3600  # (in seconds)

    def generate_key_pair(self) -> None:
        """
        Generates a 4096 bit RSA key-pair.

        :returns: None
        """

        self._private_key = rsa.generate_private_key(
            public_exponent=65537, key_size=4096, backend=default_backend()
        )
        self.public_key = self._private_key.public_key()
        self.generation_time = time.time()

    def serialize_key(self) -> str:
        """
        Serializes the public key, encodes it to utf-8 format and returns it.

        :returns: serialized public key
        """

        public_key_serialized: bytes = self._public_key.public_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PublicFormat.SubjectPublicKeyInfo,
        )

        return public_key_serialized.decode("utf-8")

    def decrypt_cipher(self, ciphertext: bytes) -> bytes:
        """
        Decrypts the cipher encrypted from the generated RSA public key using the private key.

        :param ciphertext: The encrypted text to be decrypted
        :return: plaintext
        """

        plaintext: bytes = self._private_key.decrypt(
            ciphertext=ciphertext,
            padding=padding.OAEP(
                mgf=padding.MGF1(hashes.SHA256()), algorithm=hashes.SHA256(), label=None
            ),
        )
        return plaintext

    @property
    def public_key(self) -> str:
        """
        Returns the serialized public key.
        """

        return self.serialize_key()

    def refresh_key_pair(self) -> None:
        """
        Refreshes the rsa key-pair.
        """

        self.generate_key_pair()

    def is_generated(self) -> bool:
        """
        Returns true is the key-pair is generated.
        """

        return self._private_key is not None

    @public_key.setter
    def public_key(self, value: bytes) -> None:
        """
        Sets the public key.

        :param value: The value of the public key.
        :return: None
        """

        self._public_key = value


import logging
import ast
import re
from contrxt.util.logger import build_logger
import contrxt.data.data_manager

import numpy as np
import pandas as pd

from sklearn import preprocessing

class TabularDataManager(contrxt.data.data_manager.DataManager):
    '''
    '''

    def __init__(self, X, Y_predicted, time_label):
        '''
        '''
        X.columns = [self.check_column_names(x) for x in X.columns]
        super().__init__(X, Y_predicted, time_label)

    def generate_data_predictions(self, percent_dataset: int):
        '''Generated predictions for both datasets.

        Parameters
        ----------
        percent_dataset : int
            Describes the percentage of the dataset to use.

        '''
        self.logger.info(f'Sampling dataset with percent: {percent_dataset} and saving labels...')

        data = self.df.sample(frac=percent_dataset, replace=False, random_state=42)
        self.logger.info(f'N. Samples {self.time_label}: {data.shape[0]}')

        self.feature_names = list(self.X.columns)

        max_n = data['Y_predicted'].value_counts().min()

        for i, class_id in enumerate(self.classes):

            data_class = data.copy()
            class_id = str(class_id)
            # Balancing
            data_class = pd.concat([
                data_class[data_class['Y_predicted'] == class_id].sample(n=max_n, replace=False, random_state=42),
                data_class[data_class['Y_predicted'] != class_id].sample(n=max_n, replace=False, random_state=42),
            ]).reset_index(drop=True)
            # data_class.to_csv(f'results/{self.time_label}_{class_id}.csv', index=False)

            assert sum(data_class['Y_predicted'] == class_id) == sum(data_class['Y_predicted'] != class_id)

            self.surrogate_train_data[class_id] = data_class[self.feature_names].copy()

            if data_class['Y_predicted'].dtype == np.dtype(int) or data_class['Y_predicted'].dtype == np.dtype(np.int64):
                self.Y_predicted_binarized[class_id] = np.array([1 if int(x) == i else 0 for x in data_class['Y_predicted']])
            else:
                self.Y_predicted_binarized[class_id] = np.array([1 if x == class_id else 0 for x in data_class['Y_predicted']])

        self.logger.info(f'Finished predicting {self.time_label}')

    def count_rule_occurrence(self, rule):
        '''Count the number of occurrences in the dataset for a specific rule.
        '''
        count = 0
        rule_dict = ast.literal_eval(re.sub(r'(\w+):', r'"\1":', rule))
        contains = [elem[0] for elem in rule_dict.items() if elem[1] == 1]
        avoids = [elem[0] for elem in rule_dict.items() if elem[1] == 0]
        for _, row in self.X.iterrows():
            try:
                if all(row[contains] == 1) and all(row[avoids] == 0):
                    count += 1
            except KeyError:
                continue
        return count


import os
import torch
import numpy as np
from PIL import Image
from torchvision import transforms
from torch.utils.data import Dataset

def recursive_glob(rootdir='.', suffix=''):
        return [os.path.join(looproot, filename)
                for looproot, _, filenames in os.walk(rootdir)
                    for filename in filenames if filename.endswith(suffix)]

class segmentationData(Dataset):
    def __init__(self, root, split, dataTransform = None, labelTransform = None):
        self.root = root
        self.split = split
        self.dataTransform = dataTransform
        self.labelTransform = labelTransform
        self.files = {}
        self.n_classes = 19

        self.images_base = os.path.join(self.root, 'leftImg8bit', self.split)
        self.annotations_base = os.path.join(self.root, 'gtFine',self.split)

        self.files[split] = recursive_glob(rootdir = self.images_base, suffix = '.png')
        self.void_classes = [0, 1, 2, 3, 4, 5, 6, 9, 10, 14, 15, 16, 18, 29, 30, -1]
        self.valid_classes = [7, 8, 11, 12, 13, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 31, 32, 33]
        self.class_name = ['unlabelled', 'road', 'sidewalk', 'building', 'wall', 'fence', \
                            'pole', 'traffic_light', 'traffic_sign', 'vegetation', 'terrain', \
                            'sky', 'person', 'rider', 'car', 'truck', 'bus', 'train', \
                            'motorcycle', 'bicycle']
        self.ignore_index = 255
        self.class_map = dict(zip(self.valid_classes, range(self.n_classes)))

        if not self.files[split]:
            raise Exception("No files for split=[%s] found in %s" % (split, self.images_base))

        print("found %d %s images" % (len(self.files[split]), split))

    def __len__(self):
        return len(self.files[self.split])

    def __getitem__(self, index):
        img_path = self.files[self.split][index].rstrip()
        lbl_path = os.path.join(self.annotations_base, img_path.split(os.sep)[-2],
                                    os.path.basename(img_path)[:-15] + 'gtFine_labelIds.png')

        img = Image.open(img_path).convert('RGB')
        tmp = np.array(Image.open(lbl_path), dtype = np.uint8)
        tmp = self.encode_segmap(tmp)
        target = Image.fromarray(tmp)

        if self.dataTransform:
            img = self.dataTransform(img)

        if self.labelTransform:
            target = self.labelTransform(target)
            target = target.to(torch.long)

        return img, target

    def encode_segmap(self, mask):
        for voidc in self.void_classes:
            mask[mask == voidc] = self.ignore_index

        for validc in self.valid_classes:
            mask[mask == validc] = self.class_map[validc]

        return mask