import torch
import unittest

from tests.disc.testing_base import DiscTestCase

class TestDiscBatchNorm(DiscTestCase):
    def _test_batchnorm(self, batchnorm_func, inp_test_data=None):
        if inp_test_data is not None:
            test_data = inp_test_data
        else:
            test_data = torch.randn([20, 16, 50, 100], device=self.device)
        if (isinstance(test_data, torch.Tensor)):
            test_data = (test_data.to(self.device),)
        self._test_cvt_to_disc(batchnorm_func, test_data)

    def test_batchnorm1d(self):
        batchnorm = torch.nn.BatchNorm1d(16).eval()
        self._test_batchnorm(batchnorm, torch.randn([20, 16, 60], device=self.device))
        batchnorm = torch.nn.BatchNorm1d(16, affine=False).eval()
        self._test_batchnorm(batchnorm, torch.randn([20, 16, 60], device=self.device))

    def test_batchnorm2d(self):
        batchnorm = torch.nn.BatchNorm2d(16).eval()
        self._test_batchnorm(batchnorm)
        batchnorm = torch.nn.BatchNorm2d(16, affine=False).eval()
        self._test_batchnorm(batchnorm)

    def test_batchnorm3d(self):
        batchnorm = torch.nn.BatchNorm3d(16).eval()
        self._test_batchnorm(batchnorm, torch.randn([20, 16, 60, 50, 100], device=self.device))
        batchnorm = torch.nn.BatchNorm3d(16, affine=False).eval()
        self._test_batchnorm(batchnorm, torch.randn([20, 16, 60, 50, 100], device=self.device))

    def test_functional_batchnorm(self):
        @torch.jit.script
        def batchnorm_func(x, running_mean, running_var, weight, bias):
            out_y = torch.nn.functional.batch_norm(x, running_mean, running_var, weight, bias, training=False)
            return out_y

        channel = 16
        x = torch.ones([20, channel], device=self.device)
        running_mean = torch.randn(channel, device=self.device)
        running_var = torch.randn(channel, device=self.device)
        weight = torch.randn(channel, device=self.device)
        bias = torch.randn(channel, device=self.device)
        with torch.no_grad():
            self._test_batchnorm(batchnorm_func, (x, running_mean, running_var, weight, bias))

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


# ENGR2050_09_jasayles.py
# Ethan Sayles
# April 6, 2017
import numpy as np
from matplotlib import pyplot as plt
from scipy import stats as st

#Class to plot the points and best fit lines of data
class Plot():
    def __init__(self, file):
        self.file = file
    def  read_data(self):           #Function to read the data into two lists
        x = []
        y = []
        f = open(self.file, 'r')
        for line in f:
            pari = line.split(",")
            x.append(float(pari[0]))
            y.append(float(pari[1]))
        f.close()
        self.data = [x, y]
    def show(self):                 #Function to show the data points, and the best fit lines
        self.read_data()
        y = self.data[1]
        x = self.data[0]
        Xs = np.arange(0, max(x), .1)

        line_data = st.linregress(x, y)     #Finding the best fit line using scipy
        m1 = line_data[0]
        b1 = line_data[1]
        r_squared = line_data[2] ** 2

        plt.plot(y, x,  'ro',  label = 'Data Points')                                     #Plotting the data points and the best fit lines
        plt.plot(m1 * Xs + b1 , Xs,label = "R_squared = %f" %(r_squared))
        #plt.plot(m2*Xs+b2, Xs, label = "Numpy linalg best fit line")
        plt.title("Best Fit Line",  fontsize = 20)
        plt.xlabel("x",  fontsize=10)
        plt.ylabel("y",  fontsize=10)
        print ("The coefficient of determination for the blue function is %f" %(r_squared))
        plt.legend()
        plt.show()

#Conditional to check for test cases
if __name__ == '__main__':
    f ="ENGR2050_qz2-1_6.dat"
    plot = Plot(f)
    plot.show()


# https://www.youtube.com/watch?v=BFKbbE9Tpqs 8:55
# Paritioning/divide array such that left and right sums are same
# order can vary
# [3,1,2] -> True
# [1,3,2] -> True ([3 | 1,2])
# [2, 3, 2] -> False

def canPartition(iplist):
	#print(iplist)
	if len(iplist) == 0:
		return True
	if len(iplist) == 1:
		return False
	if len(iplist) == 2:
		if iplist[0] == iplist[1]:
			return True
		else:
			return False

	op = False
	if iplist[0] == iplist[1]:
		op = op or canPartition(iplist[2:])
	if (iplist[0]+iplist[1] == iplist[2]) or (iplist[0]+iplist[2] == iplist[1]) or (iplist[1]+iplist[2] == iplist[0]):
		op = op or canPartition(iplist[3:])

	op = op or canPartition([iplist[0]+iplist[1]]+[iplist[2]]+iplist[3:])
	op = op or canPartition([iplist[0]]+[iplist[1]+iplist[2]]+iplist[3:])
	op = op or canPartition([iplist[1]]+[iplist[0]+iplist[2]]+iplist[3:])

	return op

print("##################################")
print("####### Recursive Approach #######")
print("##################################")
print("[3,1,2] True",canPartition([3,1,2]))
print("[1,3,2] True",canPartition([1,3,2]))
print("[2,3,2] False",canPartition([2,3,2]))
print("[2,3,1,2] True",canPartition([2,3,1,2]))
print("[2,3,1,2,1] False",canPartition([2,3,1,2,1]))
print("[2,6,1,2,1] True",canPartition([2,6,1,2,1]))

def canPartition(iplist):

	l = len(iplist)
	if l == 0:
		return True
	if l == 1:
		return False
	if l == 2:
		if iplist[0] == iplist[1]:
			return True
		else:
			return False

	sum = 0
	for i in range(l):
		sum += iplist[i]

	if sum % 2 != 0:
		return False

	sum_to_look_for = int(sum / 2)

	cache = [[0 for c in range(l)] for r in range(l)]

	for c in range(l):
		cache[0][c] = iplist[c]
		if cache[0][c] == sum_to_look_for:
				return True

	for r in range(1,l):
		for c in range(r,l):
			cache[r][c] = cache[r-1][c-1]+iplist[c]
			if cache[r][c] == sum_to_look_for:
				return True

	#print(sum_to_look_for)
	#print(cache)
	return False

print("###########################")
print("####### DP Approach #######")
print("###########################")
print("[3,1,2] True",canPartition([3,1,2]))
print("[1,3,2] True",canPartition([1,3,2]))
print("[2,3,2] False",canPartition([2,3,2]))
print("[2,3,1,2] True",canPartition([2,3,1,2]))
print("[2,3,1,2,1] False",canPartition([2,3,1,2,1]))
print("[2,6,1,2,1] True",canPartition([2,6,1,2,1]))

# (base) D:\>python dynamic_partition_array_with_equal_sum.py
# ##################################
# ####### Recursive Approach #######
# ##################################
# [3,1,2] True True
# [1,3,2] True True
# [2,3,2] False False
# [2,3,1,2] True True
# [2,3,1,2,1] False False
# [2,6,1,2,1] True True


import tkinter as tk
import yahoo_fin.stock_info as si
from yahoo_fin import options
import os
import tkinter.font as tkFont

# HEIGHT will be the height of the frame upon which elements
# will be built
HEIGHT = 350
# WIDTH will be the width of the frame upon which elements
# will be built
WIDTH = 500
# base_folder uses os to create a path to the folder this file is in
base_folder = os.path.dirname(__file__)
# image_path searches the base folder for the blueback.gif file which
# is to be the background
image_path = os.path.join(base_folder, 'blueback.gif')

# get_stock consumes a string representing the ticker of some stock
# and produces a statement based on the percentage difference
# between the market value of the stock and the intrinsic value
# calculated with the pe ratio method
def get_stock(entry):
    try:
        # fetches the live market price of the stock with the given
        # ticker using yahoo finance
        market = si.get_live_price(entry)
        # obtains a quote table for the stock with the given ticker
        # containing all relevant stock data
        quote_table = si.get_quote_table(entry, dict_result=False)
        # retriees the price to earnings ratio from the quote table
        pe = quote_table.iat[13,1]
        # retrieves the earnings per share value from the quote table
        eps = quote_table.iat[7,1]
        # calculates the intrinsic value of the stock with the given
        # ticker using the pe ratio and the earnings per share
        ivalue1 = pe * eps
        # produces the difference between the intrinsic value and the
        # market value, both rounded to two decimal points
        diff1 = round(ivalue1, 2) - round(market, 2)
        # calculates the percent difference between the intrinsic value
        # and market value by dividing the actual difference by the
        # market value
        diff1percent = diff1 / market

        if diff1percent >= 20:
            statement = "Estimated Intrinsic Value: " + str(round(ivalue1, 2))
            statement += "\nCurrent Market Value: " + str(round(market, 2))
            statement += "\nInteresting! " + str(entry)
            statement += " looks like a very strong buy."
            statement += "\nYou should definitely look into making an immediate purchase."
        if diff1percent >= 10:
            statement = "Estimated Intrinsic Value: " + str(round(ivalue1, 2))
            statement += "\nCurrent Market Value: " + str(round(market, 2))
            statement += "\nIntriguing. " + str(entry) + " could be a strong buy,"
            statement += " you\nshould definitely consider making a purchase."
        if diff1percent >= 5:
            statement = "Estimated Intrinsic Value: " + str(round(ivalue1, 2))
            statement += "\nCurrent Market Value: " + str(round(market, 2))
            statement += "\n" + str(entry) + " is definitely trending upwards."
            statement +="\nKeep an eye on it and be on the\nlookout for"
            statemetn += " potential buying opportunities."
        if diff1percent > 0:
            statement = "Estimated Intrinsic Value: " + str(round(ivalue1, 2))
            statement += "\nCurrent Market Value: " + str(round(market, 2))
            statement += "\n" + str(entry) + " seems to be trending in the"
            statement += " right direction, but it's too close to call right now."
            statement += "\nHold off on buying until more definitive data is available."
        if diff1percent == 0:
            statement = "Estimated Intrinsic Value: " + str(round(ivalue1, 2))
            statement += "\nCurrent Market Value: " + str(round(market, 2))
            statement += "\n" + str(entry) + " is trading at approximately"
            statement += " its intrinsic value. Hold off on making a purchase,"
            statement += " as \nthere isn't much value to be found here."
        if diff1percent < 0:
            statement = "Estimated Intrinsic Value: " + str(round(ivalue1, 2))
            statement += "\nCurrent Market Value: " + str(round(market, 2))
            statement += "\n" + str(entry) + " looks to be on the downswing."
            statement += " Don't establish\na position immediately, but keep an"
            statement += " eye out\nfor potential buying opportunities in the future."
        else:
            statement = "There is not enough data at the moment to come\n to a"
            statement += " conclusive valuation for " + str(entry)
            statement += ". Please check back\nlater when our database is updated."
    # an error will only occur if the given ticker does not exist, thus
    # this is the only error that must be handled
    except:
        statement = "The ticker you entered does not exist. Please try again."
    update1 = statement.replace('{', '')
    update2 = update1.replace('}', '')
    # updates the text in the label element with our updated statement
    label['text'] = update2

root = tk.Tk()
# makes the window size static
root.resizable(False, False)

# produces a blank canvas upon which elements can be added and positioned
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

# the font style for the text that will appear displaying the service name
titlestyle=tkFont.Font(family= "Lucida Grande", size=40)

# makes the background photo found previously an element that can be added
background_image = tk.PhotoImage(file=image_path)

# creates a label element with background_image as its image
background_label = tk.Label(root, image=background_image)

# places the background label such that it takes up the entire canvas
background_label.place(x=0, y=0, relwidth=1, relheight=1)

# creates a frame element with a given background colour and border width 5
frame = tk.Frame(root, bg='#2575C6', bd=5)

# places the frame such that it is centred at 50% of the width of the canvas
# and 27.5% of the height of the canvas, and has a width equal to 75% of
# the width of the canvas and a height of 10% of the height of the canvas
frame.place(relx=0.5, rely=0.275, relwidth=0.75, relheight=0.1, anchor='n')

# creates a frame element that will contain the service title
upper_frame = tk.Frame(root, bg='#003366', bd=10)

# places the frame such that it is centred at 50% of the width and 7% of the height,
# and has a width equal to 60% of the canvas and 20% of the height of the canvas
upper_frame.place(relx=0.5, rely=0.07, relwidth=0.6, relheight=0.2, anchor='n')

