""" Monte Carlo Learner """
import numpy as np

from blackjack.learners import BaseLearner

class MonteCarlo(BaseLearner):
    """ Monte Carlo learner """

    def __init__(self, epsilon=0.1, window_size=1000, name=None, gamma=0.9):
        super().__init__(epsilon=epsilon, window_size=window_size, name=name)
        self.gamma = gamma
        self.episode_state_action_pairs = []
        self.counter = np.zeros_like(self.Q)

    def update_Q(self, agent_state_index, action_index, reward=0):
        """ update the action-value function """
        if agent_state_index is None:
            # have reached the end of the episode
            # update Q for all visited state-action pairs
            for step, state_action_pair in enumerate(
                    reversed(self.episode_state_action_pairs)):
                state, action = state_action_pair
                G = reward*self.gamma**step
                self.counter[state][action] += 1
                self.Q[state][action] += (
                    G - self.Q[state][action])/self.counter[state][action]
            # reinitialize episode state-action pairs for next episode
            self.episode_state_action_pairs = []
        else:
            # still in episode, store state-action pairs
            self.episode_state_action_pairs.append(
                (agent_state_index, action_index))
        return self

if __name__=='__main__':
    learner = MonteCarlo()
    learner.train_and_evaluate(n_episodes=int(3e4))


#
# @lc app=leetcode id=863 lang=python3
#
# [863] All Nodes Distance K in Binary Tree
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:
        adj, res, visited = collections.defaultdict(list), [], set()

        def dfs(node):
            if node.left:
                adj[node].append(node.left)
                adj[node.left].append(node)
                dfs(node.left)
            if node.right:
                adj[node].append(node.right)
                adj[node.right].append(node)
                dfs(node.right)

        dfs(root)

        def dfs2(node, d):
            if d < k:
                visited.add(node)
                for v in adj[node]:
                    if v not in visited:
                        dfs2(v, d + 1)
            else:
                res.append(node.val)

        dfs2(target, 0)
        return res

# @lc code=end


import time

