import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from networks.deep_lab import DeepLabv3_plus
from collections import OrderedDict
from networks.tools import save_image
from networks import tools

# The Code of baseline network is referenced from https://github.com/icoz69/CaNet
# The code of training & testing is referenced from https://github.com/xiaomengyc/SG-One

class network(nn.Module):
    """
    Fewshot Segmentation model

    Args:
        in_channels:
            number of input channels
        pretrained_path:
            path of the model for initialization
        cfg:
            model configurations
    """
    def __init__(self, num_heads=15): # + background  part:29  ori:15
        super().__init__()
        self.num_heads = num_heads

        # Encoder os=8
        self.encoder = nn.Sequential(OrderedDict([('backbone', DeepLabv3_plus(nInputChannels=3, n_classes=num_heads, os=16, pretrained=True, _print=True))]))
        #self.encoder = nn.Sequential(OrderedDict([('backbone', Encoder(pretrained_path='./pretrained_model/vgg16-397923af.pth'))]))

        self.GMP = nn.AdaptiveMaxPool2d((1,1))
        self.GAP = nn.AdaptiveAvgPool2d((1,1))

        self.save_pred_list = []
        self.save_bipred_list = []
        self.save_mask_list = []
        self.save_part_mask_list = []

        self.loss_weights = torch.tensor([1.72, 46.1, 47.2, 32.4, 40.9, 49.2, 49.8, 48.7, 47.5, 50.4, 11.8, 49.8,
                                         16.9, 45.1, 32.9, 38.6, 45.3, 50.0, 22.2, 50.1, 39.9, 36.3, 44.0, 29.9,
                                         32.4, 47.8, 45.8, 41.6, 37.9]).type(torch.cuda.FloatTensor)

        # self.loss_weights = torch.tensor([1.80, 33.0, 37.4, 38.4, 18.9, 17.7, 34.5, 9.9, 37.2, 34.0, 23.5, 31.2,
        #                                  34.2, 30.0, 30.2]).type(torch.cuda.FloatTensor)

    def forward(self, img):
        # Extractor
        fts,_ = self.encoder(img) # torch.Size([8, 256, 28, 28])
        fts = F.interpolate(fts, size=img.size()[2:], mode='bilinear', align_corners=True)
        return fts

    def forward_gcn(self, img, mask): # (32,3,224,224)
        b,c,w,h = img.size()

        _, fts = self.encoder(img)  # torch.Size([32, 44, 224, 224]),torch.Size([32, 256, 56, 56])

        mask_c = torch.zeros_like(mask)
        mask_c[mask!=0] = 1.0
        mask_c = F.interpolate(mask_c, size=fts.shape[-2:], mode='bilinear', align_corners=True)

        fts = fts * mask_c #* mask # torch.Size([32, 256, 56, 56])
        fts = self.GAP(fts).view(fts.size(0), -1)

        sum = 0
        count = 0
        for i, itemi in enumerate(fts):
            for j, itemj in enumerate(fts[i+1:]):
                cos = F.cosine_similarity(itemi, itemj, dim=0)
                sum += cos.item()
                count += 1
        return sum/count

    def get_loss(self, cos_map, part_mask):
        b,c,w,h = cos_map.size()
        #loss_fuc = torch.nn.MSELoss(size_average = False)

        loss_fuc = torch.nn.CrossEntropyLoss(weight=self.loss_weights)
        #cos_map = cos_map * part_idx[:, :, None, None]
        mse_loss = loss_fuc(cos_map, part_mask[:,0,:,:].type(torch.cuda.LongTensor))# torch.Size([8, 43, 224, 224])
        return mse_loss

    def save_pred(self,cos_map, mask, part_mask, ii, mode, img=None, cat_idx=None): # torch.Size([8, 43, 224, 224])  torch.Size([8, 224, 224])
        b,c,w,h = cos_map.size()

        mask = mask.detach().cpu().numpy()[:, 0, :, :]

        cos_map = cos_map.detach().cpu().numpy()
        cos_map = np.argmax(cos_map, axis=1)
        bi_cos_map = np.zeros_like(cos_map)
        bi_cos_map[cos_map!=0]=1
        # category = np.unique(mask)[1]
        # bi_cos_map[cos_map != 0] = category

        if mode == 'train' or mode == 'test_voc':
            mask[mask != 0] = 1
            part_mask = part_mask.detach().cpu().numpy()
            save_map = np.concatenate((cos_map, part_mask[:,0,:,:], bi_cos_map, mask), axis=2)
            for j, item in enumerate(save_map):
                tools.labelTopng(item[:,:224], 'for_paper/%d_%d_attpred.png'%(ii,j))
                tools.labelTopng(item[:, 224:448], 'for_paper/%d_%d_attgt.png' % (ii, j))
                tools.labelTopng(item[:, 448:672], 'for_paper/%d_%d_bipred.png' % (ii, j), mode='binary')
                tools.labelTopng(item[:, 672:], 'for_paper/%d_%d_bigt.png' % (ii, j), mode='binary')
                img_s = img[j].cpu().numpy()
                img_s = img_s.swapaxes(0,1).swapaxes(1,2)
                img_s = tools.norm_image(img_s)
                save_image(img_s, 'for_paper/%d_%d_img.jpg'%(ii,j))

            for i in range(b):
                self.save_pred_list.append(np.uint8(cos_map[i]))
                self.save_bipred_list.append(np.uint8(bi_cos_map[i]))
                self.save_mask_list.append(np.uint8(mask[i]))
                self.save_part_mask_list.append(np.uint8(part_mask[i]))

        elif mode == 'test_coco':
            mask[mask != cat_idx] = 0
            mask[mask == cat_idx] = 1
            save_map = np.concatenate((bi_cos_map, mask), axis=2)
            # for j, item in enumerate(save_map):
            #     idx = cat_idx[j]
            #     tools.labelTopng(item[:,:224], 'for_paper_semantic/%d_%d_bipred.png'%(ii,j))
            #     tools.labelTopng(item[:, 224:448], 'for_paper_semantic/%d_%d_bigt.png' % (ii, j))
            #     img_s = img[j].cpu().numpy()
            #     img_s = img_s.swapaxes(0,1).swapaxes(1,2)
            #     img_s = tools.norm_image(img_s)
            #     save_image(img_s, 'for_paper_semantic/%d_%d_img.jpg'%(ii,j))
            for i in range(b):
                self.save_bipred_list.append(np.uint8(bi_cos_map[i]))
                self.save_mask_list.append(np.uint8(mask[i]))

    def save_semantic(self,cos_map, mask,ii): # torch.Size([8, 43, 224, 224])  torch.Size([8, 224, 224])
        b,w,h = cos_map.shape
        mask = mask[:,0]
        save_map = np.concatenate((cos_map, mask), axis=2)
        for i in range(b):
            #print(np.unique(cos_map[i]), np.unique(mask[i]))
            tools.labelTopng(save_map[i], 'check_semantic/%d_%d.png'%(ii,i))
            self.save_pred_list.append(np.uint8(cos_map[i]))
            self.save_mask_list.append(np.uint8(mask[i]))

    def get_pred(self, mode='train'):
        if mode == 'train' or mode == 'test_voc':
            _,_,miou,_ = tools.compute_miou(self.save_bipred_list, self.save_mask_list, n_class=2)
            _, _, part_miou, _ = tools.compute_miou(self.save_pred_list, self.save_part_mask_list, n_class=self.num_heads)
            self.save_pred_list = []
            self.save_bipred_list = []
            self.save_mask_list = []
            self.save_part_mask_list = []
            return miou, part_miou

        elif mode == 'test_coco':
            _,_,miou,_ = tools.compute_miou(self.save_bipred_list, self.save_mask_list, n_class=2)
            self.save_pred_list = []
            self.save_bipred_list = []
            self.save_mask_list = []
            self.save_part_mask_list = []
            return miou, 0.0

    def get_pred_semantic(self):
        _,_,miou,_ = tools.compute_miou(self.save_pred_list, self.save_mask_list, n_class=21)
        self.save_pred_list = []
        self.save_bipred_list = []
        self.save_mask_list = []
        self.save_part_mask_list = []
        return miou


import pandas
import sqlite3
import helpers

# The goal of this program is to create an HTML page with a
# table of CPD allegations:

# 1. Get data from the database:
file_path = helpers.get_file_path('cpd.db', subdirectory='data_sources')
conn = sqlite3.connect(file_path)
df = pandas.read_sql_query('''
        SELECT *
        FROM data_allegation
        WHERE length(summary) > 20
        ORDER BY incident_date desc
        LIMIT 50;
    ''', conn)
pandas.set_option('display.max_colwidth', -1) # so the table does not get truncated.

# 2. Create a simple HTML Template:
template = '''

            <title>{header}</title>
            <link rel="stylesheet" href="styles.css" />

{header}

{summary}

            {table}

'''

# Ask Pandas to create an HTML representation of the table,
# but only for the columns specified in the columns argument:
table_html = table=df.to_html(
    justify='left',
    columns=['id', 'incident_date', 'summary', 'beat_id']
)

# Merge the data with the template using the string format function:
html_text = template.format(
    header='CPD Allegations',
    summary='Here, we can put a summary of this web page',
    table=table_html
)

# write the final HTML page to a file:
file_path = helpers.get_file_path('table_cpd.html', subdirectory='results')
f = open(file_path, 'w')
f.write(html_text)
f.close()

print('\n\nYour webpage has been generated here:\nfile://{0}\n\n'.format(file_path))


from kafka import KafkaProducer
import csv
from time import sleep
import json
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor

# Kafka broker address
bootstrap_servers = '10.18.17.153:9092'
# Kafka topics to send the data
topic_LF = "Heelstrike_LF"
topic_LH = "Heelstrike_LH"
topic_LS = "Heelstrike_LS"
topic_RF = "Heelstrike_RF"
topic_RH = "Heelstrike_RH"
topic_RS = "Heelstrike_RS"
topic_SA = "Heelstrike_SA"

# Create a Kafka producer
producer = KafkaProducer(bootstrap_servers=bootstrap_servers,
                         value_serializer=lambda v: json.dumps(v).encode('utf-8'))

# Load the CSV files into DataFrames
csv_file_LF = "/home/ayemon/KafkaProjects/kafkaspark07_90/heelstrike/heelstrike_10/LF_all_10.csv"
csv_file_LH = "/home/ayemon/KafkaProjects/kafkaspark07_90/heelstrike/heelstrike_10/LH_all_10.csv"
csv_file_LS = "/home/ayemon/KafkaProjects/kafkaspark07_90/heelstrike/heelstrike_10/LS_all_10.csv"
csv_file_RF = "/home/ayemon/KafkaProjects/kafkaspark07_90/heelstrike/heelstrike_10/RF_all_10.csv"
csv_file_RH = "/home/ayemon/KafkaProjects/kafkaspark07_90/heelstrike/heelstrike_10/RH_all_10.csv"
csv_file_RS = "/home/ayemon/KafkaProjects/kafkaspark07_90/heelstrike/heelstrike_10/RS_all_10.csv"
csv_file_SA = "/home/ayemon/KafkaProjects/kafkaspark07_90/heelstrike/heelstrike_10/SA_all_10.csv"

def send_csv_to_kafka_with_label(csv_file, topic):
    with open(csv_file, 'r') as file:
        csv_reader = csv.reader(file)
        header = next(csv_reader)  # Get the header
        for row in csv_reader:
            # Assuming each row is a list of numerical values
            message = dict(zip(header, row))
            producer.send(topic, value=message)
            print(f"Sending data to topic '{topic}': {message}")
            sleep(2)