# creates a label element containing the string "BlueChip" with font being the
# previously defined titlestyle
upper_label = tk.Label(upper_frame, fg="#ffffff", bg="#003366", text="BlueChip", font=titlestyle)

# places the label in the upper_frame element such that it has identical width and height
# to upper_frame
upper_label.place(relwidth=1, relheight=1)

# creates a frame element that will contain details related to the creator of this
# service (me!)
name_frame = tk.Frame(root, bg='#003366', bd=10)

# places the frame such that it is centred at 85% of the canvas width and the highest
# point of the canvas height while having width equal to 30% of the canvas width
# and height equal to 15% of the canvas height
name_frame.place(relx=0.85, rely=0, relwidth=0.3, relheight=0.15, anchor='n')

# creates a label element that contains details pertaining to the creator of the service
# that will exist within the name_frame frame
name_label = tk.Label(name_frame, fg="#ffffff", bg="#003366", text="Designed by Rocky Wang\nr533wang@uwaterloo.ca")

# places the label element in the name_frame frame with height and width both equal to 100%
# of the frame
name_label.place(relwidth=1, relheight=1)

# creates an entry element that will consume the user input with a given font size and colour
entry = tk.Entry(frame, fg="#16589B", font=40)

# places the entry element into the frame element with width equal to 57.5% of the frame and
# 100% of the frame height
entry.place(relwidth=0.575, relheight=1)

# creates a button element that, when pressed will submit the user input as the entry element
# for get_stock
button = tk.Button(frame, text="Submit", font=1, fg='#FFFFFF', bg='#16589B', command=lambda: get_stock(entry.get()))

# places the button element in the frame element centred at 60% of the frame width with height
# equal to 100% of the frame height and width equal to 40% of the frame width
button.place(relx=0.6, relheight=1, relwidth=0.4)

# creates a frame element with a given background colour and border width
lower_frame = tk.Frame(root, bg='#2575C6', bd=10)

# places the lower_frame element centered at 50% of the canvas width and 40% of the canvas height
# with width equal to 75% of the canvas width and height equal to 55% of the canvas height
lower_frame.place(relx=0.5, rely=0.4, relwidth=0.75, relheight=0.55, anchor='n')

# creates a label element that will be used to display the welcome message to users and
# to display the response to user inputs to the user
label = tk.Label(lower_frame, fg="#16589B", text="Welcome to the BlueChip stock advisory system. \nEnter the ticker"
+ " for the stock you'd like assessed,\nand an evaluation will appear in this box.")

# places the response label element in the lower_frame element with width equal to 100% of the
# lower_frame width and height equal to 100% of the lower_frame
label.place(relwidth=1, relheight=1)

root.mainloop()


##
# CPCantalapiedra 2021

from io import StringIO
import math
import multiprocessing
import sys

import numpy as np
import pandas as pd

from pastml.acr import pastml_pipeline, acr, _validate_input
from pastml.ml import MPPA
from pastml.models.f81_like import F81

##
## ACR functions
##

##
# Compute ACR
def compute_acr_probs(tree, data, columns, threads, data_sep, id_index, verbose = False):
    name_column = None
    root_date = None
    prediction_method = MPPA
    model = F81

    if verbose == True: print("Computing ACR", file=sys.stderr)

    #
    # Validate input

    roots, columns, column2states, name_column, age_label, parameters, rates = \
    _validate_input(tree, columns, name_column, data, data_sep, id_index,
                    root_date, copy_only=False, parameters=None, rates=0)

    forced_joint = False
    reoptimise = False
    smoothing = False
    resolve_polytomies = False
    frequency_smoothing = False

    #
    # Compute ACR

    acr_results = acr(forest=roots, columns=columns, column2states=column2states,
                      prediction_method=prediction_method, model=model, column2parameters=parameters,
                      column2rates=rates,
                      force_joint=forced_joint, threads=threads, reoptimise=reoptimise, tau=None if smoothing else 0,
                      resolve_polytomies=resolve_polytomies, frequency_smoothing=frequency_smoothing)

    # marginal probabilities is a <class 'pandas.core.frame.DataFrame'>
    marginal_probs = acr_results[0]['marginal_probabilities']
    # print(marginal_probs, file=sys.stderr)

    return marginal_probs

##
# Computes ACR and remove tips
def get_acr_probs(tree, data_df, columns, threads, data_sep, id_index, verbose = False):
    # create file-like object from dataframe
    s_buf = StringIO()
    data_df.to_csv(s_buf)
    s_buf.seek(0)

    # compute ACR
    marginal_probs = compute_acr_probs(tree, s_buf, columns, threads, data_sep, id_index, verbose)

    # remove ACR probabilities of leaves
    marginal_probs = marginal_probs.drop(data_df.index)

    return marginal_probs

##
## Entropies functions
##

def compute_entropies(marginal_probs, verbose = False):

    if verbose == True: print("Computing entropies", file=sys.stderr)

    k = len(marginal_probs.columns)

    for col in marginal_probs:
        marginal_probs.loc[marginal_probs[col]>1/k, col] = marginal_probs.loc[marginal_probs[col]>1/k, col] / (1 - k) - 1 / (1 - k)

    entropies = marginal_probs.sum(axis=1)

    np.random.seed(25)
    entropies[entropies == 0] = entropies[entropies == 0] + np.random.rand(1)/10000
    entropies[entropies == 1] = entropies[entropies == 1] - np.random.rand(1)/10000

    return entropies

##
## MCMC functions
##

def lpalpha(alpha, beta, x, lambda0):
    return len(x) * (math.lgamma(alpha+beta) - math.lgamma(alpha)) - alpha * (lambda0 - sum(np.log(x)))

def mhalpha(alpha, beta, x, lambda0, se):
    a0 = alpha
    a1 = None
    r = None

    while r is None:
        a1 = math.exp(np.random.normal(math.log(a0), se))
        lpa1 = lpalpha(a1, beta, x, lambda0)
        lpa0 = lpalpha(a0, beta, x, lambda0)
        try:
            r = min(1, math.exp(lpa1 - lpa0))
        except OverflowError:
            if lpa1 >= lpa0:
                r = 1
            else:
                r = float('-inf')

    if np.random.rand(1) < r:
        pass # return a1
    else:
        a1 = a0 # return a0

    return a1

def lpbeta(alpha, beta, x, lambda0):
    return len(x) * (math.lgamma(alpha+beta) - math.lgamma(beta)) - beta * (lambda0 - sum(np.log(1-x)))

def mhbeta(alpha, beta, x, lambda0, se):
    b0 = beta
    b1 = None
    r = None

    while r is None:
        b1 = math.exp(np.random.normal(math.log(b0), se))
        lpb1 = lpbeta(alpha, b1, x, lambda0)
        lpb0 = lpbeta(alpha, b0, x, lambda0)
        try:
            r = min(1, math.exp(lpb1 - lpb0))
        except OverflowError:
            if lpb1 >= lpb0:
                r = 1
            else:
                r = float('-inf')

    if np.random.rand(1) < r:
        pass # return b1
    else:
        b1 = b0 # return b0

    return b1

def compute_mcmc_parallel(args):
    alpha, beta, x, lambda0, se, sim, thin, burn = args
    return compute_mcmc(alpha, beta, x, lambda0, se, sim, thin, burn)

def compute_mcmc(alpha, beta, x, lambda0, se, sim, thin, burn):
    gibbs = []
    # define steps to simulate
    gibbs_range = range(burn, sim+1, thin)
    # print(f"Gibbs sampling: {gibbs_range[0]}, {gibbs_range[1]}, ..., {gibbs_range[-1]}", file=sys.stderr)

    # burn
    for i in range(1, burn):
        alpha = mhalpha(alpha, beta, x, lambda0, se)
        beta = mhbeta(alpha, beta, x, lambda0, se)

    # chain
    for i in range(burn, sim+1):
        alpha = mhalpha(alpha, beta, x, lambda0, se)
        beta = mhbeta(alpha, beta, x, lambda0, se)

        if i % thin == 0:
            gibbs.append((alpha, beta))

    # print(f"Gibbs sampling: {gibbs[0]}, {gibbs[1]}, ..., {gibbs[-1]}", file=sys.stderr)

    return gibbs

##
## Compute delta
##

def compute_delta(entropies, threads, verbose = False):
    delta = None

    # Based on
    # https://github.com/mrborges23/delta_statistic/blob/master/code.R

    lambda0 = 0.1   #rate parameter of the proposal
    se      = 0.5   #standard deviation of the proposal
    sim     = 10000 #number of iterations
    thin    = 10    #we kept only each 10th iterate
    burn    = 100   #100 iterates are burned-in
    mcmc_reps = 2   # number of times MCMC is run

    if verbose == True: print("Computing delta", file=sys.stderr)

    threads = threads if threads < 2 else mcmc_reps

    # Single thread
    if threads == 1:
        divs = [b/a for (a, b) in compute_mcmc(np.random.exponential(1), np.random.exponential(1), entropies, lambda0, se, sim, thin, burn)]
        divs.extend([b/a for (a, b) in compute_mcmc(np.random.exponential(1), np.random.exponential(1), entropies, lambda0, se, sim, thin, burn)])

    # Multiprocess
    else:

        def mcmc_args(reps):
            for rep in range(1, reps+1):
                yield (np.random.exponential(1), np.random.exponential(1), entropies, lambda0, se, sim, thin, burn)

        divs = None
        with multiprocessing.Pool(processes = threads) as pool:
            vals = pool.imap_unordered(compute_mcmc_parallel, mcmc_args(mcmc_reps))
            divs = [b/a for sublist in vals for (a, b) in sublist]

    # Compute delta
    if divs is None or len(divs) <= 0:
        raise Exception("Could not compute MCMC alpha-beta values.")
    else:
        # mean of all beta/alpha divisions
        delta = sum(divs)/len(divs)

    return delta

##
## p-value functions
##

##
def compute_random_delta_parallel(args):
    tree, df, columns, threads, data_sep, id_index, verbose = args
    return compute_random_delta(tree, df, columns, threads, data_sep, id_index, verbose)

##
def compute_random_delta(tree, df, columns, threads, data_sep, id_index, verbose):
    # random shuffle of input data
    shuffled_df = df.sample(frac = 1, random_state = np.random.RandomState())
    shuffled_df.index = df.index

    # compute ACR
    marginal_probs = get_acr_probs(tree, shuffled_df, columns, threads, data_sep, id_index, verbose)

    # Entropies
    entropies = compute_entropies(marginal_probs, verbose)

    # Delta
    pval_delta = compute_delta(entropies, threads, verbose)

    return pval_delta

##
def compute_pvalue(delta, pval_reps, tree, df, columns, threads, data_sep, id_index, verbose):
    p_value = None

    def compute_random_delta_args(reps):
        threads = 1
        for rep in range(1, reps+1):
            yield (tree, df, columns, threads, data_sep, id_index, verbose)

    # obtain random deltas
    random_deltas = []
    with multiprocessing.Pool(processes = threads) as pool:
        random_deltas = pool.imap_unordered(compute_random_delta_parallel, compute_random_delta_args(pval_reps))
        random_deltas = [rd for rd in random_deltas] # parse iterator
        # p-value
        p_value = sum([1 for rd in random_deltas if rd > delta]) / len(random_deltas)

    return p_value

##
## API
##

def compute_delta_pvalue(tree, data, columns, threads, pval_reps, data_sep, id_index, verbose = False):
    delta = p_value = None

    ##
    ## Parse data to File-like object
    ## (needed to shuffle the data and be able to use it
    ## for pastml ACR function which uses pandas read_csv function)
    if verbose == True: print("Parsing input data", file=sys.stderr)

    df = pd.read_csv(data, sep=data_sep, index_col=id_index, header=0, dtype=str)

    ##
    ## Ancestral Character Reconstruction

    marginal_probs = get_acr_probs(tree, df, columns, threads, data_sep, id_index, verbose)

    ##
    ## Compute entropies
    # Based on
    # https://github.com/mrborges23/delta_statistic/blob/master/code.R

    entropies = compute_entropies(marginal_probs, verbose)

    ##
    ## Compute delta
    ## Based on
    # https://github.com/mrborges23/delta_statistic/blob/master/code.R
    delta = compute_delta(entropies, threads, verbose)

    if verbose == True: print(f"delta statistic: {delta}", file=sys.stderr)

    ##
    ## Compute p-value

    if pval_reps > 0:

        if verbose == True: print(f"Computing p-value: {pval_reps} repetitions", file=sys.stderr)

        p_value = compute_pvalue(delta, pval_reps, tree, df, columns, threads, data_sep, id_index, verbose)

        if verbose == True: print(f"p-value: {p_value}", file=sys.stderr)

    return (delta, p_value)

