#!/usr/local/bin/python3
#
# Authors: Bobby Rathore (brathore), Neha Supe (nehasupe), Kelly Wheeler (kellwhee)
#
# Mountain ridge finder
# Based on skeleton code by D. Crandall, Oct 2019

from PIL import Image
import numpy as np
from scipy.ndimage import filters
import sys
import os
import imageio

class HMM:
    def __init__(self, transitional_probabilities, emission_probabilities):
        """
        Constructor for the HMM model class
        :param transitional_probabilities:
        :param emission_probabilities:
        """
        self.transitional_probabilities = transitional_probabilities
        self.emission_probabilities = emission_probabilities

    def get_emission_dist(self, emission):
        return self.emission_probabilities[:, emission]

    @property
    def get_number_of_states(self):
        return self.transitional_probabilities.shape[0]

    @property
    def get_transition_probabilities(self):
        return self.transitional_probabilities

def get_edge_strength(input_image) -> np.array:
    """
    calculate "Edge strength map" of an image
    :param input_image: the original image for which we want the edge strengths
    :return: a 2D numpy vector containing the edge strength matrix
    """
    grayscale = np.array(input_image.convert("L"))
    filtered_y = np.zeros(grayscale.shape)
    filters.sobel(grayscale, 0, filtered_y)
    return np.sqrt(filtered_y ** 2)

def draw_edge(image, y_coordinates, color, thickness):
    """
    draw a "line" on an image (actually just plot the given y-coordinates for each x-coordinate)
    image is the image to draw on y_coordinates is a list, containing the
    y-coordinates and length equal to the x dimension size of the image
    color is a (red, green, blue) color triple (e.g. (255, 0, 0) would be pure red
    thickness is thickness of line in pixels
    :param image: the original image object to put pixels on
    :param y_coordinates: the coordinates where we shall put pixels
    :param color: color of the pixels
    :param thickness: density/thickness of the pixels
    :return: image object with superimposed pixels
    """
    for (x, y) in enumerate(y_coordinates):
        for t in range(
            int(max(y - int(thickness / 2), 0)),
            int(min(y + int(thickness / 2), image.size[1] - 1)),
        ):
            image.putpixel((x, t), color)
    return image

def bayes_net(edge_strength_matrix: np.array, filename: str, image) -> None:
    """
    Naive Bayes Net implementation for the given image matrix
    :param edge_strength_matrix: a 2D numpy vector containing the edge strength matrix
    :param filename: name of the file we're working with
    :param image: the original image object to put pixels on
    :return: None
    """
    # Save for our records
    imageio.imwrite(
        "bayes_net/{0}_output_simple.jpg".format(filename.split(".")[0].split("/")[-1]),
        draw_edge(
            image=image.copy(),
            y_coordinates=edge_strength_matrix.argmax(axis=0),
            color=(0, 0, 255),
            thickness=5,
        ),
    )

    # Save for grading system
    imageio.imwrite(
        "output_simple.jpg",
        draw_edge(
            image=image,
            y_coordinates=edge_strength_matrix.argmax(axis=0),
            color=(0, 0, 255),
            thickness=5,
        ),
    )
    return None

def viterbi(edge_strength_matrix: np.array, filename: str, image) -> None:
    """
    Viterbi implementation for the given image matrix
    :param edge_strength_matrix: a 2D numpy vector containing the edge strength matrix
    :param filename: name of file we're working with
    :param image: the original image object to put pixels on
    :return: None
    """
    # Initial probabilities are normally distributed
    normally_distributed_initial_probabilities = np.full(
        edge_strength_matrix.shape[0], 1 / edge_strength_matrix.shape[0]
    )
    max_of_each_row = np.amax(edge_strength_matrix, axis=0)

    # Setup Emission Probabilities
    emission_probabilities = np.array(
        [
            each_row / max_of_each_row[index]
            for index, each_row in enumerate(edge_strength_matrix)
        ]
    )

    # Setup transitional Probabilities
    all_rows = range(edge_strength_matrix.shape[0])
    column_value_for_each_row = [row for row in all_rows]

    # Take absolute value of each row normalized with it's corresponding column value
    transitional_probabilities = np.array(
        [
            np.array(
                [
                    abs(
                        (
                            column
                            - np.max(
                                [
                                    abs(row - absolute_column_value)
                                    for absolute_column_value in column_value_for_each_row
                                ]
                            )
                        )
                        / np.max(
                            [
                                abs(row - absolute_column_value)
                                for absolute_column_value in column_value_for_each_row
                            ]
                        )
                    )
                    for column in [
                        abs(row - absolute_column_value)
                        for absolute_column_value in column_value_for_each_row
                    ]
                ]
            )
            for row in all_rows
        ]
    )

    # Viterbi algorithm begins here
    first_product, second_product = (
        np.empty((transitional_probabilities.shape[0], edge_strength_matrix.shape[1])),
        np.empty((transitional_probabilities.shape[0], edge_strength_matrix.shape[1])),
    )

    # Setup the first column by multiplying the initial and emission probabilities
    first_product[:, 0] = (
        normally_distributed_initial_probabilities * emission_probabilities[:, 0]
    )
    second_product[:, 0] = 0

    # Update each column along the way with the maximum value
    for column in range(1, edge_strength_matrix.shape[1]):
        first_product[:, column] = np.max(
            first_product[:, column - 1]
            * transitional_probabilities.T
            * emission_probabilities[:, column].T,
            1,
        )
        second_product[:, column] = np.argmax(
            first_product[:, column - 1] * transitional_probabilities.T, 1
        )

    # Get the y_coordinates ready
    resultant_matrix = np.empty(edge_strength_matrix.shape[1])
    resultant_matrix[-1] = np.argmax(
        first_product[:, edge_strength_matrix.shape[1] - 1]
    )

    # "Backtracking"
    for column in reversed(range(1, edge_strength_matrix.shape[1])):
        resultant_matrix[column - 1] = second_product[
            int(resultant_matrix[column]), column
        ]

    # Save for our records
    imageio.imwrite(
        "viterbi/{0}_output_map.jpg".format(filename.split(".")[0].split("/")[-1]),
        draw_edge(
            image=image.copy(),
            y_coordinates=resultant_matrix,
            color=(255, 0, 0),
            thickness=5,
        ),
    )

    # Save for grading system
    imageio.imwrite(
        "output_map.jpg",
        draw_edge(
            image=image, y_coordinates=resultant_matrix, color=(255, 0, 0), thickness=5
        ),
    )

    return None

def viterbi_with_logs(
    edge_strength_matrix: np.array,
    filename: str,
    image,
    row: int = 0,
    column: int = 0,
    human: bool = False,
) -> None:
    """
    Similar viterbi implementation as above but without initial probabilities,
    with human feedback incorporation and logs to avoid underflow errors
    :param edge_strength_matrix: a 2D numpy vector containing the edge strength matrix
    :param filename: the name of the file we're working with
    :param image: the original image object to put pixels on
    :param row: row pixel value where human thinks the ridge is
    :param column: column pixel value where human thinks the ridge is
    :param human: a flag to determine whether human feedback is to be considered or not
    :return: None
    """
    # Setup Transitional Probabilities
    transition_probabilities = np.repeat(
        np.linspace(
            0, edge_strength_matrix.shape[0] - 1, edge_strength_matrix.shape[0]
        ),
        edge_strength_matrix.shape[0],
    ).reshape(
        edge_strength_matrix.shape[0], edge_strength_matrix.shape[0]
    ).T - np.linspace(
        0, edge_strength_matrix.shape[0] - 1, edge_strength_matrix.shape[0]
    ).reshape(
        edge_strength_matrix.shape[0], 1
    )

    # Take the max of each column for each row and reshape
    transition_probabilities = np.amax(
        np.absolute(transition_probabilities), axis=0
    ).reshape(1, edge_strength_matrix.shape[0]) - np.absolute(transition_probabilities)

    # Divide by sum to normalize
    transition_probabilities = transition_probabilities / np.sum(
        transition_probabilities, axis=0
    ).reshape(1, edge_strength_matrix.shape[0])

    # Add a small value to avoid log infinity errors
    transition_probabilities += 0.00001

    # Setup Emission probabilities
    emission_probabilities = edge_strength_matrix / np.amax(
        edge_strength_matrix, axis=0
    )

    # Add a small value to avoid log infinity errors
    emission_probabilities += 0.00001

    # If we're considering human feedback in our algorithm,
    # Make the column values essentially equal to zero
    # And make that particular pixel equal to 1
    if human:
        emission_probabilities[:, column] = 0.00000001
        emission_probabilities[row, column] = 1

    # All observable intermediate states
    intermediate_states = np.array([0 for _ in range(edge_strength_matrix.shape[0])])
    all_possible_paths = [str(path) for path in range(edge_strength_matrix.shape[0])]

    # Make a backup to act as a backtracking device
    intermediate_states_backup = intermediate_states.copy()

    for column in range(1, edge_strength_matrix.shape[1]):
        for row in range(edge_strength_matrix.shape[0]):
            intermediate_states[row] = np.amax(
                np.log(transition_probabilities[:, row])
                + intermediate_states_backup
                + np.log(emission_probabilities[row, column])
            )
            all_possible_paths[row] += " " + np.argmax(
                np.log(transition_probabilities[:, row])
                + intermediate_states_backup
                + np.log(emission_probabilities[row, column])
            ).astype(str)
        intermediate_states_backup = intermediate_states.copy()

    resultant_matrix = np.array(
        all_possible_paths[np.argmax(intermediate_states)].split(" ")
    ).astype(int)

    # Save for our records
    imageio.imwrite(
        "{0}/{1}_output_{2}.jpg".format(
            "human_viterbi" if human else "viterbi",
            filename.split(".")[0].split("/")[-1],
            "human" if human else "map",
        ),
        draw_edge(
            image=image, y_coordinates=resultant_matrix, color=(0, 255, 0), thickness=5
        ),
    )

    # Save for grading system
    imageio.imwrite(
        "output_human.jpg",
        draw_edge(
            image=image, y_coordinates=resultant_matrix, color=(0, 255, 0), thickness=5
        ),
    )

    return None

if __name__ == "__main__":

    if not os.path.exists("viterbi"):
        os.makedirs("viterbi")

    if not os.path.exists("bayes_net"):
        os.makedirs("bayes_net")

    if not os.path.exists("human_viterbi"):
        os.makedirs("human_viterbi")

    (input_filename, gt_row, gt_col) = sys.argv[1:]

    # load in image
    input_image_bayes_net = Image.open(input_filename)
    input_image_viterbi = Image.open(input_filename)
    input_image_viterbi_with_log = Image.open(input_filename)

    # compute edge strength mask
    edge_strength = get_edge_strength(input_image_bayes_net)
    imageio.imwrite(
        "edges.jpg", np.uint8(255 * edge_strength / (np.amax(edge_strength)))
    )

    # Module 1: Bayes Net
    bayes_net(
        edge_strength_matrix=edge_strength,
        filename=input_filename,
        image=input_image_bayes_net,
    )

    # Module 2: Viterbi
    viterbi(
        edge_strength_matrix=edge_strength,
        filename=input_filename,
        image=input_image_viterbi,
    )

    # Module 3: Viterbi with human feedback
    viterbi_with_logs(
        edge_strength_matrix=edge_strength,
        filename=input_filename,
        image=input_image_viterbi_with_log,
        row=int(gt_row),
        column=int(gt_col),
        human=True,
    )


from tkinter import *
import os
import ntpath

from pywidgets.tk.func import bind_all_childes
from pywidgets.tk.Lables.__origen import _EXPLORE
from pywidgets.tk.Verticle_Frame import VerticalScrolledFrame
from PIL import ImageTk,Image

FILE_PATH=os.path.dirname(__file__)
RIGHTARROW=Image.open(os.path.join(FILE_PATH,"icons\\go_right.png"))
DOWNARROW=Image.open(os.path.join(FILE_PATH,"icons\\down-arrow2.png"))