if __name__ == "__main__":
    with ThreadPoolExecutor(max_workers=7) as executor:
        future1 = executor.submit(send_csv_to_kafka_with_label, csv_file_LF, topic_LF)
        future2 = executor.submit(send_csv_to_kafka_with_label, csv_file_LH, topic_LH)
        future3 = executor.submit(send_csv_to_kafka_with_label, csv_file_LS, topic_LS)
        future4 = executor.submit(send_csv_to_kafka_with_label, csv_file_RF, topic_RF)
        future5 = executor.submit(send_csv_to_kafka_with_label, csv_file_RH, topic_RH)
        future6 = executor.submit(send_csv_to_kafka_with_label, csv_file_RS, topic_RS)
        future7 = executor.submit(send_csv_to_kafka_with_label, csv_file_SA, topic_SA)

        # Wait for both tasks to complete
        concurrent.futures.wait([future1, future2, future3, future4, future5, future6, future7])

    producer.close()


from tkinter import *
from tkinter import messagebox
from tkinter import simpledialog
from tkinter import filedialog
from PIL import Image, ImageTk
from test import oneImg
from test import openCma
import cv2
import os
Flag = True #控制摄像头的变量
capture = cv2.VideoCapture(0)#摄像头对象作为全局变量会更快些

def ui_process():
    root =Tk()
    root.title("人脸检测系统")
    root.geometry("1000x850")

    tempImg=Image.open("D:/working/tensorflow_mtcnn_zip/tensorflow-MTCNN-master/bg.jpg")
    tempImg=tempImg.resize((1000,900))
    bgImg=ImageTk.PhotoImage(tempImg)
    theLabel = Label(root,image=bgImg,compound=CENTER)
    theLabel.pack()

#标签
    L_titile = Label(root,text='基于MTCNN的实时人脸检测系统',)
    L_titile.config(font='Helvetica -15 bold',fg='blue')
    L_titile.place(x=150,y=20,anchor="center")
    L_author = Label(root, text='作者:刘佳昇')
    L_author.config(font='Helvetica -10 bold')
    L_author.place(x=920,y=830)
    S_title = Label(root,text='图片识别模式')
    S_title.config(font='Helvetica -20 bold',fg='red')
    S_title.place(x=40,y=50)
    S_title = Label(root, text='摄像头实时识别模式')
    S_title.config(font='Helvetica -20 bold', fg='red')
    S_title.place(x=40, y=450)
    S_title = Label(root, text='图片识别结果')
    S_title.config(font='Helvetica -15 bold', fg='red')
    S_title.place(x=740, y=100)

#图区
    canvas = Canvas(root, bd=2, relief=SUNKEN)
    canvas.place(x=40,y=140)
    resultCanvas = Canvas(root,bd=2,relief=SUNKEN)
    resultCanvas.place(x=600,y=140)
    #摄像头
    cmaCanvas = Canvas(root,bd=2,relief=SUNKEN,height=350,width=580)
    cmaCanvas.place(x=400,y=470)

#按钮

    B_NO1 = Button(root, text="打开一张图片",command=lambda :OpenFile(canvas,resultCanvas))
    B_NO1.place(x=40, y=100)
    B_NO2 = Button(root, text="打开输出文件夹", command=lambda: openOutPut())
    B_NO2.place(x=600, y=100)
    B_OpenCma = Button(root,text="打开摄像头",command=lambda :openGuiCma(cmaCanvas,root))
    B_OpenCma.place(x=40,y=500)
    B_OpenCma = Button(root, text="关闭摄像头", command=lambda: closeGuiCma())
    B_OpenCma.place(x=140, y=500)

#菜单栏
    menubar = Menu(root)
    filemenu = Menu(menubar, tearoff=0)
    filemenu.add_command(label="Open", command=lambda :openOutPut())
    mainloop()

def openOutPut():
    os.system("start explorer D:\working\\tensorflow_mtcnn_zip\\tensorflow-MTCNN-master\\output")

#文件操作的对话框
def OpenFile(canvas,resultCanvas):
    File = filedialog.askopenfilename(title='打开图片', filetypes=[ ('All Files', '*')])
    print(type(File))
    # resultImg = ImageTk.PhotoImage(Image.fromarray(oneImg(File).astype('uint8')).convert('RGB')) #这个转换方式会导致颜色异常
    resultImg = ImageTk.PhotoImage(Image.fromarray(cv2.cvtColor(oneImg(File),cv2.COLOR_BGR2RGB)))  #这里需要把cv的图像格式转换成PIL.img
    filename = Image.open(File)
    # filename.resize((3500,300))
    filename = ImageTk.PhotoImage(filename)
    print(type(Image.open(File)))
    canvas.image = filename  # <--- keep reference of your image
    canvas.create_image(0, 0, anchor='nw', image=filename)
    resultCanvas.image = resultImg
    resultCanvas.create_image(0,0,anchor='nw',image=resultImg)
    print(File)

def closeGuiCma():
    global Flag
    print(Flag)
    Flag = False

def openGuiCma(resultCanvas,root):
    # 摄像头
    if(messagebox.askokcancel('Python Tkinter', '确认打开摄像头？')==True):
        global Flag
        Flag=True
        while Flag==True:
            ref, frame = capture.read()
            cvimage = cv2.cvtColor(openCma(frame), cv2.COLOR_BGR2RGBA)
            pilImage = Image.fromarray(cvimage)
            tkImage = ImageTk.PhotoImage(image=pilImage)
            resultCanvas.create_image(0, 0, anchor='nw', image=tkImage)
            root.update()
            root.after(100)

if __name__ == "__main__":
    print("开始")
    ui_process()


# search.py
# ---------
# Licensing Information:  You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).

"""
In search.py, you will implement generic search algorithms which are called by
Pacman agents (in searchAgents.py).
"""

import util

class SearchProblem:
    """
    This class outlines the structure of a search problem, but doesn't implement
    any of the methods (in object-oriented terminology: an abstract class).

    You do not need to change anything in this class, ever.
    """

    def getStartState(self):
        """
        Returns the start state for the search problem.
        """
        util.raiseNotDefined()

    def isGoalState(self, state):
        """
          state: Search state

        Returns True if and only if the state is a valid goal state.
        """
        util.raiseNotDefined()

    def getSuccessors(self, state):
        """
          state: Search state

        For a given state, this should return a list of triples, (successor,
        action, stepCost), where 'successor' is a successor to the current
        state, 'action' is the action required to get there, and 'stepCost' is
        the incremental cost of expanding to that successor.
        """
        util.raiseNotDefined()

    def getCostOfActions(self, actions):
        """
         actions: A list of actions to take

        This method returns the total cost of a particular sequence of actions.
        The sequence must be composed of legal moves.
        """
        util.raiseNotDefined()

def tinyMazeSearch(problem):
    """
    Returns a sequence of moves that solves tinyMaze.  For any other maze, the
    sequence of moves will be incorrect, so only use this for tinyMaze.
    """
    from game import Directions
    s = Directions.SOUTH
    w = Directions.WEST
    return  [s, s, w, s, w, w, s, w]

def depthFirstSearch(problem):
    """
    Search the deepest nodes in the search tree first.

    Your search algorithm needs to return a list of actions that reaches the
    goal. Make sure to implement a graph search algorithm.

    To get started, you might want to try some of these simple commands to
    understand the search problem that is being passed in:

    print("Start:", problem.getStartState())
    print("Is the start a goal?", problem.isGoalState(problem.getStartState()))
    print("Start's successors:", problem.getSuccessors(problem.getStartState()))
    """
    frontier = util.Stack() #create a Frontier Stack
    startState = problem.getStartState() #store the Start State and Print it
    print(startState)

    frontier.push((startState,[])) #Push the Start state and an empty list of moves in the frontier.

    alreadyVisited = set() #create an empty set of already vistied states.

    while not frontier.isEmpty(): #check if frontier is not empty.

        currentNodeState, currentNodeActions = frontier.pop() #store the current state and the list of actions of the last node in the stack.

        if(currentNodeState in alreadyVisited): #check if current state has already been visited.
            continue

        if problem.isGoalState(currentNodeState): #check if current state is the goal state.
            return currentNodeActions

        alreadyVisited.add(currentNodeState) #append the current state to the set of already visited states.

        for state, action, cost in problem.getSuccessors(currentNodeState): #Iterate for the successor states.

            if(state in alreadyVisited): #check if state has already been visited.
                continue

            frontier.push((state, currentNodeActions+[action])) #push the state and appended action to the frontier.

    #util.raiseNotDefined()

def breadthFirstSearch(problem):
    """Search the shallowest nodes in the search tree first."""
    frontier = util.Queue() #create a queue for the frontier.

    frontier.push((problem.getStartState(),[])) #push the start state and the empty list of actions to the forntier.

    alreadyVisited = set() #create and empty set of already visited states.

    while not frontier.isEmpty(): #check if frontier is not empty.

        currentNodeState, currentNodeActions = frontier.pop() #store the state and actions of the first node in the queue.

        if(currentNodeState in alreadyVisited): #check if current state is already visited
            continue

        if problem.isGoalState(currentNodeState): #check if cuurent state is goal state, if yes return the list of actions to reach this state.
            return currentNodeActions

        alreadyVisited.add(currentNodeState) #append current state to already visited set.

        for state, action, cost in problem.getSuccessors(currentNodeState): #Iterate for the successor states.

            if(state in alreadyVisited): #check if state is already visited.
                continue

            frontier.push((state, currentNodeActions+[action])) #push the successor states and appended actions to the queue.
    #util.raiseNotDefined()

def uniformCostSearch(problem):
    """Search the node of least total cost first."""
    frontier = util.PriorityQueue() #Create a Priority Queue for the frontier.

    frontier.push((problem.getStartState(),[],0),0)  #push the start state, the empty list of actions and the cost in the frontier.

    alreadyVisited = set() #create an empty set of already visited states.

    while not frontier.isEmpty(): #check if frontier is not empty.

        currentNodeState, currentNodeActions, currentCost = frontier.pop() #store the current state, list of actions and cost of the node from frontier,

        if(currentNodeState in alreadyVisited): #check if already visited state.
            continue

        if problem.isGoalState(currentNodeState): #Check if goal state.
            return currentNodeActions

        alreadyVisited.add(currentNodeState) #append to already visited set.

        for state, action, cost in problem.getSuccessors(currentNodeState): #iterate for the successsor states.

            if(state in alreadyVisited): #check if state is already visited.
                continue

            frontier.push((state, currentNodeActions+[action], currentCost+cost),currentCost+cost) #store the successor states, appended actions and the appended cost in the queue.

    #util.raiseNotDefined()

def nullHeuristic(state, problem=None):
    """
    A heuristic function estimates the cost from the current state to the nearest
    goal in the provided SearchProblem.  This heuristic is trivial.
    """
    return 0

def aStarSearch(problem, heuristic=nullHeuristic):
    """Search the node that has the lowest combined cost and heuristic first."""
    frontier = util.PriorityQueue() #Create a Priority Queue for frontier.

    frontier.push((problem.getStartState(),[],0),0) #push the start state, the empty list of actions, and the cost into the frontier.

    alreadyVisited = set() #create an empty set of already visited states.

    while not frontier.isEmpty(): #Check if frontier is not empty.

        currentNodeState, currentNodeActions, currentCost = frontier.pop() #store current state, list of actions and cost of the node from the queue.

        if(currentNodeState in alreadyVisited): #check if already visited.
            continue

        if problem.isGoalState(currentNodeState): #check if goal state is reached.
            return currentNodeActions

        alreadyVisited.add(currentNodeState) #append to set of already visited states.

        for state, action, cost in problem.getSuccessors(currentNodeState): #iterate for successor nodes, actions and cost.

            if(state in alreadyVisited): #check if already visited.
                continue

            heuristicValue = heuristic(state, problem) #calculate the heurestic

            frontier.push((state, currentNodeActions+[action], currentCost+cost),currentCost+cost+heuristicValue) #push the successor state, appended actions and cost with the heuristic to frontier.

    #util.raiseNotDefined()

# Abbreviations
bfs = breadthFirstSearch
dfs = depthFirstSearch
astar = aStarSearch
ucs = uniformCostSearch