##
## MAIN
##

if '__main__' == __name__:

    ##
    ## Arguments

    tree = sys.argv[1]
    data = sys.argv[2]
    columns = sys.argv[3].split(",")
    if len(sys.argv) > 4:
        threads = int(sys.argv[4])
    else:
        threads = 0

    if len(sys.argv) > 5:
        pval_reps = int(sys.argv[5])
    else:
        pval_reps = 0

    if len(sys.argv) > 6:
        data_sep = sys.argv[6]
    else:
        data_sep = ","

    if len(sys.argv) > 7:
        id_index = int(sys.argv[7])
    else:
        id_index = 0

    threads = threads if threads > 0 else multiprocessing.cpu_count()

    delta, p_value = compute_delta_pvalue(tree, data, columns, threads, pval_reps, data_sep, id_index, verbose = True)

    print(f"delta = {delta}")
    if p_value is not None:
        print(f"p-value = {p_value}")

    print("Finished.", file=sys.stderr)

## END


from numpy import genfromtxt
import csv
import numpy as np
import math
import matplotlib.pyplot as plt
import copy
import random
import torch
from torch import nn
import datetime

def read_csv(filename):
    with open(filename, newline='') as f_input:
        data = []
        i=0
        for row in csv.reader(f_input):
            if i == 0:
                i = 1
                continue
            data.append(list(map(float, row)))
        return data

# Define Model.
class MLP(torch.nn.Module):
    def __init__(self, IDs_len, REGs_len):
            super(MLP, self).__init__()

            self.I_branch = nn.Sequential(
                nn.Linear(IDs_len, 64),
                nn.ReLU()
            )
            self.R_branch = nn.Sequential(
                nn.Linear(REGs_len, 48),
                nn.ReLU()
            )
            self.D_branch = nn.Sequential(
                nn.Linear(31, 48),
                nn.ReLU()
            )
            self.M_branch = nn.Sequential(
                nn.Linear(12, 32),
                nn.ReLU()
            )
            self.Y_branch = nn.Sequential(
                nn.Linear(2, 16),
                nn.ReLU()
            )
            self.T_branch = nn.Sequential(
                nn.Linear(2, 16),
                nn.ReLU()
            )
            ######
            self.time_branch = nn.Sequential(
                nn.Linear(96, 128),
                nn.ReLU(),
                #nn.Dropout(0.5),
                nn.Linear(128, 64),
                nn.ReLU()
            )
            self.DW_branch = nn.Sequential(
                nn.Linear(7, 32),
                nn.ReLU()
                #nn.Dropout(0.5),
                #nn.Linear(64, 32),
                #nn.ReLU()
            )
            #####

            self.main_branch = nn.Sequential(
                nn.Linear(224, 256),
                nn.ReLU(),
                #nn.Dropout(0.2),
                #nn.Linear(512, 128),
                #nn.ReLU(),
                #nn.Dropout(0.2),
                nn.Linear(256, 128),
                nn.ReLU(),
                nn.Linear(128, 32),
                nn.ReLU(),
                nn.Linear(32, 1)
            )

    def forward(self, I, R, D, M, Y, T, DW):

            I_out = self.I_branch(I)
            R_out = self.R_branch(R)

            D_out = self.D_branch(D)
            M_out = self.M_branch(M)
            Y_out = self.Y_branch(Y)
            DW_out = self.DW_branch(DW)

            time_cat = torch.cat((D_out, M_out, Y_out), dim=1)
            time_out = self.time_branch(time_cat)

            T_out = self.T_branch(T)

            out_cat = torch.cat((I_out, R_out, T_out, time_out, DW_out), dim=1)
            output = self.main_branch(out_cat)
            return output

def main():
    # Loading the training and testing data.
    training_data = read_csv("training_data.csv")
    testing_data = read_csv("test_data.csv")

    # Extracting the number of Unique IDs and Regs
    # Test the IDs.
    IDs = []
    REGs = []
    for instance in training_data:
        if not instance[0] in IDs:
            IDs.append(instance[0])
        if not instance[1] in REGs:
            REGs.append(instance[1])
    for instance in testing_data:
        if not instance[0] in IDs:
            IDs.append(instance[0])
        if not instance[1] in REGs:
            REGs.append(instance[1])

    # Getting the Mins and Maxs for each feature.
    Mins = [math.inf, math.inf, math.inf, math.inf, math.inf, math.inf, math.inf]
    Maxs = [0, 0, 0, 0, 0, 0, 0]
    for instance in training_data:
        for k in range(len(instance)):
            if Mins[k] > instance[k]:
                Mins[k] = instance[k]
            if Maxs[k] < instance[k]:
                Maxs[k] = instance[k]
    # Normalize the output feature only.
    training_data_normalized = []
    for instance in training_data:
        normalized_instance = []
        for k in range(len(instance)):
            if k==6:
                normalized_instance.append((instance[k]-Mins[k])/(Maxs[k]- Mins[k]))
            else:
                normalized_instance.append(instance[k])
        training_data_normalized.append(normalized_instance)

    # Split the data into training and validation (In my code, test_data refers to validation data.)
    random.shuffle(training_data_normalized)
    train_data = training_data_normalized[:40000]
    test_data = training_data_normalized[40000:]
    #train_data = []
    #test_data = []
    #random.shuffle(training_data_normalized)
    #for ins in training_data_normalized:
    #    if ins[4] == 2019 and ins[3] > 4 and ins[2] > 20:
    #        test_data.append(ins)
    #    else:
    #        train_data.append(ins)

    batch_size = 64

    # Put training data into Batches.
    train_data_input_batches_I_vec = []
    train_data_input_batches_R_vec = []
    train_data_input_batches_D_vec = []
    train_data_input_batches_M_vec = []
    train_data_input_batches_Y_vec = []
    train_data_input_batches_T_vec = []
    train_data_input_batches_DW_vec = []

    train_data_output_batches = []

    input_batch_I_vec = []
    input_batch_R_vec = []
    input_batch_D_vec = []
    input_batch_M_vec = []
    input_batch_Y_vec = []
    input_batch_T_vec = []
    input_batch_DW_vec = []
    output_batch = []

    for i in range(1, len(train_data)+1):
        I, R, D, M, Y, TRX_T, TRX_C = train_data[i-1]

        ## To be deleted.
        #if TRX_T == 2:
        #    continue
        ##
        # Making one hot vectors.
        I_vec = [0 for j in range(len(IDs))]
        I_vec[IDs.index(I)] = 1
        R_vec = [0 for j in range(len(REGs))]
        R_vec[REGs.index(R)] = 1
        D_vec = [0 for j in range(31)]
        D_vec[int(D)-1] = 1
        M_vec = [0 for j in range(12)]
        M_vec[int(M)-1] = 1
        Y_vec = [0, 0]
        if Y == 2019:
            Y_vec[1] = 1
        else:
            Y_vec[0] = 1
        T_vec = [0,0]
        T_vec[int(TRX_T)-1] = 1

        DW_vec = [0 for j in range(7)]
        d = datetime.datetime(int(Y), int(M), int(D))
        DW_vec[d.weekday()] = 1

        input_batch_I_vec.append(I_vec)
        input_batch_R_vec.append(R_vec)
        input_batch_D_vec.append(D_vec)
        input_batch_M_vec.append(M_vec)
        input_batch_Y_vec.append(Y_vec)
        input_batch_T_vec.append(T_vec)
        input_batch_DW_vec.append(DW_vec)

        output_batch.append(TRX_C)

        if i % batch_size == 0 or i==len(train_data):
            train_data_input_batches_I_vec.append(input_batch_I_vec)
            train_data_input_batches_R_vec.append(input_batch_R_vec)
            train_data_input_batches_D_vec.append(input_batch_D_vec)
            train_data_input_batches_M_vec.append(input_batch_M_vec)
            train_data_input_batches_Y_vec.append(input_batch_Y_vec)
            train_data_input_batches_T_vec.append(input_batch_T_vec)
            train_data_input_batches_DW_vec.append(input_batch_DW_vec)

            train_data_output_batches.append(output_batch)

            input_batch_I_vec = []
            input_batch_R_vec = []
            input_batch_D_vec = []
            input_batch_M_vec = []
            input_batch_Y_vec = []
            input_batch_T_vec = []
            input_batch_DW_vec = []
            output_batch = []

    # Put test data into Batches.
    test_data_input_batches_I_vec = []
    test_data_input_batches_R_vec = []
    test_data_input_batches_D_vec = []
    test_data_input_batches_M_vec = []
    test_data_input_batches_Y_vec = []
    test_data_input_batches_T_vec = []
    test_data_input_batches_DW_vec = []
    test_data_output_batches = []

    input_batch_I_vec = []
    input_batch_R_vec = []
    input_batch_D_vec = []
    input_batch_M_vec = []
    input_batch_Y_vec = []
    input_batch_T_vec = []
    input_batch_DW_vec = []
    output_batch = []

    for i in range(1, len(test_data)+1):
        I, R, D, M, Y, TRX_T, TRX_C = test_data[i-1]

        ## To be deleted.
        #if TRX_T == 2:
        #    continue
        ##

        # Making one hot vectors.
        I_vec = [0 for j in range(len(IDs))]
        I_vec[IDs.index(I)] = 1
        R_vec = [0 for j in range(len(REGs))]
        R_vec[REGs.index(R)] = 1
        D_vec = [0 for j in range(31)]
        D_vec[int(D)-1] = 1
        M_vec = [0 for j in range(12)]
        M_vec[int(M)-1] = 1
        Y_vec = [0, 0]
        if Y == 2019:
            Y_vec[1] = 1
        else:
            Y_vec[0] = 1
        T_vec = [0,0]
        T_vec[int(TRX_T)-1] = 1

        DW_vec = [0 for j in range(7)]
        d = datetime.datetime(int(Y), int(M), int(D))
        DW_vec[d.weekday()] = 1

        input_batch_I_vec.append(I_vec)
        input_batch_R_vec.append(R_vec)
        input_batch_D_vec.append(D_vec)
        input_batch_M_vec.append(M_vec)
        input_batch_Y_vec.append(Y_vec)
        input_batch_T_vec.append(T_vec)
        input_batch_DW_vec.append(DW_vec)

        output_batch.append(TRX_C)

        if i % batch_size == 0 or i==len(test_data):
            test_data_input_batches_I_vec.append(input_batch_I_vec)
            test_data_input_batches_R_vec.append(input_batch_R_vec)
            test_data_input_batches_D_vec.append(input_batch_D_vec)
            test_data_input_batches_M_vec.append(input_batch_M_vec)
            test_data_input_batches_Y_vec.append(input_batch_Y_vec)
            test_data_input_batches_T_vec.append(input_batch_T_vec)
            test_data_input_batches_DW_vec.append(input_batch_DW_vec)

            test_data_output_batches.append(output_batch)

            input_batch_I_vec = []
            input_batch_R_vec = []
            input_batch_D_vec = []
            input_batch_M_vec = []
            input_batch_Y_vec = []
            input_batch_T_vec = []
            input_batch_DW_vec = []

            output_batch = []
    # Make model instance.
    model = MLP(len(IDs), len(REGs))
    # Move to CUDA for faster training.
    if torch.cuda.is_available():
        model = model.cuda()
    # Loss function and optimizer.
    criterion = torch.nn.MSELoss()
    optimizer = torch.optim.Adam(model.parameters(), lr = 0.00035)

    # Training part.
    model.train()
    epochs = 15
    training_losses = []
    testing_losses = []
    for epoch in range(epochs):
        epoch_loss = 0
        for i in range(len(train_data_input_batches_I_vec)):
            x_train_I = torch.cuda.FloatTensor(train_data_input_batches_I_vec[i])
            x_train_R = torch.cuda.FloatTensor(train_data_input_batches_R_vec[i])
            x_train_D = torch.cuda.FloatTensor(train_data_input_batches_D_vec[i])
            x_train_M = torch.cuda.FloatTensor(train_data_input_batches_M_vec[i])
            x_train_Y = torch.cuda.FloatTensor(train_data_input_batches_Y_vec[i])
            x_train_T = torch.cuda.FloatTensor(train_data_input_batches_T_vec[i])
            x_train_DW = torch.cuda.FloatTensor(train_data_input_batches_DW_vec[i])

            y_train = torch.cuda.FloatTensor(train_data_output_batches[i])
            optimizer.zero_grad()
            # Forward pass
            y_pred = model(x_train_I, x_train_R, x_train_D, x_train_M, x_train_Y, x_train_T, x_train_DW)
            # Compute Loss
            loss = criterion(y_pred.squeeze(), y_train)
            # Loss on original data for printing.
            y_orig_train = y_train * (Maxs[6] - Mins[6]) + Mins[6]
            y_orig_pred = y_pred.squeeze() * (Maxs[6] - Mins[6]) + Mins[6]
            orig_loss = torch.sqrt(criterion(y_orig_pred, y_orig_train))
            epoch_loss += orig_loss.data.item()

            # Backward pass
            loss.backward()
            optimizer.step()
        # Compute Loss over test data.
        test_total_loss = 0
        test_total_num = 0
        for j in range(len(test_data_input_batches_I_vec)):
            x_test_I = torch.cuda.FloatTensor(test_data_input_batches_I_vec[j])
            x_test_R = torch.cuda.FloatTensor(test_data_input_batches_R_vec[j])
            x_test_D = torch.cuda.FloatTensor(test_data_input_batches_D_vec[j])
            x_test_M = torch.cuda.FloatTensor(test_data_input_batches_M_vec[j])
            x_test_Y = torch.cuda.FloatTensor(test_data_input_batches_Y_vec[j])
            x_test_T = torch.cuda.FloatTensor(test_data_input_batches_T_vec[j])
            x_test_DW = torch.cuda.FloatTensor(test_data_input_batches_DW_vec[j])

            y_test = torch.cuda.FloatTensor(test_data_output_batches[j])
            y_pred = model(x_test_I, x_test_R, x_test_D, x_test_M, x_test_Y, x_test_T, x_test_DW)

            y_orig_test = y_test * (Maxs[6] - Mins[6]) + Mins[6]
            y_orig_pred = y_pred.squeeze() * (Maxs[6] - Mins[6]) + Mins[6]
            # SQRT for RMSE
            test_loss = (y_orig_pred - y_orig_test)**2
            test_loss = torch.sum(test_loss)
            test_total_loss += test_loss.data.item()
            test_total_num += len(y_pred)
        # Append losses.
        training_losses.append(epoch_loss/ len(train_data_input_batches_I_vec))
        testing_losses.append(test_total_loss / len(test_data_input_batches_I_vec))
        print("Epoch:", epoch, "Train Loss = ", epoch_loss/len(train_data_input_batches_I_vec), "Test Loss = " , math.sqrt(test_total_loss / test_total_num))

    # Preprocess Test data.
    testing_batch_I_vec = []
    testing_batch_R_vec = []
    testing_batch_D_vec = []
    testing_batch_M_vec = []
    testing_batch_Y_vec = []
    testing_batch_T_vec = []
    testing_batch_DW_vec = []

    for i in range(1, len(testing_data)+1):
        I, R, D, M, Y, TRX_T = testing_data[i-1]

        # Making one hot vectors.
        I_vec = [0 for j in range(len(IDs))]
        I_vec[IDs.index(I)] = 1
        R_vec = [0 for j in range(len(REGs))]
        R_vec[REGs.index(R)] = 1
        D_vec = [0 for j in range(31)]
        D_vec[int(D)-1] = 1
        M_vec = [0 for j in range(12)]
        M_vec[int(M)-1] = 1
        Y_vec = [0, 0]
        if Y == 2019:
            Y_vec[1] = 1
        else:
            Y_vec[0] = 1
        T_vec = [0,0]
        T_vec[int(TRX_T)-1] = 1

        DW_vec = [0 for j in range(7)]
        d = datetime.datetime(int(Y), int(M), int(D))
        DW_vec[d.weekday()] = 1

        testing_batch_I_vec.append(I_vec)
        testing_batch_R_vec.append(R_vec)
        testing_batch_D_vec.append(D_vec)
        testing_batch_M_vec.append(M_vec)
        testing_batch_Y_vec.append(Y_vec)
        testing_batch_T_vec.append(T_vec)
        testing_batch_DW_vec.append(DW_vec)

    # Predict values for Testing dataset.
    x_testing_I = torch.cuda.FloatTensor(testing_batch_I_vec)
    x_testing_R = torch.cuda.FloatTensor(testing_batch_R_vec)
    x_testing_D = torch.cuda.FloatTensor(testing_batch_D_vec)
    x_testing_M = torch.cuda.FloatTensor(testing_batch_M_vec)
    x_testing_Y = torch.cuda.FloatTensor(testing_batch_Y_vec)
    x_testing_T = torch.cuda.FloatTensor(testing_batch_T_vec)
    x_testing_DW= torch.cuda.FloatTensor(testing_batch_DW_vec)
    y_pred = model(x_testing_I, x_testing_R, x_testing_D, x_testing_M, x_testing_Y, x_testing_T, x_testing_DW)
    # Reverse normalization.
    Y_values = y_pred * (Maxs[6] - Mins[6]) + Mins[6]

    # Converting the Pytorch Tensor into numpy.
    Y_values = Y_values.cpu().detach().numpy()
    #print(Y_values)
    # Save values into csv file.
    np.savetxt("test_predictions.csv", Y_values, delimiter=",")