def cal_time(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        execution_time = end_time - start_time
        print(f"{func.__name__} 执行时间: {execution_time} 秒")
        return result
    return wrapper


#a dictionary

music_list = [
    {
        "artist": "Radiohead",
        "song name": "How to Disappear Completely",
        "Album": "Kid A",
        "genre": "Alt Rock"
    },
    {
        "artist": "My Chemical Romance",
        "song name": "Sleep",
        "Album": "The Black Parade",
        "genre": "Emo"
    },
    {
        "artist": "Killing Joke",
        "song name": "Slave To Substance",
        "Album": "Absolute Dissent",
        "genre": "Industrial Metal"
    },
]

fav_bands=[] # an empty list that I'll append into

for each_dict in music_list:
    fav_bands.append(list(each_dict.values())[0])

# print(fav_bands) <- this was in just to check my list, I didn't need it in the final code

def add_to_list(band):
    fav_bands.append(band) #This function is used to use the list method .append() and add whatever the user types in to the end of the list.


# P1
data, f = [], 0
for line in open('d01.txt').read().splitlines():
    data.append(int(line))
    f += data[-1]
print(f)

# P2
i, f, fs = 0, 0, set()
while f not in fs:
    fs.add(f)
    f += data[i]
    i = (i + 1) % len(data)
print(f)


import json
from history import HistoryOfToday

def alfred(history):
    items = []
    for key in history:
        info = history[key]

        line = {
            'type':'default',
			'title':f'({key}) {info["title"]}',
			'subtitle': info['desc'],
			'arg':info['link']
        }
        items.append(line)
    result_json = json.dumps({"items":items}, ensure_ascii=False)
    return result_json

def main():
    history = HistoryOfToday().get_web_info()
    show = alfred(history)
    print(show)

if __name__ == "__main__":
    main()


class Solution(object):
    def canPartition(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        if nums is None:
            return True
        total = sum(nums)
        if total % 2 == 1:
            return False

        output = [False] * (total + 1)
        output[0] = True
        for n in nums:
            # Have to be reversed. Cannot reuse smaller elements.
            for s in reversed(range(n, total + 1)):
                output[s] = output[s] or output[s-n]

        return output[int(total/2)]


import os, requests, json

step_cnt = 3
mode = 'deploy'
skip_build = True

port = 1044
docker_image = "voice-vector-web:latest"
marathon_id = "zeze.zzz/vv-web"
is_dev = True

if step_cnt > 0 and skip_build == False:
    print('[STEP 1/3] docker build...')
    os.system('docker build . -t idock.daumkakao.io/kakaobrain/{}'.format(docker_image))

if mode == 'run':
    if step_cnt > 1:
        print('[STEP 2/2] docker run...')
        os.system('docker run -p {}:{} idock.daumkakao.io/kakaobrain/{}'.format(port, port, docker_image))

if mode == 'deploy':
    if step_cnt > 1:
        print('[STEP 2/3] docker push...')
        os.system('docker push idock.daumkakao.io/kakaobrain/{}'.format(docker_image))

    if step_cnt > 2:
        print('[STEP 3/3] restart marathon instance...')
        if is_dev:
            marathon_api_url = "http://dcos.brain.dev.9rum.cc/marathon/v2/"
        else:
            marathon_api_url = "http://dcos.brain.9rum.cc/marathon/v2/"
        marathon_restart_url = "{}/apps/{}/restart?force=True".format(marathon_api_url, marathon_id)
        marathon_response = requests.post(marathon_restart_url)

        if marathon_response.status_code == 200:
            restart_deploy_id = json.loads(marathon_response.text)['deploymentId']
            print("Restart Success - deploy id : {}".format(restart_deploy_id))

        else:
            print('Restart Failed - STATUS {}'.format(marathon_response.status_code))


import grass.script as gscript
import grass.temporal as tgis
import grass.pygrass.vector as pyvect
import grass.pygrass.vector.geometry as pygeom
import grass.pygrass.raster as pyrast
import grass.pygrass.gis.region as pyregion
import grass.lib.vector as libvect
import grass.lib.raster as librast
import sys

STR_RTYPE = {'CELL':librast.CELL_TYPE,
             'FCELL':librast.FCELL_TYPE,
             'DCELL':librast.DCELL_TYPE}

class SamplePoint(object):
    def __init__(self, x, y, cat, column=None):
        self.x = x
        self.y = y
        self.cat = cat
        self.column = column
        self.value = None

    def __str__(self):
        return str(self.x) + " " + \
            str(self.y) + " " + \
            str(self.cat) + " " + \
            str(self.column)

    def coords(self):
        return (self.x, self.y)

###############################################################################

class SamplePointComparisonY(object):
    """This comparison key can be used to sort lists of SamplePoint
       by Y coordinate

        Example:

        .. code-block:: python

            # Sort the SamplePoint's in the list by Y coordinate
            sorted_p_list = sorted(p_list, key=SamplePointComparisonY)
    """

    def __init__(self, obj, *args):
        self.obj = obj

    def __lt__(self, other):
        return self.obj.y < other.obj.y

    def __gt__(self, other):
        return self.obj.y > other.obj.y

    def __eq__(self, other):
        return self.obj.y == other.obj.y

    def __le__(self, other):
        return self.obj.y <= other.obj.y

    def __ge__(self, other):
        return self.obj.y >= other.obj.y

    def __ne__(self, other):
        return self.obj.y != other.obj.y

############################################################################

def main(options, flags):

    # Get the options
    points = options["points"]
    strds = options["strds"]
    output = options["output"]
    where = options["where"]
    order = options["order"]
    column = options["column"]
    separator = options["separator"]
    coordinates = options["coordinates"]

    # Setup separator
    if separator == "pipe":
        separator = "|"
    if separator == "comma":
        separator = ","
    if separator == "space":
        separator = " "
    if separator == "tab":
        separator = "\t"
    if separator == "newline":
        separator = "\n"

    use_cats = False

    write_header = flags["n"]
    use_raster_region = flags["r"]

    overwrite = gscript.overwrite()

    if points and coordinates:
        gscript.fatal(_("points and coordinates are mutually exclusive"))

    if not points and not coordinates:
        gscript.fatal(_("You must specify points or coordinates"))

    # Make sure the temporal database exists
    tgis.init()
    # We need a database interface
    dbif = tgis.SQLDatabaseInterfaceConnection()
    dbif.connect()

    sp = tgis.open_old_stds(strds, "strds", dbif)
    maps = sp.get_registered_maps_as_objects(where=where, order=order,
                                             dbif=dbif)
    dbif.close()

    if not maps:
        gscript.fatal(_("Space time raster dataset <%s> is empty") % sp.get_id())

    # The list of sample points
    p_list = []

    if not coordinates:
        # Check if the chosen header column is in the vector map
        vname = points
        vmapset= ""
        if "@" in points:
            vname, vmapset = points.split("@")

        v = pyvect.VectorTopo(vname, vmapset)
        v.open("r")

        col_index = 0

        if v.exist() is False:
            gscript.fatal(_("Vector map <%s> does not exist" %(points)))

        if not v.table:
            use_cats = True
            gscript.warning(_("Vector map <%s> does not have an attribute table, using cats as header column." %(points)))

        if v.table and column not in v.table.columns:
            gscript.fatal(_("Vector map <%s> has no column named %s" %(points, column)))

        if use_cats is False:
            col_index = list(v.table.columns.names()).index(column)

        # Create the point list
        for line in v:
            if line.gtype == libvect.GV_POINT:
                if use_cats is False:
                    p = SamplePoint(line.x, line.y, line.cat, line.attrs.values()[col_index])
                elif use_cats is True:
                    p = SamplePoint(line.x, line.y, line.cat)

                p_list.append(p)

        v.close()
    else:
        # Convert the coordinates into sample points
        coord_list = coordinates.split(",")
        use_cats = True

        count = 0
        cat = 1
        while count < len(coord_list):
            x = coord_list[count]
            count += 1
            y = coord_list[count]
            count += 1

            p = SamplePoint(float(x), float(y), cat)
            p_list.append(p)
            cat += 1

    if output:
        out_file = open(output, "w")
    else:
        out_file = sys.stdout

    # Write the header
    if write_header:
        out_file.write("start_time")
        out_file.write(separator)
        out_file.write("end_time")
        out_file.write(separator)
        count = 0
        for p in p_list:
            count += 1
            if use_cats is True:
                out_file.write(str(p.cat))
            else:
                out_file.write(str(p.column))
            if count != len(p_list):
                out_file.write(separator)
        out_file.write("\n")

    # Sorting the points by y-coordinate to make use of the single row cache and read direction
    sorted_p_list = sorted(p_list, key=SamplePointComparisonY)

    # Sample the raster maps
    num = 0
    for map in maps:
        num += 1
        sys.stderr.write("Sample map <%s> number  %i out of %i\n" %(map.get_name(), num, len(maps)))

        start, end = map.get_temporal_extent_as_tuple()
        out_file.write(str(start))
        out_file.write(separator)
        if not end:
            out_file.write(str(start))
        else:
            out_file.write(str(end))
        out_file.write(separator)

        r = pyrast.RasterRow(map.get_name(), map.get_mapset())
        if r.exist() is False:
            gscript.fatal(_("Raster map <%s> does not exist" %(map.get_id())))

        region = None
        if use_raster_region is True:
            r.set_region_from_rast()
            region = pyregion.Region()
            region.from_rast(map.get_name())
        # Open the raster layer after the region settings
        r.open("r")

        # Sample the raster maps with the sorted points
        for p in sorted_p_list:
            p.value = r.get_value(point=p, region=region)

        # Write the point values from the original list
        count = 0
        for p in p_list:
            count += 1
            out_file.write(str(p.value))
            if count != len(p_list):
                out_file.write(separator)
        out_file.write("\n")

        r.close()

    out_file.close()

############################################################################

if __name__ == "__main__":
    options, flags = gscript.parser()
    main(options, flags)


import numpy as np;
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import Imputer, LabelEncoder, OneHotEncoder, StandardScaler
from sklearn.model_selection import train_test_split

dataset = pd.read_csv('datafiles/Data.csv')
#take all the columns except the last one (:-1)
X = dataset.iloc[:, :-1].values
#takes the last column
y = dataset.iloc[:, 3].values

#takes care of missing data
#axis = 0 (mean of col) axis = 1(mean of rows)
imputer = Imputer(missing_values='NaN', strategy='mean', axis=0)
#takes col from matrix X index 1 to 2 and fit an imputer on it
imputer = imputer.fit(X[:, 1:3])
X[:, 1:3] = imputer.transform(X[:, 1:3])

#encoding categorical data, label encoder = ordinal, one hot encoding = non-ordinal
labelEncoder_X = LabelEncoder()
X[:, 0] = labelEncoder_X.fit_transform(X[:, 0])
#a better encoder that create n columns based on n categories, instead of assign one column to all category as it might imply france > germany
onehotEncoder = OneHotEncoder(categorical_features=[0])
X = onehotEncoder.fit_transform(X).toarray()
labelEncoder_y = LabelEncoder()
#encode dependent variable purchased can use label encoder as it is dependent variable and will be recognised as a category with no order
y = labelEncoder_y.fit_transform(y)

#splitting dataset into test set and training set, set test data to 20% of train set
#random state is to ensure that it will generate the same set everytime (not necessary)
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=0)

#feature scaling (normalise data to be on the same scale, so no variable will dominate the others)
# no need scale y as it is a categorical variable (0,1)
#We use fit_transform() on the train data so that we learn the parameters of scaling on the train data and in the same time we scale the train data.
# We only use transform() on the test data because we use the scaling paramaters learned on the train data to scale the test data.
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

print(X_train)


import pygame
from ven.utils import debugReporter
from ven.gui import *

class dialog:
    def __init__(self, engine: any, debug=False):
        self.debug=debugReporter(debug)
        self.debug.location='dialog'
        self.debug.write("hello from the dialog module.")
        self.engine=engine
        # = arrays & indexers =
        self.buffer=[]
        self.bufIndex=0
        self.chrIndex=0
        # = animation =
        self.speed=0.01
        self.timing=0
        # = controls =
        self.inDialog=False
        self.frozen=False
        # init the dialog guis.
        self.initDialogUI()

    def initDialogUI(self):
        # init the dialog frame.
        self.dialogFrame = frame(self.engine.coreDisplay)

        # NOTE: dialog size and position is /4 of the screen.
        VW, VH=self.engine.viewport.get_size()
        self.dialogFrame.background = pygame.Surface((VW, VH//3), pygame.SRCALPHA)

        # init the dialog frame.
        self.dialogFrame.background.fill((0, 0, 0))
        self.dialogFrame.backgroundPosition=[0, (VH//3)*2]
        self.dialogFrame.visible=False

        mediumFont  = self.engine.core.storage.getFont("normal", 24)

        self.dialogFromLabel = label(self.engine.coreDisplay, mediumFont, "...")
        self.dialogFromLabel.position[0] = 25
        self.dialogFromLabel.position[1] = 25 + self.dialogFrame.backgroundPosition[1]

        self.dialogFrame.addElement(self.dialogFromLabel)
        self.engine.coreDisplay.addElement(self.dialogFrame)

        # apply the variables.
        self.xPosition = 25
        self.yPosition = self.dialogFromLabel.position[1] + 50

    #####################
    # utility functions #
    #####################

    def new(self, buffer: list):
        self.debug.write("new dialog request: %s" % str(buffer))
        self.buffer=buffer
        self.dialogFrame.visible, self.inDialog=True, True
        self.engine.lockPlayer()

    def endDialog(self):
        self.debug.write("finished dialog!")
        self.buffer=[]
        self.bufIndex=0
        self.chrIndex=0
        self.inDialog, self.dialogFrame.visible=False, False
        self.frozen=False
        self.engine.unlockPlayer()

    def advanceDrawing(self):
        whatText=self.buffer[self.bufIndex]['msg']
        if self.chrIndex+1>len(whatText):
            self.frozen=True
        else:
            self.chrIndex+=1

    def skip(self):
        if self.bufIndex+1>=len(self.buffer):
            self.endDialog()
        else:
            # advance drawing.
            self.frozen=False
            self.bufIndex+=1
            self.chrIndex=0

    ###############
    # tick system #
    ###############

    def tick(self, events: any):
        if self.inDialog:
            if not self.frozen:
                if pygame.time.get_ticks() > self.timing:
                    self.advanceDrawing()

    ###############
    # draw system #
    ###############

    def draw(self):
        if self.inDialog:
            # extract the current data
            currentTalk = self.buffer[self.bufIndex]

            # from who?
            fromWho = currentTalk.get("from")
            fromWho = (self.engine.world.player.name if not fromWho else fromWho)
            self.dialogFromLabel.setText(fromWho)

            # get the font, case nothing, use the normal one.
            font=currentTalk.get('font') or 'normal'
            fontSize=currentTalk.get('fontSize') or 16
            font=self.engine.core.storage.getFont(font, fontSize)

            # draw the message
            message = currentTalk.get("msg") or '...'

            # index here
            atX, atY = self.xPosition, self.yPosition
            for idx in range(0, self.chrIndex):
                character = message[idx]
                if   character == '*':          font.set_bold(not font.get_bold())
                elif character == '_':          font.set_italic(not font.get_italic())
                elif character == '¬':          font.set_underline(not font.get_underline())
                else:
                    renderedChar=font.render(character, True, (255, 255, 255))
                    self.engine.viewport.blit(renderedChar,(atX, atY))
                    atX = atX + renderedChar.get_width()


import asyncio
from pathlib import Path

import click

@click.group()
def main():
    pass

@click.command(name="spider")
@click.option(
    "-s",
    "--start",
    default=1,
    type=int,
    help="start page of spider",
    show_default=True,
)
@click.option(
    "-e",
    "--end",
    default=-1,
    type=int,
    help="end page of spider, -1 for infinite",
    show_default=True,
)
@click.option(
    "-p",
    "--parallel",
    type=int,
    default=8,
    help="number of concurrent tasks",
    show_default=True,
)
@click.option(
    "-o",
    "--output",
    default=Path(".") / "data",
    type=Path,
    help="output directory",
    show_default=True,
)
def spider(start: int, end: int, parallel: int, output: Path):
    from SagasuSpider.spider import SagasuSpider

    instance = SagasuSpider(parallel, start, end, output)

    try:
        asyncio.run(instance())
    except KeyboardInterrupt:
        exit(1)

@click.command("upload")
@click.option(
    "-b",
    "--base",
    type=str,
    required=True,
    help="base sagasu core api url",
)
@click.option(
    "-p",
    "--parallel",
    type=int,
    default=8,
    help="number of concurrent tasks",
    show_default=True,
)
@click.option(
    "-s",
    "--source",
    default=Path(".") / "data",
    type=Path,
    help="output directory",
    show_default=True,
)
def upload(base: str, parallel: int, source: Path):
    from SagasuSpider.upload import SagasuUpload

    instance = SagasuUpload(base, source, parallel)

    try:
        asyncio.run(instance())
    except KeyboardInterrupt:
        exit(1)

main.add_command(spider)
main.add_command(upload)

if __name__ == "__main__":
    main()


'''
K’th Non-repeating Character
Given a string and a number k, find the k’th non-repeating character in the string.
Consider a large input string with lacs of characters and a small character set.
How to find the character by only doing only one traversal of input string?
Examples:
Input : str = geeksforgeeks, k = 3
Output : r
First non-repeating character is f,
second is o and third is r.
Input : str = geeksforgeeks, k = 2
Output : o
Input : str = geeksforgeeks, k = 4
Output : Less than k non-repeating
         characters in input.
'''

import collections

def KthNonRepeatingCharacter(str1,k):

    dict={}
    stk=[]
    for i in range(0,len(str1)):
        key=str1[i]

        if key in dict.keys():
            dict[key]=dict.get(key)+1
        else:
            dict[key]=1

        if dict[key]==1:
            stk.append(key)
        else:
            try:
                idx=stk.index(key)
                del stk[idx]
            except:
                pass
    if k-1<len(stk):
        return stk[k-1]
    else:
        return "Not Existing"

def main():

    str1='geeksforgeeks'
    k=3
    print(KthNonRepeatingCharacter(str1,k))

    str1 = 'geeksforgeeks'
    k = 2
    print(KthNonRepeatingCharacter(str1, k))

    str1 = 'geeksforgeeks'
    k = 4
    print(KthNonRepeatingCharacter(str1, k))

if __name__=='__main__':
    main()


from django import forms
from django.utils.safestring import mark_safe

class DayForm(forms.Form):
    day = forms.IntegerField(
        label=mark_safe('Type how many days of week you would like to exercise (1 - 6)'),
        max_value=6,
        min_value=1
    )

class CalorieForm(forms.Form):
    age = forms.IntegerField(label='Age (1-100):', help_text="", max_value=100, min_value=1, )
    weight = forms.IntegerField(help_text='Enter weight (lbs).', max_value=600, min_value=50)
    height = forms.IntegerField(help_text='Enter height (inches).', max_value=120, min_value=30)

    GENDER_ID = (
        ('M', 'Male'),
        ('F', 'Female')
    )

    gender = forms.CharField(
        max_length=1,
        widget=forms.RadioSelect(choices=GENDER_ID),
        help_text='Enter gender.'
    )

    ACTIVITY_LEVEL = (
        ('s', 'Sedentary'),
        ('l', 'Light Activity'),
        ('m', 'Moderate Activity'),
        ('v', 'Very Active'),
        ('e', 'Extremely Active')
    )

    activity = forms.CharField(
        max_length=1,
        widget=forms.RadioSelect(choices=ACTIVITY_LEVEL),
        help_text='Select activity level:'
    )

    GOAL_WEIGHT = (
        ('l', 'Lose Weight'),
        ('m', 'Maintain'),
        ('g', 'Gain Weight')
    )

    goal = forms.CharField(
        max_length=1,
        widget=forms.RadioSelect(choices=GOAL_WEIGHT),
        help_text='Select your weight goal.'
    )

class EditProfileForm(forms.Form):
    first_name = forms.CharField(max_length=100)
    last_name = forms.CharField(max_length=100)
    email = forms.EmailField(max_length=100)


from collections import defaultdict

infinit_minus = -1000000

class Node:
    def __init__(self, num, parent = None, distance = 0, finding_time = 0, color = ''):
        self.num = num
        self.color = color
        self.parent = parent
        self.d = distance
        self.f = finding_time

    def __repr__(self):
        if self.parent != None:
            return f"Node {self.num} is {self.color}, has Parent {self.parent.num} and path {self.d}"
        else:
            return f"Node {self.num} is {self.color}, dosn't have parent and has path {self.d}"

class Graph:
    def __init__(self):
        self._graph = defaultdict(list)

    @property
    def graph(self):
        return self._graph

    @graph.setter
    def graph(self, graph):
        self._graph = graph

    def addEdge(self, u, v):
        self._graph[u].append(v)

    def DFS(self, s):
        for u in self.graph.keys():
            u.color = "white"
            u.parent = None

        time = 0

        for u in self.graph.keys():
            if u.color == "white":
                self.DFS_Visit(u)

    def DFS_Visit(self, u):
        time += 1
        u.d = time
        u.color = "grey"

        for v in self.graph[u]:
            if v.color == "white":
                v.parent = u
                self.DFS_Visit(v)

        u.color = "black"
        time += 1
        u.f = time
        print()

    """
    @staticmethod
    def printPath(s, v):
        if s.num == v.num:
            print(s)
        elif v.parent == None:
            print("There is no path from s to v")
        else:
            Graph.printPath(s, v.parent)
            print(v)
    """

if __name__ == "__main__":
    g = Graph()

    nodes = []

    for i in range(4):
        nodes.append(Node(i))

    g.addEdge(nodes[0], nodes[1])
    g.addEdge(nodes[0], nodes[2])
    g.addEdge(nodes[1], nodes[2])
    g.addEdge(nodes[2], nodes[0])
    g.addEdge(nodes[2], nodes[3])
    g.addEdge(nodes[3], nodes[3])

    g.BFS(nodes[2])

    Graph.printPath(nodes[2], nodes[1])


"""Class for YOLO classifier."""
import json
import os

from core.classifiers import BaseClassifier
from core.platform.darkflow import darkflow
import vgconf

class YOLO(BaseClassifier.BaseClassifier):
    def __init__(self):
        self.config_file = ('%s.json' % vgconf.DEFAULT_YOLO_CLASSIFIER)
        self.yolo_options = {
            'gpu': 1.0,
            'gpuName': '/device:GPU:0',
            'threshold': vgconf.YOLO_THRESHOLD,
            'config': os.path.join(
                vgconf.EXTRAS_PATH, vgconf.DARKFLOW_MODULE_NAME, 'cfg')
        }
        self.labels = []
        super(YOLO, self).__init__()

    @property
    def _classifier_name(self):
        return 'YOLO'

    def _restore_model_params(self):
        pass

    def _generate_model(self):
        conf_path = self._get_data_path(self.config_file)
        yolo_conf = json.loads(open(conf_path, 'r').read())
        self.cfg_path = self._get_data_path(yolo_conf['config'])
        self.weights_path = self._get_data_path(yolo_conf['weights'])
        self.offset = yolo_conf['offset']
        self.yolo_options.update({
            'model': self.cfg_path,
            'load': self.weights_path,
            'offset': self.offset
        })
        self.model = darkflow.build_network(self.yolo_options)
        self.n_classes = yolo_conf['classes']
        self.labels_file =self._get_data_path(yolo_conf['labels'])

    def _process_labels_file(self):
        self.labels = []
        labels_file = open(self.labels_file, 'r')
        for label in labels_file.readlines():
            self.labels.append(label.strip())

    def get_labels(self):
        if not self.labels:
            self._process_labels_file()
        return self.labels

    def predict(self, frame):
        return self.model.return_predict(frame)

    def close(self):
        self.model.sess.close()


"""Support for getting tweets from a user twitter timeline."""
import logging
import tweepy

import voluptuous as vol
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
from datetime import timedelta
from datetime import datetime
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.components.rest.sensor import RestData
from homeassistant.const import (
    CONF_NAME,
    CONF_RESOURCE,
    CONF_ACCESS_TOKEN,
    CONF_UNIT_OF_MEASUREMENT,
    CONF_VALUE_TEMPLATE,
    CONF_SCAN_INTERVAL
)
from homeassistant.helpers.entity import Entity
from homeassistant.exceptions import PlatformNotReady
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)

CONF_TWITTER_KEY = 'twitter_key'
CONF_TWITTER_SECRET = 'twitter_secret'
CONF_ACCESS_SECRET = 'access_secret'
CONF_TIMELINE_ENTRIES = 'twitter_timeline_entries'
#SCAN_INTERVAL = timedelta(seconds=600)
DEFAULT_NAME = "Twitter Parse"
DOMAIN = "twitter_parse"

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
    {
        vol.Required(CONF_RESOURCE): cv.string,
        vol.Required(CONF_ACCESS_TOKEN): cv.string,
        vol.Required(CONF_TWITTER_KEY, default=''): cv.string,
        vol.Required(CONF_TWITTER_SECRET, default=''): cv.string,
        vol.Required(CONF_ACCESS_SECRET, default=''): cv.string,
        vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
        vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
        vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
        #vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL): cv.time_period,
        vol.Optional(CONF_TIMELINE_ENTRIES, default=4): cv.positive_int
    }
)

def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the Twitter Parse sensor."""
    name = config.get(CONF_NAME)
    resource = config.get(CONF_RESOURCE)

    consumer_key = config.get(CONF_TWITTER_KEY)
    consumer_secret = config.get(CONF_TWITTER_SECRET)
    access_token = config.get(CONF_ACCESS_TOKEN)
    access_secret = config.get(CONF_ACCESS_SECRET)
    unit = config.get(CONF_UNIT_OF_MEASUREMENT)
    timeline_entries = config.get(CONF_TIMELINE_ENTRIES)

    value_template = config.get(CONF_VALUE_TEMPLATE)
    if value_template is not None:
        value_template.hass = hass

    if consumer_key and consumer_secret:
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_secret)
    else:
        auth = None

    if auth is None:
        raise PlatformNotReady

    api = tweepy.API(auth)

    add_entities(
        [TwitterParseSensor(hass, api, name, resource, timeline_entries, value_template, unit)], True
    )

class TwitterParseSensor(Entity):
    """Representation of a Twitter Parse sensor."""

    def __init__(self, hass, api, name, twitter_user, num_timeline_entries, value_template, unit):
        """Initialize Twitter Parse sensor."""
        self.hass = hass
        self.api = api
        self._name = name
        self._state = None
        self._value_template = value_template
        self._unit_of_measurement = unit
        self.timeline = []
        self.twitter_user = twitter_user
        # hardcoded for now to avoid rate limiting
        self.num_timeline_entries = min(30, num_timeline_entries)

    @property
    def name(self):
        """Return the name of the sensor."""
        return self._name

    @property
    def unit_of_measurement(self):
        """Return the unit the value is expressed in."""
        return self._unit_of_measurement

    @property
    def state(self):
        """Return the state of the device."""
        return self._state

    @property
    def state_attributes(self):
        """Return the optional state attributes."""
        data = {
            'timeline': self.timeline,
            'last_update': datetime.now()
        }
        return data

    def process_status(self, twitter_status):
        return {
            "text": twitter_status.text,
            "url": "https://twitter.com/%s/status/%s" % (twitter_status.user.screen_name, twitter_status.id),
            "posted_at": twitter_status.created_at
        }

    def update(self):
        """Get the latest data from the source and update the state."""

        user = self.api.get_user(self.twitter_user)
        if (user == None):
            _LOGGER.error("Unable to retrieve data from twitter api")
            return

        try:
            self.timeline = []

            # Iterate through the first N statuses in the home timeline
            for status in tweepy.Cursor(self.api.user_timeline, id=self.twitter_user).items(self.num_timeline_entries):
                processed_status = self.process_status(status)

                if self._value_template is not None:
                    processed_status["text"] = self._value_template.render_with_possible_json_value(processed_status["text"], None)

                self.timeline.append(processed_status)

            if len(self.timeline) > 0:
                self._state = self.timeline[0]["text"][0:252]
        except tweepy.RateLimitError:
            _LOGGER.error("Unable to parse data due to Twitter Rate Limiting")
            return
        except tweepy.TweepError:
            _LOGGER.error("Unable to parse data due to API Error")
            return
        except Exception as e:
            _LOGGER.error("Unable to extract data from Twitter: " + str(e))
            return


import textwrap

from bs4 import BeautifulSoup, Tag
from django.test import TestCase
from recipes.scraping.scrapers.tineno import TineNoScraper
from recipes.scraping.tests._utils import inject_base_tests, with_params

parameters = {
    "tineno.kylling_urtepasta.html": {
        "_url": "https://www.tine.no/oppskrifter/middag-og-hovedretter/pasta-og-ris/urtepasta-med-kylling",
        "title": "Urtepasta med kylling",
        "content": "Tips\nSprøstekt bacon smaker også veldig godt i denne pastaretten.",
        "group_names": ["", "Urtesaus", "Tilbehør"],
        "preamble": "Oppskrift på deilig pastarett med kylling og nydelig urtesaus. Urtesausen består av Crème Fraîche, og det du måtte ha stående av friske urter.",
    },
    "tineno.tikka_masala.html": {
        "_url": "https://www.tine.no/oppskrifter/middag-og-hovedretter/kylling-og-fjarkre/rask-kylling-tikka-masala",
        "title": "Rask kylling tikka masala",
        "content": "Tips\nHvis retten skulle koke litt tørr, er det bare å spe på med litt vann.",
        "group_names": ["Ris", "Tikka masala", "Raita"],
        "preamble": "En god og rask oppskrift på en kylling tikka masala. Dette er en rett med små smakseksplosjoner som sender tankene til India.",
    },
    "tineno.horn_kefir.html": {
        "_url": "https://www.tine.no/oppskrifter/bakst/brod-og-rundstykker/horn-med-kefir-brunost-og-bringeb%C3%A6r",
        "title": "Horn med kefir, brunost og bringebær",
        "content": textwrap.dedent(
            """\
            Tips
            I deigen benyttes bakepulver og kefir, to ingredienser som sammen bidrar til godt bakverk. Opplever du at deigen er tung å jobbe med, anbefaler vi å elte den litt ekstra.

            Hornene kan fryses. Hvis de tines 1 time på kjøkkenbenken før du varmer dem på 200 ºC i ca. 5 minutter, vil de smake som nystekt.

            Til denne oppskriften er det fint å bruke opp brunostrester. Ta vare på brunostrestene i fryseren og bruk som naturlig søtning i bakverk, revet over havregrøt eller i vaffelrøren.
            """
        ).strip(),
        "group_names": [""],
        "preamble": "Horn bakt med kefir gir saftig bakverk med egen syrlighet som sammen med naturlig søt brunost og bringebær kanskje kan bli din nye hverdagsfavoritt. Oppskriften gir 24 horn som kan fryses, tines og nytes i en travel hverdag.",
    },
}

@with_params(parameters)
@inject_base_tests()
class TineNoTest(TestCase):
    scraper_cls = TineNoScraper

    def test_content_no_html_attributes(self):
        content = self.scraper.my_content()
        soup = BeautifulSoup(content, "html.parser")
        self.assertEqual(soup.attrs, {})
        for desc in soup.descendants:
            if isinstance(desc, Tag):
                self.assertEqual(desc.attrs, {})


#!/usr/bin/env python

import logging

logger = logging.getLogger(__name__)

def setVerbosity(
        level=logging.INFO, handler=logging.StreamHandler(),
        format='%(name)s - %(levelname)s - %(message)s'):
    formatter = logging.Formatter(format)
    handler.setFormatter(formatter)
    esneft_logger = logging.getLogger('esneft_tools')
    esneft_logger.setLevel(level)
    esneft_logger.addHandler(handler)

def formatP(p):
    """ Return formatted p for title """
    pformat = 'p '
    if p > 0.999:
        pformat += '> 0.999'
    elif p < 0.001:
        pformat += '< .001'
    else:
        pformat += '= ' + f'{p:.3f}'[1:]
    if p < 0.001:
        return pformat + ' ***'
    elif p < 0.01:
        return pformat + ' **'
    elif p < 0.05:
        return pformat + ' *'
    else:
        return pformat


import sys
import torch
from PIL import Image
# import coremltools as ct
import torchvision.transforms as transforms
from torch.utils.mobile_optimizer import optimize_for_mobile
from torch.nn import functional as F

sys.path.append('sr')
from sr.animesr.archs.vsr_arch import MSRSWVSR
from sr.scripts.inference_animesr_frames import *

class AnimeSR(MSRSWVSR):
    def __init__(self, netscale):
        super(AnimeSR, self).__init__(64, [5, 3, 2], netscale)

    def _forward(self, x: torch.Tensor) -> torch.Tensor:
        c, h, w = x.size()
        x = x.unsqueeze(0)
        state = x.new_zeros(1, 64, h, w)
        out = x.new_zeros(1, c, h * self.netscale, w * self.netscale)
        stack = torch.cat((x, x, x), dim=1)
        out, state = self.cell(stack, out, state)
        return out.squeeze(0)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        assert len(x.shape) == 3
        # print(x.shape)
        x, ori_h, ori_w = self.pre_process(x)
        # print(x.shape)
        x = self._forward(x)
        x = x.unsqueeze(0)
        x = F.interpolate(input=x, size=(ori_h*self.netscale, ori_w*self.netscale), mode="bilinear", align_corners=False)
        return x

    def pre_process(self, input: torch.Tensor) -> (torch.Tensor, int, int):
        output_stride = 32
        ori_height, ori_width = input.shape[1:]
        new_h = (ori_height // output_stride) * output_stride
        new_w = (ori_width // output_stride) * output_stride
        input = input.unsqueeze(0)
        output = F.interpolate(input=input, size=(new_h, new_w), mode="bilinear", align_corners=False)
        output = output.squeeze()
        return output, ori_height, ori_width

if __name__ == '__main__':

    device = torch.device('cpu')

    model = AnimeSR(netscale=4)

    model_path = '/Users/eky/Projects/_pretrained/animesr/AnimeSR_v2.pth'

    loadnet = torch.load(model_path)
    model.load_state_dict(loadnet, strict=True)

    # model = mobilenet_v3_large(weights='DEFAULT')

    model.train(False)
    model.cpu().eval()

    img = cv2.imread('imgs/rose.jpg')
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.resize(img, (320,320))
    img = (np.array(img) / 255.0).astype(np.float32)
    img = np.transpose(img, (2, 0, 1))
    # img = np.expand_dims(img, 0)

    input_tensor = torch.from_numpy(img)

    output = model(input_tensor)

    output = output.cpu().detach().numpy()
    output = output.clip(0, 1) * 255
    output = output.astype(np.uint8)
    output = np.squeeze(output)
    output = np.transpose(output, (1, 2, 0))
    output = cv2.cvtColor(output, cv2.COLOR_RGB2BGR)
    cv2.imwrite('outputs/rose_animesr.jpg', output)


import torch
import cv2
import matplotlib.pyplot as plt

#@save
def box_corner_to_center(boxes):
    """从（左上，右下）转换到（中间，宽度，高度）"""
    x1, y1, x2, y2 = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
    cx = (x1 + x2) / 2
    cy = (y1 + y2) / 2
    w = x2 - x1
    h = y2 - y1
    boxes = torch.stack((cx, cy, w, h), axis=-1)
    return boxes

#@save
def box_center_to_corner(boxes):
    """从（中间，宽度，高度）转换到（左上，右下）"""
    cx, cy, w, h = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
    x1 = cx - 0.5 * w
    y1 = cy - 0.5 * h
    x2 = cx + 0.5 * w
    y2 = cy + 0.5 * h
    boxes = torch.stack((x1, y1, x2, y2), axis=-1)
    return boxes

#@hzg
def MouseOnImg(img, winName='img'):
    '''
    mouse callback
    CV_EVENT_MOUSEMOVE  0   滑动   EVENT_LBUTTONDOWN   1   左键点击  EVENT_RBUTTONDOWN   2   右键点击
    EVENT_MBUTTONDOWN   3   中间点击  EVENT_LBUTTONUP 4   左键释放  EVENT_RBUTTONUP 5   右键释放
    EVENT_MBUTTONUP 6   中间释放  EVENT_LBUTTONDBLCLK 7   左键双击  EVENT_RBUTTONDBLCLK 8   右键双击
    EVENT_MBUTTONDBLCLK 9   中间双击
    :param img:
    :return:
    '''
    bbox = []
    def callback(event, x, y, flags, param):
        if event == 1:
            print(x, y)
            bbox.append((x,y))
    img_copy = img.copy()

    cv2.namedWindow(winName)
    cv2.setMouseCallback(winName, callback)
    # 设置回调函数窗口 回调函数
    while(True):
        cv2.imshow(winName, img_copy)
        if(len(bbox)==2):
            cv2.rectangle(img_copy, bbox[0], bbox[1], (0, 255, 0), thickness=1)
            bbox = []
        if cv2.waitKey(20) & 0xFF == 27:
            cv2.destroyAllWindows()
            break
    cv2.destroyAllWindows()
# MouseOnImg(img)

#@save
def bbox_to_rect(bbox, color):
    # 将边界框(左上x,左上y,右下x,右下y)格式转换成matplotlib格式：
    # ((左上x,左上y),宽,高)
    return plt.Rectangle(
        xy=(bbox[0], bbox[1]), width=bbox[2]-bbox[0], height=bbox[3]-bbox[1],
        fill=False, edgecolor=color, linewidth=2)

# PATH and get the data of image.
# imgPath = "F:\code\pytorch\Detect\datasets\data\img.png"
# img = cv2.imread(imgPath)
# the bounding box of the cat
# catbbox = [253, 118, 425, 310]
# img_copy = img.copy()
# cv2.rectangle(img_copy, (catbbox[0], catbbox[1]), (catbbox[2], catbbox[3]), (0, 255, 0), thickness=1)
# cv2.imshow("img", img_copy)
# cv2.waitKey(0)

# fig = plt.imshow(img)
# fig.axes.add_patch(bbox_to_rect(catbbox, 'blue'))
# plt.show()
# fig.axes.add_patch(bbox_to_rect(catbbox, 'red'))


from collections import generator
from typing import List

input: List[int] = [3, 5, 7, 1, 2, 4]

def findMaxNum(input: List[int]) -> int:
    a: List[int] = []
    for n in range(1, 10 + 1):
        print(n)

    for i in input:
        for j in input:
            if i < j:
                print(i, j)
                break
        else:
            return i

def find_max_num1(input: List[int]) -> int:
    max_num = input[0]
    for i in input:
        if max_num < i:
            max_num = i
    return max_num

def get_natual_number() -> generator:
    n: int = 0
    while range(0, 100):
        n += 1
        yield n

# print(findMaxNum(input))
#
# print(find_max_num1(input))

print(type(get_natual_number()))


from django.shortcuts import render
from django.http import HttpResponseRedirect
from PIL import Image

def main(request, template_name):
    if request.method == 'GET':
        return render(request, template_name)

    if request.method == 'POST':
        if request.FILES:
            img = Image.open(request.FILES['file-img'])
            if img.mode == 'RGBA':
                img = img.convert('RGB')

            colors = []
            for req in request.POST:
                if 'custom-color' in req:
                    colors.append(hex_to_rgb(request.POST[req]))

            return render(request, 'elements.html', {
                    'colors': count_pixel(img.getcolors(256000000), colors)
            })
        else:
            return render(request, template_name)

def count_pixel(colors, search_colors):
    result = {}
    for color in colors:
        if color[1] in search_colors:
            result[rgb_to_hex(color[1])] = color[0]

    return result

def hex_to_rgb(value):
    value = value.lstrip('#')
    lv = len(value)
    return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))

def rgb_to_hex(rgb):
    return '#%02x%02x%02x' % (rgb[0], rgb[1], rgb[2])


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

# In[1]:

import sys
import tensorflow as tf
import random
import numpy as np
tf.reset_default_graph()

def in_ipynb():
    try:
        cfg = get_ipython().config
        print(cfg)
        if 'jupyter' in cfg['IPKernelApp']['connection_file']:
            return True
        else:
            return False
    except NameError:
        return False

if in_ipynb():
    print("In IPython")
    exec("import Configs.SampleConfig as Conf")
    TrainConfig="Configs/SampleConfig"
else:
    TrainConfig=sys.argv[1]
    print("Importing settings from "+ TrainConfig.replace("/", "."))
    #exec("from "+TrainConfig+" import *")
    importConfig=TrainConfig.replace("/", ".")
    exec("import "+importConfig+" as Conf")

# In[2]:

if not hasattr(Conf, 'MVAlogplot'): Conf.MVAlogplot=False
if not hasattr(Conf, 'Multicore'): Conf.Multicore=False
if not hasattr(Conf, 'RandomState'): Conf.RandomState=42

# In[3]:

tf.compat.v1.random.set_random_seed(Conf.RandomState)
random.seed(Conf.RandomState)
np.random.seed(Conf.RandomState)

# In[4]:

import os
os.system("")
try:
  import uproot3 as uproot
except ImportError:
  import uproot

# In[5]:

import glob
import pandas as pd
import numpy as np
import ROOT
import matplotlib.pyplot as plt
import json

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Dropout

# In[6]:

from Tools.PlotTools import *
import Tools.ptetaRwt as ptetaRwt

# In[7]:

if Conf.Debug==True:
    prGreen("Running in debug mode : Only every 10th event will be used")

if len(Conf.MVAs)>0:
    for MVAd in Conf.MVAs:
        os.system("mkdir -p " + Conf.OutputDirName+"/"+MVAd["MVAtype"])
prGreen("Making output directory")
os.system("mkdir -p " + Conf.OutputDirName)
os.system("mkdir -p " + Conf.OutputDirName+"/CodeANDConfig")
os.system("mkdir -p " + Conf.OutputDirName+"/Thresholds")
os.system("cp "+TrainConfig+".py ./"+ Conf.OutputDirName+"/CodeANDConfig/")
os.system("cp TrainerCombined.py ./"+ Conf.OutputDirName+"/CodeANDConfig/")

# In[8]:

cat='Category'
weight='NewWt'

# In[9]:

label=Conf.Classes

# In[10]:

#Works in uproot3
prGreen("Making data frames")
Sigdf=pd.DataFrame()
Bkgdf=pd.DataFrame()

# In[11]:

import Tools.readData as readData
import sys
import os

# In[12]:

import pandas as pd
if Conf.loadfromsaved:
    df_final=pd.read_parquet(Conf.OutputDirName+'/df.parquet.gzip')
else:
    df_final=readData.daskframe_from_rootfiles(Conf.processes,Conf.Tree,branches=Conf.branches,flatten=Conf.flatten,debug=Conf.Debug)
    if hasattr(Conf, 'SaveDataFrameCSV'):
        if Conf.SaveDataFrameCSV:
            prGreen("Saving DataFrame : It can take sometime")
            df_final.to_parquet(Conf.OutputDirName+'/df.parquet.gzip',compression='gzip')

# In[13]:

fn = lambda row: Conf.Classes.index(row.Class)
df_final[cat] = df_final.apply(fn, axis=1)

# In[14]:

#df_final.head()
Conf.modfiydf(df_final)
#Conf.modfiydf(Bkgdf)

# In[15]:

#SigIndices=df_final.query("sample=='Signal'").index.values.tolist()
#BkgIndices=df_final.query("sample=='Background'").index.values.tolist()

index = df_final.index
Sigcondition = df_final["Class"] == "Signal"
Bkgcondition = df_final["Class"] == "Background"

SigIndices = index[Sigcondition].values.tolist()
BkgIndices = index[Bkgcondition].values.tolist()

from sklearn.model_selection import train_test_split
SigTrainIndices, SigTestIndices = train_test_split(SigIndices, test_size=Conf.testsize, random_state=Conf.RandomState, shuffle=True)
BkgTrainIndices, BkgTestIndices = train_test_split(BkgIndices, test_size=Conf.testsize, random_state=Conf.RandomState, shuffle=True)

# In[16]:

TrainIndices=SigTrainIndices+BkgTrainIndices
TestIndices=SigTestIndices+BkgTestIndices

# In[17]:

df_final.loc[TrainIndices,'Dataset'] = "Train"
df_final.loc[TestIndices,'Dataset'] = "Test"

df_final.loc[TrainIndices,'TrainDataset'] = 1
df_final.loc[TestIndices,'TrainDataset'] = 0

# In[18]:

import seaborn as sns
fig, axes = plt.subplots(1, 1, figsize=(10, 5))
kplot=sns.countplot(x="Class", data=df_final, ax=axes,hue='Dataset',palette=['#432371',"#FAAE7B"])
for p in kplot.patches:
    kplot.annotate(format(p.get_height(), '.2f'), (p.get_x() + p.get_width() / 2., p.get_height()), ha = 'center', va = 'center', xytext = (0, 5), textcoords = 'offset points')
axes.set_title("Number of samples")
#axes.set_yscale("log")
plt.savefig(Conf.OutputDirName+"/TotalStat_TrainANDTest.pdf")
plt.savefig(Conf.OutputDirName+"/TotalStat_TrainANDTest.png")

# In[19]:

def df_pteta_rwt(Mdf,
                 label,
                 returnOnlyPosWeights=0,
                 ptw = [10,30,40,50,200,10000],
                 etaw = [-1.5,-1.0,1.0,1.5],
                 eta='',
                 pt='',
                 SumWeightCol="wt",
                 NewWeightCol="NewWt",target=1,cand=0):
    #Mdf=Ndf.copy()
    Mdf["rwt"]=1
    Mdf[NewWeightCol]=1
    ptwt = [1.0]*len(ptw)
    etawt = [1.0]*len(etaw)

    for k in range(len(etaw)):
        if k == len(etaw)-1:
            continue
        for i in range(len(ptw)):
            if i == len(ptw)-1:
                continue

            targetSum = Mdf.loc[(Mdf[pt] <ptw[i+1]) & (Mdf[pt] >ptw[i]) & (Mdf[eta] <etaw[k+1]) & (Mdf[eta] >etaw[k]) &(Mdf[label]==target),SumWeightCol].sum()
            candSum = Mdf.loc[(Mdf[pt] <ptw[i+1]) & (Mdf[pt] >ptw[i]) & (Mdf[eta] <etaw[k+1]) & (Mdf[eta] >etaw[k]) &(Mdf[label]==cand),SumWeightCol].sum()

            #print('Number of xsec events in signal for pt '+str(ptw[i])+' to '+str(ptw[i+1])+ 'before  weighing = '+str(targetSum))
            #print('Number of xsec events in background for pt '+str(ptw[i])+' to '+str(ptw[i+1])+ 'before  weighing = '+str(candSum))

            if candSum>0 and targetSum>0:
                ptwt[i]=candSum/(targetSum)
            else:
                ptwt[i]=0
            Mdf.loc[(Mdf[pt] <ptw[i+1]) & (Mdf[pt] >ptw[i])
                    & (Mdf[eta] <etaw[k+1]) & (Mdf[eta] >etaw[k])
                    &(Mdf[label]==cand),"rwt"] = 1.0
            Mdf.loc[(Mdf[pt] <ptw[i+1]) & (Mdf[pt] >ptw[i])
                    & (Mdf[eta] <etaw[k+1]) & (Mdf[eta] >etaw[k])
                    &(Mdf[label]==target),"rwt"] = ptwt[i]

            Mdf.loc[:,NewWeightCol] = Mdf.loc[:,"rwt"]*Mdf.loc[:,SumWeightCol]

    MtargetSum = Mdf.loc[Mdf[label]==target,NewWeightCol].sum()
    McandSum = Mdf.loc[Mdf[label]==cand,NewWeightCol].sum()
    print('Number of events in signal after  weighing = '+str(MtargetSum))
    print('Number of events in background after  weighing = '+str(McandSum))

    return Mdf[NewWeightCol]

# In[20]:

df_final[weight]=1

print("In Training:")
if Conf.Reweighing=='ptetaSig':
    df_final.loc[TrainIndices,weight]=df_pteta_rwt(df_final.loc[TrainIndices],cat,ptw=Conf.ptbins,etaw=Conf.etabins,pt=Conf.ptwtvar,eta=Conf.etawtvar,
                                                   SumWeightCol='xsecwt',NewWeightCol=weight, target=0,cand=1)
if Conf.Reweighing=='ptetaBkg':
    df_final.loc[TrainIndices,weight]=df_pteta_rwt(df_final.loc[TrainIndices],cat,ptw=Conf.ptbins,etaw=Conf.etabins,pt=Conf.ptwtvar,eta=Conf.etawtvar,
                                                   SumWeightCol='xsecwt',NewWeightCol=weight, target=1,cand=0)
print("In Testing:")
if Conf.Reweighing=='ptetaSig':
    df_final.loc[TestIndices,weight]=df_pteta_rwt(df_final.loc[TestIndices],cat,ptw=Conf.ptbins,etaw=Conf.etabins,pt=Conf.ptwtvar,eta=Conf.etawtvar,
                                                  SumWeightCol='xsecwt',NewWeightCol=weight, target=0,cand=1)
if Conf.Reweighing=='ptetaBkg':
    df_final.loc[TestIndices,weight]=df_pteta_rwt(df_final.loc[TestIndices],cat,ptw=Conf.ptbins,etaw=Conf.etabins,pt=Conf.ptwtvar,eta=Conf.etawtvar,
                                                  SumWeightCol='xsecwt',NewWeightCol=weight, target=1,cand=0)

# In[21]:

#df_final["ele_pt_bin"]=-9
#df_final["ele_eta_bin"]=-9
if Conf.Reweighing=='ptetaSig' or Conf.Reweighing=='ptetaBkg':
    df_final["ele_pt_bin"] = pd.cut(df_final[Conf.ptwtvar],
                                    bins=Conf.ptbins, labels=list(range(len(Conf.ptbins)-1)))
    df_final["ele_eta_bin"] = pd.cut(df_final[Conf.etawtvar],
                                     bins=Conf.etabins, labels=list(range(len(Conf.etabins)-1)))

# In[22]:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(1, 2, figsize=(10, 5))
for i,group_df in df_final[df_final['Dataset'] == "Train"].groupby(cat):
    group_df[Conf.ptwtvar].hist(histtype='step', bins=Conf.ptbins, alpha=0.7,label=label[i], ax=ax[0], density=False, ls='-', weights =group_df["xsecwt"],linewidth=4)
    ax[0].set_title("$p_T$ before reweighting")
    ax[0].legend()
    group_df[Conf.ptwtvar].hist(histtype='step', bins=Conf.ptbins, alpha=0.7,label=label[i], ax=ax[1], density=False, ls='-', weights =group_df["NewWt"],linewidth=4)
    ax[1].set_title("$p_T$ after reweighting")
    ax[1].legend()
fig.savefig(Conf.OutputDirName+"/pT_rwt.pdf")

fig, ax = plt.subplots(1, 2, figsize=(10, 5))
for i,group_df in df_final[df_final['Dataset'] == "Train"].groupby(cat):
    group_df[Conf.etawtvar].hist(histtype='step',
                                 bins=Conf.etabins,
                                 #[i for i in range(len(Conf.etabins)-1)],
                                 alpha=0.7,label=label[i], ax=ax[0], density=False, ls='-', weights =group_df["xsecwt"],linewidth=4)
    ax[0].set_title("$\eta$ before reweighting")
    ax[0].legend()
    group_df[Conf.etawtvar].hist(histtype='step',
                                 bins=Conf.etabins,
                                 alpha=0.7,label=label[i], ax=ax[1], density=False, ls='-', weights =group_df["NewWt"],linewidth=4)
    ax[1].set_title("$\eta$ after reweighting")
    ax[1].legend()
fig.savefig(Conf.OutputDirName+"/eta_rwt.pdf")

# In[ ]:

# In[ ]:

# In[23]:

'''
fig, axee = plt.subplots(2, 2, figsize=(30, 10))
for i in [0,1]:
    axe=axee[0][i]
    sns.histplot(data=df_final.loc[TrainIndices].query("EleType==@i"), x="ele_eta_bin", y="ele_pt_bin",ax=axe,bins=40,
                 element="bars", fill=False,cbar=False,
                 weights="xsecwt")
    axe.set_title(label[i]+" before reweighting")

    axe=axee[1][i]
    sns.histplot(data=df_final.loc[TrainIndices].query("EleType==@i"), x="ele_eta_bin", y="ele_pt_bin",ax=axe,bins=40,
                 element="bars", fill=False,cbar=False,
                 weights="NewWt")
    axe.set_title(label[i]+" after reweighting")

fig.savefig(Conf.OutputDirName+"/eta_pt_rwt.pdf")
'''

# In[ ]:

# In[24]:

def PrepDataset(df_final,TrainIndices,TestIndices,features,cat,weight):
    X_train = df_final.loc[TrainIndices,features]
    Y_train = df_final.loc[TrainIndices,cat]
    Wt_train = df_final.loc[TrainIndices,weight]

    X_test = df_final.loc[TestIndices,features]
    Y_test = df_final.loc[TestIndices,cat]
    Wt_test = df_final.loc[TestIndices,weight]
    return np.asarray(X_train), np.asarray(Y_train), np.asarray(Wt_train), np.asarray(X_test), np.asarray(Y_test), np.asarray(Wt_test)

# In[25]:

import pickle
import multiprocessing

# In[26]:

for MVA in Conf.MVAs:
    if 'XGB' in MVA["MVAtype"]:
        MakeFeaturePlots(df_final,MVA["features"],MVA["feature_bins"],Set="Train",MVA=MVA["MVAtype"],OutputDirName=Conf.OutputDirName)
        MakeFeaturePlots(df_final,MVA["features"],MVA["feature_bins"],Set="Test",MVA=MVA["MVAtype"],OutputDirName=Conf.OutputDirName)
        MakeFeaturePlotsComb(df_final,MVA["features"],MVA["feature_bins"],MVA=MVA["MVAtype"],OutputDirName=Conf.OutputDirName)
        X_train, Y_train, Wt_train, X_test, Y_test, Wt_test = PrepDataset(df_final,TrainIndices,TestIndices,MVA["features"],cat,weight)
        prGreen(MVA["MVAtype"]+" Applying "+MVA["Scaler"])
        exec("from sklearn.preprocessing import "+MVA["Scaler"])
        exec("sc = "+MVA["Scaler"]+"()")
        X_train = sc.fit_transform(X_train)
        X_test = sc.transform(X_test)
        prGreen(MVA["MVAtype"]+" Training starting")
        import xgboost as xgb
        from sklearn.model_selection import cross_val_score, GridSearchCV
        xgb_model = xgb.XGBClassifier(objective="binary:logistic", random_state=Conf.RandomState)
        #xgb_model.set_config(verbosity=2)
        prGreen("Performing XGB grid search")
        if Conf.Multicore:
            cv = GridSearchCV(xgb_model, MVA["XGBGridSearch"],
                              scoring='neg_log_loss',cv=3,verbose=1,n_jobs=2)#multiprocessing.cpu_count())
        else:
            cv = GridSearchCV(xgb_model, MVA["XGBGridSearch"],
                              scoring='neg_log_loss',cv=3,verbose=1)
        search=cv.fit(X_train, Y_train, sample_weight=Wt_train,verbose=1)
        pickle.dump(cv, open(Conf.OutputDirName+"/"+MVA["MVAtype"]+"/"+MVA["MVAtype"]+"_"+"modelXGB.pkl", "wb"))
        #modelDNN.save(Conf.OutputDirName+"/"+MVA+"_"+"modelDNN.h5")
        prGreen("Expected neg log loss of XGB model = "+str((np.round(np.average(search.best_score_),3))*100)+'%')
        #prGreen("Expected accuracy of XGB model = "+str((np.average(search.best_score_))*100)+'%')
        prGreen("XGB Best Parameters")

        #json.dumps(search.best_params_)
        prGreen(str(search.best_params_))

        df_final.loc[TrainIndices,MVA["MVAtype"]+"_pred"]=cv.predict_proba(X_train)[:,1]
        df_final.loc[TestIndices,MVA["MVAtype"]+"_pred"]=cv.predict_proba(X_test)[:,1]

        prGreen("Plotting output response for XGB")
        fig, axes = plt.subplots(1, 1, figsize=(5, 5))
        plot_mva(df_final.query('TrainDataset==1'),MVA["MVAtype"]+"_pred",bins=50,cat=cat,Wt=weight,ax=axes,sample='train',ls='dashed',logscale=Conf.MVAlogplot)
        plot_mva(df_final.query('TrainDataset==0'),MVA["MVAtype"]+"_pred",bins=50,cat=cat,Wt=weight,ax=axes,sample='test',ls='dotted',logscale=Conf.MVAlogplot)
        plt.savefig(Conf.OutputDirName+"/"+MVA["MVAtype"]+"/"+MVA["MVAtype"]+"_"+"XGBMVA.pdf")
        plt.savefig(Conf.OutputDirName+"/"+MVA["MVAtype"]+"/"+MVA["MVAtype"]+"_"+"XGBMVA.png")

        prGreen("Plotting ROC for XGB")
        fig, axes = plt.subplots(1, 1, figsize=(5, 5))
        plot_roc_curve(df_final.query('TrainDataset==1'),MVA["MVAtype"]+"_pred", tpr_threshold=0, ax=axes, color=None, linestyle='-', label=MVA["Label"]+' Training',cat=cat,Wt=weight)
        plot_roc_curve(df_final.query('TrainDataset==0'),MVA["MVAtype"]+"_pred", tpr_threshold=0, ax=axes, color=None, linestyle='--', label=MVA["Label"]+' Testing',cat=cat,Wt=weight)
        if len(Conf.OverlayWP)>0:
            for color,OverlayWpi in zip(Conf.OverlayWPColors,Conf.OverlayWP):
                plot_single_roc_point(df_final.query('TrainDataset==0'), var=OverlayWpi, ax=axes, color=color, marker='o', markersize=6, label=OverlayWpi+" Test dataset", cat=cat,Wt=weight)
        axes.set_ylabel("Background efficiency")
        axes.set_xlabel("Signal efficiency")
        axes.set_title(MVA["MVAtype"])
        axes.text(1.05, 0.5, 'CMS EGamma ID-Trainer',
                  horizontalalignment='center',
                  verticalalignment='center',
                  rotation='vertical',
                  transform=axes.transAxes)
        plt.savefig(Conf.OutputDirName+"/"+MVA["MVAtype"]+"/"+MVA["MVAtype"]+"_"+"XGBROC.pdf")
        plt.savefig(Conf.OutputDirName+"/"+MVA["MVAtype"]+"/"+MVA["MVAtype"]+"_"+"XGBROC.png")

# In[27]:

from tensorflow.keras.callbacks import EarlyStopping
for MVA in Conf.MVAs:
    if 'DNN' in MVA["MVAtype"]:
        MakeFeaturePlots(df_final,MVA["features"],MVA["feature_bins"],Set="Train",MVA=MVA["MVAtype"],OutputDirName=Conf.OutputDirName)
        MakeFeaturePlots(df_final,MVA["features"],MVA["feature_bins"],Set="Test",MVA=MVA["MVAtype"],OutputDirName=Conf.OutputDirName)
        MakeFeaturePlotsComb(df_final,MVA["features"],MVA["feature_bins"],MVA=MVA["MVAtype"],OutputDirName=Conf.OutputDirName)
        X_train, Y_train, Wt_train, X_test, Y_test, Wt_test = PrepDataset(df_final,TrainIndices,TestIndices,MVA["features"],cat,weight)
        prGreen(MVA["MVAtype"]+" Applying "+MVA["Scaler"])
        exec("from sklearn.preprocessing import "+MVA["Scaler"])
        exec("sc = "+MVA["Scaler"]+"()")
        X_train = sc.fit_transform(X_train)
        X_test = sc.transform(X_test)
        prGreen("DNN fitting running")
        es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10)
        modelDNN=MVA["DNNDict"]['model']
        modelDNN.compile(loss='binary_crossentropy', optimizer=Adam(lr=MVA["DNNDict"]['lr']), metrics=['accuracy',])
        train_history = modelDNN.fit(X_train,Y_train,epochs=MVA["DNNDict"]['epochs'],batch_size=MVA["DNNDict"]['batchsize'],validation_data=(X_test,Y_test, Wt_test),
                                     verbose=1,callbacks=[es], sample_weight=Wt_train)
        modelDNN.save(Conf.OutputDirName+"/"+MVA["MVAtype"]+"/"+MVA["MVAtype"]+"_"+"modelDNN.h5")
        df_final.loc[TrainIndices,MVA["MVAtype"]+"_pred"]=modelDNN.predict(X_train)
        df_final.loc[TestIndices,MVA["MVAtype"]+"_pred"]=modelDNN.predict(X_test)

        prGreen("Plotting output response for DNN")
        fig, axes = plt.subplots(1, 1, figsize=(5, 5))
        plot_mva(df_final.query('TrainDataset==1'),MVA["MVAtype"]+"_pred",bins=50,cat=cat,Wt=weight,ax=axes,sample='train',ls='dashed',logscale=Conf.MVAlogplot)
        plot_mva(df_final.query('TrainDataset==0'),MVA["MVAtype"]+"_pred",bins=50,cat=cat,Wt=weight,ax=axes,sample='test',ls='dotted',logscale=Conf.MVAlogplot)
        plt.savefig(Conf.OutputDirName+"/"+MVA["MVAtype"]+"/"+MVA["MVAtype"]+"_"+"DNNMVA.pdf")
        plt.savefig(Conf.OutputDirName+"/"+MVA["MVAtype"]+"/"+MVA["MVAtype"]+"_"+"DNNMVA.png")

        prGreen("Plotting ROC for DNN")
        fig, axes = plt.subplots(1, 1, figsize=(5, 5))
        plot_roc_curve(df_final.query('TrainDataset==1'),MVA["MVAtype"]+"_pred", tpr_threshold=0, ax=axes, color=None, linestyle='-', label=MVA["Label"]+' Training',cat=cat,Wt=weight)
        plot_roc_curve(df_final.query('TrainDataset==0'),MVA["MVAtype"]+"_pred", tpr_threshold=0, ax=axes, color=None, linestyle='--', label=MVA["Label"]+' Testing',cat=cat,Wt=weight)
        if len(Conf.OverlayWP)>0:
            for color,OverlayWpi in zip(Conf.OverlayWPColors,Conf.OverlayWP):
                plot_single_roc_point(df_final.query('TrainDataset==0'), var=OverlayWpi, ax=axes, color=color, marker='o', markersize=6, label=OverlayWpi+" Test dataset", cat=cat,Wt=weight)
        axes.set_ylabel("Background efficiency")
        axes.set_xlabel("Signal efficiency")
        axes.set_title(MVA["MVAtype"])
        axes.text(1.05, 0.5, 'CMS EGamma ID-Trainer',
            horizontalalignment='center',
            verticalalignment='center',
            rotation='vertical',
            transform=axes.transAxes)
        plt.savefig(Conf.OutputDirName+"/"+MVA["MVAtype"]+"/"+MVA["MVAtype"]+"_"+"DNNROC.pdf")
        plt.savefig(Conf.OutputDirName+"/"+MVA["MVAtype"]+"/"+MVA["MVAtype"]+"_"+"DNNROC.png")

# In[28]:

for MVA in Conf.MVAs:
    if 'Genetic' in MVA["MVAtype"]:
        prGreen("Sorry Genetic algo not implemented yet! Coming Soon")

# In[29]:

##PlotFinalROC
prGreen("Plotting Final ROC")
fig, axes = plt.subplots(1, 1, figsize=(5, 5))
if len(Conf.OverlayWP)>0:
    for color,OverlayWpi in zip(Conf.OverlayWPColors,Conf.OverlayWP):
        plot_single_roc_point(df_final.query('TrainDataset==0'), var=OverlayWpi, ax=axes, color=color, marker='o', markersize=8, label=OverlayWpi+" Test dataset", cat=cat,Wt=weight)
if len(Conf.MVAs)>0:
    for MVAi in Conf.MVAs:
        plot_roc_curve(df_final.query('TrainDataset==0'),MVAi["MVAtype"]+"_pred", tpr_threshold=0.7, ax=axes, color=MVAi["Color"], linestyle='--', label=MVAi["Label"]+' Testing',cat=cat,Wt=weight)
        plot_roc_curve(df_final.query('TrainDataset==1'),MVAi["MVAtype"]+"_pred", tpr_threshold=0.7, ax=axes, color=MVAi["Color"], linestyle='-', label=MVAi["Label"]+' Training',cat=cat,Wt=weight)
    axes.set_ylabel("Background efficiency")
    axes.set_xlabel("Signal efficiency")
    axes.set_title("Final")
    axes.text(1.05, 0.5, 'CMS EGamma ID-Trainer',
        horizontalalignment='center',
        verticalalignment='center',
        rotation='vertical',
        transform=axes.transAxes)
plt.savefig(Conf.OutputDirName+"/ROCFinal.pdf")
plt.savefig(Conf.OutputDirName+"/ROCFinal.png")

# In[30]:

PredMVAs=[]
for MVA in Conf.MVAs:
    PredMVAs.append(MVAi["MVAtype"]+'_pred')
SigEffWPs=Conf.SigEffWPs[:]
for i,SigEffWPi in enumerate(SigEffWPs):
    SigEffWPs[i]=int(SigEffWPi.replace('%', ''))/100

if len(Conf.MVAs)>0:
    prGreen("Threshold values for requested Signal Efficiencies (Train Dataset)")
    mydf=df_final.query("TrainDataset==1 & "+cat+"==1")[PredMVAs].quantile(SigEffWPs)
    mydf.insert(0, "WPs", Conf.SigEffWPs, True)
    mydf.set_index("WPs",inplace=True)
    prGreen(mydf)
    mydf.to_html(Conf.OutputDirName+'/Thresholds/'+"SigEffWPs_Train.html")
    mydf.to_csv(Conf.OutputDirName+'/Thresholds/'+"SigEffWPs_Train.csv")
    prGreen("Threshold values for requested Signal Efficiencies (Test Dataset)")
    mydf2=df_final.query("TrainDataset==0 & "+cat+"==1")[PredMVAs].quantile(SigEffWPs)
    mydf2.insert(0, "WPs", Conf.SigEffWPs, True)
    mydf2.set_index("WPs",inplace=True)
    prGreen(mydf2)
    mydf2.to_html(Conf.OutputDirName+'/Thresholds/'+"SigEffWPs_Test.html")
    mydf2.to_csv(Conf.OutputDirName+'/Thresholds/'+"SigEffWPs_Test.csv")

# In[31]:

pngtopdf(ListPattern=[Conf.OutputDirName+'/*/*ROC*png',Conf.OutputDirName+'/*ROC*png'],Save=Conf.OutputDirName+"/mydocROC.pdf")
pngtopdf(ListPattern=[Conf.OutputDirName+'/*/*MVA*png'],Save=Conf.OutputDirName+"/mydocMVA.pdf")

prGreen("Done!! Please find the quick look ROC pdf here "+Conf.OutputDirName+"/mydocROC.pdf")
prGreen("Done!! Please find the quick look MVA pdf here "+Conf.OutputDirName+"/mydocMVA.pdf")
prGreen("Individual plots and saved model files can be found in directory: "+Conf.OutputDirName+'/')

# In[ ]:

# In[ ]:


from timeit import default_timer as timer
from typing import Optional

data_file = "sample.txt"
data_file = "data.txt"

open_close = {
    "(": ")",
    "[": "]",
    "{": "}",
    "<": ">",
}

all_scores = {")": 3, "]": 57, "}": 1197, ">": 25137}

class Tree:
    def __init__(self, opening: str, parent: Optional["Tree"] = None) -> None:
        self.opening = opening
        self.closing = open_close[opening]
        self.subtrees: list["Tree"] = []
        self.parent = parent
        self.closed = False

    def add_subtree(self, subtree: "Tree") -> None:
        self.subtrees.append(subtree)

start = timer()
with open(data_file) as f:
    lines = f.read().splitlines()

    result = 0
    corrupted_chars = ""

    for l in lines:
        current_tree: Optional[Tree] = None
        for c in l:

            if c in open_close.keys():
                if not current_tree:
                    t = Tree(c)
                else:
                    t = Tree(c, current_tree)
                    current_tree.add_subtree(t)
                current_tree = t
            else:
                if c != open_close[current_tree.opening]:  # Corrupted
                    corrupted_chars += c
                    result += all_scores[c]
                    break
                else:
                    current_tree.closing = c
                    current_tree = current_tree.parent

    print(result)

end = timer()
print(f"Total time: {end - start:.5f} seconds.")


import os
import numpy as np
import random

import torch
import torch.nn as nn

from model import *

import arguments

import utils.load_dataset_Auto
import utils.data_loader
import utils.metrics
from utils.early_stop import EarlyStopping, Stop_args
import fitlog
import debugpy
import time

def para(args):
    if args.dataset == 'yahooR3':
        args.training_args = {'batch_size': 4096, 'epochs': 500, 'patience': 60, 'block_batch': [6000, 500]}
        args.base_model_args = {'emb_dim': 10, 'learning_rate': 0.0005, 'imputaion_lambda': 0.05, 'weight_decay': 10}
        args.weight1_model_args = {'learning_rate': 0.001, 'weight_decay': 0.0001}
        args.weight2_model_args = {'learning_rate': 0.0005, 'weight_decay': 0.001}
        args.imputation_model_args = {'learning_rate': 0.1, 'weight_decay': 0.0001}
        args.auto_temperature = 10
        args.Gama_Auto = 3
        args.w1_ad_lr = 0.1
    elif args.dataset == 'coat':
        args.training_args = {'batch_size': 128, 'epochs': 500, 'patience': 60, 'block_batch': [64, 64]}
        args.base_model_args = {'emb_dim': 10, 'learning_rate': 0.0005, 'imputaion_lambda': 0.05, 'weight_decay': 1}
        args.weight1_model_args = {'learning_rate': 0.0001, 'weight_decay': 1e-4}
        args.weight2_model_args = {'learning_rate': 0.0001, 'weight_decay': 0.001}
        args.imputation_model_args = {'learning_rate': 1e-1, 'weight_decay': 1e-4}
        args.temprate = 10
        args.Gama_Auto = 25
        args.w1_ad_lr = 0.1
    else:
        print('invalid arguments')
        os._exit()

def step_test(loader, model_name, testname, epoch, dataset):
    test_users = torch.empty(0, dtype=torch.int64).to(device)
    test_items = torch.empty(0, dtype=torch.int64).to(device)
    test_pre_ratings = torch.empty(0).to(device)
    test_ratings = torch.empty(0).to(device)
    ndcg_ratings = torch.empty(0).to(device)
    ndcg_item = torch.empty(0).to(device)
    ut_dict={}
    pt_dict={}
    for batch_idx, (users, items, ratings) in enumerate(loader):
        pre_ratings = model_name(users, items)
        for i,u in enumerate(users):
            try:
                ut_dict[u.item()].append(ratings[i].item())
                pt_dict[u.item()].append(pre_ratings[i].item())
            except:
                ut_dict[u.item()]=[ratings[i].item()]
                pt_dict[u.item()]=[pre_ratings[i].item()]
        test_users = torch.cat((test_users, users))
        test_items = torch.cat((test_items, items))
        test_pre_ratings = torch.cat((test_pre_ratings, pre_ratings))
        test_ratings = torch.cat((test_ratings, ratings))

        pos_mask = torch.where(ratings>=torch.ones_like(ratings), torch.arange(0,len(ratings)).float().to(device), 100*torch.ones_like(ratings))
        pos_ind = pos_mask[pos_mask != 100].long()
        users_ndcg = torch.index_select(users, 0, pos_ind)

    test_results = utils.metrics.evaluate(test_pre_ratings, test_ratings, ['MSE', 'NLL', 'AUC', 'Recall_Precision_NDCG@'], users=test_users, items=test_items, NDCG=None, UAUC=(ut_dict, pt_dict))
    return test_results

def both_test(loader, model_name, testname, K = 5, dataset = "None", device='cuda'):
    test_users = torch.empty(0, dtype=torch.int64).to(device)
    test_items = torch.empty(0, dtype=torch.int64).to(device)
    test_pre_ratings = torch.empty(0).to(device)
    test_ratings = torch.empty(0).to(device)
    ndcg_ratings = torch.empty(0).to(device)
    ndcg_item = torch.empty(0).to(device)
    ut_dict={}
    pt_dict={}
    for batch_idx, (users, items, ratings) in enumerate(loader):
        pre_ratings = model_name(users, items)
        for i,u in enumerate(users):
            try:
                ut_dict[u.item()].append(ratings[i].item())
                pt_dict[u.item()].append(pre_ratings[i].item())
            except:
                ut_dict[u.item()]=[ratings[i].item()]
                pt_dict[u.item()]=[pre_ratings[i].item()]
        test_users = torch.cat((test_users, users))
        test_items = torch.cat((test_items, items))
        test_pre_ratings = torch.cat((test_pre_ratings, pre_ratings))
        test_ratings = torch.cat((test_ratings, ratings))

        pos_mask = torch.where(ratings>=torch.ones_like(ratings), torch.arange(0,len(ratings)).float().to(device), 100*torch.ones_like(ratings))
        pos_ind = pos_mask[pos_mask != 100].long()
        users_ndcg = torch.index_select(users, 0, pos_ind)

    test_results = utils.metrics.evaluate(test_pre_ratings, test_ratings, ['MSE', 'NLL', 'AUC', 'Recall_Precision_NDCG@'], users=test_users, items=test_items, NDCG=None, UAUC=(ut_dict, pt_dict))
    print('The {} performance of {} testing set: {}'.format(testname[0], testname[2], ' '.join([key+':'+'%.3f'%test_results[key] for key in test_results])))
    U = test_results['UAUC']
    N = test_results['NDCG']
    print(f'The performance of RD-Autodebias on uniform data is: UAUC = {U} NDCG@5 = {N}')
    return test_results

def setup_seed(seed):
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)

def inner_bound(Gama_Auto, ips_adv, ips_hat):
    upbound = torch.ones_like(ips_hat) + ( ips_hat-torch.ones_like(ips_hat) ) / Gama_Auto
    lowbound = torch.ones_like(ips_hat) + ( ips_hat-torch.ones_like(ips_hat) ) * Gama_Auto
    over_up_mask = torch.where(ips_adv>upbound, torch.ones_like(ips_hat), torch.zeros_like(ips_hat))
    over_low_mask = torch.where(ips_adv<lowbound, torch.ones_like(ips_hat), torch.zeros_like(ips_hat))
    return over_up_mask * upbound + lowbound * over_low_mask + (torch.ones_like(ips_hat) - over_up_mask - over_low_mask) * ips_adv

def train_and_eval(train_data, unif_train_data, val_data, test_data, device = 'cuda',
        base_model_args: dict = "{'emb_dim': 64, 'learning_rate': 0.05, 'imputaion_lambda': 0.01, 'weight_decay': 0.05}",
        weight1_model_args: dict = "{'learning_rate': 0.1, 'weight_decay': 0.005}",
        weight2_model_args: dict = "{'learning_rate': 0.1, 'weight_decay': 0.005}",
        imputation_model_args: dict = "{'learning_rate': 0.01, 'weight_decay': 0.5,'bias': 0}",
        training_args: dict =  "{'batch_size': 1024, 'epochs': 100, 'patience': 20, 'block_batch': [1000, 100]}", args=None):

    train_dense = train_data.to_dense()
    if args.dataset == 'coat' or args.dataset == 'kuai':
        train_dense_norm = torch.where(train_dense<-1*torch.ones_like(train_dense), -1*torch.ones_like(train_dense), train_dense)
        train_dense_norm = torch.where(train_dense_norm>torch.ones_like(train_dense_norm), torch.ones_like(train_dense_norm), train_dense_norm)
        del train_dense
        train_dense = train_dense_norm
    users_unif = unif_train_data._indices()[0]
    items_unif = unif_train_data._indices()[1]
    y_unif = unif_train_data._values()

    train_loader = utils.data_loader.Block(train_data, u_batch_size=training_args['block_batch'][0], i_batch_size=training_args['block_batch'][1], device=device)
    val_loader = utils.data_loader.DataLoader(utils.data_loader.Interactions(val_data), batch_size=training_args['batch_size'], shuffle=False, num_workers=0)
    test_loader = utils.data_loader.DataLoader(utils.data_loader.Interactions(test_data), batch_size=training_args['batch_size'], shuffle=False, num_workers=0)

    n_user, n_item = train_data.shape

    # Base model and its optimizer. This optimizer is for optimize parameters in base model using the updated weights (true optimization).
    base_model = MetaMF(n_user, n_item, dim=base_model_args['emb_dim'], dropout=0).to(device)
    base_optimizer = torch.optim.SGD(base_model.params(), lr=base_model_args['learning_rate'], weight_decay=0)

    # Load the pre-learned propensity of AutoDebias model
    weight1_model = ThreeLinear(n_user, n_item, 2).to(device)
    weight1_model.load_state_dict(torch.load(f"./datasets/{args.dataset}/propensity_auto/ps1.pth.tar"))
    weight1_optimizer = torch.optim.Adam(weight1_model.parameters(), lr=weight1_model_args['learning_rate'], weight_decay=weight1_model_args['weight_decay'])

    weight2_model = ThreeLinear(n_user, n_item, 2).to(device)
    weight2_model.load_state_dict(torch.load(f"./datasets/{args.dataset}/propensity_auto/ps2.pth.tar"))
    weight2_optimizer = torch.optim.Adam(weight2_model.parameters(), lr=weight2_model_args['learning_rate'], weight_decay=weight2_model_args['weight_decay'])

    imputation_model = OneLinear(3).to(device)
    imputation_model.load_state_dict(torch.load(f"./datasets/{args.dataset}/propensity_auto/imp.pth.tar"))
    imputation_optimizer = torch.optim.Adam(imputation_model.parameters(), lr=imputation_model_args['learning_rate'], weight_decay=imputation_model_args['weight_decay'])

    sum_criterion = nn.MSELoss(reduction='sum')
    none_criterion = nn.MSELoss(reduction='none')

    # begin training
    stopping_args = Stop_args(patience=training_args['patience'], max_epochs=training_args['epochs'])
    early_stopping = EarlyStopping(base_model, **stopping_args)

    stable_weight1_model = ThreeLinear(n_user, n_item, 2).to(device)
    stable_weight1_model.load_state_dict(torch.load(f"./datasets/{args.dataset}/propensity_auto/ps1.pth.tar"))

    stable_weight2_model = ThreeLinear(n_user, n_item, 2).to(device)
    stable_weight2_model.load_state_dict(torch.load(f"./datasets/{args.dataset}/propensity_auto/ps2.pth.tar"))
    for epo in range(training_args['epochs']):
        for u_batch_idx, users in enumerate(train_loader.User_loader):
            for i_batch_idx, items in enumerate(train_loader.Item_loader):
                users_train, items_train, y_train = train_loader.get_batch(users, items)
                if args.dataset == 'coat' or args.dataset == 'kuai':
                    y_train = torch.where(y_train<-1*torch.ones_like(y_train), -1*torch.ones_like(y_train), y_train)
                    y_train = torch.where(y_train >1*torch.ones_like(y_train), torch.ones_like(y_train), y_train)

                # all pair
                all_pair = torch.cartesian_prod(users, items)
                users_all, items_all = all_pair[:,0], all_pair[:,1]

                weight1_model.train()
                weight1 = weight1_model(users_train, items_train, (y_train==1) * 1)
                weight1 = torch.exp(torch.nn.ReLU()(weight1/args.auto_temperature))
                weight2_model.train()
                weight2 = weight2_model(users_all, items_all, (train_dense[users_all,items_all]!=0)*1)
                weight2 = torch.exp(torch.nn.ReLU()(weight2/args.auto_temperature))

                imputation_model.train()
                impu_f_all = torch.tanh(imputation_model((train_dense[users_all,items_all]).long()+1))

                # one_step_model: assumed model, just update one step on base model. it is for updating weight parameters
                one_step_model = MetaMF(n_user, n_item, dim=base_model_args['emb_dim'], dropout=0)
                one_step_model.load_state_dict(base_model.state_dict())

                # formal parameter: Using training set to update parameters
                one_step_model.train()
                # all pair data in this block
                y_hat_f_all = one_step_model(users_all, items_all)
                cost_f_all = none_criterion(y_hat_f_all, impu_f_all)
                loss_f_all = torch.sum(cost_f_all * weight2)
                # observation data
                y_hat_f_obs = one_step_model(users_train, items_train)
                cost_f_obs = none_criterion(y_hat_f_obs, y_train)
                loss_f_obs = torch.sum(cost_f_obs * weight1)
                loss_f = loss_f_obs + base_model_args['imputaion_lambda'] * loss_f_all + base_model_args['weight_decay'] * one_step_model.l2_norm(users_all, items_all)

                # update parameters of one_step_model
                one_step_model.zero_grad()
                grads = torch.autograd.grad(loss_f, (one_step_model.params()), create_graph=True)
                one_step_model.update_params(base_model_args['learning_rate'], source_params=grads)

                # latter hyper_parameter: Using uniform set to update hyper_parameters
                y_hat_l = one_step_model(users_unif, items_unif)
                loss_l = sum_criterion(y_hat_l, y_unif)

                # update hyper-parameters
                weight1_optimizer.zero_grad()
                weight2_optimizer.zero_grad()
                imputation_optimizer.zero_grad()
                loss_l.backward()
                # the same setting of AutoDebias, update the weight 1/2 when epo > 20 for stable training.
                if epo > 20 :
                    weight1_optimizer.step()
                    weight2_optimizer.step()
                    imputation_optimizer.step()

                if epo > 20:
                    weight1_model_ad = ThreeLinear(n_user, n_item, 2).to(device)
                    weight2_model_ad = ThreeLinear(n_user, n_item, 2).to(device)
                    imputation_model_ad = OneLinear(3).to(device)

                    weight1_model_ad.load_state_dict(weight1_model.state_dict())
                    weight2_model_ad.load_state_dict(weight2_model.state_dict())
                    imputation_model_ad.load_state_dict(imputation_model.state_dict())

                    weight1_model_ad.train()
                    weight1_ad = weight1_model_ad(users_train, items_train,(y_train==1)*1)
                    weight1_ad = torch.exp(torch.nn.ReLU()(weight1_ad/args.auto_temperature))

                    weight2_model_ad.train()
                    weight2_ad = weight2_model_ad(users_all, items_all,(train_dense[users_all,items_all]!=0)*1)
                    weight2_ad = torch.exp(torch.nn.ReLU()(weight2_ad/args.auto_temperature))

                    imputation_model_ad.train()
                    impu_ad_all = torch.tanh(imputation_model_ad((train_dense[users_all,items_all]).long()+1))

                    one_step_model_ad = MetaMF(n_user, n_item, dim=base_model_args['emb_dim'], dropout=0)
                    one_step_model_ad.load_state_dict(base_model.state_dict())

                    one_step_model_ad.train()
                    # all pair data in this block
                    y_hat_ad_all = one_step_model_ad(users_all, items_all)
                    cost_ad_all = none_criterion(y_hat_ad_all, impu_ad_all)
                    loss_ad_all = torch.sum(cost_ad_all * weight2_ad)
                    # observation data
                    y_hat_ad_obs = one_step_model_ad(users_train, items_train)
                    cost_ad_obs = none_criterion(y_hat_ad_obs, y_train)
                    loss_ad_obs = torch.sum(cost_ad_obs * weight1_ad)
                    loss_ad = loss_ad_obs + base_model_args['imputaion_lambda'] * loss_ad_all + base_model_args['weight_decay'] * one_step_model_ad.l2_norm(users_all, items_all)

                    # update parameters of one_step_model
                    one_step_model_ad.zero_grad()
                    grads = torch.autograd.grad(loss_ad, (one_step_model_ad.params()), create_graph=True)
                    one_step_model_ad.update_params(base_model_args['learning_rate'], source_params=grads)

                    # adversal learning on biased data
                    y_hat_ad = one_step_model_ad(users_train, items_train)
                    loss_ad = -1 * sum_criterion(y_hat_ad, y_train)

                    # adversal update hyper-parameters
                    weight1_ad_optimizer = torch.optim.SGD(weight1_model_ad.parameters(), lr=args.w1_ad_lr, weight_decay=0)
                    weight2_ad_optimizer = torch.optim.SGD(weight2_model_ad.parameters(), lr=args.w1_ad_lr, weight_decay=0)
                    weight1_ad_optimizer.zero_grad()
                    weight2_ad_optimizer.zero_grad()

                    loss_ad.backward()

                    weight1_ad_optimizer.step()
                    weight2_ad_optimizer.step()

                    weight1_after_adv = weight1_model_ad(users_train, items_train,(y_train==1)*1)
                    weight1_after_adv = torch.exp(torch.nn.ReLU()(weight1_after_adv/args.auto_temperature))

                    weight2_after_adv = weight2_model_ad(users_all, items_all,(train_dense[users_all,items_all]!=0)*1)
                    weight2_after_adv = torch.exp(torch.nn.ReLU()(weight2_after_adv/args.auto_temperature))

                    # inner bound
                    ancor_1 = stable_weight1_model(users_train, items_train,(y_train==1)*1)
                    ancor_1 = torch.exp(torch.nn.ReLU()(ancor_1/args.auto_temperature))
                    ancor_2 = stable_weight2_model(users_all, items_all,(train_dense[users_all,items_all]!=0)*1)
                    ancor_2 = torch.exp(torch.nn.ReLU()(ancor_2/args.auto_temperature))
                    weight1 = inner_bound(args.Gama_Auto, weight1_after_adv, ancor_1)
                    weight2 = inner_bound(args.Gama_Auto, weight2_after_adv, ancor_2)
                else:
                    # do not do adversal learning when epo < 20
                    weight1 = weight1_model(users_train, items_train,(y_train==1)*1)
                    weight1 = torch.exp( torch.nn.ReLU()(weight1/args.auto_temperature) )

                    weight2 = weight2_model(users_all, items_all,(train_dense[users_all,items_all]!=0)*1)
                    weight2 = torch.exp(torch.nn.ReLU()(weight2/args.auto_temperature))

                impu_all = torch.tanh(imputation_model((train_dense[users_all,items_all]).long()+1))

                # loss of training set
                base_model.train()
                # all pair
                y_hat_all = base_model(users_all, items_all)
                cost_all = none_criterion(y_hat_all, impu_all)
                loss_all = torch.sum(cost_all * weight2)
                # observation
                y_hat_obs = base_model(users_train, items_train)
                cost_obs = none_criterion(y_hat_obs, y_train)
                loss_obs = torch.sum(cost_obs * weight1)
                loss = loss_obs + base_model_args['imputaion_lambda'] * loss_all + base_model_args['weight_decay'] * base_model.l2_norm(users_all, items_all)

                base_optimizer.zero_grad()
                loss.backward()
                base_optimizer.step()

        base_model.eval()
        if epo % 30 == 0:
            # test metrics on unbias
            step_test_result = step_test(test_loader, base_model, 'unbias', epo, args.dataset)

        with torch.no_grad():
            # training metrics
            train_pre_ratings = torch.empty(0).to(device)
            train_ratings = torch.empty(0).to(device)
            for u_batch_idx, users in enumerate(train_loader.User_loader):
                for i_batch_idx, items in enumerate(train_loader.Item_loader):
                    users_train, items_train, y_train= train_loader.get_batch(users, items)
                    pre_ratings = base_model(users_train, items_train)
                    train_pre_ratings = torch.cat((train_pre_ratings, pre_ratings))
                    train_ratings = torch.cat((train_ratings, y_train))

            # validation metrics
            val_pre_ratings = torch.empty(0).to(device)
            val_ratings = torch.empty(0).to(device)
            for batch_idx, (users, items, ratings) in enumerate(val_loader):
                pre_ratings = base_model(users, items)
                val_pre_ratings = torch.cat((val_pre_ratings, pre_ratings))
                val_ratings = torch.cat((val_ratings, ratings))

        train_results = utils.metrics.evaluate(train_pre_ratings, train_ratings, ['MSE'])
        val_results = utils.metrics.evaluate(val_pre_ratings, val_ratings, ['MSE', 'AUC'])

        print('Epoch: {0:2d} / {1}, Traning: {2}, Validation: {3}'.
                format(epo, training_args['epochs'], ' '.join([key+':'+'%.5f'%train_results[key] for key in train_results]),
                    ' '.join([key+':'+'%.5f'%val_results[key] for key in val_results])))
        if epo >= 50 and early_stopping.check([val_results['AUC']], epo):
            break

    # restore best model
    print('Loading {}th epoch'.format(early_stopping.best_epoch))
    base_model.load_state_dict(early_stopping.best_state)

    test_results = both_test(test_loader, base_model, ('mm', 'DR', 'unbias'), K=5, dataset=args.dataset, device=device)

if __name__ == "__main__":
    args = arguments.parse_args()
    para(args)
    setup_seed(args.seed)
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    train, unif_train, validation, test = utils.load_dataset_Auto.load_dataset(data_name=args.dataset, type = 'explicit', seed = args.seed, device=device)
    train_and_eval(train, unif_train, validation, test, device, base_model_args = args.base_model_args,
                weight1_model_args = args.weight1_model_args, weight2_model_args = args.weight2_model_args, imputation_model_args = args.imputation_model_args, training_args = args.training_args, args=args)


from pathlib import Path
import pandas as pd
import re
from unidecode import unidecode
import numpy as np

import nama
from nama.config import data_dir as nama_dir
from nama.embedding_similarity import load_similarity_model

from canlobby import config

# load DPOH names
dpoh_df = pd.read_csv(Path(config.data_dir)/'raw_data'/'communications_ocl_cal'/'Communication_DpohExport.csv')

# Drop rows with missing name (seems like there are some blank rows for some reason)
dpoh_df = dpoh_df.dropna(subset=['DPOH_FIRST_NM_PRENOM_TCPD','DPOH_LAST_NM_TCPD'])

# Create a combined first-last name variable
dpoh_df['dpoh_raw'] = dpoh_df['DPOH_FIRST_NM_PRENOM_TCPD'] + ' ' + dpoh_df['DPOH_LAST_NM_TCPD']

# Keep only the raw name and the comlog_id
dpoh_df = dpoh_df[['COMLOG_ID','dpoh_raw']]

# Create a new matcher for the names
base_matcher = nama.Matcher(dpoh_df['dpoh_raw'])

# Match names based on simple string cleaning
def clean_name(s):
    # Standardize special characters
    s = unidecode(s)

    # Standardize capitalization
    s = s.lower()

    # Remove common titles such as "Honourable", or "Senator"
    s = re.sub(r"\b(the|l')?( right )?hon(\.|ourable)\b",' ',s)
    s = re.sub(r'\bm(rs|r|s|dr)\.? ',' ',s)
    s = re.sub(r'\b(the|mp|minister|min\.|ambassador|brigadier|conservative|national|rear-admiral|commodore|assistant|deputy|executive)\b',' ',s)

    # Standardize whitespace
    s = re.sub(r'\s+',' ',s.strip())

    return s

# clean_name('The Right Honourable John Numbley')
# clean_name('Deputy Minister John Smith')

matcher = base_matcher.unite(clean_name)

manual_pairs_df = pd.read_csv(Path(config.data_dir)/'raw_data'/'linking'/'dpoh_match_review_SR_Mar4.csv')

# Add manual alway_match information
always_match_pairs_df = manual_pairs_df[~manual_pairs_df['is_match'].isin(['n','?'])]
never_match_pairs_df = manual_pairs_df[manual_pairs_df['is_match']=='n']

base_matcher = base_matcher.unite(always_match_pairs_df[['i','j']].values)

# Add manual never_match information
never_match = [list(pair) for pair in never_match_pairs_df[['i','j']].values]

# Use pre-trained string similarity model to predict likely matches
sim = load_similarity_model(Path(nama_dir)/'models'/'nama_base.bin')
sim.to(config.nama_device)

# Embed the strings
embeddings = sim.embed(base_matcher,device='cpu')

# Swap the similarity model and embeddings on the GPU
sim.to('cpu')
embeddings.to(config.nama_device)

if config.nama_fp16:
    # Use 16bit floats for higher speed (shouldn't affect accuracy)
    embeddings.half()

# Predict matches
predicted_matcher,united_df = embeddings.predict(
                                threshold=0.5,
                                group_threshold=0.5,
                                always_match=base_matcher,
                                never_match=never_match,
                                return_united=True,
                                always_never_conflicts='ignore'
                                )

united_df.to_csv(Path(config.data_dir)/'cleaned_data'/'linking'/'dpoh_match_united_pairs.csv',index=False)

# Create linking tables
predicted_matcher.to_csv(Path(config.data_dir)/'cleaned_data'/'linking'/'dpoh_matches.csv')

dpoh_df['dpoh_clean'] = [predicted_matcher[s] for s in dpoh_df['dpoh_raw']]

dpoh_df.to_csv(Path(config.data_dir)/'cleaned_data'/'linking'/'dpoh_com_linking.csv',index=False)

# Create data for review
_,review_df = embeddings.predict(
                                threshold=0.3,
                                group_threshold=0.3,
                                always_match=base_matcher,
                                never_match=None,
                                return_united=True,
                                always_never_conflicts='ignore'
                                )

review_df = (review_df
            .query('always_match==False')
            .assign(priority=lambda x: (np.sqrt(x['n_i']*x['n_j'])*x['score']*(1-x['score'])).round(2))
            .query('score < 0.8')
            .assign(is_match=np.nan))

review_df.to_csv(Path(config.data_dir)/'cleaned_data'/'linking'/'dpoh_match_review.csv',index=False)


from openerp import api, fields, models

class SaleOrder(models.Model):

    _inherit = 'sale.order'

    # Fields
    #
    partner_contact = fields.Many2one('res.partner', 'Attention')

    @api.multi
    def onchange_partner_id(self, partner_id):

        res = super(SaleOrder, self).onchange_partner_id(partner_id)
        for so in self:
            if not res.get('value'):
                res.update({'value': {}})
            res['value'].update({'partner_contact': False})
            so.partner_contact = False
        return res


#!/usr/bin/env python3

import markup
import sys

for line in sys.stdin:
    line = line.split()
    line = (markup.markup(t, lambda x: x) for t in line)
    line = ' '.join(line)
    print(line)


from sklearn.model_selection import GridSearchCV, RandomizedSearchCV, StratifiedKFold

class SearchParams(object):
    def __init__(self, X_train, y_train, model, hyperparameters):

        self.X_train = X_train
        self.y_train = y_train
        self.model = model
        self.hyperparameters = hyperparameters

    def Kfold(self):
        return StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

    def RandomSearch(self):
        cv = self.Kfold()
        clf = RandomizedSearchCV(self.model,
                                 self.hyperparameters,
                                 random_state=42,
                                 n_iter=100,
                                 cv=cv,
                                 verbose=0,
                                 n_jobs=-1)

        best_model = clf.fit(self.X_train, self.y_train)
        message = (best_model.best_score_, best_model.best_params_)

        print("best: %f using %s" % (message))

        return best_model, best_model.best_params_

    def GridSearch(self):
        cv = self.Kfold()
        clf = GridSearchCV(self.model,
                           self.hyperparameters,
                           cv=cv,
                           verbose=0,
                           n_jobs=-1)
        best_model = clf.fit(self.X_train, self.y_train)
        message = (best_model.best_score_, best_model.best_params_)

        print("best score: %f using %s" % (message))

        return best_model, best_model.best_params_

    def BestModelPredict(self, X_test, random=False):
        if random:
            best_model, _ = self.RandomSearch()
            pred = best_model.predict(X_test)
        else:
            best_model, _ = self.GridSearch()
            pred = best_model.predict(X_test)
        return pred


# Пусть M(N) — произведение 5 наименьших различных натуральных делителей натурального числа N, не считая единицы.
# Если у числа N меньше 5 таких делителей, то M(N) считается равным нулю.
# Найдите 5 наименьших натуральных чисел, превышающих 200 000 000, для которых 0 < M(N) < N.
# В ответе запишите найденные значения M(N) в порядке возрастания соответствующих им чисел N.
# https://inf-ege.sdamgia.ru/problem?id=38959

from math import prod

nums = []
num = 200000001

def f(num):                             # Всё то же самое, что и в 25 (7), только немного другое условие для функции
    dividers = []
    for i in range(2, num+1):           # Перебираем делители числа
        if num % i == 0:
            dividers.append(i)          # Добавляем делитель в список делителей
            if len(dividers) == 5:      # Если их 5, останавливаем цикл
                break
    if prod(dividers) < num:            # Если перемножить все 5 делителей и они меньше самого числа
        return prod(dividers)           # То возвращаем результат их перемножения
    return False                        # Во всех остальных случая возвращаем False

while len(nums) < 5:
    M = f(num=num)
    if M:
        nums.append([num, M])
    num += 1

print(*nums)


# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
l1=ListNode(2)
l1.next= ListNode(3)

l2=ListNode(1)
l2.next= ListNode(6)
print(l1.next.val)
class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        print(l1.next.val)
# Solution.addTwoNumbers(l1,l2)

# class Solution:
#     def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
#         s = l1.val+l2.val
#         head = ListNode(s%10)
#         n = head
#         dummy = ListNode(0)
#         s = s//10
#         while l1.next or l2.next or s:
#             l1 = l1.next or dummy
#             l2 = l2.next or dummy
#             s = s +l1.val+l2.val
#             n.next = ListNode(s%10)
#             n = n.next
#             s = s//10
#         return head