from tkinter import *
import random

app = Tk()
Label(app,text="exercice 2").pack()

aleaNumber = random.randint(1,6)

val=DoubleVar()
Label(app,textvariable=val).pack()
def fct():
    val.set(aleaNumber)
btn=Button(app,text="nombre aleatoire",command=fct).pack()
btn=Button(app,text="exit",command=app.destroy).pack()

app.mainloop()


class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution:
    def buildTree(self, preorder, inorder):
        def recur(root, left, right):
            if left > right: return                               # 递归终止
            # 先序遍历的第一个节点为根节点，构建新节点存入
            node = TreeNode(preorder[root])                       # 建立根节点

            # 在中序的dict里找 根节点 在中序遍历的索引，索引左侧是左子树，右侧是右子树
            i = dic[preorder[root]]                               # 划分根节点、左子树、右子树

            # root+1 代表了左子树的根节点，left->i-1代表所有左子树
            node.left = recur(root + 1, left, i - 1)              # 开启左子树递归
            #
            node.right = recur(i - left + root + 1, i + 1, right) # 开启右子树递归
            return node                                           # 回溯返回根节点

        dic, preorder = {}, preorder
        # 先构建中序遍历的字典，方便查找
        for i in range(len(inorder)):
            dic[inorder[i]] = i
        # 只传入 下标位置 进行递归
        return recur(0, 0, len(inorder) - 1)

    def buildTree1(self, preorder, inorder):
        def recur(preorder_left, preorder_right, inorder_left, inorder_right):
            if preorder_left>preorder_right:
                return
            preorder_root = TreeNode(preorder[preorder_left])
            inorder_index = dic[preorder[preorder_left]]
            # 计算一下左子树的长度
            lenth_left_tree = inorder_index-inorder_left
            # 左子树递归，先序遍历的数组 从 当前根节点+1位置 ，到，根节点+左子树长度
            preorder_root.left = recur(preorder_left+1, preorder_left+lenth_left_tree,
                                       inorder_left, inorder_index-1)

            # 右子树的根节点，从先序里找，Root Left Right,
            # root+1是左子树的根节点，需要知道左子树的长度， root+1 + Lenth(左子树长度)就是右子树根节点
            preorder_root.right = recur(preorder_left+1+lenth_left_tree,
                                        preorder_right, inorder_index+1, inorder_right)
            # 返回当前构造节点的根节点
            return preorder_root

        dic, preorder = {}, preorder
        # 先构建中序遍历的字典，方便查找
        for i in range(len(inorder)):
            dic[inorder[i]] = i
        # 只传入 下标位置 进行递归
        return recur(0, len(preorder)-1, 0,len(inorder) - 1)


WINNER_COMBOS = [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],
  [0, 4, 8],
  [6, 4, 2]
]

board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]

turn = "x"

def print_board():
    print(f"\n{board[0]} | {board[1]} | {board[2]}\n"
          f"= = = = =\n"
          f"{board[3]} | {board[4]} | {board[5]}\n"
          f"= = = = =\n"
          f"{board[6]} | {board[7]} | {board[8]}")

def check_winner():
    for WINNER_COMBO in WINNER_COMBOS:
        if board[WINNER_COMBO[0]] == board[WINNER_COMBO[1]] == board[WINNER_COMBO[2]] != " ":
            return True
    return False

print_board()

while True:
    win = False

    pos = int(input(f"\n{turn}. Escoge una casilla 1 - 9: ")) - 1

    if board[pos] != " ":
        print("\nEsa casilla ya está escogida")
    elif pos < 0 or pos > 8:
        print("\nLa casilla no existe")
    else:
        board[pos] = turn
        print_board()

        if check_winner():
            print(f"\n{turn} ha ganado")
            win = True

    if win:
        break

    turn = "x" if turn == "o" else "o"


def command_check(expression):
    if expression == '/exit':
        print('Bye!')
        return 'exit'

    if expression == '/help':
        print('The program calculates the sum of numbers')
        return 'help'

    return None

def variables_store(expression):
    global vars_stored
    result = None
    key, val = expression.split('=')
    key = key.strip()
    for i in key:
        if not i.isalpha():
            print('Invalid identifier')
            result = 'wrong'
            break
    if result is None:
        val = val.strip()
        try:
            val = int(val)
            vars_stored[key] = val
        except ValueError:
            for j in val:
                if j.isdigit():
                    print('Invalid assignment')
                    result = 'wrong'
                    break
            if result is None:
                if val in vars_stored.keys():
                    vars_stored[key] = vars_stored[val]
                else:
                    print('Unknown variable')
                    result = 'wrong'

    return result

def expression_refactoring(expression):
    # rewrite expression
    expression = expression.replace('---', '-')
    expression = expression.replace('--', '+')
    expression = expression.replace('+++', '+')
    expression = expression.replace('++', '+')
    expression = expression.replace('+', ' + ')
    expression = expression.replace('-', ' - ')
    expression = expression.replace('(', ' ( ')
    expression = expression.replace(')', ' ) ')
    expression = expression.split()

    result = None
    for i in range(len(expression)):
        if expression[i] in ['+', '-']:
            continue
        if expression[i].isdigit():
            continue
        else:
            if valid_variable(*expression[i]) == 'wrong':
                print('Invalid identifier')
                result = 'wrong'
                break
            if expression[i] not in vars_stored.keys():
                result = 'wrong'
                print('Unknown variable')
                break
            if expression[i] in vars_stored.keys():
                expression[i] = str(vars_stored[expression[i]])

    if result == 'wrong':
        return None
    else:
        return expression

def valid_variable(*args):
    result = None
    digit_count = 0
    alpha_count = 0
    for i in args:
        if i.isdigit():
            digit_count += 1
        else:
            alpha_count += 1
    if digit_count > 0 and alpha_count > 0:
        result = 'wrong'
    return result

def calculation(expression):
    result = 0
    operator = '+'
    pre_element = 'operator'

    for element in expression:
        if pre_element == 'digit':
            if element.isdigit():
                result = 'Invalid expression'
            else:
                operator = element
                pre_element = 'operator'

        elif pre_element == 'operator':
            if element.isdigit():
                if operator == '+':
                    result += int(element)
                else:
                    result -= int(element)
                pre_element = 'digit'
            else:
                operator = element
                pre_element = 'operator'

    return result

def main():

    while True:
        expression = input()

        # command check
        command = command_check(expression)
        if command == 'exit':
            break
        elif command == 'help':
            continue
        elif command is None:
            pass

        if len(expression) == 0:
            continue

        # Variable assignment
        if expression.count('=') == 1:
            variables_store(expression)
            continue
        if expression.count('=') > 1:
            print('Invalid assignment')
            continue

        # expression rewrite and calculate
        expression = expression_refactoring(expression)
        if expression is None:
            continue
        else:
            print(calculation(expression))

if __name__ == '__main__':
    vars_stored = dict()
    main()


import random

osztaly = input("kérném az osztály létszámát!")
osztaly = int(osztaly)

print("Az osztály létszáma: ", osztaly)

osszeg = 0
for x in range(1, osztaly + 1):
    osztalyzat = random.randint(1, 5)
    osszeg = osszeg + osztalyzat
    print(osztalyzat)

atlag = osszeg / osztaly
print("átlag: {0:1.2f}".format(atlag))


import sys
input = sys.stdin.readline

def dfs(v):
    for i in range(n):
        if graph[v][i] == 1 and not visited[i]:
            visited[i] = 1
            dfs(i)

n = int(input())
graph = [list(map(int, input().split())) for _ in range(n)]
visited = [0] * n

for i in range(n):
    dfs(i)
    print(*visited)
    visited = [0] * n


import matplotlib.pyplot as plt
import numpy as np

def plot_histogram(data, title, output_filename):
    plt.rc('text', usetex=True)
    plt.rc('font', family='serif')

    fig, axis = plt.subplots()
    X = data[:,0]
    Y = data[:,1]
    axis.set_xlabel(r'association count', fontsize=15)
    axis.set_ylabel(r'number of edges', fontsize=15)

    # add an extra bin for 0
    apx_num_bins = 100
    step = int(X.max()/apx_num_bins)
    bins = range(-step+1, int(X.max())+step+1, step)

    plt.hist(X, bins, weights=Y, log=True, histtype='bar', color='green', align='right')
    plt.savefig(output_filename, bbox_inches='tight')

titles = [ '100000', '100', '25', '400', 'osmGerHist' ]
datasets = [ '100000hist', '100hist', '25hist', '400hist', 'osmGerHist' ]

for i in range(0, len(datasets)):
    dataset = datasets[i]
    title = titles[i]
    data = np.loadtxt('../data/' + dataset)
    output_filename = dataset + '.pdf'
    plot_histogram(data, "\\textsc{" + title.capitalize() + "}", output_filename)


# Refazer desafio 51 usando WHILE
# 51: ler o Primeiro Termo e a Razão de uma PA. Mostrar os 10 primeiros termos dessa progressão.

pa = [int(input('Digite o primeiro termo de sua PA: '))]
r = int(input('E qual seria a razão?: '))

# Usando FOR
# for f in range(1, 10):
# 	pan = pa[0] + f * r
# 	pa.append(pan)
# print(f'Números da PA: {pa}')

# Usando WHILE
c = 1
pan = pa[0]
while c < 10:
	pan = pan + r
	pa.append(pan)
	c += 1
print(f'Números da PA: {pa}')
# Usar a mesta estrutura do FOR, adicionando a linha "c+= 1", também funciona.

# Também da pra imprimir cada linha da PA.
# c = 1
# print('a{} = {}'.format(1, pa[0]))
# while c < 10:
# 	print('a{} = {}'.format(c + 1, pa[0] + c * r))
# 	c += 1


"""
Script to anonymize the classifications

The user names can be deleted and only user ids
are kept.
"""

import pandas as pd
import xarray as xr

raw_data_filename_input = '../zooniverse_raw/sugar-flower-fish-or-gravel-classifications.csv'
raw_data_filename_output = '../zooniverse_raw/sugar-flower-fish-or-gravel-classifications_anonymized.csv'

level1_data_filename_input = '../processed_data/EUREC4A_ManualClassifications_l1.nc'
level1_data_filename_output = '../processed_data/EUREC4A_ManualClassifications_l1_anonymized.nc'

# Anonymize raw data

df = pd.read_csv(raw_data_filename_input)
df = df.iloc[38003:]  # remove data from other classification days
del df['user_name']
df.to_csv(raw_data_filename_output)

# Anonymize level1 data
ds = xr.open_dataset(level1_data_filename_input)
del ds['user_name']
ds.to_netcdf(level1_data_filename_output)


import turtle as t
import random

def turn_up():
    t.left(2)    #위로 올릴때 2도 왼쪽

def turn_down():
    t.right(2)   #내릴때 2도 오른쪽

def fire(): #발사
    ang = t.heading() #각도 기억
    while t.ycor() > 0: # 거북이 땅위에 있는동안 반복
        t.forward(15)
        t.right(5)

    # while문이 끝나면 거북이는 땅
    d = t.distance(target, 0) #목표 지점과 거리
    t.sety(random.randint(10, 100)) #성공 실패 표시할 위치 지정

    if d < 25: #거리 차이가 25미만 명중
        t.color("blue")
        t.write("Good!",False,"center",("",15))
    else: #아니면 실패
        t.color("red")
        t.write("Bad!",False,"center",("",15))
    t.color("black") #거북이 색 검정색으로 되돌리기
    t.goto(-200, 10) #거북이 위치 처음 발사 했던 곳으로 되돌리기
    t.setheading(ang) #각도가 처음 기억해둔 각도로 되돌리기

# 땅그리기
t.goto(-300,0)
t.down()
t.goto(300,0)

