import numpy as np
import matplotlib.pyplot as plt

class Model(object):

    def __init__(self):
        pass

    def evaluate(self, loader):
        raise NotImplementedError

    def predict(self, data):
        raise NotImplementedError

    def train(self, train_loader, valid_loader=None, test_loader=None, epochs=1, eval_freq=1, plot=True, confusion_matrix=False):

        training_loss = []
        validation_loss = []
        testing_loss = []
        training_acc = []
        validation_acc = []
        testing_acc = []

        evaluated_data = 0
        total_batches = 0
        running_loss = 0.0
        running_acc = 0.0
        for e in range(epochs):
            for i, (data, labels) in enumerate(train_loader):
                total_batches += 1
                evaluated_data += labels.shape[0]

                self.learning_algorithm()

                # adding loss and accuracy to plots
                if total_batches % eval_freq == 0:

                    training_string = ""
                    validation_string = ""
                    testing_string = ""

                    if plot:
                        training_loss.append( running_loss / evaluated_data )
                        training_acc.append( running_acc / evaluated_data  )

                        #loss, acc = self.evaluate(train_loader)
                        #training_loss.append( loss )
                        #training_acc.append( acc )

                        training_string = f"\ttraining loss: {training_loss[-1]:.4f}"

                        if valid_loader != None:
                            loss, acc = self.evaluate(valid_loader)
                            validation_loss.append( loss )
                            validation_acc.append( acc )

                            validation_string = f"\tvalidation accuracy: {validation_acc[-1]:.4f}"

                        if test_loader != None:
                            loss, acc = self.evaluate(test_loader)
                            testing_loss.append( loss )
                            testing_acc.append( acc )

                            testing_string = f"\ttesting accuracy: {testing_acc[-1]:.4f}"

                    print(f"epoch: {e+1:4d}\tbatch: {i+1:5d}{training_string}{validation_string}{testing_string}")

        # gettings final statistics
        final_train_loss, final_train_acc = self.evaluate(train_loader)       # Training
        final_valid_loss, final_valid_acc = self.evaluate(valid_loader)       # Validation
        final_test_loss, final_test_acc = self.evaluate(test_loader)          # Testing

        training_loss.append( final_train_loss )
        validation_loss.append( final_valid_loss )
        testing_loss.append( final_test_loss )
        training_acc.append( final_train_acc )
        validation_acc.append( final_valid_acc )
        testing_acc.append( final_test_acc )

        # plottings statistics
        if plot:
            plot_loss(np.linspace(0, epochs, len(training_loss)),
                    train_error=training_loss,
                    valid_error=validation_loss,
                    test_error=testing_loss,
                    title="PLA: Loss")
            plot_accuracy(np.linspace(0, epochs, len(validation_acc)),
                    train_accuracy=training_acc,
                    valid_accuracy=validation_acc,
                    test_accuracy=testing_acc,
                    title="PLA Accuracy")

        # returning statistics
        return final_train_loss, final_train_acc, final_valid_loss, final_valid_acc, final_test_loss, final_test_acc


# coding:utf-8
from utils.util import get_code_token, get_requests, get_header ,form_post ,login ,json_post

#login('00852','20181205')

# rf_url = 'https://passport.lagou.com/grantServiceTicket/grant.html'
# get_header(rf_url)
cUserid = 80

def calling(cUserid):
	calling_url = 'https://easy.lagou.com/phonecall/getVirtualNum.json'
	calling_header = get_code_token('https://easy.lagou.com/talent/index.htm')
	calling_data = {'cUserId':cUserid}
	s = form_post(url=calling_url,headers=calling_header,data=calling_data,remark=u'直call大咖')
	# if s['message'] == u'成功':
	# print (u'virtualPhone:' ,s['content']['data']['result']['virtualPhone'])
	return s

# print(calling(cUserid))


# idea: problem is very similar to medium vertical traversal but the one thing
# we have to sort the nested array with row value
# the idea is to add row + val to nested array and sort them
# than loop throught and call sort for every nested array as well and
# add second value from this array to result

from collections import defaultdict
from typing import List, Optional

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def verticalTraversal(self, root: Optional[TreeNode]) -> List[List[int]]:
        if not root:
            return []

        result = []
        data = defaultdict(list)
        stack = [(root, 0, 0)]
        while stack:
            node, row, col = stack.pop()
            data[col].append([abs(row), node.val])
            if node.left:
                stack.append([node.left, row + 1, col - 1])
            if node.right:
                stack.append([node.right, row + 1, col + 1])

        for key in sorted(data.keys()):
            result.append([v[1] for v in sorted(data[key])])

        return result


import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#Define some hyper-parameters
learning_rate = 0.01
epochs =1000

display_step = 100

# First generate some random (X, Y) points, in the plane
N = 100
xrange = 100
yrange = 10
db = 0.0

train_X1 = xrange * np.random.random_sample(N)
train_X2 = xrange * np.random.random_sample(N)
#Here is the line Y = W1X1+ W2X2 + B. The computed value should be somehwere close
train_Y = 0.54 * train_X1 + 0.77* train_X2 + 2.5

#Define place holer for data that will be fed to the odel
X1= tf.placeholder("float")
X2= tf.placeholder("float")
Y= tf.placeholder("float")

#Define the vaiables that will be optimized by training
#Initialize them randomly
W1= tf.Variable(np.random.randn(), name="Weights1")
W2= tf.Variable(np.random.randn(), name="Weights2")
b =tf.Variable(np.random.randn(), name="Bias")

# Define the Graph, cost and optmizer
pred = tf.add(tf.add(tf.multiply(X2, W2), tf.multiply(X1, W1)), b)
cost = tf.reduce_mean(tf.pow(pred-Y, 2))/N
opti = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
init = tf.global_variables_initializer();

# Now train the network
with tf.Session() as sess :
  sess.run(init)
  for ep in range(epochs):
    for (x1, x2, y) in zip(train_X1, train_X2, train_Y) :
      sess.run(opti, feed_dict={X1:x1, X2:x2, Y:y})
    if ((ep+1) % display_step == 0) :
      cst = sess.run(cost, feed_dict={X1:train_X1, X2:train_X2, Y:train_Y})
      print("Epoch :", ep+1," - Cost :", cst)
  print("Training Complete.....")
  cost = sess.run(cost, feed_dict={X1:train_X1, X2:train_X2, Y:train_Y})
  print("Cost  :", cost)
  print("Computed W1: ", sess.run(W1))
  print("Computed W2: ", sess.run(W2))
  print("Computed B: ", sess.run(b))


import cv2
import numpy as np
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

captura = cv2.VideoCapture(0)
captura.set(cv2.CAP_PROP_FPS,90)

captura.set(cv2.CAP_PROP_FRAME_WIDTH,320)
captura.set(cv2.CAP_PROP_FRAME_HEIGHT,240)

#La resolucion de la camara es 640x480 ------> Tamaño de la ventana openCv

anchoVentana = 320
altoVentana = 240
mitadVentana = anchoVentana//2
tolerancia = 10

promAntX = 0

pwmA = 18
motorA1 = 15
motorA2 = 16

pwmB = 7
motorB1 = 11
motorB2 = 13

standBy = 12

GPIO.setup(pwmA,GPIO.OUT)
GPIO.setup(motorA1,GPIO.OUT)
GPIO.setup(motorA2,GPIO.OUT)

GPIO.setup(pwmB,GPIO.OUT)
GPIO.setup(motorB1,GPIO.OUT)
GPIO.setup(motorB2,GPIO.OUT)

GPIO.setup(standBy,GPIO.OUT)

intensidadA = GPIO.PWM(pwmA,100)
intensidadB = GPIO.PWM(pwmB,100)

intensidadA.start(0)
intensidadB.start(0)

def grabarVideo():
	ret,frame = captura.read()
	return frame