if __name__ == '__main__':
    main()


import requests
import json

gqry = "gremlin=g.V().has('vertex_label','Version').has('pname',within('io.vertx:vertx-core')).in('has_dependency').values('sid')"
payload = {'gremlin':gqry}
url = 'http://bayesian-gremlin-http-saket-graph.dev.rdu2c.fabric8.io/'

r = requests.post(url,data=json.dumps(payload))

sids = r.json()['result']['data']

sidslen = len(sids)

strsids = '\',\''.join(map(str, sids))
strsids = strsids.replace('\n',"")
gqry1 = "g.V().has('vertex_label','Stack').has('sid',within('"+strsids+"')).out('has_dependency').values('pname').count().map(union(identity(),constant("+str(sidslen)+")).fold(1){a,b -> b/a})"

payload = {'gremlin':gqry1}
url = 'http://bayesian-gremlin-http-saket-graph.dev.rdu2c.fabric8.io/'

r = requests.post(url,data=json.dumps(payload))
print(r.text)


from tools.engine import freeze_bn, custom_voc_evaluate
from utils.backbone_utils import resnet_fpn_backbone
from faster_rcnn import FasterRCNN, FastRCNNPredictor
from faster_polar_rcnn import FasterPolarRCNN
import _utils
from datasets.data import get_dataset
from run import get_transform
from polar_net import PolarNet

import torch
from thop import profile
from copy import deepcopy

def get_model_info(model, tsize):
    device = 'cuda'
    stride = 64
    img = torch.zeros((1, 3, stride, stride), device=device) #next(model.parameters()).device
    flops, params = profile(deepcopy(model.to(device)), inputs=(img,), verbose=False)
    params /= 1e6
    flops /= 1e9
    flops *= tsize[0] * tsize[1] / stride / stride * 2  # Gflops
    info = "Params: {:.2f}M, Gflops: {:.2f}".format(params, flops)
    return info

if __name__ == '__main__':

    use_attn = 0
    use_polar = 1
    mtype = 'v4.0.1c'
    _mtype = 'v4c'
    tail = '_w_neg'
    # use_new_v = 1
    model_name = "r50_use_attn=%d_use_polar=%d%s" % (use_attn, use_polar, mtype)
    model_path = "results/polar%s_r50%s/best.pth" % (mtype, tail)
    CLASSES = {"__background__", "nGEC", "AGC"}
    val_csvname = "test_w_ood.csv"
    # val_csvname = "val.csv"
    val_batch_size = 1
    num_workers = 4
    torch.cuda.set_device(2)
    device = torch.device("cuda")
    print("====Loading data====")
    dataset_val = get_dataset(val_csvname,
                              datatype="val",
                              transform=get_transform(train=False))
    dataloader_val = torch.utils.data.DataLoader(
        dataset_val, batch_size=val_batch_size, shuffle=False,
        num_workers=num_workers,
        collate_fn=_utils.collate_fn
    )

    print("====Loading model====")
    backbone = resnet_fpn_backbone("resnet50", False, use_attn=use_attn)
    if use_polar == 1:
        polar_net = PolarNet(mtype=_mtype)
        model = FasterPolarRCNN(backbone, polar_net, mtype=_mtype, num_classes=len(CLASSES))
    else:
        model = FasterRCNN(backbone, num_classes=len(CLASSES))

    in_features = model.roi_heads.box_predictor.cls_score.in_features
    model.roi_heads.box_predictor = FastRCNNPredictor(in_features,
                                                      len(CLASSES))
    model.load_state_dict(torch.load(model_path, map_location='cpu'))
    model.to(device)

    model.apply(freeze_bn)
    print(get_model_info(model, (1024, 1024)))

    val_mAP = custom_voc_evaluate(
        model, dataloader_val, device=device,
        gt_csv_path=val_csvname,
        cls_csv_path="temp/test_%s_%s_cls.csv" % (model_name, val_csvname),
        loc_csv_path="temp/test_%s_%s_loc.csv" % (model_name, val_csvname)
    )

    print(val_mAP)


import json
import time
import base64
import xml.etree.ElementTree as ET
from pymisp import MISPEvent, MISPObject

misperrors = {'error': 'Error'}
moduleinfo = {'version': 1, 'author': 'Christian Studer',
              'description': 'Import from GoAML',
              'module-type': ['import']}
moduleconfig = []
mispattributes = {'inputSource': ['file'], 'output': ['MISP objects'],
                  'format': 'misp_standard'}

t_from_objects = {'nodes': ['from_person', 'from_account', 'from_entity'],
                  'leaves': ['from_funds_code', 'from_country']}
t_to_objects = {'nodes': ['to_person', 'to_account', 'to_entity'],
                'leaves': ['to_funds_code', 'to_country']}
t_person_objects = {'nodes': ['addresses'],
                    'leaves': ['first_name', 'middle_name', 'last_name', 'gender', 'title', 'mothers_name', 'birthdate',
                               'passport_number', 'passport_country', 'id_number', 'birth_place', 'alias', 'nationality1']}
t_account_objects = {'nodes': ['signatory'],
                     'leaves': ['institution_name', 'institution_code', 'swift', 'branch', 'non_banking_insitution',
                                'account', 'currency_code', 'account_name', 'iban', 'client_number', 'opened', 'closed',
                                'personal_account_type', 'balance', 'date_balance', 'status_code', 'beneficiary',
                                'beneficiary_comment', 'comments']}
entity_objects = {'nodes': ['addresses'],
                  'leaves': ['name', 'commercial_name', 'incorporation_legal_form', 'incorporation_number', 'business', 'phone']}

goAMLobjects = {'report': {'nodes': ['reporting_person', 'location'],
                           'leaves': ['rentity_id', 'submission_code', 'report_code', 'submission_date', 'currency_code_local']},
                'reporting_person': {'nodes': ['addresses'], 'leaves': ['first_name', 'middle_name', 'last_name', 'title']},
                'location': {'nodes': [], 'leaves': ['address_type', 'address', 'city', 'zip', 'country_code', 'state']},
                'transaction': {'nodes': ['t_from', 't_from_my_client', 't_to', 't_to_my_client'],
                                'leaves': ['transactionnumber', 'transaction_location', 'date_transaction',
                                           'transmode_code', 'amount_local']},
                't_from': t_from_objects, 't_from_my_client': t_from_objects,
                't_to': t_to_objects, 't_to_my_client': t_to_objects,
                'addresses': {'nodes': ['address'], 'leaves': []},
                'address': {'nodes': [], 'leaves': ['address_type', 'address', 'city', 'zip', 'country_code', 'state']},
                'from_person': t_person_objects, 'to_person': t_person_objects, 't_person': t_person_objects,
                'from_account': t_account_objects, 'to_account': t_account_objects,
                'signatory': {'nodes': ['t_person'], 'leaves': []},
                'from_entity': entity_objects, 'to_entity': entity_objects,
                }