# 목표 지점 설정그리기
target = random.randint(50, 150)
t.pensize(3)
t.color("green")
t.up()
t.goto(target - 25,2)
t.down()
t.goto(target + 25,2)

# 거북이 색 검은색 지정 처음 발사했던곳으로 되돌리기
t.color("black")
t.up
t.goto(-200, 10)
t.setheading(20)

# 거북이 동작
t.onkeypress(turn_up, "Up")
t.onkeypress(turn_down, "Down")
t.onkeypress(fire, "space")
t.listen()


# 展示playwright工具的使用
from playwright.sync_api import sync_playwright

def run():
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page()
        page.goto('https://langchain.com/')
        title = page.title()
        print('title=', title)
        browser.close()

if __name__ == '__main__':
    run()


import math as m
import numpy as np
class BinaryImage:
    def __init__(self):
        pass

    def compute_histogram(self, image):

        hist = [0]*256
        height,width=np.shape(image)
        for i in range(height):
            for j in range(width):
                # k= image[i,j]
                hist[image[i,j]]+=1

        return hist

    def find_otsu_threshold(self, hist):

        threshold = 0
        t_value = [0] * 256
        sum=0
        w1,w2,m1,m2,var_left,var_right,wcv,value=0,0,0,0,0,0,0,0

        # print(np.shape(hist))
        size = np.shape(hist)[0]
        for i in range(size):
            sum += hist[i]
        # print("sum", sum)
        for t in range(1,size):
            temp=0
            temp1=0
            temp2=0
            for i in range(t):
                temp+=hist[i]
                temp1+=i*hist[i]
            w1=temp/sum
            if temp!=0:
                m1=temp1/temp
            else:
                m1=0
            for i in range(t ):
                temp2+=m.pow((i-m1),2)*hist[i]
            if temp!=0:
                var_left=temp2/temp
            else:
                var_left=0
            for i in range(t,size):
                temp+=hist[i]
                temp1 += i * hist[i]
            w2=temp/sum
            if temp != 0:
                m2 = temp1 / temp
            else:
                m2 = 0
            for i in range(t,size):
                temp2+=m.pow((i-m2),2)*hist[i]
            if temp!=0:
                var_right=temp2/temp
            else:
                var_right=0
            wcv=(w1*var_left)+(w2*var_right)

            t_value[t]=wcv
        t_value.pop(0)
        value=min(t_value)
        for k in range(np.shape(t_value)[0]):
            if value==t_value[k]:
                threshold=k

        # print("thre",threshold)

        return threshold

    def binarize(self, image):

        hist=BinaryImage.compute_histogram(self,image)
        t=BinaryImage.find_otsu_threshold(self,hist)
        t= 130
        box3 = np.zeros((np.shape(image)[0], np.shape(image)[1]), np.uint8)
        for i in range(np.shape(image)[0]):
            for j in range(np.shape(image)[1]):
                if image[i,j]>t:
                    box3[i,j]=0
                else:
                    box3[i,j]=255

        return box3


# 31. Задайте список из нескольких чисел. Напишите программу, которая найдёт сумму элементов списка,
# стоящих на нечётной позиции.

from random import randint

n = int(input('Введите количество элементов в списке:\n'))

numbers = [randint(-n, n) for i in range(n)]
print('Вот Ваш список:\n', numbers)

print('Сумма чисел списка, стоящих на нечетной позиции:\n', sum(numbers[1::2]))


# -*- coding: utf-8 -*-
"""
Created on Mon Feb  6 16:11:33 2023

@author: SWannell
"""

import matplotlib.pyplot as plt; plt.style.use('ggplot')
import matplotlib.ticker as mtick
import pandas as pd

df = pd.read_csv('AmendedData\\appeal_by_hour.csv', parse_dates=['datehour'])
df.info()

appeal_types = {'Turkey-Syria': '#ee2a24',
                'Haiti': '#850000',
                'Pakistan': '#f86652',
                'Beirut': '#652c4a',
                'Global Coronavirus': '#158aba',
                'National Emergencies Trust': '#1a3351',
                'Afghanistan': '#714200',
                'Ukraine': '#f1b13b'
                }

colour_list = [appeal_types[c] for c in df['appeal'].unique()]

# Plot the first chunk
days = 16
figs, axs = plt.subplots(2, 1, sharex=True, figsize=(8, 10))
for appeal, c in appeal_types.items():
    df_slice = df.loc[df['appeal'] == appeal, ['datehour', 'gifts']]
    df_slice.set_index('datehour', inplace=True)
    df_slice = df_slice.resample('H').sum()
    df_slice = df_slice.reset_index(drop=True)
    # Plot standard
    df_slice.loc[:days*24-1].plot(ax=axs[1], linewidth=3, color=c)
    # Plot cumu
    df_slice_cumu = df_slice.cumsum()
    df_slice_cumu.loc[:days*24-1].plot(ax=axs[0], linewidth=3, color=c)
# Check that resample is working
# [i for i in pd.date_range('2022-02-24', '2022-03-02', freq='H')[:-1] if i not in df_slice.index]
# Format the plot
thoufmt = mtick.StrMethodFormatter('{x:,.0f}')
for ax in axs:
    ax.set_xlim(0)
    ax.set_ylim(0)
    ax.yaxis.set_major_formatter(thoufmt)
# Bottom plot
axs[1].set_ylabel('Gifts')
axs[1].legend().set_visible(False)
day_range = range(0, 24*(days+1), 24)
if days < 4:
    axs[1].set_xticks(day_range)  # label the hours
    axs[1].set_title('By hour', fontsize=15)
    axs[1].set_xlabel('Hours')
else:
    xlist = [[''] * 23 + [i] for i in range(days+1)]
    xlist = [item for sublist in xlist for item in sublist]
    axs[1].set_xticks(day_range, range(0, days+1))  # label the days
    axs[1].set_title('By day', fontsize=15)
    axs[1].set_xlabel('Days')
axs[1].set_ylim(0, 2000)
# Top cumu plot
axs[0].set_ylabel('Gifts (cumulative)')
axs[0].legend(labels=appeal_types.keys())
axs[0].set_title('Cumulative', fontsize=15)
annot = "Data from Google Analytics. 'First week' starts on first 500+ gift\
 day."
figs.text(0.05, 0, annot, color='#1d1a1c', fontsize=10, alpha=0.5)
plt.suptitle('Appeal volumes from launch', fontsize=30)
plt.tight_layout()

# Save plot
last_appeal = df[df['datehour'] == df['datehour'].max()]['appeal'].values[0]
plt.savefig(f'Outputs\\first_{days}_days_{last_appeal}.png')


import numpy as np

import pandas as pd

from fastai.vision import *

from fastai import *

import zipfile

import torch.nn as nn

import os

# print(os.listdir("../input/carvana-image-masking-challenge"))
def unzip_file(path_name):

    with zipfile.ZipFile(path_name, 'r') as zip_ref:

        zip_ref.extractall("/kaggle/working/")
unzip_file("/kaggle/input/carvana-image-masking-challenge/train_masks.zip")

unzip_file("/kaggle/input/carvana-image-masking-challenge/train.zip")
from subprocess import check_output

print(check_output(["ls", "/kaggle/working/"]).decode("utf8"))
path = Path("/kaggle/working/")
image_path = path/"train"

mask_path = path/"train_masks"
img = open_image("/kaggle/input/carvana-image-masking-challenge/29bb3ece3180_11.jpg")

img.show(figsize = (5,5))

img.shape
images = get_image_files("/kaggle/working/train")

images.sort()

images[:5]
masks = get_image_files("/kaggle/working/train_masks")

masks.sort()

masks[:4]
get_masks = lambda x: mask_path/f'{x.stem}_mask.gif'
msk = open_mask(get_masks(images[1]), div = True)

msk.show(figsize = (10,7))
class LabelList(SegmentationLabelList):

    def open(self,fn):

        return open_mask(fn, div=True)

class ItemList(ImageList):

    _label_cls= LabelList
#Defining the dataset

src_size = np.array(msk.shape[1:])

size = src_size//4

codes = ["background", "car"]

# size = 224

bs = 4

src = (ItemList.from_folder(image_path)

       .use_partial_data(sample_pct=0.1)

       .split_by_rand_pct(0.2,42)

       .label_from_func(get_masks, classes = codes))
data = (src.transform(get_transforms(), size = size, tfm_y = True)

        .databunch(bs=bs)

        .normalize(imagenet_stats))
data.show_batch(2,figsize = (7,7))
data.show_batch(2,figsize = (7,7), ds_type = DatasetType.Valid)
wd = 1e-2

learn = unet_learner(data, models.resnet50, metrics = [dice], wd = wd).to_fp16()
learn.lr_find()

learn.recorder.plot()
lr = 1e-03

learn.fit_one_cycle(3,slice(lr), pct_start = 0.9)
learn.save('stage-1')
learn.load('stage-1')
learn.unfreeze()
# common practice

lrs = slice(lr/400, lr/4)
learn.fit_one_cycle(5, lrs, pct_start = 0.8)


from __future__ import print_function
import os
import argparse
import datetime
import six
import math

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
from torchvision import datasets
from torchvision import transforms
from torch.autograd import Variable
from tqdm import tqdm

import numpy as np

from tensorboardX import SummaryWriter
import callbacks

# Training settings
parser = argparse.ArgumentParser(description='Deep Learning JHU Assignment 1 - Fashion-MNIST')
parser.add_argument('--batch-size', type=int, default=256, metavar='B',
                    help='input batch size for training (default: 64)')
parser.add_argument('--test-batch-size', type=int, default=1000, metavar='TB',
                    help='input batch size for testing (default: 1000)')
parser.add_argument('--epochs', type=int, default=10, metavar='E',
                    help='number of epochs to train (default: 10)')
parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
                    help='learning rate (default: 0.01)')
parser.add_argument('--optimizer', type=str, default='sgd', metavar='O',
                    help='Optimizer options are sgd, p1sgd, adam, rms_prop')
parser.add_argument('--momentum', type=float, default=0.5, metavar='MO',
                    help='SGD momentum (default: 0.5)')
parser.add_argument('--no-cuda', action='store_true', default=False,
                    help='disables CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
                    help='random seed (default: 1)')
parser.add_argument('--log_interval', type=int, default=100, metavar='I',
                    help="""how many batches to wait before logging detailed
                            training status, 0 means never log """)
parser.add_argument('--dataset', type=str, default='mnist', metavar='D',
                    help='Options are mnist and fashion_mnist')
parser.add_argument('--data_dir', type=str, default='../data/', metavar='F',
                    help='Where to put data')
parser.add_argument('--name', type=str, default='', metavar='N',
                    help="""A name for this training run, this
                            affects the directory so use underscores and not spaces.""")
parser.add_argument('--model', type=str, default='default', metavar='M',
                    help="""Options are default, P2Q7DefaultChannelsNet,
                    P2Q7HalfChannelsNet, P2Q7DoubleChannelsNet,
                    P2Q8BatchNormNet, P2Q9DropoutNet, P2Q10DropoutBatchnormNet,
                    P2Q11ExtraConvNet, P2Q12RemoveLayerNet, and P2Q13UltimateNet.""")
parser.add_argument('--print_log', action='store_true', default=False,
                    help='prints the csv log when training is complete')

required = object()

def timeStamped(fname, fmt='%Y-%m-%d-%H-%M-%S_{fname}'):
    """ Add a timestamp to your training run's name.
    """
    # http://stackoverflow.com/a/5215012/99379
    return datetime.datetime.now().strftime(fmt).format(fname=fname)

# choose the dataset

