#from collections import deque

"""
def search(lines, pattern, histroy=5):
    privious_lines = deque(naxlen=history)
    for line in lines:
        if pattern in lines:
            yield line, previous_lines
        privious_lines.append(line)

if __name__ == '__main__':
    with open('somefile.txt') as f:
        for line, privlines in search(f, 'python', 5):
            for pline in prevlines:
                print(pline, end='')
            print(line, end='')
            print('-' * 20)
"""

import heapq

portfolio = [
    {'name': 'IBM', 'shares': 100, 'price': 91.1},
    {'name': 'apple', 'shares': 50, 'price': 543.22},
    {'name': 'fb', 'shares': 200, 'price': 21.09},
    {'name': 'hpq', 'shares': 35, 'price': 31.75},
    {'name': 'yhoo', 'shares': 45, 'price': 16.35},
    {'name': 'acme', 'shares': 75, 'price': 115.65},
]

cheap = heapq.nsmallest(3, portfolio, key=lambda s: ['price'])
expensive = heapq.nlargest(3, portfolio, key=lambda s: ['price'])
print(cheap)
print(expensive)


# uncompyle6 version 3.7.4
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /Users/amarin/dev/sphinx-mediatel/src/sphinx_paw/rewritable.py
# Compiled at: 2019-11-19 17:00:50
# Size of source mod 2**32: 1413 bytes
"""Allow default templates and configuration files redefinition locally"""
from sphinx.util import logging
from os import path
from sphinx.application import Sphinx
from sphinx.util import console
from sphinx_paw.constants import NOTSET
DEFAULT_PATH = path.join(path.dirname(__file__), 'defaults')
logger = logging.getLogger(__name__)

def rewritable_file_path(app, filename, local_path=NOTSET, default_path=NOTSET):
    """Get filepath in documentation src or default in sphinx_paw"""
    if not isinstance(app, Sphinx):
        raise AssertionError
    else:
        src_dir = app.srcdir
        if local_path:
            found_path = path.join(src_dir, local_path, filename)
        else:
            found_path = path.join(src_dir, filename)
    if path.exists(found_path):
        logger.info(f"Using local {console.bold(filename)}")
        return found_path
    else:
        if default_path is NOTSET:
            default_path = DEFAULT_PATH
        else:
            if default_path is None:
                raise FileNotFoundError(f"Local file {found_path} not found")
        logger.info(f"Using {console.bold('default')} {filename}")
        return path.join(default_path, filename)

def rewritable_file_content(app, filename):
    """Get rewritable file content"""
    assert isinstance(app, Sphinx)
    file_path = rewritable_file_path(app, filename, DEFAULT_PATH)
    with open(file_path, 'rb') as (content_fh):
        content = content_fh.read().decode()
        return content


import csv
import re
with open('votes/votes_results.csv', 'r', newline='', encoding="utf8") as f:
    csv_reader = csv.reader(f)
    with open('votes/parsed_votes_results.csv', 'w', newline='', encoding="utf8") as p:
        write_vote = csv.writer(p)
        characters_to_eliminate = ["'"]
        max_votes = 5
        for line in csv_reader:
            is_it_pos = 0
            is_it_neg = 0
            is_it_neu = 0
            for i in range(0, len(line)):
                if  line[i] == "Positivo":
                    is_it_pos=is_it_pos+1
                elif line[i] == "Negativo":
                    is_it_neg=is_it_neg+1
                elif line[i] == "Neutral":
                    is_it_neu=is_it_neu+1
            print("pos: ",is_it_pos,"neg: ",is_it_neg,"neu:",is_it_neu)
            story_wo_url = re.sub(r"http\S+", "", line[0])
            story_wo_characters = story_wo_url.translate({ord(x): '' for x in characters_to_eliminate})
            if is_it_pos >= max_votes:
                write_vote.writerow([story_wo_characters.lower(), "positive"])
            elif is_it_neg >= max_votes:
                write_vote.writerow([story_wo_characters.lower(), "negative"])


def find_max(array):
    max_value = 0
    if len(array) == 0:
        return -1
    for v in array:
        if v >= max_value:
            max_value = v
    return max_value

def average(array):
    if len(array) == 0:
        return -1
    sum = 0
    for m in array:
        sum += m
    average_value = sum / len(array)
    return average_value

def main():
    mas1 = [1, 2, 3]

    print ("max =" + str(find_max(mas1)))

    print ("average=" + str(average(mas1)))
main()


#recursive
def lcs(a,b):
    if len(a)==0 or len(b)==0:
        return []
    elif a[-1]==b[-1]:
        return lcs(a[:-1],b[:-1])+[a[-1]]
    else:
        return max(lcs(a[:-1],b),lcs(a,b[:-1]),key=len)

#dp
def lcs(a,b,c=None):
    if not None:
        c={}
    key=(len(a),len(b))
    if key not in c:
        if len(a)==0 or len(b)==0:
            c[key]=[]
            #return
        else:
           if a[-1]==b[-1]:
               c[key]=lcs(a[:-1],b[:-1],c)+[a[-1]]
           else:
               c[key]=max(lcs(a[:-1],b,c),lcs(a,b[:-1],c),key=len)
    return c[key]


from flask import redirect, url_for, render_template
from flask_login import login_required

from . import meetings_bp
from .forms import MeetingForm, ActivityForm
from ..models import Meetings, Activity
from datetime import datetime, date

@meetings_bp.route('/addclass')
def addclass():
    activity_a = Activity(topic="taiwan", lead_by="person_taiwan", avenue="Lims", date=datetime.today())
    activity_b = Activity(topic="singapore", lead_by="person_singapore", avenue="Tans", date=datetime.today())
    category = "saturday"
    season = "winter 2018"
    remark = ""
    # meet_a = Meetings(category=category, activities=[activity_a, activity_b])
    # meet_a.save()

    meet_a = Meetings(category=category, season=season, remark=remark)
    meet_a.activities.append(activity_a)
    meet_a.activities.append(activity_b)
    meet_a.save()

    return redirect(url_for('justhello.withmsg', inmsg="add meeting success"))

@meetings_bp.route('/class_category', methods=['GET', 'POST'])
@login_required
def add_class_category():
    form = MeetingForm()
    # user =
    if form.validate_on_submit():
        meet = Meetings(category=form.category.data, season=form.season.data, remark=form.remark.data)
        is_success = meet.save()
        return redirect(url_for('justhello.withmsg', inmsg="add meeting success"))

    return render_template('meetings/class_category.html', title="add category", form=form)

@meetings_bp.route('/class_activities', methods=['GET', 'POST'])
@login_required
def add_activities():
    form = ActivityForm()
    if form.validate_on_submit():
        #for now just pick any category
        category_picked = Meetings.objects.first()
        # from datetime.date to datetime.datetime
        form_date_str = form.date.data.strftime('%Y-%m-%d')  # 2002-03-11
        date_picked = datetime.strptime(form_date_str, '%Y-%m-%d')
        activity = Activity(topic=form.topic.data, lead_by=form.lead_by.data, avenue=form.avenue.data, date=date_picked)

        category_picked.activities.append(activity)
        is_success = category_picked.save()
        return render_template('meetings/class_category.html', title="add category", form=form)
    return render_template('meetings/class_activities.html', title="add activities", form=form)


#!/usr/bin/env python3
from collections import deque
import sys
try:
    from typing import List
except ImportError:
    pass

def solve(N: int, u: "List[int]", v: "List[int]", w: "List[int]"):
    G = [[] for _ in range(N)]
    for ui, vi, wi in zip(u, v, w):
        ui -= 1
        vi -= 1
        G[ui].append((vi, wi))
        G[vi].append((ui, wi))
    color = [-1] * N
    color[0] = 0
    q = deque([0])
    while q:
        s = q.popleft()
        for t, wi in G[s]:
            if color[t] >= 0:
                continue
            color[t] = (color[s] + wi) % 2
            q.append(t)
    for colori in color:
        print(colori)

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    u = [int()] * (N - 1)  # type: "List[int]"
    v = [int()] * (N - 1)  # type: "List[int]"
    w = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        u[i] = int(next(tokens))
        v[i] = int(next(tokens))
        w[i] = int(next(tokens))
    solve(N, u, v, w)

if __name__ == '__main__':
    main()


from flask_restplus import Resource, Api
from .. import api

ns = api.namespace('user', description="用户信息")

class User(Resource):
    def get(self):
        "获取用户信息"
        return {}

    def post(self):
        "修改用户信息"

        payload = request.json

        introduction = payload["introduction"]
        dictionary_type = payload["dictionary_type"]
        image = payload["image"]
        if not isinstance(image, list):
            image = list(image)
        if not isinstance(image, list):
            return {"status": 400, "msg": "图片传递错误"}

ns.add_resource(User, '/')


from tkinter import *

root = Tk()
root.title("BMI Calculator")
root.configure(width=100, height=100)
root.configure(bg="black")

def calc():
    BMI = BMI_val(mass.get(), height.get())
    Stat = getStatus()
    stat.set(Stat)
    bmi_Val.set(format(BMI, ".2f"))

def BMI_val(mass, height):
    return mass/height**2

#def getHeight():
#    return height

def clear():
    stat.set('')
    bmi_Val.set('0.0')
    mas.delete(0, 'end')
    heigh.delete(0, 'end')

#def getWidth():
 #   return mass

def getStatus():
    if bmi_Val.get() > 40:
        return "You are Obese Class 3"
    elif bmi_Val.get() > 35 and bmi_Val.get() < 40:
        return "You are Obese Class 2"
    elif bmi_Val.get() > 30 and bmi_Val.get() < 35:
        return "You are Obese Class 1"
    elif bmi_Val.get() > 25 and bmi_Val.get() < 30:
        return "You are Overweight"
    elif bmi_Val.get() > 18.5 and bmi_Val.get() < 25:
        return "You are at a Normal weight"
    elif bmi_Val.get() > 17 and bmi_Val.get() < 18.5:
        return "You are thin"
    else:
        return "You are very thin"

height = DoubleVar()
h_label = Label(root, text="Height", fg="red", bg="black", font=("Calibri 14 bold"), pady=5, padx=8)
heigh = Entry(root, textvariable=height)
h_label.grid(row=2)
heigh.grid(row=2, column=1, columnspan=2, padx=5)

mass = DoubleVar()
w_label = Label(root, text="Mass", fg="red", bg="black", font=("Calibri 14 bold"), pady=10, padx=2)
mas = Entry(root, textvariable=mass)
w_label.grid(row=4)
mas.grid(row=4, column=1)

bmi_Val = DoubleVar()
stat = StringVar()
bmi = Label(root, text="BMI: ", fg="blue", bg="black", font=("Calibri 14 bold"), pady=10, padx=2, justify=LEFT)
status = Label(root, text="Status", fg="blue", bg="black", font=("Calibri 14 bold"), pady=10, padx=2)
status_msg = Label(root, textvariable=stat, fg="white", bg="black", font=("Calibri 14 bold"), pady=10, padx=2)
BMI_total = Label(root, textvariable=bmi_Val, fg="white", bg="black", font=("Calibri 14 bold"), pady=10, padx=2)
bmi.grid(row=6)
status.grid(row=7)
BMI_total.grid(row=6, column=1)
status_msg.grid(row=7, column=1)

calculate = Button(root, text="Calculate", command=calc, fg="black", bg="white", font=("Calibri 12 bold") )
clear = Button(root, text="Reset", command=clear, fg="black", bg="white", font=("Calibri 14 bold"))
calculate.grid(row=8)
clear.grid(row=8, column=3)

