#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov  2 10:39:10 2022

@author: patry
"""
import sys
import random
check =False
while check == False:
    check =True
    file = open("labyrinthe.txt","w+")

    height =int(sys.argv[1])
    width =int(sys.argv[2])
    chars =['*',' ','o','1','2']
    gates='$'
    entry = random.randint(0, width-4) +2
    entry2= random.randint(0, width-4) +2
    file.write(str(height))
    file.write('X')
    file.write(str(width))
    for i in range(len(chars)):
        file.write(str(chars[i]))
    file.write('\n')
    for i in range(height):
        for j in range(width):
            if i == 0 and j == entry:
                file.write(chars[3])
            elif i == height -1 and j == entry2:
                file.write(chars[4])
            elif 0<i<height-1 and 0<j<width - 1 and random.randint(0, 100) > 20:
                file.write(chars[1])
            else :
                file.write(chars[0])
        file.write('\n')

    file=open("labyrinthe.txt",'r')
    mat=file.readlines()

    del mat[0]

    def split(s):
        return [char for char in s]
    for i in range(len(mat)):
        mat[i]=split(mat[i])
    for i in range(len(mat)):
        for j in range(len(mat[i])):
            if j == len(mat[i])-1:
                del mat[i][j]

    for i in range(len(mat[0])):
        if mat[0][i]==str(1):
            x=i
    if mat[1][x]=='*':
        check=False
    long=len(mat)
    for i in range(len(mat[long-1])):
        if mat[long-1][i]==str(2):
            absi_2=i
    if mat[long-2][absi_2]=='*':
        check=False


#contributedByAasthaChauhan
class Graph:
    def __init__(self, edges, n):
        self.adjList = [[] for _ in range(n)]

        for (src, dest) in edges:
            self.adjList[src].append(dest)
            self.adjList[dest].append(src)

# Function to assign colors to vertices of a graph
def colorGraph(graph, n):

    result = {}

    for u in range(n):

        assigned = set([result.get(i) for i in graph.adjList[u] if i in result])

        color = 1
        for c in assigned:
            if color != c:
                break
            color = color + 1

        result[u] = color

    for v in range(n):
        print(f'Color assigned to vertex {v} is {colors[result[v]]}')

if __name__ == '__main__':

    colors = ['', 'BLUE', 'GREEN', 'RED', 'YELLOW', 'ORANGE', 'PINK',
            'BLACK', 'BROWN', 'WHITE', 'PURPLE', 'VOILET']

    edges = [(0, 1), (0, 4), (0, 5), (4, 5), (1, 4), (1, 3), (2, 3), (2, 4)]

    n = 6

    graph = Graph(edges, n)

    colorGraph(graph, n)


import os
import sys

from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QGridLayout, QWidget, QPushButton, QLabel, QVBoxLayout

from PaintWidgets.TrianglePaintWidget import TrianglePaintWidget
from menus.cilinderShape import CilinderMenu
from menus.circleShape import CircleMenu
from menus.coneShape import ConeMenu
from menus.cubeShape import CubeMenu
from menus.ellipseShape import EllipseMenu
from menus.kiteShape import KiteMenu
from menus.parellogramShape import ParellogramMenu
from menus.prismaTriangleShape import PrismaTriangleMenu
from menus.pyramidShape import PyramidMenu
from menus.sphereShape import SphereMenu
from menus.squareShape import SquareMenu
from menus.squareTriangleShape import SquareTriangleMenu
from menus.trapeziumShape import TrapeziumMenu
from menus.triangleShape import TriangleMenu

class Menu(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Shapes By Kjell Vos")

        #self.setFixedWidth(600)
        #self.setFixedHeight(600)

        #self.central = QWidget()
        self.central = TrianglePaintWidget(None)
        #self.central.setContentsMargins(10, 10, 10, 10)

        self.setCentralWidget(self.central)
        #self.layout = self.mainMenuUI()
        #self.central.setLayout(self.layout)

    def goToMenu(self):
        self.setWindowTitle("Shapes By Kjell Vos")

        #self.setFixedWidth(600)
        #self.setFixedHeight(600)

        deleteItemsOfLayout(self.layout)
        self.central = QWidget()
        self.setCentralWidget(self.central)
        self.layout = self.mainMenuUI()
        self.central.setLayout(self.layout)

    def mainMenuUI(self):
        """Square"""
        # Button
        squareButton = QPushButton()
        squareButton.clicked.connect(self.squareClick)
        squareButton.setIcon(QIcon('assets/square.png'))
        squareButton.setIconSize(QSize(100, 100))
        # squareButton.setFixedSize(QSize(105, 105))
        # Label
        squareLabel = QLabel("Square")
        squareLabel.setAlignment(Qt.AlignCenter)
        # VBox
        squareVBox = QVBoxLayout()
        squareVBox.addWidget(squareButton)
        squareVBox.addWidget(squareLabel)

        """Circle"""
        # Button
        circleButton = QPushButton()
        circleButton.clicked.connect(self.circleClick)
        circleButton.setIcon(QIcon('assets/circle.png'))
        circleButton.setIconSize(QSize(100, 100))
        # Label
        circleLabel = QLabel("Circle")
        circleLabel.setAlignment(Qt.AlignCenter)
        # VBox
        circleVBox = QVBoxLayout()
        circleVBox.addWidget(circleButton)
        circleVBox.addWidget(circleLabel)

        """Triangle"""
        # Button
        triangleButton = QPushButton()
        triangleButton.clicked.connect(self.triangleClick)
        triangleButton.setIcon(QIcon('assets/triangle.png'))
        triangleButton.setIconSize(QSize(100, 100))
        # Label
        triangleLabel = QLabel("Triangle")
        triangleLabel.setAlignment(Qt.AlignCenter)
        # VBox
        triangleVBox = QVBoxLayout()
        triangleVBox.addWidget(triangleButton)
        triangleVBox.addWidget(triangleLabel)

        """Pythagorean Triangle"""
        # Button
        squareTriangleButton = QPushButton()
        squareTriangleButton.clicked.connect(self.squareTriangleClick)
        squareTriangleButton.setIcon(QIcon('assets/squareTriangle.png'))
        squareTriangleButton.setIconSize(QSize(100, 100))
        # Label
        squareTriangleLabel = QLabel("Pythagorean triangle")
        squareTriangleLabel.setAlignment(Qt.AlignCenter)
        # VBox
        squareTriangleVBox = QVBoxLayout()
        squareTriangleVBox.addWidget(squareTriangleButton)
        squareTriangleVBox.addWidget(squareTriangleLabel)

        """Kite"""
        # Button
        kiteButton = QPushButton()
        kiteButton.clicked.connect(self.kiteClick)
        kiteButton.setIcon(QIcon('assets/kite.png'))
        kiteButton.setIconSize(QSize(100, 100))
        # Label
        kiteLabel = QLabel("Kite")
        kiteLabel.setAlignment(Qt.AlignCenter)
        # VBox
        kiteVBox = QVBoxLayout()
        kiteVBox.addWidget(kiteButton)
        kiteVBox.addWidget(kiteLabel)

        """Cube"""
        # Button
        cubeButton = QPushButton()
        cubeButton.clicked.connect(self.cubeClick)
        cubeButton.setIcon(QIcon('assets/cube.png'))
        cubeButton.setIconSize(QSize(100, 100))
        # Label
        cubeLabel = QLabel("Cube")
        cubeLabel.setAlignment(Qt.AlignCenter)
        # VBox
        cubeVBox = QVBoxLayout()
        cubeVBox.addWidget(cubeButton)
        cubeVBox.addWidget(cubeLabel)

        """Cilinder"""
        # Button
        cilinderButton = QPushButton()
        cilinderButton.clicked.connect(self.cilinderClick)
        cilinderButton.setIcon(QIcon('assets/cilinder.png'))
        cilinderButton.setIconSize(QSize(100, 100))
        # Label
        cilinderLabel = QLabel("Cilinder")
        cilinderLabel.setAlignment(Qt.AlignCenter)
        # VBox
        cilinderVBox = QVBoxLayout()
        cilinderVBox.addWidget(cilinderButton)
        cilinderVBox.addWidget(cilinderLabel)

        """Pyramid"""
        # Button
        pyramidButton = QPushButton()
        pyramidButton.clicked.connect(self.pyramidClick)
        pyramidButton.setIcon(QIcon('assets/pyramid.png'))
        pyramidButton.setIconSize(QSize(100, 100))
        # Label
        pyramidLabel = QLabel("Pyramid")
        pyramidLabel.setAlignment(Qt.AlignCenter)
        # VBox
        pyramidVBox = QVBoxLayout()
        pyramidVBox.addWidget(pyramidButton)
        pyramidVBox.addWidget(pyramidLabel)

        """Parellogram"""
        # Button
        parellogramButton = QPushButton()
        parellogramButton.clicked.connect(self.parellogramClick)
        parellogramButton.setIcon(QIcon('assets/parellogram.png'))
        parellogramButton.setIconSize(QSize(100, 100))
        # Label
        parellogramLabel = QLabel("Parellogram")
        parellogramLabel.setAlignment(Qt.AlignCenter)
        # VBox
        parellogramVBox = QVBoxLayout()
        parellogramVBox.addWidget(parellogramButton)
        parellogramVBox.addWidget(parellogramLabel)

        """Trapezium"""
        # Button
        trapeziumButton = QPushButton()
        trapeziumButton.clicked.connect(self.trapeziumClick)
        trapeziumButton.setIcon(QIcon('assets/trapezium.png'))
        trapeziumButton.setIconSize(QSize(100, 100))
        # Label
        trapeziumLabel = QLabel("Trapezium")
        trapeziumLabel.setAlignment(Qt.AlignCenter)
        # VBox
        trapeziumVBox = QVBoxLayout()
        trapeziumVBox.addWidget(trapeziumButton)
        trapeziumVBox.addWidget(trapeziumLabel)

        """Prisma Triangle"""
        # Button
        prismaTriangleButton = QPushButton()
        prismaTriangleButton.clicked.connect(self.prismaTriangleClick)
        prismaTriangleButton.setIcon(QIcon('assets/prismaTriangle.png'))
        prismaTriangleButton.setIconSize(QSize(100, 100))
        # Label
        prismaTriangleLabel = QLabel("Prisma Triangle")
        prismaTriangleLabel.setAlignment(Qt.AlignCenter)
        # VBox
        prismaTriangleVBox = QVBoxLayout()
        prismaTriangleVBox.addWidget(prismaTriangleButton)
        prismaTriangleVBox.addWidget(prismaTriangleLabel)

        """Cone"""
        # Button
        coneButton = QPushButton()
        coneButton.clicked.connect(self.coneClick)
        coneButton.setIcon(QIcon('assets/cone.png'))
        coneButton.setIconSize(QSize(100, 100))
        # Label
        coneLabel = QLabel("Cone")
        coneLabel.setAlignment(Qt.AlignCenter)
        # VBox
        coneVBox = QVBoxLayout()
        coneVBox.addWidget(coneButton)
        coneVBox.addWidget(coneLabel)

        """Sphere"""
        # Sphere
        sphereButton = QPushButton()
        sphereButton.clicked.connect(self.sphereClick)
        sphereButton.setIcon(QIcon('assets/sphere.png'))
        sphereButton.setIconSize(QSize(100, 100))
        # Label
        sphereLabel = QLabel("Sphere")
        sphereLabel.setAlignment(Qt.AlignCenter)
        # VBox
        sphereVBox = QVBoxLayout()
        sphereVBox.addWidget(sphereButton)
        sphereVBox.addWidget(sphereLabel)

        """Ellipse"""
        # Button
        ellipseButton = QPushButton()
        ellipseButton.clicked.connect(self.ellipseClick)
        ellipseButton.setIcon(QIcon('assets/ellipse.png'))
        ellipseButton.setIconSize(QSize(100, 100))
        # Label
        ellipseLabel = QLabel("Ellipse")
        ellipseLabel.setAlignment(Qt.AlignCenter)
        # VBox
        ellipseVBox = QVBoxLayout()
        ellipseVBox.addWidget(ellipseButton)
        ellipseVBox.addWidget(ellipseLabel)

        """Main layout containing all menus their buttons and labels inside their resprective VBoxLayout"""
        layout = QGridLayout()
        layout.setHorizontalSpacing(10)
        layout.setVerticalSpacing(15)

        layout.addLayout(squareVBox, 0, 0)
        layout.addLayout(circleVBox, 0, 1)
        layout.addLayout(triangleVBox, 0, 2)
        layout.addLayout(squareTriangleVBox, 0, 3)

        layout.addLayout(kiteVBox, 1, 0)
        layout.addLayout(cubeVBox, 1, 1)
        layout.addLayout(cilinderVBox, 1, 2)
        layout.addLayout(pyramidVBox, 1, 3)

        layout.addLayout(parellogramVBox, 2, 0)
        layout.addLayout(trapeziumVBox, 2, 1)
        layout.addLayout(prismaTriangleVBox, 2, 2)
        layout.addLayout(coneVBox, 2, 3)

        layout.addLayout(sphereVBox, 3, 1)
        layout.addLayout(ellipseVBox, 3, 2)

        return layout

    def changeScreenToShapeMenu(self, shapeMenu):
        deleteItemsOfLayout(self.layout)
        self.central = QWidget()
        self.setCentralWidget(self.central)
        self.shapeMenu = shapeMenu
        self.layout = self.shapeMenu.getUI(self)
        self.central.setLayout(self.layout)

    def squareClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=SquareMenu())

    def circleClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=CircleMenu())

    def triangleClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=TriangleMenu())

    def squareTriangleClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=SquareTriangleMenu())

    def kiteClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=KiteMenu())

    def cubeClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=CubeMenu())

    def cilinderClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=CilinderMenu())

    def pyramidClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=PyramidMenu())

    def parellogramClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=ParellogramMenu())

    def trapeziumClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=TrapeziumMenu())

    def prismaTriangleClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=PrismaTriangleMenu())

    def coneClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=ConeMenu())

    def sphereClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=SphereMenu())

    def ellipseClick(self, event):
        self.changeScreenToShapeMenu(shapeMenu=EllipseMenu())

def deleteItemsOfLayout(layout):
    if layout is not None:
        while layout.count():
            item = layout.takeAt(0)
            widget = item.widget()
            if widget is not None:
                widget.setParent(None)
            else:
                deleteItemsOfLayout(item.layout())

if __name__ == '__main__':
    os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "2"
    app = QApplication(sys.argv)
    ex = Menu()
    ex.show()
    sys.exit(app.exec_())


import tensorflow as tf
import tensorflow.contrib.layers as layers

x_data = [[73,80,75],[93,88,93],[89,91,90],[99,98,100],[73,66,70.0]]
y_data = [[152],[185],[180],[196],[142]]

X = tf.placeholder(tf.float32,shape=[None,3])
Y = tf.placeholder(tf.float32,shape=[None,1])

hypothesis = layers.fully_connected(X,1) # W, b 알아서 다해줌
# W = tf.Variable(tf.random_normal([3,1]))
# b = tf.Variable(tf.random_normal([1]))
# hypothesis = tf.matmul(X,W) + b
# hypothesis = tf.nn.reLu(hypothesis)

cost = tf.reduce_mean(tf.square(hypothesis-Y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.00001)
train = optimizer.minimize(cost)

#기능( = 모델 = 그래프 ) 구현 긑, 이제 사용하자
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(2001):
    cost_val,hy_val,_ = sess.run([cost,hypothesis,train],feed_dict={X:x_data,Y:y_data})
    if step % 20==0:
        print(cost_val,hy_val)


import lightbulb
import hikari
import datetime
import random

plugin = lightbulb.Plugin('flip', 'flip')

bot = lightbulb.BotApp

@plugin.listener(hikari.GuildMessageCreateEvent)
async def print_messages(event):
    pass

@plugin.command
@lightbulb.command('coinflip', 'Flips a coin')
@lightbulb.implements(lightbulb.PrefixCommand, lightbulb.SlashCommand)
async def poll(ctx):
    coin = random.randint(1,2)
    if coin == 1:
        result = "Heads"
    elif coin == 2:
        result = "Tails"
    await ctx.respond(
        hikari.Embed
        (
            title="Coin Flip",
            description=f"{result}"
        )
    )

def load(bot):
    bot.add_plugin(plugin)


from scrapy.exceptions import DropItem
from twisted.internet import defer, reactor

from basedata.models import (AccountingSubject, Concept, ConceptStock,
                             Industry, IndustryStock, Report, ReportItem,
                             ReportType, Stock, Territory, XReport,
                             XReportItem)
from tdxStock.abstract_models import DynamicModel

class StockPipeline(object):
    """下载股票到数据库"""

    def __init__(self):
        self.stock_map = {}
        for stock in Stock.objects.only('code', 'name').all():
            self.stock_map[stock.code] = stock.name

    def process_item(self, item, spider):
        if spider.name == "stock_list":
            if item['code'] in self.stock_map:
                if item['name'] == self.stock_map[item['code']]:
                    # 去重
                    raise DropItem("Duplicate item found: %s" % item)

                # else 要更新
                self.stock_map.update({item['code']: item['name']})

                def update_stock():
                    Stock.objects.filter(code=item['code']).update(name=item['name'], pinyin=item["pinyin"])

                reactor.callInThread(update_stock)
            else:
                # 新增的
                self.stock_map.update({item['code']: item['name']})

                def create_stock():
                    Stock.objects.create(**dict(item))

                reactor.callInThread(create_stock)

        elif spider.name == "stock_detail":
            def update_stock():
                del item['name']
                Stock.objects.filter(code=item['code']).update(**dict(item))

            reactor.callInThread(update_stock)

        else:
            return item

class IndustryPipeline(object):

    def process_item(self, item, spider):
        if spider.name in ("industry_zjh", "industry_sw"):
            # item is dict(name, code, industry_id)
            async def process_industry_stock():
                industry = Industry.objects.filter(pk=item['industry_id']).first()
                stock = Stock.objects.filter(code__endswith=item['code']).first()
                if stock and industry:
                    IndustryStock.objects.get_or_create(
                        stock=stock,
                        industry=industry
                    )

            defer.ensureDeferred(process_industry_stock())
        else:
            return item

class ConceptPipeline(object):

    def process_item(self, item, spider):
        if spider.name in ("concept",):
            # item is dict(name, code, concept_id)
            async def process_concept_stock():
                concept = Concept.objects.filter(pk=item['concept_id']).first()
                stock = Stock.objects.filter(code__endswith=item['code']).first()
                if stock and concept:
                    ConceptStock.objects.get_or_create(
                        stock=stock,
                        concept=concept
                    )

            defer.ensureDeferred(process_concept_stock())
        else:
            return item

class TerritoryPipeline(object):

    def process_item(self, item, spider):
        if spider.name == "territory":
            # item is dict(name, code, territory_id)
            async def process_territory_stock():
                territory = Territory.objects.filter(pk=item['territory_id']).first()
                if territory:
                    Stock.objects.filter(code__endswith=item['code']).update(
                        territory=territory
                    )

            defer.ensureDeferred(process_territory_stock())
        else:
            return item

class ReportPipeline(object):
    """处理Report的下载"""

    def __init__(self):
        self.report_types = {}
        self.subjects = {}

    def process_item(self, item, spider):
        if spider.name == "report":
            defer.ensureDeferred(self.download_report(item))
        else:
            return item

    async def get_report_type(self, slug):
        """优先从缓存获取 report_type"""
        report_type = self.report_types.get(slug)
        if report_type is None:
            report_type = ReportType.objects.get(slug=slug)
            self.report_types[slug] = report_type

        return report_type

    async def get_subject(self, report_type, slug):
        """优先从缓存获取 subject"""
        key = "type_%s__%s" % (report_type.id, slug)
        subject = self.subjects.get(key)
        if subject is None:
            subject, _ = AccountingSubject.objects.get_or_create(
                report_type=report_type,
                slug=slug,
                defaults={
                    "memo": "由 spider 创建"
                }
            )
            self.subjects.update({key: subject})

        return subject

    async def download_report(self, item):
        report_model = Report if item['is_single_quarter'] else XReport
        report_type = await self.get_report_type(item['report_type'])
        report, created = await self.get_or_create_report(
            report_model,
            stock_id=item['stock_id'],
            report_type=report_type,
            year=item['report_year'],
            quarter=item['report_quarter'],
            defaults={
                'report_date': item['report_date'],
                'name': item['report_name'],
            }
        )

        await self.download_report_items(report, created, item)

    async def download_report_items(self, report, created, item):
        if item['is_single_quarter']:
            report_item_model = DynamicModel(ReportItem, item['report_year'])
        else:
            report_item_model = DynamicModel(XReportItem, item['report_year'])

        async def has_items():
            return report_item_model.objects.filter(report=report).exists()

        if created or not await has_items():
            items_to_insert = list()
            for slug, value in item['report_data'].items():
                subject = await self.get_subject(report.report_type, slug)

                if isinstance(value, list):
                    value = value[0]

                if isinstance(value, int) or isinstance(value, float):
                    value_type = ReportItem.NUMBER_TYPE
                else:
                    value_type = ReportItem.STRING_TYPE

                items_to_insert.append(report_item_model(
                    report=report,
                    subject=subject,
                    value_number=value if value_type == ReportItem.NUMBER_TYPE else None,
                    value=value if value_type != ReportItem.NUMBER_TYPE else None,
                    value_type=value_type
                ))

            await self.bulk_create_items(report_item_model, items_to_insert)

        elif item['crawl_mode'] == 'all':
            for slug, value in item['report_data'].items():
                subject = await self.get_subject(report.report_type, slug)

                if isinstance(value, list):
                    value = value[0]

                if isinstance(value, int) or isinstance(value, float):
                    value_type = ReportItem.NUMBER_TYPE
                else:
                    value_type = ReportItem.STRING_TYPE

                await self.update_or_create_item(
                    report_item_model,
                    report=report,
                    subject=subject,
                    defaults={
                        "value_number": value if value_type == ReportItem.NUMBER_TYPE else None,
                        "value": value if value_type != ReportItem.NUMBER_TYPE else None,
                        "value_type": value_type,
                    },
                )

    async def get_or_create_report(self, report_model, *args, **kwargs):
        """检查是否已经下载, 去重"""
        return report_model.objects.get_or_create(*args, **kwargs)

    async def update_or_create_item(self, item_model, *args, **kwargs):
        """异步更新报表项"""
        return item_model.objects.update_or_create(*args, **kwargs)

    async def bulk_create_items(self, item_model, items_to_insert):
        """批量插入"""
        return item_model.objects.bulk_create(items_to_insert)


import random
import numpy
import os
import tensorflow as tf
from tensorflow.keras.layers.experimental import preprocessing

random.seed(2)
numpy.random.seed(2)
os.environ['PYTHONHASHSEED'] = str(2)

__imagenet_stats = {'mean': [0.485, 0.456, 0.406],
                    'variance': [0.229, 0.224, 0.225]}

__imagenet_pca = {
    'eigval': tf.Variable([0.2175, 0.0188, 0.0045]),
    'eigvec': tf.Variable([
        [-0.5675,  0.7192,  0.4009],
        [-0.5808, -0.0045, -0.8140],
        [-0.5836, -0.6948,  0.4203],
    ])
}

def scale_crop(input_size, scale_size=None, normalize=__imagenet_stats):
    processing = tf.keras.Sequential(
        [
            preprocessing.CenterCrop(input_size),
            preprocessing.Normalize(**normalize),
        ])
    if scale_size != input_size:
        processing.add(preprocessing.Rescaling(scale_size))

    return processing

def scale_random_crop(input_size, scale_size=None, normalize=__imagenet_stats):
    processing = tf.keras.Sequential(
        [
            preprocessing.RandomCrop(input_size[1], input_size[2]),
            preprocessing.Normalization(**normalize),
        ])
    if scale_size != input_size:
        processing.add(preprocessing.Rescaling(scale_size))

    return processing

def get_transform(input_size=None, scale_size=None, normalize=None, augment=True):
    normalize = normalize or __imagenet_stats

    input_size = input_size or 32
    #if augment:
    #    scale_size = scale_size or 40
    #    return pad_random_crop(input_size, scale_size=scale_size,
    #                           normalize=normalize)
    #else:
    scale_size = scale_size or 32
    return scale_crop(input_size=input_size,
                      scale_size=scale_size, normalize=normalize)

# class Lighting(object):
#     """Lighting noise(AlexNet - style PCA - based noise)"""
#
#     def __init__(self, alphastd, eigval, eigvec):
#         self.alphastd = alphastd
#         self.eigval = eigval
#         self.eigvec = eigvec
#
#     def __call__(self, img):
#         if self.alphastd == 0:
#             return img
#
#         alpha = img.new().resize_(3).normal_(0, self.alphastd)
#         rgb = self.eigvec.type_as(img).clone()\
#             .mul(alpha.view(1, 3).expand(3, 3))\
#             .mul(self.eigval.view(1, 3).expand(3, 3))\
#             .sum(1).squeeze()
#
#         return img.add(rgb.view(3, 1, 1).expand_as(img))
#
#
# class Grayscale(object):
#
#     def __call__(self, img):
#         gs = img.clone()
#         gs[0].mul_(0.299).add_(0.587, gs[1]).add_(0.114, gs[2])
#         gs[1].copy_(gs[0])
#         gs[2].copy_(gs[0])
#         return gs
#
#
# class Saturation(object):
#
#     def __init__(self, var):
#         self.var = var
#
#     def __call__(self, img):
#         gs = Grayscale()(img)
#         alpha = random.uniform(0, self.var)
#         return img.lerp(gs, alpha)
#
#
# class Brightness(object):
#
#     def __init__(self, var):
#         self.var = var
#
#     def __call__(self, img):
#         gs = img.new().resize_as_(img).zero_()
#         alpha = random.uniform(0, self.var)
#         return img.lerp(gs, alpha)
#
#
# class Contrast(object):
#
#     def __init__(self, var):
#         self.var = var
#
#     def __call__(self, img):
#         gs = Grayscale()(img)
#         gs.fill_(gs.mean())
#         alpha = random.uniform(0, self.var)
#         return img.lerp(gs, alpha)
#
#
# class RandomOrder(object):
#     """ Composes several transforms together in random order.
#     """
#
#     def __init__(self, transforms):
#         self.transforms = transforms
#
#     def __call__(self, img):
#         if self.transforms is None:
#             return img
#         order = torch.randperm(len(self.transforms))
#         for i in order:
#             img = self.transforms[i](img)
#         return img
#
#
# class ColorJitter(RandomOrder):
#
#     def __init__(self, brightness=0.4, contrast=0.4, saturation=0.4):
#         self.transforms = []
#         if brightness != 0:
#             self.transforms.append(Brightness(brightness))
#         if contrast != 0:
#             self.transforms.append(Contrast(contrast))
#         if saturation != 0:
#             self.transforms.append(Saturation(saturation))


