import argparse
import sys
from xml.etree import ElementTree

def junit_to_trs(junit_xml):
    root = ElementTree.fromstring(junit_xml)
    testcases = root.findall("./testsuite/testcase")

    if len(testcases) < 1:
        print(":test-result: ERROR convert-junit-to-trs.py")
        return 99

    has_fail = False
    has_error = False
    has_skipped = False
    for testcase in testcases:
        filename = f"{testcase.attrib['classname'].replace('.', '/')}.py"
        name = f"{filename}::{testcase.attrib['name']}"
        res = "PASS"
        for node in testcase:
            if node.tag == "failure":
                res = "FAIL"
                has_fail = True
            elif node.tag == "error":
                res = "ERROR"
                has_error = True
            elif node.tag == "skipped":
                if node.attrib.get("type") == "pytest.xfail":
                    res = "XFAIL"
                else:
                    res = "SKIP"
                    has_skipped = True
        print(f":test-result: {res} {name}")

    if has_error:
        return 99
    if has_fail:
        return 1
    if has_skipped:
        return 77
    return 0

def main():
    parser = argparse.ArgumentParser(
        description="Convert JUnit XML to Automake TRS and exit with "
        "the appropriate Automake-compatible exit code."
    )
    parser.add_argument(
        "junit_file",
        type=argparse.FileType("r", encoding="utf-8"),
        help="junit xml result file",
    )
    args = parser.parse_args()

    junit_xml = args.junit_file.read()
    sys.exit(junit_to_trs(junit_xml))

if __name__ == "__main__":
    main()


# Draws an off-screen buffer and display it in the corner of the view.
import bpy
from bgl import *

class OffScreenDraw(bpy.types.Operator):
    bl_idname = "view3d.offscreen_draw"
    bl_label = "View3D Offscreen Draw"

    _handle_calc = None
    _handle_draw = None
    is_enabled = False

    # manage draw handler
    @staticmethod
    def draw_callback_px(self, context):
        scene = context.scene
        aspect_ratio = scene.render.resolution_x / scene.render.resolution_y

        self._update_offscreen(context, self._offscreen)
        self._opengl_draw(context, self._texture, aspect_ratio, 0.2)

    @staticmethod
    def handle_add(self, context):
        OffScreenDraw._handle_draw = bpy.types.SpaceView3D.draw_handler_add(
                self.draw_callback_px, (self, context),
                'WINDOW', 'POST_PIXEL',
                )

    @staticmethod
    def handle_remove():
        if OffScreenDraw._handle_draw is not None:
            bpy.types.SpaceView3D.draw_handler_remove(OffScreenDraw._handle_draw, 'WINDOW')

        OffScreenDraw._handle_draw = None

    # off-screen buffer
    @staticmethod
    def _setup_offscreen(context):
        import gpu
        scene = context.scene
        aspect_ratio = scene.render.resolution_x / scene.render.resolution_y

        try:
            offscreen = gpu.offscreen.new(512, int(512 / aspect_ratio))
        except Exception as e:
            print(e)
            offscreen = None

        return offscreen

    @staticmethod
    def _update_offscreen(context, offscreen):
        scene = context.scene
        render = scene.render
        camera = scene.camera

        modelview_matrix = camera.matrix_world.inverted()
        projection_matrix = camera.calc_matrix_camera(
                render.resolution_x,
                render.resolution_y,
                render.pixel_aspect_x,
                render.pixel_aspect_y,
                )

        offscreen.draw_view3d(
                scene,
                context.space_data,
                context.region,
                projection_matrix,
                modelview_matrix,
                )

    @staticmethod
    def _opengl_draw(context, texture, aspect_ratio, scale):
        """
        OpenGL code to draw a rectangle in the viewport
        """

        glDisable(GL_DEPTH_TEST)

        # view setup
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glLoadIdentity()

        glMatrixMode(GL_MODELVIEW)
        glPushMatrix()
        glLoadIdentity()

        glOrtho(-1, 1, -1, 1, -15, 15)
        gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)

        act_tex = Buffer(GL_INT, 1)
        glGetIntegerv(GL_TEXTURE_2D, act_tex)

        viewport = Buffer(GL_INT, 4)
        glGetIntegerv(GL_VIEWPORT, viewport)

        width = int(scale * viewport[2])
        height = int(width / aspect_ratio)

        glViewport(viewport[0], viewport[1], width, height)
        glScissor(viewport[0], viewport[1], width, height)

        # draw routine
        glEnable(GL_TEXTURE_2D)
        glActiveTexture(GL_TEXTURE0)

        glBindTexture(GL_TEXTURE_2D, texture)

        texco = [(1, 1), (0, 1), (0, 0), (1, 0)]
        verco = [(1.0, 1.0), (-1.0, 1.0), (-1.0, -1.0), (1.0, -1.0)]

        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

        glColor4f(1.0, 1.0, 1.0, 1.0)

        glBegin(GL_QUADS)
        for i in range(4):
            glTexCoord3f(texco[i][0], texco[i][1], 0.0)
            glVertex2f(verco[i][0], verco[i][1])
        glEnd()

        # restoring settings
        glBindTexture(GL_TEXTURE_2D, act_tex[0])

        glDisable(GL_TEXTURE_2D)

        # reset view
        glMatrixMode(GL_PROJECTION)
        glPopMatrix()

        glMatrixMode(GL_MODELVIEW)
        glPopMatrix()

        glViewport(viewport[0], viewport[1], viewport[2], viewport[3])
        glScissor(viewport[0], viewport[1], viewport[2], viewport[3])

    # operator functions
    @classmethod
    def poll(cls, context):
        return context.area.type == 'VIEW_3D'

    def modal(self, context, event):
        if context.area:
            context.area.tag_redraw()

        return {'PASS_THROUGH'}

    def invoke(self, context, event):
        if OffScreenDraw.is_enabled:
            self.cancel(context)

            return {'FINISHED'}

        else:
            self._offscreen = OffScreenDraw._setup_offscreen(context)
            if self._offscreen:
                self._texture = self._offscreen.color_texture
            else:
                self.report({'ERROR'}, "Error initializing offscreen buffer. More details in the console")
                return {'CANCELLED'}

            OffScreenDraw.handle_add(self, context)
            OffScreenDraw.is_enabled = True

            if context.area:
                context.area.tag_redraw()

            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}

    def cancel(self, context):
        OffScreenDraw.handle_remove()
        OffScreenDraw.is_enabled = False

        if context.area:
            context.area.tag_redraw()

def register():
    bpy.utils.register_class(OffScreenDraw)

def unregister():
    bpy.utils.unregister_class(OffScreenDraw)

if __name__ == "__main__":
    register()


from block.models import Block

from django.contrib.auth.models import User
from django.shortcuts import render
from django.shortcuts import redirect
from .models import Article
from .forms import ArticleForm

from comment.models import Comment
from django.views.generic import View
from django.views.generic import DeleteView
from django.views.generic import DetailView

from .paginate import paginate_queryset

def article_list(request,block_id):
    block_id = int(block_id)
    block = Block.objects.get(id=block_id)
    page_no = int(request.GET.get("page_no","1"))
    all_articles = Article.objects.filter(block=block,status=0).order_by("-id")

    ###页面上需要分布的变量
    page_articles,paginate_data = paginate_queryset(all_articles,page_no)

    return render(request,"article_list.html",{"articles":page_articles,"b":block,"paginate_data":paginate_data})

ARTICLE_CNT_1PAGE = 3     ##定义每页显示多少
def article_list_v1(request,block_id):
    block_id = int(block_id)
    block = Block.objects.get(id=block_id)
    #article_objs = Article.objects.filter(block=block,status=0).order_by("-id")
    ##实现分布 需要的变更
    page_no = int(request.GET.get("page_no","1"))   ##当前页码 ，如果没有相应参数，默认给出1
    start_index = (page_no-1) * ARTICLE_CNT_1PAGE   ##当前页数据的起始索引
    end_index = page_no * ARTICLE_CNT_1PAGE         ##当前页数据的结束索引

    all_articles = Article.objects.filter(block=block,status=0).order_by("-id")
    p = Paginator(all_articles,ARTICLE_CNT_1PAGE)
    page = p.page(page_no)
    articles = page.object_list

    ###页面上需要分布的变量
    page_nums = p.num_pages  #总页数
    page_links = [i for i in range(page_no-3,page_no+4) if i>0 and i<=page_nums]  ##标页列表
    page_first = page_links[0]-1   ###标页列表最小页
    page_max = page_links[-1]-1    ###标页列表最大页
    print("page_nums.type:",type(page_nums))
    #page_next = page_max <= page_nums    ###是否有后一页
    #page_pre = page_first > 0 ###是否有前一页

    ##上边看起来像是判断对象相等，其实是django包装了，底层是判断id比对
    ##所以，也可以写成filter(block__id=block_id)
    ## fileter(block=block,status=0)  表示 where block=? and status=?
    ## 如果想表达或，需要使用Q对象,每个Q表示一个条件，用|表示或，from django.db.models import Q
    ## filter(Q(block=block)|Q(status=0)|Q(x=y))

    return render(request,"article_list.html",{"articles":articles,"b":block,"page_nums":page_nums,"page_links":page_links,"page_first":page_first,"page_max":page_max,"page_no":page_no,"page_next":page_no+1,"page_pre":page_no-1})

#基于函数实现的处理逻辑：简单快速、但是长期看面临迁移到类

def publish_article(request,b_id):
    b_id = int(b_id)
    block = Block.objects.get(id=b_id)
    if request.method=="GET":
        return render(request,"publish_article.html",{"bol":block})
    else:
        #第一种判断语句写法
        #title = request.POST["title"].strip()
        #content = request.POST["content"].strip()
        #if not title or not content:
        #    return render(request,"publish_article.html",{"bol":block,"error":"标题或内容不能为空","title":title,"content":content})
        #if len(title)>100 or len(content)>10000:
        #    return render(request,"publish_article.html",{"bol":block,"error":"标题或内容过长","title":title,"content":content})
        #else:
        #    article = Article(block=block,title=form.cleaned_data["title"],content=form.cleaned_data["content"],status=0)
        #    article.save()

        #第二种写法：把判断语句 抽出来到forms.py 里
        #form = ArticleForm(request.POST)
        #if form.is_valid():
        #    article = Article(block=block,title=form.cleaned_data["title"],content=form.cleaned_data["content"],status=0)
        #    article.save()
        #    return redirect("/article/list/%s" % b_id)
        #else:
        #    print(form.errors)
        #    return render(request,"publish_article.html",{"bol":block,"form":form})

        #第三种写法：整合,先用form.save提交form中的信息，保存在内存中
        form = ArticleForm(request.POST)
        if form.is_valid():
            article = form.save(commit=False)
            article.block = block
            article.status=0
            article.save()
            return redirect("/article/list/%s" % b_id)
        else:
            return render(request,"publish_article.html",{"bol":block,"form":form})

#基于类实现处理逻辑，相对复杂，可以使用高级的设计模式
class ArticleCreate(View):
    template_name="publish_article.html"

    def init_data(self,block_id):
        self.block_id = block_id
        self.block = Block.objects.get(id=block_id)
        #self.owner = User.objects.get(id)

    def get(self,request,block_id):
        print("request.method............:",request.method)
        self.init_data(block_id)
        return render(request,self.template_name,{"bol":self.block})

    def post(self,request,block_id):
        self.init_data(block_id)
        self.user = request.user
        form = ArticleForm(request.POST)
        if form.is_valid():
            article = form.save(commit=False)
            article.block = self.block
            article.owner = self.user
            article.status=0
            article.save()
            return redirect("/article/list/%s" % block_id)
        else:
            return render(request,"publish_article.html",{"bol":self.block,"form":form})

#####文章详情页面也使用两种方式分别实现一下
###基于函数

###2017-05-30 增加评论展示

def article_detail(request,block_id,aid):
    block_id = int(block_id)
    aid = int(aid)
    block = Block.objects.get(id=block_id)
    article = Article.objects.get(id=aid)

    ##评论内容及评论分页
    #all_comments = Comment.objects.filter(article = aid,status=0).order_by("-create_timestamp")  ###order_by ("-column_name")  表示降序
    all_comments = Comment.normal_objects.filter(article=aid).order_by("-create_timestamp")  ##增加数据管理员逻辑
    page_no = int(request.GET.get("page_no","1"))
    page_comments,paginate_data = paginate_queryset(all_comments,page_no)
    #print("all_comments..........",all_comments.size)
    #msg_cnt = message_cnt(request.user)
    print(block_id,block.name,block.manager_name)
    return render(request,"article_detail.html",{"block_id":block_id,"block_name":block.name,"art":article,"comments":page_comments,"paginate_data":paginate_data})