root.mainloop()


import sys
import time
import numpy as np
from pathlib import Path
import json
import b2sdk.v1 as b2
import aljpy
import multiprocessing
from pavlov import runs, files, storage
from tqdm.auto import tqdm

@aljpy.memcache()
def api(bucket):
    # Keys are in 1Password
    api = b2.B2Api()
    keys = json.loads(Path('credentials.json').read_text())['backblaze'][bucket]
    api.authorize_account('production', **keys)
    return api

def sync_up(local, remote):
    bucket, path = remote.split(':')
    workers = multiprocessing.cpu_count()
    syncer = b2.Synchronizer(workers)
    with b2.SyncReport(sys.stdout, False) as reporter:
        syncer.sync_folders(
            source_folder=b2.parse_sync_folder(local, api(bucket)),
            dest_folder=b2.parse_sync_folder(f'b2://{bucket}/{path}', api(bucket)),
            now_millis=int(round(time.time() * 1000)),
            reporter=reporter)

def sync_down(local, remote, **kwargs):
    bucket, path = str(remote).split(':')
    workers = multiprocessing.cpu_count()
    syncer = b2.Synchronizer(workers, **kwargs)
    with b2.SyncReport(sys.stdout, False) as reporter:
        syncer.sync_folders(
            source_folder=b2.parse_sync_folder(f'b2://{bucket}/{path}', api(bucket)),
            dest_folder=b2.parse_sync_folder(str(local), api(bucket)),
            now_millis=int(round(time.time() * 1000)),
            reporter=reporter)

def upload(local, remote):
    bucket, path = remote.split(':')
    bucket = api(bucket).get_bucket_by_name(remote)
    bucket.upload_local_file(
        local_file=local,
        file_name=path)

def ablate_run_snapshots(run):
    for i, info in tqdm(storage.snapshots(run).items()):
        if (i == 0) or (np.log2(i) % 1 == 0):
            pass
        else:
            print('Removing', run, info['path'].name)
            files.remove(run, info['path'].name)

def ablate_snapshots():
    for run, info in runs.runs().items():
        ablate_run_snapshots(run)

def download(local, remote):
    bucket, path = remote.split(':')
    b2.get_bucket_by_name()
    download_dest = b2.DownloadDestLocalFile(local)
    bucket.download_file_by_name(path, download_dest)

def backup():
    sync_up('./output/pavlov', 'boardlaw:output/pavlov')
    sync_up('./output/experiments/architecture/results', 'boardlaw:output/experiments/architecture/results')

def fetch():
    sync_down('./output/pavlov', 'boardlaw:output/pavlov')

if __name__ == '__main__':
    backup()


import math
import numpy as np

from config import *
from matrix.Mat import transl
from os.path import dirname, join

project_root = dirname(dirname(__file__))

# --------------------------------------------------------------
def work_place_size(z):
    """
    :param z: distance between work place and camera. Measured in meters
    :return: work place height and width. Measured im meters
    """

    H = 2 * math.tan(math.radians(alpha) / 2) * z
    W = 2 * math.tan(math.radians(betta) / 2) * z

    return H, W
# --------------------------------------------------------------
def camera_matrix(z):
        """
            Getting the transformation matrix, that allows to
        transform pixels to mm

        :param z: distance between work place and camera. Measured in meters
        :return: transformation matrix

        Variables:

        W, H - workplace width and height in meters
        coef - transformation coefficient, that transformate pixels to meters
        """

        H, W = work_place_size(z)

        coef_x = W / resolution[1]
        coef_y = H / resolution[0]

        camera_matrix = np.array([[coef_x, 0, 0, -W / 2],
                                  [0, coef_y, 0, -H / 2],
                                  [0, 0, 1, 0],
                                  [0, 0, 0, 1]])

        return camera_matrix
# --------------------------------------------------------------
def camera_shift_mtx(camera_bias):
    return transl(camera_bias)
#---------------------------------------------------------------

def get_coordinates(xyz=None, centr=None):
    """
    Getting the center of the object relative robotic arm

    :param xyz: coordinates of the gripper, in the shot time
    :param center: object center in the image, gotten in pixels
    :return: center of the object relative robotic arm. type: np.array([x, y , z])

    Pinhole camera model

    K is a camera intrinsic matrix like [[f, 0, cx],
                                         [0, f, cy],
                                         [0, 0, 1]]
    f - camera focal length interpreted in pixels
    cx - distance (x) to the principal axis
    cy - distance (y) to the principal axis

    inv_K -> [[1/f, 0, -cx/f],
              [0, 1/f, -cy/f],
              [0, 0, 1]]

    R is a rotation matrix -> [[0, 1, 0], swap x and y axis
                               [1, 0, 0],
                               [0, 0, 1]]
    Camera axis -------x
                |
                |
                y

    Robot base -------y
               |
               |
               x

    Pixel_coord - matrix with centre coordinates (pixels)

    To find real coordinate -> inv_K * R * Pixel_coord + Flange_T + Camera_relative_flange_T

    Useful links:
    https://stackoverflow.com/questions/41503561/whats-the-difference-between-reprojectimageto3dopencv-and-disparity-to-3d-coo
    http://www.cs.cmu.edu/~16385/s17/Slides/11.1_Camera_matrix.pdf
    http://ais.informatik.uni-freiburg.de/teaching/ws09/robotics2/pdfs/rob2-08-camera-calibration.pdf

    """

    X = np.array([centr[0], centr[1], 1])
    K = np.load(join(project_root, 'July_project\data\cameradata\newcam_mtx.npy'))

    R = np.array([[0, 1, 0],
                  [1, 0, 0],
                  [0, 0, 1]])

    Z = get_correct_z(xyz[2]) - CAMERA_SHIFTING[2] + TOOL_HEIGHT
    K_inv = np.linalg.inv(K)
    T_flange = np.array([xyz[0], xyz[1], 0])
    T_cam = np.array(CAMERA_SHIFTING)
    T = T_flange + T_cam
    coordinates = K_inv.dot(X).dot(R) * Z + T

    return {'x':coordinates[0], 'y':coordinates[1], 'z':coordinates[2]}

def save_matrix(path, matrix):
    return np.save(path, matrix)

def get_correct_z(z):
    new_z = z + CAMERA_SHIFTING[2] - OBJECT_HEIGHT
    return new_z

def tic():
    """Start a stopwatch timer"""
    import time
    global TICTOC_START_TIME
    TICTOC_START_TIME = time.time()

def toc():
    """Read the stopwatch timer"""
    import time
    if 'TICTOC_START_TIME' in globals():
        elapsed = time.time() - TICTOC_START_TIME
        #print("Elapsed time is " + str(elapsed) + " seconds.")
        return elapsed
    else:
        print("Toc: start time not set")
        return -1


import ROOT
import AbstractClasses
import os, json

class SetEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, set):
            return list(obj)
        return json.JSONEncoder.default(self, obj)