from faker import Faker

class BuilderBase:

    def __init__(self):
        self.fake = Faker()
        self.result = {}

    def update_inner_value(self, keys, value):
        """
        The function allows you to change the generated objects at any level of
        nesting, and also makes it possible to create new ones
        """
        if not isinstance(keys, list):
            self.result[keys] = value
        else:
            temp = self.result
            for item in keys[:-1]:
                if item not in temp.keys():
                    temp[item] = {}
                temp = temp[item]
            temp[keys[-1]] = value
        return self

    def build(self):
        return self.result


import math
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout
from keras import regularizers, optimizers

def predict_close_lstm(df, cur_day, num_of_days, window_size=10, look_back=4, epochs=100, plot=False, plot_callback=None, loss_threshold=1e-4):
    actuals = []
    predictions = []

    start = cur_day
    end = cur_day + num_of_days
    # Go through all requested days and make a new training for every day
    for i in range(start, end):
        print("Training day {}".format(i - start))
        # For each day, train a new model using Close values of previous days.
        # Number of previous days is defined by "window_size"
        samples_start = i - window_size
        samples_end = i - 1

        # Important!
        # The training set will be taken from [samples_start:samples_end + 1]
        # and the last sample is for the prediction of next day
        df_cur_dataset = df[samples_start:samples_end + 2]

        actual, predicted = train_lstm(df_cur_dataset, look_back, epochs, plot=plot, plot_callback=plot_callback, loss_threshold=loss_threshold)
        actuals.append(actual)
        predictions.append(predicted)

    # Build a DataFrame that includes the actual and predicted results on the given data set
    df_results = df.iloc[cur_day:cur_day + num_of_days]
    cols_pred = pd.Series(predictions, index=range(cur_day, cur_day + num_of_days))
    df_results["Prediction"] = pd.Series(cols_pred)

    return actuals, predictions, df_results