class Explorer(_EXPLORE,VerticalScrolledFrame):
    def __init__(self, app, scrolling=False, *args, **kw):
        super().__init__(app, *args, **kw)
        VerticalScrolledFrame.__init__(self,app,scrolling=scrolling,*args,**kw)
        self.interior.columnconfigure(0,weight=1)
        self._lastactive=None
    def _get_item(self,title,**kwargs):
        return Title_Explorebar(self.interior,self,title,True,bd=0,**kwargs)
    def _append_child(self,k,frame_container:Label):
        last_column = self.interior.grid_size()[1]
        frame_container.grid(row=last_column,column=0,sticky=NSEW)
        #to insert the same number
        if k in range(len(self)):
            list.__setitem__(self,k,frame_container)
        else:
            list.append(self,frame_container)
        self.disabelall(frame_container)
        return frame_container
    def add_item(self,k,value,**kwargs):
        item=super().add_item(k,value,**kwargs)
        self.disabelall(item)
        return item
    def disabelall(self,excepted:str=None):
        bar=super().disabelall(excepted)
        _change=self._lastactive!=bar
        if False:
            for label in self:
                if not label is bar:
                    label.disabelall()
            self._lastactive=bar

        return bar,_change

"""
expanded Frame whcih present number or names of image
"""
class Title_Explorebar(_EXPLORE):
    def _create(self,the_title,rightarrow:Image=RIGHTARROW,downnarrow:Image=DOWNARROW):
        self.iconframe=Label(self.title_frame,compound=LEFT)
        self.text_title=Label(self.title_frame,text=the_title)

        self.resized=rightarrow.resize((20,20),Image.ANTIALIAS)
        self.rightarrow = ImageTk.PhotoImage(self.resized)

        self.downarrow=downnarrow.resize((20,20),Image.ANTIALIAS)
        self.downarrow = ImageTk.PhotoImage(self.downarrow)

        self.iconframe.grid(row=0,column=0,sticky=NE)
        self.text_title.grid(row=0,column=1,sticky=NSEW)

        bind_all_childes(self.iconframe,lambda e:self._hide(),"<Button-1>")
    def recreate(self):
        #getting the name of the path
        the_title=str(ntpath.basename(self.title)).split(".")[0]
        for child in self.title_frame.winfo_children():
            child.destroy()
        #creating the file:
        self._create(the_title)
    def __init__(self,app,parent_title:Explorer,title:str,defultstate=True,
        style_child_active:dict={},style_hover_active:dict={},
        style_child_disable:dict={},style_hover_disabled:dict={},
        *args,**kwargs):
        """title is full path or normal title
        defult state to the frist child added
        """
        super().__init__(app, *args, **kwargs)
        self.title=title
        self.number=0

        self._lastactive=None
        #parent title to disable other unselected images in the scene
        self.parent=parent_title

        self.style_child_active=style_child_active
        self.style_child_disable=style_child_disable
        self.style_hover_active=style_hover_active
        self.style_hover_disabled=style_hover_disabled

        self.rowconfigure(1,weight=1)
        self.columnconfigure(0,weight=1)
        self.title_frame=Frame(self,*args,**kwargs)
        self.containerframe=Frame(self)
        #getting wedget swished out with frame
        self.title_frame.rowconfigure(1,weight=1)
        self.recreate()

        self.state=defultstate
        self.title_frame.grid(row=0,column=0,sticky=NSEW)
        #getting child container frame expand by clicking Frame

        self._hide()

    def _hide(self):
        self.state = not self.state
        if self.state:
            self.containerframe.grid(row=1,column=0,sticky=NSEW)
            self.iconframe.config(image=self.downarrow)
        else:
            self.iconframe.config(image=self.rightarrow)
            self.containerframe.grid_forget()

    def disabelall(self,excepted:int=None):
        bar=super().disabelall(excepted)
        _change=self._lastactive!=self.active_child
        if _change:
            for bar in self:
                if bar is bar:
                    bar.config(**self.style_child_active)
                    self.parent.disabelall(self)
                else:
                    bar.config(**self.style_child_disable)
            self._lastactive=self.active_child
        return bar,_change
    def add_item(self,title=None,**kwargs):
        frame=self._get_item(title=title,**kwargs)
        self._append_child(frame)
        self.disabelall(frame)
        #it will be used in installation
        return frame

    def _get_item(self,title=None,**kwargs):
        """get origion label to define
           you can change this in your subclass
            """
        title=title if not title is None else str(self.number)
        self.number+=1
        frame_container=Label(self.containerframe,text=str(title),**kwargs)
        return frame_container
    def _append_child(self,frame_container:Label):
        last_column = self.containerframe.grid_size()[1]
        frame_container.grid(row=last_column,column=0,sticky=NSEW)
        #to insert the same number

        list.append(self,frame_container)
        self.disabelall(frame_container)

        self._bind_child(frame_container)
        return frame_container
    def _bind_child(self,frame_container):
        def Hovering(enter):
            index=self.index(frame_container)
            if enter:
                frame_container.config(**self.style_hover_active)
            elif self.active_child!=index:
                frame_container.config(**self.style_hover_disabled)

        bind_all_childes(frame_container,lambda e:Hovering(True),"<Enter>")
        bind_all_childes(frame_container,lambda e:Hovering(False),"<Leave>")
        bind_all_childes(frame_container,lambda e:self._active_child(frame_container),"<Button-1>")
        return frame_container

def main():
    root =Tk()
    the_list=Explorer(root)
    root.columnconfigure(1,weight=1)
    the_list.interior["bg"]="blue"
    the_list["bg"]="blue"
    the_list[0]="Yes"
    the_list[0].add_item("1dfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfs")
    the_list[0].add_item("2")
    the_list[0].add_item("3")
    the_list[1]="no"
    the_list[1].add_item("3")
    the_list[3]="okey"

    the_list.grid(row=0,column=0,sticky=NSEW)
    Frame(bg="red").grid(row=0,column=1,sticky=NSEW)
    root.mainloop()
if __name__=="__main__":
    main()


""" get_top_bottom_movies.py
Usage: get_top_bottom_movies
Return top and bottom 10 movies, by ratings.
"""
import sys
import imdb
i = imdb.IMDb()
top250 = i.get_top250_movies()
bottom100 = i.get_bottom100_movies()

out_encoding = sys.stdout.encoding or sys.getdefaultencoding()

for label, ml in [('top 10', top250[:10]), ('bottom 10', bottom100[:10])]:

    print ('%s movies' % label)
    print ('rating\tvotes\ttitle')
    for movie in ml:
        outl = u'%s\t%s\t%s' % (movie.get('rating'), movie.get('votes'),
                                    movie['long imdb title'])
        print (outl.encode(out_encoding, 'replace'))


import numpy as np
import math
import h5py
import openpyxl
from scipy import signal as signal
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score, recall_score
from sklearn.externals import joblib
import joblib

NegativeThreshold = 15
PositiveThreshold = 5
Threshold =  NegativeThreshold + PositiveThreshold
TimeWidth = 32                    #时间窗长度
BreakTime = 30
InvalidTime = 5
standard = {                      #归一化基准
            'ip': 225,
            'Bt': 2.05,
            'axuv_ca_01': 1.3,
            'sxr_cb_024': 1.5,
            'sxr_cc_049': 4,
            'vs_c3_aa001': 5,
            'vs_ha_aa001': 1,
            'exsad1': 6, 'exsad2': 2.5, 'exsad4': 1.3, 'exsad7': 4, 'exsad8': 1, 'exsad10': 6,
            'Ivfp': 3,
            'Ihfp': 1,
            'MA_POL_CA01T': 2, 'MA_POL_CA02T': 2, 'MA_POL_CA03T': 2, 'MA_POL_CA05T': 2, 'MA_POL_CA06T': 2,
            'MA_POL_CA07T': 2, 'MA_POL_CA19T': 2, 'MA_POL_CA20T': 2, 'MA_POL_CA21T': 2, 'MA_POL_CA22T': 2,
            'MA_POL_CA23T': 2, 'MA_POL_CA24T': 2
            }

#训练信号
lf_tags = [
            r'\ip',
            r'\Bt',
            r'\axuv_ca_01',
            r'\sxr_cb_024',
            r'\vs_c3_aa001',
            r'\vs_ha_aa001',
            r'\sxr_cc_049',
            r'\exsad1', r'\exsad2', r'\exsad4', r'\exsad7', r'\exsad8', r'\exsad10',
            r'\Ivfp', r'\Ihfp'
        ]

hf_tags = [
            r'\MA_POL_CA01T', r'\MA_POL_CA02T', r'\MA_POL_CA03T', r'\MA_POL_CA05T', r'\MA_POL_CA06T',
            r'\MA_POL_CA07T',r'\MA_POL_CA19T', r'\MA_POL_CA20T', r'\MA_POL_CA21T', r'\MA_POL_CA22T', r'\MA_POL_CA23T', r'\MA_POL_CA24T'
        ]
all_tags =  lf_tags + hf_tags
for i in range(len(all_tags)):       #去掉\
    all_tags[i] = all_tags[i][1:]

#对10Hz采样的信号进行加工处理
def process_10Hz(data):
    processed_data = []
    section = math.ceil(len(data) / 10)
    for j in range(section):
        if j < (section - 1):
            mean = np.mean(data[(10 * j) :(10 * j + 10)])
            processed_data.append(mean)
        else:
            mean = np.mean(data[(10 * j):])
            processed_data.append(mean)
    return processed_data

#对250Hz采样的信号进行加工处理
def process_250Hz(data):
    processed_data = []
    section = math.ceil(len(data) / 250)
    for j in range(section):
        if j < (section - 1):
            mean = np.mean(data[(250 * j):(250 * j + 250)])
            processed_data.append(mean)
        else:
            mean = np.mean(data[(250 * j):])
            processed_data.append(mean)
    return processed_data

#对500Hz采样的信号进行加工处理
def process_500Hz(data):
    processed_data = []
    section = math.ceil(len(data) / 500)
    for j in range(section):
        if j < (section - 1):
            mean = np.mean(data[(500 * j):(500 * j + 500)])
            processed_data.append(mean)
        else:
            mean = np.mean(data[(500 * j):])
            processed_data.append(mean)
    return processed_data

#下降时间
def down_time(data):
    ip = data
    ba = signal.butter(8, 0.04, 'lowpass')
    ip_ = signal.filtfilt(ba[0], ba[1], ip)
    start = 0
    end = 0
    for i in range(len(ip_ - 640)):
        if ip_[i] > ip_[i + 20] > ip_[i + 40] and ip_[i] * 0.95 > ip_[i + 160] and ip_[i] * 0.9 > ip_[i + 320] \
                and ip_[i] * 0.8 > ip_[i + 640]:
            start = i
            break
    for i in range(start, len(ip_ - 640)):
        if ip_[i] <= 10:
            end = i
            break
    if start and end:
        if end - start < 600:
            shot_label = -1
        else:
            shot_label = 1
    k = []
    for i in range(190):
        k_i = (data[50 * i + 49] - data[50 * i]) / 0.005
        k.append(k_i)
    if shot_label == 1:
        # print("此炮为正常炮")
        DownTime = (start + 500) / 10000
        DownData = data[start]
    else:
        # print("此炮为破裂炮")
        for i in range(30, 190):
            if (k[i] > -4000) and (k[i + 1] < -4000):
                for j in range(50):
                    error = data[50 * i + 50 + j + 1] - data[50 * i + 50 + j]
                    if error < -5:
                        DownTime = ((50 * i + 50 + j) / 10000) + 0.05
                        down_data = data[50 * i + 50 + j]
                        break
                break
    return  DownTime, shot_label