class ProductionOverview(AbstractClasses.GeneralProductionOverview.GeneralProductionOverview):

    def CustomInit(self, TestResultEnvironmentObject = 0):
        if TestResultEnvironmentObject:
            self.TestResultEnvironmentObject = TestResultEnvironmentObject
        self.HTMLFileName = 'ProductionOverview.html'
        self.ImportPath = 'OverviewClasses.CMSPixel.ProductionOverview.ProductionOverviewPage'

        self.NameSingle = 'ProductionOverviewPage'
        self.Name = 'CMSPixel_ProductionOverview_%s'%self.NameSingle

        self.SaveHTML = True

        Rows = self.FetchData()
        ModuleIDsList = self.GetModuleIDsList(Rows)
        NumModules = len(ModuleIDsList)
        NumModulesMaxPerList = 50

        TestsList = ['m20_1', 'p17_1']
        ReadbackParameters = [
            {
                'Parameter': 'par0vd',
                'Xmin': -30,
                'Xmax': 30
            },
            {
                'Parameter': 'par1vd',
                'Xmin': 30,
                'Xmax': 100
            },
            {
                'Parameter': 'par0va',
                'Xmin': -30,
                'Xmax': 30
            },
            {
                'Parameter': 'par1va',
                'Xmin': 0,
                'Xmax': 100
            },
            {
                'Parameter': 'par0rbia',
                'Xmin': -20,
                'Xmax': 50
            },
            {
                'Parameter': 'par1rbia',
                'Xmin': -1,
                'Xmax': 3,
            },
            {
                'Parameter': 'par0tbia',
                'Xmin': 0,
                'Xmax': 10
            },
            {
                'Parameter': 'par1tbia',
                'Xmin': 0,
                'Xmax': 1
            },
            {
                'Parameter': 'par2tbia',
                'Xmin': -0.001,
                'Xmax': 0.001
            },
        ]

        self.SubPages.append({
            "InitialAttributes" : {
                "Sections": ["BumpBonding", "DeadPixel", "PerformanceParameters", "DACs", "IVCurves", "Readback", "HighRate", "VcalCalibration"],
                "DateBegin": self.Attributes['DateBegin'],
                "DateEnd": self.Attributes['DateEnd'],
            },
            "Key": "Section",
            "Module": "SectionNavigation"
        })

        self.SubPages.append(
            {
                "Key": "GradingOverview",
                "Module": "GradingOverview",
                "InitialAttributes" : {
                    "StorageKey" : "GradingOverview",
                    "DateBegin": self.Attributes['DateBegin'],
                    "DateEnd": self.Attributes['DateEnd'],
                },
            }
        )

        if self.Attributes['ShowWeeklyPlots']:
            self.SubPages.append(
                {
                    "Key": "WeeklyProduction",
                    "Module": "WeeklyProduction",
                    "InitialAttributes" : {
                        "StorageKey" : "WeeklyProduction",
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    },
                }
            )

            self.SubPages.append(
                {
                    "Key": "CumulativeProductionGraph",
                    "Module": "CumulativeProductionGraph",
                    "InitialAttributes" : {
                        "StorageKey" : "CumulativeProductionGraph",
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    },
                }
            )

            self.SubPages.append(
                {
                    "Key": "ModuleList",
                    "Module": "ModuleList",
                    "InitialAttributes" : {
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    },
                }
            )

            Offset = 0
            NumModulesToShow = NumModules

            while (NumModulesToShow > 0):
                self.SubPages.append(
                    {
                        "Key": "ModuleFailuresOverview",
                        "Module": "ModuleFailuresOverview",
                        "InitialAttributes" : {
                            "StorageKey" : "ModuleFailuresOverview_%d"%Offset,
                            "DateBegin": self.Attributes['DateBegin'],
                            "DateEnd": self.Attributes['DateEnd'],
                            "NumModules": NumModulesMaxPerList,
                            "Offset": Offset,
                        },
                    }
                )
                NumModulesToShow -= NumModulesMaxPerList
                Offset += NumModulesMaxPerList
        else:
            self.SubPages.append(
                {
                    "Key": "ModuleFailuresOverview",
                    "Module": "ModuleFailuresOverview",
                    "InitialAttributes" : {
                        "StorageKey" : "ModuleFailuresOverview",
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                        "Width": 4,
                    },
                }
            )

            self.SubPages.append(
                {
                    "Key": "ModuleList",
                    "Module": "ModuleList",
                    "InitialAttributes" : {
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    },
                }
            )

        ### bump bonding ###
        self.SubPages.append({"InitialAttributes" : {"Anchor": "BumpBonding", "Title": "BumpBonding Defects"}, "Key": "Section","Module": "Section"})
        for Grade in ['All','A', 'B', 'C']:
            self.SubPages.append(
                {
                    "Key": "BumpBondingOverlay_{Grade}".format(Grade = Grade),
                    "Module": "BumpBondingOverlay",
                    "InitialAttributes" : {
                        "Test": "m20_1",
                        "Grade": "{Grade}".format(Grade = Grade),
                        "StorageKey" : "BumpBondingOverlay_{Grade}".format(Grade = Grade),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )
        self.SubPages.append(
            {
                "Key": "BumpBondingVsId",
                "Module": "BumpBondingVsId",
                "InitialAttributes" : {
                    "StorageKey" : "BumpBondingVsId",
                    "DateBegin": self.Attributes['DateBegin'],
                    "DateEnd": self.Attributes['DateEnd'],
                }
            }
        )
        ### dead pixels ###
        self.SubPages.append({"InitialAttributes" : {"Anchor": "DeadPixel", "Title": "Dead Pixels"}, "Key": "Section","Module": "Section"})
        for Grade in ['All','A', 'B', 'C']:
            self.SubPages.append(
                {
                    "Key": "DeadPixelOverlay_{Grade}".format(Grade = Grade),
                    "Module": "DeadPixelOverlay",
                    "InitialAttributes" : {
                        "Test": "m20_1",
                        "Grade": "{Grade}".format(Grade = Grade),
                        "StorageKey" : "DeadPixelOverlay_{Grade}".format(Grade = Grade),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )

        ### pixels with too high or low gain ###
        self.SubPages.append({"InitialAttributes" : {"Anchor": "BadGain", "Title": "Pixels with bad gain"}, "Key": "Section","Module": "Section"})
        for Grade in ['All','A', 'B', 'C']:
            self.SubPages.append(
                {
                    "Key": "GainOverlay_{Grade}".format(Grade = Grade),
                    "Module": "GainOverlay",
                    "InitialAttributes" : {
                        "Test": "m20_1",
                        "Grade": "{Grade}".format(Grade = Grade),
                        "StorageKey" : "GainOverlay_{Grade}".format(Grade = Grade),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )

        ### performance parameters ###
        self.SubPages.append({"InitialAttributes" : {"Anchor": "PerformanceParameters", "Title": "Performance Parameters per ROC"}, "Key": "Section","Module": "Section"})

        for Test in TestsList:
            self.SubPages.append(
                {
                    "Key": "MeanNoise_{Test}".format(Test = Test),
                    "Module": "MeanNoise",
                    "InitialAttributes" : {
                        "Test": "{Test}".format(Test = Test),
                        "StorageKey" : "MeanNoise_{Test}".format(Test = Test),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )

        #self.SubPages.append({"Key": "Dummy","Module": "Dummy"})

        for Test in TestsList:
            self.SubPages.append(
                {
                    "Key": "RelativeGainWidth_{Test}".format(Test = Test),
                    "Module": "RelativeGainWidth",
                    "InitialAttributes" : {
                        "Test": "{Test}".format(Test = Test),
                        "StorageKey" : "RelativeGainWidth_{Test}".format(Test = Test),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )

        #self.SubPages.append({"Key": "Dummy","Module": "Dummy"})

        for Test in TestsList:
            self.SubPages.append(
                {
                    "Key": "PedestalSpread_{Test}".format(Test = Test),
                    "Module": "PedestalSpread",
                    "InitialAttributes" : {
                        "Test": "{Test}".format(Test = Test),
                        "StorageKey" : "PedestalSpread_{Test}".format(Test = Test),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )

        #self.SubPages.append({"Key": "Dummy","Module": "Dummy"})

        for Test in TestsList:
            self.SubPages.append(
                {
                    "Key": "VcalThresholdWidth_{Test}".format(Test = Test),
                    "Module": "VcalThresholdWidth",
                    "InitialAttributes" : {
                        "Test": "{Test}".format(Test = Test),
                        "StorageKey" : "VcalThresholdWidth_{Test}".format(Test = Test),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )

        ### per pixel ###
        self.SubPages.append({"InitialAttributes" : {"Anchor": "PerPixel", "Title": "Pixel-based quantities"}, "Key": "Section","Module": "Section"})
        for Test in TestsList:
            self.SubPages.append(
                {
                    "Key": "GainPerPixel_{Test}".format(Test = Test),
                    "Module": "GainPerPixel",
                    "InitialAttributes" : {
                        "Test": "{Test}".format(Test = Test),
                        "StorageKey" : "GainPerPixel__{Test}".format(Test = Test),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )
        for Test in TestsList:
            self.SubPages.append(
                {
                    "Key": "PedestalPerPixel_{Test}".format(Test = Test),
                    "Module": "PedestalPerPixel",
                    "InitialAttributes" : {
                        "Test": "{Test}".format(Test = Test),
                        "StorageKey" : "PedestalPerPixel__{Test}".format(Test = Test),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )
        for Test in TestsList:
            self.SubPages.append(
                {
                    "Key": "SCurveWidthsPerPixel_{Test}".format(Test = Test),
                    "Module": "SCurveWidthsPerPixel",
                    "InitialAttributes" : {
                        "Test": "{Test}".format(Test = Test),
                        "StorageKey" : "SCurveWidthsPerPixel__{Test}".format(Test = Test),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )
        for Test in TestsList:
            self.SubPages.append(
                {
                    "Key": "VcalThresholdTrimmedPerPixel_{Test}".format(Test = Test),
                    "Module": "VcalThresholdTrimmedPerPixel",
                    "InitialAttributes" : {
                        "Test": "{Test}".format(Test = Test),
                        "StorageKey" : "VcalThresholdTrimmedPerPixel__{Test}".format(Test = Test),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )

        ### DACs ###
        self.SubPages.append({"InitialAttributes" : {"Anchor": "DACs", "Title": "DAC Parameters"}, "Key": "Section","Module": "Section"})
        TrimThresholds = ['', '35']
        DACs = ['caldel', 'phoffset', 'phscale', 'vana', 'vthrcomp', 'vtrim']
        for Test in TestsList:
            for Trim in TrimThresholds:
                for DAC in DACs:
                    self.SubPages.append(
                        {
                            "Key": "DACDistribution_{Test}".format(Test = Test),
                            "Module": "DACDistribution",
                            "InitialAttributes" : {
                                "Test": "{Test}".format(Test = Test),
                                "Trim": "{Trim}".format(Trim = Trim),
                                "DAC": "{DAC}".format(DAC = DAC),
                                "Maximum": 256,
                                "NBins": 256,
                                "StorageKey" : "DACDistribution_{Test}_{DAC}_{Trim}".format(Test=Test, DAC=DAC,Trim=Trim),
                                "DateBegin": self.Attributes['DateBegin'],
                                "DateEnd": self.Attributes['DateEnd'],
                            }
                        }
                    )

        ### TrimBits ###
        for Test in TestsList:
            for Trim in TrimThresholds:
                self.SubPages.append(
                    {
                        "Key": "DACDistribution_{Test}".format(Test = Test),
                        "Module": "DACDistribution",
                        "InitialAttributes" : {
                            "Test": "{Test}".format(Test = Test),
                            "Trim": "{Trim}".format(Trim = Trim),
                            "DAC": "{DAC}".format(DAC = "TrimBits_mu"),
                            "Maximum": 16,
                            "NBins": 64,
                            "StorageKey" : "DACDistribution_{Test}_{DAC}_{Trim}".format(Test=Test, DAC="TrimBits_mu",Trim=Trim),
                            "DateBegin": self.Attributes['DateBegin'],
                            "DateEnd": self.Attributes['DateEnd'],
                        }
                    }
                )
                self.SubPages.append(
                    {
                        "Key": "DACDistribution_{Test}".format(Test = Test),
                        "Module": "DACDistribution",
                        "InitialAttributes" : {
                            "Test": "{Test}".format(Test = Test),
                            "Trim": "{Trim}".format(Trim = Trim),
                            "DAC": "{DAC}".format(DAC = "TrimBits_sigma"),
                            "Maximum": 5,
                            "NBins": 64,
                            "StorageKey" : "DACDistribution_{Test}_{DAC}_{Trim}".format(Test=Test, DAC="TrimBits_sigma",Trim=Trim),
                            "DateBegin": self.Attributes['DateBegin'],
                            "DateEnd": self.Attributes['DateEnd'],
                        }
                    }
                )

        ### Full test duration ###
        self.SubPages.append(
            {
                "Key": "Duration",
                "Module": "Duration",
                "InitialAttributes" : {
                    "DateBegin": self.Attributes['DateBegin'],
                    "DateEnd": self.Attributes['DateEnd'],
                }
            }
        )

        ### IV ###
        self.SubPages.append({"InitialAttributes" : {"Anchor": "IVCurves", "Title": "IV Curves"}, "Key": "Section","Module": "Section"})
        for Test in TestsList:
            self.SubPages.append(
                {
                    "Key": "IVCurveOverlay_{Test}".format(Test = Test),
                    "Module": "IVCurveOverlay",
                    "InitialAttributes" : {
                        "Test": "{Test}".format(Test = Test),
                        "StorageKey" : "IVCurveOverlay_{Test}".format(Test = Test),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )

        for Test in TestsList:
            self.SubPages.append(
                {
                    "Key": "LeakageCurrent_{Test}".format(Test = Test),
                    "Module": "LeakageCurrent",
                    "InitialAttributes" : {
                        "Test": "{Test}".format(Test = Test),
                        "StorageKey" : "LeakageCurrent_{Test}".format(Test = Test),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )

        for Test in TestsList:
            self.SubPages.append(
                {
                    "Key": "LeakageCurrentSlope_{Test}".format(Test = Test),
                    "Module": "LeakageCurrentSlope",
                    "InitialAttributes" : {
                        "Test": "{Test}".format(Test = Test),
                        "StorageKey" : "LeakageCurrentSlope_{Test}".format(Test = Test),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )

        ### Readback ###
        #self.SubPages.append({"InitialAttributes" : {"Anchor": "Readback", "Title": "Readback"}, "Key": "Section","Module": "Section"})
        #for Test in TestsList:
        #    for Parameter in ReadbackParameters:
        #        self.SubPages.append(
        #            {
        #                "Key": "ReadbackParameter_{Test}_{Parameter}".format(Test = Test, Parameter = Parameter['Parameter']),
        #                "Module": "ReadbackParameter",
        #                "InitialAttributes" : {
        #                    "Test": Test,
        #                    "Parameter": Parameter['Parameter'],
        #                    "Xmin": Parameter['Xmin'],
        #                    "Xmax": Parameter['Xmax'],
        #                    "StorageKey" : "ReadbackParameter_{Test}_{Parameter}".format(Test = Test, Parameter = Parameter['Parameter']),
        #                    "DateBegin": self.Attributes['DateBegin'],
        #                    "DateEnd": self.Attributes['DateEnd'],
        #                }
        #            }
        #        )

        ### HR ###
        self.SubPages.append({"InitialAttributes" : {"Anchor": "HighRate", "Title": "HighRateTests"}, "Key": "Section","Module": "Section"})
        for Rate in [20,50,80]:
            self.SubPages.append(
                {
                    "Key": "Efficiency_{Rate}".format(Rate = Rate),
                    "Module": "Efficiency",
                    "InitialAttributes" : {
                        "Rate": "{Rate}".format(Rate = Rate),
                        "StorageKey" : "Efficiency_{Rate}".format(Rate = Rate),
                        "DateBegin": self.Attributes['DateBegin'],
                        "DateEnd": self.Attributes['DateEnd'],
                    }
                }
            )
        #self.SubPages.append(
        #    {
        #        "Key": "XrayNoisePerPixel",
        #        "Module": "XrayNoisePerPixel",
        #        "InitialAttributes" : {
        #            "StorageKey" : "XrayNoisePerPixel",
        #            "DateBegin": self.Attributes['DateBegin'],
        #            "DateEnd": self.Attributes['DateEnd'],
        #        }
        #    }
        #)

        ### Vcal Calibration ###
        #self.SubPages.append({"InitialAttributes" : {"Anchor": "VcalCalibration", "Title": "Vcal Calibration"}, "Key": "Section","Module": "Section"})
        #self.SubPages.append(
        #    {
        #        "Key": "VcalSlope",
        #        "Module": "VcalSlope",
        #        "InitialAttributes" : {
        #            "StorageKey" : "VcalSlope_Spectrum",
        #            "DateBegin": self.Attributes['DateBegin'],
        #            "DateEnd": self.Attributes['DateEnd'],
        #        }
        #    }
        #)
        #self.SubPages.append(
        #    {
        #        "Key": "VcalOffset",
        #        "Module": "VcalOffset",
        #        "InitialAttributes" : {
        #            "StorageKey" : "VcalOffset_Spectrum",
        #            "DateBegin": self.Attributes['DateBegin'],
        #            "DateEnd": self.Attributes['DateEnd'],
        #        }
        #    }
        #)

    def GenerateOverview(self):
        AbstractClasses.GeneralProductionOverview.GeneralProductionOverview.GenerateOverview(self)

        # for all plots that have been split into several boxes
        # merge the JSON files to simplify further processing of the data
        SumJSONFilesModules = ['ModuleFailuresOverview']
        for SumJSONFilesModule in SumJSONFilesModules:
            print("merge JSON files for '%s'..."%SumJSONFilesModule)
            TotalJSONDict = {}
            for Page in [x for x in self.SubPages if x['Key'] == SumJSONFilesModule]:
                Path = self.GlobalOverviewPath + '/' + self.Attributes['BasePath'] + '/' + Page['InitialAttributes']['StorageKey'] + "/KeyValueDictPairs.json"
                with open(Path) as data_file:
                    JSONData = json.load(data_file)
                    TotalJSONDict.update(JSONData)
                    print("add file: %s"%Path)

            # create directory
            JsonFileDir = self.GlobalOverviewPath + '/' + self.Attributes['BasePath'] + '/' + SumJSONFilesModule
            JsonFileName = JsonFileDir + '/KeyValueDictPairs.json'
            try:
                os.mkdir(JsonFileDir)
            except:
                pass

            # save file
            try:
                f = open(JsonFileName, 'w')
                f.write(json.dumps(TotalJSONDict, sort_keys=True, indent=4, separators=(',', ': '), cls=SetEncoder))
                f.close()
                print("-"*100)
                print("-> written to %s"%JsonFileName)
            except:
                print("could not write json file: '%s'!"%JsonFileName)

        HTML = "<a href='%s'>%s</a><br />"%(self.GetStorageKey()+'/'+self.HTMLFileName, self.Attributes['Title'])
        return HTML


from flask import Flask, render_template,redirect, url_for, request, session
from pymongo import MongoClient
from bson import ObjectId
from datetime import datetime
import hashlib
import time
import mysql.connector

mydb = mysql.connector.connect(
	host="localhost",
	user="root",
	password="",
	database="dz67"
)

app = Flask(__name__)
app.config['SECRET_KEY'] = 'januar2020'

@app.route('/')
@app.route('/raspored')
def prva():
	mc = mydb.cursor()
	mc.execute("SELECT * FROM raspored")
	res = mc.fetchall()
	mc.execute("SELECT DISTINCT nastavnik FROM raspored")
	a = mc.fetchall()
	mc.execute("SELECT DISTINCT vreme FROM raspored")
	b = mc.fetchall()

	return render_template("raspored.html",raspored=res,jed=a,jed2=b)

if __name__ == '__main__':
	app.run(debug=True)


class RandomizedSamplingSVM(object):
	def __init__(self, svm_parameters={}):
		self.svm_parameters = svm_parameters
		self.model = None

	def set_svm_params(self, svm_parameters={}):
		self.svm_parameters = svm_parameters

	def __union_set(self, samples, new_index):
		index = np.array([],dtype=np.int64)
		N = len(samples)
		for i in range(N):
			sample = samples[i]
			ind = new_index[i]
			index = np.union1d(index, sample[ind])
		return index.tolist()

	def __create_subsamples(self, N, m, k):
		ind = []
		x = 0
		P = np.random.permutation(N)

		for i in range(m):
			sub = np.array([])
			if x + k < N:
				sub = P[x:(x + k)]
				x = x + k
			else:
				sub = P[x:N]
				P = np.random.permutation(N)
				sub = np.append(sub, P[0:k - N + x])
				x = k - N + x
			ind.append(sub)
		return ind

	def __get_support_vectors(self, X, y):
		svc = SVC(**self.svm_parameters)
		svc.fit(X, y)
		return svc.support_

	def train(self, X_init, y_init, beta, g):
		c = 1
		i = 0
		X = X_init
		y = y_init
		N = X.shape[0]
		n = [N]
		while True:
			i = i + 1
			k = math.ceil(i*beta*N)
			m = math.ceil(n[i-1] * g / k)
			subsamples = self.__create_subsamples(n[i-1], m, k)
			index = []
			for sample in subsamples:
				index.append(self.__get_support_vectors(X[sample,],y[sample,]))
			new_X_index = self.__union_set(subsamples,index)
			X = X[new_X_index,]
			y = y[new_X_index,]
			n.append(X.shape[0])
			if  g*n[i]*k/c >= (n[i-1]-n[i])**2:
				break
		svc = SVC(**self.svm_parameters)
		self.model = svc.fit(X,y)
		return self.model


# -*- coding: utf-8 -*-
"""
Created on Sat Sep 16 14:47:26 2017

@author: 王
"""

#encoding=utf-8
import os
import tensorflow as tf
from PIL import Image
import numpy as np
import time
start=time.time()

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
    #x是输入，W是卷积的参数，如[5,5,1,32]:前面两个数字代表卷积核的尺寸，第三个表示channel
    #灰度图就是1，RGB就是3，最后一个数字代表卷积核的数量，就是卷积层提取多少类的特征。
    #Strides代表卷积模板移动的步长，[1,1,1,1]代表不遗漏每个点
    #padding代表处理的方式，AAME代表给边界上加上Padding让卷积的输出和输入保持同样尺寸
    #因此卷积前后图像尺寸不变，而池化时候减小
def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
    #最大池化函数，对k*k，是ksize=[1, k, k, 1],strides=[1, k, k, 1]

cwd = os.getcwd()

classes = {'classOne','classTwo','classThree'}
#制作二进制数据
def create_record():
    writer = tf.python_io.TFRecordWriter("train.tfrecords")
    for index, name in enumerate(classes):
        class_path = cwd +"/"+ name+"/"
        for img_name in os.listdir(class_path):
            img_path = class_path + img_name
            img = Image.open(img_path)
            img = img.resize((64, 64))
            img_raw = img.tobytes() #将图片转化为原生bytes
            print(index,img_raw)
            example = tf.train.Example(
               features=tf.train.Features(feature={
                    "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
                    'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
               }))
            writer.write(example.SerializeToString())
    writer.close()

data = create_record()

#读取二进制数据
def read_and_decode(filename):
    # 创建文件队列,不限读取的数量
    filename_queue = tf.train.string_input_producer([filename])
    # create a reader from file queue
    reader = tf.TFRecordReader()
    # reader从文件队列中读入一个序列化的样本
    _, serialized_example = reader.read(filename_queue)
    # get feature from serialized example
    # 解析符号化的样本
    features = tf.parse_single_example(
        serialized_example,
        features={
            'label': tf.FixedLenFeature([], tf.int64),
            'img_raw': tf.FixedLenFeature([], tf.string)
        }
    )
    label = features['label']
    img = features['img_raw']
    img = tf.decode_raw(img, tf.uint8)
    #############################################################################
    img = tf.reshape(img, [64, 64, 3])
    img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
    label = tf.cast(label, tf.int32)
    return img, label

if __name__ == '__main__':
    if 0:
        data = create_record("train.tfrecords")
    else:
        img, label = read_and_decode("train.tfrecords")
        print(img,label)
        #使用shuffle_batch可以随机打乱输入 next_batch挨着往下取
        # shuffle_batch才能实现[img,label]的同步,也即特征和label的同步,不然可能输入的特征和label不匹配
        # 比如只有这样使用,才能使img和label一一对应,每次提取一个image和对应的label
        # shuffle_batch返回的值就是RandomShuffleQueue.dequeue_many()的结果
        # Shuffle_batch构建了一个RandomShuffleQueue，并不断地把单个的[img,label],送入队列中
        img_batch, label_batch = tf.train.shuffle_batch([img, label],
                                                    batch_size=4, capacity=2000,
                                                    min_after_dequeue=1000)

        '''
        # 初始化所有的op
        init =tf.global_variables_initializer()

        sess = tf.Session()
        sess.run(init)
        # 启动队列
        threads = tf.train.start_queue_runners(sess=sess)
        for i in range(5):
            print(img_batch.get_shape(),label_batch)
            #修改img_batch.shape为img_batch.get_shape()
            val, l = sess.run([img_batch, label_batch])
            # l = to_categorical(l, 12)
            print(val.shape, l)
        '''
    print('开始构建图......')
    sess = tf.InteractiveSession()
    x = tf.placeholder(tf.float32, shape=[None, 64*64])#tf.float32, [None, w*h] x为特征
    y_ = tf.placeholder(tf.float32, shape=[None, 3])#Y_是真实的label
    # [None, ysize]) y的数目个数 比如3个类 就是3
    x_image = tf.reshape(x, [-1,64,64,3])
    #将输入的1*784形式转为图片原始的28*28结构 tf.reshape
    #第2、第3维对应图片的宽、高，最后一维代表图片的颜色通道数
    #(因为是灰度图所以这里的通道数为1，如果是rgb彩色图，则为3)
    #-1代表样本数量不顾的
    ############# 第一层卷积 #################
    W_conv1 = weight_variable([5, 5, 3, 32])
    #5*5的卷积核(patch)，1是输入的通道数目，32是32个不同的卷积核，算出32个特征
    b_conv1 = bias_variable([32])
    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
    #卷积
    h_pool1 = max_pool_2x2(h_conv1)
    #池化
    ############# 第二层卷积 ################
    W_conv2 = weight_variable([5, 5, 32, 64])
    #32个输入，64个输出
    b_conv2 = bias_variable([64])
    h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
    #第一层卷积所得的h_pool1作为输入
    h_pool2 = max_pool_2x2(h_conv2)

    ############ 密集连接层 #################
    W_fc1 = weight_variable([7 * 7 * 64, 1024])
    b_fc1 = bias_variable([1024])
    h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
    ########### Dropout层 ##################
    keep_prob = tf.placeholder(tf.float32)
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
    ########### Softmax层 (输出) ############
    W_fc2 = weight_variable([1024, 3])
    b_fc2 = bias_variable([3])
    y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
    #W = tf.Variable(tf.zeros([784,10]))
    #b = tf.Variable(tf.zeros([10]))

    #训练和评估模型
    #定义损失函数和优化器
    cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    #定义评测准确率的操作
    correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    print('开始执行op......')
    sess.run(tf.global_variables_initializer())

    threads = tf.train.start_queue_runners(sess=sess)

    for i in range(1000):
        batch = img_batch
        #if i%10 == 0:
        #   train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
        #    print("step %d, training accuracy %g"%(i, train_accuracy))
        train_step.run(feed_dict={x: batch[0].eval(), y_: batch[1].eval(), keep_prob: 0.5})

    #print("test accuracy %g"%accuracy.eval(feed_dict={
    #    x: img_batch, y_: label_batch, keep_prob: 1.0}))

    end=time.time()
    print('运行时长：',(end-start)/60,'分钟')


import logging
import os
import random
import datetime
import time
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, BaseFilter
from telegram import Message, User, Chat, MessageEntity, Document
from random import randint
import duel

games = ['R6','R6','R6', 'RL', 'RL', 'RL', 'Apex']
stickerCount = {}
englishCount = {}
baseClock = time.time()
msgCount = {}
floodStat = {}
floodClock = time.time()

def start(bot, update):
    update.message.reply_text("GG ?")

def randomgame(bot, update):
    update.message.reply_text("GG "+ random.choice(games))

def roll(bot, update):
    update.message.reply_text("GG "+ str(randint(0,100)))

def isEnglish(text):
    countEnglishLetters = 0
    for c in text:
        if ord(c) < 128 and c.isalpha():
            countEnglishLetters += 1
    return countEnglishLetters/len(text) >0.5

def processText(bot, update):
    global englishCount
    global baseClock
    print("new Text Upate : "+str(update))

    user = update.effective_user
    username = user['username']
    print('user name'+ str(username))
    print('update.message.text '+ update.message.text + 'isenglish: '+str(isEnglish(update.message.text)))
    if isEnglish(update.message.text):
        englishCount[username] = englishCount.setdefault(username, 0) + 1

    timenow = time.time()
    temp = timenow-baseClock
    hours = temp//3600
    if(hours>5):
        baseClock = timenow
        englishCount = {}

    if(englishCount.setdefault(username, 0)>10) and isEnglish(update.message.text):
        bot.delete_message(update.message.chat.id, update.message.message_id)

def processSticker(bot, update):
    global stickerCount
    global baseClock
    print("sticker update"+str(update))

    user = update.effective_user
    username = user['username']

    stickerCount[username] = stickerCount.setdefault(username, 0) + 1

    timenow = time.time()
    temp = timenow-baseClock
    hours = temp//3600
    if(hours>5):
        baseClock = timenow
        stickerCount = {}

    if(englishCount.setdefault(username, 0)>5):
        bot.delete_message(update.message.chat.id, update.message.message_id)

def antiFlood(bot, update):
    global msgCount
    global floodClock

    print("All Filter update"+str(update))

    user = update.effective_user
    username = user['username']
    msgCount[username] = msgCount.setdefault(username, 0) + 1

    timenow = time.time()

    temp = timenow - floodClock
    if(temp < 10 and msgCount.setdefault(username, 0) > 10 ):
        bot.delete_message(update.message.chat.id, update.message.message_id)
        floodStat[username] = True

    if(temp > 10):
        floodClock = time.time()
        floodStat[username] = False
        msgCount[username] = 0

def error(bot, update, error):
    logger.warning('Update "%s" caused error "%s"', update, error)

if __name__ == "__main__":
    # Set these variable to the appropriate values
    TOKEN = os.environ.get('TOKEN')
    NAME = "gg-manager-bot"

    # Port is given by Heroku
    PORT = os.environ.get('PORT')

    # Enable logging
    logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',level=logging.INFO)
    logger = logging.getLogger(__name__)

    # Set up the Updater
    updater = Updater(TOKEN)
    dp = updater.dispatcher
    # Add handlers
    dp.add_handler(CommandHandler('start', start))
    dp.add_handler(CommandHandler('roll', roll))
    dp.add_handler(CommandHandler('randomgame', randomgame))
    dp.add_handler(CommandHandler('start_duel', duel.start_duel))
    dp.add_handler(CommandHandler('shot!', duel.shot))
    dp.add_handler(MessageHandler((Filters.text & (~ Filters.entity(MessageEntity.MENTION))), processText))
    dp.add_handler(MessageHandler((Filters.sticker | Filters.animation), processSticker))
    dp.add_handler(MessageHandler(Filters.all, antiFlood))

    dp.add_error_handler(error)

    # Start the webhook
    updater.start_webhook(listen="0.0.0.0",port=int(PORT),url_path=TOKEN)
    updater.bot.setWebhook("https://{}.herokuapp.com/{}".format(NAME, TOKEN))

    updater.idle()


from django.urls import path, include
from django.contrib import admin
from . import views

urlpatterns = [
    path('', views.courses, name = 'Courses'),
    path('new_account/', views.new_account, name = 'Create an account'),
    path('create_account/', views.create_account, name = 'Create an account - step 2'),
    path('user/', views.user_page, name = 'User page'),
    path('delete_account/', views.delete_account, name = 'Delete user account'),
    path('course/<course_id>/', views.course_page, name = 'Course page'),
    path('private_courses/', views.private_courses, name = 'Private courses'),
    path('create/', views.create_course, name = 'Create a new course'),
    path('new_course/', views.create_course_follow_up, name = 'Create a new course - step 2'),
    path('edit/<course_id>/', views.edit_course, name = 'Edit course'),
    path('edit/<course_id>/modify_data/', views.modify_course_data, name = 'Modify course data'),
    path('edit/<course_id>/add_level', views.add_level_to_course, name = 'Add a level to a course'),
    path('add_unit/<course_id>/<level_num>/', views.add_unit_to_level, name = 'Add a unit to an existing level of a course'),
    path('edit_new_unit/', views.edit_new_unit, name = 'Edit a newly created unit'),
    path('edit_unit/<unit_id>/', views.edit_unit, name = 'Edit a unit'),
    path('edit_unit/<unit_id>/add_lesson/', views.add_lesson, name = 'Add a lesson to a unit'),
    path('edit_unit/<unit_id>/<lesson_id>/', views.edit_lesson, name = 'Edit a lesson of a unit'),
    path('exercise_types/', views.exercise_types, name = 'A listing of the available sorts of exercises'),
    path('edit_unit/<unit_id>/<lesson_id>/add_exercise/', views.add_exercise, name = 'Add an exercise to a lesson'),
    path('edit_unit/<unit_id>/<lesson_id>/exercise_created/', views.exercise_created, name = 'Used right after a new exercise is created'),
    path('edit_unit/<unit_id>/<lesson_id>/<exercise_id>/', views.edit_exercise, name = 'Edit an existing exercise'),
    path('edit_unit/<unit_id>/<lesson_id>/<exercise_id>/delete/', views.delete_exercise, name = 'Delete an exercise'),
    path('edit/<course_id>/edit_glosses/', views.edit_glosses, name = 'Edit glosses used in the course'),
    path('unit/<unit_id>/', views.view_unit, name = 'View unit'),
    path('unit/<unit_id>/<lesson_id>/', views.do_lesson, name = 'Do a lesson'),
    path('submit_ticket/', views.submit_ticket, name = 'Submit an error ticket'),
    path('course/<course_id>/submit_ticket/', views.submit_ticket_to_course, name = 'Submit an error ticket for a particular course'),
    path('comments/', views.see_messages, name = 'See comments, report tickets, etc.'),
    path('comment/<exercise_id>/', views.view_exercise_comments, name = 'Read and write comments for an exercise'),
    path('add_new_UI/', views.add_ui_language, name = 'Add an interface language'),
    path('edit_UI/<ui_lang_id>/', views.edit_ui_language, name = 'Edit an interface language'),
    path('special/', views.special, name = 'Used for development purposes'),
    #path('accounts/', include('django.contrib.auth.urls')),
]

admin.site.site_header = 'Erkonlang admin'
admin.site.site_title = 'Erkonlang admin'


#!/usr/bin/env python3

from argparse import ArgumentParser
from operator import itemgetter
from collections import OrderedDict
import pathlib

def loaddata(filepath):
    data = {}
    fpath = pathlib.Path(filepath).resolve()
    with open(fpath) as fh:
        for line in fh:
            cols = line.split()
            target = cols[0].split('_')[0]
            try:
                data[target].append((cols[0], float(cols[1])))
            except KeyError:
                data[target] = [(cols[0], float(cols[1]))]
    return data

def output(data):
    text = []
    for target in data:
        text.append('Target: {}'.format(target))
        subdata = data[target]
        text.append('\tHighest GDT score: '
                    '\n\t\t{}, {}'.format(subdata[0][0],
                                          subdata[0][1]))
        text.append('\tHighest alignment score:')
        for cand in subdata[1:]:
            text.append('\t\t{}, {}, GDT={}, '
                        'diff={:.2f}, rank={}'.format(*cand))
        text.append('\n')
    print('\n'.join(text))

def compare(gdt, other, howmany=1):
    #Ensure gdt and other share a common target set
    target1 = set(gdt.keys())
    target2 = set(other.keys())
    for key in target1 - target2:
        gdt.pop(key)
    for key in target2 - target1:
        other.pop(key)

    results = {}
    for target in gdt:
        first = sorted(gdt[target], key=itemgetter(1), reverse=True)
        firstod = OrderedDict(first)
        second = sorted(other[target], key=itemgetter(1), reverse=True)
        best1 = first[0]
        data = [(best1[0], best1[1], best1[1], 0 , 1)]
        i = 0
        for cand in second:
            candname = cand[0]
            candscore = cand[1]
            try:
                gdtscore = firstod[candname]
                diff = best1[1] - firstod[candname]
                rank = first.index((candname, gdtscore)) + 1
            except KeyError:
                continue
            data.append((
                candname, candscore, gdtscore, diff, rank
            ))
            i += 1
            if i == howmany:
                break
        results[target] = data

    return results

def run(gdtfile, otherfile, howmany=1):
    data = compare(loaddata(gdtfile), loaddata(otherfile), howmany)
    output(data)

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('gdtfile', help='GDT score file')
    parser.add_argument('otherfile',
                        help='File to be compared to GDT file')
    parser.add_argument('-m', '--howmany', type=int, default=1,
                        help='How many resuts from the other file '
                        'to display')
    args = parser.parse_args()
    run(args.gdtfile, args.otherfile, args.howmany)


import torch
import torch.nn as nn
import torch.nn.functional as F

from horch.models.modules import get_norm_layer, Conv2d, get_activation

from horch.nas.darts.operations import OPS, FactorizedReduce, ReLUConvBN
from horch.nas.darts.genotypes import PRIMITIVES, Genotype

class MixedOp(nn.Module):

    def __init__(self, C):
        super().__init__()
        self._ops = nn.ModuleList()
        for primitive in PRIMITIVES:
            op = OPS[primitive](C, 1)
            if 'pool' in primitive:
                op = nn.Sequential(op, get_norm_layer(C))
            self._ops.append(op)

    def forward(self, x, hardwts, index):
        return sum(hardwts[i] * op(x) if i == index else hardwts[i] for i, op in enumerate(self._ops))

class NormalCell(nn.Module):

    def __init__(self, steps, multiplier, C_prev_prev, C_prev, C, reduction_prev):
        super().__init__()

        if reduction_prev:
            self.preprocess0 = FactorizedReduce(C_prev_prev, C)
        else:
            self.preprocess0 = ReLUConvBN(C_prev_prev, C, 1)
        self.preprocess1 = ReLUConvBN(C_prev, C, 1)
        self._steps = steps
        self._multiplier = multiplier

        self._ops = nn.ModuleList()
        for i in range(self._steps):
            ops = nn.ModuleList()
            for j in range(2 + i):
                op = MixedOp(C)
                ops.append(op)
            self._ops.append(ops)

    def forward(self, s0, s1, hardwts, indices):
        s0 = self.preprocess0(s0)
        s1 = self.preprocess1(s1)

        states = [s0, s1]
        for i in range(self._steps):
            s = sum(self._ops[i][j](h, hardwts[i][j], int(indices[i][j])) for j, h in enumerate(states))
            states.append(s)

        return torch.cat(states[-self._multiplier:], dim=1)

class ReductionCell(nn.Module):

    def __init__(self, C_prev_prev, C_prev, C):
        super().__init__()
        self.preprocess0 = ReLUConvBN(C_prev_prev, C, 1)
        self.preprocess1 = ReLUConvBN(C_prev, C, 1)

        self.branch_a1 = nn.Sequential(
            get_activation(),
            Conv2d(C, C, (1, 3), stride=(1, 2), groups=8, bias=False),
            Conv2d(C, C, (3, 1), stride=(2, 1), groups=8, bias=False),
            get_norm_layer(C, affine=True),
            get_activation(),
            Conv2d(C, C, 1),
            get_norm_layer(C, affine=True),
        )
        self.branch_a2 = nn.Sequential(
            nn.MaxPool2d(3, stride=2, padding=1),
            get_norm_layer(C, affine=True)
        )
        self.branch_b1 = nn.Sequential(
            get_activation(),
            Conv2d(C, C, (1, 3), stride=(1, 2), groups=8, bias=False),
            Conv2d(C, C, (3, 1), stride=(2, 1), groups=8, bias=False),
            get_norm_layer(C, affine=True),
            get_activation(),
            Conv2d(C, C, 1),
            get_norm_layer(C, affine=True),
        )
        self.branch_b2 = nn.Sequential(
            nn.MaxPool2d(3, stride=2, padding=1),
            get_norm_layer(C, affine=True)
        )

    def forward(self, s0, s1, *args):
        s0 = self.preprocess0(s0)
        s1 = self.preprocess1(s1)

        x0 = self.branch_a1(s0)
        x1 = self.branch_a2(s1)
        # if self.training and drop_prob > 0.:
        #     X0, X1 = drop_path(X0, drop_prob), drop_path(X1, drop_prob)

        x2 = self.branch_b1(s0)
        x3 = self.branch_b2(s1)
        # if self.training and drop_prob > 0.:
        #     X2, X3 = drop_path(X2, drop_prob), drop_path(X3, drop_prob)

        return torch.cat([x0, x1, x2, x3], dim=1)

def gumbel_sample(a, tau):
    while True:
        gumbels = -torch.empty_like(a).exponential_().log()
        logits = (a.log_softmax(dim=1) + gumbels) / tau
        probs = nn.functional.softmax(logits, dim=1)
        index = probs.max(-1, keepdim=True)[1]
        one_h = torch.zeros_like(logits).scatter_(-1, index, 1.0)
        hardwts = one_h - probs.detach() + probs
        if (torch.isinf(gumbels).any()) or (torch.isinf(probs).any()) or (torch.isnan(probs).any()):
            continue
        else:
            return hardwts, [int(i) for i in index.cpu().numpy()]

class Network(nn.Module):

    def __init__(self, C, layers, steps=4, multiplier=4, stem_multiplier=3, num_classes=10, tau=10.0):
        super().__init__()
        self.C = C
        self.num_classes = num_classes
        self.layers = layers
        self.steps = steps
        self.multiplier = multiplier
        self.tau = tau

        C_curr = stem_multiplier * C
        self.stem = nn.Sequential(
            Conv2d(3, C_curr, kernel_size=3, bias=False),
            get_norm_layer(C_curr),
        )

        C_prev_prev, C_prev, C_curr = C_curr, C_curr, C
        self.cells = nn.ModuleList()
        reduction_prev = False
        for i in range(layers):
            if i in [layers // 3, 2 * layers // 3]:
                C_curr *= 2
                reduction = True
            else:
                reduction = False
            if reduction:
                cell = ReductionCell(C_prev_prev, C_prev, C_curr)
            else:
                cell = NormalCell(steps, multiplier, C_prev_prev, C_prev, C_curr, reduction_prev)
            reduction_prev = reduction
            self.cells.append(cell)
            C_prev_prev, C_prev = C_prev, multiplier * C_curr

        self.post_activ = nn.Sequential(
            get_norm_layer(C_prev),
            get_activation(),
        )
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.classifier = nn.Linear(C_prev, num_classes)

        self._initialize_alphas()

    def forward(self, x):
        hardwts, indices = zip(*[ gumbel_sample(a, self.tau) for a in self.alphas ])
        s0 = s1 = self.stem(x)
        for cell in self.cells:
            s0, s1 = s1, cell(s0, s1, hardwts, indices)
        s1 = self.post_activ(s1)
        out = self.avg_pool(s1)
        logits = self.classifier(out.view(out.size(0), -1))
        return logits

    def _initialize_alphas(self):
        num_ops = len(PRIMITIVES)

        self.alphas = nn.ParameterList([
            nn.Parameter(1e-3 * torch.randn(i + 2, num_ops), requires_grad=True)
            for i in range(self.steps)
        ])

    def model_parameters(self):
        ids = set(id(p) for p in self.arch_parameters())
        for p in self.parameters():
            if id(p) not in ids:
                yield p

    def arch_parameters(self):
        return self.alphas.parameters()

    def genotype(self):

        def _parse(weights):
            gene = []
            for i in range(self.steps):
                W = weights[i].copy()
                edges = sorted(
                    range(i + 2),
                    key=lambda x: -max(W[x][k] for k in range(len(W[x])) if k != PRIMITIVES.index('none')))[:2]
                for j in edges:
                    k_best = None
                    for k in range(len(W[j])):
                        if k != PRIMITIVES.index('none'):
                            if k_best is None or W[j][k] > W[j][k_best]:
                                k_best = k
                    gene.append((PRIMITIVES[k_best], j))
            return gene

        gene_normal = _parse([F.softmax(w.detach().cpu(), dim=0).numpy() for w in self.alphas])

        concat = range(2 + self.steps - self.multiplier, self.steps + 2)
        genotype = Genotype(
            normal=gene_normal, normal_concat=concat,
        )
        return genotype


from flask_restful import Resource, reqparse
from flask_jwt import jwt_required
from models.item import ItemModel

class Item(Resource):  # Defining the resource
    parser = reqparse.RequestParser()
        # price and store_id are gonna be the only field accepted, any other entered is gonna
        # be deleted and not passed to the api. IT BELONGS TO THE CLASS Item!
        # True for every instance
    parser.add_argument('price',
            type = float,
            required = True,
            help = 'This message cannot be left blank'
            )
    parser.add_argument('store_id',
            type = int,
            required = True,
            help = 'Every Item needs a store_id'
            )

    @jwt_required()
    def get(self, name):
        item = ItemModel.find_by_name(name)
        if item:
            return item.json()

        return {'message':'Item not found'}, 404

    def post(self, name):
        '''
        Creates an item. Can use force=True or silent=True in get_json().
        force skip read the header content-type, silent doesnt return an error
        but just 'None'
        '''
        if ItemModel.find_by_name(name):
            return {'message':"Item named '{}' already exists".format(name)}, 400

        data = Item.parser.parse_args() # check the data is valid

        item = ItemModel(name, data['price'], data['store_id'])

        try:
            item.save_to_db()
        except:
            return {'message': 'An error occured whem inserting the item'}, 500

        return item.json(), 201

    def delete(self, name):
        item = ItemModel.find_by_name(name)

        if item:
            item.delete_from_db()

        return {'message':'Item has been deleted'}

    def put(self, name):
        data = Item.parser.parse_args() # check the data is valid

        item = ItemModel.find_by_name(name)

        if item is None:
            item = ItemModel(name, data['price'], data['store_id'])
            # could write **data as well
        else:
            item.price = data['price']

        item.save_to_db()

        return item.json()

class ItemList(Resource):

    def get(self):
        items = ItemModel.query.all()
        return {'items': [item.json() for item in ItemModel.query.all()]}


# coding=utf-8

import pytest
from test_module.models.tool import getResults
from test_module.models.TDB import *

class TestOrderdetail():
    """
    订单详情测试
    """
    test_data=[
        ('/order/',"SELECT A.id FROM Order_baseorder A WHERE A.`status`='CONFIRMED' AND A.product_id in (1,7) AND A.contract_id IS NULL LIMIT 1",200),
        ('/order/terminalorder/',"SELECT A.id FROM Order_baseorder A  WHERE A.`status`='CONFIRMED' AND A.product_id in (2,3,4,5,6,8) AND A.contract_id IS NULL LIMIT 3",200),
        ('/order/contract_order/',"SELECT A.id FROM Order_baseorder A  WHERE A.`status`='CONFIRMED'AND A.product_id in (16,17,32,33,48,49) AND A.contract_id IS NOT NULL LIMIT 1",200)
    ]
    #凯励程订单详情
    @pytest.mark.parametrize('url,sql,result1',test_data)
    def test_orderdetail1(self,url,sql,result1,connectDB):
        sql=sql
        cur = connectDB
        data = searchDB(cur, sql)
        if data is None:
            pytest.skip('无数据，用例无效')
        else:
            result = getResults(url+'%s/' % (str(data['id'])))
            assert result.status_code == result1
            response = result.json()
            try:
                assert response['id']==data['id']
            except AssertionError:
                raise AssertionError('订单详情接口失败')


import os, glob, json
from PIL import Image

anno_file = '/media/irelin/data_disk/dataset/afp/noseprint_recognition/result.json'
image_root = '/media/irelin/data_disk/dataset/afp/noseprint_recognition/595_dogs_nose'
output_dir = '/media/irelin/data_disk/dataset/afp/noseprint_recognition/nose_split_choice'

annos = json.load(open(anno_file))

for i, anno in enumerate(annos):
    im_fn = os.path.basename(anno['data']['image']).split('-')[1]
    found = glob.glob(os.path.join(image_root, "*" + im_fn))
    if not found:
        print("not exists")
        continue
    im_path = found[0]
    im_dir = "_".join(im_fn.split("_")[:2])
    print(i, len(annos), im_path)
    anno_w = anno["completions"][0]["result"][0]["original_width"]
    anno_h = anno["completions"][0]["result"][0]["original_height"]
    im = Image.open(im_path)
    w, h = im.size
    if w != anno_w or h != anno_h:
        print('not same size')
        continue

    bbox_w = float(anno["completions"][0]["result"][0]["value"]["width"])
    bbox_h = float(anno["completions"][0]["result"][0]["value"]["height"])
    x1 = float(anno["completions"][0]["result"][0]["value"]["x"])
    y1 = float(anno["completions"][0]["result"][0]["value"]["y"])
    x2 = x1 + bbox_w
    y2 = y1 + bbox_h

    x1 = x1 / 100 * w
    x2 = x2 / 100 * w
    y1 = y1 / 100 * h
    y2 = y2 / 100 * h

    im = im.crop((x1, y1, x2, y2))
    os.makedirs(os.path.join(output_dir, im_dir), exist_ok=True)
    output_path = os.path.join(output_dir, im_dir, os.path.splitext(im_fn)[0] + ".jpg")
    im.save(output_path, quality=100)


# from https://www.rototron.info/raspberry-pi-esp32-micropython-web-server-tutorial/
# modified for MicroWebSrv2

from time import sleep
from MicroWebSrv2 import *
from machine import Pin
from neopixel import NeoPixel
import json

np = NeoPixel(Pin(13), 1)

@WebRoute(POST, '/led')
def RequestLedGet(microWebSrv2, request):
    content = request.Content  # Read JSON color data
    colors = json.loads(content)
    blue, green, red = [colors[k] for k in sorted(colors.keys())]
    np[0] = (red, green, blue)
    np.write()
    response = request.Response
    response.ReturnOk()

mws2 = MicroWebSrv2()

# For embedded MicroPython, use a very light configuration,
mws2.SetEmbeddedConfig()

# Blake 2020-08-09 set root path for ESP32
mws2.RootPath = '/www/'

mws2.StartManaged()

# Main program loop until keyboard interrupt,
try:
    while mws2.IsRunning:
        sleep(1)
except KeyboardInterrupt:
    pass

# End,
print()
mws2.Stop()
print('Bye')
print()


from PyQt5.QtWidgets import (QWidget, QLabel, QListWidget, QPushButton,
                             QVBoxLayout, QSplitter, QGroupBox, QRadioButton,
                             QAbstractItemView, QPlainTextEdit)
from PyQt5.QtCore import pyqtSignal, Qt
from mooda import WaterFrame
from mooda.access import EGIM
from mooda.app.mooda_gui.widgets import (DropWidget, QCWidget, RenameWidget,
                                         ResampleWidget, SliceWidget,
                                         ScatterMatrixPlotWidget, QCPlotWidget,
                                         TSPlotWidget, QCBarPlotWidget,
                                         SpectrogramPlotWidget)
from mooda.app.mooda_gui.widgets.histoplotwidget import HistoPlotWidget

class PlotSplitter(QSplitter):
    """Pyqt5 widget to show data in plots and lists."""

    # Signals
    msg2Statusbar = pyqtSignal(str)
    msg2TextArea = pyqtSignal(str)

    def __init__(self):
        """
        Constructor
        """
        super().__init__()

        # Instance variables
        self.wf = WaterFrame()
        # List of PlotWidget, to control them in any case
        self.plotWidgetList = []

        self.initUI()

    def initUI(self):
        """UI creator"""

        # Lists
        self.dataList = QListWidget(self)
        self.dataList.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.dataList.itemClicked.connect(self.dataListClick)
        self.graphList = QListWidget(self)
        self.graphList.itemClicked.connect(self.graphClick)

        # PlainTextEdit
        self.otherInfoPlainText = QPlainTextEdit(self)
        self.metadataPlainText = QPlainTextEdit(self)

        # Labels
        metadataLabel = QLabel("Metadata")
        dataLabel = QLabel("Data")
        graphLabel = QLabel("Graph")
        infoLabel = QLabel("Other information")

        # DropWidget
        self.dropWidget = DropWidget()
        self.dropWidget.list2drop[list, list, bool].connect(self.dropData)
        self.dropWidget.hide()

        # Group Box
        plotGroupBox = QGroupBox("Plot properties", self)
        vPlotGroupBox = QVBoxLayout()
        # - RadioButton
        self.autoPlotRadioButton = QRadioButton("Time series plot", self)
        self.autoPlotRadioButton.setChecked(True)
        self.multipleParameterRadioButton = QRadioButton("Multiparameter", self)
        self.correlationRadioButton = QRadioButton("Correlation", self)
        self.histogramRadioButton = QRadioButton("Histogram", self)
        self.parameterQCRadioButton = QRadioButton("QC of the parameter", self)
        vPlotGroupBox.addWidget(self.autoPlotRadioButton)
        vPlotGroupBox.addWidget(self.histogramRadioButton)
        vPlotGroupBox.addWidget(self.multipleParameterRadioButton)
        vPlotGroupBox.addWidget(self.correlationRadioButton)
        vPlotGroupBox.addWidget(self.parameterQCRadioButton)
        plotGroupBox.setLayout(vPlotGroupBox)

        # QCWidget
        self.qcWidget = QCWidget()
        self.qcWidget.list2qc[list].connect(self.applyQC)
        self.qcWidget.hide()

        # RenameWidget
        self.renameWidget = RenameWidget()
        self.renameWidget.key2change[str, str].connect(self.applyRename)
        self.renameWidget.hide()

        # ResampleWidget
        self.resampleWidget = ResampleWidget()
        self.resampleWidget.resampleRule[str].connect(self.applyResample)
        self.resampleWidget.hide()

        # SliceWidget
        self.sliceWidget = SliceWidget()
        self.sliceWidget.sliceTimes[str, str].connect(self.applySlice)
        self.sliceWidget.hide()

        # Splitters
        self.vDataSplitter = QSplitter(Qt.Vertical)
        # Custom Widget Metadata
        self.vMetadataWidget = QWidget()

        # Buttons
        # - For metadata area
        hideMetadataButton = QPushButton("Hide")
        hideMetadataButton.clicked.connect(self.vMetadataWidget.hide)
        # - For data Area
        plotButton = QPushButton("Plot")
        plotButton.clicked.connect(self.addPlot)
        hideDataButton = QPushButton("Hide")
        hideDataButton.clicked.connect(self.vDataSplitter.hide)

        # Custom Widget Data
        # - Data Widget
        parameterWidget = QWidget()
        # -- Layout
        vData = QVBoxLayout()
        vData.addWidget(dataLabel)
        vData.addWidget(self.dataList)
        vData.addWidget(plotGroupBox)
        vData.addWidget(plotButton)
        parameterWidget.setLayout(vData)
        # - Graph Widget
        graphWidget = QWidget()
        # -- Layout
        vGraph = QVBoxLayout()
        vGraph.addWidget(graphLabel)
        vGraph.addWidget(self.graphList)
        vGraph.addWidget(hideDataButton)
        graphWidget.setLayout(vGraph)
        # - Data splitter
        self.vDataSplitter.addWidget(parameterWidget)
        self.vDataSplitter.addWidget(graphWidget)

        # Layouts
        # - Metadata -
        vMetadata = QVBoxLayout()
        vMetadata.addWidget(metadataLabel)
        vMetadata.addWidget(self.metadataPlainText)
        vMetadata.addWidget(infoLabel)
        vMetadata.addWidget(self.otherInfoPlainText)
        vMetadata.addWidget(hideMetadataButton)
        self.vMetadataWidget.setLayout(vMetadata)

        # Splitter (self)
        # - Layout for actions
        vActionsWidget = QWidget()
        vActions = QVBoxLayout()
        vActions.addWidget(self.renameWidget)
        vActions.addWidget(self.resampleWidget)
        vActions.addWidget(self.dropWidget)
        vActions.addWidget(self.sliceWidget)
        vActions.addStretch()
        vActionsWidget.setLayout(vActions)
        # - Add to self (splitter)
        self.addWidget(self.qcWidget)
        self.addWidget(vActionsWidget)
        self.addWidget(self.vMetadataWidget)
        self.addWidget(self.vDataSplitter)

    def dataListClick(self):
        if (self.autoPlotRadioButton.isChecked() or
        self.histogramRadioButton.isChecked()):
            self.addPlot()

    def addPlot(self):
        """It creates a FigureCanvas with the input figure"""

        self.msg2Statusbar.emit("Making the figure")

        # Create key list
        keys = [item.text() for item in self.dataList.selectedItems()]

        # If nothing is selected, go out
        if not keys:
            self.msg2Statusbar.emit("Ready")
            return

        # Check if it is a QC plot
        if self.parameterQCRadioButton.isChecked():
            keys = [keys[0] + "_QC"]

        # Check if it is a correlation
        if self.correlationRadioButton.isChecked():
            plotWidget = ScatterMatrixPlotWidget(wf=self.wf, keys=keys)
            self.addWidget(plotWidget)
            return

        # Create name of the plot
        name = '_'.join(keys)

        if self.histogramRadioButton.isChecked():
            name = "hist_" + name

        # Check if plot is done
        new = True
        for plotWidget in self.plotWidgetList:
            if plotWidget.name == name:
                if ~plotWidget.isVisible():
                    plotWidget.refreshPlot()
                    plotWidget.show()
                new = False
                break

        # Create the plot if is new
        if new:
            if len(keys) == 1 and "_QC" in keys[0]:
                plotWidget = QCPlotWidget(wf=self.wf, key=keys[0])
            else:
                if self.histogramRadioButton.isChecked():
                    plotWidget = HistoPlotWidget(wf=self.wf, keys=keys)
                else:
                    plotWidget = TSPlotWidget(wf=self.wf, keys=keys)
                plotWidget.msg2Statusbar[str].connect(self.msg2Statusbar.emit)
            self.addWidget(plotWidget)
            # Add the widget to the list
            self.plotWidgetList.append(plotWidget)

        self.msg2Statusbar.emit("Ready")

    def addQCBarPlot(self):
        """
        It creates a FigureCanvas with the input figure (QC)
        """
        self.msg2Statusbar.emit("Making the figure")
        # Check if the plot exists
        plotWidget = None
        for plotWidget_ in self.plotWidgetList:
            if plotWidget_.name == "QC":
                plotWidget = plotWidget_
                plotWidget.wf = self.wf
                plotWidget.refreshPlot()
                plotWidget.show()
                break
        if plotWidget is None:

            plotWidget = QCBarPlotWidget(wf=self.wf)
            self.addWidget(plotWidget)
            # Add the widget to the list
            self.plotWidgetList.append(plotWidget)
        self.msg2Statusbar.emit("Ready")

    def addSpectroPlot(self):
        """It ads the SpectroPlotWidget to the screen"""

        self.msg2Statusbar.emit("Making the figure")
        # Check if the plot exists
        plotWidget = None
        for plotWidget_ in self.plotWidgetList:
            if plotWidget_.name == "Spectrogram":
                plotWidget = plotWidget_
                plotWidget.wf = self.wf
                plotWidget.refreshPlot()
                plotWidget.show()
                break
        if plotWidget is None:
            plotWidget = SpectrogramPlotWidget(wf=self.wf)
            self.addWidget(plotWidget)
            # Add the widget to the list
            self.plotWidgetList.append(plotWidget)
        self.msg2Statusbar.emit("Ready")

    def openData(self, path, concat=False):
        """
        It opens the netcdf of the path.

        Parameters
        ----------
            path: str or WaterFrame
                Path where the file is or WaterFrame object.
            concat: bool, optional (concat = False)
                It adds the new dataframe to the current dataframe.

        Returns
        -------
            True/False: bool
                It indicates if the operation is ok.
        """
        self.msg2Statusbar.emit("Opening data")
        if isinstance(path, str):
            # Obtain type of file
            extension = path.split(".")[-1]
            # Init ok
            ok = False
            wf_new = WaterFrame()
            if extension == "nc":
                ok = wf_new.from_netcdf(path)
            elif extension == "pkl":
                ok = wf_new.from_pickle(path)
            elif extension == "csv":
                try:
                    wf_new = EGIM.from_raw_csv("EMSO-Azores", path)
                    ok = True
                except:
                    ok = False
            if ok:
                # Check if we want actual data
                if not concat:
                    self.newWaterFrame()
                self.wf.concat(wf_new)

                self.msg2TextArea.emit("Working with file {}".format(path))
                # Add metadata information into metadataList
                self.addMetadata(self.wf.metadata)
                # Add other information
                self.otherInfoPlainText.setPlainText(repr(self.wf))
                # Add data information into dataList
                self.addData(self.wf.data)
                # Plot QC
                self.addQCBarPlot()
                self.msg2Statusbar.emit("Ready")
                return True
            else:
                self.msg2Statusbar.emit("Error opening data")
                return False
        else:
            # Path is a WaterFrame

            # Check if there is no data in the waterframe
            if path.data.empty:
                return False

            # Check if it is a dataframe of an acoustic data.
            # In this case, we are going to delete the previous dataframe.
            if "Sequence" in self.wf.data.keys():
                self.wf.clear()
            self.wf.concat(path)

            # Add data information into dataList
            self.addData(self.wf.data)
            self.addMetadata(self.wf.metadata)
            # Plot QC
            self.addQCBarPlot()
            return True

    def addMetadata(self, metadataDict):
        """
        Add Metadata information into self.metadataPlainText
        :param metadataDict: WaterFrame Metadata Dictionary
        """
        # Clear the list
        self.metadataPlainText.clear()

        items = []
        msg = "\nMetadata:"
        for key, value in metadataDict.items():
            items.append("{}: {}".format(key, value))
            msg += "\n- {}: {}".format(key, value)
        self.metadataPlainText.setPlainText(msg[11:])
        # Send a message to the text area
        self.msg2TextArea.emit(msg)

    def addData(self, data):
        """
        Add data names into self.dataList
        :param data: WaterFrame data variable
        """

        def is_acoustic_data(a):
            # will be True also for 'NaN'
            if a == "Sequence":
                return True
            elif a == "Data Points":
                return True
            try:
                float(a)
                return True
            except ValueError:
                return False

        # Clear the list
        self.dataList.clear()
        # Parameter keys (without QC)
        # NO DEPTH in nc files, NO TIME
        keys_to_work = [key for key in data.keys() if 'TIME' not in key
                        if not is_acoustic_data(key)
                        if key + "_QC" in data.keys()]
        self.dataList.addItems(keys_to_work)
        # Add graphs
        self.graphList.clear()
        self.graphList.addItem("QC")
        # Check if we have acoustic data
        for key in data.keys():
            if is_acoustic_data(key):
                self.graphList.addItem("Spectrogram")
                break

        # Add tooltip
        msg = "\nData:"
        for i in range(self.dataList.count()):
            if "_QC" in self.dataList.item(i).text():
                self.dataList.item(i).setToolTip(
                    'QC flags of {}'.format(self.dataList.item(i).text()[:-3]))
            else:
                try:
                    self.dataList.item(i).setToolTip('{} ({})'.format(
                        self.wf.meaning[
                            self.dataList.item(i).text()]['long_name'],
                        self.wf.meaning[
                            self.dataList.item(i).text()]['units']))
                    msg += "\n- {}: {} ({})".format(
                        self.dataList.item(i).text(),
                        self.wf.meaning[
                            self.dataList.item(i).text()]['long_name'],
                        self.wf.meaning[
                            self.dataList.item(i).text()]['units'])
                except KeyError:
                    pass
        # Send a message to the text area
        self.msg2TextArea.emit(msg)
        # Send the labels to the dropWidget
        self.dropWidget.addLabels(keys_to_work)
        self.qcWidget.addLabels(keys_to_work)
        self.renameWidget.addLabels(keys_to_work)
        self.sliceWidget.refresh(self.wf.data.index[0], self.wf.data.index[-1])

    def saveData(self, path):
        """
        Save current data into a pickle file
        :param path: File path
        :return: Bool
        """
        self.msg2Statusbar.emit("Saving data")
        extension = path.split(".")[-1]
        # Init ok
        ok = False
        wf_new = WaterFrame()
        if extension == "nc":
            pass
        elif extension == "pkl":
            ok = self.wf.to_pickle(path)
        elif extension == "csv":
            ok = self.wf.to_csv(path)

        if ok:
            self.msg2TextArea.emit("Data saved on file {}".format(path))
            self.msg2Statusbar.emit("Ready")
        return ok

    def dropData(self, labels, flagList, dropNaN):
        """
        Delete some parameters from self.wf and refresh the lists
        :param labels: list of labels to drop
        :param flagList: list of flags to drop
        :return:
        """
        self.msg2TextArea.emit("Deleting data")

        # This is a trick, delete the list if is a list of None
        if flagList[0] is None:
            flagList = None

        if flagList:
            self.wf.use_only(parameters=labels, flags=[0, 1], dropnan=dropNaN)
        else:
            # Delete the parameters
            self.wf.drop(keys=labels, flags=flagList)
        # Refresh the lists
        self.addData(self.wf.data)

        # Delete plots with the key
        for label in labels:
            for plotWidget in self.plotWidgetList:
                if plotWidget.name == "QC":
                    plotWidget.refreshPlot()
                if label == plotWidget.name or label+"_" in \
                   plotWidget.name or "_"+label in plotWidget.name:
                    plotWidget.deleteLater()
                    self.plotWidgetList.remove(plotWidget)

        # Send message
        msg = ""
        if flagList is None:
            for label in labels:
                if '_QC' in label:
                    continue
                msg += "{} ".format(label)
            msg += "deleted"
        else:
            msg += "Data with QC Flags "
            for flag in flagList:
                msg += "{}, ".format(flag)
            msg += "from "
            for label in labels:
                if '_QC' in label:
                    continue
                msg += "{}, ".format(label)
            msg += "deleted"
        self.msg2TextArea.emit("\n{}".format(msg))
        self.msg2Statusbar.emit(msg)

    def applyQC(self, listQC):
        """
        Apply the QC procedures
        :param listQC:
        :return:
        """

        def doIt(key_in):
            """
            Common part, to not repeat code
            :param key_in: key to apply QC tests
            """
            if '_QC' in key_in:
                return
            if listQC[0]:
                #  Reset flags
                self.msg2Statusbar.emit(
                    "Setting flags from {} to {}".format(key_in, listQC[0]))
                self.wf.reset_flag(key=key_in, flag=int(listQC[0]))
                self.msg2Statusbar.emit("Ready")
            if listQC[3]:
                # Spike test
                threshold = float(listQC[4].replace(',', '.'))
                self.msg2Statusbar.emit(
                    "Applying spike test to "
                    "{}, with rolling window {} and threshold {}".format(
                        key_in, listQC[5], threshold))
                self.wf.spike_test(key=key_in, window=int(listQC[5]),
                                   threshold=threshold, flag=int(listQC[3]))
                self.msg2Statusbar.emit("Ready")
            if listQC[1]:
                # Range test
                self.msg2Statusbar.emit(
                    "Applying range test to {}".format(key_in))
                self.wf.range_test(key=key_in, flag=int(listQC[1]))
                self.msg2Statusbar.emit("Ready")
            if listQC[2]:
                # Flat test
                self.msg2Statusbar.emit(
                    "Applying flat test to"
                    " {}, with rolling window {}".format(key_in, listQC[5]))
                self.wf.flat_test(key=key_in, window=int(listQC[5]),
                                  flag=int(listQC[2]))
                self.msg2Statusbar.emit("Ready")
            if listQC[6]:
                # Flag to flag
                self.msg2Statusbar.emit(
                    "Changing flags of "
                    "{} from {} to {}".format(key_in, listQC[6], listQC[7]))
                self.wf.flag2flag(key=key_in, original_flag=int(listQC[6]),
                                  translated_flag=int(listQC[7]))
                self.msg2Statusbar.emit("Ready")

        self.msg2Statusbar.emit("Creating QC flags")

        if listQC[8] == 'all':
            for key in self.wf.parameters():
                doIt(key_in=key)
        else:
            for i in range(8, len(listQC)):
                key = listQC[i]
                doIt(key_in=key)

        self.msg2Statusbar.emit("Updating graphs")
        # Refresh the QC graph
        for plotWidget in self.plotWidgetList:
            if "QC" in plotWidget.name:

                if plotWidget.isVisible():
                    plotWidget.refreshPlot()
                # Show the QCBarPlot
                elif plotWidget.name == "QC":
                    plotWidget.refreshPlot()
                    plotWidget.show()
        self.msg2Statusbar.emit("Ready")

    def applyRename(self, original_key, new_key):
        """It renames keys from a WaterFrame"""
        # Rename key from the Waterframe
        self.msg2Statusbar.emit(
            "Changing name {} to {}".format(original_key, new_key))
        self.wf.rename(original_key, new_key)
        # Rename the key of the plotWidgets if it process
        for plotWidget in self.plotWidgetList:
            if isinstance(plotWidget.key, list):
                for i, key in enumerate(plotWidget.key):
                    if key == original_key:
                        plotWidget.key[i] = new_key
                        plotWidget.name = plotWidget.name.replace(original_key,
                                                                  new_key)
                        plotWidget.refreshPlot()
            else:
                if plotWidget.name == "QC":
                    plotWidget.refreshPlot()
                elif plotWidget.key == original_key:
                    plotWidget.key = new_key
                    plotWidget.name = new_key
                    plotWidget.refreshPlot()
        # Add data information into dataList
        self.addData(self.wf.data)
        self.msg2Statusbar.emit("Ready")
        self.msg2TextArea.emit(
            "Key name {} changed to {}.".format(original_key, new_key))

    def applyResample(self, rule):
        """
        It applies the resample function to  self.waterframe
        :param rule: Rule to resample.
        """
        self.msg2Statusbar.emit("Resampling data")
        self.wf.resample(rule)
        self.msg2Statusbar.emit("Ready")

    def applySlice(self, start, stop):
        """
        It applies the resample function to  self.waterframe
        :param start: Start time.
        :param stop: Stop time
        """
        self.msg2Statusbar.emit("Slicing data")

        self.wf.slice_time(start, stop)

        self.addData(self.wf.data)
        self.refreshPlots()

        self.msg2Statusbar.emit("Ready")
        self.msg2TextArea.emit(
            "Dataframe sliced from {} to {}.".format(start, stop))

    def graphClick(self, item):
        if item.text() == "QC":
            self.addQCBarPlot()
        elif item.text() == "Spectrogram":
            self.addSpectroPlot()

    def newWaterFrame(self):
        """Create a new WaterFrame object and clean all screens."""
        self.wf = WaterFrame()
        # Delete all plots
        for plotWidget in self.plotWidgetList:
            plotWidget.deleteLater()
        self.plotWidgetList.clear()
        # Hide the widget
        self.hide()

    def refreshPlots(self):
        """It refresh all plots"""
        self.msg2Statusbar.emit("Refreshing plots")

        for plotWidget in self.plotWidgetList:
            try:
                plotWidget.refreshPlot()
            except KeyError:
                self.plotWidgetList.remove(plotWidget)
                plotWidget.hide()
                plotWidget.deleteLater()

        self.msg2Statusbar.emit("Ready")


def listscale(alist):
	tmp = []
	mi = min(alist)
	ma = max(alist)
	for entry in alist:
		tmp.append(scale(entry, mi, ma))
	return tmp

def scale(value, min, max):
	return (value - min) / (max - min)

def rgbify(value):
	if (value < 0.33):
		return [scale(entry, 0, 0.33), 1, 0]
	elif (value >= 0.33 and value < 0.66):
		return [1, 1, scale(value, 0.33, 0.67)]
	else:
		return [1, 1 - scale(value, 0.67, 1)]

def rgbscale(alist):
	print(alist)
	tmp = []
	mi = min(alist)
	ma = max(alist)
	for entry in alist:
		if entry < 0.30:
			tmp.append([0,1,0])
		elif (entry >= 0.30 and entry < 0.60):
			tmp.append(rgbify(scale(entry, mi, ma)))
		else:
			tmp.append([1,1,0])
	return tmp