def train_lstm(df, look_back, epochs, plot=False, plot_callback=None, loss_threshold=10**-4):
    # Set multi cores and print cores info
    # tf_session = tf.Session(config=tf.ConfigProto(device_count={"CPU": 8}, intra_op_parallelism_threads=2, inter_op_parallelism_threads=2))
    # keras.backend.tensorflow_backend.set_session(tf_session)
    # print(tf_session)

    # Normalize the Close dataset (this will include Xs and Ys)
    scaler = MinMaxScaler(feature_range=(0, 1))
    close_vals = np.reshape(df["Close"].values, (len(df), 1))
    dataset = scaler.fit_transform(close_vals.astype('float32'))

    # reshape into X=t and Y=t+1
    train_x, train_y, x_inc_predict, y_inc_predict = create_lstm_dataset(dataset, look_back)

    # reshape input to be [samples, time steps, features]
    train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))
    x_inc_predict = np.reshape(x_inc_predict, (x_inc_predict.shape[0], x_inc_predict.shape[1], 1))

    # Create the LSTM network
    batch_size = 1
    model = create_model(batch_size, look_back)

    # Fit the network - use multiple epochs
    for i in range(epochs):
        print("epoch {}".format(i + 1))
        res = model.fit(train_x, train_y, epochs=1, batch_size=batch_size, verbose=2, shuffle=False)
        model.reset_states()
        if res.history["loss"][0] < loss_threshold:
            print("Loss is lower than threshold of {}. Quitting.".format(loss_threshold))
            break

    # make predictions
    train_predict = model.predict(x_inc_predict, batch_size=batch_size)
    model.reset_states()

    # invert predictions
    train_y = scaler.inverse_transform([train_y])
    train_predict = scaler.inverse_transform(train_predict)

    # calculate root mean squared error
    trainScore = math.sqrt(mean_squared_error(train_y[0], train_predict[:-1, 0]))
    print('Train Score RMSE: %.2f' % (trainScore))

    # shift train predictions for plotting
    train_predict_plot = np.empty_like(dataset)
    train_predict_plot[:, :] = np.nan
    train_predict_plot[look_back:len(train_predict) + look_back, :] = train_predict

    if plot_callback:
        plot_callback(scaler.inverse_transform(dataset), train_predict_plot)

    if plot:
        # plot baseline and predictions
        plt.figure(figsize=(15, 10))
        plt.plot(scaler.inverse_transform(dataset))
        plt.plot(train_predict_plot)
        plt.show()

    actual = df.iloc[-1]["Close"]
    predicted = train_predict[-1, 0]

    return actual, predicted

def create_model(batch_size, look_back, regularization_factor=0, dropout=False, custom_optimizer=True):
    model = Sequential()
    model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True, return_sequences=True))
    if dropout:
        model.add(Dropout(0.5))
    model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True))
    if dropout:
        model.add(Dropout(0.5))
    model.add(Dense(1, kernel_regularizer=regularizers.l2(regularization_factor)))

    if custom_optimizer:
        learning_rate = 0.005
        optimzer = optimizers.RMSprop(lr=learning_rate)
    else:
        optimzer = 'adam'

    model.compile(loss='mean_squared_error', optimizer=optimzer)

    return model

def create_lstm_dataset(dataset, look_back=1):
    # Actual training set
    data_x, data_y = [], []

    # X and Y sets that include the last sample (which is not part of the training set)
    # This will be used for predicting and ploting the next day
    data_inc_predict_x, data_inc_predict_y = [], []

    def add_i_features(ds_x, ds_y, i):
        # Append to the list of timeseries, a 1 dimensional array that
        # includes values of our single feature (index 0) across current look_back range
        ds_x.append(dataset[i:(i + look_back), 0])

        # Append to the list of Ys, the y of the current look_back, which is our feature
        # of next day after our look_back range
        ds_y.append(dataset[i + look_back, 0])

    # Go through all training samples
    # For example
    # if the windows size is 10
    # 11 samples will be passed
    # samples 0-9 will be used for the training set
    # sample 10 will be used for the prediction
    for i in range(len(dataset) - look_back - 1):
        add_i_features(data_x, data_y, i)
        add_i_features(data_inc_predict_x, data_inc_predict_y, i)

    # Add the last sample for prediction purposes
    add_i_features(data_inc_predict_x, data_inc_predict_y, i + 1)

    return np.array(data_x), np.array(data_y), np.array(data_inc_predict_x), np.array(data_inc_predict_y)

# If we don't have a fixed seed, every time that I train the model, it predicts different results.
# Probably we should make enough epochs in order to get to a very small loss in order to get deterministic results
np.random.seed(7)

# Read data
df = pd.read_csv('~/code/dscience/stocks/data/facebook.csv', engine='python', skipfooter=3)
df.loc[:, 'Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')

##############################3

# # A callback to save plot data from within the training function in order to be able to plot the data in a different cell in Jupyter.
# ds1 = None
# ds2 = None
# def save_plot_data(original_dataset, predicted_shifted_dataset):
#     global ds1
#     global ds2
#     ds1 = original_dataset
#     ds2 = predicted_shifted_dataset
# numebr_of_days = 1
# first_day = int(len(df) / 2)
# res_actuals, res_predicted, df_results = predict_close_lstm(df, first_day, numebr_of_days, window_size=10, look_back=4, epochs=600, plot=False, plot_callback=save_plot_data, loss_threshold=1e-3)
# root_mse = math.sqrt(mean_squared_error(res_actuals, res_predicted))
# print('Test Score RMSE: %.2f' % (root_mse))
#
# # plot baseline and predictions
# plt.figure(figsize=(15, 10))
# plt.plot(ds1)
# plt.plot(ds2)
# plt.show()

##############################3

numebr_of_days = 100
first_day = len(df) - 100  # Same start date that we used for the polynomial regression
res_actuals, res_predicted, df_results = predict_close_lstm(df, first_day, numebr_of_days, window_size=10, look_back=4, epochs=600, plot=False,
                                                            loss_threshold=1e-3)
root_mse = math.sqrt(mean_squared_error(res_actuals, res_predicted))
print('TestScore RMSE: %.2f' % (root_mse))