def reduce_sampling(shot):
    reduce_sampling_signal = {}
    g = h5py.File(r"/home/axk/chipsvm/data/{}.hdf5".format(shot), 'r')
    dataset = g.get("ip")
    data = list(dataset["data"])
    time = list(dataset["time"])
    DownTime, shot_flag = down_time(data)
    if shot_flag == -1:
        print("此炮为正常炮")
        shot_label = -1
        print("DownTime : {}".format(DownTime))
        for shottag in all_tags:
            dataset = g.get(shottag)
            data = np.array(dataset["data"])
            time = np.array(dataset["time"])
            data = data[time > 0.1]
            time = time[time > 0.1]
            data = data[time <= DownTime]
            time = time[time <= DownTime]
            datatemp = data[time < 0.11]
            if len(datatemp) < 200:
                processed_data = process_10Hz(data)
            elif len(datatemp) < 3000:
                processed_data = process_250Hz(data)
            else:
                processed_data = process_500Hz(data)
            reduce_sampling_signal.update({shottag:processed_data})
    else:
        print("此炮为破裂炮")
        shot_label = 1
        print("DownTime : {}".format(DownTime))
        for shottag in all_tags:
            dataset = g.get(shottag)
            data = np.array(dataset["data"])
            time = np.array(dataset["time"])
            data = data[time > 0.1]
            time = time[time > 0.1]
            data = data[time <= DownTime]
            time = time[time <= DownTime]
            datatemp = data[time < 0.11]
            if len(datatemp) < 200:
                processed_data = process_10Hz(data)
            elif len(datatemp) < 3000:
                processed_data = process_250Hz(data)
            else:
                processed_data = process_500Hz(data)
            reduce_sampling_signal.update({shottag: processed_data})
    g.close()
    return reduce_sampling_signal, shot_label

def time_chipping(shot):
    signal, shot_label = reduce_sampling(shot)
    data = signal["ip"]
    step = len(data) - TimeWidth + 1
    incident = np.mat(np.ones((step, 1)))
    for tag in all_tags:
        data = signal[tag]
        data = (np.array(data)) / (standard[tag])
        chip = []
        for i in range(step):
            chip.append(data[i: (i + TimeWidth)])
        chip = np.mat(chip)
        incident = np.hstack((incident, chip))
    incident = np.delete(incident, 0, axis=1)
    incident = np.array(incident)
    return incident

#获取测试炮和其对应的标签，正常炮200个，破裂炮200个
np.random.seed(4)
TestBreak = np.load(r"/home/axk/chipsvm/BreakTest.npy")
TestNormal = np.load(r"/home/axk/chipsvm/NormalTest.npy")
# TestBreak = np.random.choice(TestBreak, 200)
# TestNormal = np.random.choice(TestNormal, 200)
BreakLabel = []
for i in range(len(TestBreak)):
    BreakLabel.append(1)
BreakLabel = np.array(BreakLabel)

NormalLabel = []
for i in range(len(TestNormal)):
    NormalLabel.append(-1)
NormalLabel = np.array(NormalLabel)
TestTemp = list(TestBreak) + list(TestNormal)
LabelTemp = list(BreakLabel) + list(NormalLabel)
TrainIndex = np.arange(len(LabelTemp))
TrainIndex = np.random.permutation(TrainIndex)
test = []
label = []
for i in TrainIndex:
    test.append(TestTemp[i])
    label.append(LabelTemp[i])
label = np.array(label)
print("label : ")
print(len(label))

#对测试炮进行预测
clf = joblib.load(r"/home/axk/chipsvm/SVMmodal/svmModel1c0.1.plk")
gamma = 1
C = 0.1
name = "shot" + str(gamma) + "vs" + str(C)
n = 1
shotlabel = []
Advance = []
for shot in test:
    flag = -1
    chip = time_chipping(shot)
    chiplength = len(chip)
    print("第{}炮".format(n))
    n += 1
    print(chip.shape)
    ChipPredict = []
    for i in range(len(chip)):
        count_positive = 0
        count_negative = 1
        ChipPred_Label = clf.predict([chip[i]])
        ChipPredict.append(ChipPred_Label)
        if len(ChipPredict) > Threshold:
            Sect = np.array(ChipPredict[-Threshold : ])
            num = Sect[Sect == 1].size
            if (num/len(Sect)) > 0.5:
                if Sect[0] == 1:
                    for j in range(1,len(Sect)):
                        if ((Sect[j-1]==1)and(Sect[j]==1)):
                            count_negative += 1
                            if count_negative > NegativeThreshold:
                                flag = 1
                                break
                        elif (((Sect[j-1]==1)and(Sect[j]==-1))or((Sect[j-1]==-1)and(Sect[j]==-1))):
                            count_positive += 1
                            if count_positive >= PositiveThreshold:
                                flag = -1
                                count_negative = 0
                                break
                        elif ((Sect[j-1]==-1)and(Sect[j]==1)):
                            count_positive = 0
                            count_negative += 1
                            if count_negative > NegativeThreshold:
                                flag = 1
                                break
        if flag == 1:
            shotlabel.append(flag)
            AdvanceTime = chiplength - i - 1
            print("提前时间 ： {}".format(AdvanceTime))
            Advance.append(AdvanceTime)
            break
    if flag == -1:
        shotlabel.append(flag)
    print(shotlabel[-1])
print("shotlabel  :   {}".format(len(shotlabel)))
print(shotlabel)
print(label)
# 评价
print("返回给定测试集和对应标签的平均准确率")
shotlabel = np.array(shotlabel)
correct = 0
for i in range(len(label)):
        if int(label[i]) == int(shotlabel[i]):
            correct += 1
print(correct)
accuary = correct / len(shotlabel)
print(accuary)
print("混淆矩阵：")
conf = confusion_matrix(label, shotlabel)
print(conf)
print("precision : ")
precision = precision_score(label, shotlabel)
print(precision)
print("recall : ")
recall = recall_score(label, shotlabel)
print(recall)
print("FPR : ")
FPR = conf[0, 1] / (conf[0, 1] + conf[0, 0])
print(FPR)

result = [name,accuary,precision,recall,FPR,conf[0,0],conf[0,1],conf[1,0],conf[1,1]]
title = ["name","accuary","precision","recall","FPR","conf[0,0]","conf[0,1]","conf[1,0]","conf[1,1]"]
np.save(r"/home/axk/shotsvm/result/1c0.1/single/title.npy",title)
np.save(r"/home/axk/shotsvm/result/1c0.1/single/{}.npy".format(name),result)
Aname = "advance" + str(gamma) + "vs" + str(C)
np.save(r"/home/axk/shotsvm/result/1c0.1/single/{}.npy".format(Aname),Advance)


from celery.result import AsyncResult
from django.shortcuts import render
from django.views.decorators.http import require_GET, require_http_methods

from dnaStrings.forms import dnaStringSubmission
from dnaStrings.models import Tasks
from dnaStrings.tasks import findProtein
from django.contrib import messages