t_account_mapping = {'misp_name': 'bank-account', 'institution_name': 'institution-name', 'institution_code': 'institution-code',
                     'iban': 'iban', 'swift': 'swift', 'branch': 'branch', 'non_banking_institution': 'non-bank-institution',
                     'account': 'account', 'currency_code': 'currency-code', 'account_name': 'account-name',
                     'client_number': 'client-number', 'personal_account_type': 'personal-account-type', 'opened': 'opened',
                     'closed': 'closed', 'balance': 'balance', 'status_code': 'status-code', 'beneficiary': 'beneficiary',
                     'beneficiary_comment': 'beneficiary-comment', 'comments': 'comments'}

t_person_mapping = {'misp_name': 'person', 'comments': 'text', 'first_name': 'first-name', 'middle_name': 'middle-name',
                    'last_name': 'last-name', 'title': 'title', 'mothers_name': 'mothers-name', 'alias': 'alias',
                    'birthdate': 'date-of-birth', 'birth_place': 'place-of-birth', 'gender': 'gender', 'nationality1': 'nationality',
                    'passport_number': 'passport-number', 'passport_country': 'passport-country', 'ssn': 'social-security-number',
                    'id_number': 'identity-card-number'}

location_mapping = {'misp_name': 'geolocation', 'city': 'city', 'state': 'region', 'country_code': 'country', 'address': 'address',
                    'zip': 'zipcode'}

t_entity_mapping = {'misp_name': 'legal-entity', 'name': 'name', 'business': 'business', 'commercial_name': 'commercial-name',
                    'phone': 'phone-number', 'incorporation_legal_form': 'legal-form', 'incorporation_number': 'registration-number'}

goAMLmapping = {'from_account': t_account_mapping, 'to_account': t_account_mapping, 't_person': t_person_mapping,
                'from_person': t_person_mapping, 'to_person': t_person_mapping, 'reporting_person': t_person_mapping,
                'from_entity': t_entity_mapping, 'to_entity': t_entity_mapping,
                'location': location_mapping, 'address': location_mapping,
                'transaction': {'misp_name': 'transaction', 'transactionnumber': 'transaction-number', 'date_transaction': 'date',
                                'transaction_location': 'location', 'transmode_code': 'transmode-code', 'amount_local': 'amount',
                                'transmode_comment': 'transmode-comment', 'date_posting': 'date-posting', 'teller': 'teller',
                                'authorized': 'authorized', 'transaction_description': 'text'}}

nodes_to_ignore = ['addresses', 'signatory']
relationship_to_keep = ['signatory', 't_from', 't_from_my_client', 't_to', 't_to_my_client', 'address']

class GoAmlParser():
    def __init__(self):
        self.misp_event = MISPEvent()

    def read_xml(self, data):
        self.tree = ET.fromstring(data)

    def parse_xml(self):
        self.first_itteration()
        for t in self.tree.findall('transaction'):
            self.itterate(t, 'transaction')

    def first_itteration(self):
        submission_date = self.tree.find('submission_date').text.split('+')[0]
        self.misp_event.timestamp = int(time.mktime(time.strptime(submission_date, "%Y-%m-%dT%H:%M:%S")))
        for node in goAMLobjects['report']['nodes']:
            element = self.tree.find(node)
            if element is not None:
                self.itterate(element, element.tag)

    def itterate(self, tree, aml_type, referencing_uuid=None, relationship_type=None):
        objects = goAMLobjects[aml_type]
        referenced_uuid = referencing_uuid
        rel = relationship_type
        if aml_type not in nodes_to_ignore:
            try:
                mapping = goAMLmapping[aml_type]
                misp_object = MISPObject(name=mapping['misp_name'])
                for leaf in objects['leaves']:
                    element = tree.find(leaf)
                    if element is not None:
                        object_relation = mapping[element.tag]
                        attribute = {'object_relation': object_relation, 'value': element.text}
                        misp_object.add_attribute(**attribute)
                if aml_type == 'transaction':
                    for node in objects['nodes']:
                        element = tree.find(node)
                        if element is not None:
                            self.fill_transaction(element, element.tag, misp_object)
                self.misp_event.add_object(misp_object)
                last_object = self.misp_event.objects[-1]
                referenced_uuid = last_object.uuid
                if referencing_uuid and relationship_type:
                    referencing_object = self.misp_event.get_object_by_uuid(referencing_uuid)
                    referencing_object.add_reference(referenced_uuid, rel, None, **last_object)
            except KeyError:
                pass
        for node in objects['nodes']:
            element = tree.find(node)
            if element is not None:
                tag = element.tag
                if tag in relationship_to_keep:
                    rel = tag[2:] if tag.startswith('t_') else tag
                self.itterate(element, element.tag, referencing_uuid=referenced_uuid, relationship_type=rel)

    @staticmethod
    def fill_transaction(element, tag, misp_object):
        if 't_from' in tag:
            from_funds = element.find('from_funds_code').text
            from_funds_attribute = {'object_relation': 'from-funds-code', 'value': from_funds}
            misp_object.add_attribute(**from_funds_attribute)
            from_country = element.find('from_country').text
            from_country_attribute = {'object_relation': 'from-country', 'value': from_country}
            misp_object.add_attribute(**from_country_attribute)
        if 't_to' in tag:
            to_funds = element.find('to_funds_code').text
            to_funds_attribute = {'object_relation': 'to-funds-code', 'value': to_funds}
            misp_object.add_attribute(**to_funds_attribute)
            to_country = element.find('to_country').text
            to_country_attribute = {'object_relation': 'to-country', 'value': to_country}
            misp_object.add_attribute(**to_country_attribute)

def handler(q=False):
    if q is False:
        return False
    request = json.loads(q)
    if request.get('data'):
        data = base64.b64decode(request['data']).decode('utf-8')
    else:
        misperrors['error'] = "Unsupported attributes type"
        return misperrors
    aml_parser = GoAmlParser()
    try:
        aml_parser.read_xml(data)
    except Exception:
        misperrors['error'] = "Impossible to read XML data"
        return misperrors
    aml_parser.parse_xml()
    r = {'results': {'Object': [obj.to_json() for obj in aml_parser.misp_event.objects]}}
    return r

def introspection():
    return mispattributes

def version():
    moduleinfo['config'] = moduleconfig
    return moduleinfo


import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=PC-TP3-02;'
                      'Database=BR_RK7_MAIN;'
                      'Trusted_Connection=yes;')

print("Connected")

cursor = conn.cursor()
cursor.execute("select cast(cast(sqlquery as varbinary(max)) as nvarchar(max)) as sqlquery from PLG_IR_DATASETS where code = 126")

for row in cursor:
    ts = row[0]
    s = str(ts)
    s = s.replace(":USE_ALT_LANG","/*:USE_ALT_LANG*/0")

    tmpc = conn.cursor()
    tmpc.execute("select guidstring from restaurants where code = 26")
    for tr in tmpc:
        s = s.replace(":RESTAURANT","'"+str(tr[0])+"'")

    tmpc.execute("select guidstring from classificatorgroups where sifr = 10 and numingroup = 0")
    for tr in tmpc:
        s = s.replace(":CLASS","'"+str(tr[0]+"'"))

    tmpc.close

    s = s.replace(":date1","'2001-01-01'")
    s = s.replace(":date2","'2020-01-01'")

print(s)

cursor.execute(s)
for row in cursor:
    print(row)

cursor.close
conn.close


import requests

from pyrogram import Filters, Message

from rosebot import BOT
from rosebot.helpers import ReplyCheck

def define_word_ud(word):
    def get_data(word):
        r = requests.get(
            "http://api.urbandictionary.com/v0/define?term={}".format(word)
        )
        if r.status_code == 200:
            data = r.json()
            return data

    def get_definitions(data):
        definitions = []
        for definition in data["list"]:
            definitions.append(
                "{}\t{}\t{}".format(
                    definition["word"], definition["definition"], definition["example"]
                )
            )
        return definitions

    def get_five_results(definitions):
        five_definitions = ""
        for i in range(5):
            definition = definitions[i].split("\t")
            word = definition[0]
            definition_ = definition[1]
            example = definition[2]
            five_definitions = "{}\n**{}**\n{}\n\nExample:\n{}\n\n".format(
                five_definitions, word, definition_, example
            )
        return five_definitions

    data = get_data(word)
    if not data:
        return "Found nothing on Urban Dictionary"
    else:
        definitions = get_definitions(data)
        five_definitions = get_five_results(definitions)
        return five_definitions

@BOT.on_message(Filters.command("ud", "!"))
def post_ud(bot: BOT, message: Message):
    message_text = message.text.replace("!ud ", "")
    text = define_word_ud(message_text)
    BOT.send_message(
        chat_id=message.chat.id,
        text=text[:4096],
        disable_notification=True,
        reply_to_message_id=ReplyCheck(message),
    )
    if text[4096:]:
        BOT.send_message(
            chat_id=message.chat.id, text=text[4096:], disable_notification=True
        )
    if message.from_user.is_self:
        message.delete()


import tensorflow as tf;
import numpy as np;
import matplotlib.pyplot as plt;
from tensorflow.examples.tutorials.mnist import  input_data;
mnist=input_data.read_data_sets("MNIST_data/",one_hot=True,reshape=False);

def fully_connected(prev_layer, num_units):
    layer = tf.layers.dense(prev_layer, num_units, activation=tf.nn.relu)
    return layer

def conv_layer(prev_layer,layer_depth):
    strides = 2 if layer_depth % 3 == 0 else 1;
    conv_layer = tf.layers.conv2d(prev_layer, layer_depth*4, 3, strides, 'same', activation=tf.nn.relu)
    return conv_layer

def train(num_batches, batch_size,learning_rate):
    # Build placeholders for the input samples and labels
    inputs = tf.placeholder(tf.float32,[None,28,28,1]);
    labels = tf.placeholder(tf.float32,[None,10]);

    layer = inputs;
    for layer_i in range(1,20):
        layer = conv_layer(layer,layer_i);

    orig_shape = layer.get_shape().as_list();
    layer = tf.reshape(layer, shape=[-1,orig_shape[1]*orig_shape[2]*orig_shape[3]]);

    # Add one fully connected layer
    layer = fully_connected(layer,100);

    #Create the output layer with 1 node for each
    logits = tf.layers.dense(layer,10);

    #Define
    model_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits,labels=labels));

    train_opt =  tf.train.AdamOptimizer(learning_rate).minimize(model_loss);

    correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer());
        for batch_i in range(num_batches):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size);
            sess.run(train_opt,{inputs:batch_xs,labels:batch_ys});
            if batch_i % 100 == 0:
                loss, acc = sess.run([model_loss, accuracy], {inputs: mnist.validation.images,
                                                              labels: mnist.validation.labels})
                print(
                'Batch: {:>2}: Validation loss: {:>3.5f}, Validation accuracy: {:>3.5f}'.format(batch_i, loss, acc))
            elif batch_i % 25 == 0:
                loss, acc = sess.run([model_loss, accuracy], {inputs: batch_xs, labels: batch_ys})
                print('Batch: {:>2}: Training loss: {:>3.5f}, Training accuracy: {:>3.5f}'.format(batch_i, loss, acc))

        # At the end, score the final accuracy for both the validation and test sets
        acc = sess.run(accuracy, {inputs: mnist.validation.images,
                                  labels: mnist.validation.labels})
        print('Final validation accuracy: {:>3.5f}'.format(acc))
        acc = sess.run(accuracy, {inputs: mnist.test.images,
                                  labels: mnist.test.labels})
        print('Final test accuracy: {:>3.5f}'.format(acc))

        # Score the first 100 test images individually, just to make sure batch normalization really worked
        correct = 0
        for i in range(100):
            correct += sess.run(accuracy, feed_dict={inputs: [mnist.test.images[i]],
                                                     labels: [mnist.test.labels[i]]})

        print("Accuracy on 100 samples:", correct / 100);

num_batches = 800
batch_size = 64
learning_rate = 0.002

tf.reset_default_graph()
with tf.Graph().as_default():
    train(num_batches, batch_size, learning_rate)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 30 08:37:41 2023