plt.figure(figsize=(15,10))
plt.plot(df_results[["Date"]], df_results[["Close"]], 'b-')
plt.plot(df_results[["Date"]], df_results[["Prediction"]], 'g-')
#plt.title("Prediction of 8th day ({})".format(predicted_date.date()))
plt.xlabel('Date')
plt.ylabel('Price [$]')
plt.legend(["Actual Close", "Predicted Close"])
plt.show()

# ax = df_results.plot(x='Date', y='Close', style='b-', grid=True)
# ax = df_results.plot(x='Date', y='Prediction', style='g-', grid=True, ax=ax)
# ax.set_xlabel("Date")
# ax.set_ylabel("Price [$]")
# ax.legend(["Actual Close", "Predicted Close"])

##############################3


from sklearn.datasets import fetch_openml
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import zero_one_loss
from sklearn import datasets

# Problem data.

# Load MNIST dataset
mnist = fetch_openml('mnist_784')
X, y = mnist.data, mnist.target.astype(int)

# Reduce dataset size to 1/8
X, _, y, _ = train_test_split(X, y, test_size=0.875, random_state=42)

# Split the dataset into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Define SVM pipeline
svm_pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('svm', SVC(kernel='poly', degree=20, gamma='auto'))
])

# Train SVM model
#svm_pipeline.fit(X_train, y_train)

# Evaluate model performance
#train_score = svm_pipeline.score(X_train, y_train)
#test_score = svm_pipeline.score(X_test, y_test)

#print(f'Train score: {train_score:.3f}')
#print(f'Test score: {test_score:.3f}')

# Plot double descent curve
n_samples = len(X_train)
test_errors = []

for i in range(2, n_samples , 500):
    X_train_subset, y_train_subset = X_train[:i], y_train[:i]
    svm_pipeline.fit(X_train_subset, y_train_subset)
    y_pred = svm_pipeline.predict(X_test)

    print(i)

    test_error = zero_one_loss(y_test, y_pred)
    test_errors.append(test_error)

#plt.plot(np.arange(1, n_samples + 1, 500), train_scores, 'o-', label='train')
#plt.plot(np.arange(1, n_samples + 1, 500), test_scores, 'o-', label='test')
plt.plot(np.arange(2, n_samples , 500), test_errors, 'o-', label='test')
plt.legend()
plt.xlabel('Number of training samples')
plt.ylabel('Accuracy')
plt.show()


import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.hub import load_state_dict_from_url
import torchvision
from functools import partial
from collections import OrderedDict
import math

import os,inspect,sys
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
sys.path.insert(0,currentdir)
from Attention import ProjectorBlock3D, LinearAttentionBlock3D

"""
Implementation of 3D CNN.
"""
class CNN3D(nn.Module):
    def __init__(self, sample_size=128, sample_duration=16, drop_p=0.0, hidden1=512, hidden2=256, num_classes=100):
        super(CNN3D, self).__init__()
        self.sample_size = sample_size
        self.sample_duration = sample_duration
        self.num_classes = num_classes

        # network params
        self.ch1, self.ch2, self.ch3 = 32, 48, 48
        self.k1, self.k2, self.k3 = (3,7,7), (3,7,7), (3,5,5)
        self.s1, self.s2, self.s3 = (2,2,2), (2,2,2), (2,2,2)
        self.p1, self.p2, self.p3 = (0,0,0), (0,0,0), (0,0,0)
        self.d1, self.d2, self.d3 = (1,1,1), (1,1,1), (1,1,1)
        self.hidden1, self.hidden2 = hidden1, hidden2
        self.drop_p = drop_p
        self.pool_k, self.pool_s, self.pool_p, self.pool_d = (1,2,2), (1,2,2), (0,0,0), (1,1,1)
        # Conv1
        self.conv1_output_shape = self.compute_output_shape(self.sample_duration, self.sample_size,
            self.sample_size, self.k1, self.s1, self.p1, self.d1)
        # self.conv1_output_shape = self.compute_output_shape(self.conv1_output_shape[0], self.conv1_output_shape[1],
        #     self.conv1_output_shape[2], self.pool_k, self.pool_s, self.pool_p, self.pool_d)
        # Conv2
        self.conv2_output_shape = self.compute_output_shape(self.conv1_output_shape[0], self.conv1_output_shape[1],
            self.conv1_output_shape[2], self.k2, self.s2, self.p2, self.d2)
        # self.conv2_output_shape = self.compute_output_shape(self.conv2_output_shape[0], self.conv2_output_shape[1],
        #     self.conv2_output_shape[2], self.pool_k, self.pool_s, self.pool_p, self.pool_d)
        # Conv3
        self.conv3_output_shape = self.compute_output_shape(self.conv2_output_shape[0], self.conv2_output_shape[1],
            self.conv2_output_shape[2], self.k3, self.s3, self.p3, self.d3)
        # print(self.conv1_output_shape, self.conv2_output_shape, self.conv3_output_shape)

        # network architecture
        # in_channels=1 for grayscale, 3 for rgb
        self.conv1 = nn.Conv3d(in_channels=3, out_channels=self.ch1, kernel_size=self.k1,
            stride=self.s1, padding=self.p1, dilation=self.d1)
        self.bn1 = nn.BatchNorm3d(self.ch1)
        self.conv2 = nn.Conv3d(in_channels=self.ch1, out_channels=self.ch2, kernel_size=self.k2,
            stride=self.s2, padding=self.p2, dilation=self.d2)
        self.bn2 = nn.BatchNorm3d(self.ch2)
        self.conv3 = nn.Conv3d(in_channels=self.ch2, out_channels=self.ch3, kernel_size=self.k3,
            stride=self.s3, padding=self.p3, dilation=self.d3)
        self.bn3 = nn.BatchNorm3d(self.ch3)
        self.relu = nn.ReLU(inplace=True)
        self.drop = nn.Dropout3d(p=self.drop_p)
        self.pool = nn.MaxPool3d(kernel_size=self.pool_k)
        self.fc1 = nn.Linear(self.ch3 * self.conv3_output_shape[0] * self.conv3_output_shape[1] * self.conv3_output_shape[2], self.hidden1)
        self.fc2 = nn.Linear(self.hidden1, self.hidden2)
        self.fc3 = nn.Linear(self.hidden2, self.num_classes)

    def forward(self, x):
        # Conv1
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        # x = self.pool(x)
        # x = self.drop(x)
        # Conv2
        x = self.conv2(x)
        x = self.bn2(x)
        x = self.relu(x)
        # x = self.pool(x)
        # x = self.drop(x)
        # Conv3
        x = self.conv3(x)
        x = self.bn3(x)
        x = self.relu(x)
        # x = self.drop(x)
        # MLP
        # print(x.shape)
        # x.size(0) ------ batch_size
        x = x.view(x.size(0), -1)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.dropout(x, p=self.drop_p, training=self.training)
        x = self.fc3(x)

        return x

    def compute_output_shape(self, D_in, H_in, W_in, k, s, p, d):
        # Conv
        D_out = np.floor((D_in + 2*p[0] - d[0]*(k[0] - 1) - 1)/s[0] + 1).astype(int)
        H_out = np.floor((H_in + 2*p[1] - d[1]*(k[1] - 1) - 1)/s[1] + 1).astype(int)
        W_out = np.floor((W_in + 2*p[2] - d[2]*(k[2] - 1) - 1)/s[2] + 1).astype(int)

        return D_out, H_out, W_out