@require_http_methods(["GET", "POST"])
def dnaStringsView(request):
    if request.method == "POST":
        form = dnaStringSubmission(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            job_name = data["job_name"]
            dna_string = data["dna_string"]

            # send it to the celery worker (delay method)
            findProtein.delay(job_name=job_name, dna_string=dna_string)
            # generate a success method
            messages.success(
                request,
                f"Your job {job_name} was submitted. Click Monitor Jobs to see its progress.",
            )
            # return the same form back to user to enter more data
            return render(request, "submit.html", context={"form": dnaStringSubmission})
    else:
        return render(request, "submit.html", context={"form": dnaStringSubmission})

# polling function
def track_jobs():
    entries = Tasks.objects.all()
    information = []
    for item in entries:
        progress = 100  # max value for bootstrap progress bar, when  the job is finished
        result = AsyncResult(item.task_id)
        if isinstance(result.info, dict):
            progress = result.info["progress"]
        information.append(
            [item.job_name, item.dna_string, item.dna_result, result.state, progress, item.task_id]
        ) # could possibly be used dynamically
    return information

# monitor view
@require_GET
def monitor(request):
    info = track_jobs()
    return render(request, "monitor.html", context={"info": info})

# cancel completely cancels the task from the queue and stops evaluating it
@require_GET
def cancel_job(request, task_id=None):
    result = AsyncResult(task_id)
    result.revoke(terminate=True)
    info = track_jobs()
    return render(request, "monitor.html", context={"info": info})

# deletes the job from the database
@require_GET
def delete_job(request, task_id=None):
    a = Tasks.objects.filter(task_id=task_id)
    a.delete()
    info = track_jobs()
    return render(request, "monitor.html", context={"info": info})


'''
出现最多次的整数，输入多个逗号分隔的整数，输出出现最多次的整数的值以及对应的出现次数 1,1,3,4,5,7,11,3,3,3,4,6,8,9,2
'''
#输入
try:
    a=input('请输入整数，用“,”分割')#用中文，怎么办
    intlist=[]
    if ',' not in a:
        print(f'出现最多的数字是{int(a)}，出现次数为1次')
        exit()
    else:
        list = a.split(',')
        intlist = [int(i) for i in list]
except ValueError:
    print('请输入整数，多个整数请用,分割')
    exit()
except Exception:
    print('输入有误')
    exit()

#方法1
dict= {}
num=[]
temp=1
for i in intlist:
    if i not in dict.keys():
        dict[i]=1
    else:
        dict[i]=dict[i]+1
        #判断出现最多的次数temp
        if dict[i] > temp:
            temp=dict[i]
#循环遍历找到最多次数对应的key
for key,value in dict.items():
    if value == temp:
        num.append(key)
print(f'出现最多的数字是{num}，出现次数为{temp}次')

#方法2
dict= {}
num=[]
temp=1
for i in intlist:
    if i not in dict.keys():
        dict[i]=1
    else:
        dict[i]=dict[i]+1
    #判断出现最多的次数temp
    if dict[i] > temp:
        temp=dict[i]
        num.clear()
        num.append(i)
    elif dict[i] == temp:
        num.append(i)
print(f'出现最多的数字是{num}，出现次数为{temp}次')


from django.shortcuts import render, redirect
import random, datetime

# Create your views here.
def index(request):
	if 'activities' not in request.session:
		request.session['activities'] = []
	if 'gold_count' not in request.session:
		request.session['gold_count'] = 0
	return render(request, 'gold/index.html')

def process(request, location):
	if request.method == "POST":
		if 'gold_count' not in request.session:
			request.session['gold_count'] = 0
		if 'activities' not in request.session:
			request.session['activities'] = []

		locations = {
			"farm": ("won", random.randint(10, 20)),
			"cave": ("won", random.randint(5, 10)),
			"house": ("won", random.randint(2, 5)),
			"casino": ["", random.randint(-50, 50)],
		}

		for key, value in locations.items():
			if location == key:
				request.session['gold_count'] += value[1]
				if location == "casino":
					locations['casino'][0] = "won" if locations['casino'][1] >= 0 else "lost"
					if locations['casino'][0] == "won":
						exclam = "Nice"
						outcome = 1
					else:
						exclam = "Ouch"
						outcome = 0
					casino_str = "Entered the casino and {} {} golds... {}... {}".format(locations['casino'][0], locations['casino'][1], exclam, datetime.datetime.now())
					request.session['activities'].insert(0, (outcome, casino_str))
				else:
					outcome = 1
					string = "Earned {} golds from the {}! {}".format(value[1], location, datetime.datetime.now())
					request.session['activities'].insert(0, (outcome, string))
		return redirect('/')
	else:
		return redirect('/')


import sys
sys.path.append('../')
from src.luscioustwitch import *
from src.luscioustwitch.events import *
import json
import unittest

class TestTwitchAPI(unittest.TestCase):
  @classmethod
  def setUpClass(self):
    with open('../secrets.json', 'r') as f:
      cred_json = json.load(f)
      f.close()
    self.api = TwitchAPI({ "CLIENT_ID": cred_json['TWITCH']["CLIENT_ID"], "CLIENT_SECRET": cred_json['TWITCH']['CLIENT_SECRET'] })

  def test_get_user_id(self):
    user_id = self.api.get_user_id("lusciouslollipop")
    self.assertEqual(user_id, '82920215')

  def test_get_user_info(self):
    user_info = self.api.get_user_info('82920215')
    self.assertEqual(user_info['login'], 'lusciouslollipop')

  def test_get_channel_info(self):
    user_id = self.api.get_user_id("lusciouslollipop")
    channel_info = self.api.get_channel_info(user_id = user_id)

    self.assertEqual(channel_info['broadcaster_id'], user_id)
    self.assertEqual(channel_info['broadcaster_login'], "lusciouslollipop")

  def test_get_category_info(self):
    game_name = "Old School RuneScape"
    category_info = self.api.get_category_info(game_name)

    self.assertEqual(category_info['id'], '459931')
    self.assertEqual(category_info['name'], game_name)

  def test_get_category_id(self):
    game_name = "Old School RuneScape"
    category_id = self.api.get_category_id(game_name)

    self.assertEqual(category_id, '459931')

  def test_get_clip(self):
    clip_id = 'IgnorantEnergeticBananaHotPokket-9Rg1MRwc97VXfSzr'
    clip_info = self.api.get_clip(clip_id)

    self.assertEqual(clip_info['id'], clip_id)

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


#algorithm responsible for interpreting the hints

def sasta_ai(data:dict):
    c=[]
    for index,j in data.items():
        val=j[1]
        if j[0]=="slate":
            test=False
            for k in data.values():
                if k[1]==val and k[0]!="slate":
                    test=True
                    break
            if test==False:
                c.append(f"x.count('{val}')==0")
        if j[0]=="slate":
            c.append(f"x[{index}]!='{val}'")
        elif j[0]=="yellow":
            c.append(f"x[{index}]!='{val}'")
            count=0
            for k in data.values():
                if j[1]==k[1] and k[0]!="slate":
                    count+=1
            c.append(f"x.count('{val}')=={count}")
        elif j[0]=="green":
            c.append(f"x[{index}]=='{val}'")

    return ' and '+' and '.join(c)

if __name__=="__main__":
    #test
    print(sasta_ai({0:["yellow","1"],1:["slate","2"],2:["slate","+"],3:["slate","4"],4:["green","3"],5:["green","2"]}))


from rest_framework.views import APIView
from dj_rest_auth.views import AllowAny
from django.http import FileResponse, HttpRequest, HttpResponse, JsonResponse
from rest_framework.decorators import api_view
from rest_framework.request import Request
from tempfile import NamedTemporaryFile
from docxtpl import DocxTemplate
from drfauth.models import Document

from django.contrib.staticfiles.storage import staticfiles_storage

def write_file(data, filename):
    # Convert binary data to proper format and write it on Hard Disk
    with open("staticfiles/"+filename, 'wb') as file:
        file.write(data)
class PdfView(APIView):
    permission_classes=[AllowAny]
    def get(self,request):
        one = Document.objects.get(id=1)
        write_file(one.Doc_Content,one.FileName+"."+one.Extension)
        file_path = staticfiles_storage.path(one.FileName+"."+one.Extension)
        print(file_path)
        with open(file_path, 'rb') as pdf:
            response = HttpResponse(pdf.read(), content_type='application/pdf')
            response['Content-Disposition'] = 'attachment; filename="Instrumento de Evaluacion.pdf"'
            return response

class ReporteView(APIView):
    permission_classes=[AllowAny]
    def post(self,request:Request):
        one = Document.objects.get(id=2)
        write_file(one.Doc_Content,one.FileName+"."+one.Extension)
        file_path = staticfiles_storage.path(one.FileName+"."+one.Extension)
        template = DocxTemplate(file_path)
        contextVars = request.data.copy()
        template.render(contextVars)
        template.save("staticfiles/informe"+"."+one.Extension)
        file_path = staticfiles_storage.path("informe" +"."+one.Extension)
        with open(file_path, 'rb') as pdf:
            response = HttpResponse(pdf.read(), content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
            response['Content-Disposition'] = 'attachment; filename="Infome.docx"'
            return response
        # return JsonResponse(request.data,safe=False)

class Reporte2View(APIView):
    permission_classes=[AllowAny]
    def post(self,request:Request):
        one = Document.objects.get(id=3)
        write_file(one.Doc_Content,one.FileName+"."+one.Extension)
        file_path = staticfiles_storage.path(one.FileName+"."+one.Extension)
        template = DocxTemplate(file_path)
        contextVars = request.data.copy()
        template.render(contextVars)
        template.save("staticfiles/reporte"+"."+one.Extension)
        file_path = staticfiles_storage.path("reporte" +"."+one.Extension)
        with open(file_path, 'rb') as pdf:
            response = HttpResponse(pdf.read(), content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
            response['Content-Disposition'] = 'attachment; filename="reporte.docx"'
            return response
        # return JsonResponse(request.data,safe=False)


#!/usr/bin/python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from datetime import datetime
import weasyprint
import shutil
import os
import time
import json

def nfe_read(driver):
	nfe_dict = {}
	for fieldset in driver.find_elements_by_tag_name("fieldset"):
		try:
			legend = fieldset.find_element_by_tag_name("legend")
			table = fieldset.find_element_by_tag_name("table")
			tbody = table.find_element_by_tag_name("tbody")
			section = legend.get_attribute('textContent').strip()
			section_dict = {}
			for tr in tbody.find_elements_by_tag_name("tr"):
				for td in tr.find_elements_by_tag_name("td"):
					try:
						label = td.find_element_by_tag_name("label")
						span = td.find_element_by_tag_name("span")
						attribute = label.get_attribute('textContent').strip()
						value = span.get_attribute('textContent').strip()
						section_dict[attribute] = value
					except BaseException as Argument:
						continue;
			if len(section_dict) > 0:
				nfe_dict[section] = section_dict
		except BaseException as Argument:
			continue;

	return nfe_dict

def download_html(driver, filename):
	driver.find_element_by_id("ctl00_ContentPlaceHolder1_hplImprimir").click()
	driver.switch_to_window(driver.window_handles[1])
	with open(filename, "w") as f:
	    f.write(driver.page_source)

def nfe_navigate(driver, chave):
	driver.get("http://www.nfe.fazenda.gov.br/portaL/consultaRecaptcha.aspx")
	driver.find_element_by_link_text("Serviços").click()
	driver.find_element_by_link_text("Consultar NF-e Completa").click()
	txtChave=driver.find_element_by_id('ctl00_ContentPlaceHolder1_txtChaveAcessoCompleta')
	txtChave.send_keys(chave)
	try:
		element = WebDriverWait(driver, 120).until(
			EC.presence_of_element_located((By.ID, "ctl00_ContentPlaceHolder1_btnVoltar"))
		)
		print("Detected NF-E screen, waiting it to load")
		time.sleep(1)
	except:
		print("Timeout on NF-E captcha screen")

def validar_chave(chave):
	n = 2;
	soma = 0
	digitos =  list(chave);
	if len(digitos) != 44:
		print("Comprimento inválido (%s)" % len(digitos))
		return False;

	for i in digitos[0:43]:
		soma = soma + int(i) * (n + 2)
		n = (n + 7) % 8

	resto = (soma % 11)
	if resto == 0 or resto == 1:
		dv = 0
	else:
		dv = 11 - resto

	if (dv == int(digitos[-1])):
		return True
	else:
		print("Digito de verificação inválido %s != %s" % (dv, int(digitos[-1])))
		return False

def decode_chave(chave):
	if not validar_chave(chave):
		print("Chave invalida")
		return None;

	self = {}
	self["UF"] = chave[0:2]
	self["YY"] = chave[2:4]
	self["MM"] = chave[4:6]
	self["cnpj"] = chave[6:20]
	self["model"] = chave[20:22]
	self["serie"] = chave[22:25]
	self["numero"] = chave[25:34]
	self["emissao"] = chave[35]
	self["codigo"] = chave[35:43];
	self["dv"] = chave[43];
	return self

def nfe_download_json(driver, filename):
	data = nfe_read(driver)
	print(data["Dados da NF-e"]["Série"])
	print(data["Dados da NF-e"]["Número"])
	print(data["Dados da NF-e"]["Data de Emissão"])
	print(data["Dados da NF-e"]["Valor Total da Nota Fiscal"])
	print("Saving it")
	with open(filename, 'w') as f:
		json.dump(data, f)

def cache_path(chave, ext):
	chave_fields = decode_chave(chave)
	if chave_fields is None:
		return False

	return "cache/%s/%s/%s.%s" % (chave_fields["YY"], chave_fields["MM"], chave, ext)

def nfe_download_to_cache(chave):

	chave_fields = decode_chave(chave)
	if chave_fields is None:
		return False

	newpath = "cache/%s/%s" % (chave_fields["YY"], chave_fields["MM"])
	if not os.path.exists(newpath):
	    shutil.copytree("cache/css", newpath + "/css")

	if os.path.isfile(cache_path(chave, "json")) and os.path.isfile(cache_path(chave, "html")):
		print("%s hit cache" % chave)
		return True

	print("Starting selenium with Firefox")
	driver = webdriver.Firefox()

	nfe_navigate(driver, chave)

	print("Downloading values")
	nfe_download_json(driver, cache_path(chave, "json"))

	print("Saving SnapShot")
	download_html(driver, cache_path(chave, "html"))

	print("Finishing section")
	driver.quit()

	return True

def download_json(chave):
	if not nfe_download_to_cache(chave):
		return None

	with open(cache_path(chave, "json"), 'r') as f:
		return json.load(f)

def nfe_create_pdf(chave):
	if not nfe_download_to_cache(chave):
		return
	pdf = weasyprint.HTML(cache_path(chave, "html")).write_pdf()
	with open(cache_path(chave, "pdf"), 'wb') as f:
		f.write(pdf)

	return cache_path(chave, "pdf")


from AIPUBuilder.Optimizer.utils import *
from AIPUBuilder.Optimizer.framework import *
from AIPUBuilder.Optimizer.ops.rnn import *
from AIPUBuilder.Optimizer.ops.conv import clear_lower_bits_for_bias
from AIPUBuilder.Optimizer.logger import *
import torch.nn as nn

split_weights_name = ['wx_gk', 'wh_gk', 'wx_ck', 'wh_ck']

def list_equal(left, right):
    if isinstance(left, list) and isinstance(right, list):
        return left == right
    return False

@op_register(OpType.GRUv3)
def gruv3(self, *args):
    inp0 = self.inputs[0]
    inp1 = self.inputs[1]

    current_batch_idx = self.current_batch_idx
    input_seq = inp0.betensor.float()
    initial_batch = self.inputs[1].betensor.shape[0]
    batch_size = inp0.betensor.shape[0]
    time_step = self.get_param('time_steps')
    input_size = self.get_param('input_size')
    cell_size = self.get_param('cell_size')
    direction = self.get_param('direction')
    threshold = self.get_param('threshold', optional=True, default_value=float('inf'))

    if direction == 'reverse':
        input_seq = torch.flip(input_seq, [1])

    initial_H = inp1.betensor.float()
    start_data_idx = self.current_batch_idx * batch_size

    start_initial_idx = start_data_idx % initial_batch
    in_state = initial_H[start_initial_idx: start_initial_idx + 1]
    for initial_idx in range(1, batch_size):
        current_initial_idx = (start_data_idx + initial_idx) % initial_batch
        in_state = torch.cat((in_state, initial_H[current_initial_idx: current_initial_idx + 1]), dim=0)

    w = self.constants["weights"].betensor.float()
    bias = self.constants['biases'].betensor.float()
    out_sequence = self.get_param('out_sequence')
    activations_list = self.get_param('activations') \
        if 'activations' in self.params else ['SIGMOID', 'TANH']

    state_batch = torch.zeros([batch_size, time_step, cell_size], device=inp0.betensor.device)
    state_last = torch.zeros([batch_size, cell_size], device=inp0.betensor.device)

    weights = w.permute(1, 0).float()
    gates_kernel = weights[:, :2 * cell_size]
    candidate_kernel = weights[:, 2 * cell_size:]

    if not self.quantized:
        # save weights to placeholder for caculating max_key_axis and min_key_axis through per-channel
        f_lut_in = torch.zeros([batch_size, time_step, 2*cell_size], device=inp0.betensor.device)
        f_lut_out = torch.zeros([batch_size, time_step, 2*cell_size], device=inp0.betensor.device)
        g_lut_in = torch.zeros([batch_size, time_step, cell_size], device=inp0.betensor.device)
        g_lut_out = torch.zeros([batch_size, time_step, cell_size], device=inp0.betensor.device)
        hidden_gate_out = torch.zeros([batch_size, time_step, cell_size], device=inp0.betensor.device)

        wx_gk = gates_kernel[0:input_size, :].permute(1, 0)
        wh_gk = gates_kernel[input_size:, :].permute(1, 0)
        wx_ck = candidate_kernel[0:input_size, :].permute(1, 0)
        wh_ck = candidate_kernel[input_size:, :].permute(1, 0)
        for idx, weights_name in enumerate(split_weights_name):
            if weights_name not in self.constants:
                split_weights = eval(weights_name)
                self.constants[weights_name] = PyTensor(
                    self.name+"/constants"+str(idx), split_weights.cpu().numpy().astype(dtype2nptype(Dtype.FP32)))
                self.constants[weights_name].betensor = split_weights
                self.constants[weights_name].ir_shape = TensorShape(list(split_weights.cpu().numpy().shape))
                self.constants[weights_name].ir_dtype = self.constants[weights_name].dtype

        gates_bias = bias[: 2 * cell_size]
        candidate_bias = bias[2 * cell_size: 3 * cell_size]
        for b in range(batch_size):
            state = torch.unsqueeze(in_state[b], dim=0)
            state_all = torch.zeros([1, cell_size], device=inp0.betensor.device, dtype=torch.float)
            for ts in range(time_step):
                in_ts = torch.reshape(input_seq[b, ts, :], (-1, input_size))
                if 'version' in self.params and self.params['version'] == "GRUV1":

                    gate_kernel_x_f = gates_kernel[:input_size, :]
                    gate_kernel_h_f = gates_kernel[input_size:, :]
                    candidate_kernel_x_f = candidate_kernel[:input_size, :]
                    candidate_kernel_h_f = candidate_kernel[input_size:, :]

                    hidden_bias = bias[3 * cell_size:]
                    h_by_wg_f = torch.matmul(state, gate_kernel_h_f)
                    x_by_wg_f = torch.add(torch.matmul(in_ts, gate_kernel_x_f),
                                          torch.unsqueeze(gates_bias, 0))

                    mat_sum_f = x_by_wg_f + h_by_wg_f
                    mat_sum_f = torch.clamp(mat_sum_f, -threshold, threshold)
                    f_lut_in[b, ts, :] = torch.squeeze(mat_sum_f, 0)
                    sig_lut_out_f = g_rnn_activation_func[activations_list[0]][1](mat_sum_f)
                    f_lut_out[b, ts, :] = torch.squeeze(sig_lut_out_f, 0)
                    r_f, u_f = torch.chunk(sig_lut_out_f, 2, dim=1)

                    h_by_wc_f = torch.matmul(state, candidate_kernel_h_f)
                    h_by_wc_f = torch.add(h_by_wc_f, torch.unsqueeze(hidden_bias, 0))
                    hidden_gate_out[b, ts, :] = torch.squeeze(h_by_wc_f)
                    r_hwc_f = torch.multiply(r_f, h_by_wc_f)
                    x_by_wc_f = torch.add(torch.matmul(in_ts, candidate_kernel_x_f), torch.unsqueeze(candidate_bias, 0))
                    mat_sum2_f = x_by_wc_f + r_hwc_f
                    mat_sum2_f = torch.clamp(mat_sum2_f, -threshold, threshold)
                    g_lut_in[b, ts, :] = torch.squeeze(mat_sum2_f, 0)
                    c_f = g_rnn_activation_func[activations_list[1]][1](mat_sum2_f)
                    g_lut_out[b, ts, :] = torch.squeeze(c_f, 0)
                    state = torch.multiply((1.0 - u_f), c_f) + torch.multiply(u_f, state)
                else:  # GRUV3
                    gate_input = torch.cat((in_ts, state), dim=1)
                    sum0 = torch.add(torch.matmul(gate_input, gates_kernel), torch.unsqueeze(gates_bias, 0))  # [1,1024]
                    sum0 = torch.clamp(sum0, -threshold, threshold)
                    f_lut_in[b, ts, :] = torch.squeeze(sum0, 0)
                    f_out = g_rnn_activation_func[activations_list[0]][1](sum0)
                    f_lut_out[b, ts, :] = torch.squeeze(f_out, 0)
                    r, u = torch.chunk(f_out, 2, dim=1)
                    state_r = torch.multiply(state, r)

                    candidate_input = torch.cat((in_ts, state_r), dim=1)
                    sum1 = torch.add(torch.matmul(candidate_input, candidate_kernel),
                                     torch.unsqueeze(candidate_bias, 0))
                    sum1 = torch.clamp(sum1, -threshold, threshold)
                    g_lut_in[b, ts, :] = torch.squeeze(sum1, 0)
                    c = g_rnn_activation_func[activations_list[1]][1](sum1)
                    g_lut_out[b, ts, :] = torch.squeeze(c, 0)
                    state = torch.multiply((1.0 - u), c) + torch.multiply(u, state)
                state_all = state if ts == 0 else torch.cat((state_all, state), dim=0)

            state_last = state if b == 0 else torch.cat((state_last, state), dim=0)
            state_all = torch.unsqueeze(state_all, dim=0)
            state_batch = state_all if b == 0 else torch.cat((state_batch, state_all), dim=0)
        placeholders_list = ['state_batch', 'f_lut_in', 'f_lut_out', 'g_lut_in', 'g_lut_out', 'hidden_gate_out']
        if len(self.placeholders) < len(placeholders_list):
            for placeholder_name in placeholders_list:
                ph = PyTensor(self.name + '/' + placeholder_name,
                              eval(placeholder_name).cpu().numpy().astype(dtype2nptype(Dtype.FP32)))
                self.placeholders.append(ph)
        for idx, placeholder_name in enumerate(placeholders_list):
            self.placeholders[idx].betensor = eval(placeholder_name)

    else:
        dtype = dtype2str(self.outputs[0].dtype)
        q_bits_activation = dtype2bits(self.outputs[0].dtype)
        if dtype not in ['int8', 'int16']:
            OPT_FATAL("Currently gruv3/gruv1 only  support quantization bits  of activations is 8 or 16")
        qmin, qmax = bits2range(q_bits_activation, True)

        lut_in_bits = self.inputs[0].qbits
        lut_out_bits = self.outputs[0].qbits

        biases = torch.squeeze(bias)
        gates_bias_q = biases[: 2 * cell_size]
        candidate_bias_q = biases[2 * cell_size:3 * cell_size]
        if 'version' in self.params and self.params['version'] == "GRUV1":
            hidden_bias_q = biases[3 * cell_size:]

        lut_names = ['lut_rt', 'lut_zt', 'lut_ht']
        rt_table = self.constants['lut_rt'].betensor
        zt_table = self.constants['lut_zt'].betensor
        ht_table = self.constants['lut_ht'].betensor

        wx_gk_q = gates_kernel[0:input_size, :]
        wh_gk_q = gates_kernel[input_size:, :]
        wx_ck_q = candidate_kernel[0:input_size, :]
        wh_ck_q = candidate_kernel[input_size:, :]
        if 'scale_value' in self.params:
            scale = torch.tensor(self.params["scale_value"], device=inp0.betensor.device)
            shift = torch.tensor(self.params["shift_value"], device=inp0.betensor.device)
        elif "scale" in self.constants:
            requant_scale = self.constants["scale"].betensor
            requant_shift = self.constants["shift"].betensor
            gk_step = wx_gk_q.shape[1]
            ck_step = wx_ck_q.shape[1]
            step_length = [gk_step, gk_step, ck_step, ck_step, 1, 1]
            start_idx = 0
            scale = []
            shift = []
            for idx, step in enumerate(step_length):
                scale.append(requant_scale[start_idx: start_idx + step].to(inp0.betensor.device))
                shift.append(requant_shift[start_idx: start_idx + step].to(inp0.betensor.device))
                start_idx += step
        if dtype == 'int8':
            act_qmax = 2 ** 31 - 1
            act_qmin = -2 ** 31
        elif dtype == 'int16':
            act_qmax = 2 ** 47 - 1
            act_qmin = -2 ** 47
            qmax = 2**15 - 1
            qmin = -qmax

        for b in range(batch_size):
            state = torch.unsqueeze(in_state[b], dim=0).float()
            state_all = torch.zeros([1, cell_size], device=inp0.betensor.device, dtype=torch.float)
            for ts in range(time_step):
                in_ts = torch.reshape(input_seq[b, ts, :], (-1, input_size)).float()
                x_by_wg = torch.matmul(in_ts, wx_gk_q)
                h_by_wg = torch.matmul(state, wh_gk_q)
                re_scaled_h_by_wg = linear_requantize(h_by_wg, scale[0], shift[0], 0, act_qmin, act_qmax)

                mat_sum = x_by_wg + re_scaled_h_by_wg + torch.unsqueeze(gates_bias_q, 0)
                rescaled_mat_sum = linear_requantize(mat_sum, scale[1], shift[1], 0, qmin, qmax)
                rt_zt_lut_out = lookup_lut_powerof2(rescaled_mat_sum, rt_table, lut_in_bits, True, lut_out_bits, True)
                r, u = torch.chunk(rt_zt_lut_out, 2, dim=1)

                x_by_wc = torch.matmul(in_ts, wx_ck_q)
                if 'version' in self.params and self.params['version'] == "GRUV1":
                    hidden_scale = self.params["hidden_scale_value"]
                    hidden_shift = self.params["hidden_shift_value"]
                    h_by_wc = torch.add(torch.matmul(state, wh_ck_q), torch.unsqueeze(hidden_bias_q, 0))
                    h_by_wc = linear_requantize(h_by_wc, hidden_scale, hidden_shift, 0, qmin, qmax)
                    h_by_wc = torch.multiply(r, h_by_wc)
                else:
                    factor = 256.0 if dtype == 'int8' else 65536.0
                    hprev_r = torch.round(torch.div(torch.multiply(r, state), factor))
                    h_by_wc = torch.matmul(hprev_r, wh_ck_q)
                re_scaled_h_by_wc = linear_requantize(h_by_wc, scale[2], shift[2], 0, act_qmin, act_qmax)
                mat_sum2 = x_by_wc + re_scaled_h_by_wc + torch.unsqueeze(candidate_bias_q, 0)

                rescaled_mat_sum2 = linear_requantize(mat_sum2, scale[3], shift[3], 0, qmin, qmax)
                c = lookup_lut_powerof2(rescaled_mat_sum2, ht_table, lut_in_bits, True, lut_out_bits, True)
                max_pre = qmax
                c_times_1_minus_u = (max_pre - u) * c
                rescaled_c_times_1_minus_u = linear_requantize(
                    c_times_1_minus_u, scale[4], shift[4], 0, act_qmin, act_qmax)

                state = rescaled_c_times_1_minus_u + u * state
                state = linear_requantize(state, scale[5], shift[5], 0, qmin, qmax)
                state_all = state if ts == 0 else torch.cat((state_all, state), dim=0)
            state_last = state if b == 0 else torch.cat((state_last, state), dim=0)
            state_all = torch.unsqueeze(state_all, dim=0)
            state_batch = state_all if b == 0 else torch.cat((state_batch, state_all), dim=0)

    if direction == 'reverse':
        state_batch = torch.flip(state_batch, [1])

    if list_equal(out_sequence, ['H', 'Hn']):
        self.outputs[0].betensor = state_batch
        self.outputs[1].betensor = state_last
        return (state_batch, state_last)
    elif list_equal(out_sequence, ['Hn']):
        self.outputs[0].betensor = state_last
        return state_last
    else:
        self.outputs[0].betensor = state_batch
        return state_batch

def adaptation_weights_for_unify_shifts_for_aiff(node, weights_name, multiplicator):
    w_ele_t = node.constants[weights_name]
    w_ele = w_ele_t.betensor
    do_scale, do_scale_type, do_shift, do_shift_type = unify_shifts_for_aiff_with_per_n_channel_quant(
        node, w_ele_t, node.attrs["q_mode_weight"], node.attrs["q_bits_weight"], True, lambda xt: multiplicator/xt.scale)
    scale_zp_shape = [w_ele.shape[0]] + [1 for i in range(len(w_ele.shape) - 1)]
    w_scale_expand = w_ele_t.scale.reshape(scale_zp_shape)
    w_zerop_expand = w_ele_t.zerop.reshape(scale_zp_shape)
    quantized_weight = linear_quantize_clip(w_ele, w_scale_expand, w_zerop_expand, w_ele_t.qmin, w_ele_t.qmax)
    return quantized_weight, w_ele_t.scale, w_ele_t.zerop

@quant_register(OpType.GRUv3)
def gruv3_quantize(self, *args):
    q_mode_weight = self.attrs["q_mode_weight"]
    q_mode_activation = self.attrs["q_mode_activation"]
    if QuantMode.is_per_channel(q_mode_activation) == True:
        OPT_FATAL("Currently not support per-channel quantization of activations")
    q_bits_weight = self.attrs["q_bits_weight"]
    q_bits_bias = self.attrs["q_bits_bias"]
    q_bits_activation = self.attrs["q_bits_activation"]

    dtype = dtype2str(bits2dtype(q_bits_activation, is_signed=True))
    if dtype not in ['int8', 'int16']:
        OPT_FATAL("Currently gruv3/gruv1 only  support quantization bits  of activations is 8 or 16")

    inp = self.inputs[0]
    time_step = self.get_param('time_steps')
    input_size = self.get_param('input_size')
    cell_size = self.get_param('cell_size')
    activations_list = self.get_param('activations', optional=True, default_value=['SIGMOID', 'TANH'])
    activation_alpha = self.get_param('activation_alpha', optional=True, default_value=[1, 1])
    activation_beta = self.get_param('activation_beta', optional=True, default_value=[0, 0])

    h_all = self.placeholders[0]
    h_all.scale, h_all.zerop, h_all.qmin, h_all.qmax, h_all.dtype = \
        get_linear_quant_params_from_tensor(h_all, QuantMode.to_symmetric(
            q_mode_activation), q_bits_activation, is_signed=True)
    h_all.qbits = q_bits_activation
    h_all.qinvariant = False
    h_scale = h_all.scale

    bak_inp_tensor_property = get_bak_tensor_property(self.inputs[0])
    bak_outp_tensor_property = get_bak_tensor_property(self.outputs[0])

    activations_in_scale = []
    activations_out_scale = []
    activation_idx_lut_name = {0: ('lut_rt', 'lut_zt'), 1: ('lut_ht',)}
    for activation_idx, activation in enumerate(activations_list):
        if activation in ["SIGMOID", "TANH"]:  # "SOFTPLUS", "SOFTSIGN", "ELU", "HARDSIGMOID", "SCALEDTANH", "AFFINE", "THRESHOLDEDRELU"
            activation_in_scale, activation_out_scale = \
                generate_lut_with_placeholders(self, activation_idx, activation, q_mode_activation,
                                               q_bits_activation, activation_idx_lut_name, with_lut=True, with_clamp=True, *args)
            if dtype == 'int8':
                activation_in_scale = 256 * activation_in_scale
            activations_in_scale.append(activation_in_scale)
            activations_out_scale.append(activation_out_scale)
        else:
            OPT_FATAL("Don't support activation[%s]." % (activation))

    _tensor_default_property = get_tensor_default_property()
    for p in _tensor_default_property:
        self.inputs[0].__setattr__(p, bak_inp_tensor_property[self.inputs[0].name][p])
        self.outputs[0].__setattr__(p, bak_outp_tensor_property[self.outputs[0].name][p])

    f_in_scale, g_in_scale = activations_in_scale
    f_out_scale, g_out_scale = activations_out_scale

    if self.attrs['unify_shifts_for_aiff'] and QuantMode.is_per_channel(q_mode_weight):
        # quantize wx_gk_t_scale
        quantized_wx_gk, wx_gk_t_scale, wx_gk_t_zerop = adaptation_weights_for_unify_shifts_for_aiff(
            self, 'wx_gk', f_in_scale / inp.scale)
        quantized_wh_gk, wh_gk_t_scale, wh_gk_t_zerop = adaptation_weights_for_unify_shifts_for_aiff(
            self, 'wh_gk', inp.scale * wx_gk_t_scale / h_scale)
        quantized_wx_ck, wx_ck_t_scale, wx_ck_t_zerop = adaptation_weights_for_unify_shifts_for_aiff(
            self, 'wx_ck', g_in_scale / inp.scale)
        quantized_wh_ck, wh_ck_t_scale, wh_ck_t_zerop = adaptation_weights_for_unify_shifts_for_aiff(
            self, 'wh_ck', inp.scale * wx_ck_t_scale / (h_scale * f_out_scale))
        w_scale_expand = [wx_gk_t_scale, wh_gk_t_scale, wx_ck_t_scale, wh_ck_t_scale]
        w_zerop_expand = [wx_gk_t_zerop, wh_gk_t_zerop, wx_ck_t_zerop, wh_ck_t_zerop]
        [self.constants.pop(weights_name) for weights_name in split_weights_name]
    else:
        w_scale_expand = []
        w_zerop_expand = []
        quantized_w = []
        for idx, weights_name in enumerate(split_weights_name):
            w_ele_t = self.constants[weights_name]
            w_ele = w_ele_t.betensor
            w_ele_t.scale, w_ele_t.zerop, w_ele_t.qmin, w_ele_t.qmax, w_ele_t.dtype = \
                get_linear_quant_params_from_tensor(w_ele_t, q_mode_weight, q_bits_weight, is_signed=True)
            if QuantMode.is_per_channel(q_mode_weight):
                w_scale_expand.append(w_ele_t.scale)
                w_zerop_expand.append(w_ele_t.zerop)
                scale_zp_shape = [w_ele.shape[0]] + [1 for i in range(len(w_ele.shape) - 1)]
                quantized_weight = linear_quantize_clip(w_ele, w_scale_expand[idx].reshape(scale_zp_shape),
                                                        w_zerop_expand[idx].reshape(scale_zp_shape), w_ele_t.qmin,
                                                        w_ele_t.qmax)
            else:
                w_scale_expand.append(torch.tensor(w_ele_t.scale, device=w_ele.device))
                w_zerop_expand.append(torch.tensor(w_ele_t.zerop, device=w_ele.device))
                quantized_weight = linear_quantize_clip(
                    w_ele, w_scale_expand[idx], w_zerop_expand[idx], w_ele_t.qmin, w_ele_t.qmax)
            quantized_w.append(quantized_weight)
            self.constants.pop(weights_name)
        wx_gk_t_scale, wh_gk_t_scale, wx_ck_t_scale, wh_ck_t_scale = w_scale_expand
        wx_gk_t_zerop, wh_gk_t_zerop, wx_ck_t_zerop, wh_ck_t_zerop = w_zerop_expand
        quantized_wx_gk, quantized_wh_gk, quantized_wx_ck, quantized_wh_ck = quantized_w

    gate_kernel_q = torch.cat((quantized_wx_gk, quantized_wh_gk), dim=1)
    cand_kernel_q = torch.cat((quantized_wx_ck, quantized_wh_ck), dim=1)
    quantized_weight = torch.cat((gate_kernel_q, cand_kernel_q), dim=0).contiguous()
    weights = self.constants["weights"]
    weights.scale = w_scale_expand
    weights.zerop = w_zerop_expand
    weights.betensor = quantized_weight
    weights.qbits = q_bits_weight
    weights.dtype = bits2dtype(weights.qbits, is_signed=True)
    weights.qinvariant = False

    # quantized bias
    b = self.constants["biases"]
    bias = b.betensor
    gates_bias = bias[: 2 * cell_size]
    candidate_bias = bias[2 * cell_size: 3 * cell_size]
    xwg_scale = inp.scale * wx_gk_t_scale
    hwg_scale = h_scale * wh_gk_t_scale
    # torch.min(xwg_scale, hwg_scale) if isinstance(xwg_scale, torch.Tensor) else min(xwg_scale, hwg_scale)
    gb_scale = xwg_scale
    zerop = 0
    qmin = -2**(q_bits_bias-1)
    qmax = 2**(q_bits_bias-1) - 1
    gates_bias_q = linear_quantize_clip(gates_bias, gb_scale, zerop, qmin, qmax)

    if 'version' in self.params and self.params['version'] == "GRUV1":
        factor = 1
        hidden_out_placeholders = self.placeholders[5]
        hidden_out_placeholders.scale, hidden_out_placeholders.zerop, \
            hidden_out_placeholders.qmin, hidden_out_placeholders.qmax, hidden_out_placeholders.dtype = \
            get_linear_quant_params_from_tensor(
                hidden_out_placeholders, "per_tensor_symmetric_restricted_range", q_bits_activation, is_signed=True)
        hidden_out_placeholders.qbits = q_bits_activation
        hidden_out_placeholders.qinvariant = False
        hidden_scale, hidden_scale_type, hidden_shift, hidden_shift_type = \
            get_scale_approximation_params(hidden_out_placeholders.scale / (h_scale * wh_ck_t_scale),
                                           q_bits_activation,
                                           force_shift_positive=self.force_shift_positive)
        if QuantMode.is_per_channel(q_mode_weight):
            self.constants["hidden_scale"] = PyTensor(
                self.name + "/hidden_scale", hidden_scale.cpu().numpy().astype(dtype2nptype(hidden_scale_type)))
            self.constants["hidden_scale"].dtype = hidden_scale_type
            self.constants["hidden_shift"] = PyTensor(
                self.name + "/hidden_shift", hidden_shift.cpu().numpy().astype(dtype2nptype(hidden_shift_type)))
            self.constants["hidden_shift"].dtype = hidden_shift_type
        else:
            self.params["hidden_shift_value"] = int(hidden_shift)
            self.params["hidden_shift_type"] = hidden_shift_type
            self.params["hidden_scale_value"] = int(hidden_scale)
            self.params["hidden_scale_type"] = hidden_scale_type

        hidden_bias = bias[3 * cell_size:]
        hidden_bias_scale = h_scale * wh_ck_t_scale
        hidden_bias_q = linear_quantize_clip(hidden_bias, hidden_bias_scale, zerop, qmin, qmax)
        hwc_scale = hidden_scale * f_out_scale
        quantized_bias = hidden_bias_q
    else:
        factor = 2 ** q_bits_activation
        hwc_scale = h_scale * f_out_scale * wh_ck_t_scale
        quantized_bias = torch.zeros([0], device=inp.betensor.device)

    xwc_scale = inp.scale * wx_ck_t_scale
    # torch.min(xwc_scale, hwc_scale) if isinstance(xwc_scale, torch.Tensor) else min(xwc_scale, hwc_scale)  # xwc_scale if xwc_scale <= hwc_scale else hwc_scale
    cb_scale = xwc_scale
    candidate_bias_q = linear_quantize_clip(candidate_bias, cb_scale, zerop, qmin, qmax)
    quantized_bias = torch.cat((gates_bias_q, candidate_bias_q, quantized_bias), dim=0)
    b.zerop = 0
    b.scale = [gb_scale, cb_scale]  # min(gb_scale, cb_scale)
    b.qmin = qmin
    b.qmax = qmax
    b.qbits = q_bits_bias
    b.betensor = quantized_bias
    b.dtype = bits2dtype(b.qbits, is_signed=True)
    b.qinvariant = False

    absorb_input_h_zp_to_bias(self, *args)
    clear_lower_bits_for_bias(self, *args)

    # get_scale_approximation_params = compute_requantization_param
    hwg_do_scale, hwg_do_scale_type, hwg_do_shift, hwg_do_shift_type = \
        get_scale_approximation_params(gb_scale / hwg_scale,
                                       q_bits_activation,
                                       force_shift_positive=self.force_shift_positive)
    sum_to_sigm_do_scale, sum_to_sigm_do_scale_type, sum_to_sigm_do_shift, sum_to_sigm_do_shift_type = \
        get_scale_approximation_params(f_in_scale / gb_scale,
                                       q_bits_activation,
                                       force_shift_positive=self.force_shift_positive)

    hwc_do_scale, hwc_do_scale_type, hwc_do_shift, hwc_do_shift_type = \
        get_scale_approximation_params(factor * cb_scale / hwc_scale,
                                       q_bits_activation,
                                       force_shift_positive=self.force_shift_positive)
    sum_to_tanh_do_scale, sum_to_tanh_do_scale_type, sum_to_tanh_do_shift, sum_to_tanh_do_shift_type = \
        get_scale_approximation_params(g_in_scale / cb_scale,
                                       q_bits_activation,
                                       force_shift_positive=self.force_shift_positive)
    if dtype == 'int8':
        sum_to_sigm_do_shift += 8
        sum_to_tanh_do_shift += 8

    c1mu_do_scale, c1mu_do_scale_type, c1mu_do_shift, c1mu_do_shift_type = \
        get_scale_approximation_params(h_scale / g_out_scale,
                                       q_bits_activation,
                                       force_shift_positive=self.force_shift_positive)

    hm1_do_scale, hm1_do_scale_type, hm1_do_shift, hm1_do_shift_type = \
        get_scale_approximation_params(1.0 / f_out_scale,
                                       q_bits_activation,
                                       force_shift_positive=self.force_shift_positive)

    if QuantMode.is_per_channel(q_mode_weight):
        do_scale = torch.cat((hwg_do_scale, sum_to_sigm_do_scale, hwc_do_scale, sum_to_tanh_do_scale,
                              torch.tensor([c1mu_do_scale], device=inp.betensor.device),
                              torch.tensor([hm1_do_scale], device=inp.betensor.device)))
        do_shift = torch.cat((hwg_do_shift, sum_to_sigm_do_shift, hwc_do_shift, sum_to_tanh_do_shift,
                              torch.tensor([c1mu_do_shift], device=inp.betensor.device),
                              torch.tensor([hm1_do_shift], device=inp.betensor.device)))

        _, do_scale_type = range2dtype(0, do_scale.max().item())  # bits2dtype(q_bits_activation, is_signed=False)
        _, do_shift_type = range2dtype(do_shift.min().item(), do_shift.max().item(), force_int=True)
        self.constants["scale"] = PyTensor(
            self.name+"/scale", do_scale.cpu().numpy().astype(dtype2nptype(do_scale_type)))
        self.constants["scale"].dtype = do_scale_type
        self.constants["shift"] = PyTensor(
            self.name+"/shift", do_shift.cpu().numpy().astype(dtype2nptype(do_shift_type)))
        self.constants["shift"].dtype = do_shift_type
    else:
        do_scale = torch.tensor([hwg_do_scale, sum_to_sigm_do_scale, hwc_do_scale, sum_to_tanh_do_scale,
                                 c1mu_do_scale, hm1_do_scale], device=inp.betensor.device)
        do_shift = torch.tensor([hwg_do_shift, sum_to_sigm_do_shift, hwc_do_shift, sum_to_tanh_do_shift,
                                 c1mu_do_shift, hm1_do_shift], device=inp.betensor.device)
        _, do_scale_type = range2dtype(0, do_scale.max().item())  # bits2dtype(q_bits_activation, is_signed=False)
        _, do_shift_type = range2dtype(do_shift.min().item(), do_shift.max().item(), force_int=True)
        scale_value = do_scale.cpu().numpy().astype(dtype2nptype(do_scale_type)).tolist()
        shift_value = do_shift.cpu().numpy().astype(dtype2nptype(do_shift_type)).tolist()
        self.params["shift_value"] = shift_value
        self.params["shift_type"] = [do_shift_type] * len(shift_value)
        self.params["scale_value"] = scale_value
        self.params["scale_type"] = [do_scale_type] * len(scale_value)

    for out in self.outputs:
        out.qbits = q_bits_activation
        out.scale, out.zerop, out.qmin, out.qmax, out.dtype = get_linear_quant_params_from_tensor(
            out, QuantMode.to_symmetric(q_mode_activation), out.qbits, is_signed=True)
        out.qinvariant = False


from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('main', views.main, name='main'),
    path('resources', views.resources, name='resources'),
    path('room', views.room, name='room'),
    path('select-custom', views.select_custom, name='select_custom'),
    path('select-pre', views.select_pre, name='select_pre'),
    path('clothing-details', views.clothing_details, name='clothing_details'),
]


#!/usr/bin/env python3
import requests
import sys
from bs4 import BeautifulSoup

class WrongNumberOfArgsError(Exception):
    pass

class InfiniteLoopError(Exception):
    pass

def to_snake_case(string):
    return string \
            .replace(',', '_') \
            .replace('.', '_') \
            .replace(' ', '_') \
            .replace('-', '_') \

def roads_to_philosophy(url, previous):
    try:
        data = requests.get(url)

        data.raise_for_status()

        road = BeautifulSoup(data.text, 'html.parser')

        title = road.find(id='firstHeading').text

        if title in previous:
            raise InfiniteLoopError

        previous.append(title)

        print(title)

        if title == 'Philosophy':
            return print(f"{len(previous)} roads from {previous[0]} to Philosophy")

        content = road.find(id='mw-content-text')

        allLinks = content.select('p > a')

        for link in allLinks:
            if (link.get('href') is not None) \
                    and \
                (link['href'].startswith('/wiki/')) \
                    and \
                (not link['href'].startswith('/wiki/Wikipedia:')) \
                    and \
                (not link['href'].startswith('/wiki/Help:')):

                return roads_to_philosophy('https://en.wikipedia.org' + link['href'], previous)

        return print("It leads to a dead end !")

    except requests.HTTPError as e:
        if (data.status_code == 404):
            return print("It leads to a dead end !")

    except InfiniteLoopError:
        return print("It leads to an infinite loop !")

    except Exception as e:
        return print(f'An exception occurred: {e}')

def orquestrator():
    try:
        if len(sys.argv) != 2:
            raise WrongNumberOfArgsError

        previous = []

        query = to_snake_case(sys.argv[1].strip())

        url = f'https://en.wikipedia.org/wiki/{query}'

        roads_to_philosophy(url, previous)

    except WrongNumberOfArgsError:
        return print('Wrong number of args!')

if __name__ == '__main__':
    orquestrator()


#!/usr/bin/env python
from random import randint
from typing import Union

import tcod.color

from pyrl.components import Equipment, Inventory, Level, Player, Stairs
from pyrl.components.equippable import Equippable, Slot
from pyrl.components.item import Item
from pyrl.components.visual import RenderOrder
from pyrl.components.xp_reward import XpReward
from pyrl.random_utils import SpawnRow, SpawnTable
from pyrl.resources import Fov

from . import config
from .components import Collider, Energy, Fighter, Name, Position, Visual
from .components.ai import basic as basic_ai
from .components.ai import player as player_ai
from .esper_ext import WorldExt
from .resources.map import Map, Rect
from .vector import Vector

def create_room(map: Map, room: Rect) -> None:
    for x in range(room.x1 + 1, room.x2):
        for y in range(room.y1 + 1, room.y2):
            map.tiles[x][y].blocked = False

            map.tiles[x][y].block_sight = False

def create_h_tunnel(map: Map, x1: int, x2: int, y: int) -> None:
    for x in range(min(x1, x2), max(x1, x2) + 1):
        map.tiles[x][y].blocked = False
        map.tiles[x][y].block_sight = False

def create_v_tunnel(map: Map, y1: int, y2: int, x: int):
    for y in range(min(y1, y2), max(y1, y2) + 1):
        map.tiles[x][y].blocked = False
        map.tiles[x][y].block_sight = False

def dummy_map() -> Map:
    map = Map(width=config.MAP_WIDTH, height=config.MAP_HEIGHT, dungeon_level=0)
    create_room(map, Rect(20, 15, 10, 15))
    create_room(map, Rect(35, 15, 10, 15))
    create_h_tunnel(map, 25, 40, 23)
    return map

def random_map(dungeon_level: int) -> Map:
    map = Map(
        width=config.MAP_WIDTH, height=config.MAP_HEIGHT, dungeon_level=dungeon_level
    )

    for _ in range(config.MAX_ROOMS):
        # random width and height
        w = randint(config.ROOM_MIN_SIZE, config.ROOM_MAX_SIZE)
        h = randint(config.ROOM_MIN_SIZE, config.ROOM_MAX_SIZE)
        # random position without going out of the boundaries of the map
        x = randint(0, map.width - w - 1)
        y = randint(0, map.height - h - 1)

        # "Rect" class makes rectangles easier to work with
        new_room = Rect(x, y, w, h)

        if not any(new_room.intersect(other_room) for other_room in map.rooms):
            create_room(map, new_room)
            new_center = new_room.center
            if not map.rooms:
                map.spawn_position = Position(new_center)
            else:
                connect_room(map, new_center)
            map.rooms.append(new_room)

    return map

def connect_room(map: Map, new_center: Vector) -> None:
    # center coordinates of previous room
    prev_center = map.rooms[-1].center
    # flip a coin (random number that is either 0 or 1)
    if randint(0, 1) == 1:
        # first move horizontally, then vertically
        create_h_tunnel(map, prev_center.x, new_center.x, prev_center.y)
        create_v_tunnel(map, prev_center.y, new_center.y, new_center.x)
    else:
        # first move vertically, then horizontally
        create_v_tunnel(map, prev_center.y, new_center.y, prev_center.x)
        create_h_tunnel(map, prev_center.x, new_center.x, new_center.y)

max_monsters_per_room = SpawnRow(2, 1)(3, 4)(5, 6)
monster_spawn_table: SpawnTable[str] = SpawnTable()
monster_spawn_table.add("orc")(80)
monster_spawn_table.add("troll")(15, 3)(30, 5)(60, 7)

def generate_monsters(world: WorldExt) -> None:
    map = world.get_resource(Map)
    our_max_monsters_per_room = max_items_per_room.weight(map.dungeon_level)

    for room in map.rooms:
        number_of_monsters = randint(0, our_max_monsters_per_room)

        for i in range(number_of_monsters):
            # Choose a random location in the room
            position = Position(
                Vector(
                    x=randint(room.x1 + 1, room.x2 - 1),
                    y=randint(room.y1 + 1, room.y2 - 1),
                )
            )

            if not any(match[1] == position for match in world.get_component(Position)):
                monster = world.create_entity(position, Collider(), Energy(1), basic_ai)
                monster_choice = monster_spawn_table.spawn(map.dungeon_level)

                if monster_choice == "orc":
                    world.add_components(
                        monster,
                        Name("Orc"),
                        Visual(
                            char="o",
                            color=tcod.desaturated_green,
                            render_order=RenderOrder.ACTOR,
                        ),
                        Fighter.new(hp=20, defense=0, power=4),
                        XpReward(35),
                    )
                else:
                    world.add_components(
                        monster,
                        Name("Troll"),
                        Visual(
                            char="T",
                            color=tcod.darker_green,
                            render_order=RenderOrder.ACTOR,
                        ),
                        Fighter.new(hp=30, defense=2, power=8),
                        XpReward(100),
                    )

max_items_per_room = SpawnRow(1, 1)(2, 4)
item_spawn_table: SpawnTable[Union[Item, Slot]] = SpawnTable()
item_spawn_table.add(Item.HEALING_POTION)(35)
item_spawn_table.add(Item.LIGHTNING_SCROLL)(25, 4)
item_spawn_table.add(Item.LIGHTNING_SCROLL)(25, 6)
item_spawn_table.add(Item.CONFUSION_SCROLL)(10, 2)
item_spawn_table.add(Slot.MAIN_HAND)(5, 4)
item_spawn_table.add(Slot.OFF_HAND)(15, 8)

def generate_items(world: WorldExt) -> None:
    map = world.get_resource(Map)
    our_max_items_per_room = max_items_per_room.weight(map.dungeon_level)

    for room in map.rooms:
        number_of_items = randint(0, our_max_items_per_room)

        for i in range(number_of_items):
            position = Position(
                Vector(
                    x=randint(room.x1 + 1, room.x2 - 1),
                    y=randint(room.y1 + 1, room.y2 - 1),
                )
            )

            if not any(match[1] == position for match in world.get_component(Position)):
                item_choice = item_spawn_table.spawn(map.dungeon_level)

                if item_choice is Item.HEALING_POTION:
                    world.create_entity(
                        position,
                        Visual("!", tcod.violet, RenderOrder.ITEM),
                        Name("Healing Potion"),
                        Item.HEALING_POTION,
                    )
                elif item_choice is Item.FIREBALL_SCROLL:
                    world.create_entity(
                        position,
                        Visual("#", tcod.red, RenderOrder.ITEM),
                        Name("Fireball Scroll"),
                        Item.FIREBALL_SCROLL,
                    )
                elif item_choice is Item.LIGHTNING_SCROLL:
                    world.create_entity(
                        position,
                        Visual("#", tcod.yellow, RenderOrder.ITEM),
                        Name("Lightning Scroll"),
                        Item.LIGHTNING_SCROLL,
                    )
                elif item_choice is Item.CONFUSION_SCROLL:
                    world.create_entity(
                        position,
                        Visual("#", tcod.purple, RenderOrder.ITEM),
                        Name("Confusion Scroll"),
                        Item.CONFUSION_SCROLL,
                    )
                elif item_choice is Slot.MAIN_HAND:
                    world.create_entity(
                        position,
                        Visual("/", tcod.sky, RenderOrder.ITEM),
                        Name("Sword"),
                        Equippable(slot=Slot.MAIN_HAND, power_bonus=3,),
                    )
                elif item_choice is Slot.OFF_HAND:
                    world.create_entity(
                        position,
                        Visual("[", tcod.darker_orange, RenderOrder.ITEM),
                        Name("Shield"),
                        Equippable(slot=Slot.OFF_HAND, defense_bonus=1,),
                    )

def generate_stairs(world: WorldExt) -> None:
    map = world.get_resource(Map)
    last_room = map.rooms[-1]
    world.create_entity(
        Stairs(map.dungeon_level + 1),
        Position(last_room.center),
        Visual(">", tcod.white, RenderOrder.STAIRS),
        Name("Stairs"),
    )

def add_player(world) -> None:
    map = world.get_resource(Map)

    dagger = world.create_entity(
        Visual("-", tcod.sky, RenderOrder.ITEM),
        Name("Dagger"),
        Equippable(slot=Slot.MAIN_HAND, power_bonus=2,),
    )

    world.create_entity(
        map.spawn_position,
        Visual("@", tcod.white, RenderOrder.ACTOR),
        player_ai,
        Player(),
        Name("Player"),
        Collider(),
        Energy(1),
        Fighter.new(hp=100, defense=1, power=2),
        Inventory(26).add(dagger),
        Level(level_up_base=200, level_up_factor=150),
        Equipment({}).toggle_equip(world, dagger),
    )

def generate_level(world: WorldExt, dungeon_level: int = 1) -> None:
    map = random_map(dungeon_level)
    world.add_resource(map)
    world.add_resource(Fov(map))
    generate_monsters(world)
    generate_items(world)
    generate_stairs(world)


# coding=utf-8
import os
import requests
from tqdm import tqdm
from mindsearch.utils.logger import Logger

logger = Logger(__name__).get_logger()

def download_url(url, save_path):
    """
    Download file from remote `url` to local directory, the file will be named `path` locally.
    """
    if os.path.dirname(save_path) != '':
        os.makedirs(os.path.dirname(save_path), exist_ok=True)

    req = requests.get(url, stream=True, verify=False)
    if req.status_code != 200:
        logger.error("Exception when downloading {}, response code {}".format(url, req.status_code))
        req.raise_for_status()
        return

    download_filepath = f"{save_path}_part"
    with open(download_filepath, "wb") as writer:
        content_length = req.headers.get('Content-Length')
        total = int(content_length) if content_length is not None else None
        progress = tqdm(unit="B", total=total, unit_scale=True)
        for chunk in req.iter_content(chunk_size=1024):
            if chunk:
                writer.write(chunk)
                progress.update(len(chunk))

    os.rename(download_filepath, save_path)
    progress.close()


import keras
from keras.models import Sequential, Input, Model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU

width = 256

train_X = train_X.reshape(-1, width, width, 1)
test_X = test_X.reshape(-1, width, width, 1)

# Network parameters:

batch_size = 64
epochs = 20
num_classes = 25

# Architecture

Malware_Model = Sequential()
Malware_Model.add(Conv2D(32,
                         kernel_size=(3, 3),
                         activation='linear',
                         input_shape=(width, width, 1),
                         padding='same'))
Malware_Model.add(LeakyReLU(alpha=0.1))
Malware_model.add(MaxPooling2D(pool_size=(2, 2),
                               padding='same'))
Malware_Model.add(Conv2D(64, (3, 3),
                         activation='linear',
                         padding='same'))
Malware_Model.add(LeakyReLU(alpha=0.1))
Malware_Model.add(Dense(1024,
                        activation='linear'))
Malware_Model.add(LeakyReLU(alpha=0.1))
Malware_Model.add(Dropout(0.4))
Malware_Model.add(Dense(num_classes,
                        activation='softmax'))

# Compile the model

Malware_Model.compile(loss=keras.losses.categorical_crossentropy,
                      optimizer=keras.optimizers.Adam(),
                      metrics=['accuracy'])

# Fit and Train the model:

Malware_Model.fit(train_X, train_label,
                  batch_size=batch_size,
                  epochs=epochs,
                  verbose=1)
                #   validation_data=(valid_X, valid_label))

# Evaluste the model

Malware_Model.evaluate(test_X, test_Y_one_hot, verbose=0)
# print('The accuracy of the Test is:', test_eval[1])


import os
import warnings
from abc import ABCMeta
from collections import namedtuple

import re

from .base import Operation, Bank, Account, Historic, Entity

class AxaBank(Bank):
    def __init__(self):
        super().__init__("Axa", "AXABBE22")

    def __repr__(self):
        return "{}()".format(self.__class__.__name__)

class AxaOp(Operation):
    # Always try to be more specific
    def __init__(self, account, operation_date, effective_date, value,
                 description, amount_remaining, message=""):
        super().__init__(account, operation_date, effective_date, value,
                         description)
        self.amount_remaining = amount_remaining
        self.message = message

class NotAccountOp(AxaOp, metaclass=ABCMeta):
    """The other party is not an account"""
    def __init__(self, account, operation_date, effective_date, value,
                 description, amount_remaining, other_party_name,
                 other_party_place, message=""):
        super().__init__(account, operation_date, effective_date, value,
                         description, amount_remaining, message)
        self.other_party_name = Entity(other_party_name)
        self.other_party_place = other_party_place

    def get_other_party(self):
        return self.other_party_name

class Payment(NotAccountOp):
    pass

class Withdrawal(NotAccountOp):
    pass

class AccountOp(AxaOp, metaclass=ABCMeta):
    def __init__(self, account, operation_date, effective_date, value,
                 description, amount_remaining, other_party_account,
                 other_party_name, message=""):
        super().__init__(account, operation_date, effective_date, value,
                         description, amount_remaining, message)

        self.other_party = Account(iban=other_party_account,
                                   name=other_party_name)

    def get_other_party(self):
        return self.other_party

class Transfer(AccountOp):
    pass

class PossiblyWithinOperation(AccountOp):
    def get_other_party(self):
        other = super().get_other_party()
        if isinstance(other, Account) and other.name == "":
            return self.account.bank
        return other

class Debit(PossiblyWithinOperation):
    """Like fiscal domiciliation"""
    pass

class PermanentOrder(AccountOp):
    pass

class VisaDebit(Debit):
    pass

class Fee(PossiblyWithinOperation):
    def get_other_party(self):
        return self.account.bank

class AxaParser(object):
    OpStr = namedtuple('OpStr', ['id', 'operation_date', 'effective_date',
                                 'writing_date', 'value', 'amount_remaining',
                                 'type', 'other_account', 'other_name',
                                 'machine_name', 'place', 'card_number',
                                 'msg1', 'msg2', 'details'])

    def _parse_general_header(self, hdl, account_name):
        bank = AxaBank()
        type = hdl.readline().strip()
        account_str = hdl.readline()
        bic_str = hdl.readline()
        for _ in range(5):
            # Skip useless lines
            hdl.readline()

        iban = account_str[5:].replace(" ", "").strip()
        bic = bic_str[4:].strip()
        if bank.bic != bic:
            print(len(bank.bic))
            print(len(bic))
            raise ValueError("BIC of the bank is not correct. "
                             "Expecting {}, found {}".format(bank.bic, bic))
        return Account(iban, bank, account_name, type=type)

    def _parse_operation(self, line, line_number, account):
        op_str = self.__class__.OpStr(*line.split(";"))
        # Selecting on type

        other_is_account = False
        other_is_not_account = False
        if op_str.type == "Achat - Bancontact" or \
           op_str.type == "Achat - Maestro" or \
           op_str.type == "Vente glob. - Bancontact":
            factory = Payment
            other_is_not_account = True
        elif op_str.type.startswith("Retrait"):
            factory = Withdrawal
            other_is_not_account = True
        elif op_str.type.startswith("Virement"):
            factory = Transfer
            other_is_account = True
        elif op_str.type == "Ordre permanent":
            factory = PermanentOrder
            other_is_account = True
        elif op_str.type.startswith("Domiciliation") or \
             op_str.type == "Encaissement interne":
            if "visa" in op_str.type.lower():
                factory = VisaDebit
            else:
                factory = Debit
            other_is_account = True
        elif op_str.type.startswith("Contribution"):
            factory = Fee
            other_is_account = True
        elif op_str.type == "Capitalisation":
            factory = Fee  # Weird
            other_is_account = True
        else:
            warnings.warn("Unknown operation type '{}' (line {})."
                          "".format(op_str.type, line_number))
            factory = AxaOp

        def partial(**kwargs):
            return factory(account=account,
                           operation_date=op_str.operation_date,
                           effective_date=op_str.effective_date,
                           value=float(op_str.value.replace(",", ".")),
                           description=op_str.details,
                           amount_remaining=op_str.amount_remaining,
                           message=os.linesep.join([op_str.msg1, op_str.msg2]),
                           **kwargs)

        if other_is_account:
            return partial(other_party_account=op_str.other_account,
                           other_party_name=op_str.other_name)
        elif other_is_not_account:
            return partial(other_party_name=op_str.machine_name,
                           other_party_place=op_str.place)
        else:
            return partial()

    def parse_csv(self, fpath, account_name="n/a", encoding="latin"):
        ops = []
        line_start = re.compile(r"^\d\d\d\d(\s)?/(\s)?\d+")
        with open(fpath, "r", encoding=encoding) as hdl:
            account = self._parse_general_header(hdl, account_name)
            header_line = hdl.readline().strip()  # skip csv header
            curr_line = ""
            for i, line in enumerate(hdl):
                if line_start.match(line):
                    if len(curr_line) > 0:
                        ops.append(self._parse_operation(curr_line.strip(),
                                                         i + 8,
                                                         account))
                    curr_line = line
                else:
                    curr_line += line

        return Historic(ops)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import jsonfield.fields
from django.conf import settings
import django.utils.timezone

class Migration(migrations.Migration):

    dependencies = [
        ('contenttypes', '0002_remove_content_type_name'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Notification',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, verbose_name='ID', serialize=False)),
                ('level', models.CharField(max_length=20, default='info', choices=[('success', 'success'), ('info', 'info'), ('warning', 'warning'), ('error', 'error')])),
                ('unread', models.BooleanField(default=True)),
                ('actor_object_id', models.PositiveIntegerField()),
                ('verb', models.CharField(max_length=255)),
                ('description', models.TextField(null=True, blank=True)),
                ('target_object_id', models.PositiveIntegerField(null=True, blank=True)),
                ('action_object_object_id', models.PositiveIntegerField(null=True, blank=True)),
                ('timestamp', models.DateTimeField(default=django.utils.timezone.now)),
                ('public', models.BooleanField(default=True)),
                ('deleted', models.BooleanField(default=False)),
                ('emailed', models.BooleanField(default=False)),
                ('data', jsonfield.fields.JSONField(null=True, blank=True)),
                ('action_object_content_type', models.ForeignKey(null=True, blank=True, to='contenttypes.ContentType', related_name='notify_action_object')),
                ('actor_content_type', models.ForeignKey(related_name='notify_actor', to='contenttypes.ContentType')),
                ('recipient', models.ForeignKey(related_name='notifications', to=settings.AUTH_USER_MODEL)),
                ('target_content_type', models.ForeignKey(null=True, blank=True, to='contenttypes.ContentType', related_name='notify_target')),
            ],
            options={
                'ordering': ('-timestamp',),
            },
        ),
    ]