def filtrar(frame):
    img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    th, img_bw = cv2.threshold(img, 250, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
    rect = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    aux = cv2.morphologyEx(img_bw, cv2.MORPH_DILATE, rect)
    out = aux - img_bw
    return (out,img_bw)

def extraerLinea(canny):
	mascara = cv2.findNonZero(canny)
	return mascara

def puntosImportantesVentana(frame):
	cv2.line(frame,(mitadVentana-tolerancia,0),(mitadVentana-tolerancia,altoVentana),(255,255,0),3)#Linea media de la pantalla
	cv2.line(frame,(mitadVentana+tolerancia,0),(mitadVentana+tolerancia,altoVentana),(255,255,0),3)#Linea media de la pantalla

	for i in range(0,240,50): # Lineas delimitadoras (zonas de estudio)
		cv2.line(frame,(0,i),(320,i),(0,0,255),2)

def zona1(frame,mascara):
	global promAntX

	promX = (mascara[:96,0,0].sum())//96
	promY = (mascara[:96,0,1].sum())//96

	if(promX > promAntX+5 or promX < promAntX-5):
		cv2.circle(frame,(promX,25),3,(0,10,254),-1)
		cv2.line(frame,(promX-tolerancia,0),(promX-tolerancia,50),(0,0,255),3)
		cv2.line(frame,(promX+tolerancia,0),(promX+tolerancia,50),(0,0,255),3)
		promAntX = promX
	else:
		cv2.circle(frame,(promAntX,25),3,(0,10,254),-1)
		cv2.line(frame,(promAntX-tolerancia,0),(promAntX-tolerancia,50),(0,0,255),3)
		cv2.line(frame,(promAntX+tolerancia,0),(promAntX+tolerancia,50),(0,0,255),3)

		if((promAntX-tolerancia)- (mitadVentana-tolerancia) < -23):
			movimientoMotores(20,30) # Mizq, Mder
			print("Aumentar motor Derecho")
		elif((promAntX-tolerancia)- (mitadVentana-tolerancia) > 23):
			movimientoMotores(30,20) # Mizq, Mder
			print("Aumentar motor Izquierdo")
		else:
			movimientoMotores(30,30) # Mizq, Mder
			print("esta centrado")

def mostrarVentanas(frame,canny,umbralizada):
	cv2.imshow("Video Color",frame)
	cv2.imshow("Video Canny",canny)
	#cv2.imshow("Video umbralizado",umbralizada)

def movimientoMotores(vD,vI):

	GPIO.output(motorA1, False)
	GPIO.output(motorA2, True)

	GPIO.output(motorB1, True)
	GPIO.output(motorB2, False)

	GPIO.output(standBy, True)

	intensidadA.ChangeDutyCycle(vD) # Motor Derecho
	intensidadB.ChangeDutyCycle(vI) # Motor Izquierdo

def programaPrincipal():
	contador = []
	while True:
		tiempoInicial = time.time()

		frame = grabarVideo()
		canny,umbralizada = filtrar(frame)
		mascara = extraerLinea(canny)
		puntosImportantesVentana(frame)

		zona1(frame,mascara)

		mostrarVentanas(frame,canny,0)

		tiempoFinal = time.time()
		contador.append(tiempoFinal-tiempoInicial)

		if(cv2.waitKey(1) & 0xFF == ord('q')):
			suma = 0
			final = 0
			for idx,i in enumerate(contador):
				suma += i
				final = idx
			print("{0} medicion(es) y el promedio es: {1}".format(final,suma/final))
			break

if __name__ == '__main__':
	programaPrincipal()

	captura.release()
	cv2.destroyAllWindows()


import os
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.applications import VGG19
from tensorflow.keras.layers import MaxPooling2D

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Settings
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
CONTENT_IMG_PATH = "./input/cat.jpg"
STYLE_IMG_PATH = "./input/starry_night.jpg"
GENERATED_IMG_PATH = "./output/generated_img.jpg"
IMG_SIZE = (400, 300)
NUM_COLOR_CHANNELS = 3
ALPHA = 10
BETA = 40
NOISE_RATIO = 0.6
CONTENT_LAYER_INDEX = 13
STYLE_LAYER_INDICES = [1, 4, 7, 12, 17]
STYLE_LAYER_COEFFICIENTS = [0.2, 0.2, 0.2, 0.2, 0.2]
NUM_ITERATIONS = 500
LEARNING_RATE = 2
VGG_IMAGENET_MEANS = np.array([103.939, 116.779, 123.68]).reshape((1, 1, 3)) # In blue-green-red order
LOG_GRAPH = False

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Functions
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
def create_output_dir():
    """Create output dir if it does not exist."""
    cwd = os.getcwd()
    output_dir_path = os.path.join(cwd, "output")
    if not os.path.exists(output_dir_path):
        os.makedirs(output_dir_path)

def load_img(path, size, color_means):
    """Load image from path, preprocess it, and return the image."""
    img = cv2.imread(path)
    img = cv2.resize(img, dsize=size, interpolation=cv2.INTER_CUBIC)
    img = img.astype("float32")
    img -= color_means
    img = np.expand_dims(img, axis=0)

    return img

def save_img(img, path, color_means):
    """Save image to path after postprocessing."""
    img += color_means
    img = np.clip(img, 0, 255)
    img = img.astype("uint8")
    cv2.imwrite(path, img)

def create_noisy_img(img, noise_ratio):
    """Add noise to img and return it."""
    noise = np.random.uniform(-20, 20, (img.shape[0], img.shape[1], img.shape[2], img.shape[3])).astype("float32")
    noisy_img = noise_ratio * noise + (1 - noise_ratio) * img

    return noisy_img

def create_output_tensors(input_variable, content_layer_index, style_layer_indices):
    """
    Create output tensors, using a pretrained Keras VGG19-model.
    Return tensors for content and style layers.
    """
    vgg_model = VGG19(weights="imagenet", include_top=False)
    layers = [l for l in vgg_model.layers]

    x = layers[1](input_variable)
    x_content_tensor = x
    x_style_tensors = []
    if 1 in style_layer_indices:
        x_style_tensors.append(x)

    for i in range(2, len(layers)):
        # Use layers from vgg model, but swap max pooling layers for average pooling
        if type(layers[i]) == MaxPooling2D:
            x = tf.nn.avg_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        else:
            x = layers[i](x)

        # Store appropriate layer outputs
        if i == content_layer_index:
            x_content_tensor = x
        if i in style_layer_indices:
            x_style_tensors.append(x)

    return x_content_tensor, x_style_tensors

def content_cost(a_c, a_g):
    """Return a tensor representing the content cost."""
    _, n_h, n_w, n_c = a_c.shape

    return (1/(4 * n_h * n_w * n_c)) * tf.reduce_sum(tf.square(tf.subtract(a_c, a_g)))

def style_cost(a_s_layers, a_g_layers, style_layer_coefficients):
    """Return a tensor representing the style cost."""
    style_cost = 0
    for i in range(len(a_s_layers)):
        # Compute gram matrix for the activations of the style image
        a_s = a_s_layers[i]
        _, n_h, n_w, n_c = a_s.shape
        a_s_unrolled = tf.reshape(tf.transpose(a_s), [n_c, n_h*n_w])
        a_s_gram = tf.matmul(a_s_unrolled, tf.transpose(a_s_unrolled))

        # Compute gram matrix for the activations of the generated image
        a_g = a_g_layers[i]
        a_g_unrolled = tf.reshape(tf.transpose(a_g), [n_c, n_h*n_w])
        a_g_gram = tf.matmul(a_g_unrolled, tf.transpose(a_g_unrolled))

        # Compute style cost for the current layer
        style_cost_layer = (1/(4 * n_c**2 * (n_w* n_h)**2)) * tf.reduce_sum(tf.square(tf.subtract(a_s_gram, a_g_gram)))

        style_cost += style_cost_layer * style_layer_coefficients[i]

    return style_cost

def total_cost(content_cost, style_cost, alpha, beta):
    """Return a tensor representing the total cost."""
    return alpha * content_cost + beta * style_cost

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Model
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
create_output_dir()

# Load, resize, and preprocess content and style images
content_img = load_img(CONTENT_IMG_PATH, IMG_SIZE, VGG_IMAGENET_MEANS)
style_img = load_img(STYLE_IMG_PATH, IMG_SIZE, VGG_IMAGENET_MEANS)

# Create initial generated image, this is the starting point for the optimization process
generated_img_init = create_noisy_img(content_img, NOISE_RATIO)

# Create tensorflow variable that will be used as an input to the network.
# This variable will later be assigned generated_img_init and trained.
input_var = tf.Variable(content_img, dtype=tf.float32, expected_shape=(None, None, None, NUM_COLOR_CHANNELS), name="input_var")

# Create output tensors for the activations of the content and style layers,
# using a Keras VGG19-model pretrained on the ImageNet dataset.
x_content, x_styles = create_output_tensors(input_var, CONTENT_LAYER_INDEX, STYLE_LAYER_INDICES)

optimizer = tf.train.AdamOptimizer(LEARNING_RATE)

# Use the Keras session instead of creating a new one
with K.get_session() as sess:
    sess.run(tf.variables_initializer([input_var]))

    # Extract the layer activations for content and style images
    a_content = sess.run(x_content, feed_dict={K.learning_phase(): 0})
    sess.run(input_var.assign(style_img))
    a_styles = sess.run(x_styles, feed_dict={K.learning_phase(): 0})

    # Define the cost function
    J_content = content_cost(a_content, x_content)
    J_style = style_cost(a_styles, x_styles, STYLE_LAYER_COEFFICIENTS)
    J_total = total_cost(J_content, J_style, ALPHA, BETA)

    # Log the graph. To display use "tensorboard --logdir=log".
    if LOG_GRAPH:
        writer = tf.summary.FileWriter("log", sess.graph)
        writer.close()

    # Assign the generated random initial image as input
    sess.run(input_var.assign(generated_img_init))

    # Create the training operation
    train_op = optimizer.minimize(J_total, var_list=[input_var])
    sess.run(tf.variables_initializer(optimizer.variables()))

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
    # Train the generated image
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
    for i in range(NUM_ITERATIONS):
        sess.run(train_op)

        if (i%20) == 0:
            print(
                "Iteration: " + str(i) +
                ", Content cost: " + "{:.2e}".format(sess.run(J_content)) +
                ", Style cost: " + "{:.2e}".format(sess.run(J_style)) +
                ", Total cost: " + "{:.2e}".format(sess.run(J_total))
            )

            # Save the generated image
            generated_img = sess.run(input_var)[0]
            save_img(generated_img, GENERATED_IMG_PATH, VGG_IMAGENET_MEANS)

    # Save the generated image
    generated_img = sess.run(input_var)[0]
    save_img(generated_img, GENERATED_IMG_PATH, VGG_IMAGENET_MEANS)


# -*- coding: utf-8 -*-
# 02/28/2022

import argparse
import os
import re
import json
import sys
import copy
from collections import OrderedDict
import opcodes_def
import cpx_opcode_def

def _read_json(filename=None):
    json_object = None
    crashfile = open(filename, 'r')
    json_object = json.load(crashfile)

#    if 'HardwareErrorLog' in json_object:
#        json_object = json_object.get("HardwareErrorLog", None)
#    else:
#        json_object = None
    crashfile.close()
    return json_object

SAD_RESULT = {
    'HOM': 0,
    'MMIO': 1,
    'CFG': 2,
    'MMIO Partial Read': 3,
    'IO': 4,
    'Intel Reserved': 5,
    'SPC': 6,
    'Intel Reserved': 7
}

def covert_cpx_tor_dump(tor_info, dw):
    torkey_list = list(tor_info.keys())
    if "CPU" in torkey_list and "CHA" in torkey_list:
        torkey_list.remove("CPU")
        torkey_list.remove("CHA")
    else:
        return False

    for tor_key in torkey_list:
        if isinstance(tor_info[tor_key], str):
            tor_info[tor_key] = tor_info[tor_key].strip()
        if "Valid" == tor_key:
            dw[0] = dw[0] | (tor_info["Valid"] << 1 | tor_info["Valid"])
        elif "Retry" == tor_key:
            dw[0] = dw[0] | (tor_info["Retry"] << 2)
        elif "InPipe" == tor_key:
            dw[0] = dw[0] | (tor_info["InPipe"] << 3)
        elif "TOR" == tor_key:
            dw[0] = (tor_info["TOR"] & 0x1F) << 9
        elif "SadResult" == tor_key:
            dw[1] = dw[1] | ((cpx_opcode_def.Sad_Result_Decoding[tor_info["SadResult"]]) & 0x7) << 25
        elif "Target" == tor_key:
            tor_info["Target"] = tor_info["Target"].replace("UPI", "KTI")
            try:
                dw[1] = dw[1] | (int(tor_info["Target"], 16) & 0x1f) << 20
            except:
                dw[1] = dw[1] | (
                        (cpx_opcode_def.Targeted_Port_Decoding[tor_info["Target"]]) & 0x1f) << 20
        elif "Core" == tor_key:
            tor_info["Core"] = tor_info["Core"].lower()
            if tor_info["Core"].startswith("core"):
                id = int(tor_info["Core"][4:])
            else:
                id = cpx_opcode_def.CoreId_decoding[tor_info["Core"].upper()]
            dw[0] = dw[0] | (id & 0x3f) << 14
        elif "Thread" == tor_key:
            dw[0] = dw[0] | (tor_info["Thread"] & 0x1) << 20
        elif "OpCode" == tor_key:
            try:
                dw[0] = dw[0] | (int(tor_info["OpCode"], 16) & 0x7ff) << 21
            except:
                opcode = tor_info["OpCode"].strip()
                if opcode == "PerfData":
                    opcode = "PrefData"
                elif opcode == "RdData":
                    opcode = "KRdData"
                elif opcode == "Interrupt Priority Update":
                    opcode = "IntPriUp"
                dw[0] = dw[0] | (cpx_opcode_def.TOR_OPCODES[opcode] & 0x7ff) << 21
        elif "CacheState" == tor_key:
            try:
                dw[1] = dw[1] | (int(tor_info["CacheState"], 16) & 0xf) << 28
            except:
                dw[1] = dw[1] | (cpx_opcode_def.CatchState_Decoding[tor_info["CacheState"]] & 0xf) << 28
        elif "SystemAddres" == tor_key or "SystemAddress" == tor_key:
            addr = int(tor_info[tor_key].split(" ")[0], 16)
            dw[1] = dw[1] | (addr & 0x3FFF)
            dw[2] = dw[2] | (addr >> 14)

        elif "Fsm" == tor_key:
            if isinstance(tor_info["Fsm"], str):
                try:
                    dw[1] = dw[1] | (int(tor_info["Fsm"], 16) & 0x3f) << 14
                except:
                    print("ERROR: Unknown FSM State %s"%tor_info["Fsm"])
            elif isinstance(tor_info["Fsm"], int):
                dw[1] = dw[1] | (tor_info["Fsm"] & 0x3f) << 14
        else:
            print("ERROR: Unknown tor key %s" % tor_key)
    return True

def covert_tor_dump(tor_info, tordump0, tordump1, tordump2):
    torkey_list = list(tor_info.keys())
    if "CPU" in torkey_list and "CHA" in torkey_list and "TOR" in torkey_list:
        torkey_list.remove("CPU")
        torkey_list.remove("CHA")
        torkey_list.remove("TOR")
    else:
        return False

    for tor_key in torkey_list:
        if "Valid" == tor_key:
            tordump0[0] = tordump0[0] | (tor_info["Valid"] << 2)
        elif "Retry" == tor_key:
            tordump0[0] = tordump0[0] | (tor_info["Retry"] << 5)
        elif "InPipe" == tor_key:
            tordump0[0] = tordump0[0] | (tor_info["InPipe"] << 7)
        elif "Thread" == tor_key:
            tordump0[1] = (tor_info["Thread"] & 0x7) << 8
        elif "SadResult" == tor_key:
            tordump0[2] = tordump0[2] | ((SAD_RESULT[tor_info["SadResult"]]) & 0x7) << 18
        elif "Target" == tor_key:
            tor_info["Target"] = tor_info["Target"].replace("UPI", "KTI")
            tordump0[2] = tordump0[2] | (
                        (opcodes_def.inverseDict(opcodes_def.TARGET_LOC_PORT_BY_VAL)[tor_info["Target"]]) & 0x1f) << 7
        elif "Core" == tor_key:
            id = 0
            if tor_info["Core"].lower().startswith("core"):
                id = int(tor_info["Core"][4:])
            elif tor_info["Core"].lower().startswith("upi"):  # TODO "UPI" ?
                id = int(tor_info["Core"][3:]) + 54
            elif tor_info["Core"].lower().startswith("pcie"):  # TODO "IIO" or "PCIE"?
                id = (~(int(tor_info["Core"][4:]) & 0x3) & 0x3) + 60
            else:
                print("ERROR: Unknown Core value %s"%tor_info["Core"])
                raise
            tordump0[3] = tordump0[3] | (id & 0x3f) << 21
        elif "OpCode" == tor_key:
            try:
                tordump0[4] = tordump0[4] | (int(tor_info["OpCode"], 16) & 0x7ff) << 17
            except:
                opcode = tor_info["OpCode"].strip()
                if opcode == "PerfData":
                    opcode = "PrefData"
                elif opcode == "RdData":
                    opcode = "KRdData"
                tordump0[4] = tordump0[4] | (opcodes_def.TOR_OPCODES[opcode] & 0x7ff) << 17
        elif "CacheState" == tor_key:
            try:
                tordump0[7] = tordump0[7] | (int(tor_info["CacheState"], 16) & 0xf) << 3
            except:
                tordump0[7] = tordump0[7] | (opcodes_def.CACHE_STATE[tor_info["CacheState"]] & 0xf) << 3
        elif "SystemAddres" == tor_key or "SystemAddress" == tor_key:
            addr = int(tor_info[tor_key].split(" ")[0], 16)
            tordump1[0] = tordump1[0] | (addr & 0x01c0) << (29 - 6)
            tordump1[1] = tordump1[1] | (addr & 0x0E0000) << (29 - 17)
            tordump1[2] = tordump1[2] | (addr & 0x070000000) << (29 - 28)
            tordump1[3] = tordump1[3] | (addr & 0x038000000000) >> (39 - 29)
            tordump1[4] = tordump1[4] | (addr & 0x0c000000000000) >> (50 - 29)
            tordump2[0] = tordump2[0] | (addr & 0x1fe00) >> 9
            tordump2[1] = tordump2[1] | (addr & 0x0ff00000) >> 20
            tordump2[2] = tordump2[2] | (addr & 0x07f80000000) >> 31
            tordump2[3] = tordump2[3] | (addr & 0x03fc0000000000) >> 42
        elif "Fsm" == tor_key:
            if isinstance(tor_info["Fsm"], str):
                try:
                    tordump1[7] = tordump1[7] | (int(tor_info["Fsm"], 16) & 0x1f) << 1
                except:
                    tordump1[7] = tordump1[7] | (opcodes_def.ISMQ_FSM[tor_info["Fsm"]] & 0x1f) << 1
            elif isinstance(tor_info["Fsm"], int):
                tordump1[7] = tordump1[7] | (tor_info["Fsm"] & 0x1f) << 1
        else:
            print("ERROR: Unknown tor key %s" % tor_key)
    return True

def get_report_cpx_TOR_dump(Errorlist):
    tor_data = {}
    for error in Errorlist:
        if error.get("ErrorArch", None) == "TorDump":
            tor_info_list = error.get("TorDump Info", None)
            for tor_info in tor_info_list:
                DW = [0,0,0]
                if covert_cpx_tor_dump(tor_info, DW):
                    if tor_info["CPU"] not in tor_data.keys():
                        tor_data[tor_info["CPU"]] = OrderedDict()
                    if "cha" + str(tor_info["CHA"]) not in tor_data[tor_info["CPU"]].keys():
                        tor_data[tor_info["CPU"]]["cha" + str(tor_info["CHA"])] = OrderedDict()
                    tor_data[tor_info["CPU"]]["cha" + str(tor_info["CHA"])][
                        "index" + str(tor_info["TOR"])] = OrderedDict()
                    for i in range(3):
                        tor_data[tor_info["CPU"]]["cha" + str(tor_info["CHA"])]["index" + str(tor_info["TOR"])][
                            "subindex" + str(i)] = hex(DW[i])
                else:
                    print("ERROR: tor dump decode failed\n", tor_info)
    return tor_data

def get_report_TOR_dump(Errorlist):

    tor_data = {}
    for error in Errorlist:
        if error.get("ErrorArch", None) == "TorDump":
            tor_info_list = error.get("TorDump Info", None)
            for tor_info in tor_info_list:
                tordump0 = [0, 0, 0, 0, 0, 0, 0, 0]
                tordump1 = [0, 0, 0, 0, 0, 0, 0, 0]
                tordump2 = [0, 0, 0, 0, 0, 0, 0, 0]
                if covert_tor_dump(tor_info, tordump0, tordump1, tordump2):
                    if tor_info["CPU"] not in tor_data.keys():
                        tor_data[tor_info["CPU"]] = OrderedDict()
                    if "cha" + str(tor_info["CHA"]) not in tor_data[tor_info["CPU"]].keys():
                        tor_data[tor_info["CPU"]]["cha" + str(tor_info["CHA"])] = OrderedDict()
                    tor_data[tor_info["CPU"]]["cha" + str(tor_info["CHA"])][
                        "index" + str(tor_info["TOR"])] = OrderedDict()
                    for i in range(8):
                        tor_data[tor_info["CPU"]]["cha" + str(tor_info["CHA"])]["index" + str(tor_info["TOR"])][
                            "subindex" + str(i)] = hex(tordump2[i] << 64 | tordump1[i] << 32 | tordump0[i])
                else:
                    print("ERROR: tor dump decode failed\n", tor_info)
    return tor_data

imc_chn_map = {
    "0x0": ["10","3"],
    "0x1": ["10","7"],
    "0x2": ["11","3"],
    "0x3": ["12","3"],
    "0x4": ["12","7"],
    "0x5": ["13","3"]
}
cpx_reg_map = {
    "MSR_MCG_CONTAIN": "cpu%c/uncore/RDIAMSR_0x178",
    "IA32_MCG_STATUS": "cpu%c/uncore/RDIAMSR_0x17A",
    "IA32_MCG_CAP": "cpu%c/uncore/RDIAMSR_0x179",
    "MCA_ERROR_CONTROL": "cpu%c/uncore/RDIAMSR_0x17F",
    "IA32_MCi_CTL": "cpu%c/MCA/uncore/MC%b/mc%b_ctl",
    "IA32_MCi_CTL2": "cpu%c/MCA/uncore/MC%b/mc%b_ctl2",
    "IA32_MCi_STATUS": "cpu%c/MCA/uncore/MC%b/mc%b_status",
    "IA32_MCi_ADDR": "cpu%c/MCA/uncore/MC%b/mc%b_addr",
    "IA32_MCi_MISC": "cpu%c/MCA/uncore/MC%b/mc%b_misc",
    "IA32_MCc_CTL": "cpu%c/MCA/uncore/CBO%n/cbo%n_ctl",
    "IA32_MCc_CTL2": "cpu%c/MCA/uncore/CBO%n/cbo%n_ctl2",
    "IA32_MCc_STATUS": "cpu%c/MCA/uncore/CBO%n/cbo%n_status",
    "IA32_MCc_ADDR": "cpu%c/MCA/uncore/CBO%n/cbo%n_addr",
    "IA32_MCc_MISC": "cpu%c/MCA/uncore/CBO%n/cbo%n_misc",
    "IA32_MCx_CTL": "cpu%c/MCA/core%d/thread0/MC%b/mc%b_ctl",
    "IA32_MCx_CTL2": "cpu%c/MCA/core%d/thread0/MC%b/mc%b_ctl2",
    "IA32_MCx_STATUS": "cpu%c/MCA/core%d/thread0/MC%b/mc%b_status",
    "IA32_MCx_ADDR": "cpu%c/MCA/core%d/thread0/MC%b/mc%b_addr",
    "IA32_MCx_MISC": "cpu%c/MCA/core%d/thread0/MC%b/mc%b_misc",
    "THREAD_SMI_ERR_SRC": "cpu%c/core%d/thread0/RDIAMSR_0x158",
    "CORE_SMI_ERR_SRC": "cpu%c/core%d/RDIAMSR_0x17C",
    "UNCORE_SMI_ERR_SRC": "cpu%c/uncore/RDIAMSR_0x17E",
    "IerrLoggingReg": "cpu%c/uncore/B00_D08_F0_0xA4",
    "McerrLoggingReg": "cpu%c/uncore/B00_D08_F0_0xA8",
    "EMCA_CORE_CSMI_LOG": "cpu%c/uncore/B00_D08_F0_0xB0",
    "EMCA_CORE_CSMI_LOG1": "cpu%c/uncore/B00_D08_F0_0xB4",
    "EMCA_CORE_MSMI_LOG": "cpu%c/uncore/B00_D08_F0_0xB8",
    "EMCA_CORE_MSMI_LOG1": "cpu%c/uncore/B00_D08_F0_0xBC",
    "MCA_ERR_SRC_LOG": "cpu%c/uncore/B01_D30_F2_0xEC",

    "RETRY_RD_CH_NUM": "0",
    "RETRY_RD_ERR_LOG": "cpu%c/uncore/B02_D%e_F%f_0x154",
    "RETRY_RD_ERR_LOG_MISC": "cpu%c/uncore/B02_D%e_F%f_0x148",
    "RETRY_RD_ERR_LOG_PARITY": "cpu%c/uncore/B02_D%e_F%f_0x150",
    "RETRY_RD_ERR_LOG_ADDRESS1": "cpu%c/uncore/B02_D%e_F%f_0x15C",
    "RETRY_RD_ERR_LOG_ADDRESS2": "cpu%c/uncore/B02_D%e_F%f_0x114",
    "Correrrorstatus": "cpu%c/uncore/B02_D%e_F%f_0x134",
    "RETRY_RD_ERR_LOG_ADDRESS3N0": None,
    "RETRY_RD_ERR_LOG_ADDRESS3N1": None,

    "PCU_FIRST_IERR_TSC_LO": "cpu%c/uncore/B01_D30_F4_0xF0",
    "PCU_FIRST_IERR_TSC_HI": "cpu%c/uncore/B01_D30_F4_0xF4",
    "PCU_FIRST_MCERR_TSC_LO": "cpu%c/uncore/B01_D30_F4_0xF8",
    "PCU_FIRST_MCERR_TSC_HI": "cpu%c/uncore/B01_D30_F4_0xFC",

    "UNCORE_FIVR_ERR_LOG": "cpu%c/uncore/B01_D30_F2_0x84",
    "CORE_FIVR_ERR_LOG_0": "cpu%c/uncore/B31_D30_F2_0x80",
    "CORE_FIVR_ERR_LOG_1": None
}

reg_map = {
    "MSR_MCG_CONTAIN":      ["cpu%c/uncore/RDIAMSR_0x178"],
    "IA32_MCG_STATUS":      ["cpu%c/uncore/RDIAMSR_0x17A"],
    "IA32_MCG_CAP":         ["cpu%c/uncore/RDIAMSR_0x179"],
    "MCA_ERROR_CONTROL":    ["cpu%c/uncore/RDIAMSR_0x17F"],
    "IA32_MCi_CTL":         ["cpu%c/MCA/uncore/MC%b/mc%b_ctl"],
    "IA32_MCi_CTL2":        ["cpu%c/MCA/uncore/MC%b/mc%b_ctl2"],
    "IA32_MCi_STATUS":      ["cpu%c/MCA/uncore/MC%b/mc%b_status"],
    "IA32_MCi_ADDR":        ["cpu%c/MCA/uncore/MC%b/mc%b_addr"],
    "IA32_MCi_MISC":        ["cpu%c/MCA/uncore/MC%b/mc%b_misc"],
    "IA32_MCc_CTL":         ["cpu%c/MCA/uncore/CBO%n/cbo%n_ctl"],
    "IA32_MCc_CTL2":        ["cpu%c/MCA/uncore/CBO%n/cbo%n_ctl2"],
    "IA32_MCc_STATUS":      ["cpu%c/MCA/uncore/CBO%n/cbo%n_status"],
    "IA32_MCc_ADDR":        ["cpu%c/MCA/uncore/CBO%n/cbo%n_addr"],
    "IA32_MCc_MISC":        ["cpu%c/MCA/uncore/CBO%n/cbo%n_misc"],
    "IA32_MCx_CTL":         ["cpu%c/MCA/core%d/thread%t/MC%b/mc%b_ctl"],
    "IA32_MCx_CTL2":        ["cpu%c/MCA/core%d/thread%t/MC%b/mc%b_ctl2"],
    "IA32_MCx_STATUS":      ["cpu%c/MCA/core%d/thread%t/MC%b/mc%b_status"],
    "IA32_MCx_ADDR":        ["cpu%c/MCA/core%d/thread%t/MC%b/mc%b_addr"],
    "IA32_MCx_MISC":        ["cpu%c/MCA/core%d/thread%t/MC%b/mc%b_misc"],
    "THREAD_SMI_ERR_SRC":   ["cpu%c/core%d/thread%t/RDIAMSR_0x158"],
    "CORE_SMI_ERR_SRC":     ["cpu%c/core%d/RDIAMSR_0x17C"],
    "UNCORE_SMI_ERR_SRC":   ["cpu%c/uncore/RDIAMSR_0x17E"],
    "IerrLoggingReg":       ["cpu%c/uncore/B30_D00_F0_0xA4"],
    "McerrLoggingReg":      ["cpu%c/uncore/B30_D00_F0_0xA8"],
    "EMCA_CORE_CSMI_LOG":   ["cpu%c/uncore/B30_D00_F0_0xB0"],
    "EMCA_CORE_CSMI_LOG1":  ["cpu%c/uncore/B30_D00_F0_0xB4"],
    "EMCA_CORE_MSMI_LOG":   ["cpu%c/uncore/B30_D00_F0_0xB8"],
    "EMCA_CORE_MSMI_LOG1":  ["cpu%c/uncore/B30_D00_F0_0xBC"],
    "MCA_ERR_SRC_LOG":      ["cpu%c/uncore/B31_D30_F2_0xEC"],
    "RETRY_RD_CH_NUM":      [0,0x4000],
    "RETRY_RD_SET2_CH_NUM": [0,0x4000],
    "RETRY_RD_ERR_LOG":             ["cpu%c/uncore/B30_D00_F1_%o", 0x22C60],
    "RETRY_RD_ERR_LOG_MISC":        ["cpu%c/uncore/B30_D00_F1_%o", 0x22C54],
    "RETRY_RD_ERR_LOG_PARITY":      ["cpu%c/uncore/B30_D00_F1_%o", 0x22C5C],
    "RETRY_RD_ERR_LOG_ADDRESS1":    ["cpu%c/uncore/B30_D00_F1_%o", 0x22C58],
    "RETRY_RD_ERR_LOG_ADDRESS2":    ["cpu%c/uncore/B30_D00_F1_%o", 0x22C28],
    "RETRY_RD_ERR_LOG_ADDRESS3N0":  ["cpu%c/uncore/B30_D00_F1_%o", 0x20ED8],
    "RETRY_RD_ERR_LOG_ADDRESS3N1":  ["cpu%c/uncore/B30_D00_F1_%o", 0x20EDC],
    "RETRY_RD_ERR_SET2_LOG":        ["cpu%c/uncore/B30_D00_F1_%2o", 0x22E54],
    "RETRY_RD_ERR_SET2_LOG_MISC":   ["cpu%c/uncore/B30_D00_F1_%2o", 0x22E60],
    "RETRY_RD_ERR_SET2_LOG_PARITY": ["cpu%c/uncore/B30_D00_F1_%2o", 0x22E64],
    "RETRY_RD_ERR_SET2_LOG_ADDRESS1":   ["cpu%c/uncore/B30_D00_F1_%2o", 0x22E58],
    "RETRY_RD_ERR_SET2_LOG_ADDRESS2":   ["cpu%c/uncore/B30_D00_F1_%2o", 0x22E5C],
    "RETRY_RD_ERR_SET2_LOG_ADDRESS3N0": ["cpu%c/uncore/B30_D00_F1_%2o", 0x20EE0],
    "RETRY_RD_ERR_SET2_LOG_ADDRESS3N1": ["cpu%c/uncore/B30_D00_F1_%2o", 0x20EE4],
    "Correrrorstatus":                  ["cpu%c/uncore/B30_D00_F1_%o", 0x22C50],
    "PCU_FIRST_IERR_TSC_LO":            ["cpu%c/uncore/B31_D30_F4_0xF0"],
    "PCU_FIRST_IERR_TSC_HI":            ["cpu%c/uncore/B31_D30_F4_0xF4"],
    "PCU_FIRST_MCERR_TSC_LO":           ["cpu%c/uncore/B31_D30_F4_0xF8"],
    "PCU_FIRST_MCERR_TSC_HI":           ["cpu%c/uncore/B31_D30_F4_0xFC"],
    "UNCORE_FIVR_ERR_LOG":              ["cpu%c/uncore/B31_D30_F2_0x84"],
    "CORE_FIVR_ERR_LOG_0":              ["cpu%c/uncore/B31_D30_F2_0xC0"],
    "CORE_FIVR_ERR_LOG_1":              ["cpu%c/uncore/B31_D30_F2_0xC4"]
}

def get_dic_key(key_list, value):
    if len(key_list) >1:
        return {key_list[0]: get_dic_key(key_list[1:], value)}
    else:
        return {key_list[0]: value}

def cpx_reg_mapping(reg, value, cpuinfo):
    data_dic = {}
    if "IA32_MCi" in reg:

        if int(cpuinfo["bank_id"]) < 4:
            reg = reg.replace("MCi", "MCx")
        elif cpuinfo["cha_id"] is not None:
            reg = reg.replace("MCi", "MCc")

    if cpx_reg_map[reg]:
        # print(reg, reg_map[reg])
        if cpx_reg_map[reg] != "0":
            if '/' in cpx_reg_map[reg]:
                key_list = cpx_reg_map[reg].split("/")
                for i in range(0, len(key_list)):
                    if "%c" in key_list[i]:
                        key_list[i] = key_list[i].replace("%c", str(int(cpuinfo["socket_id"])))
                    elif "%d" in key_list[i]:
                        if cpuinfo["core_id"] == "uncore":
                            cpuinfo["core_id"] = "0"
                        key_list[i] = key_list[i].replace("%d", str(int(cpuinfo["core_id"])))
                    elif "%b" in key_list[i]:
                        key_list[i] = key_list[i].replace("%b", str(int(cpuinfo["bank_id"])))
                    elif "%n" in key_list[i]:
                        key_list[i] = key_list[i].replace("%n", str(int(cpuinfo["cha_id"])))
                    elif "%f" in key_list[i]:
                        key_list[i] = key_list[i].replace("%e", imc_chn_map[cpx_reg_map["RETRY_RD_CH_NUM"]][0])
                        key_list[i] = key_list[i].replace("%f", imc_chn_map[cpx_reg_map["RETRY_RD_CH_NUM"]][1])

                data_dic = get_dic_key(key_list, value)
                # print(data_dic)
        else:
            cpx_reg_map[reg] = hex(int(value, 16))

    return data_dic

def reg_mapping(reg, value, cpuinfo):
    data_dic = {}
    if "IA32_MCi" in reg:

        if int(cpuinfo["bank_id"]) < 4:
            reg = reg.replace("MCi","MCx")
        elif cpuinfo["cha_id"] is not None:
            reg = reg.replace("MCi", "MCc")

    if reg_map[reg]:
        #print(reg, reg_map[reg])
        if reg_map[reg][0]:
            if '/' in reg_map[reg][0]:
                key_list = reg_map[reg][0].split("/")
                for i in range(0,len(key_list)):
                    if "%c" in key_list[i]:
                        key_list[i] = key_list[i].replace("%c", str(int(cpuinfo["socket_id"])))
                    elif "%d" in key_list[i]:
                        if cpuinfo["core_id"]  == "uncore":
                            cpuinfo["core_id"] = "0"
                        key_list[i] = key_list[i].replace("%d", str(int(cpuinfo["core_id"])))
                    elif "%b" in key_list[i]:
                        key_list[i] = key_list[i].replace("%b", str(int(cpuinfo["bank_id"])))
                    elif "%n" in key_list[i]:
                        key_list[i] = key_list[i].replace("%n", str(int(cpuinfo["cha_id"])))
                    elif "%t" in key_list[i]:
                        if cpuinfo["thread_id"] == 'NA' or cpuinfo["thread_id"] == 'None':
                            print("WARNING: No thread id. Assume 0...", key_list)
                            cpuinfo["thread_id"] = '0'
                        key_list[i] = key_list[i].replace("%t", cpuinfo["thread_id"])
                    elif "%o" in key_list[i]:
                        key_list[i] = key_list[i].replace("%o", hex(reg_map[reg][1]+int(reg_map["RETRY_RD_CH_NUM"][0], 16)*reg_map["RETRY_RD_CH_NUM"][1]))
                    elif "%2o" in key_list[i]:
                        key_list[i] = key_list[i].replace("%2o", hex(reg_map[reg][1]+int(reg_map["RETRY_RD_SET2_CH_NUM"][0], 16)*reg_map["RETRY_RD_SET2_CH_NUM"][1]))

                data_dic = get_dic_key(key_list, value)
                #print(data_dic)
        else:
            reg_map[reg][0] = hex(int(value,16))

    return data_dic

def merge_dic(dic1, dic2):
    for key in dic2.keys():
        if key in dic1.keys():
            if isinstance(dic1[key], dict) and isinstance(dic2[key], dict):
                merge_dic(dic1[key], dic2[key])
            else:
                if(dic1[key]!=dic2[key]):
                    if dic1[key] == "N/A":
                        dic1[key] = dic2[key]
                    else:
                        print("WARNING: Duplicate regs with different value: ", key, dic1[key], dic2[key])
        else:
            dic1.update({key:dic2[key]})
    return dic1

#def convertanalyrpt(rptfile, raw_src="ALL", raw_time="ALL"):
def convertanalyrpt(jsonobj, raw_src="ALL", raw_time="ALL", cputype = 'icx'):
#    try:
#        jsonobj = _read_json(rptfile)
#    except:
#        print("can't find the erroranalyreport.")
#        return

    if jsonobj is None:
        print("ERROR: Unknown format erroranalyreport.")
        return None
    jsonobj = jsonobj.get("HardwareErrorLog", [])
    all_log_list = []
    crashdump_processor = {}
    for log in jsonobj:
        crashdump_all = {}
        crashdump_processor = {}  # OrderedDict()
        dump_time = log.get("Time", None)
        crashdump_all["Time"] = dump_time
        dump_src = log.get("Collect", None)
        crashdump_all["Source"] = dump_src
        crashdump_all["LogIndex"] = log.get("HardwareErrorLogNumber", None)
        valid_flag = log.get("CollectIntegrity", None)
        if raw_src != "ALL" and dump_src != raw_src:
            continue
        if raw_time != "ALL" and raw_time != dump_time:
            continue
        if 1: #(valid_flag == "Validate(0x00)"):
            error_list = log.get("ErrorEntry", None)

            for err_evt in error_list:
                cpu_info = {}
                if "ErrorArch" in err_evt.keys():
                    #print(err_evt["ErrorArch"])
                    if err_evt["ErrorArch"].startswith("CSR"):
                        continue
                    cpu_info["socket_id"] = str(err_evt.get("CPU", None))

                    #print("socket_id", cpu_info["socket_id"])
                    cpu_info["core_id"] = str(err_evt.get("Core", None))
                    #print(cpu_info["core_id"],"core_id")
                    cpu_info["thread_id"] = str(err_evt.get("Thread", None))
                    #print(err_evt.get("Module", None))
                    bank_str = err_evt.get("Module", None)
                    cpu_info["cha_id"] = (err_evt.get("CHAId", None))
                    bank_check = None
                    if bank_str:
                        bank_check = re.match(r'Bank(\d+)\([\w\d_ ]+\)', bank_str)
                        if bank_check:
                            cpu_info["bank_id"] = str(bank_check.group(1))
                            #print(cpu_info["bank_id"],"bank_id",bank_str)
                        else:
                            print("WARNING: No bank id. %s"%bank_str)

                    if not cpu_info["cha_id"] and cputype == 'icx' and bank_check:
                        if "REGISTER DUMP" in err_evt:
                            mcerrreg = err_evt["REGISTER DUMP"].get("McerrLoggingReg", None)
                            if mcerrreg:
                                srcid = int(mcerrreg,16) & 0xff
                                if srcid>=0x40 and srcid <= 0x67:
                                    if (srcid-0x40)%3+9 == int(bank_check.group(1)):
                                        cpu_info["cha_id"] = str(srcid - 0x40)
                    if "REGISTER DUMP" in err_evt.keys():
                        for reg, value in err_evt["REGISTER DUMP"].items():
                            #print(reg, value)
                            if cputype == 'icx':
                                temp_dic = reg_mapping(reg, value, cpu_info)
                            elif cputype == 'cpx':
                                temp_dic = cpx_reg_mapping(reg, value, cpu_info)
                            #print(temp_dic)
                            crashdump_processor = merge_dic(crashdump_processor, temp_dic)

                            pass
            #crashdump_processor.update()
            if cputype == 'icx':
                tor_data = get_report_TOR_dump(error_list)
                ver_id = "1a001"
            elif cputype == 'cpx':
                tor_data = get_report_cpx_TOR_dump(error_list)
                ver_id = "34001"
            else:
                tor_data = {}
                print("Error: Unknown CPU TOR dump")

            for socketid in tor_data.keys():
                if crashdump_processor.get('cpu%d' % socketid, None) == None:
                    crashdump_processor['cpu%d' % socketid] = OrderedDict()
                crashdump_processor['cpu%d' % socketid]['TOR'] = copy.deepcopy(tor_data[socketid])
                crashdump_processor['cpu%d' % socketid]['TOR']["_version"] = "0x090"+ver_id
        if crashdump_processor:
            crashdump_all["PROCESSORS"] = copy.deepcopy(crashdump_processor)
            all_log_list.append(crashdump_all)

    return crashdump_processor, all_log_list

if __name__ == "__main__":
    jsonobj = _read_json(sys.argv[1])
    convertanalyrpt(jsonobj)


# this is editorial soln

from collections import deque
from sys import maxint

class Solution(object):
    def bfs_search(self, matrix, queue, min_dist):
        n, m = len(matrix), len(matrix[0])
        while queue:
            (x, y), d = queue.popleft()
            for i, j in ((x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)):
                if 0 <= i < n and 0 <= j < m and (d + 1) < min_dist[i][j]:
                    min_dist[i][j] = d + 1
                    queue.append(((i, j), d + 1))

    def updateMatrix(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[List[int]]
        """
        n, m = len(matrix), len(matrix[0])
        min_dist = [[0 for _ in xrange(m)] for _ in xrange(n)]
        queue = deque()
        for x in xrange(n):
            for y in xrange(m):
                if matrix[x][y] == 1:
                    min_dist[x][y] = maxint
                else:
                    queue.append(((x, y), 0))
        self.bfs_search(matrix, queue, min_dist)
        return min_dist


N = input()
miniL = maxiL = miniR = maxiR = 0

for i in range(3):
	if N[i] == "*":
		maxiL += 9
	else:
		miniL += int(N[i])
		maxiL += int(N[i])
	if N[i+3] == "*":
		maxiR += 9
	else:
		miniR += int(N[i+3])
		maxiR += int(N[i+3])

if  maxiL < miniR or maxiR < miniL:
	print("No")
else:
	print("Yes")

Town-2022/5-Счастливый билетик.py

5-Счастливый билетик.py


import numpy as np
import matplotlib.pyplot as plt
import random

def random_angle():
    return random.uniform(0, 2*np.pi)

def next_step(x_pos, y_pos):
    angle = random_angle()
    x_pos += np.cos(angle)
    y_pos += np.sin(angle)
    return (x_pos, y_pos)

colors = ['black', 'red', 'yellow', 'blue', 'orange', 'purple', 'cyan', 'brown', 'pink', 'green', 'grey', 'chocolate', 'navy', 'indigo', 'coral']

number_of_walks = 15

number_of_coin_flips = 10**5

for i in range(number_of_walks):
    x = []
    y = []
    x_pos = 0
    y_pos = 0
    for j in range(number_of_coin_flips + 1):
        (x_pos, y_pos) = next_step(x_pos, y_pos)
        x.append(x_pos)
        y.append(y_pos)
    plt.plot(x, y, lw=0.1, color=colors[i])
    plt.plot(x[-1], y[-1], 'o', ms=10, color=colors[i])

plt.savefig('plot1B.png', format="png", box_inches='tight', pad_inches=0.05)
plt.show()


#!/usr/bin/env python3
# coding=utf-8
"""

@athor:weifeng.guo
@data:2019/6/25 9:41
@filename:test_my_math

"""
import unittest
from chapter16 import my_math

class test_integer(unittest.TestCase):

    def test_integers(self):
        for x in range(-10, 10):
            for y in range(-10, 10):
                p = my_math.product(x, y)
                self.assertEqual(p, x * y, 'Integer multiplication failed')

    def test_floats(self):
        for x in range(-10, 10):
            for y in range(-10, 10):
                x = x /10
                y = y / 10
                p = my_math.product(x, y)
                self.assertEqual(p, x * y, 'Floats multiplication failed')

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


import graphene

class CreateAuctionInput(graphene.InputObjectType):

    meme = graphene.List(graphene.NonNull(graphene.ID))
    initial_price = graphene.Int(required = True)
    limit = graphene.Int()
    starts_at = graphene.DateTime(required = True)
    ends_at = graphene.DateTime(required = True)

class UpdateAuctionInput(graphene.InputObjectType):

    id = graphene.ID(required = True)
    initial_price = graphene.Int()
    limit = graphene.Int()
    starts_at = graphene.DateTime()
    ends_at = graphene.DateTime()

class DeleteAuctionInput(graphene.InputObjectType):

    id = graphene.ID(required = True)


import torch
import json
import numpy as np

import torch.utils.data

class DataSet(torch.utils.data.Dataset):
    def __init__(self, data_path: str, metadata_path: str, args):

        with open(metadata_path, 'r') as j:
            obj = json.load(j)
            self.normalize = obj['normalized']

            data_type = obj['data_type']
            data_variables = obj['variables']

            if args.forecast_channel not in data_variables:
                raise ValueError(
                    f'Provided channel variable was not found in the provided data, channel: {args.forecast_channel}'
                )

            self.min = obj['var_max_min_values'][args.forecast_channel]['min']
            self.max = obj['var_max_min_values'][args.forecast_channel]['max']

            self.variable_len = len(data_variables)

            self.y_channel = data_variables.index(args.forecast_channel)
            self.x_channel = []

            # Index of forecast channel in x_channel array
            self.channel_y_idx_in_x = None

            # Collects all used channel idxes in one array
            for c in args.channel:
                self.x_channel.append(data_variables.index(c))

                # If forecast channel abbreviation is the same as channel abbreviation the its idx is saved
                if c == args.forecast_channel:
                    self.channel_y_idx_in_x = len(self.x_channel) - 1

            self.rollout = args.rollout
            self.delta = args.delta
            self.seq_cut = args.delta * args.rollout

            self.data = np.memmap(filename=data_path, dtype=data_type, mode='r', shape=tuple(obj['shape']))

        with open(args.extra_data, 'r') as j:
            obj = json.load(j)

            lon_idx = obj['variables'].index('lon')
            lat_idx = obj['variables'].index('lat')
            self.lon = obj['data'][lon_idx]
            self.lat = obj['data'][lat_idx]

    def __len__(self):
        return len(self.data) - self.seq_cut

    def __getitem__(self, idx):
        if self.variable_len == len(self.x_channel):
            x = torch.FloatTensor(self.data[idx].copy())
        else:
            x = torch.FloatTensor(self.data[idx, self.x_channel].copy())

        y_end_idx = idx + self.delta + self.rollout * self.delta
        y_idx_arange = np.arange(idx + self.delta, y_end_idx, self.delta)
        y = torch.FloatTensor(self.data[y_idx_arange, self.y_channel].copy())

        return x, y


from Logger import Logger
from ConsoleLogger import ConsoleLogger
from HTTPRequest import HTTPRequest
from HTTPProxyRequest import HTTPProxyRequest
from ProxyManager import ProxyManager
import multiprocessing
import multiprocessing.managers
import json
import re

regex_email = '([A-Z0-9%+\-\._]+@[A-Z0-9\.\-_]+\.[A-Z0-9]+)'
regex_category_id = 'Shop\.php/Listing\?category=([0-9]+)'
regex_category_page = 'Shop\.php/Listing\?category=[0-9]+&p=([0-9]+)'
regex_about_user_link = 'my_page\.php\?uid=([0-9]+)'
regex_auction_link = '"(/.*?\.html)"'
regex_user_name = '\<span class="uname"\>(.*?)\</span\>'
allegro_category_url = 'http://allegro.pl/Shop.php/Listing?category='
allegro_about_user_url = 'http://allegro.pl/my_page.php?uid='
allegro_user_auctions_url = 'http://allegro.pl/listing/user/listing.php?us_id='

processes_no = 5
ProxyManager.proxy_alert_count = 5
HTTPProxyRequest.headers = HTTPRequest.header = {'User-agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36'}
proxy_list_url = ''  # URL for a proxy list

Logger.init()
ConsoleLogger.init()

class MyManager(multiprocessing.managers.BaseManager):
	pass

def parse_user_page(worker_id, uid):
	Logger.message('Worker {}: processing user with id: {}'.format(worker_id, uid))
	request = HTTPProxyRequest(allegro_about_user_url + str(uid))
	response_0 = request.read()
	email_list = re.findall(regex_email, response_0, re.IGNORECASE)
	user_name = re.findall(regex_user_name, response_0, re.IGNORECASE)
	if len(user_name) == 0:
		user_name = ''
	else:
		user_name = user_name[0]
	if len(email_list) == 0:
		Logger.message('Worker {}: requesting user page, user id: {}'.format(worker_id, uid))
		request = HTTPProxyRequest(allegro_user_auctions_url + str(uid))
		auctions = re.findall(regex_auction_link, request.read(), re.IGNORECASE)
		if len(auctions) > 0:
			Logger.message('Worker {}: requesting user auction, user id: {}'.format(worker_id, uid))
			request = HTTPProxyRequest('http://allegro.pl' + auctions[0])
			email_list = re.findall(regex_email, request.read(), re.IGNORECASE)
		else:
			email_list = []
	return user_name, email_list

def parse_categories(worker_id, input_queue, output_queue, proxy_manager, done_user_list, done_categories_list):
	try:
		HTTPProxyRequest.set_proxy_manager(proxy_manager)
		Logger.message('Worker {}: started'.format(worker_id))
		while True:
			Logger.message('Worker {}: trying to acquire job'.format(worker_id))
			category_id = input_queue.get()
			if category_id is None:
				Logger.message('Worker {}: found poison pill, terminating'.format(worker_id))
				input_queue.task_done()
				break
			Logger.message('Worker {}: processing category id {}'.format(worker_id, category_id))
			if (category_id in done_categories_list) and ('all' in done_categories_list[category_id]):
				Logger.message('Worker {}: category {} was already processed'.format(worker_id, category_id))
				continue
			response = HTTPProxyRequest(allegro_category_url + str(category_id)).read()
			m = re.findall(regex_category_page, response, re.IGNORECASE)
			if len(m) == 0:
				Logger.message('Worker {}: category {} is empty'.format(worker_id, category_id))
				output_queue.put({'type': 'category_data', 'category_id': category_id, 'page': 'all'})
				input_queue.task_done()
				continue
			pages = int(m[-1])
			for page_no in range(1, pages + 1):
				Logger.message('Worker {}: started processing page {} of {} from category {}'.format(worker_id, page_no, pages, category_id))
				if (category_id in done_categories_list) and (page_no in done_categories_list[category_id]):
					Logger.message('Worker {}: page {} in category {} was already processed'.format(worker_id, page_no, category_id))
					continue
				if page_no > 1:
					response = HTTPProxyRequest(allegro_category_url + str(category_id) + '&p=' + str(page_no)).read()
				user_ids = re.findall(regex_about_user_link, response, re.IGNORECASE)
				for user_id in user_ids:
					if user_id in done_user_list:
						Logger.message('Worker {}: user {} already processed'.format(worker_id, user_id))
						continue
					(user_login, email_list) = parse_user_page(worker_id, user_id)
					email_list = set(email_list)
					Logger.message('Worker {}: found {} emails for uid {}'.format(worker_id, len(email_list), user_id))
					if len(email_list) == 0:
						output_queue.put({'type': 'user_data', 'user_id': user_id, 'login': user_login, 'email': ''})
					else:
						for email in email_list:
							output_queue.put({'type': 'user_data', 'user_id': user_id, 'login': user_login, 'email': email})
					done_user_list.append(user_id)
				output_queue.put({'type': 'category_data', 'category_id': category_id, 'page': page_no})
				Logger.message('Worker {}: finished processing page {} of {} from category {}'.format(worker_id, page_no, pages, category_id))
			output_queue.put({'type': 'category_data', 'category_id': category_id, 'page': 'all'})
			Logger.message('Worker {}: done processing category id {}'.format(worker_id, category_id))
			input_queue.task_done()
		Logger.message('Worker {}: finished'.format(worker_id))
	except KeyboardInterrupt:
		pass

def output_handler(output_queue):
	output_file = open('output.txt', 'a', encoding='utf-8')
	try:
		while True:
			item = output_queue.get()
			if item is None:
				break
			output_file.write(json.dumps(item) + '\n')
			output_file.flush()
	except KeyboardInterrupt:
		pass
	finally:
		output_file.flush()
		output_file.close()

def main():
	Logger.message('Start')
	# Find all categories ids
	response = HTTPRequest(allegro_category_url + '0').read()
	matches = re.findall(regex_category_id, response, re.IGNORECASE)
	main_categories = set(matches)
	main_categories.add('0')
	categories_ids = []
	for category_id in matches:
		Logger.message('Collecting categories ids in category: {}'.format(category_id))
		response = HTTPRequest(allegro_category_url + category_id).read()
		id_list = re.findall(regex_category_id, response, re.IGNORECASE)
		for sub_cat_id in id_list:
			if sub_cat_id not in main_categories:
				categories_ids.append(sub_cat_id)

	# Create queues
	input_queue = multiprocessing.JoinableQueue()
	output_queue = multiprocessing.Queue()

	# Fill input queue
	for category_id in categories_ids:
		input_queue.put(category_id)

	Logger.message('Queue size: {}'.format(input_queue.qsize()))

	# Create proxy manager
	MyManager.register('ProxyManagerClass', ProxyManager)
	manager = MyManager()
	manager.start()
	proxy_manager = manager.ProxyManagerClass()
	proxy_manager.set_url(proxy_list_url)
	proxy_manager.update_proxies()

	# Create processed user list
	user_list = multiprocessing.Manager().list()

	# Create categories dictionary
	categories_list = multiprocessing.Manager().dict()

	# Get list of already processed items
	try:
		database = open('output.txt', 'r', encoding='utf-8').read().splitlines(False)
		for row in database:
			data = json.loads(row)
			if data['type'] == 'user_data':
				if data['user_id'] not in user_list:
					user_list.append(data['user_id'])
			elif data['type'] == 'category_data':
				if data['category_id'] not in categories_list:
					categories_list[data['category_id']] = []
				tmp = categories_list[data['category_id']]
				if data['page'] not in tmp:
					tmp.append(data['page'])
					categories_list[data['category_id']] = tmp
	except IOError:
		pass

	Logger.message('Already processed {} users'.format(len(user_list)))

	# Create worker processes
	processes = []
	Logger.message('Starting worker processes.')
	for worker_id in range(processes_no):
		# Add poison pill for each process
		input_queue.put(None)
		process = multiprocessing.Process(target=parse_categories,
			args=(worker_id, input_queue, output_queue, proxy_manager, user_list, categories_list), daemon=True)
		processes.append(process)
		process.start()

	# Setup finish flag for output handler

	# Create output handler process
	Logger.message('Starting output handler process.')
	output_process = multiprocessing.Process(target=output_handler, args=(output_queue,), daemon=True)
	output_process.start()

	# Wait for worker processes to finish
	Logger.message('Waiting for worker processes to finish.')
	try:
		for process in processes:
			process.join()
	except KeyboardInterrupt:
		pass
	finally:
		# Put poison pill for output handler
		output_queue.put(None)

	Logger.message('Done loading, waiting for output handler process to finish.')

	# Wait for output handler to finish
	output_process.join()

if __name__ == '__main__':
	main()


#!/usr/bin/python3
import sys
import os.path
from parse_file import parse_file

def main():

    file_name = ""
    arg_num = len(sys.argv)

    # check if too few arguments were supplied
    if arg_num != 2:
        print("Usage: factors <file>")
        return 1

    # check if file exists
    if not os.path.isfile(sys.argv[1]):
        print("Error: File < {} > does not exist!".format(sys.argv[1]))
        sys.exit(1)

    file_name = sys.argv[1]
    parse_file(file_name)

    return 0

if __name__ == "__main__":
    main()


from django.shortcuts import render,redirect
from django.http import JsonResponse
from rest_framework.decorators import api_view,permission_classes
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticatedOrReadOnly,IsAuthenticated
from .models import Advocate,Company
from .serializers import AdvocateSerializer,CompanySerializer
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q
from rest_framework.views import APIView
from django.http import Http404
import requests
from dotenv import load_dotenv
load_dotenv()
import os

TWITTER_API_KEY=os.environ.get('TWITTER_API_KEY')

# Create your views here.
@api_view(['GET'])
@permission_classes([IsAuthenticatedOrReadOnly])
def enpoint(request):
    print(TWITTER_API_KEY )
    data=['advocates','advocates/:username']
    return Response(data)

@api_view(['GET','POST'])
@permission_classes([IsAuthenticatedOrReadOnly])
def advocate_list(request):
    if request.method=='GET':
        query=request.GET.get('query')
        if query==None:
            query=''
        advocate=Advocate.objects.filter(Q(username__icontains=query)|Q(bio__icontains=query))
        serializer=AdvocateSerializer(advocate,many=True)
        return Response(serializer.data)
    if request.method=='POST':
        advocate=Advocate.objects.create(
            username=request.data['username'],
            bio=request.data['bio']
        )
        serializer=AdvocateSerializer(advocate,many=False)
        return Response(serializer.data)

@permission_classes([IsAuthenticatedOrReadOnly])
class AdvocateDetails(APIView):
    def get_object(self,username):
        try:
            return Advocate.objects.get(username=username)
        except Advocate.DoesNotExist:
            raise Http404("Advocate doesn't exist.")
    def get(self, request, username):

        # head={'Authorization': 'Bearer ' + TWITTER_API_KEY}
        # url='https://api.twitter.com/2/users/by/username/{}'.format(username)
        # test=requests.get(url,headers=head).json()
        # print('Data form Twitter***',test)

        advocate=self.get_object(username)
        serializer=AdvocateSerializer(advocate,many=False)
        return Response(serializer.data)
    def put(self,request,username):
        advocate=self.get_object(username)
        advocate.bio=request.data['username']
        advocate.bio=request.data['bio']
        advocate.save()
        serializer=AdvocateSerializer(advocate,many=False)
        return Response(serializer.data)
    def delete(self,request,username):
        advocate=self.get_object(username)
        advocate.delete()
        return Response('user was deleted')

# @api_view(['GET','PUT','DELETE'])
# @permission_classes([IsAuthenticatedOrReadOnly])
# def advocate_details(request,username):
#     try:
#         advocate=Advocate.objects.get(username=username)
#         if request.method=="GET":
#             serializer=AdvocateSerializer(advocate,many=False)
#             return Response(serializer.data)
#         if request.method=='PUT':
#             advocate.username=request.data['username']
#             advocate.bio=request.data['bio']
#             advocate.save()
#             serializer=AdvocateSerializer(advocate,many=False)
#             return Response(serializer.data)
#         if request.method=='DELETE':
#             advocate.delete()
#             return Response('delete done')
#     except ObjectDoesNotExist:
#         return Response({'error': 'Advocate username not found.'}, status=404)

@api_view(['GET'])
@permission_classes([IsAuthenticatedOrReadOnly])
def Companis_List(request):
    companies=Company.objects.all()
    selizer=CompanySerializer(companies,many=True)
    return Response(selizer.data)


import requests

from bs4 import BeautifulSoup

from .crawler import Crawler
from ..models.page import PageScraperModel

class SeedingCrawler(Crawler):

    def __init__(self, base_url):
        super().__init__(base_url + 'd/furniture/search/fua?')
        self.stack = []

    def scrape(self):
        html = self.get_html(self.base_url)
        soup = self.get_soup(html)
        self.stack.append(
            PageScraperModel(
                soup,
                0
            )
        )
        self._dfs()

    def _dfs(self):
        while self.stack:
            node = self.stack.pop()
            if node.next_cursor:
                url = self.get_next_url(node.next_cursor)
                print(url, flush=True)
                html = self.get_html(url)
                soup = self.get_soup(html)
                self.stack.append(
                    PageScraperModel(
                        soup,
                        node.next_cursor,
                        node.max_cursor
                    )
                )
                node.save_to_db()

    def get_next_url(self, next_cursor):
        return self.base_url + f's={next_cursor}'

    def get_html(self, url):
        req = requests.get(url)
        if req.status_code == 200:
            return req.text

    def get_soup(self, html):
        return BeautifulSoup(html, 'html.parser')


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

# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.

__author__="andrew.tuley"
__date__ ="$24-Jul-2015 16:06:48$"

from Players import Player1, Player2, Player3, Player4, Player5, Player6
import random

def show_line(board, idx):
    print ('{0}|{1}|{2}'.format(board[0+idx],board[1+idx],board[2+idx]))

def show_board(board):
    show_line(board,0)
    show_line(board,3)
    show_line(board,6)
    print ('')

def is_draw(board):
    idx=0
    draw=True
    while idx < 9 and draw == True:
        if board[idx] == ' ':
            draw = False
        idx += 1
    return draw

def add_to_score(player, score_add):
    if player in scores.keys():
        score = scores[player]
        scores[player] = score+score_add
    else:
        scores[player] = score_add

if __name__ == "__main__":
    players=[Player1('player1'), Player2('player2'), Player3('player3'), Player4('player4'), Player5('player5'), Player6('player6')]

    game=0
    scores={}
    while game < 500:
        player1 = random.choice(players).__class__('p1')
        player2 = random.choice(players).__class__('p2')

        player1.set_piece('X')
        player2.set_piece('O')

        print ('Pitting {0} against {1}'.format(player1, player2))

        winner = None
        board = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
        player=player1
        while winner == None:
            board = player.play(board)
            if player.has_won(board, player.piece):
                winner = player

            if is_draw(board):
                winner = 'draw'

            show_board(board)
            if winner == None:
                if player == player1:
                    player = player2
                else:
                    player = player1

        if winner == 'draw':
            print('draw')
            add_to_score(player1.description, 0.5)
            add_to_score(player2.description, 0.5)
        else:
            print ('Winner is {0}'.format(winner))
            add_to_score(winner.description, 1)

        game += 1
    for jouist in scores:
        print ('Player {0} scored {1}'.format(jouist, scores[jouist]))


from pygame import *

from meteor import Meteor

MOVE_SPEED = 3
WIDTH = 90
HEIGHT = 82

class SpaceShip(sprite.Sprite):
    def __init__(self, x, y):
        super(SpaceShip, self).__init__()
        position = (x, y)
        self.alive = True
        self.image = Surface((WIDTH, HEIGHT), SRCALPHA)
        self.image = image.load('images/ships/enemyBlue1.png')
        self.original_image = self.image
        self.rect = self.image.get_rect(center=position)
        self.position = Vector2(x, y)
        self.direction = Vector2(0, 1)
        self.speed = MOVE_SPEED
        self.angle_speed = 0
        self.angle = 0

    def update(self, items):
        if self.angle_speed != 0:
            self.direction.rotate_ip(self.angle_speed)
            self.angle += self.angle_speed
            self.image = transform.rotate(self.original_image, -self.angle)
            self.rect = self.image.get_rect(center=self.rect.center)
        self.position += self.direction * self.speed
        self.rect.center = self.position
        self.collide(items)

    def die(self):
        # self.direction = Vector2(0, 0)
        # time.wait(1500)
        self.teleport(700, 400)

    def teleport(self, goX, goY):
        self.rect.x = goX
        self.rect.y = goY
        self.position = (goX, goY)
        self.alive = True

    def collide(self, items):
        for i in items:
            for j in i:
                if sprite.collide_rect(self, j):
                    if isinstance(j, Meteor):
                        self.die()
                        self.alive = False


from functools import cache

import matplotlib.pyplot as plt
import numpy as np

from data import NICKEL_REGIMES, load_experiment_data
from experiment_setup import NI_FOIL
from intensity import output_intensity
from materials import Layer, Cell, KAPTON

def get_geometric_factor_ascan(plot=False):

    # Limits determined manually
    limits_x = [75, 160]
    limits_y = [[40, 90],
                [130, 180],
                [220, 270],
                [300, 350],
                [400, 450]]

    # Good scans determined manually
    # Selection is I02-independent
    scans = {
        'in_cell': (2, [21, 30]),
        'out_of_cell': (48, [6, 75]),
    }

    coefficients = {}
    for name, (number, scan_range) in scans.items():
        data = np.load(f'data/geometry_data/scan_{number}.npy')

        # Plot detector receptive field
        # fig, ax = plt.subplots(2)
        # ax[0].plot(data.sum(axis=(0, 2)))
        # ax[1].plot(data.sum(axis=(0, 1)))
        #
        # plt.show()

        crystal_data = [data[:, limits_x[0]:limits_x[1], limit_y[0]: limit_y[1]] for limit_y in limits_y]

        # Intensities per crystal and scan
        intensities = np.array([np.sum(crystal, axis=(1, 2)) for crystal in crystal_data]).T

        if plot:
            plt.plot(intensities)
            plt.show()

        intensities = intensities[scan_range[0]:scan_range[1] + 1]
        coefficients[name] = intensities.sum(axis=0)
        coefficients[name] /= coefficients[name][0]

    return coefficients

def get_norm(energy, intensity):

    # Subtract background
    mask_background = energy < NICKEL_REGIMES['pre_edge']
    background = intensity.loc[mask_background].mean()

    intensity = intensity - background

    return np.trapz(intensity, energy)

def get_geometric_factors_energy_scans():

    cells = {'in_cell': load_experiment_data(NI_FOIL[0]),
             'out_of_cell': load_experiment_data(NI_FOIL[1])}

    detectors = NI_FOIL[0].detectors

    norms = {}
    for name, cell in cells.items():
        norms[name] = np.array([get_norm(cell['energy'], cell[f'intensity_{detector}']) for detector in detectors])
        norms[name] = norms[name] / norms[name][0]

    return norms

def get_theoretical_angle_distribution():

    ni_foil = Cell(layers=[KAPTON, Layer(depth=5.0, densities={'Ni': 8.908})])

    density = np.zeros(7000)
    density[6500:] = 1.0  # 65 um of no nickel and 5um of nickel only

    theta_in = np.pi / 180 * 31
    theta_out = np.pi / 180 * np.array([41, 34, 27, 20, 13])  # Detectors C1-5

    energies = np.array([8400])

    results = output_intensity(theta_in, theta_out, ni_foil, energies, 7480, density)
    results = results[0]

    results = results / results[0]
    return results

@cache
def get_geometric_factors():

    factors = get_geometric_factors_energy_scans()
    factors['theoretical'] = get_theoretical_angle_distribution()

    return factors


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

# In[ ]:

# WDQ7005 - Data Mining
# Master of Data Science | University of Malaya
# Assignment Part A: Web Crawling of Real-time Data

# Group Members:
# Azwa Kamaruddin (WQD170089)
# Kok Hon Loong (WQD170086)

# In[1]:

import requests
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.figure(figsize=(20,10))
from bs4 import BeautifulSoup

# In[ ]:

### =============================== ###
### CODE STRUCTURE
### =============================== ###
#
# We partitioned the code into 3 sections:
# 1. Number of CONFIRMED cases.
# 2. Number of DEATH cases.
# 3. Number of RECOVERED cases.
# For each section, we display the time series trend for ASEAN countries and China and compare between them.

# In[2]:

### =============================== ###
### NO. OF CONFIRMED CASES
### =============================== ###
url_confirmed_cases = "https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv"

try:
    page = requests.get(url_confirmed_cases, timeout=5)
    if page.status_code == 200:
        soup = BeautifulSoup(page.content,'html.parser')
        table = soup.find("table", {"class": "js-csv-data csv-data js-file-line-container"})
        df = pd.read_html(str(table))
    else:
        print(str(page.status_code) + " - Error, page not found.")
except requests.ConnectionError as e:
    print('Connection error')
    print(str(e))

# In[3]:

# Put the tabulated data into a dataframe and display the first 5 results:
confirmed_cases = df[0]
confirmed_cases.head()

# In[4]:

# Drop unneeded columns from the dataframe:
confirmed_cases = confirmed_cases.drop('Unnamed: 0', axis=1)
confirmed_cases = confirmed_cases.drop(['Lat','Long'], axis=1)
confirmed_cases = confirmed_cases.drop('Province/State', axis=1)

# Rename column header to simply 'Country' and set it as the index:
confirmed_cases = confirmed_cases.rename(columns={'Country/Region':'Country'})
confirmed_cases = confirmed_cases.set_index('Country')

# Display first 5 results:
confirmed_cases.head()

# In[5]:

# COMMENT: From the above table we can see that the number of confirmed cases are increasing for all listed countries.

# In[6]:

# Let's transpose the table and describe the data for each of the different countries:
confirmed_cases.transpose().describe()

# In[7]:

# COMMENT:
# count - shows how many days of data cases are being tracked. There are 51 days since the first tracking.
# mean - shows the average number of confirmed cases during the 51 days.
# max - the total number of confirmed cases to date.

# In[8]:

# Obtain only the confirmed cases for the 10 ASEAN countries + China

# Get data for ASEAN regions:
asean = ['Malaysia', 'Singapore', 'Thailand', 'Brunei', 'Cambodia', 'Indonesia', 'Laos', 'Myanmar', 'Philippines', 'Vietnam']
confirmed_cases_asean = confirmed_cases[confirmed_cases.index.isin(asean)]
confirmed_cases_asean

# In[9]:

# China:
confirmed_cases_china = confirmed_cases[confirmed_cases.index == 'China']
confirmed_cases_china.head()

# In[10]:

# For China, we combined all the different regions into a single row representing the entire China:
confirmed_cases_china_combined = confirmed_cases_china.groupby('Country').sum()
confirmed_cases_china_combined

# In[11]:

# Plot the number of confirmed cases over time for all ASEAN countries:

confirmed_cases_asean.T.plot()
plt.ylabel('No. of confirmed cases')
plt.xlabel('Days')
plt.show()

# In[12]:

# COMMENT: Singapore has the most number of recorded confirmed cases and are increasing exponentially.
# Malaysia is second followed by Thailand as the 3rd most recorded confirmed cases in ASEAN.

# In[13]:

# Plot the number of confirmed cases over time for China:

confirmed_cases_china_combined.T.plot()
plt.ylabel('No. of confirmed cases')
plt.xlabel('Days')
plt.show()

# In[14]:

# COMMENT: Confirmed cases in China continues to rise. More than 80k cases confirmed to date.

# In[15]:

# Plot the number of confirmed cases over time for China vs ASEAN countries:

ax = confirmed_cases_china_combined.T.plot()
confirmed_cases_asean.T.plot(ax=ax)

# In[16]:

# COMMENT: China numbers are too huge to compare with the number of cases in ASEAN.

# In[17]:

### =============================== ###
### NO. OF DEATH CASES
### =============================== ###
url_death_cases = "https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Deaths.csv"

try:
    page = requests.get(url_death_cases, timeout=5)
    if page.status_code == 200:
        soup = BeautifulSoup(page.content,'html.parser')
        table = soup.find("table", {"class": "js-csv-data csv-data js-file-line-container"})
        df = pd.read_html(str(table))
    else:
        print(str(page.status_code) + " - Error, page not found.")
except requests.ConnectionError as e:
    print('Connection error')
    print(str(e))

# In[18]:

# Put the tabulated data into a dataframe and display the first 5 results:
death_cases = df[0]
death_cases.head()

# In[19]:

# Drop unneeded columns from the dataframe:
death_cases = death_cases.drop('Unnamed: 0', axis=1)
death_cases = death_cases.drop(['Lat','Long'], axis=1)
death_cases = death_cases.drop('Province/State', axis=1)

# Rename column header to simply 'Country' and set it as the index:
death_cases = death_cases.rename(columns={'Country/Region':'Country'})
death_cases = death_cases.set_index('Country')

# Display first 5 results:
death_cases.head()

# In[20]:

# Transpose the table and describe the data for each of the different countries:
death_cases.transpose().describe()

# In[21]:

# COMMENT:
# count - shows how many days of data cases are being tracked. There are 51 days since the first tracking.
# mean - shows the average number of death cases during the 51 days.
# max - the total number of death cases to date.

# In[22]:

# Obtain only the death cases for the 10 ASEAN countries + China

# Get data for ASEAN regions:
asean = ['Malaysia', 'Singapore', 'Thailand', 'Brunei', 'Cambodia', 'Indonesia', 'Laos', 'Myanmar', 'Philippines', 'Vietnam']
death_cases_asean = death_cases[death_cases.index.isin(asean)]
death_cases_asean

# In[23]:

# China:
death_cases_china = death_cases[death_cases.index == 'China']
death_cases_china.head()

# In[24]:

# For China, we combined all the different regions into a single row representing the entire China:
death_cases_china_combined = death_cases_china.groupby('Country').sum()
death_cases_china_combined

# In[25]:

# Plot the number of death cases over time for all ASEAN countries:

death_cases_asean.T.plot()
plt.ylabel('No. of death cases')
plt.xlabel('Days')
plt.show()

# In[26]:

# COMMENT: The number of deaths from the covid-19 is rare/low in the ASEAN region.
# Only 4 patients have died: 2 from Phillipines, 1 from Thailand, and 1 from Indonesia.

# In[27]:

# Plot the number of death cases over time for China:

death_cases_china_combined.T.plot()
plt.ylabel('No. of confirmed cases')
plt.xlabel('Days')
plt.show()

# In[28]:

# COMMENT: The number of death cases in China has risen to more than 3k cases.

# In[29]:

# Plot the number of death cases over time for China vs ASEAN countries:

ax = death_cases_china_combined.T.plot()
death_cases_asean.T.plot(ax=ax)

# In[30]:

# COMMENT: Deaths in ASEAN countries due to covid-19 is rare or low compared to China.

# In[31]:

### =============================== ###
### NO. OF RECOVERED CASES
### =============================== ###
url_recovered_cases = "https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Recovered.csv"

try:
    page = requests.get(url_recovered_cases, timeout=5)
    if page.status_code == 200:
        soup = BeautifulSoup(page.content,'html.parser')
        table = soup.find("table", {"class": "js-csv-data csv-data js-file-line-container"})
        df = pd.read_html(str(table))
    else:
        print(str(page.status_code) + " - Error, page not found.")
except requests.ConnectionError as e:
    print('Connection error')
    print(str(e))

# In[32]:

# Put the tabulated data into a dataframe and display the first 5 results:
recovered_cases = df[0]
recovered_cases.head()

# In[33]:

# Drop unneeded columns from the dataframe:
recovered_cases = recovered_cases.drop('Unnamed: 0', axis=1)
recovered_cases = recovered_cases.drop(['Lat','Long'], axis=1)
recovered_cases = recovered_cases.drop('Province/State', axis=1)

# In[34]:

# Rename column header to simply 'Country' and set it as the index:
recovered_cases = recovered_cases.rename(columns={'Country/Region':'Country'})
recovered_cases = recovered_cases.set_index('Country')

# In[35]:

# Display first 5 results:
recovered_cases.head()

# In[36]:

# Transpose the table and describe the data for each of the different countries:
recovered_cases.transpose().describe()

# In[37]:

# COMMENT:
# count - shows how many days of data cases are being tracked. There are 51 days since the first tracking.
# mean - shows the average number of recovered cases during the 51 days.
# max - the total number of recovered cases to date.

# In[38]:

# Obtain only the recovered cases for the 10 ASEAN countries + China

# Get data for ASEAN regions:
asean = ['Malaysia', 'Singapore', 'Thailand', 'Brunei', 'Cambodia', 'Indonesia', 'Laos', 'Myanmar', 'Philippines', 'Vietnam']
recovered_cases_asean = recovered_cases[recovered_cases.index.isin(asean)]
recovered_cases_asean

# In[39]:

# China:
recovered_cases_china = recovered_cases[recovered_cases.index == 'China']
recovered_cases_china.head()

# In[40]:

# For China, we combined all the different regions into a single row representing the entire China:
recovered_cases_china_combined = recovered_cases_china.groupby('Country').sum()
recovered_cases_china_combined

# In[41]:

# Plot the number of recovered cases over time for all ASEAN countries:

recovered_cases_asean.T.plot()
plt.ylabel('No. of recovered cases')
plt.xlabel('Days')
plt.show()

# In[42]:

# COMMENT: Singapore continues to lead in terms of the number of patients who recovered from covid-19.
# Thailand is second in recovery, while Malaysia is in 3rd place.

# In[43]:

# Plot the number of recovered cases over time for China:

recovered_cases_china_combined.T.plot()
plt.ylabel('No. of confirmed cases')
plt.xlabel('Days')
plt.show()

# In[44]:

# COMMENT: Despite the huge number of confirmed cases at 80k, the number of patients who recovered is also increasing rapidly at 60k people.

# In[45]:

# Plot the number of recovered cases over time for China vs ASEAN countries:

ax = recovered_cases_china_combined.T.plot()
recovered_cases_asean.T.plot(ax=ax)

# In[46]:

# COMMENT: China recovery is increasing since they have the most case, while ASEAN countries are seen recovering as well but ASEAN cases are lower compared to China.

# In[47]:

### =============================== ###
### COMPARISON OF DEATH vs RECOVERED
### =============================== ###

# For ASEAN countries:

ax = death_cases_asean.plot(kind='hist')
recovered_cases_asean.plot(kind='hist', ax=ax)

# In[48]:

recovered_cases_asean

# In[49]:

death_cases_asean.transpose()[-1:].plot(kind='hist')

# In[50]:

recovered_cases_asean.transpose()[-1:].unstack().plot(kind='hist')

# In[ ]:


import time, datetime
from decoder import Decoder, InvalidGearException
import os, yaml, shutil, json
from gear import Gear, Substat
import unidecode

from PIL import Image

def show_img(path):
    img = Image.open(path)
    img.show()

def log(msg):
    print(f"[{datetime.datetime.now()}] - {msg}")

config = yaml.safe_load(open("config.yaml","r"))
folder_left, folder_substats = config['folder_left'], config['folder_substats']
folder_result = config['folder_result']

gear_paths = [f'{folder_result}/{filename}' for filename in os.listdir(folder_result) if '.json' in filename]

incompleted_paths = [gear_path for gear_path in gear_paths if not json.load(open(gear_path, 'r'))['meta']['completed_substats']]
log(f"Incomplete gears: {len(incompleted_paths)}/{len(gear_paths)}")

for index_correction,gear_path in enumerate(incompleted_paths):

    with open(gear_path, 'r') as f:
        dict_gear = json.load(f)
        f.close()

    if not dict_gear['meta']['completed_substats']:
        print('...')
        log(f'Correcting {gear_path} - correction {index_correction}/{len(incompleted_paths)}')
        skip_this_correction = False
        stop_all_corrections = False
        while not skip_this_correction and not stop_all_corrections:
            decision = input('What do you want to do ?\nskip : skip this correction\nsub : Enter the substats value separated by a "-". Example "5-8%-365-7.5%" for 4 substats: [5, 8%, 365, 7.5%]\nshow : show picture of the substats values\nstop : stop the whole process\n>>')
            # skipping this correction
            if decision.lower() == "skip":
                break
            # entering the sub input
            elif "-" in decision.lower():
                subs_list = decision.replace(" ","").replace("sub","").split("-")
                confirm_subs = input(f'Do you confirm adding subs {subs_list} to gear n°{dict_gear["meta"]["index_img"]} ?  y/n\n>>')
                if confirm_subs.lower() == 'y':
                    print(f'Adding subs {decision} to gear n°{dict_gear["meta"]["index_img"]}')
                    # creating list of substats
                    substats = []
                    sub_dicts = dict_gear['substats']
                    for i,sub_value in enumerate(subs_list):
                        sub_dict = sub_dicts[i]
                        is_percent =  '%' in sub_value
                        stat_amount = float(sub_value.replace('%',''))
                        substats.append(Substat(stat_name=sub_dict['stat_name'],
                                                stat_amount=stat_amount,
                                                nbs_procs=sub_dict['nbs_procs'],
                                                is_percent=is_percent))
                    # creating gear object
                    gear = Gear(
                        type = unidecode.unidecode(dict_gear['type']),
                        level = dict_gear['level'],
                        main_stat_name = dict_gear['main_stat_name'],
                        main_stat_amount = dict_gear['main_stat_amount'],
                        main_stat_is_percent = dict_gear['main_stat_is_percent'],
                        substats = substats,
                        set_name = dict_gear['set_name'],
                        meta = {"completed_substats":True, "index_img":dict_gear["meta"]["index_img"]})
                    # updating json dict of gear with new gear._asdict()
                    print(f"Updating gear dict: {gear._asdict()}")
                    with open(f"{folder_result}/{dict_gear['meta']['index_img']}.json","w+") as f:
                        json.dump(gear._asdict(), f)
                        f.close()
                    # breaking the loop to go to next gear to correct
                    break
                else:
                    print('Cancelling this subs input')
                    pass
            # showing image
            elif decision.lower() == "show":
                show_img(f"{folder_substats}/{dict_gear['meta']['index_img']}.png")
            # stopping the whole correction process
            elif decision.lower() == "stop":
                stop_all_corrections = True
                break
            else:
                pass
        if stop_all_corrections:
            log(f'Stopping all corrections')
            break


import json
import sys
from btcc_trade_collector import bttc_trade_collector
from btcc_client import Market
from btcc_log import create_timed_rotating_log
from btcc_log import set_log_name_prefix

with open("./collector_config.json") as config_file:
    config = json.load(config_file)

if (len(sys.argv) != 2):
    print ("Usage: python run_collector.py [job name]")
    print ("Optional job names are:")
    print ("  ".join(config["jobs"].keys()))
    sys.exit(1)

common = config["common"]
jobname = sys.argv[1]
job = config["jobs"][jobname]

set_log_name_prefix(jobname)
logger = create_timed_rotating_log()

submit_config = common.copy()
submit_config.update(job)
a = bttc_trade_collector(submit_config)
logger.info("btcc collector started with the following config:")
logger.info(json.dumps(submit_config))
a.run()


from typing import TYPE_CHECKING, Any, Dict, List, Type, TypeVar, Union

import attr

from ..models.extension_value_type import ExtensionValueType
from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.reference import Reference

T = TypeVar("T", bound="Extension")

@attr.s(auto_attribs=True)
class Extension:
    """
    Attributes:
        name (Union[Unset, str]):
        value (Union[Unset, str]):
        value_type (Union[Unset, ExtensionValueType]):
        refers_to (Union[Unset, List['Reference']]):
        supplemental_semantic_ids (Union[Unset, List['Reference']]):
        semantic_id (Union[Unset, Reference]):
    """

    name: Union[Unset, str] = UNSET
    value: Union[Unset, str] = UNSET
    value_type: Union[Unset, ExtensionValueType] = UNSET
    refers_to: Union[Unset, List["Reference"]] = UNSET
    supplemental_semantic_ids: Union[Unset, List["Reference"]] = UNSET
    semantic_id: Union[Unset, "Reference"] = UNSET
    additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        name = self.name
        value = self.value
        value_type: Union[Unset, str] = UNSET
        if not isinstance(self.value_type, Unset):
            value_type = self.value_type.value

        refers_to: Union[Unset, List[Dict[str, Any]]] = UNSET
        if not isinstance(self.refers_to, Unset):
            refers_to = []
            for refers_to_item_data in self.refers_to:
                refers_to_item = refers_to_item_data.to_dict()

                refers_to.append(refers_to_item)

        supplemental_semantic_ids: Union[Unset, List[Dict[str, Any]]] = UNSET
        if not isinstance(self.supplemental_semantic_ids, Unset):
            supplemental_semantic_ids = []
            for supplemental_semantic_ids_item_data in self.supplemental_semantic_ids:
                supplemental_semantic_ids_item = supplemental_semantic_ids_item_data.to_dict()

                supplemental_semantic_ids.append(supplemental_semantic_ids_item)

        semantic_id: Union[Unset, Dict[str, Any]] = UNSET
        if not isinstance(self.semantic_id, Unset):
            semantic_id = self.semantic_id.to_dict()

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if name is not UNSET:
            field_dict["name"] = name
        if value is not UNSET:
            field_dict["value"] = value
        if value_type is not UNSET:
            field_dict["valueType"] = value_type
        if refers_to is not UNSET:
            field_dict["refersTo"] = refers_to
        if supplemental_semantic_ids is not UNSET:
            field_dict["supplementalSemanticIds"] = supplemental_semantic_ids
        if semantic_id is not UNSET:
            field_dict["semanticId"] = semantic_id

        return field_dict

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        from ..models.reference import Reference

        d = src_dict.copy()
        name = d.pop("name", UNSET)

        value = d.pop("value", UNSET)

        _value_type = d.pop("valueType", UNSET)
        value_type: Union[Unset, ExtensionValueType]
        if isinstance(_value_type, Unset):
            value_type = UNSET
        else:
            value_type = ExtensionValueType(_value_type)

        refers_to = []
        _refers_to = d.pop("refersTo", UNSET)
        for refers_to_item_data in _refers_to or []:
            refers_to_item = Reference.from_dict(refers_to_item_data)

            refers_to.append(refers_to_item)

        supplemental_semantic_ids = []
        _supplemental_semantic_ids = d.pop("supplementalSemanticIds", UNSET)
        for supplemental_semantic_ids_item_data in _supplemental_semantic_ids or []:
            supplemental_semantic_ids_item = Reference.from_dict(supplemental_semantic_ids_item_data)

            supplemental_semantic_ids.append(supplemental_semantic_ids_item)

        _semantic_id = d.pop("semanticId", UNSET)
        semantic_id: Union[Unset, Reference]
        if isinstance(_semantic_id, Unset):
            semantic_id = UNSET
        else:
            semantic_id = Reference.from_dict(_semantic_id)

        extension = cls(
            name=name,
            value=value,
            value_type=value_type,
            refers_to=refers_to,
            supplemental_semantic_ids=supplemental_semantic_ids,
            semantic_id=semantic_id,
        )

        extension.additional_properties = d
        return extension

    @property
    def additional_keys(self) -> List[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties


from itertools import compress

def bool_code(number):
    b = list()
    while number != 0:
        if number % 2 == 1:
            b.append(True)
        else:
            b.append(False)
        number >>= 1
    return b

def bit_not(n, num_bits):
    return (1 << num_bits) - 1 - n

def translate_code(items, mask):
    return ' '.join(list(compress(items, bool_code(mask))))

def print_closed_frequent_itemsets(closed_frequent_itemsets, l1):
    for itemset in closed_frequent_itemsets:
        print(translate_code(l1, itemset), ' #SUP:', closed_frequent_itemsets[itemset])

def bit_map_code(transaction, f_1, l_1):
    bc = 0
    for item in transaction:
        if item in f_1:
            f_1[item] += 1
        else:
            f_1[item] = 1
            l_1.append(item)

        bc += 2 ** l_1.index(item)

    # return bc, transaction.__len__(), f_1, l_1
    return bc, f_1, l_1


__author__ = "Dev Churiwala"

"""
Simple model to predict the XOR/XNOR output of 2 bits based on third input
"""

import numpy as np

def sigmoid (x):
    return (1/(1 + np.exp(-x)))

def sigmoid_derivative(x):
    return (x * (1 - x))

#Input datasets
inputs = np.array([[1,1,0],[1,0,0],[0,1,0],[0,0,0],[1,1,1],[1,0,1],[0,1,1],[0,0,1]])
expected_output = np.array([[1],[0],[0],[1],[0],[1],[1],[0]])

epochs = 10000
lr = 0.1
inputLayerNeurons, hiddenLayerNeurons, outputLayerNeurons = 3,2,1

#Parameter initialization
hidden_weights = np.random.randn(inputLayerNeurons,hiddenLayerNeurons)*0.01
hidden_bias =np.zeros((1,hiddenLayerNeurons))
output_weights = np.random.randn(hiddenLayerNeurons,outputLayerNeurons)*0.01
output_bias = np.zeros((1,outputLayerNeurons))

for _ in range(epochs):
	#Forward Propagation
	hidden_layer_activation = np.dot(inputs,hidden_weights)
	hidden_layer_activation += hidden_bias
	hidden_layer_output = sigmoid(hidden_layer_activation)

	output_layer_activation = np.dot(hidden_layer_output,output_weights)
	output_layer_activation += output_bias
	predicted_output = sigmoid(output_layer_activation)

	#Backpropagation
	error = expected_output - predicted_output
	d_predicted_output = error * sigmoid_derivative(predicted_output)

	error_hidden_layer = d_predicted_output.dot(output_weights.T)
	d_hidden_layer = error_hidden_layer * sigmoid_derivative(hidden_layer_output)

	#Updating Weights and Biases
	output_weights += hidden_layer_output.T.dot(d_predicted_output) * lr
	output_bias += np.sum(d_predicted_output,axis=0,keepdims=True) * lr
	hidden_weights += inputs.T.dot(d_hidden_layer) * lr
	hidden_bias += np.sum(d_hidden_layer,axis=0,keepdims=True) * lr

print("Output from neural network after 10,000 epochs: ")
print(*predicted_output)

'''
Output from neural network after 10,000 epochs:
[0.5000056] [0.50000035] [0.5000069] [0.50000165] [0.49999834] [0.4999931] [0.49999965] [0.4999944]
'''

Neural Net for XOR-XNOR/2 Layer Model.py