###基于类

class ArticleDetailView(DetailView):
    model = Article
    tempate_name = 'article_detail.html'
    context_object_name = 'article'

    def init_data(self,block_id,aid):
        self.block_id = block_id
        self.block = Block.objects.get(id=block_id)
        self.aid = aid
        self.article = Article.objects.get(id = aid)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        page_no = int(self.request.GET.get("page_no","1"))
        all_comments = Comment.objects.filter(article=context["article"],status=0)
        print("all_comments...............",all_comments)
        comments,pagination_data = paginate_queryset(all_comments,page_no,cnt_per_page=3)
        context['comments'] = comments

        return context

    def get(self,request,block_id,aid):
        print("request.method............:",request.method)
        self.init_data(block_id,aid)
        return render(request,self.template_name,{"block_id":block_id,"block_name":block.name,"art":article})

    def post(self,request,block_id):
        self.init_data(block_id,aid)
        self.user = request.user
        form = ArticleForm(request.POST)
        if form.is_valid():
            article = form.save(commit=False)
            article.block = self.block
            article.owner = self.user
            article.status=0
            article.save()
            return redirect("/article/list/%s" % block_id)
        else:
            return render(request,"article_detail.html",{"bol":self.block,"form":form})


import tkinter as tk
import requests
import datetime

def get_covid_data():
    api = "https://disease.sh/v3/covid-19/all"
    json_data = requests.get(api).json()
    total_cases = str(json_data['cases'])
    total_deaths = str(json_data['deaths'])
    today_cases = str(json_data['todayCases'])
    today_deaths = str(json_data['todayDeaths'])
    today_recovered = str(json_data['todayRecovered'])
    updated_at = json_data['updated']
    date = datetime.datetime.fromtimestamp(updated_at/1e3)
    label.config(text="Total Cases: " + total_cases + "\nTotal Deaths: " + total_deaths + "\nToday's Cases: " + today_cases + "\nToday's Deaths: " + today_deaths + "\nRecovered: " + today_recovered)
    label2.config(text = date)
canvas = tk.Tk()
canvas.geometry("400x400")
canvas.title("Covid-19 Tracker")

f = ("poppins", 20, "bold")
button = tk.Button(canvas, text="Get Data", font=f, command=get_covid_data)
button.pack(pady=20)

label =tk.Label(canvas, text="Covid-19 Tracker", font=f)
label.pack(pady=20)

label2 = tk.Label(canvas, font=8)
label2.pack()
get_covid_data()
canvas.mainloop()


import sys
import time
import signal
import threading
import numpy as np
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QVBoxLayout, QGridLayout
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import pyqtgraph as pg
sys.path.append("..")
from Modules import Module, Analyzer

class ParameterPlotter(Analyzer):
    def __init__(self, id=None, autoMove=True, standalone=False, stepMode=False):
        super().__init__(id)

        self.parameterInputs.append({"name": "input", "module": None, "sourceIndex": 0, "default": 0.0})
        self.parameterInputs.append({"name": "input", "module": None, "sourceIndex": 0, "default": 0.0})
        self.parameterInputs.append({"name": "input", "module": None, "sourceIndex": 0, "default": 0.0})
        self.parameterInputs.append({"name": "input", "module": None, "sourceIndex": 0, "default": 0.0})

        self.autoMove = autoMove
        self.standalone = standalone
        self.stepMode = stepMode
        self.standaloneT = 0
        self.updateFunction = None

        self.maxSampleTime = 60 * 5          # [s]
        self.maxTimeWidth = 5                # [s]
        self.manualControlTime = 5           # [s]
        self.x = []
        self.y = [[], [], [], []]
        self.yMin = -1.0
        self.yMax = 1.0
        self.running = False
        self.ready = False
        self.legendAdded = [False] * 4

        if self.standalone:
            def signal_handler(sig, frame):
                self.running = False
                self.widget.graphWidget.close()

            signal.signal(signal.SIGINT, signal_handler)
            threading.Thread(target=self.run, args=(self, )).start()
            while not self.running:
                time.sleep(0.1)

        else:
            self.widget = MainWindow(stepMode=self.stepMode)
            self.app = QtWidgets.QApplication(sys.argv)
            threading.Thread(target=self.app.exec_).start()
            self.widget.show()

    def stop(self):
        self.running = False
        self.widget.close()

    def run(self, ref):
        ref.app = QtWidgets.QApplication(sys.argv)
        ref.widget = MainWindow(stepMode=self.stepMode)
        ref.widget.show()

        self.running = True
        setattr(ref, "app", ref.app)
        setattr(ref, "widget", ref.widget)

        if ref.standalone:
            ref.timer = QTimer()
            ref.timer.timeout.connect(ref.standaloneUpdate)
            ref.timer.start(int(1000 / Module.framerate))

        while ref.widget.graphWidget.isVisible():
            ref.app.processEvents()
        ref.widget.close()
        ref.app.quit()

    def isRunning(self):
        return self.widget.graphWidget.isVisible()

    def isReady(self):
        return self.ready

    def update(self, t):
        if(Module.framerate == 0):
            return False
        self.ready = True
        if self.standalone:
            while not any(self.parameterInputs):
                print("waiting")
                time.sleep(0.1)
        if not super().update(t):
            return False

        if any([i["module"] for i in self.parameterInputs]):    # Check if any input is connected to module
            self.widget.setWindowTitle(f"Parameter Plotter [{self.id}]")
        else:
            self.widget.setWindowTitle(f"Parameter Plotter [{self.id}]: Unconnected")
            return True

        for i in range(len(self.widget.data_line)):
            if not self.parameterInputs[i]["module"]:
                continue

            if not self.legendAdded[i]:
                self.legendAdded[i] = True
                module = self.parameterInputs[i]
                moduleName = f"{module['module'].superClassType}.{module['module'].__module__}"
                self.widget.legend.plotItem.legend.addItem(self.widget.legendItem[i], f"{moduleName} [{module['module'].id}] (Output: {module['sourceIndex']})")

            output = self._getParameterValue(i)
            self.yMin = min(self.yMin, output)
            self.yMax = max(self.yMax, output)
            if t not in self.x:
                self.x.append(t)
            self.y[i].append(output)

            maxSamples = int(self.maxSampleTime * Module.framerate)
            if(len(self.y[i]) > maxSamples):
                self.x = self.x[-maxSamples:]
            self.y[i] = self.y[i][-len(self.x):]

            self.widget.plotItem.setYRange(self.yMin * 1.2, self.yMax * 1.2, padding=0)
            if(time.time() - self.widget.graphWidget.mouseEventTime > self.manualControlTime) and self.autoMove:
                self.widget.plotItem.setXRange(max(0, t - self.maxTimeWidth), t, padding=0)

            if(len(self.x) > 2):
                self.widget.data_line[i].setData(self.x, self.y[i])
        return True

    def standaloneUpdate(self):
        if not self.running:
            return
        self.standaloneT += 1 / Module.framerate
        if not self.isReady():      # Start time increment only when analyzer is ready
            self.standaloneT = 0
        if self.updateFunction:
            try:
                self.updateFunction(self.standaloneT)
            except Exception as e:
                self.end()
                print(e)