def prepareDatasetAndLogging(args):
    # choose the dataset
    if args.dataset == 'mnist':
        DatasetClass = datasets.MNIST
    elif args.dataset == 'fashion_mnist':
        DatasetClass = datasets.FashionMNIST
    else:
        raise ValueError('unknown dataset: ' + args.dataset + ' try mnist or fashion_mnist')

    training_run_name = timeStamped(args.dataset + '_' + args.name)

    kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}

    # Create the dataset, mnist or fasion_mnist
    dataset_dir = os.path.join(args.data_dir, args.dataset)
    training_run_dir = os.path.join(args.data_dir, training_run_name)
    train_dataset = DatasetClass(
        dataset_dir, train=True, download=True,
        transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.1307,), (0.3081,))
        ]))
    train_loader = torch.utils.data.DataLoader(
        train_dataset, batch_size=args.batch_size, shuffle=True, **kwargs)
    test_dataset = DatasetClass(
        dataset_dir, train=False, transform=transforms.Compose([
                        transforms.ToTensor(),
                        transforms.Normalize((0.1307,), (0.3081,))
                    ]))
    test_loader = torch.utils.data.DataLoader(
        test_dataset, batch_size=args.test_batch_size, shuffle=True, **kwargs)

    # Set up visualization and progress status update code
    callback_params = {'epochs': args.epochs,
                       'samples': len(train_loader) * args.batch_size,
                       'steps': len(train_loader),
                       'metrics': {'acc': np.array([]),
                                   'loss': np.array([]),
                                   'val_acc': np.array([]),
                                   'val_loss': np.array([])}}
    if args.print_log:
        output_on_train_end = os.sys.stdout
    else:
        output_on_train_end = None

    callbacklist = callbacks.CallbackList(
        [callbacks.BaseLogger(),
         callbacks.TQDMCallback(),
         callbacks.CSVLogger(filename=training_run_dir + training_run_name + '.csv',
                             output_on_train_end=output_on_train_end)])
    callbacklist.set_params(callback_params)

    tensorboard_writer = SummaryWriter(log_dir=training_run_dir, comment=args.dataset + '_embedding_training')

    # show some image examples in tensorboard projector with inverted color
    images = 255 - test_dataset.test_data[:100].float()
    label = test_dataset.test_labels[:100]
    features = images.view(100, 784)
    tensorboard_writer.add_embedding(features, metadata=label, label_img=images.unsqueeze(1))
    return tensorboard_writer, callbacklist, train_loader, test_loader

# TODO Add classes for every option listed under the --model parser argument above.

# Define the neural network classes

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        # F is just a functional wrapper for modules from the nn package
        # see http://pytorch.org/docs/_modules/torch/nn/functional.html
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2(x), 2))
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)

class P2Q7HalfChannelsNet(nn.Module):
    def __init__(self):
        super(P2Q7HalfChannelsNet, self).__init__()
        # TODO Implement me
        raise NotImplementedError

    def forward(self, x):
        # TODO Implement me
        raise NotImplementedError

class P2Q7DoubleChannelsNet(nn.Module):
    def __init__(self):
        super(P2Q7DoubleChannelsNet, self).__init__()
        # TODO Implement me
        raise NotImplementedError

    def forward(self, x):
        # TODO Implement me
        raise NotImplementedError

class P2Q8BatchNormNet(nn.Module):
    def __init__(self):
        super(P2Q8BatchNormNet, self).__init__()
        # TODO Implement me
        raise NotImplementedError

    def forward(self, x):
        # TODO Implement me
        raise NotImplementedError

class P2Q9DropoutNet(nn.Module):
    def __init__(self):
        super(P2Q9DropoutNet, self).__init__()
        # TODO Implement me
        raise NotImplementedError

    def forward(self, x):
        # TODO Implement me
        raise NotImplementedError

class P2Q10DropoutBatchnormNet(nn.Module):
    def __init__(self):
        super(P2Q10DropoutBatchnormNet, self).__init__()
        # TODO Implement me
        raise NotImplementedError

    def forward(self, x):
        # TODO Implement me
        raise NotImplementedError

class P2Q11ExtraConvNet(nn.Module):
    def __init__(self):
        super(P2Q11ExtraConvNet, self).__init__()
        # TODO Implement me
        raise NotImplementedError

    def forward(self, x):
        # TODO Implement me
        raise NotImplementedError

class P2Q12RemoveLayerNet(nn.Module):
    def __init__(self):
        super(P2Q12RemoveLayerNet, self).__init__()
        # TODO Implement me
        raise NotImplementedError

    def forward(self, x):
        # TODO Implement me
        raise NotImplementedError

class P2Q13UltimateNet(nn.Module):
    def __init__(self):
        super(P2Q13UltimateNet, self).__init__()
        # TODO Implement me
        raise NotImplementedError

    def forward(self, x):
        # TODO Implement me
        raise NotImplementedError

def chooseModel(model_name='default', cuda=True):
    # TODO add all the other models here if their parameter is specified
    if model_name == 'default' or model_name == 'P2Q7DefaultChannelsNet':
        model = Net()
    elif model_name in globals():
        model = globals()[model_name]()
    else:
        raise ValueError('Unknown model type: ' + model_name)

    if args.cuda:
        model.cuda()
    return model

def chooseOptimizer(model, optimizer='sgd'):
    if optimizer == 'sgd':
        optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum)
    elif optimizer == 'adam':
        optimizer = optim.Adam(model.parameters())
    elif optimizer == 'rmsprop':
        optimizer = optim.RMSprop(model.parameters())
    else:
        raise ValueError('Unsupported optimizer: ' + args.optimizer)
    return optimizer

def train(model, optimizer, train_loader, tensorboard_writer, callbacklist, epoch, total_minibatch_count):
    # Training
    model.train()
    correct_count = np.array(0)
    for batch_idx, (data, target) in enumerate(train_loader):
        callbacklist.on_batch_begin(batch_idx)
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)
        optimizer.zero_grad()

        # Forward prediction step
        output = model(data)
        loss = F.nll_loss(output, target)

        # Backpropagation step
        loss.backward()
        optimizer.step()

        # The batch has ended, determine the
        # accuracy of the predicted outputs
        _, argmax = torch.max(output, 1)

        # target labels and predictions are
        # categorical values from 0 to 9.
        accuracy = (target == argmax.squeeze()).float().mean()
        pred = output.data.max(1, keepdim=True)[1]  # get the index of the max log-probability
        correct_count += pred.eq(target.data.view_as(pred)).cpu().sum()

        batch_logs = {
            'loss': np.array(loss.data[0]),
            'acc': np.array(accuracy.data[0]),
            'size': np.array(len(target))
        }

        batch_logs['batch'] = np.array(batch_idx)
        callbacklist.on_batch_end(batch_idx, batch_logs)

        if args.log_interval != 0 and total_minibatch_count % args.log_interval == 0:
            # put all the logs in tensorboard
            for name, value in six.iteritems(batch_logs):
                tensorboard_writer.add_scalar(name, value, global_step=total_minibatch_count)

            # put all the parameters in tensorboard histograms
            for name, param in model.named_parameters():
                name = name.replace('.', '/')
                tensorboard_writer.add_histogram(name, param.data.cpu().numpy(), global_step=total_minibatch_count)
                tensorboard_writer.add_histogram(name + '/gradient', param.grad.data.cpu().numpy(), global_step=total_minibatch_count)

        total_minibatch_count += 1

    # display the last batch of images in tensorboard
    img = torchvision.utils.make_grid(255 - data.data, normalize=True, scale_each=True)
    tensorboard_writer.add_image('images', img, global_step=total_minibatch_count)

    return total_minibatch_count

def test(model, test_loader, tensorboard_writer, callbacklist, epoch, total_minibatch_count):
    # Validation Testing
    model.eval()
    test_loss = 0
    correct = 0
    progress_bar = tqdm(test_loader, desc='Validation')
    for data, target in progress_bar:
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data, volatile=True), Variable(target)
        output = model(data)
        test_loss += F.nll_loss(output, target, size_average=False).data[0]  # sum up batch loss
        pred = output.data.max(1, keepdim=True)[1]  # get the index of the max log-probability
        correct += pred.eq(target.data.view_as(pred)).cpu().sum()

    test_size = np.array(len(test_loader.dataset), np.float32)
    test_loss /= test_size

    acc = np.array(correct, np.float32) / test_size
    epoch_logs = {'val_loss': np.array(test_loss),
                  'val_acc': np.array(acc)}
    for name, value in six.iteritems(epoch_logs):
        tensorboard_writer.add_scalar(name, value, global_step=total_minibatch_count)
    callbacklist.on_epoch_end(epoch, epoch_logs)
    progress_bar.write(
        'Epoch: {} - validation test results - Average val_loss: {:.4f}, val_acc: {}/{} ({:.2f}%)'.format(
            epoch, test_loss, correct, len(test_loader.dataset),
            100. * correct / len(test_loader.dataset)))

    return acc

def run_experiment(args):
    total_minibatch_count = 0
    args.cuda = not args.no_cuda and torch.cuda.is_available()

    torch.manual_seed(args.seed)
    if args.cuda:
        torch.cuda.manual_seed(args.seed)

    epochs_to_run = args.epochs
    tensorboard_writer, callbacklist, train_loader, test_loader = prepareDatasetAndLogging(args)
    model = chooseModel(args.model)
    # tensorboard_writer.add_graph(model, images[:2])
    optimizer = chooseOptimizer(model, args.optimizer)
    # Run the primary training loop, starting with validation accuracy of 0
    val_acc = 0
    callbacklist.on_train_begin()
    for epoch in range(1, epochs_to_run + 1):
        callbacklist.on_epoch_begin(epoch)
        # train for 1 epoch
        total_minibatch_count = train(model, optimizer, train_loader, tensorboard_writer,
                                      callbacklist, epoch, total_minibatch_count)
        # validate progress on test dataset
        val_acc = test(model, test_loader, tensorboard_writer,
                       callbacklist, epoch, total_minibatch_count)
    callbacklist.on_train_end()
    tensorboard_writer.close()

    if args.dataset == 'fashion_mnist' and val_acc > 0.92 and val_acc <= 1.0:
        print("Congratulations, you beat the Question 13 minimum of 92 with ({:.2f}%) validation accuracy!".format(val_acc))

if __name__ == '__main__':
    args = parser.parse_args()
    # Run the primary training and validation loop
    run_experiment(args)


# -*- coding: utf-8 -*-
"""
Created on Mon Aug  2 18:47:35 2021

@author: Alejandro MF
"""
contar=input("Ingrese el # de veces a contar: ")
contar=int(contar)
contador=1
while True:
    print(contador)
    contador+=1
    if contador>contar:
        break


from airflow.hooks.postgres_hook import PostgresHook
from airflow.models import BaseOperator
from helpers import SqlQueries

class LoadFactOperator(BaseOperator):
    ui_color = '#F98866'

    load_fact_table_insert = """
    INSERT INTO {} {}
    """
    load_fact_table_truncate = """
    TRUNCATE TABLE {}
    """

    def __init__(self,
                 sql="",
                 conn_id="",
                 table="",
                 mode="",
                 *args, **kwargs):
        super(LoadFactOperator, self).__init__(*args, **kwargs)
        self.sql = sql
        self.conn_id = conn_id
        self.table = table
        self.mode = mode

    def execute(self, context):

        redshift = PostgresHook(postgres_conn_id=self.conn_id)

        if (self.mode == "append"):
            redshift.run(LoadFactOperator.load_fact_table_insert.format(self.table, self.sql))

        if (self.mode == "truncate"):
            redshift.run(LoadFactOperator.load_fact_table_truncate.format(self.table))
            redshift.run(LoadFactOperator.load_fact_table_insert.format(self.table, self.sql))

            # self.log.info(f"Successful completion of {self.mode} on {self.table}")


import os
import os.path

import cake.filesys
import cake.path
from cake.library import memoise, getPaths
from cake.library.compilers import Compiler, makeCommand
from cake.gnu import parseDependencyFile