@author: admin
"""
import copy,random,pygame

class Aliens(object):
    def __init__(self, k,openSpaces,row,column, pixel_w, pixel_h):
        self.k=k
        self.openSpaces = openSpaces
        self.alienSpaces = random.sample(self.openSpaces,k)
        self.row=row
        self.column=column
        self.h = row
        self.w = column
        self.pixel_w = pixel_w
        self.pixel_h = pixel_h
        self.xscale = pixel_w / column
        self.yscale = pixel_h / row

    def moveAlien(self, screen, x_offset, y_offset):

        def checkForOutOfBound(x,y,row,col):
            if x>=row or x<0 or y<0 or y>=col:
                return True
            return False

        moves=[[0,1],[1,0],[0,-1],[-1,0],[0,0]]
        random.shuffle(self.alienSpaces)
        aliensCopy=copy.deepcopy(self.alienSpaces)
        for alien in aliensCopy:
            #print(alien)
            random.shuffle(moves)
            for move in moves:
                neighbourRow = alien[0]+move[0]
                neighbourCol = alien[1]+move[1]
                res = checkForOutOfBound(neighbourRow,neighbourCol,self.row,self.column)
                if [neighbourRow,neighbourCol] not in self.alienSpaces and res == False and [neighbourRow,neighbourCol] in self.openSpaces:
                    self.alienSpaces.remove(alien)
                    self.alienSpaces.append([neighbourRow,neighbourCol])
                    break
        print("alienspaces",self.alienSpaces)
        for x,y in self.alienSpaces:
            bx = y * self.xscale
            by = x * self.yscale
            pygame.draw.rect(screen, (255, 5, 5), pygame.Rect(bx + x_offset, by + y_offset, self.xscale, self.yscale))


from gi.repository import MyPaint

def brushsetting_from_str(setting_str):
    return getattr(MyPaint.BrushSetting, "SETTING_" + setting_str.upper())

def set_base_value(brush, setting_str, value):
    setting = brushsetting_from_str(setting_str)
    return brush.set_base_value(setting, value)

def get_base_value(brush, setting_str):
    setting = brushsetting_from_str(setting_str)
    return brush.get_base_value(setting)

def set_base_color(brush, color_hsv):
    color_h, color_s, color_v = color_hsv
    set_base_value(brush, "color_h", color_h)
    set_base_value(brush, "color_s", color_s)
    set_base_value(brush, "color_v", color_v)

def get_base_color(brush):
    color_h = get_base_value(brush, "color_h")
    color_s = get_base_value(brush, "color_s")
    color_v = get_base_value(brush, "color_v")
    return color_h, color_s, color_v

__test__ = dict(allem="""

These are auxiliary functions to simplify the API provided by GObject
introspection.

>>> setting = brushsetting_from_str("color_h")
>>> setting == MyPaint.BrushSetting.SETTING_COLOR_H
True

>>> brush = MyPaint.Brush()
>>> set_base_value(brush, "radius_logarithmic", 12.0)
>>> get_base_value(brush, "radius_logarithmic")
12.0

>>> set_base_color(brush, (0.0, 1.0, 0.5))
>>> get_base_color(brush)
(0.0, 1.0, 0.5)

>>> get_base_value(brush, "color_s")
1.0

>>> get_base_value(brush, "color_v")
0.5

""")

if __name__ == '__main__':
    import doctest
    doctest.testmod()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov  6 15:00:41 2023

@author: maggie clarke

Step #1
Append task data files, perform tSSS & movement compensation
"""
import mne
import os
import os.path as op
from mne.preprocessing import maxwell_filter, compute_average_dev_head_t
from mne.chpi import (compute_chpi_amplitudes, compute_chpi_locs,
                      compute_head_pos, write_head_pos)

####### set these  before running #############################################
path = '/home/maggie/data/'

# setup subject name ### change this for each subject
s = 'vml_meg_015'

# set to True if headpos has not already been calculated & saved
calc_headpos = True
###############################################################################

# read in system specific cross-talk and fine calibration files
ct = op.join(path, 'ct_sparse.fif')
fc = op.join(path, 'sss_cal.dat')

# append task runs & save output
raw = mne.io.Raw(op.join(path, 'VML', '%s' %s,
                         '%s_1_raw.fif' %s), preload=True)
raw2 = mne.io.Raw(op.join(path, 'VML', '%s' %s,
                         '%s_2_raw.fif' %s), preload=True)
raw.append([raw, raw2])
raw.save(op.join(path, 'VML', '%s' %s, '%s_all_raw.fif' %s))
# delete old raw objects for memory
del raw, raw2

# read in raw data
raw = mne.io.Raw(op.join(path, 'VML', '%s' %s,
                         '%s_all_raw.fif' %s), preload=True)

# make a copy of raw and apply a lowpass filter (55 Hz) for vizualization
raw_filtered = raw.copy().load_data().filter(l_freq=None, h_freq=55.)

# plot raw data and visually inspect for bad channels
raw_filtered.plot()
# delete filtered raw object
del raw_filtered

# set bad channels in data structure - get these from
# runsheet OR visual inspection
raw.info['bads'] = ['MEG1213']

# setup for movement compensation by extracting coil info
if calc_headpos == True:
    amps = compute_chpi_amplitudes(raw)
    locs = compute_chpi_locs(raw.info, amps)
    pos = compute_head_pos(raw.info, locs)
    # save pos file (computing takes awhile)
    write_head_pos(op.join(path, 'VML', '%s' %s, '%s_all_pos.fif' %s), pos)
else:
    pos = mne.chpi.read_head_pos(op.join(path, 'VML', '%s' %s,
                                         '%s_all_pos.fif' %s))

# get initial head position and calculate average position (across time)
orig_head_dev_t = mne.transforms.invert_transform(raw.info["dev_head_t"])
avg_head_dev_t = mne.transforms.invert_transform(compute_average_dev_head_t
                                                     (raw, pos))

# plot head positions over time (green=initial, red=average)
if not os.path.exists(op.join(path, 'VML', '%s' %s, 'figures')):
    os.makedirs(op.join(path, 'VML', '%s' %s, 'figures'))

fig = mne.viz.plot_head_positions(pos)
for ax, val, val_ori in zip(
    fig.axes[::2],
    avg_head_dev_t["trans"][:3, 3],
    orig_head_dev_t["trans"][:3, 3],
):
    ax.axhline(1000 * val, color="r")
    ax.axhline(1000 * val_ori, color="g")
fig.savefig(op.join(path, 'VML', '%s' %s, 'figures', '%s_att_all_headpos' %s))

# use average position for movement compensation destination
destination = (raw.info['dev_head_t']['trans'][:3, 3])

# perform tSSS - we don't need to apply eSSS for event-related data
tsss_mc = maxwell_filter(raw, calibration=fc, cross_talk=ct,
                         st_duration=6, head_pos=pos,
                         destination=destination, st_correlation=0.98)

# save the tSSS processed file for next steps
tsss_mc.save(op.join(path, 'VML', '%s' %s, '%s_all_tsss.fif' %s))


import numpy as np

import torch
import torch.nn.functional
import torchvision.transforms

import PSMNet.models

# global preprocessing transform (from PSMNet Test_img.py)
_normal_mean_var = {'mean': [0.485, 0.456, 0.406],
                    'std': [0.229, 0.224, 0.225]}
_inference_transform = \
   torchvision.transforms.Compose([torchvision.transforms.ToTensor(),
                                   torchvision.transforms.Normalize(**_normal_mean_var)])

class PyramidalStereoMatchingNet:

    def __init__(self, psmnet_pretrained_weights, use_cuda, max_disp=192):
        self.psmnet = PSMNet.models.stackhourglass(maxdisp=max_disp)
        if use_cuda:
            state_dict = torch.load(psmnet_pretrained_weights)
        else:
            state_dict = torch.load(psmnet_pretrained_weights, map_location=torch.device('cpu'))

        self.psmnet = torch.nn.DataParallel(self.psmnet, device_ids=[0]) # required to be before load_state_dict()
        self.psmnet.load_state_dict(state_dict['state_dict'])
        self.psmnet.eval() # prep for inference
        if use_cuda:
            self.psmnet.cuda()
        else:
            self.psmnet.cpu()
    # /__init__()

    def _preprocess(self, imgL_in, imgR_in):

        # following steps from PSMNet Test_img.py
        imgL = _inference_transform(imgL_in)
        imgR = _inference_transform(imgR_in)

        # pad to width and height to 16 times
        top_pad = (16 - (imgL.shape[1] % 16)) % 16
        right_pad = (16 - (imgL.shape[2] % 16)) % 16

        imgL = torch.nn.functional.pad(imgL,(0,right_pad, top_pad,0)).unsqueeze(0)
        imgR = torch.nn.functional.pad(imgR,(0,right_pad, top_pad,0)).unsqueeze(0)

        return imgL, imgR
    # /_preprocess()

    def disparityField(self, left_img, right_img, use_cuda):
        psmnet_left_img, psmnet_right_img = \
            self._preprocess(left_img, right_img)
        if use_cuda:
            psmnet_left_img.cuda()
            psmnet_right_img.cuda()
        with torch.no_grad():
            disparity_field = self.psmnet(psmnet_left_img, psmnet_right_img)

        disparity_field = torch.squeeze(disparity_field)
        disparity_field_cpu = disparity_field.data.cpu().numpy()
        print('\tmin disp = {:.2f}, max disp = {:.2f}'.format(np.min(disparity_field_cpu),
                                                              np.max(disparity_field_cpu)))
        return disparity_field_cpu
    # /disparityField()

# /class PyramidalStereoMatching


import cv2
import imutils
import matplotlib.pyplot as plt
import numpy as np

def midPoint(ptA, ptB):
	return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)

def preparedImgForCntr(img):
    # preparing image for contour detction
    gray = cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY)  # converting image to the grayScale to be able to get the Cnt
    gray = cv2.GaussianBlur(gray, (7, 7), 0)  # blurring the image slightly to avoid false lines
    edged = cv2.Canny(gray, 50, 100)  # edge detecting
    edged = cv2.dilate(edged, None, iterations=1)  # it is followed by the erosion process to increase the size of the erroted objects
    edged = cv2.erode(edged, None, iterations=1)  # erasing the noise like the small white and black points
    return edged

def getSqrCoord (img):
    # getting the coordinates of the blue square
    image = preparedImgForCntr(img)
    cnts = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
    copy = img.copy()
    square = None
    for c in cnts:
        epsilon = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.015 * epsilon, True)
        if len(approx) == 4: # square has 4-points
            square = approx
            cv2.drawContours(copy, [square], -1, (0, 0, 0), 3)
            cv2.imshow("square", copy)
            square = np.array(square, dtype=np.float32)
            break  # if you found the square, break
    return square

def calcTransMtrx (sourceImg,sqrCoord,startX,startY,step):  # step is the width of the figure
    # compute transformation matrix and apply it
    # newCoord = topLeft, bottomLeft, bottomRight, topRight
    newCoord = np.array([[startX, startY], [startX, startY + step], [startX + step, startY + step], [startX + step, startX]], dtype=np.float32)  # new square coordinates
    transfMatrix = cv2.getPerspectiveTransform(sqrCoord, newCoord) # getting the transformation matrix
    modifiedImg = cv2.warpPerspective(sourceImg, transfMatrix, sourceImg.shape[:2][::-1])  # appying the transformation matrix on the source image

    newCoord = np.array(newCoord, dtype=int)
    return modifiedImg, newCoord

def regionOfInterest(img, vertices):
    mask = np.zeros_like(img)  # a zero matrix with the same size as the original image
    match_mask_color = (255, 255, 255)  # color to be put as a mask
    # cropping the region of interest by drawing a polygon with the passed vertices
    cv2.fillPoly(mask, vertices, match_mask_color)
    masked_image = cv2.bitwise_and(img, mask)
    return masked_image

def cropRegionOfInterest(img, heightFactor, widthFactor):
    height, width, _ = img.shape
    # region_of_interest_vertices = [topLeft,bottomLeft ,topRight, bottomRight]
    RegionOfInterestList = [(width * widthFactor, 0), (width * widthFactor, height), (height * heightFactor, 0),
                            (height * heightFactor, height)]
    imagePrepared = preparedImgForCntr(img)
    croppedImage = regionOfInterest(imagePrepared, np.array([RegionOfInterestList], np.int32))
    return croppedImage

def drawLines(img, lines, thickness=3):
    # If there are no lines to draw, exit.
    if lines is None:
        return
    # Make a copy of the original image.
    img = np.copy(img)
    # Create a blank image that matches the original in size.
    line_img = np.zeros(
        (
            img.shape[0],
            img.shape[1],
            3
        ),
        dtype=np.uint8,
    )
    # Loop over all lines and draw them on the blank image.
    for line in lines:
        for x1, y1, x2, y2 in line:
            cv2.line(line_img, (x1, y1), (x2, y2), [255, 0, 0], thickness)
    # Merge the image with the lines onto the original, we could both functions but, addWeighted is better
    #img = cv2.add(img, line_img)
    img = cv2.addWeighted(img, 0.8, line_img, 1.0, 0.0)
    # Return the modified image.
    return img