class MainPlotWidget(pg.PlotWidget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.mouseEventTime = 0
        self.isClicked = False

    def wheelEvent(self,event):
        super().wheelEvent(event)
        self.mouseEventTime = time.time()

    def mouseReleaseEvent(self, event):
        super().mouseReleaseEvent(event)
        self.mouseEventTime = time.time()
        self.isClicked = False

    def mousePressEvent(self, event):
        super().mousePressEvent(event)
        self.mouseEventTime = time.time()
        self.isClicked = True

    def mouseMoveEvent(self, event):
        super().mouseMoveEvent(event)
        if self.isClicked:
            self.mouseEventTime = time.time()

class CustomLegend(pg.PlotWidget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def wheelEvent(self,event): pass
    def mouseReleaseEvent(self, event): pass
    def mousePressEvent(self, event): pass
    def mouseMoveEvent(self, event): pass

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        self.plotMode = "right" if kwargs.pop("stepMode", None) else None
        super(MainWindow, self).__init__(*args, **kwargs)
        self.widget = QWidget()
        self.widget.setStyleSheet('background-color: #212124')

        self.graphWidget = MainPlotWidget()
        self.setCentralWidget(self.graphWidget)
        self.setGeometry(300, 300, 700, 500)
        self.graphWidget.setBackground('#212124')
        self.plotItem = self.graphWidget.getPlotItem()
        self.plotItem.showGrid(x=True, y=True, alpha=0.3)
        self.plotItem.getAxis('bottom').setGrid(False)             # Disable bottom axis grid, prevent bus as noted here: https://stackoverflow.com/questions/69816567/pyqtgraph-cuts-off-tick-labels-if-showgrid-is-called
        self.plotItem.showAxis('top')                              # Show top/right axis (and grid, since enabled here)
        self.plotItem.getAxis('top').setStyle(showValues=False)    # Hide tick labels on top/right
        self.plotItem.setMouseEnabled(x=True, y=False)
        self.plotItem.hideButtons()

        self.pen = [pg.mkPen(color=(0, 142, 211), width=3),
                    pg.mkPen(color=(211, 105, 0), width=3),
                    pg.mkPen(color=(211, 0, 172), width=3),
                    pg.mkPen(color=(0, 211, 18), width=3)]

        self.data_line = [self.graphWidget.plot([0, 0], [0, 0], pen=self.pen[0], stepMode=self.plotMode),
                          self.graphWidget.plot([0, 0], [0, 0], pen=self.pen[1], stepMode=self.plotMode),
                          self.graphWidget.plot([0, 0], [0, 0], pen=self.pen[2], stepMode=self.plotMode),
                          self.graphWidget.plot([0, 0], [0, 0], pen=self.pen[3], stepMode=self.plotMode)]

        self.legendItem = [pg.LegendItem(pen=self.pen[0]),
                           pg.LegendItem(pen=self.pen[1]),
                           pg.LegendItem(pen=self.pen[2]),
                           pg.LegendItem(pen=self.pen[3])]

        self.legend = CustomLegend()
        self.legend.setBackground('#212124')
        self.legend.setMouseEnabled(x=False, y=False)
        self.legend.hideButtons()
        self.legend.setMaximumHeight(45)
        self.legend.hideAxis('left')
        self.legend.hideAxis('bottom')
        self.legend.addLegend()
        self.legend.plotItem.legend.setColumnCount(2)
        self.legend.plotItem.legend.anchor((-0.05, 0.15), (0.0, 0.0))

        self.layout = QVBoxLayout()
        self.widget.setLayout(self.layout)
        self.layout.addWidget(self.graphWidget)
        self.layout.addWidget(self.legend)
        self.setCentralWidget(self.widget)

    def closeEvent(self, event):
        event.accept()

if __name__ == '__main__':
    from Modules import Coefficient, Generator, Analyzer
    Module.framerate = 60

    enable = 1.0
    freq = 1
    rep = 1
    amp = 0.5
    offset = 0.5
    phase = 0

    sine = Generator.Sine(0)
    sine.setParameterInput(0, Coefficient(1000, enable))
    sine.setParameterInput(1, Coefficient(1001, freq))
    sine.setParameterInput(2, Coefficient(1002, rep))
    sine.setParameterInput(3, Coefficient(1003, amp))
    sine.setParameterInput(4, Coefficient(1004, offset))
    sine.setParameterInput(5, Coefficient(1005, phase))

    ramp = Generator.Ramp(1)
    rect = Generator.Rect(2)
    triangle = Generator.Triangle(3)

    plotter = Analyzer.ParameterPlotter(5000, standalone=True, stepMode=False)
    plotter.setParameterInput(0, sine, 0)
    plotter.setParameterInput(1, ramp, 0)
    plotter.setParameterInput(2, rect, 0)
    plotter.setParameterInput(3, triangle, 0)

    def update(t):
        sine.update(t)
        ramp.update(t)
        rect.update(t)
        triangle.update(t)
        plotter.update(t)

    plotter.updateFunction = update
    while not plotter.isReady(): pass    # Wait until plotter is ready
    while plotter.isRunning():
        time.sleep(0.1)


from django.shortcuts import render , get_object_or_404
from .models import Post , Category

def show_category(request,hierarchy= None):
    category_slug = hierarchy.split('/')
    parent = None
    root = Category.objects.all()
    for slug in category_slug[:-1]:
        parent = root.get(parent=parent, slug = slug)

    try:
        instance = Category.objects.get(parent=parent,slug=category_slug[-1])
    except:
        instance = get_object_or_404(Post, slug = category_slug[-1])
        return render(request, "post_detail.html", {'instance':instance})
    else:
        return render(request, 'categories.html', {'instance':instance})

def post_detail(request,slug=None):
    instance = get_object_or_404(Post,slug=slug)
    return render(request,"post_detail.html",{ 'instance':instance})

def post_list(request,slug=None):
    posts = Post.objects.all()
    return render(request,"post_list.html",{ 'posts':posts})


from datetime import datetime
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'espa.espa.settings')

import django

django.setup()

from espa.espa.models import Project
from espa.espa.processors.AnaptyxiBudgetAnalysis import convertAmountStringToInteger
from espa.espa.scrapers.anaptyxi.ΑnaptyxiScraper import ratioToBudgetForEachPrefecture, prefectures, prefectureCodeToNUTSCodeMapping

'''
Prints a list of budget per municipality.
'''
def municipalityAnalysis():
    prefectureSums = {}

    for code in prefectures.keys():
        prefectureSums[code] = {}
        for i in range(1996, 2016):  # we magically know that project years are in this range
            prefectureSums[code][i] = 0

    totalBudgetSum = 0  # for checking
    countOfProjects = 0
    ignoredBecauseOfDateBudgetSum = 0

    for project in Project.objects.all():
        countOfProjects += 1
        prefTotalWeight = 0
        projectBudget = convertAmountStringToInteger(project.budget)
        totalBudgetSum += projectBudget
        if project.startDate == "-":
            ignoredBecauseOfDateBudgetSum += projectBudget
            continue
        for prefecture in project.prefecturesOwning.all():
            if prefecture.code != "14":
                prefTotalWeight += ratioToBudgetForEachPrefecture[prefecture.code]

        localPrefectureSum = 0
        for prefecture in project.prefecturesOwning.all():
            if prefecture.code != "14":
                currentPrefWeight = ratioToBudgetForEachPrefecture[prefecture.code] / prefTotalWeight
                currentPrefectureAllocation = projectBudget * currentPrefWeight
                projectYear = datetime.strptime(project.startDate, "%d/%m/%Y").year
                prefectureSums[prefecture.code][projectYear] += currentPrefectureAllocation
                localPrefectureSum += currentPrefectureAllocation
        if abs(localPrefectureSum - projectBudget) > 1:
            print("project %s has prefectures %s but the budget does not equal the allocation (%d vs %d)"
                  % (project, project.prefecturesOwning.all(), projectBudget, localPrefectureSum))
    print(totalBudgetSum)
    valuesSum = 0
    for yearValue in prefectureSums.values():
        for value in yearValue.values():
            valuesSum += value
    prettyPrintBudgets(prefectureSums)
    print(valuesSum)
    print(totalBudgetSum - valuesSum)
    print("ignored because of date sum up to %d " % ignoredBecauseOfDateBudgetSum)

def prettyPrintBudgets(budgets):
    # Prints csv with AreaCode, Year, Value
    print('"AreaCode","Year","Value"')
    for areaCode, yearValues in budgets.items():
        nutsValue = prefectureCodeToNUTSCodeMapping[areaCode]
        for year, value in yearValues.items():
            print('"%s","%d","%d"' % (nutsValue, year, value))


# coding = utf-8

# 导入函数库
import datetime
import random
import numpy as np
from TextCnn_inference import inference
import tensorflow as tf
from tensorflow.contrib import learn

# pycharm not need the “/souhu_algorithm_competition"
# 参数设置
#=====================================================#
# Data loading params
tf.flags.DEFINE_float('test_sample_percentage', 0.005, 'Percentage of the training data to use for testing')
tf.flags.DEFINE_string('training_data_file', './data_bunch/cnn_non_stop_words_train_bunch.dat', 'Data for training')
tf.flags.DEFINE_string('training_data_label_file', './data_bunch/cnn_train_label_bunch.dat', 'Some infomation for training,such as label, picture list and so on')

# Model Hyperparameters
tf.flags.DEFINE_integer('embedding_size', 300, "Dimensionality of character embedding (default: 128)")
tf.flags.DEFINE_string('filter_sizes', '2,3,4', "Comma-separated filter sizes (defulat: '2,3,4')")
tf.flags.DEFINE_integer('num_filters', 128, "Number of filters per filter size (default: 128)")
tf.flags.DEFINE_float('dropout_keep_prob', 0.5, "Dropout keep probality (default: 0.5)")
tf.flags.DEFINE_integer('fc1_nodes', 50, "Number of nodes in first full connection layer (default: 50) ")
# tf.flags.DEFINE_float('moving_average_decay', 0.99, "移动平均指数")
tf.flags.DEFINE_float('learning_rate_basic', 0.9, "基础学习率")
tf.flags.DEFINE_float('learning_rate_decay', 0.96, "学习率衰减速度")

# Training parameters
tf.flags.DEFINE_integer('batch_size', 500, "Batch size (default: 64)")
tf.flags.DEFINE_integer('num_epochs', 200000, "Number of training epochs (default: 2000)")
tf.flags.DEFINE_integer("evaluate_every", 100, "Evaluate model on dev set after this many steps (default: 100)")
tf.flags.DEFINE_integer("checkpoint_every", 100, "Save model after this many steps (default: 100)")
tf.flags.DEFINE_integer("num_checkpoints", 5, "Number of checkpoints to store (default: 5)")

# Misc Parameters
tf.flags.DEFINE_boolean('allow_soft_placement', True, "Allow device soft device placement")
tf.flags.DEFINE_boolean('log_device_placement', False, "Log placement of ops on devices")

FLAGS = tf.flags.FLAGS

# Load data
from instrument import read_bunch
train_data = read_bunch(FLAGS.training_data_file)
train_label = read_bunch(FLAGS.training_data_label_file)
# validate_data = read_bunch(FLAGS.validate_data_file)

# Build vocabulary
# sequence_length = max(train_data.news_length)                        # 取句子的最大长度
sequence_length = int(np.percentile(train_data.news_length,95))        #95%分位数
vocab_processor = learn.preprocessing.VocabularyProcessor(sequence_length)
train_matrix = np.array(list(vocab_processor.fit_transform(train_data.news_words_jieba)))
# Writer vocabulary to local
vocab_path = './vocabulary'
vocab_processor.save(vocab_path)
# release memeory
del train_data

# save trained model to local with a name
model_save_path = './model/'
model_name = 'TextCnn.ckpt'

# Training
#=========================================================#
def TextCnn_train(x_array,y_label, FLAGS):
    # Split train/test set
    from sklearn.model_selection import train_test_split
    train_x, test_x, train_y, test_y = train_test_split(x_array,
                                                        y_label,
                                                        test_size=FLAGS.test_sample_percentage,
                                                        random_state=33)
    # Placeholders for input, output and dropout
    x  = tf.placeholder(tf.int32, [None, train_x.shape[1]], name='input-x')
    y_ = tf.placeholder(tf.float32, [None, len(train_y[1])], name='input-y')
    DropOut_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")

    # use the inference function to compute logit
    y = inference(input_tensor= x,
                  sequence_length=train_x.shape[1],
                  embedding_size=FLAGS.embedding_size,
                  vocab_size=len(vocab_processor.vocabulary_),
                  filter_sizes=list(map(int, FLAGS.filter_sizes.split(','))),
                  num_filters=FLAGS.num_filters,
                  dropout_keep_prob= DropOut_keep_prob,
                  fc1_nodes=FLAGS.fc1_nodes,
                  num_classes=len(train_y[1]))
    global_step = tf.Variable(0, trainable=False)

    # define accuracy
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # define cost function, learning rate, moving average operation and train step
    cross_entroy = -(tf.reduce_sum(y_*tf.log(tf.clip_by_value(tf.nn.softmax(y), 1e-30, 1.0)), reduction_indices=[1]))
    loss = tf.reduce_mean(cross_entroy)
    learning_rate = tf.train.exponential_decay(FLAGS.learning_rate_basic,
                                                global_step, train_x.shape[0]/FLAGS.batch_size,
                                                FLAGS.learning_rate_decay)
    train_step = tf.train.AdadeltaOptimizer(learning_rate).minimize(loss, global_step=global_step)

    # 初始化TensorFlow持久化类
    saver = tf.train.Saver()

    session_conf = tf.ConfigProto(allow_soft_placement=FLAGS.allow_soft_placement,
                                  log_device_placement=FLAGS.log_device_placement)
    with tf.Session(config=session_conf) as sess:
        tf.global_variables_initializer().run()

        # 在训练过程中不再测试模型在验证数据上的表现，验证和测试的过程将会有一个独立的程序完成
        for epoch in range(FLAGS.num_epochs):
            # 随机从训练集中抽取 batch_size 条数据参加模型训练
            shuffle_indices = np.random.permutation(np.arange(train_x.shape[1]))
            batch_indices = random.sample(list(shuffle_indices), FLAGS.batch_size)
            batch_x, batch_y = zip(*np.array(list(zip(train_x, train_y)))[batch_indices])
            print(''.center(130, '*'))
            print(batch_x)
            print(batch_y)

            # Training
            train_feed_dict = {x: batch_x, y_: batch_y, DropOut_keep_prob: FLAGS.dropout_keep_prob}
            _, step, train_loss, train_accuracy, y_value = sess.run([train_step, global_step, loss, accuracy, y], train_feed_dict)
            time_str = datetime.datetime.now().isoformat()
            print("{}: step {}, loss {:g}, acc {:g}".format(time_str, step, train_loss, train_accuracy))
            print(y_value)

            current_step = tf.train.global_step(sess, global_step)
            if current_step % 10 == 0:   # 每 FLAGS.evaluate_every 步进行验证一次
                print("\nEvaluation:")
                # data structure is tuple
                test_feed_dict = {x: tuple(test_x), y_: tuple(test_y), DropOut_keep_prob: 1.0}
                step, valid_accuracy = sess.run([global_step, accuracy], test_feed_dict)
                time_str = datetime.datetime.now().isoformat()
                print("{}: step {}, acc {:g}".format(time_str, step, valid_accuracy))
                print("")
            if current_step % FLAGS.checkpoint_every == 0:  # 每 FLAGS.checkpoint_every 保存一次模型
                path = saver.save(sess, './model/TextCnn_model.ckpt', global_step=current_step)
                print("Saved model checkpoint to {}\n".format(path))

def main(argv=None):
    TextCnn_train(train_matrix, train_label.news_pic_label_one_hot, FLAGS=FLAGS)

if __name__ == '__main__':
    main()


#출처: '으뜸 머신러닝' kNN 모델을 통한 이미지의 노이즈 제거 구현
import matplotlib.pyplot as plt
import numpy as np
from skimage.io import imread
from skimage.transform import resize

url = 'https://github.com/dknife/ML/raw/main/data/Proj1/40/'
imgR, imgC, channel = 24, 24, 3
images = []

for i in range(40):
  file = url+ 'img{0:02d}.jpg'.format(i+1)
  img = imread(file)
  img = resize(img, (imgR, imgC, 3))
  #print(img.shape)
  images.append(img)

def plot_images(nRow, nCol, img):
  fig = plt.figure()
  fig, ax = plt.subplots(nRow, nCol, figsize = (nCol, nRow))
  for i in range(nRow):
    for j in range(nCol):
      if nRow <= 1: axis = ax[j]
      else : axis = ax[i, j]
      axis.get_xaxis().set_visible(False)
      axis.get_yaxis().set_visible(False)
      axis.imshow(img[i*nCol+j])

x = np.array(images[:30])
x_test = np.array(images[30:])
plot_images(3, 10, x)
plot_images(1, 10, x_test)

x_noise = x+ np.random.randn(len(x), imgR, imgC, channel)*0.1
x_noise = np.clip(x_noise, 0, 1)
x_test_noise = x_test+ np.random.randn(len(x_test), imgR, imgC, channel)*0.1
x_test_noise = np.clip(x_test_noise, 0, 1)
plot_images(3, 10, x_noise)
plot_images(1, 10, x_test_noise)

#input -> (0,1) , output -> (0, 255)
x_noise_flat = x_noise.reshape(-1, imgR*imgC*channel)
x_flat = np.array(x.reshape(-1, imgR*imgC*channel)*255, dtype = np.uint)
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(x_noise_flat, x_flat)
denoised_result = knn.predict(x_noise_flat)
denoised_result = denoised_result.reshape(-1, imgR, imgC, channel)
plot_images(3, 10, denoised_result) #images' each pixels are classified by knn model. Outputs are the labels(pixels' classes) of the noised images.

##New images are added by simply creating more noised images from the same 40 images. (for training and testing)
n_augmentation = 100 #add x100 more
x_noise_aug = x+np.random.rand(len(x), imgR, imgC, channel)*0.2
y_label = np.array(x*255, dtype = np.uint) #maybe images can't be perfectly retrieved (without noises) because dtype = np.unit forces some float vals to same int vals (classification inprecise)
y = y_label

print(x_noise.shape)
for i in range(n_augmentation):
  noisy_data = x+np.random.randn(len(x), imgR, imgC, channel) * 0.2
  x_noise_aug = np.append(x_noise_aug, noisy_data, axis = 0)
  y = np.append(y, y_label, axis = 0)

x_noise_aug = np.clip(x_noise_aug, 0, 1)
print(x_noise_aug.shape, y.shape)

plot_images(1, 10, x_noise_aug[0:300:30]) #index 0~300 by step 30 (10 elements sliced)
x_noise_aug_flattened = x_noise_aug.reshape(-1, imgR*imgC*channel)
y_flattened = y.reshape(-1, imgR*imgC*channel)
knn.fit(x_noise_aug_flattened, y_flattened)
denoised_result = knn.predict(x_noise_flat) #test the model with x_noise_flat created before (created when no image was augmented)
denoised_result = denoised_result.reshape(-1, imgR, imgC, channel)
plot_images(3, 10, x_noise)
plot_images(3, 10, denoised_result)

rndidx = np.random.randint(0, 20)
data = x[rndidx:rndidx+10] + np.random.randn(10, imgR, imgC, channel)*.4
data = np.clip(data, 0, 1)
data_flat = data.reshape(-1, imgR*imgC*channel)
denoised = knn.predict(data_flat)
denoised = denoised.reshape(-1, imgR, imgC, channel)
denoised = np.clip(denoised, 0, 255)

plot_images(1, 10, data)
plot_images(1, 10, denoised)

denoised = knn.predict(x_test_noise.reshape(-1, imgR*imgC*channel))
denoised = denoised.reshape(-1, imgR, imgC, channel)

plot_images(1, 10, x_test_noise)
plot_images(1, 10, denoised)
#overfitting problem -> because new labels were generated just by copying 30 training data labels over and over (x100 times)
#way to solve this problem is to augment new labels by actually modifying labels(rotation, shifting...)

from keras.preprocessing.image import ImageDataGenerator
image_generator = ImageDataGenerator(
    rotation_range = 360,
    zoom_range = 0.1,
    shear_range = 0.1,
    width_shift_range = 0.1,
    height_shift_range = 0.1,
    horizontal_flip = True,
    vertical_flip = True
) #keras class iterator for generating newly augmented images

y_aug = x.reshape(-1, imgR, imgC, channel)
print(y_aug.shape)
it = image_generator.flow(y_aug) #iterator for ImageDataGenerator() class object
nData = y_aug.shape[0]

x_aug = y_aug +np.random.randn(nData, imgR, imgC, channel)*0.1
n_augmentation = 500 #augmentation by x500
for _ in range(n_augmentation):
  new_y = it.next() #iterator usage
  new_x = new_y + np.random.randn(nData, imgR, imgC, channel)*0.1
  y_aug = np.append(y_aug, new_y, axis = 0)
  x_aug = np.append(x_aug, new_x, axis = 0)#append newly generated y,x row-wise

y_aug = np.array(y_aug*255, dtype = np.uint)
y_aug = y_aug.reshape(-1, imgR, imgC, channel)
x_aug = x_aug.reshape(-1, imgR, imgC, channel)
y_aug = np.clip(y_aug, 0, 255)
x_aug = np.clip(x_aug, 0, 1)
plot_images(3, 10, y_aug[30:])

#Now, train the newly generated images with KNN model
x_aug_flat = x_aug.reshape(-1, imgR*imgC*channel)
y_aug_flat = y_aug.reshape(-1, imgR*imgC*channel)
knn.fit(x_aug_flat, y_aug_flat)
denoised = knn.predict(x_test_noise.reshape(-1, imgR*imgC*channel))
denoised = denoised.reshape(-1, imgR, imgC, channel)
plot_images(1, 10, x_test_noise)
plot_images(1, 10, denoised)

images = it.next() #load 30 images generated by ImageDataGenerator()'s iterator method
testX = images + np.random.randn(nData, imgR, imgC, channel)* 0.4
testX = np.clip(testX, 0, 1)
denoised = knn.predict(testX.reshape(-1, imgR*imgC*channel))
denoised = denoised.reshape(-1, imgR, imgC, channel)

plot_images(1, 10, testX)
plot_images(1, 10, denoised)


from typing import TYPE_CHECKING

from paul_plans.defensive_building import DefensiveBuilding, DefensePosition
from sc2.ids.unit_typeid import UnitTypeId
from sharpy.plans import BuildOrder, Step, StepBuildGas
from sharpy.plans.acts import ActBuilding
from sharpy.plans.acts.zerg import ZergUnit, AutoOverLord
from sharpy.plans.require import UnitExists, RequiredAll
from sharpy.plans.tactics import PlanDistributeWorkers

if TYPE_CHECKING:
    from sharpy.knowledges import Knowledge

class RoachRushResponse(BuildOrder):
    """Respond to a Roach Rush."""

    def __init__(self):
        """Create plan for build order."""
        self.drones = ZergUnit(UnitTypeId.DRONE, to_count=0)
        self.lings = ZergUnit(UnitTypeId.ZERGLING, to_count=999)
        self.queens = ZergUnit(UnitTypeId.QUEEN, to_count=3)
        self.roaches = ZergUnit(UnitTypeId.ROACH, to_count=100, priority=True)
        self.ravagers = ZergUnit(UnitTypeId.RAVAGER, to_count=0)
        self.defense_spines = DefensiveBuilding(
            unit_type=UnitTypeId.SPINECRAWLER, position_type=DefensePosition.Entrance, to_base_index=1, to_count=3
        )
        self.gas = StepBuildGas(to_count=3)

        unit_building = BuildOrder(
            [
                Step(None, self.drones, skip_until=self.should_build_drones),
                Step(UnitExists(UnitTypeId.SPAWNINGPOOL), self.defense_spines),
                Step(
                    RequiredAll([UnitExists(UnitTypeId.ROACHWARREN), UnitExists(UnitTypeId.ROACH)]),
                    self.ravagers,
                    skip_until=self.should_build_ravagers,
                ),
                Step(UnitExists(UnitTypeId.ROACHWARREN), self.roaches),
                Step(
                    RequiredAll(
                        [
                            UnitExists(UnitTypeId.SPAWNINGPOOL),
                            UnitExists(
                                UnitTypeId.ROACHWARREN,
                                include_pending=True,
                                include_not_ready=True,
                                include_killed=True,
                            ),
                        ]
                    ),
                    self.lings,
                ),
                Step(UnitExists(UnitTypeId.SPAWNINGPOOL), self.queens),
                Step(UnitExists(UnitTypeId.SPAWNINGPOOL), self.lings),
            ]
        )

        buildings: BuildOrder = BuildOrder(
            [
                Step(None, ActBuilding(UnitTypeId.SPAWNINGPOOL, to_count=1)),
                Step(UnitExists(UnitTypeId.SPAWNINGPOOL), ActBuilding(UnitTypeId.ROACHWARREN, to_count=1)),
                Step(None, self.gas, skip_until=self.should_build_gas),
            ]
        )

        super().__init__(buildings, unit_building)

    def should_build_drones(self, knowledge):
        """Determine if we need to build drones."""
        if self.cache.own(UnitTypeId.DRONE).amount >= 16 + self.get_count(UnitTypeId.EXTRACTOR) * 3:
            return False
        if not self.knowledge.game_analyzer.our_power.is_enough_for(
            self.knowledge.enemy_units_manager.enemy_total_power
        ):
            return False
        self.drones.to_count = 16 + self.get_count(UnitTypeId.EXTRACTOR) * 3
        return True

    def should_build_gas(self, knowledge):
        if (
            self.knowledge.game_analyzer.our_power.is_enough_for(self.knowledge.enemy_units_manager.enemy_total_power)
            and self.cache.own(UnitTypeId.DRONE).amount >= 16 * self.get_count(UnitTypeId.EXTRACTOR) * 3
        ):
            self.gas.to_count = min(3, self.get_count(UnitTypeId.EXTRACTOR) + 1)
            return True
        return False

    def should_build_ravagers(self, knowledge):
        if self.cache.own(UnitTypeId.ROACH).amount >= 10:
            self.ravagers.to_count = self.cache.own(UnitTypeId.ROACH).amount // 2
            return True
        return False


from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PainterWidget import PainterWidget
import numpy as np
import time
import requests
import json

server_addr = "the_addr/predict/"

class PredictionProcessor(QObject):
    predicted = pyqtSignal(list)
    finished = pyqtSignal()

    @pyqtSlot()
    def run(self, array:np.ndarray):
        try:
            req_dict = {"array":array.tolist()}
            req = requests.post(server_addr, data=json.dumps(req_dict))
            response = req.json()

            if response["Error"]:
                ret = [False, response["Message"]]
            else:
                ret = [True, response["Prediction"]]
        except:
            ret = [False, "Error occurred!"]

        self.predicted.emit(ret)
        self.finished.emit()

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setWindowTitle("Number Recognizer")
        self.setFixedSize(400, 460)

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        self.vertical_layout = QVBoxLayout()
        self.vertical_layout.setAlignment(Qt.AlignHCenter)
        self.central_widget.setLayout(self.vertical_layout)

        self.painter_widget = PainterWidget()
        self.vertical_layout.addWidget(self.painter_widget)

        self.label = QLabel()
        self.label.setText("Type any number above")
        self.label.setStyleSheet(f"font-size: 20px;padding:15px")
        self.vertical_layout.addWidget(self.label)
        self.label.setAlignment(Qt.AlignBottom)

        self.btns_layout = QHBoxLayout()
        self.vertical_layout.addLayout(self.btns_layout)

        self.predict_btn = QPushButton()
        self.predict_btn.setFixedSize(120,50)
        self.predict_btn.setText("Predict")
        self.predict_btn.setStyleSheet(f"font-size: 16px")
        self.btns_layout.addWidget(self.predict_btn)

        self.clear_btn = QPushButton()
        self.clear_btn.setFixedSize(120,50)
        self.clear_btn.setText("Clear")
        self.clear_btn.setStyleSheet(f"font-size: 16px")
        self.btns_layout.addWidget(self.clear_btn)

        self.predict_btn.clicked.connect(self.recognizer_clicked)
        self.clear_btn.clicked.connect(self.clear_clicked)

    def recognizer_clicked(self):
        self.predict_btn.setEnabled(False)
        self.label.setText("Waiting ...")
        self.label.repaint()
        get_req = self.painter_widget.get_img()

        if not get_req[0]:
            self.label.setText(get_req[1])
            self.label.repaint()
            self.predict_btn.setEnabled(True)
        else:
            img_array = get_req[1]

            self.predict_obj = PredictionProcessor()
            self.prediction_thread = QThread()

            self.predict_obj.predicted.connect(self.image_predicted)
            self.predict_obj.moveToThread(self.prediction_thread)
            self.predict_obj.finished.connect(self.prediction_thread.quit)
            self.prediction_thread.started.connect(lambda: self.predict_obj.run(img_array))
            self.prediction_thread.start()

    def image_predicted(self, ret):
        if not ret[0]:
            text = ret[1]
        else:
            text = f"The number is {ret[1]}"

        self.label.setText(text)
        self.label.repaint()
        self.predict_btn.setEnabled(True)

    def clear_clicked(self):
        self.painter_widget.clear_board()

if __name__ == '__main__':
    import sys
    app = QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())