class MwcwCompiler(Compiler):

  libraryPrefixSuffixes = [('', '.a')]
  programSuffix = '.elf'
  pchSuffix = '.mch'
  _name = 'mwcw'

  def __init__(
    self,
    configuration,
    ccExe=None,
    ldExe=None,
    binPaths=None,
    ):
    Compiler.__init__(self, configuration=configuration, binPaths=binPaths)
    self.__ccExe = ccExe
    self.__ldExe = ldExe

  def _formatMessage(self, inputText):
    """Format errors to be clickable in MS Visual Studio.
    """
    if self.messageStyle != self.MSVS_CLICKABLE:
      return inputText

    def readLine(text):
      res, _, text = text.partition("\r\n")
      return res, text

    line, inputText = readLine(inputText)
    outputText = ""
    indent = "   "

    while line.count("|") == 2:
      executable, component, type = line.split("|")
      line, inputText = readLine(inputText)

      if line.count("|") == 5:
        path, lineNum, colNum, _, _, _ = line[1:-1].split("|")
        line, inputText = readLine(inputText)
      else:
        path = executable
        lineNum = component
        colNum = None

      contextLines = []
      while line.startswith("="):
        contextLines.append(line[1:])
        line, inputText = readLine(inputText)

      messageLines = []
      while line.startswith(">"):
        messageLines.append(line[1:])
        line, inputText = readLine(inputText)

      outputText += "%s(%s): %s: %s\n" % (
        path,
        lineNum,
        type.lower(),
        messageLines[0],
        )

      # Context from the offending source file
      if contextLines:
        # Write out first line with ^ underneath pointing to the offending column
        outputText += indent + contextLines[0] + "\n"
        if colNum is not None:
          outputText += indent + " " * (int(colNum) - 1) + "^\n"

        # Write out any remaining lines (if any)
        for line in contextLines[1:]:
          outputText += indent + line + "\n"

      if len(messageLines) > 1:
        # Write out the message again if it was multi-line
        for messageLine in messageLines:
          outputText += indent + messageLine + "\n"

    # Write the remaining lines
    if line:
      outputText += line + "\n"
    outputText += inputText.replace("\r", "")

    return outputText

  def _outputStdout(self, text):
    Compiler._outputStdout(self, self._formatMessage(text))

  def _outputStderr(self, text):
    Compiler._outputStderr(self, self._formatMessage(text))

  @memoise
  def _getCommonArgs(self):
    args = [
      '-msgstyle', 'parseable',  # Use parseable message output
      '-nowraplines',            # Don't wrap long lines
      ]

    if self.warningsAsErrors:
      args.extend(['-w', 'error'])

    if self.debugSymbols:
      args.extend(['-sym', 'dwarf-2'])

    return args

  @memoise
  def _getCompileArgs(self, suffix):
    args = [
      self.__ccExe,
      '-c',                      # Compile only
      '-MD',                     # Generate dependency file
      '-gccdep',                 # Output dependency file next to target
      '-gccinc',                 # Use GCC #include semantics
      '-pragma', 'cats off',     # Turn off Codewarrior Analysis Tool
      '-enum', 'int',            # Enumerations always use 'int' for storage
      ]
    args.extend(self._getCommonArgs())

    if self.language is not None:
      args.extend(['-lang', self.language])

    language = self._getLanguage(suffix)
    if language in ['c++', 'cplus', 'ec++']:
      args.extend(self.cppFlags)
    elif language in ['c', 'c99']:
      args.extend(self.cFlags)
    elif language == 'objc':
      args.extend(self.mFlags)

    if self.enableRtti is not None:
      if self.enableRtti:
        args.extend(['-RTTI', 'on'])
      else:
        args.extend(['-RTTI', 'off'])

    if self.enableExceptions is not None:
      if self.enableExceptions:
        args.extend(['-cpp_exceptions', 'on'])
      else:
        args.extend(['-cpp_exceptions', 'off'])

    if self.optimisation == self.NO_OPTIMISATION:
      args.extend([
        '-inline', 'off',
        '-opt', 'off',
        '-ipa', 'off',
        ])
    elif (
      self.optimisation == self.PARTIAL_OPTIMISATION or
      self.optimisation == self.FULL_OPTIMISATION
      ):
      args.extend([
        '-inline', 'all',        # Let the compiler auto inline small functions
        '-str', 'reuse,pool',    # Reuse string constants, place them together
        '-ipa', 'file',          # File level optimisation
        ])

      if self.optimisation == self.PARTIAL_OPTIMISATION:
        args.extend(['-opt', 'level=2']) # Optimisation level 2
      elif self.optimisation == self.FULL_OPTIMISATION:
        args.extend([
          '-opt', 'level=4',       # Optimisation level 4
          '-opt', 'peep',          # Eliminate unnecessary moves/loads/stores
          '-opt', 'schedule',      # Reorder instructions to eliminate stalls
          ])
        # Note: ipa program requires you to:
        #  - link with cc.exe
        #  - pass '-ipa program' to the link line
        #  - pass .irobj's to the link line instead of .o's
        # Even after this the compiler may run out of memory trying
        # to optimise a large program.
        #args.extend(['-ipa', 'program']) # Whole program optimisation

    for p in self.getIncludePaths():
      args.extend(['-i', p])

    args.extend('-D' + d for d in self.getDefines())

    for p in getPaths(self.getForcedIncludes()):
      args.extend(['-include', p])

    return args

  def _getLanguage(self, suffix):
    language = self.language
    if language is None:
      if suffix in self.cSuffixes:
        language = 'c99'
      elif suffix in self.cppSuffixes:
        language = 'c++'
      elif suffix in self.mSuffixes:
        language = 'objc'
    return language

  def getPchCommands(self, target, source, header, object):
    depPath = self._generateDependencyFile(target)
    args = list(self._getCompileArgs(cake.path.extension(source)))
    args.extend([source, '-precompile', target])

    def compile():
      dependencies = self._runProcess(args + ['-MF', depPath], target)
      dependencies.extend(self._scanDependencyFile(depPath, target))
      return dependencies

    canBeCached = True
    return compile, args, canBeCached

  def getObjectCommands(self, target, source, pch, shared):
    depPath = self._generateDependencyFile(target)
    args = list(self._getCompileArgs(cake.path.extension(source)))
    args.extend([source, '-o', target])

    if pch is not None:
      args.extend(['-include', pch.path])

    def compile():
      dependencies = self._runProcess(args + ['-MF', depPath], target)
      dependencies.extend(self._scanDependencyFile(depPath, target))

      if pch is not None:
        dependencies.append(pch.path)

      return dependencies

    canBeCached = True
    return compile, args, canBeCached

  @memoise
  def _getCommonLibraryArgs(self):
    args = [self.__ldExe, '-library']
    args.extend(self._getCommonArgs())
    args.extend(self.libraryFlags)
    return args

  def getLibraryCommand(self, target, sources):
    args = list(self._getCommonLibraryArgs())
    args.extend(['-o', target])
    args.extend(sources)

    @makeCommand(args)
    def archive():
      cake.filesys.remove(self.configuration.abspath(target))
      self._runProcess(args, target)

    @makeCommand("lib-scan")
    def scan():
      # TODO: Add dependencies on DLLs used by ld.exe
      targets = [target]
      dependencies = [args[0]]
      dependencies += sources
      return targets, dependencies

    return archive, scan

  @memoise
  def _getCommonLinkArgs(self, dll):
    args = [self.__ldExe, '-application']
    args.extend(self._getCommonArgs())

    if dll:
      args.extend(self.moduleFlags)
    else:
      args.extend(self.programFlags)

    if self.linkerScript is not None:
      args.extend(['-lcf', self.linkerScript])

    args.extend('-L' + p for p in self.getLibraryPaths())
    return args

  def getProgramCommands(self, target, sources):
    return self._getLinkCommands(target, sources, dll=False)

  def getModuleCommands(self, target, sources, importLibrary, installName):
    return self._getLinkCommands(target, sources, importLibrary, installName, dll=True)

  def _getLinkCommands(self, target, sources, importLibrary=None, installName=None, dll=False):

    objects, libraries = self._resolveObjects()

    args = list(self._getCommonLinkArgs(dll))
    args.extend(sources)
    args.extend(objects)
    args.extend('-l' + l for l in libraries)
    args.extend(['-o', target])

    if self.outputMapFile:
      mapFile = cake.path.stripExtension(target) + '.map'
      args.extend(['-map', mapFile])

    @makeCommand(args)
    def link():
      self._runProcess(args, target)

    @makeCommand("link-scan")
    def scan():
      targets = [target]
      if self.outputMapFile:
        targets.append(mapFile)

      # TODO: Add dependencies on DLLs used by gcc.exe
      # Also add dependencies on system libraries, perhaps
      #  by parsing the output of ',Wl,--trace'
      dependencies = [args[0]]
      dependencies += sources
      dependencies += objects
      dependencies += self._scanForLibraries(libraries)

      return targets, dependencies

    return link, scan

class WiiMwcwCompiler(MwcwCompiler):

  @memoise
  def _getCommonArgs(self):
    args = MwcwCompiler._getCommonArgs(self)
    args.extend([
      '-processor', 'gekko',   # Target the Gekko processor
      '-fp', 'fmadd',          # Use fmadd instructions where possible
      '-sdatathreshold', '0',  # Max size for objects in small data section
      '-sdata2threshold', '0', # Ditto for const small data section
      ])
    return args


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

from sys import argv, exit
from subprocess import Popen, PIPE
from os import path, listdir, sep, name
from datetime import date, datetime, time, timedelta
from PyQt5 import QtWidgets, QtGui, QtCore, QtSql