def drawHoughLines (img):
    # detecting the lines
    lines = cv2.HoughLinesP(  # this function will return the lines holding line's points instead of rho and theta
        cropRegionOfInterest(img, 4/3, 1/3),
        rho=10,  # the length of the perpendicular line between the origin and the line
        theta=0.01,  # angle with the x-axis clockwise where, the x-axis is at the top of the image
        threshold=200,
        lines=np.array([]),
        minLineLength=170,  # minimum line length
        maxLineGap=10  # the max distance between two lines to treat them as two different lines
    )
    listOfLines = []
    if lines is None:
        raise Exception("No lines found")
    if len(lines) < 2:
        raise Exception("you can't calculate distance with less than two lines")

    print("number of lines:\n", len(lines))
    i = 0
    for line in lines:
        listOfPoints = []
        for x1, y1, x2, y2 in line:
            print("line number:", i, "[ x1:", x1, "x2:", x2, "y1:", y1, "y2:", y2, "]")
            listOfPoints.append(x1)
            listOfPoints.append(y1)
            listOfPoints.append(x2)
            listOfPoints.append(y2)
        listOfLines.append(listOfPoints)
        i += 1
    # drawing the lines
    line_image = drawLines(image, lines)
    print()
    return listOfLines, line_image

def calcDistancebetTwoLines (line1, line2 , Y, scale=1):
    # calc the distance between the 2-lines in the x direction at point Y
    slope1 = (line1[3] - line1[1]) / (line1[2] - line1[0])
    slope2 = (line2[3] - line2[1]) / (line2[2] - line2[0])
    # the value of X at height y
    XOfline1AtY = line1[0] + (Y - line1[1]) / slope1
    XOfline2AtY = line2[0] + (Y - line2[1]) / slope2

    distanceBetMidPoints = abs(XOfline1AtY - XOfline2AtY)
    # actual length is: length / scale
    distanceBetMidPoints /= scale
    return distanceBetMidPoints

def drawDistanceAtY (img, line1, line2, D, Y):
    # drawing the distance in meters at height = Y
    # drawing the contours on the image
    orig = img.copy()
    slope1 = (line1[3] - line1[1]) / (line1[2] - line1[0])
    slope2 = (line2[3] - line2[1]) / (line2[2] - line2[0])
    XOfline1AtY = int(line1[0] + (Y - line1[1]) / slope1)
    XOfline2AtY = int(line2[0] + (Y - line2[1]) / slope2)
    # drawing circles corresponding to the current points
    cv2.circle(orig, (XOfline1AtY, Y), 5, (100, 50, 100), -1)
    cv2.circle(orig, (XOfline2AtY, Y), 5, (100, 50, 100), -1)

    # connecting them with a line
    cv2.line(orig, (XOfline1AtY, Y), (XOfline2AtY, Y), (100, 50, 100), 2)

    (mX, mY) = midPoint((XOfline1AtY, Y), (XOfline2AtY, Y))  # writting the text in the mid distance between the two contours
    writeDistAboveLineBy = 10
    cv2.putText(orig, "{:.2f}m".format(D), (int(mX), int(mY - writeDistAboveLineBy)), cv2.FONT_HERSHEY_SIMPLEX, 0.55,
                (100, 50, 100), 2)

    # returning the output image
    return orig

#############################################################################################
if __name__ == '__main__':
    if not cv2.useOptimized():
        cv2.setUseOptimized(True)  # setting the optimization to true to decrease the execution time of the code

    # Reading the image
    source = cv2.imread('road_final.jpg')
    cv2.imshow("Source Image", source)

    #  detecting the blue square and saving its coordinates
    sqrCoord = getSqrCoord(source)
    sqWidth = 75  # the real width of the square
    scale = 1  # the scale of the image after modifying the perspective
    scaledWidth = sqWidth * scale  # 1 : 1 scale
    # transforming the image and getting square's new coordinates
    modifiedImage, sqrCoord = calcTransMtrx(source, sqrCoord, 250, 250, scaledWidth)
    cv2.imshow("birdEyeView", modifiedImage)
#############################################################################################
    image = modifiedImage
    # Extracting the lines from the image and creating an image with the lines drawn on it
    lines, line_image = drawHoughLines(image)  # the Hough lines and the new image with lines drawn on it
    line1 = lines[0]
    line2 = lines[1]

    H = 350  # the height to calc and draw the distance at
    distanceBetTwoLines = calcDistancebetTwoLines(line1, line2,H,scale)   # the distance between the 2 lines at height H
    distanceBetTwoLines /= 100.00  # so that the distance is in meters
    dimImage = drawDistanceAtY(image, line1, line2, distanceBetTwoLines, H)  # drawing the distance between the 2 lines at height H
    cv2.imshow("Dimensions", dimImage)
    print('width of the road at height', H, ':', distanceBetTwoLines, "m")
    # printing the Hough lines
    plt.imshow(line_image)
    plt.show()

    cv2.waitKey()


#!/usr/bin/env python
# coding: utf-8

# In[1]:

#jaiiii sri ram

# In[2]:

#steps
'''
1.input raw data
2.tokenizing
3.textcleaning
   - remove punctuations
   - remove stopwords
   - stemming/lemmatization
4.vectorization
5.modelling
6.accuracy and final model
'''

# # BUSINESS PROBLEM UNDERSTANDING

# In[3]:

#Performing sentimental analysis on the input data and saying whether the feedback of the students is positive or not
#application - applying on the feedback data of the students on the lecturer and analying their data instead of manually reading each and every feedback

# In[4]:

import pandas as pd
import seaborn as sns
import matplotlib as plt

# In[5]:

df = pd.read_csv("C:\\Users\\Sricharan Reddy\\Downloads\\feedback dataset.csv")

# In[6]:

df.head()

# In[7]:

df.info()

# In[8]:

#here the first column is a text column in which the feedback is written and sentiment is the output column of dataset

# In[9]:

import nltk
import re

# In[10]:

from nltk.corpus import stopwords
from nltk.stem import PorterStemmer

# In[11]:

ps = PorterStemmer()

# In[12]:

corpus = []
for i in range(len(df)):
    rp = re.sub("[^a-zA-Z]"," ",df['text'][i])
    rp = rp.lower()
    rp = rp.split()
    rp = [ps.stem(word) for word in rp if not  word in set(stopwords.words('english'))]
    rp = " ".join(rp)
    corpus.append(rp)

# In[13]:

corpus[:5]

# In[14]:

#working fine here we have done removing punctuations , stopwords , stemming lessgooo

# In[15]:

from sklearn.feature_extraction.text import CountVectorizer

# In[16]:

cv = CountVectorizer()

# In[17]:

x = cv.fit_transform(corpus).toarray()

# In[18]:

y = df['sentiment']

# In[19]:

from sklearn.model_selection import train_test_split

# In[20]:

x_train , x_test , y_train , y_test = train_test_split(x,y,test_size=0.2,random_state = 9)

# In[ ]:

# # Modelling

# ## 1.Naive Bayes classifier

# In[21]:

from sklearn.naive_bayes import MultinomialNB

# In[22]:

nbmodel = MultinomialNB()

# In[23]:

nbmodel.fit(x_train,y_train)

# In[24]:

y_pred_train  = nbmodel.predict(x_train)

# In[25]:

y_pred_test = nbmodel.predict(x_test)

# In[26]:

from sklearn.metrics import accuracy_score

# In[27]:

accuracy_score(y_train,y_pred_train)

# In[28]:

accuracy_score(y_test,y_pred_test)

# In[29]:

nbmodel.score(x_test,y_test)

# ## 2.XGBOOST ALGORITHM

# In[30]:

from xgboost import XGBClassifier

# In[31]:

model = XGBClassifier()
model.fit(x_train,y_train)

# In[32]:

y_pred_train  = model.predict(x_train)
y_pred_test = model.predict(x_test)

# In[33]:

from sklearn.metrics import accuracy_score

# In[34]:

accuracy_score(y_train,y_pred_train)

# In[35]:

accuracy_score(y_test,y_pred_test)

# In[36]:

model.score(x_test,y_test)

# # 3.Logistic regression

# In[37]:

from sklearn.linear_model import LogisticRegression

# In[38]:

model = LogisticRegression()
model.fit(x_train,y_train)

# In[39]:

y_pred_train  = model.predict(x_train)
y_pred_test = model.predict(x_test)

# In[40]:

print(accuracy_score(y_train,y_pred_train))
print(accuracy_score(y_test,y_pred_test))

# In[41]:

model.score(x_test,y_test)

# # To make a new prediction : -

# In[42]:

#for predicting for a new input you have to do all the steps of the preprocessing before model u can't just give it directly

# In[59]:

new_feedback = "teaching is excellent and the way of explanation is very good "

# In[60]:

#new_feedback = "teaching is not good the way of explanation is bad and ambiguious "

# In[61]:

corpus = []

# In[62]:

rp = re.sub("[^a-zA-Z]"," ",new_feedback)
rp = rp.lower()
rp = rp.split()
rp = [ps.stem(word) for word in rp if not  word in set(stopwords.words('english'))]
rp = " ".join(rp)
corpus.append(rp)

# In[63]:

x_new = cv.transform(corpus).toarray()

# In[64]:

x_new

# In[65]:

ans = model.predict(x_new)

# In[66]:

if(ans==1):
    print("Positive opinion")
else:
    print("Negative opinion")

# In[67]:

#scr

# In[ ]:


from tensorflow.keras.preprocessing.image import ImageDataGenerator

import pandas as pd
import numpy as np

from data_loaders.utils import _getLabels
from utils.paths import getDataDirectory

import types

def getData(dataset_name, test_dataset_name, label_set, get_batches_with_fake_waves_fn, dim=256):
    train_data = _getLabels(label_set, "train")
    validation_data = _getLabels(label_set, "validation")
    test_data = _getLabels(label_set, "test")

    data_path = getDataDirectory(dataset_name)
    test_data_path = getDataDirectory(test_dataset_name)

    train_datagen = ImageDataGenerator()
    test_datagen = ImageDataGenerator()

    train = train_datagen.flow_from_dataframe(
        train_data,
        directory=data_path,
        x_col="filename", y_col=["left", "top", "right", "bottom"],
        target_size=(dim, dim),
        batch_size=32,
        color_mode = "rgb",
        class_mode="raw",
        shuffle=True,
    )
    val = test_datagen.flow_from_dataframe(
        validation_data,
        directory=test_data_path,
        x_col="filename", y_col=["left", "top", "right", "bottom"],
        target_size=(dim, dim),
        batch_size=1,
        color_mode = "rgb",
        class_mode="raw",
        shuffle=False
    )
    test = test_datagen.flow_from_dataframe(
        test_data,
        directory=test_data_path,
        x_col="filename", y_col=["left", "top", "right", "bottom"],
        target_size=(dim, dim),
        batch_size=1,
        color_mode = "rgb",
        class_mode="raw",
        shuffle=False
    )
    train._get_batches_of_transformed_samples = types.MethodType( get_batches_with_fake_waves_fn, train ) # replace method for loading train batches
    return train, val, test


import json

class Asset(object):

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def serialize(self):
        return json.dumps(self, default=lambda o: o.__dict__)

if __name__ == '__main__':
    a = Asset('lee', 100.80)
    print(a.serialize())


from __future__ import (
    annotations,
)

import hashlib
import hmac
from io import (
    BytesIO,
)
from typing import (
    Generic,
)

from btctoy.codec import (
    encode_base58_checksum,
)
from btctoy.crypto.prime import (
    is_prime,
)
from btctoy.crypto.types import (
    FieldElementT,
)

# Secp256k1 ECDSA parameters
# https://en.bitcoin.it/wiki/Secp256k1

EC_A = 0
EC_B = 7
EC_PRIME = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 2**0
G_X = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
G_Y = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
G_ORDER = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141

def is_on_elliptic_curve(
    x: FieldElementT, y: FieldElementT, a: FieldElementT, b: FieldElementT
) -> bool:
    return y**2 == x**3 + a * x + b

def hash160(s: bytes) -> bytes:
    """sha256 followed by ripemd160"""
    return hashlib.new("ripemd160", hashlib.sha256(s).digest()).digest()

def hash256(s: bytes) -> bytes:
    """two rounds of sha256"""
    return hashlib.sha256(hashlib.sha256(s).digest()).digest()