# encoding:utf-8

import json
import os
import re
import time
import numpy as np

# 配置文件路径
eid_path = '../../Twitter/json/'						# 源文件路径
label_path_read = '../../Twitter/'					# 标签路径
label_path_write = '../label/'
event_path = '../event_text/'					    # 分词处理后的保存路径
time_path = '../event_time/'
index_path = '../event_index/'
word2vec_path_read = '../../../word2vector/'			# 词向量读取路径
word2vec_path_write = '../word2vec/'
embedding_size = 300

def get_file_name(file_path):						# 获取file_path路径下的文件名
	for root, dirs, files in os.walk(file_path):
		return files

def get_time(date):
	dt_time = time.strptime(date, '%a %b %d %H:%M:%S %z %Y')
	dt_sec = time.mktime(dt_time)
	return dt_sec

def fenci(eid_path, label_path_read, label_path_write, event_path):
	char = '[+\.\!\/_,-?\[\]@*&""\'‘’”“$%„{}^()#]'	# 要过滤符号列表

	labels = {}

	fl = open(label_path_write + 'labels.json', 'w')		# 输出标签文件
	# fl = open(label_path + 'labels.txt', 'w')
	with open(label_path_read + 'label.txt', 'r') as lf:
		lines = lf.readlines()
		for line in lines:
			m = line.split()						# m[:2]为每个子事件的'eid'和'label'
			m = m[:2]
			eid = m[0].split(':')[1]
			label = m[1].split(':')[1]
			if int(label) == 0:						# 两类问题的第一类：否
				labels[eid] = [1, 0]
			else:
				labels[eid] = [0, 1]				# 两类问题的第一类：是
			# fl.write(eid + ' ' + label + '\n')	# 输出格式'eid label'
	json.dump(labels, fl)
	fl.close()

	list_eid = get_file_name(eid_path)				# 文件名列表
	for eid in list_eid:
		fe = open(event_path + eid + '.txt', 'w', encoding='utf8')		# 将每个event的分词结果输出
		ft = open(time_path + eid + '.txt', 'w')

		with open(eid_path + eid, 'r') as f:
			data = json.load(f)						# data的类型为 'list'
			n = len(data)
			t0 = get_time(data[0]['created_at'])

			for item in data:

				t = get_time(item['created_at'])
				ft.write(str(int(t-t0)))

				text = re.sub(char, '', item['text']).split()			# 过滤'text'里的符号并分词
				for s in text:
					if s[0:4] == 'http':			# 过滤掉'http'开头的网址
						text.remove(s)
				for token in text:
					fe.write(token + ' ')

				n -= 1
				if n > 0:
					fe.write('\n')
					ft.write('\n')
		fe.close()
		ft.close()