"""
Implementation of 3D Resnet
Reference: Can Spatiotemporal 3D CNNs Retrace the History of 2D CNNs and ImageNet?
"""
class BasicBlock(nn.Module):
    expansion = 1
    # planes refer to the number of feature maps
    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(BasicBlock, self).__init__()
        self.stride = stride
        self.downsample = downsample
        self.conv1 = nn.Conv3d(
            inplanes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn1 = nn.BatchNorm3d(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = nn.Conv3d(
            planes, planes, kernel_size=3, padding=1, bias=False)
        self.bn2 = nn.BatchNorm3d(planes)

    def forward(self, x):
        residual = x
        # conv1
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        # conv2
        out = self.conv2(out)
        out = self.bn2(out)
        # downsample
        if self.downsample is not None:
            residual = self.downsample(x)

        # print(out.shape, residual.shape)
        out += residual
        out = self.relu(out)

        return out

class Bottleneck(nn.Module):
    expansion = 4
    # planes refer to the number of feature maps
    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(Bottleneck, self).__init__()
        self.stride = stride
        self.downsample = downsample
        self.conv1 = nn.Conv3d(
            inplanes, planes, kernel_size=1, bias=False) # kernal_size=1 don't need padding
        self.bn1 = nn.BatchNorm3d(planes)
        self.conv2 = nn.Conv3d(
            planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm3d(planes)
        self.conv3 = nn.Conv3d(planes, planes * 4, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm3d(planes * 4)
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        residual = x
        # conv1
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        # conv2
        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)
        # conv3
        out = self.conv3(out)
        out = self.bn3(out)
        # downsample
        if self.downsample is not None:
            residual = self.downsample(x)

        # print(out.shape, residual.shape)
        out += residual
        out = self.relu(out)

        return out

def downsample_basic_block(x, planes, stride):
    # decrease data resolution if stride not equals to 1
    out = F.avg_pool3d(x, kernel_size=1, stride=stride)
    # shape: (batch_size, channel, t, h, w)
    # try to match the channel size
    zero_pads = torch.Tensor(
        out.size(0), planes - out.size(1), out.size(2), out.size(3),
        out.size(4)).zero_()
    if isinstance(out.data, torch.cuda.FloatTensor):
        zero_pads = zero_pads.cuda()

    out = Variable(torch.cat([out.data, zero_pads], dim=1))

    return out

class ResNet(nn.Module):
    def __init__(self, block, layers, shortcut_type, sample_size, sample_duration, attention=False, num_classes=500):
        super(ResNet, self).__init__()
        # initialize inplanes to 64, it'll be changed later
        self.inplanes = 64
        self.conv1 = nn.Conv3d(
            3, 64, kernel_size=7, stride=(1, 2, 2), padding=(3, 3, 3), bias=False)
        self.bn1 = nn.BatchNorm3d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool3d(kernel_size=(3, 3, 3), stride=2, padding=1)
        # layers refers to the number of blocks in each layer
        self.layer1 = self._make_layer(
            block, 64, layers[0], shortcut_type, stride=1)
        self.layer2 = self._make_layer(
            block, 128, layers[1], shortcut_type, stride=2)
        self.layer3 = self._make_layer(
            block, 256, layers[2], shortcut_type, stride=2)
        self.layer4 = self._make_layer(
            block, 512, layers[3], shortcut_type, stride=2)
        # calclatue kernal size for average pooling
        last_duration = int(math.ceil(sample_duration / 16))
        last_size = int(math.ceil(sample_size / 32))
        self.avgpool = nn.AvgPool3d(
            (last_duration, last_size, last_size), stride=1)
        # attention blocks
        self.attention = attention
        if self.attention:
            self.attn1 = LinearAttentionBlock3D(in_channels=512*block.expansion, normalize_attn=True)
            self.attn2 = LinearAttentionBlock3D(in_channels=512*block.expansion, normalize_attn=True)
            self.attn3 = LinearAttentionBlock3D(in_channels=512*block.expansion, normalize_attn=True)
            self.attn4 = LinearAttentionBlock3D(in_channels=512*block.expansion, normalize_attn=True)
            self.projector1 = ProjectorBlock3D(in_channels=64*block.expansion, out_channels=512*block.expansion)
            self.projector2 = ProjectorBlock3D(in_channels=128*block.expansion, out_channels=512*block.expansion)
            self.projector3 = ProjectorBlock3D(in_channels=256*block.expansion, out_channels=512*block.expansion)
            self.fc = nn.Linear(512 * block.expansion * 4, num_classes)
        else:
            self.fc = nn.Linear(512 * block.expansion, num_classes)
        # init the weights
        for m in self.modules():
            if isinstance(m, nn.Conv3d):
                m.weight = nn.init.kaiming_normal_(m.weight, mode='fan_out')
            elif isinstance(m, nn.BatchNorm3d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def _make_layer(self, block, planes, blocks, shortcut_type, stride):
        downsample = None
        # when the in-channel and the out-channel dismatch, downsample!!!
        if stride != 1 or self.inplanes != planes * block.expansion:
            # stride once for downsample and block.
            if shortcut_type == 'A':
                downsample = partial(
                    downsample_basic_block,
                    planes=planes * block.expansion,
                    stride=stride)
            else:
                downsample = nn.Sequential(
                    nn.Conv3d(
                        self.inplanes,
                        planes * block.expansion,
                        kernel_size=1,
                        stride=stride,
                        bias=False), nn.BatchNorm3d(planes * block.expansion))

        layers = []
        # only the first block needs downsample.
        layers.append(block(self.inplanes, planes, stride, downsample))
        # change inplanes for the next layer
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        l1 = self.layer1(x)
        l2 = self.layer2(l1)
        l3 = self.layer3(l2)
        l4 = self.layer4(l3)

        g = self.avgpool(l4)
        # attention
        if self.attention:
            # print(l1.shape, l2.shape, l3.shape, l4.shape, g.shape)
            c1, g1 = self.attn1(self.projector1(l1), g)
            c2, g2 = self.attn2(self.projector2(l2), g)
            c3, g3 = self.attn3(self.projector3(l3), g)
            c4, g4 = self.attn4(l4, g)
            g = torch.cat((g1,g2,g3,g4), dim=1)
            x = self.fc(g)
        else:
            c1, c2, c3, c4 = None, None, None, None
            # x.size(0) ------ batch_size
            g = g.view(g.size(0), -1)
            x = self.fc(g)

        return [x, c1, c2, c3, c4]

    def load_my_state_dict(self, state_dict):
        my_state_dict = self.state_dict()
        for name, param in state_dict.items():
            if name == 'fc.weight' or name == 'fc.bias':
                continue
            my_state_dict[name].copy_(param.data)

model_urls = {
    'resnet18': 'https://www.jianguoyun.com/c/dl-file/resnet-18-kinetics.pth?dt=q67aev&kv=YXF6QHpqdS5lZHUuY24&sd=a54cr&ud=B8Sbfz0nRv1pG8YNAbo0KiCnzvJHDsLYQsWjtT4b1j8&vr=1',
    'resnet34': 'https://www.jianguoyun.com/c/dl-file/resnet-34-kinetics.pth?dt=q67acv&kv=YXF6QHpqdS5lZHUuY24&sd=a54cr&ud=BftTcvolMjyywptfxelwwjXJksCaU0ektvfMwCbMD1I&vr=1',
    'resnet50': 'https://www.jianguoyun.com/c/dl-file/resnet-50-kinetics.pth?dt=q67atr&kv=YXF6QHpqdS5lZHUuY24&sd=a54cr&ud=uKpTbIK63qX3bHs2weOGqYYc2gtssQi-o7UqpoTaG6Q&vr=1',
    'resnet101': '',
    'resnet152': '',
    'resnet200': '',
}

def resnet18(pretrained=False, progress=True, **kwargs):
    """Constructs a ResNet-18 model.
    """
    model = ResNet(BasicBlock, [2, 2, 2, 2], shortcut_type='A', **kwargs)
    if pretrained:
        checkpoint = load_state_dict_from_url(model_urls['resnet18'],
            progress=progress)
        state_dict = checkpoint['state_dict']

        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            name = k[7:] # remove 'module.'
            new_state_dict[name]=v
        model.load_my_state_dict(new_state_dict)

    return model

def resnet34(pretrained=False, progress=True, **kwargs):
    """Constructs a ResNet-34 model.
    """
    model = ResNet(BasicBlock, [3, 4, 6, 3], shortcut_type='A', **kwargs)
    if pretrained:
        checkpoint = load_state_dict_from_url(model_urls['resnet34'],
            progress=progress)
        state_dict = checkpoint['state_dict']

        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            name = k[7:] # remove 'module.'
            new_state_dict[name]=v
        model.load_my_state_dict(new_state_dict)

    return model

def resnet50(pretrained=False, progress=True, **kwargs):
    """Constructs a ResNet-50 model.
    """
    model = ResNet(Bottleneck, [3, 4, 6, 3], shortcut_type='B', **kwargs)
    if pretrained:
        checkpoint = load_state_dict_from_url(model_urls['resnet50'],
            progress=progress)
        state_dict = checkpoint['state_dict']

        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            name = k[7:] # remove 'module.'
            new_state_dict[name]=v
        model.load_my_state_dict(new_state_dict)

    return model

def resnet101(pretrained=False, progress=True, **kwargs):
    """Constructs a ResNet-101 model.
    """
    model = ResNet(Bottleneck, [3, 4, 23, 3], shortcut_type='B', **kwargs)
    if pretrained:
        checkpoint = load_state_dict_from_url(model_urls['resnet101'],
            progress=progress)
        state_dict = checkpoint['state_dict']

        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            name = k[7:] # remove 'module.'
            new_state_dict[name]=v
        model.load_my_state_dict(new_state_dict)

    return model

def resnet152(pretrained=False, progress=True, **kwargs):
    """Constructs a ResNet-101 model.
    """
    model = ResNet(Bottleneck, [3, 8, 36, 3], shortcut_type='B', **kwargs)
    if pretrained:
        checkpoint = load_state_dict_from_url(model_urls['resnet152'],
            progress=progress)
        state_dict = checkpoint['state_dict']

        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            name = k[7:] # remove 'module.'
            new_state_dict[name]=v
        model.load_my_state_dict(new_state_dict)

    return model

def resnet200(pretrained=False, progress=True, **kwargs):
    """Constructs a ResNet-101 model.
    """
    model = ResNet(Bottleneck, [3, 24, 36, 3], shortcut_type='B', **kwargs)
    if pretrained:
        checkpoint = load_state_dict_from_url(model_urls['resnet200'],
            progress=progress)
        state_dict = checkpoint['state_dict']

        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            name = k[7:] # remove 'module.'
            new_state_dict[name]=v
        model.load_my_state_dict(new_state_dict)

    return model

"""
3D CNN Models from torchvision.models
Reference: https://pytorch.org/docs/stable/torchvision/models.html#video-classification
"""
class r3d_18(nn.Module):
    def __init__(self, pretrained=True, num_classes=500):
        super(r3d_18, self).__init__()
        self.pretrained = pretrained
        self.num_classes = num_classes
        model = torchvision.models.video.r3d_18(pretrained=self.pretrained)
        # delete the last fc layer
        modules = list(model.children())[:-1]
        # print(modules)
        self.r3d_18 = nn.Sequential(*modules)
        self.fc1 = nn.Linear(model.fc.in_features, self.num_classes)

    def forward(self, x):
        out = self.r3d_18(x)
        # print(out.shape)
        # Flatten the layer to fc
        out = out.flatten(1)
        out = self.fc1(out)

        return out

class mc3_18(nn.Module):
    def __init__(self, pretrained=True, num_classes=500):
        super(mc3_18, self).__init__()
        self.pretrained = pretrained
        self.num_classes = num_classes
        model = torchvision.models.video.mc3_18(pretrained=self.pretrained)
        # delete the last fc layer
        modules = list(model.children())[:-1]
        # print(modules)
        self.mc3_18 = nn.Sequential(*modules)
        self.fc1 = nn.Linear(model.fc.in_features, self.num_classes)

    def forward(self, x):
        out = self.mc3_18(x)
        # print(out.shape)
        # Flatten the layer to fc
        out = out.flatten(1)
        out = self.fc1(out)

        return out

class r2plus1d_18(nn.Module):
    def __init__(self, pretrained=True, num_classes=500):
        super(r2plus1d_18, self).__init__()
        self.pretrained = pretrained
        self.num_classes = num_classes
        model = torchvision.models.video.r2plus1d_18(pretrained=self.pretrained)
        # delete the last fc layer
        modules = list(model.children())[:-1]
        # print(modules)
        self.r2plus1d_18 = nn.Sequential(*modules)
        self.fc1 = nn.Linear(model.fc.in_features, self.num_classes)

    def forward(self, x):
        out = self.r2plus1d_18(x)
        # print(out.shape)
        # Flatten the layer to fc
        out = out.flatten(1)
        out = self.fc1(out)

        return out

# Test
if __name__ == '__main__':
    import sys
    sys.path.append("..")
    import torchvision.transforms as transforms
    from dataset import CSL_Isolated
    sample_size = 128
    sample_duration = 16
    num_classes = 500
    transform = transforms.Compose([transforms.Resize([sample_size, sample_size]), transforms.ToTensor()])
    dataset = CSL_Isolated(data_path="/home/haodong/Data/CSL_Isolated/color_video_125000",
        label_path="/home/haodong/Data/CSL_Isolated/dictionary.txt", frames=sample_duration,
        num_classes=num_classes, transform=transform)
    # cnn3d = CNN3D(sample_size=sample_size, sample_duration=sample_duration, num_classes=num_classes)
    cnn3d = resnet50(pretrained=True, progress=True, sample_size=sample_size, sample_duration=sample_duration, attention=True, num_classes=num_classes)
    # cnn3d = r3d_18(pretrained=True, num_classes=num_classes)
    # cnn3d = mc3_18(pretrained=True, num_classes=num_classes)
    # cnn3d = r2plus1d_18(pretrained=True, num_classes=num_classes)
    # print(dataset[0]['images'].shape)
    print(cnn3d(dataset[0]['data'].unsqueeze(0)))

    # Test for loading pretrained models
    # state_dict = torch.load('resnet-18-kinetics.pth')
    # for name, param in state_dict.items():
    #     print(name)
    # # print(state_dict['arch'])
    # # print(state_dict['optimizer'])
    # # print(state_dict['epoch'])


from typing import List, Optional, Type

from aioarangodb import CursorCloseError
from aioarangodb.cursor import Cursor

from arangodantic.exceptions import CursorError, CursorNotFoundError

class ArangodanticCursor:
    """
    Wrapper for the aioarangodb.cursor.Cursor that will give back instances of the
    defined class rather than dictionaries.
    """

    __slots__ = [
        "cls",
        "cursor",
    ]

    def __init__(self, cls, cursor: Cursor):
        from arangodantic.models import Model

        self.cls: Type[Model] = cls
        self.cursor: Cursor = cursor

    def __aiter__(self):
        return self

    async def __anext__(self):  # pragma: no cover
        return await self.next()

    async def __aenter__(self):
        return self

    def __len__(self):
        return len(self.cursor)

    async def __aexit__(self, *_):
        await self.close(ignore_missing=True)

    def __repr__(self):
        cursor_id_str = ""
        if self.cursor.id:
            cursor_id_str = f" (Cursor: {self.cursor.id})"
        return f"<ArangodanticCursor ({self.cls.__name__}){cursor_id_str}>"

    async def close(self, ignore_missing: bool = False) -> Optional[bool]:
        """
        Close the cursor to free server side resources.

        :param ignore_missing: Do not raise an exception if the cursor is missing on the
        server side.
        :return: True if cursor was closed successfully, False if cursor was missing on
        the server side and **ignore_missing** was True, None if there were no cursors
        to close server-side.
        :raise CursorNotFoundError: If the cursor was missing and **ignore_missing** was
        False.
        """
        try:
            result: Optional[bool] = await self.cursor.close(
                ignore_missing=ignore_missing
            )
        except CursorCloseError as ex:
            if ex.error_code == 404:
                raise CursorNotFoundError(ex.error_message)
            raise

        return result

    async def next(self):
        return self.cls(**(await self.cursor.next()))

    async def to_list(self) -> List:
        """
        Convert the cursor to a list.
        """
        async with self as cursor:
            return [i async for i in cursor]

    @property
    def full_count(self) -> int:
        """
        Get the full count.

        :return: The full count.
        :raise CursorError: If the cursor statistics do not contain the full count.
        """
        stats = self.cursor.statistics()
        try:
            full_count: int = stats["fullCount"]
        except KeyError as e:
            raise CursorError(
                "Cursor statistics has no full count, did you use full_count=True?"
            ) from e

        return full_count


from typing import Union, Optional, List

def task(array: Union[str, List[float], List[str]], level: int = None,
         first_group_num: Union[float, str] = None) -> Optional[int]:
    """Функция, принимает последовательность
    состоящей из двух групп единиц и нулей(в общем случае любых знаков)
    возвращает индекс последнего элемента группы

    Сложность: O(N), где N - длина последовательности array

    >>> dummy_data = "111111111110000000000000000"
    >>> task(dummy_data)
    11
    >>> task(dummy_data[::-1])
    16
    >>> task('')
    Пустой массив
    >>> task([i for i in dummy_data]) # список строк
    11
    >>> task([int(i) for i in dummy_data]) # список целых чисел
    11
    >>> task("1111111111")
    Всего одна группа знаков!

    """
    if len(array) == 1:
        print('Всего одна группа знаков!')
        return None
    if len(array) == 0:
        print('Пустой массив')
        return None
    first_group_num = first_group_num or array[0]
    level = level or 0
    if first_group_num != array[0]:
        return level
    return task(array[1:], level=level + 1, first_group_num=first_group_num)

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


import time

import matplotlib.pyplot as plt
import numpy as np
import open3d as o3d

pcd = o3d.io.read_point_cloud(r"D:\PycharmProject\论文需要\RANSAC\outer\outer2.pcd")
start = time.time()
point = np.asarray(pcd.points)  # 获取点坐标
kdtree = o3d.geometry.KDTreeFlann(pcd)  # 建立KD树索引
point_size = point.shape[0]  # 获取点的个数
dd = np.zeros(point_size)
for i in range(point_size):
    [_, idx, dis] = kdtree.search_knn_vector_3d(point[i], 2)
    dd[i] = dis[1]  # 获取到最近邻点的距离平方
density = np.mean(np.sqrt(dd))
pcd = o3d.io.read_point_cloud(r"D:\PycharmProject\论文需要\RANSAC\outer\outer2.pcd")
# 设置为debug调试模式
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
    # -------------------密度聚类--------------------------
    labels = np.array(pcd.cluster_dbscan(eps=0.01,  # 邻域距离
                                         min_points=50,
                                           # 最小点数
                                         print_progress=True))  # 是否在控制台中可视化进度条
max_label = labels.max()
print(f"point cloud has {max_label + 1} clusters")
# ---------------------保存聚类结果------------------------
for i in range(max_label + 1):
    ind = np.where(labels == i)[0]
    clusters_cloud = pcd.select_by_index(ind)
    file_name = "Dbscan_cluster" + str(i + 1) + ".pcd"
    # print(clusters_cloud)
    pointnum= int(str(clusters_cloud).strip('PointCloud with ').strip(' points.'))
    if pointnum > 10000:
        print(pointnum)
        o3d.io.write_point_cloud(file_name, clusters_cloud)
        o3d.visualization.draw_geometries([clusters_cloud], window_name="DBSCAN处理",
                                          width=1024, height=768,
                                          left=50, top=50,
                                          mesh_show_back_face=False)

end =time.time()
print(end-start)
# # --------------------可视化聚类结果----------------------
colors = plt.get_cmap("tab20")(labels / (max_label if max_label > 0 else 1))
colors[labels < 0] = 0
pcd.colors = o3d.utility.Vector3dVector(colors[:, :3])
o3d.visualization.draw_geometries([pcd], window_name="点云密度聚类",
                                  height=480, width=600,
                                  mesh_show_back_face=0)


def check_id() :
# 주민등록번호 입력받기
    while True:
        q1 = input("주민등록번호 앞자리를 넣어주세요: ")
        if (len(q1) != 6):
            print("다시 확인하시고 넣어주세요(앞 6숫자)\n")
            continue
        q2 = input("주민등록번호 뒷자리를 넣어주세요: ")
        if (len(q2) != 7):
            print("다시 확인하시고 넣어주세요(뒷 7숫자)\n")
            continue
        else:
            q = q1 + '-' + q2
            print('주민등록번호', q)
            break
# 년,월 + 성별구분 슬라이스
    b_year = list(q1[:2])
    b_month = list(q1[2:4])
    gender = list(q2[0])
# 성별 조건문
    for n in gender :
        if n == '1' or n == '3':
             s = '남자'
        elif n == '2' or n =='4':
             s = '여자'
        else:
             print('성별을 알 수 없습니다.')
# 00년 이후 출생자 체크
    if (b_year[0] == '0') or (b_year[0] == '1') or (b_year[0] == '2'):
        while True:
            y = input('2000년 이후 출생자 입니까? 맞으면 o 아니면 x: ')
            if y == 'o' and (gender == '1' or gender == '2'):
                print('잘못된 번호입니다.')
                print('입력된 주민등록번호를 재확인 해주세요.')
                print('2000년 이후 출생자의 뒷자리 첫숫자는 3 혹은 4 입니다.')
                break
            elif y == 'o' and (gender == '3' or gender == '4') :
                print(f'{b_year}년{b_month}월 {s}')
                break
            elif y == 'x' :
                print(f'{b_year}년{b_month}월 {s}')
                print('입력된 출생년도가 2000년 이후 입니다.')
                break
            else:
                print('잘못 입력하셨습니다.\n')
                continue
    else :
        if gender == '3' or gender == '4':
            print('잘못된 번호입니다.')
            print('입력된 주민등록번호를 재확인 해주세요.')
            print('2000년 이전 출생자의 뒷자리 첫숫자는 1 혹은 2 입니다.')
        else :
           print(f'{b_year}년{b_month}월 {s}')
# 결과 출력
check_id()


import pygame
from components.pieces_cls import *

tile_size = 120
round_int = 0

def main():
    pygame.init()

    screen_size = (8*tile_size, 8*tile_size)

    go = True
    white_is_checked = False
    black_is_checked = False

    screen = pygame.display.set_mode(screen_size)
    pygame.display.set_caption("Chess")

    clock = pygame.time.Clock()

    brown = pygame.Color(179,140,94)

    #creating the board
    tile_dir_list = []
    for y in range(8):
        tile_dir_list.append([])
        for x in range(8):
            tile_dir_list[y].append((x*tile_size, y*tile_size))
            if y % 2 == 0:
                if x % 2 == 0:
                    pygame.draw.rect(screen, (245, 216, 188), [x*tile_size, y*tile_size, tile_size, tile_size])
                else:
                    pygame.draw.rect(screen, (176, 142, 109), [x*tile_size, y*tile_size, tile_size, tile_size])
            else:
                if x % 2 == 0:
                    pygame.draw.rect(screen, (176, 142, 109), [x*tile_size, y*tile_size, tile_size, tile_size])
                else:
                    pygame.draw.rect(screen, (245, 216, 188), [x*tile_size, y*tile_size, tile_size, tile_size])

    #summoning the Pieces

    white_pawn_img = pygame.image.load(r'assets/white_pawn.png')
    white_rook_img = pygame.image.load(r'assets/white_rook.png')
    white_knight_img = pygame.image.load(r'assets/white_knight.png')
    white_bishop_img = pygame.image.load(r'assets/white_bishop.png')
    white_queen_img = pygame.image.load(r'assets/white_queen.png')
    white_king_img = pygame.image.load(r'assets/white_king.png')

    black_pawn_img = pygame.image.load(r'assets/black_pawn.png')
    black_rook_img = pygame.image.load(r'assets/black_rook.png')
    black_knight_img = pygame.image.load(r'assets/black_knight.png')
    black_bishop_img = pygame.image.load(r'assets/black_bishop.png')
    black_queen_img = pygame.image.load(r'assets/black_queen.png')
    black_king_img = pygame.image.load(r'assets/black_king.png')

    white_pawn_img = pygame.transform.scale(white_pawn_img, (tile_size, tile_size))
    white_rook_img = pygame.transform.scale(white_rook_img, (tile_size, tile_size))
    white_knight_img = pygame.transform.scale(white_knight_img, (tile_size, tile_size))
    white_bishop_img = pygame.transform.scale(white_bishop_img, (tile_size, tile_size))
    white_queen_img = pygame.transform.scale(white_queen_img, (tile_size, tile_size))
    white_king_img = pygame.transform.scale(white_king_img, (tile_size, tile_size))

    black_pawn_img = pygame.transform.scale(black_pawn_img, (tile_size, tile_size))
    black_rook_img = pygame.transform.scale(black_rook_img, (tile_size, tile_size))
    black_knight_img = pygame.transform.scale(black_knight_img, (tile_size, tile_size))
    black_bishop_img = pygame.transform.scale(black_bishop_img, (tile_size, tile_size))
    black_queen_img = pygame.transform.scale(black_queen_img, (tile_size, tile_size))
    black_king_img = pygame.transform.scale(black_king_img, (tile_size, tile_size))

    # Keine magicnumbers (num_7 = 7)
    A_Pawn_white =      WhitePawns(master = screen, name = 'A-Pawn-W', tile_x = 0, tile_y = 6, farbe = 'weiss', image = white_pawn_img)
    B_Pawn_white =      WhitePawns(master = screen, name = 'B-Pawn-W', tile_x = 1, tile_y = 6, farbe = 'weiss', image = white_pawn_img)
    C_Pawn_white =      WhitePawns(master = screen, name = 'C-Pawn-W', tile_x = 2, tile_y = 6, farbe = 'weiss', image = white_pawn_img)
    D_Pawn_white =      WhitePawns(master = screen, name = 'D-Pawn-W', tile_x = 3, tile_y = 6, farbe = 'weiss', image = white_pawn_img)
    E_Pawn_white =      WhitePawns(master = screen, name = 'E-Pawn-W', tile_x = 4, tile_y = 6, farbe = 'weiss', image = white_pawn_img)
    F_Pawn_white =      WhitePawns(master = screen, name = 'F-Pawn-W', tile_x = 5, tile_y = 6, farbe = 'weiss', image = white_pawn_img)
    G_Pawn_white =      WhitePawns(master = screen, name = 'G-Pawn-W', tile_x = 6, tile_y = 6, farbe = 'weiss', image = white_pawn_img)
    H_Pawn_white =      WhitePawns(master = screen, name = 'H-Pawn-W', tile_x = 7, tile_y = 6, farbe = 'weiss', image = white_pawn_img)
    L_Rook_white =      Rooks(master = screen, name = 'L-Rook-W', tile_x = 0, tile_y = 7, farbe = 'weiss', image = white_rook_img)
    R_Rook_white =      Rooks(master = screen, name = 'R-Rook-W', tile_x = 7, tile_y = 7, farbe = 'weiss', image = white_rook_img)
    L_Knight_white =    Knights(master = screen, name = 'L-Knight-W', tile_x = 6, tile_y = 7, farbe = 'weiss', image = white_knight_img)
    R_Knight_white =    Knights(master = screen, name = 'R-Knight-W', tile_x = 1, tile_y = 7, farbe = 'weiss', image = white_knight_img)
    L_Bishop_white =   Bishops(master = screen, name = 'L-Bishop-W', tile_x = 2, tile_y = 7, farbe = 'weiss', image = white_bishop_img)
    R_Bishop_white =   Bishops(master = screen, name = 'R-Bishop-W', tile_x = 5, tile_y = 7, farbe = 'weiss', image = white_bishop_img)
    Queen_white =       Queens(master = screen, name = 'Queen-W', tile_x = 3, tile_y = 7, farbe = 'weiss', image = white_queen_img)
    King_white =        Kings(master = screen, name = 'King-W', tile_x = 4, tile_y = 7, farbe = 'weiss', image = white_king_img)

    A_Pawn_black =      BlackPawns(master = screen, name = 'A-Pawn-B', tile_x = 0, tile_y = 1, farbe = 'schwarz', image = black_pawn_img)
    B_Pawn_black =      BlackPawns(master = screen, name = 'B-Pawn-B', tile_x = 1, tile_y = 1, farbe = 'schwarz', image = black_pawn_img)
    C_Pawn_black =      BlackPawns(master = screen, name = 'C-Pawn-B', tile_x = 2, tile_y = 1, farbe = 'schwarz', image = black_pawn_img)
    D_Pawn_black =      BlackPawns(master = screen, name = 'D-Pawn-B', tile_x = 3, tile_y = 1, farbe = 'schwarz', image = black_pawn_img)
    E_Pawn_black =      BlackPawns(master = screen, name = 'E-Pawn-B', tile_x = 4, tile_y = 1, farbe = 'schwarz', image = black_pawn_img)
    F_Pawn_black =      BlackPawns(master = screen, name = 'F-Pawn-B', tile_x = 5, tile_y = 1, farbe = 'schwarz', image = black_pawn_img)
    G_Pawn_black =      BlackPawns(master = screen, name = 'G-Pawn-B', tile_x = 6, tile_y = 1, farbe = 'schwarz', image = black_pawn_img)
    H_Pawn_black =      BlackPawns(master = screen, name = 'H-Pawn-B', tile_x = 7, tile_y = 1, farbe = 'schwarz', image = black_pawn_img)
    L_Rook_black =      Rooks(master = screen, name = 'L-Rook-B', tile_x = 0, tile_y = 0, farbe = 'schwarz', image = black_rook_img)
    R_Rook_black =      Rooks(master = screen, name = 'R-Rook-B', tile_x = 7, tile_y = 0, farbe = 'schwarz', image = black_rook_img)
    L_Knight_black =    Knights(master = screen, name = 'L-Knight-B', tile_x = 1, tile_y = 0, farbe = 'schwarz', image = black_knight_img)
    R_Knight_black =    Knights(master = screen, name = 'R-Knight-B', tile_x = 6, tile_y = 0, farbe = 'schwarz', image = black_knight_img)
    L_Bishop_black =   Bishops(master = screen, name = 'L-Bishop-B', tile_x = 2, tile_y = 0, farbe = 'schwarz', image = black_bishop_img)
    R_Bishop_black =   Bishops(master = screen, name = 'R-Bishop-B', tile_x = 5, tile_y = 0, farbe = 'schwarz', image = black_bishop_img)
    Queen_black =       Queens(master = screen, name = 'Queen-B', tile_x = 3, tile_y = 0, farbe = 'schwarz', image = black_queen_img)
    King_black =        Kings(master = screen, name = 'King-B', tile_x = 4, tile_y = 0, farbe = 'schwarz', image = black_king_img)

    all_pieces_list =[
                        A_Pawn_white, B_Pawn_white, C_Pawn_white, D_Pawn_white,
                        E_Pawn_white, F_Pawn_white, G_Pawn_white, H_Pawn_white,
                        L_Rook_white, R_Rook_white, L_Knight_white, R_Knight_white,
                        L_Bishop_white, R_Bishop_white, Queen_white, King_white,
                        A_Pawn_black, B_Pawn_black, C_Pawn_black, D_Pawn_black,
                        E_Pawn_black, F_Pawn_black, G_Pawn_black, H_Pawn_black,
                        L_Rook_black, R_Rook_black, L_Knight_black, R_Knight_black,
                        L_Bishop_black, R_Bishop_black, Queen_black, King_black
                     ]

    occupied_tiles = []

    # DOENST WORK QUIET YET !!!!!!!!!!!

    # round_int = 0

    while go:

        #Frames
        clock.tick(30)

        occupied_tiles.clear()

        for piece in all_pieces_list:
            occupied_tiles.append((piece.x, piece.y))

        #creating the board
        for y in range(8):
            for x in range(8):
                if y % 2 == 0:
                    if x % 2 == 0:
                        pygame.draw.rect(screen, (245, 216, 188), [x*tile_size, y*tile_size, tile_size, tile_size])
                    else:
                        pygame.draw.rect(screen, (176, 142, 109), [x*tile_size, y*tile_size, tile_size, tile_size])
                else:
                    if x % 2 == 0:
                        pygame.draw.rect(screen, (176, 142, 109), [x*tile_size, y*tile_size, tile_size, tile_size])
                    else:
                        pygame.draw.rect(screen, (245, 216, 188), [x*tile_size, y*tile_size, tile_size, tile_size])

        if white_is_checked:
            pygame.draw.rect(screen, (247, 87, 87), [King_white.x, King_white.y, tile_size, tile_size])

        elif black_is_checked:
            pygame.draw.rect(screen, (247, 87, 87), [King_black.x, King_black.y, tile_size, tile_size])

        #draw all the pieces
        for pieces in all_pieces_list:
            pieces.draw()

        pygame.display.update()

        #checking for events
        for event in pygame.event.get():

            #closing the screen by clicking the X
            if event.type == pygame.QUIT:
                go = False

            #Keyboard-Inputs
            if event.type == pygame.KEYDOWN:
                #kill window if ESC is pressed
                if event.key == pygame.K_ESCAPE:
                    pygame.event.post(pygame.event.Event(pygame.QUIT))
                if event.key == pygame.K_DOWN:
                    print(type(King_black))
            #Mouse-Inputs
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                mouse_pos = pygame.mouse.get_pos()

                #checking if a Piece stands on the clicked tile
                for piece in all_pieces_list:
                    if mouse_pos[0] >= piece.x and mouse_pos[1] >=piece.y:
                        if mouse_pos[0] < piece.x+tile_size and mouse_pos[1] < piece.y+tile_size:
                            from components.pieces_cls import round_int
                            if round_int % 2 == 1 and piece.farbe == (0,0,0) or round_int % 2 == 0 and piece.farbe == (255, 255, 255):

                                if (white_is_checked or black_is_checked) and str(type(piece)) == "<class 'components.pieces_cls.Kings'>":
                                    piece.move(occupied_tiles=occupied_tiles, all_pieces_list = all_pieces_list)

                                elif (white_is_checked or black_is_checked) and str(type(piece)) != "<class 'components.pieces_cls.Kings'>":
                                    print('You have to deny check')

                                else:
                                    piece.move(occupied_tiles=occupied_tiles, all_pieces_list = all_pieces_list)

                                for white_king in all_pieces_list:
                                    if str(type(white_king)) == "<class 'components.pieces_cls.Kings'>" and white_king.farbe == (255, 255, 255):
                                        for piece in all_pieces_list:
                                            if piece != white_king and piece.farbe != white_king.farbe:
                                                if (white_king.x, white_king.y) in piece.attacted_tiles(all_pieces_list = all_pieces_list):
                                                    white_is_checked = True
                                                    checking_piece = piece
                                                    break
                                                else:
                                                    white_is_checked = False
                                                    checking_piece = None

                                for black_king in all_pieces_list:
                                    if str(type(black_king)) == "<class 'components.pieces_cls.Kings'>" and black_king.farbe == (0, 0, 0):
                                        for piece in all_pieces_list:
                                            if piece != black_king and piece.farbe != black_king.farbe:
                                                if (black_king.x, black_king.y) in piece.attacted_tiles(all_pieces_list = all_pieces_list):
                                                    black_is_checked = True
                                                    checking_piece = piece
                                                    break
                                                else:
                                                    black_is_checked = False
                                                    checking_piece = None

                                if white_is_checked or black_is_checked:
                                    print('Someone is checked')

if __name__ == '__main__':

    main()


"""
  BEGIN-HEADER

  Name: Mohammad Shahriar Hossain
  Student-ID: 1724709

  List any resources you used below (eg. urls, name of the algorithm from our code archive).
  Remember, you are permitted to get help with general concepts about algorithms
  and problem solving, but you are not permitted to hunt down solutions to
  these particular problems!

  no resources

  List any classmate you discussed the problem with. Remember, you can only
  have high-level verbal discussions. No code should be shared, developed,
  or even looked at in these chats. No formulas or pseudocode should be
  written or shared in these chats.

  I did not discuss this exercise with anyone.

  By submitting this code, you are agreeing that you have solved in accordance
  with the collaboration policy in CMPUT 303/403.

  END-HEADER
"""

import math

s, c, k = map(int, input().split())
sock = list(map(int, input().split()))

sock.sort()

machine = 1
basket = [sock[0]]

for i in range(1, s):
    if sock[i] - basket[0] > k or len(basket) == c:
        machine += 1
        basket = [sock[i]]
    else:
        basket.append(sock[i])

print(machine)


import os
import torch as T
import torch.nn.functional as F
import numpy as np
from buffer import ReplayBuffer
from critic import CriticNetwork
from value import ValueNetwork
from actor import ActorNetwork
from torchviz import make_dot
from torch.utils.tensorboard import SummaryWriter

class Agent(object):
    def __init__(self, alpha=0.0003, beta=0.0003, input_dims=(1, 84, 84),
            env=None, gamma=0.99, n_actions=2, max_size=100000, tau=0.005,
            layer1_size=256, layer2_size=256, batch_size=16, reward_scale=2):
        self.summary_writer = SummaryWriter()
        self.gamma = gamma
        self.tau = tau
        self.memory = ReplayBuffer(max_size, input_dims, n_actions)
        self.batch_size = batch_size
        self.n_actions = n_actions

        self.actor = ActorNetwork(alpha, input_dims, n_actions=n_actions,
                    name='actor', max_action=env.action_space.high)
        self.critic_1 = CriticNetwork(beta, input_dims, n_actions=n_actions, name='critic_1' )
        self.critic_2 = CriticNetwork(beta, input_dims, n_actions=n_actions, name='critic_2')
        self.value = ValueNetwork(beta, input_dims, name='value')
        self.target_value = ValueNetwork(beta, input_dims, name='target_value')

        self.scale = reward_scale
        self.update_network_parameters(tau=1)

    def choose_action(self, observation):
        state = T.Tensor([observation]).to(self.actor.device)
        actions, _ = self.actor.sample_normal(state, reparameterize=False)  # actions is Tensor(1, 1,18,3)

        return actions.cpu().detach().numpy()

    def remember(self, state, action, reward, new_state, done):
        self.memory.store_transition(state, action, reward, new_state, done)

    def update_network_parameters(self, tau=None):
        if tau is None:
            tau = self.tau

        target_value_params = self.target_value.named_parameters()
        value_params = self.value.named_parameters()

        target_value_state_dict = dict(target_value_params)
        value_state_dict = dict(value_params)

        for name in value_state_dict:
            value_state_dict[name] = tau*value_state_dict[name].clone() + \
                    (1-tau)*target_value_state_dict[name].clone()

        self.target_value.load_state_dict(value_state_dict)

    def save_models(self):
        print('.... saving models ....')
        self.actor.save_checkpoint()
        self.value.save_checkpoint()
        self.target_value.save_checkpoint()
        self.critic_1.save_checkpoint()
        self.critic_2.save_checkpoint()

    def load_models(self):
        print('.... loading models ....')
        self.actor.load_checkpoint()
        self.value.load_checkpoint()
        self.target_value.load_checkpoint()
        self.critic_1.load_checkpoint()
        self.critic_2.load_checkpoint()

    def learn(self):
        if self.memory.mem_cntr < self.batch_size:
            return
        """"
          Step 1: Sample from the replay buffer.
        """

        state, action, reward, new_state, done = \
                self.memory.sample_buffer(self.batch_size)

        reward = T.tensor(reward, dtype=T.float).to(self.actor.device)
        done = T.tensor(done).to(self.actor.device)
        state_ = T.tensor(new_state, dtype=T.float).to(self.actor.device)
        state = T.tensor(state, dtype=T.float).to(self.actor.device)
        action = T.tensor(action, dtype=T.float).to(self.actor.device)

        """
            Step 2: Compute the values of the current and next state
        """
        value = self.value(state)
        value_ = self.target_value(state_).view(-1)  # value_ is Tensor(21504,)
        value_[done] = 0.0 #  the size of value_ is Tensor(21504,). done is of size Tensor(256,)d

        """
            Step 3: Predict the actions  and log probabilities of the current state.
        """
        actions, log_probs = self.actor.sample_normal(state, reparameterize=False)  # state is Tensor(256,1,84,84)
                                                            # action is Tensor(256,3) log_probs is Tensor(256,1)
         # log_probs = log_probs.view(-1)  # log_probs is Tensor(256,)
        q1_new_policy = self.critic_1.forward(state, actions)  # q1_new_policy is Tensor(256,1)
        q2_new_policy = self.critic_2.forward(state, actions)  # q2_new_policy is Tensor(256,1)
        critic_value = T.min(q1_new_policy, q2_new_policy)  # critic_value is Tensor(256,1)
        # critic_value = critic_value.view(-1)  # critic_value is now Tensor(256,)

        """
            Step 4: Backpropagate the value network.
        """
        self.value.optimizer.zero_grad()
        value_target = critic_value - log_probs # value_target is Tensor(256,1)
        the_mse_loss = F.mse_loss(value, value_target, reduction='none')  # the_mse_loss is Tensor(256,1)
        value_loss = 0.5 * the_mse_loss
        value_loss = value_loss.sum()
        value_loss.backward(retain_graph=True)  #value_loss is Tensor(256,1), keep getting error here. why?
        self.value.optimizer.step()

        actions, log_probs = self.actor.sample_normal(state, reparameterize=True)
        log_probs = log_probs.view(-1)

        q1_new_policy = self.critic_1.forward(state, actions)
        q2_new_policy = self.critic_2.forward(state, actions)
        critic_value = T.min(q1_new_policy, q2_new_policy)
        critic_value = critic_value.view(-1)

        actor_loss = log_probs - critic_value
        actor_loss = T.mean(actor_loss)
        self.actor.optimizer.zero_grad()
        actor_loss.backward(retain_graph=True)
        self.actor.optimizer.step()

        self.critic_1.optimizer.zero_grad()
        self.critic_2.optimizer.zero_grad()
        q_hat = self.scale*reward + self.gamma*value_
        q1_old_policy = self.critic_1.forward(state, action).view(-1)
        q2_old_policy = self.critic_2.forward(state, action).view(-1)
        critic_1_loss = 0.5 * F.mse_loss(q1_old_policy, q_hat)
        critic_2_loss = 0.5 * F.mse_loss(q2_old_policy, q_hat)

        critic_loss = critic_1_loss + critic_2_loss
        critic_loss.backward()
        self.critic_1.optimizer.step()
        self.critic_2.optimizer.step()

        self.update_network_parameters()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar  9 09:47:14 2019

@author: davidwilbur
"""
import os
import re
import lda
import time
import logging
import numpy as np
import sqlite3
import pandas as pd
from sklearn.manifold import TSNE
from Functions_Wilbur_Zimmermann import tokenize_only
from sklearn.feature_extraction.text import CountVectorizer
# Plotly plotting packages
import plotly
import plotly.graph_objs as go
from nltk.corpus import stopwords
from gensim.test.utils import common_texts
from gensim.models.doc2vec import Doc2Vec, TaggedDocument
from sklearn import metrics
import gensim.models as g
import codecs
from nltk.cluster import KMeansClusterer
import nltk

from gensim.models import Word2Vec
from nltk.cluster import KMeansClusterer
import nltk
import numpy as np
from sklearn import cluster
from sklearn import metrics
#%%
# training data
 # %%
# Set global path variables
directory = os.path.dirname('__file__')
input_file = os.path.join(directory,'..','Article Collection')
_dbFile = os.path.join(input_file,'articles_zenodo.db')

#%%
# Set up our connection to the database
#
_db = sqlite3.connect(_dbFile)
_cursor = _db.cursor()

# Pull the relevant bits of data out of the database and
# store them in a pandas dataframe.
#
print('Pulling article IDs, leanings, and text from database')
_cursor.execute("SELECT ln.id, ln.bias_final, cn.text " +
                "FROM train_lean ln, train_content cn " +
                "WHERE cn.`published-at` >= '2018-01-01' AND ln.id == cn.id AND ln.url_keep='1'")
_df = pd.DataFrame(_cursor.fetchall(), columns=('id', 'lean', 'text'))
_db.close()
#%%
_df.text = _df.text.str.lower()
nonAlpha = re.compile('[^a-zA-Z]+')
_df.text = _df.text.replace(nonAlpha, ' ')

cachedStopWords = stopwords.words('english')
_df.text = _df.text.apply(
                          lambda x: (' '.join([word for word in x.split()
                          if word not in cachedStopWords])))

#%%
print('Tokenizing Articles')
import gensim
from nltk.tokenize import word_tokenize
raw_documents = _df.text
gen_docs = [[w.lower() for w in word_tokenize(text)] for text in raw_documents]

 #%%

model = Word2Vec(gen_docs, min_count=1)

def sent_vectorizer(sent, model):
    sent_vec =[]
    numw = 0
    for w in sent:
        try:
            if numw == 0:
                sent_vec = model[w]
            else:
                sent_vec = np.add(sent_vec, model[w])
            numw+=1
        except:
            pass

    return np.asarray(sent_vec) / numw

X=[]
for sentence in gen_docs:
    X.append(sent_vectorizer(sentence, model))

NUM_CLUSTERS=5
kclusterer = KMeansClusterer(NUM_CLUSTERS, distance=nltk.cluster.util.cosine_distance, repeats=25)
assigned_clusters = kclusterer.cluster(X, assign_clusters=True)
print (assigned_clusters)

for index, sentence in enumerate(sentences):
    print (str(assigned_clusters[index]) + ":" + str(sentence))

kmeans = cluster.KMeans(n_clusters=NUM_CLUSTERS)
kmeans.fit(X)

labels = kmeans.labels_
centroids = kmeans.cluster_centers_

print ("Cluster id labels for inputted data")
print (labels)
print ("Centroids data")
print (centroids)

print ("Score (Opposite of the value of X on the K-means objective which is Sum of distances of samples to their closest cluster center):")
print (kmeans.score(X))

silhouette_score = metrics.silhouette_score(X, labels, metric='euclidean')

print ("Silhouette_score: ")
print (silhouette_score)

import matplotlib.pyplot as plt

from sklearn.manifold import TSNE

model = TSNE(n_components=3, random_state=0)
np.set_printoptions(suppress=True)

Y=model.fit_transform(X)
_lean = _df['lean'][_idx]

def SetColor(x):
    if(x == 'left'): return "blue"
    elif(x == 'left-center'): return "lightblue"
    elif(x == 'least'): return "lightgray"
    elif(x == 'right-center'): return "orangered"
    elif(x == 'right'): return "red"

tsne_lda_df = pd.DataFrame(Y, columns=['x', 'y', 'z'])
# tsne_lda_df['Resumes'] = tsne_lda_df['Resumes']
x = tsne_lda_df.x
y = tsne_lda_df.y
z = tsne_lda_df.z

trace1 =\
    go.Scatter3d(x=x, y=y, z=z,
                 mode='markers',
                 marker = dict(color=list(map(SetColor, _lean))))

data = [trace1]
layout = go.Layout(
                   scene=dict(
                    xaxis=dict(
                        nticks=4, range=[-20, 20], ),
                    yaxis=dict(
                        nticks=4, range=[-20, 20], ),
                    zaxis=dict(
                        nticks=4, range=[-20, 20], ), ),
                    width=700,
                    margin=dict(
                    r=20, l=10,
                    b=10, t=10)
                  )
fig = go.Figure(data=data, layout=layout)
plotly.offline.plot(fig)

# plt.scatter(Y[:, 0], Y[:, 1], c=assigned_clusters, s=290,alpha=.5)
#
#
#for j in range(len(sentences)):
#   plt.annotate(assigned_clusters[j],xy=(Y[j][0], Y[j][1]),xytext=(0,0),textcoords='offset points')
#   print ("%s %s" % (assigned_clusters[j],  sentences[j]))
#
#
#plt.show()


# 2001 파리 퇴치

T = int(input())

for a in range(T):
    N, M = map(int, input().split()) # N, M 받기
    arr = [list(map(int, input().split())) for i in range(N)] # arr에 배열 받기

    kills = [] # 파리 죽인 수 받을 리스트 생성
    # 파리채를 내려칠 곳 탐색
    for i in range(N-M+1): # M~N 까지 범위
        for j in range(N-M+1):
            fly = 0 # 파리 개수
            # 해당 위치를 타격했을 때 잡을 수 있는 파리의 수 탐색
            for k in range(M): # M*M 배열의 [k][l] 칸으로 탐색
                for l in range(M):
                    fly += arr[i+k][j+l] # 칸의 번호 반환
            kills.append(fly)

	# 배열 범위 안에서 가능한 경우의 수 중에서 가장 많은 파리를 잡을 때의 수 출력
    print("#"+str(a+1), max(kills))

#     range(N-M+1) : 파리채를 휘두를 수 있는 공간
# ex) M의 크기가 1일 경우, 모든 배열 공간을 대상으로 탐색할(파리채를 휘두를) 수 있다. 그런데 파리채의 크기가 커지게 되면, 탐색할 수 있는 배열 공간은 줄어들게 된다.
# 이에 따라 탐색 범위를 조절해줘야 하기 때문에 N-M+1으로 조정하였다.
# N이 5일 때, 파리채(M)이 1일 때 휘두를 수 있는 경우의 수 = 25가지
# 동일한 조건에서 M = 2일 때 휘두를 수 있는 경우의 수 = 16가지
# 동일한 조건에서 M = 3일 때 휘두를 수 있는 경우의 수 = 9가지 ...


"""
Constraints and assumptions:
    1. The pattern should not start with '*', becaues there is no element before that to match
    2. String and pattern can have any length
    3. '*' can match an element zero, one, or more times.

    @author: Mehrnaz
"""

import sys

def isMatch(string, pattern):

    if not pattern:   #if pattern is empty, it returns true if the string is empty and false if not
        return not string
    elif not string:  #string is empty and pattern is not, so it always returns false
        return False
    else:
        match = pattern[0] in {string[0],'.'}
        if len(pattern) > 1 and pattern[1] == '*':
            #When there is '*', we check two scenarios: if '*' is matching the preceding element for 1 or more times or
            #it does not do anything (match for 0 time) and we should compare the rest of the string with the rest of the
            #pattern starting from the element after '*'
            return match and (isMatch(string[1:],pattern) or isMatch(string[1:],pattern[2:]))
        else:
            return match and isMatch(string[1:],pattern[1:])