class ModularFieldInteger:
    value: int
    prime: int

    def __init__(self, value: int, prime: int) -> None:
        if is_prime(prime) is False:
            raise ValueError(f"{prime} is not a prime integer")
        self.value = value % prime
        self.prime = prime

    def __repr__(self) -> str:
        return f"F_{self.prime}({self.value})"

    def __eq__(self, other: ModularFieldInteger) -> bool:
        if other is None:
            return False
        return self.value == other.value and self.prime == other.prime

    def __ne__(self, other: ModularFieldInteger) -> bool:
        # this should be the inverse of the == operator
        return not (self == other)

    def __add__(self, other: ModularFieldInteger) -> ModularFieldInteger:
        if self.prime != other.prime:
            raise TypeError("Cannot add two numbers in different Fields")
        value = (self.value + other.value) % self.prime
        return self.__class__(value, self.prime)

    def __sub__(self, other: ModularFieldInteger) -> ModularFieldInteger:
        if self.prime != other.prime:
            raise TypeError("Cannot subtract two numbers in different Fields")
        value = (self.value - other.value) % self.prime
        return self.__class__(value, self.prime)

    def __mul__(self, other: ModularFieldInteger) -> ModularFieldInteger:
        if self.prime != other.prime:
            raise TypeError("Cannot multiply two numbers in different Fields")
        value = (self.value * other.value) % self.prime
        return self.__class__(value, self.prime)

    def __pow__(self, exponent: int) -> ModularFieldInteger:
        n = exponent % (self.prime - 1)
        value = pow(self.value, n, self.prime)
        return self.__class__(value, self.prime)

    def __truediv__(self, other: ModularFieldInteger) -> ModularFieldInteger:
        if self.prime != other.prime:
            raise TypeError("Cannot divide two numbers in different Fields")
        value = (self.value * pow(other.value, self.prime - 2, self.prime)) % self.prime
        return self.__class__(value, self.prime)

    def __rmul__(self, coefficient: int) -> ModularFieldInteger:
        value = (self.value * coefficient) % self.prime
        return self.__class__(value=value, prime=self.prime)

class EllipcCurvePoint(Generic[FieldElementT]):
    x: FieldElementT
    y: FieldElementT
    a: FieldElementT
    b: FieldElementT

    def __init__(
        self,
        x: FieldElementT | None,
        y: FieldElementT | None,
        a: FieldElementT,
        b: FieldElementT,
    ) -> None:
        self.a = a
        self.b = b
        self.x = x
        self.y = y

        # x being None and y being None represents the point at infinity
        # Check for that here since the equation below won't make sense
        # with None values for both.
        if self.x is None and self.y is None:
            return

        if self.x is None or self.y is None:
            raise ValueError("Coordinates should both be None, or both be FieldElement")

        if is_on_elliptic_curve(x, y, a, b) is False:
            raise ValueError(f"({x}, {y}) is not on the elliptic curve")

    def __eq__(self, other: EllipcCurvePoint[FieldElementT]) -> bool:
        return (
            self.x == other.x
            and self.y == other.y
            and self.a == other.a
            and self.b == other.b
        )

    def __ne__(self, other: EllipcCurvePoint[FieldElementT]) -> bool:
        # this should be the inverse of the == operator
        return not (self == other)

    def __repr__(self) -> str:
        if self.x is None:
            return "ECP(infinity)"
        elif isinstance(self.x, ModularFieldInteger):
            return f"ECP({self.x.value}, {self.y.value})_{ self.a.value}_{self.b.value}_F_{self.x.prime}"
        return f"ECP({self.x}, {self.y})_{self.a}_{self.b}".format(
            self.x, self.y, self.a, self.b
        )

    def __add__(
        self, other: EllipcCurvePoint[FieldElementT]
    ) -> EllipcCurvePoint[FieldElementT]:
        if self.a != other.a or self.b != other.b:
            raise TypeError(
                "Points {}, {} are not on the same curve".format(self, other)
            )
        # Case 0.0: self is the point at infinity, return other
        if self.x is None:
            return other
        # Case 0.1: other is the point at infinity, return self
        if other.x is None:
            return self

        # Case 1: self.x == other.x, self.y != other.y
        # Result is point at infinity
        if self.x == other.x and self.y != other.y:
            return self.__class__(None, None, self.a, self.b)

        # Case 2: self.x ≠ other.x
        # Formula (x3,y3)==(x1,y1)+(x2,y2)
        # s=(y2-y1)/(x2-x1)
        # x3=s**2-x1-x2
        # y3=s*(x1-x3)-y1
        if self.x != other.x:
            s = (other.y - self.y) / (other.x - self.x)
            x = s**2 - self.x - other.x
            y = s * (self.x - x) - self.y
            return self.__class__(x, y, self.a, self.b)

        # Case 4: if we are tangent to the vertical line,
        # we return the point at infinity
        # note instead of figuring out what 0 is for each type
        # we just use 0 * self.x
        if self == other and self.y == 0 * self.x:
            return self.__class__(None, None, self.a, self.b)

        # Case 3: self == other
        # Formula (x3,y3)=(x1,y1)+(x1,y1)
        # s=(3*x1**2+a)/(2*y1)
        # x3=s**2-2*x1
        # y3=s*(x1-x3)-y1
        if self == other:
            s = (3 * self.x**2 + self.a) / (2 * self.y)
            x = s**2 - 2 * self.x
            y = s * (self.x - x) - self.y
            return self.__class__(x, y, self.a, self.b)

    def __rmul__(self, coefficient: int) -> EllipcCurvePoint[FieldElementT]:
        # Implementation of fast scalar product
        coef = coefficient
        current = self
        result = self.__class__(None, None, self.a, self.b)
        while coef:
            if coef & 1:
                result += current
            current += current
            coef >>= 1
        return result

class S256Integer(ModularFieldInteger):
    def __init__(self, value: int, prime: int = EC_PRIME) -> None:
        if prime != EC_PRIME:
            raise ValueError(
                f"S256Integer only supports EC_PRIME={EC_PRIME:x} as prime"
            )
        super().__init__(value=value, prime=prime)

    def __repr__(self) -> str:
        return f"{self.value:064x}"

    def sqrt(self) -> ModularFieldInteger:
        return self ** ((EC_PRIME + 1) // 4)

A = S256Integer(EC_A)
B = S256Integer(EC_B)

class S256Point(EllipcCurvePoint[ModularFieldInteger]):
    def __init__(
        self,
        x: int | S256Integer | None,
        y: int | S256Integer | None,
        a: S256Integer = A,
        b: S256Integer = B,
    ) -> None:
        if a != A:
            raise ValueError(f"S256Integer only supports A={A} for a")
        if b != B:
            raise ValueError(f"S256Integer only supports B={B} for b")
        super().__init__(
            x=None if x is None else S256Integer(x) if isinstance(x, int) else x,
            y=None if y is None else S256Integer(y) if isinstance(x, int) else y,
            a=A,
            b=B,
        )

    def __repr__(self) -> str:
        if self.x is None:
            return "S256Point(infinity)"
        else:
            return f"S256Point({self.x}, {self.y})"

    def __rmul__(self, coefficient: int) -> S256Point:
        coef = coefficient % G_ORDER
        return super().__rmul__(coef)

    def verify(self, z: int, sig: Signature) -> bool:
        s_inv = pow(sig.s, G_ORDER - 2, G_ORDER)
        u = z * s_inv % G_ORDER
        v = sig.r * s_inv % G_ORDER
        total = u * G + v * self
        return total.x.value == sig.r

    def sec(self, compressed: bool = True) -> bytes:
        """returns the binary version of the SEC format"""
        # TODO implementer la fonction
        if compressed:
            # if compressed, starts with b'\x02' if self.y.value is even, b'\x03' if self.y is odd
            # then self.x.value
            # remember, you have to convert self.x.value and self.y.value to binary (some_integer.to_bytes(32, 'big'))
            if self.y.value % 2 == 0:
                return b"\x02" + self.x.value.to_bytes(32, "big")
            else:
                return b"\x03" + self.x.value.to_bytes(32, "big")
            # pass
        else:
            # if non-compressed, starts with b'\x04' followod by self.x and then self.y
            return (
                b"\x04"
                + self.x.value.to_bytes(32, "big")
                + self.y.value.to_bytes(32, "big")
            )
            _# pass

    def hash160(self, compressed: bool = True) -> bytes:
        return hash160(self.sec(compressed))

    def address(self, compressed: bool = True, testnet: bool = False) -> str:
        h160 = self.hash160(compressed)
        if testnet:
            prefix = b"\x6f"
        else:
            prefix = b"\x00"
        return encode_base58_checksum(prefix + h160)

    @classmethod
    def parse(cls, sec_bin: bytes) -> S256Point:
        """returns a Point object from a SEC binary (not hex)"""
        # TODO completer la fonction qui décode ce qui est encodé dans sec()
        pass

G = S256Point(
    G_X,
    G_Y,
)

class Signature:
    r: int
    s: int

    def __init__(self, r: int, s: int) -> None:
        self.r = r
        self.s = s

    def __repr__(self) -> str:
        return f"Signature(r={self.r}, s={self.s})"

    def der(self) -> bytes:
        rbin = self.r.to_bytes(32, byteorder="big")
        # remove all null bytes at the beginning
        rbin = rbin.lstrip(b"\x00")

        if rbin[0] & 0x80:
            rbin = b"\x00" + rbin
        result = bytes([2, len(rbin)]) + rbin  # <1>
        sbin = self.s.to_bytes(32, byteorder="big")
        sbin = sbin.lstrip(b"\x00")
        if sbin[0] & 0x80:
            sbin = b"\x00" + sbin
        result += bytes([2, len(sbin)]) + sbin
        return bytes([0x30, len(result)]) + result

    @classmethod
    def parse(cls, signature_bin: bytes) -> Signature:
        s = BytesIO(signature_bin)
        compound = s.read(1)[0]
        if compound != 0x30:
            raise SyntaxError("Bad Signature")
        length = s.read(1)[0]
        if length + 2 != len(signature_bin):
            raise SyntaxError("Bad Signature Length")
        marker = s.read(1)[0]
        if marker != 0x02:
            raise SyntaxError("Bad Signature")
        rlength = s.read(1)[0]
        r = int.from_bytes(s.read(rlength), "big")
        marker = s.read(1)[0]
        if marker != 0x02:
            raise SyntaxError("Bad Signature")
        slength = s.read(1)[0]
        s = int.from_bytes(s.read(slength), "big")
        if len(signature_bin) != 6 + rlength + slength:
            raise SyntaxError("Signature too long")
        return cls(r, s)

class PrivateKey:
    secret: int
    point: S256Point

    def __init__(self, secret: int) -> None:
        self.secret = secret
        self.point = secret * G

    def hex(self) -> str:  # noqa: A003
        return f"{self.secret:064x}"

    def sign(self, z: int) -> Signature:
        k = self.deterministic_k(z)
        r = (k * G).x.value
        k_inv = pow(k, G_ORDER - 2, G_ORDER)
        s = (z + r * self.secret) * k_inv % G_ORDER
        if s > G_ORDER / 2:
            s = G_ORDER - s
        return Signature(r, s)

    def deterministic_k(self, z: int) -> int:
        k = b"\x00" * 32
        v = b"\x01" * 32
        if z > G_ORDER:
            z -= G_ORDER
        z_bytes = z.to_bytes(32, "big")
        secret_bytes = self.secret.to_bytes(32, "big")
        s256 = hashlib.sha256
        k = hmac.new(k, v + b"\x00" + secret_bytes + z_bytes, s256).digest()
        v = hmac.new(k, v, s256).digest()
        k = hmac.new(k, v + b"\x01" + secret_bytes + z_bytes, s256).digest()
        v = hmac.new(k, v, s256).digest()
        while True:
            v = hmac.new(k, v, s256).digest()
            candidate = int.from_bytes(v, "big")
            if candidate >= 1 and candidate < G_ORDER:
                return candidate
            k = hmac.new(k, v + b"\x00", s256).digest()
            v = hmac.new(k, v, s256).digest()

    def wif(self, compressed: bool = True, testnet: bool = False) -> str:
        secret_bytes = self.secret.to_bytes(32, "big")
        if testnet:
            prefix = b"\xef"
        else:
            prefix = b"\x80"
        if compressed:
            suffix = b"\x01"
        else:
            suffix = b""
        return encode_base58_checksum(prefix + secret_bytes + suffix)