def build_embeddings(word2vec_path_read, word2vec_path_write):
	words_embeddings = {}
	with open(word2vec_path_read + 'Google_IJCAI2016_w2v.txt', 'r') as files:
		lines = files.readlines()
		for line in lines:
			l = line.split()
			word = l[0]
			value = l[1:]
			words_embeddings[word] = value

	with open(word2vec_path_write + 'Google_IJCAI2016_w2v.json', 'w') as fj:
		json.dump(words_embeddings, fj)

	print('word-vector build succeed...')

# 数据预处理，返回处理后的输入数据
def get_embeddings(time_path, index_path, embedding_size):
	with open(word2vec_path_write + 'Google_IJCAI2016_w2v.json', 'r') as fj:
		vector = json.load(fj)

	files = get_file_name(event_path)                   # 获取当前目录下的所有文件名和文件数
	num_event = len(files)

	for i in range(num_event):
		eid = files[i].split('.')[0]
		ft = open(time_path + eid + '.txt', 'r')
		time_lines = ft.readlines()
		with open(event_path + files[i], 'r', encoding='utf8') as event:
			lines = event.readlines()
			length = len(lines)                                 # 计算每个事件的评论数
			vec = np.zeros((length, embedding_size+1), dtype=np.float32)     # 初始化事件矩阵

			for j in range(length):
				vec[j, 0] = int(time_lines[j])
				sents = lines[j].split()
				for word in sents:
					if vector.__contains__(word):
						vec[j, 1: ] = vec[j, 1: ] + np.array(vector[word], dtype=np.float32)     # 对每条评论的各词向量求均值
				if len(sents) == 0:
					vec[j, 1: ] = vec[j, 1: ]
				else:
					vec[j, 1: ] = vec[j, 1: ] / len(sents)
			ft.close()
			with open(index_path + files[i], 'w', encoding='utf8') as fi:
				for k in range(length):
					for n in range(embedding_size+1):
						fi.write(str(vec[k, n]))
						if n < embedding_size:
							fi.write(' ')
					if k < length-1:
						fi.write('\n')

if __name__ == '__main__':
	fenci(eid_path, label_path_read, label_path_write, event_path)
	build_embeddings(word2vec_path_read, word2vec_path_write)
	get_embeddings(time_path, index_path, embedding_size)


from keras.models import Sequential
from keras.layers import Dense
import keras
import numpy
import matplotlib.pyplot as plt
import math

def list_generator(num_points=100):
    X_ = numpy.arange(-1.0,1.0, 2/num_points)
    y_ = 1 + numpy.sin(((3*math.pi)/2)*X_)
    return X_, y_

def baseline_model():
    # create model
    model = Sequential()
    adam = keras.optimizers.Adam(lr=0.003)

    # Use Activation function tanh
    model.add(Dense(7, input_shape=(1,), activation='tanh'))

    # Use Activation function linear
    model.add(Dense(1, activation='linear'))

    # Compile model
    model.compile(loss='mse', optimizer=adam, metrics=['mae'])
    return model

# Generating list
X, y = list_generator()

# Define the model
model = baseline_model()

print(model.summary())

model.fit(X, y, epochs = 10000, batch_size = 128, verbose = 2)

y_pred = model.predict(X)
diff = numpy.empty(0)
for i in range(0, 100):
    diff = numpy.append(diff, numpy.abs(y[i] - y_pred[i]))
print("Maximum Difference: ",numpy.max(diff))
plt.plot(X, y)
plt.plot(X, y_pred)
plt.show()


import sys, os
from pprint import pprint as print
from datetime import date
sys.path.insert(0, os.path.expanduser('~/Galaxy_Box_Integration/'))
from galaxy_api_class import gx_api

class controller:
    def __init__(self):
        self.gx = gx_api(production = False)

    def get_assignables(self):
        # get all assignable images (unsorted)
        today = date.today().strftime("%m/%d/%Y")
        end_date = today.split('/')
        end_date = str(int(end_date[0])+3)+"/"+end_date[1]+'/'+end_date[2]
        launch_date_range = f'{today}...{end_date}'
        params = {
            'query':[{
            'c_WA_Launch_Date_Earliest':launch_date_range,
            'RetouchStatus':'Ready for Revisions',
            'RetoucherName':'=',
            'omit': "false"
            }]
        }
        results = self.gx.find_records(params, layout = "Retoucher_DetailView")
        # abort if no data in the response
        if 'response' not in results or 'data' not in results['response']: return -1
        self.assignables = results['response']['data']

        # later, we'll determine how to assign them

traffic = controller()
traffic.get_assignables()


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

import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

def save_result_img(args, in_list, name):
        plt.figure()
        plt.plot(range(len(in_list)), in_list)
        plt.ylabel(name)
        plt.savefig('./save/fed_{}_{}_{}_C{}_iid{}_{}.png'.format(args.dataset, args.model, args.epochs, args.frac, args.iid, name))

def test_img(net_g, datatest, args):
    net_g.eval()
    # testing
    test_loss = 0
    correct = 0

    correct_list = []
    loss_list = []

    data_loader = DataLoader(datatest, batch_size=args.bs)
    l = len(data_loader)
    for idx, (data, target) in enumerate(data_loader):
        if args.gpu != -1:
            data, target = data.cuda(), target.cuda()
        log_probs = net_g(data)
        # sum up batch loss
        test_loss += F.cross_entropy(log_probs, target, reduction='sum').item()
        # get the index of the max log-probability
        y_pred = log_probs.data.max(1, keepdim=True)[1]
        correct += y_pred.eq(target.data.view_as(y_pred)).long().cpu().sum()

        # store correct list for report
        correct_list.append(100.00 * correct / len(data_loader.dataset))
        # loss_list.append(test_loss)

    test_loss /= len(data_loader.dataset)
    accuracy = 100.00 * correct / len(data_loader.dataset)
    # save_result_img(args, correct_list, name="test accurcy")
    if args.verbose:
        print('\nTest set: Average loss: {:.4f} \nAccuracy: {}/{} ({:.2f}%)\n'.format(
            test_loss, correct, len(data_loader.dataset), accuracy))
    return accuracy, test_loss

def state_dict_to_xferable( state_dict, lr=None, ep=None ):
    import json
    xfer_dict = {}
    if lr is not None:
        xfer_dict['lr'] = lr
    if ep is not None:
        xfer_dict['ep'] = ep
    for k in state_dict:
        xfer_dict[k] = state_dict[k].cpu().tolist()
    xfer_string = json.dumps(xfer_dict)
    return xfer_string

def xferable_to_state_dict(xfer_string):
    import json
    import collections
    json_decode_dict = json.loads(xfer_string)
    recovered_dict = collections.OrderedDict()
    for k in json_decode_dict:
        if k != 'lr' and k != 'ep':
            recovered_dict[k] = torch.Tensor(json_decode_dict[k])
    print("recovered_dict: ", type(recovered_dict))
    return recovered_dict, json_decode_dict['lr'], json_decode_dict['ep']


# -*- coding: utf-8 -*-
"""
Created on Tue Jan  8 10:18:42 2019

@author: Administrator
"""
# Begin: Python 2/3 compatibility header small
# Get Python 3 functionality:
from __future__ import\
    absolute_import, print_function, division, unicode_literals
# End: Python 2/3 compatability header small

import tensorflow as tf
import argparse
import imp
import os
import pickle
from MyDataDealer import CrossData,Data

AbaloneData = imp.load_source('Abalone','../Data.py')

parser = argparse.ArgumentParser(description="show some parse for the progess")
parser.add_argument("--DataDir",type=str,default="../uci_data_set/Ecoli.mat",help="the mat data file path")
parser.add_argument("--train_epoch",type=int,default=50,help="number of training epochs")
parser.add_argument("--model_save_name",type=str,default="Abalone.ckpt",help="the file name for model to save")
parser.add_argument("--model_save_dir",type=str,default=None,help="the dir for model to save")
parser.add_argument("--lr", type=float, default=0.001, help="initial learning rate for adam")
parser.add_argument("--batch_size", type=int, default=100, help="number of images in batch")
parser.add_argument("--use_gpu", default=1, type=int, choices=[0,1], help="Whether to use GPU")
parser.add_argument("--nb_item",default=0,choices=[0,1,2,3,4], help="crosssplit data ,it select which group to use")
parser.add_argument("--reload_data", default=True, help="Whether to reload data")
parser.add_argument("-cpdr","--create_partial_data_rate", type=float,default=0.5, help="how much date transpose to partial data ,the value is (0,1)")
parser.add_argument("-npt","--nb_partial_target", type=int, default=1, choices=[1,2,3], help="number of partial target without count the true target")
parse = parser.parse_args()

if parse.use_gpu:
    os.environ["CUDA_VISIBLE_DEVICES"] = "1"
else:
    os.environ["CUDA_VISIBLE_DEVICES"] = ""

#Abalone_data = AbaloneData.UCIData(parse.DataDir)
#X,Y,partial_Y = Abalone_data.createPartialData()
#X = Data.guiYiHua(Abalone_data.data)
#item_data = CrossData(X)
#item_data.cross_split()
#
#l=[]
#for i in range(parse.train_epoch):
#    ind = item_data.next_batch(parse.batch_size,parse.nb_item)
#    print(type(ind),len(ind))
#    l.append(ind)