class MyWindows(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.zandronum_path_string = QtWidgets.QLineEdit()
        if (name == 'posix'):
            self.zandronum_path_string.setText(Popen(['which zandronum'], stdout=PIPE, shell=True).communicate()[0].decode())
        #
        self.cbx_select_iwad = QtWidgets.QComboBox(self)
        self.cbx_pk3s_menu = QtWidgets.QMenu(self)
        #
        self.chbx_networking_game = QtWidgets.QCheckBox()
        self.edit_server = QtWidgets.QLineEdit('ice2heart.com')
        self.edit_server_port = QtWidgets.QLineEdit('10666')
        #
        self.info = QtWidgets.QTextEdit()
        # Список pk3-файлов
        self.pk3s = []
        #
        self.db = self.dbInit()
        self.serverModel = QtSql.QSqlTableModel(self, self.db)
        #
        self.initUI()
        self.cbx_populate()
        # self.saveOptionsload()
        self.makeInfo()

    def initUI(self):
        central_widget = QtWidgets.QWidget(self)
        self.setCentralWidget(central_widget)
        central_widget_grid_layout = QtWidgets.QGridLayout()
        central_widget.setLayout(central_widget_grid_layout)
        #
        file_group = QtWidgets.QGroupBox()
        file_group.setFixedSize(380, 150)
        file_group_layout = QtWidgets.QGridLayout()
        #
        self.zandronum_path_string.setFixedHeight(25)
        btn_select_zandronum_path = QtWidgets.QPushButton("..")
        btn_select_zandronum_path.setFixedSize(25, 25)
        btn_select_zandronum_path.setToolTip("Указать путь к бинарнику Zandronum")
        btn_select_zandronum_path.pressed.connect(lambda: self.openZandronumBinPath())
        # Выбор wad'ов
        self.cbx_select_iwad.currentTextChanged.connect(lambda: self.fileSelection("wad"))
        self.cbx_select_iwad.setFixedSize(225, 25)
        # Выбор pk3-файлов
        cbx_select_pk3 = QtWidgets.QToolButton(self)
        cbx_select_pk3.setMenu(self.cbx_pk3s_menu)
        cbx_select_pk3.setPopupMode(QtWidgets.QToolButton.InstantPopup)
        cbx_select_pk3.triggered.connect(lambda cbx_select_pk3: self.fileSelection("pk3", cbx_select_pk3))
        cbx_select_pk3.setFixedSize(225, 25)
        #
        file_group_layout.addWidget(QtWidgets.QLabel("Путь к zandronum:"), 0, 0)
        file_group_layout.addWidget(self.zandronum_path_string, 1, 0, 1, 2)
        file_group_layout.addWidget(btn_select_zandronum_path, 1, 2)
        file_group_layout.addWidget(QtWidgets.QLabel("iwad-файл:"), 2, 0)
        file_group_layout.addWidget(self.cbx_select_iwad, 2, 1)
        file_group_layout.addWidget(QtWidgets.QLabel("pk3-файл(ы):"), 3, 0)
        file_group_layout.addWidget(cbx_select_pk3, 3, 1)
        # Сеть
        network_group = QtWidgets.QGroupBox()
        network_group_layout = QtWidgets.QGridLayout()
        #
        self.chbx_networking_game.setText("Сетевая игра")
        self.chbx_networking_game.setChecked(True)
        self.chbx_networking_game.stateChanged.connect(lambda: self.networkingGame())
        #
        self.edit_server.setFixedSize(220, 25)
        btn_add_server = QtWidgets.QPushButton()
        btn_add_server.setText("+")
        btn_add_server.setToolTip("Сохранить сервер")
        btn_add_server.setFixedSize(25, 25)
        btn_add_server.pressed.connect(lambda: self.db_queryServer())
        # Таблица с сохранёнными серверами
        self.serverModel.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)
        self.serverModel.setTable('servers')
        self.serverModel.select()
        # self.serverModel.removeColumn(0)
        # self.serverModel.setHeaderData(0, QtCore.Qt.Horizontal, "ID")
        self.serverModel.setHeaderData(1, QtCore.Qt.Horizontal, "Сервер")
        self.serverModel.setHeaderData(2, QtCore.Qt.Horizontal, "Порт")
        tbl_servers = QtWidgets.QTableView()
        tbl_servers.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        tbl_servers.verticalHeader().hide()
        tbl_servers.setSelectionMode(QtWidgets.QTableView.SingleSelection)
        tbl_servers.setSelectionBehavior(QtWidgets.QTableView.SelectRows)
        tbl_servers.setEditTriggers(QtWidgets.QTableView.NoEditTriggers)
        tbl_servers.setAlternatingRowColors(True)
        tbl_servers.setModel(self.serverModel)
        tbl_servers.setColumnHidden(0, True)
        tbl_servers.doubleClicked.connect(lambda: self.db_getSaveServer(tbl_servers.currentIndex())) # Двойной клик по таблице (выбор сохраненного сервера)
        QtWidgets.QShortcut(QtCore.Qt.Key_Delete, tbl_servers, activated=lambda: self.db_queryServer(1, tbl_servers.currentIndex())) # Удаление записи из БД по Delete
        #
        network_group_layout.addWidget(self.chbx_networking_game, 0, 0)
        network_group_layout.addWidget(QtWidgets.QLabel("Сервер"), 1, 0)
        network_group_layout.addWidget(self.edit_server, 2, 0)
        network_group_layout.addWidget(QtWidgets.QLabel("Порт"), 1, 1)
        network_group_layout.addWidget(self.edit_server_port, 2, 1)
        network_group_layout.addWidget(btn_add_server, 2, 2)
        #
        network_group_layout.addWidget(QtWidgets.QLabel("Сохраненные сервера"), 3, 0)
        network_group_layout.addWidget(tbl_servers, 4, 0, 1, 3)
        #
        rungame_group = QtWidgets.QGroupBox()
        rungame_group_layout = QtWidgets.QGridLayout()
        # Информация
        info_group = QtWidgets.QGroupBox()
        info_group_layout = QtWidgets.QGridLayout()
        #
        info_group_layout.addWidget(self.info)
        info_group.setLayout(info_group_layout)
        # Кнопка запуска
        btn_rungame = QtWidgets.QPushButton()
        btn_rungame.setText("Запустить игру!")
        btn_rungame.pressed.connect(lambda: self.runGame())
        rungame_group_layout.addWidget(btn_rungame)
        #
        file_group.setLayout(file_group_layout)
        network_group.setLayout(network_group_layout)
        rungame_group.setLayout(rungame_group_layout)
        #
        central_widget_grid_layout.addWidget(file_group, 0, 0)
        central_widget_grid_layout.addWidget(network_group, 1, 0)
        central_widget_grid_layout.addWidget(info_group, 2, 0)
        central_widget_grid_layout.addWidget(rungame_group, 3, 0)
        #
        self.setWindowTitle("ZLauncher - Zandronum launcher ver. 0.1")
        self.setFixedSize(400, 600)
        self.setCenter()
        self.show()

    # Ручками указать путь к Zandronum
    def openZandronumBinPath(self):
        filename = QtWidgets.QFileDialog().getOpenFileName(self, 'Найти бинарь Zandronum\'а', path.dirname(path.realpath(__file__)))[0]
        if ("zandronum" in filename.lower()):
            self.zandronum_path_string.setText(filename)
        elif (len(filename) < 1):
            pass
        else:
            self.message("Не верное имя файла!\n Укажите путь к \"zandorum\".")

    # Центрирование окна
    def setCenter(self):
        qr = self.frameGeometry()
        cp = QtWidgets.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    # Заполнение ComboBox'ов файлами
    def cbx_populate(self):
        try:
            for i in map(path.splitext, listdir(path.dirname(path.realpath(__file__))+sep+"wads")):
                if (i[1].upper() == ".WAD"):
                    self.cbx_select_iwad.addItem(i[0]+i[1])
                elif (i[1].upper() == ".PK3"):
                    action = self.cbx_pk3s_menu.addAction(i[0]+i[1])
                    action.setCheckable(True)
            self.cbx_select_iwad.setCurrentIndex(0)
        except:
            self.message("Отсутствует каталог с wad-файлами!")

    # Формирование списка файлов
    def fileSelection(self, file, selected = ""):
        if file != "wad" and selected != "":
            if (selected.isChecked()):
                self.pk3s.append(selected.text())
            else:
                self.pk3s.remove(selected.text())
        self.makeInfo()

    # Контроль сетевой игры
    def networkingGame(self):
        if self.chbx_networking_game.isChecked():
            self.edit_server.setEnabled(True)
            self.edit_server_port.setEnabled(True)
        else:
            self.edit_server.setDisabled(True)
            self.edit_server_port.setDisabled(True)
        self.makeInfo()

    # Выбор сохраненного сервера
    def db_getSaveServer(self, currentIndex):
        self.edit_server.setText(str(self.serverModel.record(currentIndex.row()).value(1)))
        self.edit_server_port.setText(str(self.serverModel.record(currentIndex.row()).value(2)))

    # Сохранить сервер в БД или удалить из БД
    def db_queryServer(self, delete = 0, currentIndex = 0):
        query = QtSql.QSqlDatabase(self.db)
        if delete == 1:
            query.exec_("DELETE FROM `servers` WHERE `id` = "+str(self.serverModel.record(currentIndex.row()).value(0)))
            self.message("Сервер удалён.")
        else:
            query.exec_("INSERT INTO `servers`(`host`, `port`) VALUES('"+self.edit_server.text()+"', '"+self.edit_server_port.text()+"')")
            self.message("Сервер добавлен.")
        self.serverModel.select()

    # Инициализация БД
    def dbInit(self):
        dbfile = path.dirname(path.realpath(__file__))+sep+"zlauncher.db"
        db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
        db.setDatabaseName(dbfile)
        if not db.open():
                self.message("Ошибка инициализации БД!")
                return(False)
        if (path.getsize(dbfile) <= 0):
            query = QtSql.QSqlQuery(db)
            query.exec_("CREATE TABLE `servers`(`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `host` VARCHAR(50) NOT NULL, `port` INTEGER(5) NOT NULL)")
            query.exec_("INSERT INTO `servers`(`host`, `port`) VALUES('ice2heart.com', '10666')")
            query.exec_("CREATE TABLE `options`(`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `path` VARCHAR(255), `iwad` VARCHAR(50), `pk3` VARCHAR(50), `network` BOOLEAN, `host` VARCHAR(50), `port` INTEGER(5))")
        return(db)

    # Информация о выбранных компонентах
    def makeInfo(self):
        if self.chbx_networking_game.isChecked():
            server = "Сервер: "+self.edit_server.text()+"  : "+self.edit_server_port.text()
        else:
            server = ""
        self.info.setText("iwad: "+self.cbx_select_iwad.currentText()+
            "
pk3: "+", ".join(self.pk3s)+"
"+server)

    # Формирование параметров запуска
    def makeRunstring(self):
        if len(self.zandronum_path_string.text()) <= 0:
            self.message("На компьютере не обнаружен Zandronum!\n Укажите путь к исполняемому файлу Zandronum.")
            runstring = 0
            return(runstring)
        path_to_wads=path.dirname(path.realpath(__file__))+sep+"wads"+sep
        runstring = str(self.zandronum_path_string.text()).strip() + " "
        if (len(self.cbx_select_iwad.currentText()) > 0):
            runstring += "-iwad " + path_to_wads + self.cbx_select_iwad.currentText() + " "
        else:
            self.message("Отсутствует iwad-фалй!")
            runstring = 0
        if (len(self.pk3s) > 0):
            runstring = runstring + "-file "
            for i in self.pk3s:
                runstring += path_to_wads+i+" "
        if self.chbx_networking_game.isChecked():
            if len(self.edit_server.text()) > 1 and len(self.edit_server_port.text()) > 1:
                runstring += "+connect " + self.edit_server.text().strip() + " -port " + self.edit_server_port.text().strip()
            else:
                self.message("Укажите сервер и порт.")
                runstring = 0
        #
        if (runstring != 0):
            return(runstring)

    # Сохранение и восстановление параметров
    def saveOptionsload(self, runstring = "", save = 1):
        if (save == 1):
            import re
            print(re.split("(-iwad|-file|\+connect)", runstring))
            # rs = runstring.split(' ')
            # zp = rs.pop(0)
            # rs.pop(0)
            # iwad = rs.pop(0).split(sep)[-1]
            # rs.pop(0)
            # pk3 = []
            # server = ""
            # port = ""

            # # for i in (x for x in rs if x != "+connect"):
            # for i in rs:
            #     if i != "+connect":
            #         print(i)
            #     else:
            #         break

    # Запуск игры
    def runGame(self):
        runstring = self.makeRunstring()
        if (runstring != 0):
            print(runstring)
            # self.saveOptionsload(runstring)
            Popen(runstring, shell=True)

    # Сообщения
    def message(self, msg):
        mb = QtWidgets.QMessageBox()
        mb.question(self, "Сообщение", msg, QtWidgets.QMessageBox.Ok)

if __name__ == '__main__':
    app = QtWidgets.QApplication(argv)
    w = MyWindows()
    exit(app.exec_())


# -*- coding: utf-8 -*-
import numpy as np
import pickle
import os
import gc
from typing import Dict

curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0] + "/Support/chapter02"
NBayesPath = rootPath + "/train_word_bag/NBayes.dat"

def writeBunch(path, bunch):
    fileObj = open(path, "wb")
    pickle.dump(bunch, fileObj)
    fileObj.close()