#createVar = globals()
#for i in range(5):
#    createVar["g"+str(i)] = tf.Graph()
#    print(type(createVar["g"+str(i)]))
#    with exec("g{}.as_default()".format(i)):
#        with tf.variable_scope("Graph{}".format(i)):
#            createVar["xs_g"+str(i)] = tf.placeholder(tf.float32, [None, 7])
#            createVar["ys_g"+str(i)] = tf.placeholder(tf.float32, [None, 29])
#if parse.use_gpu:
#    print(parse.use_gpu)
print(parse.nb_partial_target)


from os import path
import sys, json, time
import torch
import numpy as np
from model import Net
from dataset import NodeDataset

# Get Directory
BASE_DIR = path.dirname(path.dirname(path.abspath(__file__)))

# Read input data
print('Reading Weights into network')

# Model Build output
model_path=path.join(BASE_DIR, 'data/model_build_outputs/mlp_state_dict.pt')

net = Net()
net.load_state_dict(torch.load(model_path))

print('Reading new dataset')
test_routes_path = path.join(BASE_DIR, 'data/model_apply_inputs/new_route_data.json')
test_travel_path = path.join(BASE_DIR, 'data/model_apply_inputs/new_travel_times.json')
test_pacakge_path = path.join(BASE_DIR, 'data/model_apply_inputs/new_package_data.json')

dataset = NodeDataset(test_pacakge_path, test_travel_path, test_routes_path, train=False)

routes = {route_id: {'proposed': {}} for route_id in data_set.route_ids}

def route_sequencer(start_node, travel_time, start_time, end_time, volume, service):
    #Parameters
    available_nodes = set(range(travel_time.shape[0]))
    visited_nodes = set([])
    sequence = []
    visited_nodes.add(start_node)
    sequence.append(start_node)
    current = start_node

    #Initial state
    d = distance[start_node, :].view(-1,1)

    x = torch.cat([d, service, volume, start_time, end_time], dim=1)

    for i in range(travel_time.shape[0]):
        if len(visited_nodes) == travel_time.shape[0]:
            break

        idx = list(available_nodes-visited_nodes)
        evals = net(x[idx,:]).view(1,-1)
        selected_node = idx[torch.argmax(evals)]

        visited_nodes.add(selected_node)
        sequence.append(selected_node)
        current = selected_node

        #Create new x
        d = distance[current, :].view(-1,1)
        volume[selected_node] = 0
        service[selected_node] = 0
        x = torch.cat([d, service, volume, start_time, end_time], dim=1)

    return sequence

for route_id in tqdm(data_set.route_ids, total=len(data_set.route_ids)):
    keys = np.array(list(data_set.routes[route_id]['stops'].keys()))
    distance = data_set.distance_matrix(route_id)
    ser, vol, st, et = data_set.parse_packages(route_id)
    start_node = data_set.get_start_node(route_id)
    ser, vol, st, et = ser.view(-1,1), vol.view(-1,1), st.view(-1,1), et.view(-1,1)

    sequence = route_sequencer(start_node, distance, st, et, vol, ser)

    for i, stop in enumerate(keys[sequence]):
        routes[route_id]['proposed'][stop] = i

# Write output data
output_path = path.join(BASE_DIR, 'data/model_apply_outputs/proposed_sequences.json')

with open(output_path, 'w') as out_file:
    json.dump(routes, out_file)
    print("Success: The '{}' file has been saved".format(output_path))

print('Done!')


import bluetooth
import Tkinter as tki
import threading

class TS:
    def __init__(self):
        """Creating the initial bluetooth connection and defining the char protocol"""
        print("Checking connection...")
        self.serverMACAddress = '20:15:10:12:39:01'
        self.port = 1
        self.s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
        self.s.connect((self.serverMACAddress, self.port))
        self.isConnected = True
        self.CW = ["A","B","C","D"]
        self.CCW = ["Z","Y","X","W"]
        self.speed = 0#Default at 25%
        #Maps speeds 0 to 3, 0 being 25%, 3 being 100%
        self.threads =[]

    def reconnect(self):
        """Handles the reconnection to the bluetooth module"""
        try:
            self.s.close()
        except Exception as e:
            print("Failed to close socket")
            print(e)
        self.s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
        self.s.connect((self.serverMACAddress, self.port))

    def _deco(func):
        """Decorator function that handles errors"""
        def blah(self,*arg):
            try:
                func(self,*arg)
            except IOError:
                print("Bluetooth Error, attempting reconnection")
                self.isConnected = False
                try:
                    self.reconnect()
                    self.isConnected = True
                    self.threads = []
                except IOError:
                    print("Reconnection failed, please get in range of the device")

        return blah

    @_deco
    def handshake(self):
        """Handshaking between the arduino to check the connection is valid"""
        print("No: "+str(len(self.threads)))
        indexes_to_del = []
        if len(self.threads)>2:
                    raise IOError
        for i in range(0,len(self.threads)):
            if not self.threads[i].is_alive():
                indexes_to_del.append(i)

        for i in indexes_to_del:#do this otherwise if deleted above, out of index error occurs
            del self.threads[i]

        while True:
            data =  self.s.recv(1024)
            if data =="O":
                print("Hanshake Received")
                return

    def closeConnection(self):
        """Closes the connection and kills the program"""
        print("closing connection...")
        self.s.close()
        quit()

    def change_Focus(self, rate):
        """Changes the speed of the focuser"""
        self.speed = int(rate)
        print("Setting Focus Rate to: " + str(rate))

    @_deco
    def focus_Halt(self,e):
        """sends the H command to the arduino, halting the focuser, additionally has handshake verification"""
        if self.isConnected:
            a = threading.Thread(target=self.handshake)
            self.threads.append(a)
            a.start()
        print("\nHalting focuser\n")
        self.s.send("H")

    @_deco
    def focus_Left(self,e):
        print("sending "+self.CCW[self.speed])
        self.s.send(self.CCW[self.speed])

    @_deco
    def focus_Right(self,e):
        print("sending "+self.CW[self.speed])
        self.s.send(self.CW[self.speed])

class App(TS):

    WINDOW_TITLE = "Telescope Control Software"
    WINDOW_WIDTH = 300
    WINDOW_HEIGHT = 300
    SLEW_OPTIONS = ["0","1","2","3"]

    def __init__(self,root_win,ts):
        """Setting up the frames and windows"""
        self.ts = ts

        root_win.title(self.WINDOW_TITLE)
        root_win.minsize(self.WINDOW_WIDTH,self.WINDOW_HEIGHT)
        #root_win.protocol("WM_DELETE_WINDOW", self.ts.closeConnection)

        self.control = tki.Frame(root_win)
        self.controlFrame()
        self.control.config(height=self.WINDOW_HEIGHT,width=600,borderwidth=2,relief="groove")
        self.control.grid(row=0,column =1,sticky="N")

        self.checkConnection()

    def controlFrame(self):
        """Widgets for Telescope Control Frame"""

        #Focus Buttons
        focusVar = tki.StringVar(self.control)
        slewRevOpt = list(self.SLEW_OPTIONS)

        focus_OptionMenu = tki.OptionMenu(self.control,focusVar,*self.SLEW_OPTIONS,command= lambda x: self.ts.change_Focus(slewRevOpt.index(focusVar.get())))
        self.left_button = tki.Button(self.control,text="<")
        self.right_button = tki.Button(self.control, text=">")

        self.left_button.bind('<ButtonPress-1>',self.ts.focus_Left)
        self.left_button.bind('<ButtonRelease-1>',self.ts.focus_Halt)
        self.right_button.bind('<ButtonPress-1>',self.ts.focus_Right)
        self.right_button.bind('<ButtonRelease-1>',self.ts.focus_Halt)

        #Labels and placement
        tki.Label(self.control,text="Telescope Control").grid(column=0, row=0,columnspan=4,ipadx=50)
        tki.Label(self.control, text="Focus Control").grid(column=0,columnspan=4,row=4)
        tki.Label(self.control,text="Focus Speed").grid(column=0,columnspan=2,row=5)

        focus_OptionMenu.grid(column=2,columnspan=2,row=5)

        self.left_button.grid(column=0,columnspan=2,row=6)
        self.right_button.grid(column=2,columnspan=2,row=6)

    def checkConnection(self):
        """Handle GUI depending on connection status"""
        if self.ts.isConnected:
            self.left_button.config(state = 'normal')
            self.right_button.config(state = "normal")
        else:
            self.left_button.config(state = 'disabled')
            self.right_button.config(state = 'disabled')

        self.left_button.update()
        self.right_button.update()

        root.after(100,self.checkConnection)

if __name__ =="__main__":
    root = tki.Tk()
    app = App(root,TS())
    root.mainloop()
#note checkConnection will re enable the buttons when the reconnect function deletes all the threads, need to deal with the reconnect function thread handling and reconnection.


import cv2
import requests
from capture_aruco import detectar_aruco

# Abre la cámara (usualmente la cámara predeterminada, 0)
cap = cv2.VideoCapture(0)

green = (0, 255, 0)
red = (0, 0, 255)
blue = (255, 0, 0)

while True:
    # Captura el fotograma
    ret, frame = cap.read()

    # Muestra el fotograma en una ventana llamada 'Frame'
    # cv2.imshow('Frame', frame)
    image_with_aruco_marker, corners, ids = detectar_aruco(frame.copy())

    for id in ids:
        if id >= 3:
            index = ids.index(id)
            esquina_superior_izquierda = corners[index][0][0]
            esquina_inferior_derecha = corners[index][0][2]
            centro_x = int((esquina_superior_izquierda[0] + esquina_inferior_derecha[0]) // 2)
            centro_y = int((esquina_superior_izquierda[1] + esquina_inferior_derecha[1]) // 2)

            # Especifica la URL a la que deseas hacer la solicitud GET
            url = f'http://localhost:8500/api/slots/{id}/'

            # Realiza la solicitud GET
            response = requests.get(url)

            # Verifica si la solicitud fue exitosa (código de estado 200)
            if response.status_code == 200:
                # Imprime el contenido de la respuesta
                data = response.json()
                cv2.putText(image_with_aruco_marker, data["name"], (centro_x,centro_y), cv2.FONT_HERSHEY_SIMPLEX, 2, red, 3)

    cv2.imshow("Video con aruco", image_with_aruco_marker)

    # Rompe el bucle si se presiona la tecla 'q'
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Libera la cámara y cierra la ventana
cap.release()
cv2.destroyAllWindows()


from rx import create, operators
from rx.core import Observer
import requests
#7ad3d9b4

class Printer(Observer):
    def on_next(self, content):
        print(content)
    def on_completed(self):
        print('Done')

def observer_teclado(observer, scheduler):
    while 1:
        msg = input('\nIntroduce la pelicula a buscar:\t')
        if msg:
            print('Buscando...')
            params = { 'apikey': '7ad3d9b4', 's': msg}
            content = requests.get(f'https://www.omdbapi.com/', params)
            for movie in content.json().get('Search') or []:
                observer.on_next(movie)
        else:
            observer.on_completed()
            return

observable = create(observer_teclado)

cadena = observable.pipe(
    operators.filter(lambda v: v["Type"] == 'movie'),
    operators.map(lambda v: "(" + v["imdbID"] + ")" + " - " + v["Title"] + ": " + v["Poster"] + " (" + v["Year"] + ")")
)

cadena.subscribe(Printer())


# encoding = utf-8

from future import standard_library
standard_library.install_aliases()
from builtins import str
import os
import sys
import time
import datetime
import json
from urllib.parse import urlencode
from urllib.request import urlopen
from urllib.request import Request
from urllib2 import urlparse
from urllib.request import build_opener
from urllib.request import ProxyHandler
from urllib.request import install_opener

'''
    IMPORTANT
    Edit only the validate_input and collect_events functions.
    Do not edit any other part in this file.
    This file is generated only once when creating the modular input.
'''
'''
# For advanced users, if you want to create single instance mod input, uncomment this method.
def use_single_instance_mode():
    return True
'''

# Copyright 2019 Jorrit Folmer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

def is_proxy(helper):
    """ Determine whether or not a proxy is configured """
    try:
        proxy = helper.get_proxy()
        proxies = {}
        if proxy.get('proxy_url', False):
            return True
        else:
            return False
    except Exception as e:
        return False

def get_proxies(helper):
    """ Return a proxy dict from proxy settings in addon """
    proxy = helper.get_proxy()
    proxies = {}
    if proxy.get('proxy_url', False):
        if(proxy["proxy_username"] and proxy["proxy_password"]):
            proxy_url = "%s://%s:%s@%s:%s" % (proxy["proxy_type"], proxy["proxy_username"], proxy["proxy_password"], proxy["proxy_url"], proxy["proxy_port"])
            proxies = {
                "http" : proxy_url,
                "https" : proxy_url
            }
        else:
            proxy_url = "%s://%s:%s" % (proxy["proxy_type"], proxy["proxy_url"], proxy["proxy_port"])
            proxies = {
                "http" : proxy_url,
                "https" : proxy_url
            }
    helper.log_debug("proxies dict is : {}".format(proxies))
    return proxies

def get_defender_connectivity(helper, api_endpoint):
    """ Check basic connectivity after saving new inputs in the addon """
    if is_proxy(helper):
        install_opener(build_opener(ProxyHandler(get_proxies(helper))))
    # Do request
    try:
        url = "https://login.windows.net/"
        req = Request(url)
        response = urlopen(req)
    except Exception as e:
        raise Exception("Error connecting to %s: %s" % (url, str(e)))
        exit(1)
    else:
        helper.log_info("Succesfully connected to %s" % url)

def get_defender_access_token(helper, client_id, secret, tenant_id):
    """ Returns an oauth2 access token string from login.windows.net
        given a client_id, secret and tenant_id """

    body = {
        'resource' : 'https://api.securitycenter.windows.com',
        'client_id' : client_id,
        'client_secret' : secret,
        'grant_type' : 'client_credentials'
    }

    # Prepare request
    try:
        url = "https://login.windows.net/%s/oauth2/token" % (tenant_id)
        data = urlencode(body).encode("utf-8")
    except Exception as e:
        raise Exception("Error encoding token request body for client_id %s: %s" % (client_id, str(e)))
        exit(1)
    else:
        helper.log_debug("Succesfully parsed token request prep for %s" % url)

    # Do request
    if is_proxy(helper):
        try:
            proxies = get_proxies(helper)
            install_opener(build_opener(ProxyHandler(proxies)))
        except Exception as e:
            raise Exception("Error setting proxy %s: %s" % (get_proxies(helper), str(e)))
            exit(1)
        else:
            helper.log_debug("Succesfully installed proxies %s" % proxies)
    try:
        helper.log_debug("Sending token request to %s" % url)
        req = Request(url, data)
        response = urlopen(req)
    except Exception as e:
        raise Exception("Error getting token for client_id %s from %s: %s" % (client_id, url, str(e)))
        exit(1)
    else:
        helper.log_info("Succesfully sent token request to %s" % url)
        # Parse request
        try:
            jsonResponse = json.loads(response.read())
            access_token = jsonResponse["access_token"]
        except Exception as e:
            raise Exception("Error parsing token response for client_id %s from %s: %s" % (client_id, url, str(e)))
            exit(1)
        else:
            helper.log_info("Succesfully parsed access_token for client_id %s from o %s" % (client_id, url))
            return access_token

def get_defender_query_results(helper, client_id, access_token, api_endpoint, query):
    headers = {
	'Content-Type' : 'application/json',
	'Accept' : 'application/json',
	'Authorization' : "Bearer " + access_token
    }

    # Prepare request
    try:
        url = "https://%s/api/advancedqueries/run" % api_endpoint
        data = json.dumps({ 'Query' : query }).encode("utf-8")
    except Exception as e:
        raise Exception("Error encoding query request body for client_id %s: %s" % (client_id, str(e)))
        exit(1)
    else:
        helper.log_debug("Succesfully parsed query request prep for %s" % url)

    # Do request
    if is_proxy(helper):
        try:
            proxies = get_proxies(helper)
            install_opener(build_opener(ProxyHandler(proxies)))
        except Exception as e:
            raise Exception("Error setting proxy %s: %s" % (get_proxies(helper), str(e)))
            exit(1)
        else:
            helper.log_debug("Succesfully installed proxies %s" % proxies)
    try:
        helper.log_debug("Sending query request to %s: %s \n %s\n" % (url, headers, data))
        req = Request(url, data, headers)
        response = urlopen(req)
    except Exception as e:
        # 1) Permission for AdvancedQuery.Read.All under WindowsDefenderATP should be granted
        #    under AAD -> App registrations -> WindowsDefenderATPThreatIntelAPI
        # 2) Grant permissions by admin should also be run because we can't ok this question panel through the API
        raise Exception("Error getting query results for client_id %s from %s: %s" % (client_id, url, str(e)))
        exit(1)
    else:
        helper.log_info("Succesfully sent query request to %s" % url)
        try:
            jsonResponse = json.loads(response.read())
            #schema = jsonResponse["Schema"]
            results = jsonResponse["Results"]
        except Exception as e:
            raise Exception("Error parsing query response for client_id %s from %s: %s" % (client_id, url, str(e)))
            exit(1)
        else:
            helper.log_debug("Succesfully parsed query results for client_id %s from o %s" % (client_id, url))
            return results

def validate_input(helper, definition):
    api_endpoint   = definition.parameters.get('api_endpoint', None)
    get_defender_connectivity(helper, api_endpoint)

def collect_events(helper, ew):
    global_account = helper.get_arg('client_id', None)
    client_id = global_account.get("username", None)
    secret = global_account.get("password", None)
    tenant_id = helper.get_arg('tenant_id', None)
    api_endpoint = helper.get_arg('api_endpoint', None)
    query = helper.get_arg('query', None)

    access_token = get_defender_access_token(helper, client_id, secret, tenant_id)
    results = get_defender_query_results(helper, client_id, access_token, api_endpoint, query)
    helper.log_info("Got %d results for client_id %s from %s" % (len(results), client_id, api_endpoint))
    for result in results:
        eventJson = json.dumps(result, indent=2, sort_keys=True)
        event = helper.new_event(eventJson, time=None, host=None, index=helper.get_output_index(),
                                 source=None, sourcetype=helper.get_sourcetype(),
                                 done=True, unbroken=True)
        ew.write_event(event)


import torch
import torch.nn as nn

class DSCNN(torch.nn.Module):
    def __init__(self, input_shape=(1, 49, 10)):
        self.input_shape = input_shape
        super().__init__()

        # Model layers

        # Input pure conv2d
        self.inputlayer = nn.Conv2d(
            in_channels=1, out_channels=64, kernel_size=(10, 4), stride=(2, 2), padding=(5, 1))
        self.bn = nn.BatchNorm2d(64, momentum=0.99)
        self.relu = nn.ReLU()
        self.dropout1 = nn.Dropout(p=0.2)

        # First layer of separable depthwise conv2d
        # Separable consists of depthwise conv2d followed by conv2d with 1x1 kernels
        self.depthwise1 = nn.Conv2d(
            in_channels=64, out_channels=64, kernel_size=3, padding=1, groups=64)
        self.pointwise1 = nn.Conv2d(
            in_channels=64, out_channels=64, kernel_size=1, stride=1, padding=0)
        self.bn11 = nn.BatchNorm2d(64, momentum=0.99)
        self.relu11 = nn.ReLU()
        self.conv1 = nn.Conv2d(
            in_channels=64, out_channels=64, kernel_size=1, stride=1, padding=0)
        self.bn12 = nn.BatchNorm2d(64, momentum=0.99)
        self.relu12 = nn.ReLU()

        # Second layer of separable depthwise conv2d
        self.depthwise2 = nn.Conv2d(
            in_channels=64, out_channels=64, kernel_size=3, padding=1, groups=64)
        self.pointwise2 = nn.Conv2d(
            in_channels=64, out_channels=64, kernel_size=1, stride=1, padding=0)
        self.bn21 = nn.BatchNorm2d(64, momentum=0.99)
        self.relu21 = nn.ReLU()
        self.conv2 = nn.Conv2d(
            in_channels=64, out_channels=64, kernel_size=1, stride=1, padding=0)
        self.bn22 = nn.BatchNorm2d(64, momentum=0.99)
        self.relu22 = nn.ReLU()

        # Third layer of separable depthwise conv2d
        self.depthwise3 = nn.Conv2d(
            in_channels=64, out_channels=64, kernel_size=3, padding=1, groups=64)
        self.pointwise3 = nn.Conv2d(
            in_channels=64, out_channels=64, kernel_size=1, stride=1, padding=0)
        self.bn31 = nn.BatchNorm2d(64, momentum=0.99)
        self.relu31 = nn.ReLU()
        self.conv3 = nn.Conv2d(
            in_channels=64, out_channels=64, kernel_size=1, stride=1, padding=0)
        self.bn32 = nn.BatchNorm2d(64, momentum=0.99)
        self.relu32 = nn.ReLU()

        # Fourth layer of separable depthwise conv2d
        self.depthwise4 = nn.Conv2d(
            in_channels=64, out_channels=64, kernel_size=3, padding=1, groups=64)
        self.pointwise4 = nn.Conv2d(
            in_channels=64, out_channels=64, kernel_size=1, stride=1, padding=0)
        self.bn41 = nn.BatchNorm2d(64, momentum=0.99)
        self.relu41 = nn.ReLU()
        self.conv4 = nn.Conv2d(
            in_channels=64, out_channels=64, kernel_size=1, stride=1, padding=0)
        self.bn42 = nn.BatchNorm2d(64, momentum=0.99)
        self.relu42 = nn.ReLU()

        self.dropout2 = nn.Dropout(p=0.4)
        self.avgpool = torch.nn.AvgPool2d((25, 5))
        self.out = nn.Linear(64, 12)

    def forward(self, input):

        # Input pure conv2d
        x = self.inputlayer(input)
        x = self.dropout1(self.relu(self.bn(x)))

        # First layer of separable depthwise conv2d
        x = self.depthwise1(x)
        x = self.pointwise1(x)
        x = self.relu11(self.bn11(x))
        x = self.conv1(x)
        x = self.relu12(self.bn12(x))

        # Second layer of separable depthwise conv2d
        x = self.depthwise2(x)
        x = self.pointwise2(x)
        x = self.relu21(self.bn21(x))
        x = self.conv2(x)
        x = self.relu22(self.bn22(x))

        # Third layer of separable depthwise conv2d
        x = self.depthwise3(x)
        x = self.pointwise3(x)
        x = self.relu31(self.bn31(x))
        x = self.conv3(x)
        x = self.relu32(self.bn32(x))

        # Fourth layer of separable depthwise conv2d
        x = self.depthwise4(x)
        x = self.pointwise4(x)
        x = self.relu41(self.bn41(x))
        x = self.conv4(x)
        x = self.relu42(self.bn42(x))

        x = self.dropout2(x)
        x = self.avgpool(x)
        # x = torch.squeeze(x)
        x = x.flatten(1)
        x = self.out(x)

        return x


from pyspark import SparkContext, SQLContext
from pyspark.sql.types import StructType
from pyspark.sql.types import StructField
from pyspark.sql.types import StringType, IntegerType

sc = SparkContext()
sqlContext = SQLContext(sc)

####
# 1. Setup (10 points): Download the gbook file and write a function to load it in an RDD & DataFrame
####

# RDD API
# Columns:
# 0: place (string), 1: count1 (int), 2: count2 (int), 3: count3 (int)

rdd = sc.textFile("gbooks")

schema = StructType([
    StructField("word", StringType(), True),
    StructField("count1", StringType(), True),
    StructField("count2", StringType(), True),
    StructField("count3", StringType(), True)
])

df = sqlContext.createDataFrame(rdd.map(lambda x: x.split("\t")), schema)

# Spark SQL - DataFrame API

####
# 3. Filtering (10 points) Count the number of appearances of word 'ATTRIBUTE'
####

# Spark SQL
df.createOrReplaceTempView("MP8")
sqlContext.sql("SELECT COUNT(*) FROM MP8 WHERE word = 'ATTRIBUTE'").show()

# +--------+
# |count(1)|
# +--------+
# |     201|
# +--------+


# -*- coding: utf-8 -*-
# @Author: Popeye
# @Date:   2019-02-16 13:59:32
# @Last Modified by:   Raaghul Umapathy
# @Last Modified time: 2019-02-24 14:09:12

import requests
import pandas as pd
from bs4 import BeautifulSoup

# Sample base url
# Base_url = ("https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbolCode=-10003&symbol=NIFTY&symbol=NIFTY&instrument=OPTIDX&date=-&segmentLink=17&segmentLink=17")

def scrape_data(search_string):

    base_url = "https://www.nseindia.com"
    query_prefix = "/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?"

    url = base_url + query_prefix + search_string
    # print(url)
    try:
        page = requests.get(url)
        print("Request Status code={}".format(page.status_code))
        page.content

    except:
        return "Invalid url or HTTP request failed"

    soup = BeautifulSoup(page.content, 'html.parser')

    return soup

def get_data_from_web(search_string):

    soup_data = scrape_data(search_string)
    # scrape HTML content
    table_it = soup_data.find_all(class_="opttbldata")
    table_cls_1 = soup_data.find_all(id="octable")

    col_list = []

# The code given below will pull the headers of the Option Chain table
    for mytable in table_cls_1:
        table_head = mytable.find('thead')

        try:
            rows = table_head.find_all('tr')
            for tr in rows:
                cols = tr.find_all('th')
                for th in cols:
                    er = th.text
                    ee = er.encode('utf8')
                    ee = str(ee, 'utf-8')
                    col_list.append(ee)

        except:
            print("no thead")

    col_list_fnl = [e for e in col_list if e not in ('CALLS', 'PUTS', 'Chart', '\xc2\xa0', '\xa0')]

    table_cls_2 = soup_data.find(id="octable")
    all_trs = table_cls_2.find_all('tr')
    req_row = table_cls_2.find_all('tr')

    column_names = ['CALLS_OI', 'CALLS_Chng_in_OI', 'CALLS_Volume', 'CALLS_IV', 'CALLS_LTP', 'CALLS_Net_Chng', 'CALLS_Bid_Qty', 'CALLS_Bid_Price', 'CALLS_Ask_Price', 'CALLS_Ask_Qty',
                    'Strike_Price',
                    'PUTS_Bid_Qty', 'PUTS_Bid_Price', 'PUTS_Ask_Price', 'PUTS_Ask_Qty', 'PUTS_Net_Chng', 'PUTS_LTP', 'PUTS_IV', 'PUTS_Volume',
                    'PUTS_Chng_in_OI', 'PUTS_OI']

    new_table = pd.DataFrame(index=range(0, len(req_row) - 3), columns=column_names)

    row_marker = 0

    for row_number, tr_nos in enumerate(req_row):

        # This ensures that we use only the rows with values
        if row_number <= 1 or row_number == len(req_row) - 1:
            continue

        td_columns = tr_nos.find_all('td')

        # This removes the graphs columns
        select_cols = td_columns[1:22]
        cols_horizontal = range(0, len(select_cols))

        for nu, column in enumerate(select_cols):

            utf_string = column.get_text()
            utf_string = utf_string.strip('\n\r\t": ')

            tr = utf_string.encode('utf-8')
            tr = str(tr, 'utf-8')
            tr = tr.replace(',', '')
            new_table.iloc[row_marker, [nu]] = tr

        row_marker += 1

    # Get underlying stock price
    span_list = soup_data.find_all("span")

    spot_price = span_list[0].get_text().split()[3]

    # new_table.to_csv('Option_Chain_Table.csv')
    return new_table, spot_price


import glob
import ntpath
import os
import re
from pathlib import Path

def get_uploads_dir():
    curr_path = Path().absolute()
    proj_path = Path(curr_path).parents[0]
    uploads_path = Path.joinpath(proj_path, "apps/app_web/priv/uploads")
    return uploads_path

def get_path(user_id):
    up_path = get_uploads_dir()
    user_images_path = Path.joinpath(up_path, str(user_id))
    if not Path.is_dir(user_images_path):
        Path.mkdir(user_images_path)
    return user_images_path

def get_last_dir(path):
    files = []
    for file in get_filepaths_with_glob(path, r'*[0-9]*.png'):
        file = ntpath.basename(file)
        file_without_ext = os.path.splitext(file)[0]
        file_ = re.search(r'\d+', file_without_ext).group(0)
        files.append(file_)

    files = sorted(files, key=lambda x: int(x))
    if not files:
        return 0
    else:
        return files[-1]

def get_filepaths_with_glob(root_path: str, file_regex: str):
    return glob.glob(os.path.join(root_path, file_regex))


from __future__ import annotations

import logging
import sys

# logging.basicConfig(level=100)

class InheritLogger:
    lvl = logging.DEBUG
    lgr: logging.Logger = logging.getLogger("mrp")
    h = logging.StreamHandler(sys.stdout)

    h.setLevel(lvl)
    lgr.addHandler(h)
    lgr.setLevel(lvl)

    @classmethod
    def set_logger_by_name(cls, lgr_name):
        """To overwrite the deafult logger"""
        cls.lgr = logging.getLogger(lgr_name)

    @classmethod
    def set_lgr_level(cls, lvl: int):
        for h in cls.lgr.handlers:
            h.setLevel(lvl)

        cls.lgr.setLevel(lvl)


# All Necessary Library Imports
#from Database import Connector
import threading
from joblib import load
import pandas as pd
import numpy as np
from CustomLogger.logger import Logger

logging = Logger('logFiles/test.log')

class ThreadWithResult(threading.Thread):
    def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None):
        def function():
            self.result = target(*args, **kwargs)
        super().__init__(group=group, target=function, name=name, daemon=daemon)

def backend(result):

    logging.info('INFO', 'Data for Database: {}'.format(result))
    result['Journey_month'] = int(result['Journey_month'])
    result['Journey_day'] = int(result['Journey_day'])
    result['Total_Duration'] = int(result['Total_Duration'])

def featureCorrection(result):

    logging.info('INFO', 'Data is received  : {}'.format(result))
    result['Journey_month'] = result['Departure_Date'].split('-')[1]
    result['Journey_day'] = result['Departure_Date'].split('-')[2]
    result.pop('submit')
    result.pop('Departure_Date')
    logging.info('INFO', 'Data Cleaned : {}'.format(result))

    frame = pd.read_csv("Removedoutlierfile.csv")
    frame = frame.drop('Price', axis=1)
    frame = frame.append(result, ignore_index=True)
    logging.info('INFO', 'Frame created and data appended')

    frame[['Journey_day', 'Journey_month']] = frame[['Journey_day', 'Journey_month']].astype('int64')
    frame['Total_Duration'] = frame['Total_Duration'].astype('float64')

    frame = pd.get_dummies(frame, drop_first=True)
    logging.info('INFO', 'Dummy variables created from received data')
    scaler = load("Scaling.pkl")
    result = frame.iloc[-1].values
    result = scaler.transform(np.reshape(result, (1, -1)))
    logging.info('INFO', 'Data scaled and sent for prediction')

    return result

def getResult(result):

    logging.info('INFO', 'Threading Called !')
    in1 = result
    in2 = result
    thread1 = ThreadWithResult(target=featureCorrection, args=(in1,))
    thread2 = ThreadWithResult(target=backend, args=(in2,))
    logging.info('INFO', 'Threading Created !')
    thread1.start()
    thread2.start()
    logging.info('INFO', 'Threading Started !')
    thread1.join()
    thread2.join()
    logging.info('INFO', 'Threading join !')
    return thread1.result


import torch
from torchsummary import summary
from torch.utils.data import Dataset,DataLoader
import torch.nn as nn
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler

from dataset import load_prepare, boxplot, normalize, split_window, DatasetSales

from Model import ConvNet, train_fn, test_fn

# Time Series Prediction

# Clemence Vast - 2020
# Objectives

# We want to predict the next month for items and shops

#     load and clean data
#     visualize different data representations
#     select interesting data
#     build a CNN
#     train one or multiple items-shop series in the CNN
#     evaluate the CNN and analyze results

# general values
path_csv= "./sales_train.csv"
window = 12 # number of months in the sliding window

# training values
learning_rate = 0.001
epochs = 200

# evaluation values
prediction = []
batch_size = 2
iterations =  int(inputs.shape[0]/2)

def main():
    ## Load DataFrame and visualize info
    train_csv = load_prepare(path_csv)
    print(train_csv.head(10))
    print(train_csv.info())
    print(train_csv.shape)

    # Preparing and cleaning data
    ## let's remove negative values for sales
    train_csv.query('item_price > 0', inplace=True)
    print(train_csv.shape)

    # We want to check if there are some rare exceptions in the values and filter them
    # Box plot
    boxplot(train_csv, "Boxplot of sales")

    # We see that values tend to be under 1000 except 2 values.
    # To clean a little bit our dataset we're going to remove those values.
    train_csv.query('item_cnt_day < 1000', inplace=True)

    # group items by date blocks
    df_date = pd.DataFrame(train_csv.groupby(["date_block_num", "shop_id", "item_id", "item_price"], as_index=False)['item_cnt_day'].sum())
    print(df_date.head(5))

    # Visualize by Month

    plt.plot( "date_block_num", "item_cnt_day", data = df_date.sort_values(by="date_block_num"))

    plt.xticks(rotation=90)

    plt.show()

    #Normalizing

    train_csv["item_csv_day_norm"] = normalize(train_csv["item_cnt_day"])

    print(train_csv)

    # Cleaning
    # Let's check the data and filter when we see unuseful values
    train_csv = train_csv.groupby(['date_block_num',"shop_id", "item_id"]).agg({"item_cnt_day_norm":"count"}, as_index=False)
    print(train_csv)

    ## We could group all values together, by date block

    train_grouped = train_csv.sort_values('date_block_num').groupby(['date_block_num'], as_index=True)
    train_grouped = train_grouped.agg({'item_cnt_day_norm':"mean"})
    train_grouped.rename(columns={"item_cnt_day_norm":"sales_norm"}, inplace=True)
    print(train_grouped.head())

    ## Split values between train and test
    test_proportion = 0.4
    train, test = train_test_split(train_grouped, test_size=test_proportion)

    print('Train proportion : {:.0f}%'.format((1-test_proportion)*100))
    print('Validation proportion : {:.0f}%'.format(test_proportion*100))

    train_x,train_y = split_window(train.sales_norm.values,window)
    valid_x,valid_y = split_window(test.sales_norm.values,window)

    ## preparing Train
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    cnn = ConvNet().to(device)
    optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate)
    criterion = nn.MSELoss()

    # Torch summary gives a small summary of the Neural Network with the input size

    print(summary(cnn,[12,1]))

    ## Create sets and Data Loaders

    trainset = DatasetSales(train_x.reshape(train_x.shape[0],train_x.shape[1],1),train_y)
    validset = DatasetSales(valid_x.reshape(valid_x.shape[0],valid_x.shape[1],1),valid_y)
    trainloader = torch.utils.data.DataLoader(trainset,batch_size=2,shuffle=False)
    validationloader = torch.utils.data.DataLoader(trainset,batch_size=2,shuffle=False)

    dataset_loaders = {"train": trainloader, "test": validationloader}

    # Train model
    train_losses = []
    valid_losses = []

    for epoch in range(epochs):
        print('\nEpochs {}/{}'.format(epoch+1,epochs))
        train_fn()
        test_fn()
    # Evaluation of the model

    # We want to print the Loss for both training and validation, to check if the
    # model learned correctly from the features and detect overfitting.

    plt.plot(train_losses,label='Loss train')
    plt.plot(valid_losses,label='Loss validation')
    plt.title('MSE Loss graph')
    plt.legend(bbox_to_anchor=(1.05, 1), loc= 'center left', borderaxespad=0.)
    plt.show()

    targetX , targetY = split_window(train.sales_norm.values,window)
    inputs = targetX.reshape(targetX.shape[0],targetX.shape[1],1)

    cnn.eval()

    for i in range(iterations):
        preds = cnn(torch.tensor(inputs[batch_size*i:batch_size*(i+1)]).float())
        prediction.append(preds.detach().numpy())

    fig, ax = plt.subplots(1, 2,figsize=(11,4))
    ax[0].set_title('Prediction')
    ax[0].plot(prediction)
    ax[1].set_title('Reality')
    ax[1].plot(targetY)
    plt.show()

# Analysis

# Of course my model is not really representing the reality and is not complete, I would normally need to create a model by item-shop duo and then connect them all together for example. To work with a bigger dataset and add additional values I could also use a k-fold technique. Maybe convolutional layers are also not the best way to predict this kind of information. Overall, the Loss looks good but I'm not sure it would with a different dataset of values.

# If I had more time here's what I wouldve done:

#     Build a model for each item-shop duo
#     Connect them all with a fully connected layer
#     Build a model by shop (for all items)
#     Build a model by item (for all shops)

# I could've compare all those models and select my favorite one or analyse them too.