def loadDataSet():
    postingList = [['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],
                   ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],
                   ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him', 'my'],
                   ['stop', 'posting', 'stupid', 'worthless', 'garbage'],
                   ['mr', 'licks', 'ate', 'umy', 'steak', 'how', '84', '8stop', 'asawsx'],
                   ['mr554', 'licks', 'ate', 'umy', 'steak', 'how', 'ftdto', '8stop', 'asawsx'],
                   ['mr', 'licks', 'ate', 'umy', 'st55eak', 'how', 'ftdto', '8stop', 'asawsx'],
                   ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']]
    classVec = [0, 1, 0, 1, 3, 3, 3, 1]  # 1 is abusive, 0 not
    return postingList, classVec

# 写入bunch对象
def self(path, bunchobj):
    file_obj = open(path, "wb")
    pickle.dump(bunchobj, file_obj)
    file_obj.close()

isTFIDF = True

class ClassData(object):
    def __init__(self, classIndex, label, vocaLength, rate):
        self.label = label
        self.vocaLength = vocaLength
        self.classIndex = classIndex
        self.tfs = []
        self.rate = rate
        self.tdm = None

    def putTf(self, tf):
        self.tfs.append(tf)
        return self.tfs

    def calPXY(self, idf):
        self.tdm = np.zeros([1, self.vocaLength])
        for tf in self.tfs:
            self.tdm += np.multiply(tf, idf)
        if isTFIDF:
            self.tdm /= float(np.shape(self.tfs)[0])
        else:
            self.tdm /= float(np.shape(self.tfs)[0])

    def toPredict(self, testVec):

        if isTFIDF:
            weight = np.sum(np.multiply(self.tdm, testVec))  # 这种效果比上面好.上面的符合理论
        else:
            weight = np.sum(np.log(np.multiply(self.tdm, testVec) + 1)) * self.rate  # 朴素贝叶斯
        return weight

class NBayes(object):
    def __init__(self):
        self.vocabularys = []  # 词典
        self.vocabularyIndex = {}  # 字典表记录词典索引
        self.classMap = {}  # P(yi)--是个类别字典,结构Pcates[label]
        self.stopWordDict = None
        self.idf = None
        self.classIndex: Dict = None

    def makeTrain(self, trainMatrix, classVec):

        self.vocabularys = list(set([word for i in range(len(trainMatrix)) for word in trainMatrix[i]]))
        self.vocabularyIndex = {name: index for index, name in enumerate(self.vocabularys)}  # 索引序列

        self.makeClassMap(classVec)  # 计算每个分类在总类别集合中出现的概率：P(yi)
        if isTFIDF:
            self.calTfIdf(trainMatrix, classVec)  # 生成tf-idf权值
        else:
            self.calWordFreq(trainMatrix, classVec)  # 朴素贝叶斯

        gc.collect()
        self.makeTdm()  # 按分类累计向量空间的每维值：P(x|yi),X词在当前类别出现的次数/类别下所有词数(yi)

    # 生成 tf-idf
    def calTfIdf(self, trainMatrix, classVec):
        idf = np.zeros([len(self.classIndex), len(self.vocabularys)])

        for i in range(np.shape(trainMatrix)[0]):
            tf = np.zeros([1, len(self.vocabularys)])
            if i % 100 == 0:
                print("第", i + 1, "次运行")
            classData: ClassData = self.classMap[classVec[i]]
            for word in set(trainMatrix[i]):
                index = self.vocabularyIndex[word]
                tf[0, index] += trainMatrix[i].count(word)
                idf[self.classIndex[classVec[i]], index] += 1
            # 统计文本词频TF
            tf[0] = tf[0] / float(np.sum(tf[0]))
            classData.putTf(tf)
        self.idf = np.log(float(np.shape(trainMatrix)[0] + 1) / (np.sum(idf, axis=0)))  # 理论上要这个
        idf = idf.clip(max=1)
        idf = np.sum(idf, axis=0)
        self.idf *= np.log(float(len(self.classIndex) + 1) / (idf))  # 我加了这个

    """
    生成词频
    适配TFIDF
    """

    def calWordFreq(self, trainMatrix, classVec):
        self.idf = np.ones([1, len(self.vocabularys)])
        for i in range(np.shape(trainMatrix)[0]):
            tf = np.zeros([1, len(self.vocabularys)])
            if i % 100 == 0:
                print("第", i + 1, "次运行")
            classData: ClassData = self.classMap[classVec[i]]
            for word in set(trainMatrix[i]):
                index = self.vocabularyIndex[word]
                tf[0, index] += trainMatrix[i].count(word)
            tf[0, index] /= np.sum(tf[0, index])
            classData.putTf(tf)

    """
    生成分类字典,统计每个类别出现的概率P(yi)
    生成分类索引字典
    """

    def makeClassMap(self, classVec):
        classList = list(set(classVec))
        for index, label in enumerate(classList):  # 对分类进行去重遍历
            rate = float(classVec.count(label)) / float(np.alen(classVec))
            self.classMap[label] = ClassData(index, label, np.alen(self.vocabularys), rate)
        self.classIndex = {k: i for i, (k, v) in enumerate(self.classMap.items())}

    """
     按分类计算P(x|yi)
    """

    def makeTdm(self):
        print("构建分类权重")
        for label, classData in self.classMap.items():
            classData.calPXY(self.idf)
        print("分类权重构建完毕")

    def getDumpData(self):
        dictData = {}
        dictData["vocabularys"] = self.vocabularys
        dictData["vocabularyIndex"] = self.vocabularyIndex
        for label, classData in self.classMap.items():
            classData.tfs = None
        dictData["classMap"] = self.classMap
        dictData["stopWordDict"] = self.stopWordDict
        return dictData

    def loadDumpData(self, dictData):
        self.vocabularys = dictData["vocabularys"]
        self.vocabularyIndex = dictData["vocabularyIndex"]
        self.classMap = dictData["classMap"]
        self.stopWordDict = dictData["stopWordDict"]
        return self

    # 构建测试集合
    def makeTest(self):
        print("构造预测函数")

        def predict(testMatrix, nb: NBayes):
            print("准备预测")
            testMatrix = nb.deleteStopWord(testMatrix)
            testDetail = [{"matrixIndex": i, "all": 0, "notFind": 0} for i in range(np.shape(testMatrix)[0])]
            testTfMatrix = np.zeros([np.shape(testMatrix)[0], len(nb.vocabularys)])
            for i in range(np.shape(testTfMatrix)[0]):
                testDetail[i]["all"] = len(testMatrix[i])
                notFind = 0
                for word in testMatrix[i]:
                    index = nb.vocabularyIndex[word] if word in nb.vocabularyIndex else None
                    if index is not None:
                        testTfMatrix[i, index] += 1
                    else:
                        notFind += 1
                testDetail[i]["notFind"] = notFind

                testTfMatrix[i] = testTfMatrix[i] / float(np.sum(testTfMatrix[i]))

            testClassList = [0] * np.shape(testTfMatrix)[0]
            print("开始预测")
            for i in range(np.shape(testTfMatrix)[0]):
                predvalue = 0
                retClass = 0
                for label, classData in nb.classMap.items():
                    # P(x|yi)P(yi)
                    value = classData.toPredict(testTfMatrix[i])
                    if value > predvalue:
                        predvalue = value
                        retClass = label
                testClassList[i] = retClass
            print("预测结束")
            return testClassList, testDetail

        return predict

    """
    去除停用词
    """

    def deleteStopWord(self, matrix, stopWords=None):
        if (stopWords is not None):
            self.stopWordDict = set(stopWords)
            self.stopWordDict.add("")
            self.stopWordDict.add('\ufeff')
        print("正在删除停用词")
        for i in range(len(matrix)):
            for j in range(len(matrix[i]) - 1, -1, -1):
                word = matrix[i][j]
                if word in self.stopWordDict:
                    matrix[i].pop(j)

        print("停用词删除完毕")
        return matrix


"""
1499. Max Value of Equation
You are given an array points containing the coordinates of points on a 2D plane, sorted by the x-values, where points[i] = [x_i, y_i] such that x_i < x_y for all 1 <= i < j <= points.length. You are also given an integer k

Return the maximum value of the equation y_i + y_j + |x_i - x_j| where |x_i - x_j| <= k and 1 <= i < j <= points.length.

It is guaranteed that there exists at least one pair of points that satisfy the constraint |x_i - x_j| <= k.

Example1:
Input: points = [[1,3],[2,0],[5,10],[6,-10]], k = 1
Output: 4
Explanation: The first two points satisfy the condition |xi - xj| <= 1 and if we calculate the equation we get 3 + 0 + |1 - 2| = 4. Third and fourth points also satisfy the condition and give a value of 10 + -10 + |5 - 6| = 1.
No other pairs satisfy the condition, so we return the max of 4 and 1.

Example2:
Input: points = [[0,0],[3,0],[9,2]], k = 3
Output: 3
Explanation: Only the first two points have an absolute difference of 3 or less in the x-values, and give the value of 0 + 0 + |0 - 3| = 3.

Constraints:
2 <= points.length <= 10^5
points[i].length == 2
-10^8 <= x_i, y_i <= 10^8
0 <= k <= 2 * 10^8
x_i < x_j for all 1 <= i < j <= points.length
x_i form a strictly increasing sequence.
"""

"""
Note:
1. maxHeap: O(nlogn) time | O(n) space - where n is the number of points
2. monotonic queue: O(n) time | O(n) space - where n is the number of points
"""

import collections
from typing import List
import unittest
import heapq
class Solution:
    def findMaxValueOfEquation(self, points: List[List[int]], k: int) -> int:
        # yi + yj + | xi - xj | = (yi - xi) + (yj + xj)
        # |x_i - x_j| <= k
        maxVal = float("-inf")
        maxHeap = []
        for x, y in points:
            # x - maxHeap = |x_i - x_j|
            # pop out invalid points
            while maxHeap and x - maxHeap[0][1] > k:
                heapq.heappop(maxHeap)

            if maxHeap:
                # -maxHeap[0][0] = (yi - xi)
                # y + x = y_j + x_j
                maxVal = max(maxVal, -maxHeap[0][0] + (y + x))

            heapq.heappush(maxHeap, (-(y - x), x))
        return maxVal

    def findMaxValueOfEquation2(self, points: List[List[int]], k: int) -> int:
        # yi + yj + | xi - xj | = (yi - xi) + (yj + xj)
        maxVal = float("-inf")
        queue = collections.deque()

        for x, y in points:
            while queue and x - queue[0][1] > k:
                queue.popleft()

            if queue:
                maxVal = max(maxVal, queue[0][0] + (y + x))

            # queue[-1][0] = (yi - xi)
            # pop out all candidates with smaller yi - xi
            while queue and queue[-1][0] <= y - x:
                queue.pop()

            queue.append((y - x, x))
        return maxVal

# Unit Tests
funcs = [Solution().findMaxValueOfEquation, Solution().findMaxValueOfEquation2]

class TestFindMaxValueOfEquation(unittest.TestCase):
    def testFindMaxValueOfEquation1(self):
        for func in funcs:
            points = [[1, 3], [2, 0], [5, 10], [6, -10]]
            k = 1
            self.assertEqual(func(points=points, k=k), 4)

    def testFindMaxValueOfEquation2(self):
        for func in funcs:
            points = [[0, 0], [3, 0], [9, 2]]
            k = 3
            self.assertEqual(func(points=points, k=k), 3)

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


from py2max import Patcher

def test_layout_vertical():
    p = Patcher('outputs/test_layout_vertical.maxpat', layout="vertical")

    fbox = p.add_floatbox
    ibox = p.add_intbox
    tbox = p.add_textbox
    link = p.add_line

    # objects
    freq1 = fbox()
    freq2 = fbox()
    phase = fbox()
    osc1 = tbox('cycle~')
    osc2 = tbox('cycle~')
    amp1 = fbox()
    amp2 = fbox()
    mul1 = tbox('*~')
    mul2 = tbox('*~')
    add1 = tbox('+~')
    dac = tbox('ezdac~')
    scop = tbox('scope~')
    scp1 = ibox()
    scp2 = ibox()

    # lines
    link(freq1, osc1)
    link(osc1, mul1)
    link(mul1, add1)
    link(amp1, mul1, inlet=1)
    link(freq2, osc2)
    link(phase, osc2, inlet=1)
    link(osc2, mul2)
    link(amp2, mul2, inlet=1)
    link(mul2, add1, inlet=1)
    link(add1, dac)
    link(add1, dac, inlet=1)
    link(add1, scop)
    link(scp1, scop)
    link(scp2, scop, inlet=1)
    p.save()

if __name__ == '__main__':
    test_layout_vertical()