import numpy as np

import matplotlib.pyplot as plt
from matplotlib import colors
import datetime
from qutip import *
import qutip.control.pulseoptim as pulseoptim

from .miscellaneous import toLatex

def plotExpectation(time,observables,result):
    """
    observables = [{'Hcavity':Hcavity}]
    """
    fig, axs = plt.subplots(len(observables), 1)
    if len(observables)==1: axs = [axs]
    for i, row in enumerate(observables):
        for name, observable in row.items():
            expectation = expect(observable, result.states)
            axs[i].plot(time, expectation, label=name)
        axs[i].legend()
    fig.tight_layout(); plt.show()

def plotWigners(
    rhoList,radius,
    nCol=6,titleList=None, ptraceIndex=None,
):
    if(len(rhoList)<nCol): nCol = len(rhoList)
    x = np.linspace(-radius, radius, 200)
    nRow = int(np.ceil(len(rhoList) / nCol))
    fig, axs = plt.subplots(nRow, nCol,
        figsize=(3*nCol,3*nRow)
    )
    for i, rho in enumerate(rhoList):
        if ptraceIndex is not None:
            rho = ptrace(rho, ptraceIndex)
        W = wigner(rho, x, x)
        row = i // nCol
        col = i %  nCol
        ax = axs[row][col] if nRow > 1 else axs[col]
        ax.contourf(
            x, x, W, 100, cmap=plt.get_cmap('RdBu'),
            norm=colors.Normalize(-.25,.25),
        )
        if titleList: ax.set_title(titleList[i])
    plt.tight_layout(); plt.show()

def makeTitle(t):
        return "t = {:.2f}".format(t)

def plotWignersIntermediateStates(
    time,result,number,
    radius, nCol=6,titleList=None, ptraceIndex=None,
):
    N = len( result.states )
    if number > 1: number = number-1
    step = N // number
    indices = np.arange(0,N,step)
    rhoList = [ket2dm(result.states[i]) for i in indices]
    rhoList.append(result.states[-1])
    titleList = [makeTitle(time[i]) for i in indices]
    titleList.append(makeTitle(time[-1]))
    plotWigners(
        rhoList,radius,
        nCol,titleList, ptraceIndex,
    )

def plotOccupations(
    rhoList,
    nCol=6,titleList=None, ptraceIndex=None,
):
    if(len(rhoList)<nCol): nCol = len(rhoList)
    if ptraceIndex is not None:
        rho0 = ptrace(rhoList[0], ptraceIndex)
    else:
        rho0 = rhoList[0]
    x = np.arange(0,rho0.diag().shape[0])
    nRow = int(np.ceil(len(rhoList) / nCol))
    fig, axs = plt.subplots(nRow, nCol, figsize=(3*nCol,3*nRow))
    for i, rho in enumerate(rhoList):
        if ptraceIndex is not None:
            rho = ptrace(rho, ptraceIndex)
        row = i // nCol
        col = i %  nCol
        ax = axs[row][col] if nRow > 1 else axs[col]
        ax.set_ylim(-0.2, 1.2)
        ax.bar(x, np.abs(rho.diag()))
        if titleList: ax.set_title(titleList[i])
    plt.tight_layout(); plt.show()

def plotOccupationsIntermediateStates(
    time,result,number,
    nCol=6,titleList=None, ptraceIndex=None,
):
    N = len( result.states )
    if number > 1: number = number-1
    step = N // number
    indices = np.arange(0,N,step)
    rhoList = [ket2dm(result.states[i]) for i in indices]
    rhoList.append(result.states[-1])
    titleList = [makeTitle(time[i]) for i in indices]
    titleList.append(makeTitle(time[-1]))
    plotOccupations(
        rhoList,
        nCol,titleList, ptraceIndex,
    )

def plotOptimalControl(result,controlsName,title='title'):
    fig, axs = plt.subplots(2,1)
    initial = result.initial_amps
    final = result.final_amps
    def stack(x,j): return np.hstack((x[:, j], x[-1, j]))
    pulseNames = ['initial','final']
    for i,pulse in enumerate([initial,final]):
        for j in range(pulse.shape[1]):
            axs[i].step(result.time, stack(pulse,j),
                        label=toLatex(controlsName[j]))
        axs[i].set_title(pulseNames[i]+" control")
        axs[i].legend()
    fig.tight_layout(); plt.show()
    print('*'*16 +' '+ title + " summary " + '*'*16)
    print("Final fidelity error {}".format(result.fid_err))
    print("Final gradient normal {}".format(result.grad_norm_final))
    print("Terminated due to {}".format(result.termination_reason))
    print("Number of iterations {}".format(result.num_iter))
    print("Completed in {} HH:MM:SS.US".format(
        datetime.timedelta(seconds=result.wall_time)))


from django.utils.deprecation import MiddlewareMixin
import re
from django.shortcuts import render,HttpResponse
from rbac import models

class Permisson(MiddlewareMixin):

    def process_request(self,request):

        current_path = request.path

        urls=['/','/login/','/reg/','/admin.*/',]
        for i in urls:
            i = '^' + i + '$'
            re_result=re.match(i,current_path)
            if re_result:
                return None

        permisson=request.session.get('permisson_dict')
        if permisson:
            for i in permisson.values():
                for j in i['url']:
                    url = '^' + j + '$'
                    re_result=re.match(url,current_path)

                    if re_result:
                        request.action = i['action']
                        return None
            else:
                return HttpResponse('没有访问权限！！')

        else:
            return HttpResponse('请登录！！')


from PyQt5 import QtCore, QtWidgets
from PyQt5.Qt import *
from PyQt5.QtCore import *

class imageBox(QtWidgets.QLabel):
    signal_refresh_list = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(imageBox, self).__init__(parent)
        self.points = []
        self.wires = []
        self.currentPoint = QPoint(0, 0)
        self.anchorPoint = QPoint(-1, -1)
        self.setCursor(Qt.CrossCursor)
        self.setMouseTracking(True)
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.contextMenu = QMenu(self)
        self.customContextMenuRequested.connect(self.showMenu)
        self.reselectLast = self.contextMenu.addAction('Reselect last')
        self.abandon = self.contextMenu.addAction('Abandon')
        self.apply = self.contextMenu.addAction('Apply')
        self.reselectLast.triggered.connect(self.reselectLastMenu_triggered)
        self.abandon.triggered.connect(self.abandonMenu_triggered)
        self.apply.triggered.connect(self.applyMenu_triggered)

    def showMenu(self):
        self.contextMenu.exec_(QCursor.pos())

    def reselectLastMenu_triggered(self):
        if len(self.points) > 1:
            self.points.pop(-1)
            self.anchorPoint = self.points[len(self.points) - 1]
            self.update()
        elif len(self.points) == 1:
            self.points = []
            self.anchorPoint = QPoint(-1, -1)
            self.update()

    def abandonMenu_triggered(self):
        self.points = []
        self.anchorPoint = QPoint(-1, -1)
        self.update()

    def applyMenu_triggered(self):
        if len(self.points) >= 2:
            self.wires.append(self.points)
            self.points = []
            self.anchorPoint = QPoint(-1, -1)
            self.update()
            self.signal_refresh_list.emit(len(self.wires))

    def mouseMoveEvent(self, QMouseEvent):
        self.currentPoint = QMouseEvent.pos()
        self.update()

    def mousePressEvent(self, QMouseEvent):
        if QMouseEvent.buttons() == QtCore.Qt.LeftButton:
            self.anchorPoint = QMouseEvent.pos()
            self.points.append(self.anchorPoint)
            self.update()

    def paintEvent(self, QPaintEvent):
        p = QPainter(self)
        p.setPen(QPen(Qt.red, 2, Qt.SolidLine))
        font = QFont('宋体', 16, QFont.Bold, True)
        p.setFont(font)
        for idx, w in enumerate(self.wires):
            p.drawPolyline(self.convert2polygon(w))
            p.drawText(w[0].x(), w[0].y(), str(idx))
        if len(self.points) > 1:
            p.drawPolyline(self.convert2polygon(self.points))
        if self.anchorPoint.x() >= 0 and self.anchorPoint.y() >= 0:
            p.drawLine(self.anchorPoint.x(), self.anchorPoint.y(), self.currentPoint.x(), self.currentPoint.y())

    def convert2polygon(self, points):
        return QPolygon(points)

    def deleteAt(self, index):
        self.wires.pop(index)
        self.update()

    def clearAll(self):
        self.points = []
        self.wires = []
        self.currentPoint = QPoint(0, 0)
        self.anchorPoint = QPoint(-1, -1)
        self.update()


from FrameIterator import FrameIterator
import cv2
import numpy as np
from matplotlib import pyplot as plt

from intrinsics import calibrateChessboard, stereoCalibrateChessboard
from segmentation import manual_split, lk_segmentation
from extrinsics import calculate_E_F, rectification
from utils import * # contains utility functions
from parser import make_parser
from os import path

###############################################################################
# Setup Parameters and Parse Arguments
###############################################################################

args = make_parser().parse_args()

opticalflow_path ='data/blender/optical_flow.avi'
intrinsics_path = 'data/blender/calibration/*.png'
temp_path = make_temp_dir('temp')

input_path = 'data/blender/blender.png'
output_path = os.path.join(temp_path,'disparity.png')

# parameters for intrinsics calibration
intrinsics_params = dict(
    chess_size=(5,5),
    tile_size=0.25, # <= 14 mm
    mirror='left',
    verbose=1,
    show=True)

# parameters for lukas kanade calibration
lk_segmentation_params = dict(
    grid_size=100,
    iterater_max=10,
    verbose=1,
    show=True)

###############################################################################
# Intrinsics Calibration
###############################################################################

if args.intrinsic:

    # get mirror segmentation once for all intrinsics images
    mirror_seg_intr = manual_split(FrameIterator(intrinsics_path).first())

    # compute intrinsics matrix K from Chessboard
    intr, extr = stereoCalibrateChessboard(
        filepattern=intrinsics_path,
        split_position=mirror_seg_intr,
        **intrinsics_params)
    K = intr['KL']

else: K=None

###############################################################################
# Image Segmentation: Detect the mirror
###############################################################################

if args.mirror:

    # compute split with Lukas-Kanade optical flow
    mirror_segmentation = lk_segmentation(
        path=opticalflow_path,
        **lk_segmentation_params
        )

    # optical flow frames are at 50% of real resolution
    mirror_segmentation = mirror_segmentation * 2

else: mirror_segmentation=None

###############################################################################
#  Depth Estimation on input image
###############################################################################

# load input image
img = FrameIterator(input_path).first()
img = getDownSampledImg(1, img)
print(f'img.shape={img.shape}')
cv2.imwrite(path.join(temp_path,'00_input.png'), img)

# fill missing values with defaults

if K is None:
    width, height = img.shape[1], img.shape[0]
    K = manual_K(width, height, focal_length_mm=27.9, sensor_width_mm=36)
if mirror_segmentation is None:
    mirror_segmentation = manual_split(img, verbose=1)

height, width, _ = img.shape
# split and flip image according to mirror position into stereo pair
imgL, imgR, maskL, maskR = split_image(img, mirror_segmentation, flip='left', temp_path=temp_path, show=True)
# calculate essential and fundamental matrices as well as the SIFT keypoints

_, F, pts1, pts2 = calculate_E_F(imgL, imgR, K, temp_path)
#if 'extr' in locals():
#    F = extr['F']

print(K)
print(F)

window_name = 'Disparity Computation'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
canvL, canvR = draw_epilines(imgL, imgR, pts1, pts2, F)
canv = draw_stereo(canvL, canvR, path.join(temp_path,'06_epilines_unrect.png'))
cv2.imshow(window_name, canv)
cv2.waitKey(0)

# compute rectified stereo pair
canvL, canvR, = rectification(canvL, canvR, pts1, pts2, F)
canv = draw_stereo(canvL, canvR, path.join(temp_path,'07_epilines_rect.png'))
cv2.imshow(window_name, canv)
cv2.waitKey(0)

rectL, rectR = rectification(imgL, imgR, pts1, pts2, F)
canv = draw_stereo(rectL, rectR, path.join(temp_path,'08_rectification.png'))
cv2.imshow(window_name, canv)
cv2.waitKey(0)

maskL, maskR = rectification(maskL, maskR, pts1, pts2, F)
canv = draw_stereo(maskL * 255, maskR * 255, path.join(temp_path,'08_rectification_masked.png'))
cv2.imshow(window_name, canv)
cv2.waitKey(0)

# downsample for smoother disparity map
rectL = getDownSampledImg(0.5, rectL)
rectR = getDownSampledImg(0.5, rectR)
maskL = getDownSampledImg(0.5, maskL)
maskR = getDownSampledImg(0.5, maskR)

# compute disparity using semi-global block matching
stereo = cv2.StereoSGBM_create(minDisparity=-5, numDisparities=48, blockSize=16, speckleRange=0,
    speckleWindowSize=0, uniquenessRatio=10)
disparity = stereo.compute(rectL, rectR)

# mask disparity
mask = np.logical_and(maskL, maskR).astype(np.uint8)
disparity[mask[:,:,0]==0] = disparity.min()

###############################################################################
#  Plot Estimated Disparity Map
###############################################################################

im = plt.imshow(disparity)
plt.colorbar(im,fraction=0.046, pad=0.04)
plt.show()


import sys
import argparse
import numpy as np
import pandas as pd
from dtsr.config import Config
from dtsr.signif import bootstrap
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt

if __name__ == '__main__':

    argparser = argparse.ArgumentParser('''
        Computes pairwise significance of error differences between DTSR models and competitors.
        Assumes models are named using the template <MODELNAME>_<TASKNAME>, where <TASKNAME> is
        shared between models that should be compared. For example, if the config file contains
        4 models --- DTSR_TASK1, DTSR_TASK2, COMPETITOR_TASK1, and COMPETITOR_TASK2 --- the script
        will perform 2 comparisons: DTSR_TASK1 vs COMPETITOR_TASK1 and DTSR_TASK2 vs. COMPETITOR_TASK2.
    ''')
    argparser.add_argument('config_path', help='Path to configuration (*.ini) file')
    argparser.add_argument('-m', '--models', nargs='*', default=[], help='Path to configuration (*.ini) file')
    argparser.add_argument('-p', '--partition', type=str, default='dev', help='Name of partition to use (one of "train", "dev", "test")')
    argparser.add_argument('-M', '--metric', type=str, default='loss', help='Metric to use for comparison (either "loss" or "loglik")')
    argparser.add_argument('-t', '--tails', type=int, default=2, help='Number of tails (1 or 2)')
    args, unknown = argparser.parse_known_args()

    p = Config(args.config_path)
    if len(args.models) > 0:
        models = args.models
    else:
        models = p.model_list[:]

    run_baseline = False
    run_dtsr = False
    for m in models:
        if not run_baseline and m.startswith('LM') or m.startswith('GAM'):
            run_baseline = True
        elif not run_dtsr and m.startswith('DTSR'):
            run_dtsr = True

    sys.stderr.write('\n')
    dtsr_models = [x for x in models if x.startswith('DTSR')]

    if args.metric == 'loss':
        file_name = '/%s_losses_%s.txt'%(p.loss, args.partition)
    else:
        file_name = '/loglik_%s.txt'%args.partition

    for i in range(len(dtsr_models)):
        m1 = dtsr_models[i]
        for j in range(i+1, len(dtsr_models)):
            m2 = dtsr_models[j]
            name = '%s_v_%s' %(m1, m2)
            a = pd.read_csv(p.outdir + '/' + m1 + file_name, sep=' ', header=None, skipinitialspace=True)
            b = pd.read_csv(p.outdir + '/' + m2 + file_name, sep=' ', header=None, skipinitialspace=True)
            select = np.logical_and(np.isfinite(np.array(a)), np.isfinite(np.array(b)))
            diff = float(len(a) - select.sum())
            p_value, base_diff, diffs = bootstrap(a[select], b[select], n_iter=10000, n_tails=args.tails, mode=args.metric)
            sys.stderr.write('\n')
            with open(p.outdir + '/' + name + '_' + args.partition + '.txt', 'w') as f:
                f.write('='*50 + '\n')
                f.write('Model comparison: %s vs %s\n' %(m1, m2))
                if diff > 0:
                    f.write('%d NaN rows filtered out (out of %d)\n' %(diff, len(a)))
                f.write('Partition: %s\n' %args.partition)
                f.write('Loss difference: %.4f\n' %base_diff)
                f.write('p: %.4e\n' %p_value)
                f.write('='*50 + '\n')
            plt.hist(diffs, bins=1000)
            plt.savefig(p.outdir + '/' + name + '_' + args.partition + '.png')
            plt.close('all')


# 自定义异常处理
from rest_framework.views import exception_handler
from rest_framework.views import Response
from rest_framework import status

# 将仅针对由引发的异常生成的响应调用异常处理程序。它不会用于视图直接返回的任何响应
def custom_exception_handler(exc, context):
    response = exception_handler(exc, context)

    if response is None:
        return Response({
            'message': '服务器错误'
        }, status=status.HTTP_500_INTERNAL_SERVER_ERROR, exception=True)

    else:
        # 这个循环是取第一个错误的提示用于渲染
        for index, value in enumerate(response.data):
            if index == 0:
                key = value
                value = response.data[key]

                if isinstance(value, str):
                    message = value
                else:
                    message = key + value[0]
        # print('123 = %s - %s - %s' % (context['view'], context['request'].method, exc))
        return Response({
            'message': message
        }, status=response.status_code, exception=True)

    return response


import Orange
import numpy as np

#TEST DATA TO NUMPY
tab = Orange.data.Table("Test_Data_With_Domain_Data.tab")
U, _, _ = tab.to_numpy()

np.save("TestWithoutZipData", U)

#TRAIN DATA TO NUMPY
tab = Orange.data.Table("Train_Data_With_Domain_Data.tab")
U, _, _ = tab.to_numpy()

np.save("TrainWithoutZipData", U)

Clustering and atribute chosing/numpyData/final_Clusters/Final Prediction Test Set/Preparing Train and Test data/DataToNumpy.py


from collections import defaultdict
from math import inf
from math import sqrt
import random
import csv

def point_avg(points):
    """
    Accepts a list of points, each with the same number of dimensions.
    (points can have more dimensions than 2)

    Returns a new point which is the center of all the points.
    """

    new_p = []

    p_len = len(points)
    d_len = len(points[0])

    for d in range(d_len):
        sum = 0
        for p in range(p_len):
            sum = sum + points[p][d]

        new_p.append((float(sum))/(float(p_len)))

    return new_p

def update_centers(data_set, assignments):
    """
    Accepts a dataset and a list of assignments; the indexes
    of both lists correspond to each other.
    Compute the center for each of the assigned groups.
    Return `k` centers in a list
    """

    new_set = defaultdict(list)
    center = []

    for (assignment,point)in zip(assignments,data_set):
        new_set[assignment].append(point)

    for i in new_set.keys():
        center.append(point_avg(new_set[i]))

    return center

def assign_points(data_points, centers):
    """
    """
    assignments = []
    for point in data_points:
        shortest = inf  # positive infinity
        shortest_index = 0
        for i in range(len(centers)):
            val = distance(point, centers[i])
            if val < shortest:
                shortest = val
                shortest_index = i
        assignments.append(shortest_index)
    return assignments

def distance(a, b):
    """
    Returns the Euclidean distance between a and b
    """
    dim = len(a)

    sum = 0

    for d in range(dim):
        elem = (a[d]-b[d])**2
        sum = sum + elem

    return sqrt(sum)

def generate_k(data_set, k):
    """
    Given `data_set`, which is an array of arrays,
    return a random set of k points from the data_set
    """
    if k<= 0 or k > len(data_set):
        raise ValueError("k is not valid")
    return random.sample(data_set,k)

def get_list_from_dataset_file(dataset_file):
    data = []

    with open(dataset_file) as f:
        datas = csv.reader(f)
        for l in datas:
            row = []
            for n in l:
                elem = int(n)
                row.append(elem)
            data.append(row)
    return data

def cost_function(clustering):
    c = 0

    for k in clustering.keys():
        lst = clustering[k]
        center_point = point_avg(lst)

        for p in lst:
            c = c + distance(center_point,p)

    return c

def k_means(dataset_file, k):
    dataset = get_list_from_dataset_file(dataset_file)
    k_points = generate_k(dataset, k)
    assignments = assign_points(dataset, k_points)
    old_assignments = None
    while assignments != old_assignments:
        new_centers = update_centers(dataset, assignments)
        old_assignments = assignments
        assignments = assign_points(dataset, new_centers)
    clustering = defaultdict(list)
    for assignment, point in zip(assignments, dataset):
        clustering[assignment].append(point)
    print("hi")
    return clustering


from __future__ import annotations

import random
from fractions import Fraction
from typing import Any, Callable, Union

import aiger_discrete
import attr
from aiger_discrete import FiniteFunc
from aiger_discrete.discrete import project, TIMED_NAME
from pyrsistent import pmap
from pyrsistent.typing import PMap

Probability = Union[float, Fraction]
Distribution = Callable[[Any], Union[float, Fraction]]

def rejection_sample(name: str, func: FiniteFunc, dist: Distribution) -> Any:
    size = func.imap[name].size
    encoding = func.input_encodings.get(name, aiger_discrete.Encoding())
    while True:
        val = encoding.decode(random.getrandbits(size))
        prob = dist(val)

        if prob == 1:
            return val
        elif prob == 0:
            continue
        elif random.random() <= dist(val):
            return val

def to_finite_func(circ) -> FiniteFunc:
    if isinstance(circ, FiniteFunc):
        return circ
    return aiger_discrete.from_aigbv(circ.aigbv)

@attr.s(frozen=True, auto_attribs=True)
class PCirc:
    """Wrapper around AIG representing a function with some random inputs."""
    circ: FiniteFunc = attr.ib(converter=to_finite_func)
    dist_map: PMap[str, Probability] = attr.ib(converter=pmap, default=pmap())

    def __attrs_post_init__(self):
        """Make sure unneeded distributions are forgotten."""
        dist_map = project(self.dist_map, self.circ.inputs)
        object.__setattr__(self, "dist_map", dist_map)

    @property
    def inputs(self): return self.circ.inputs - set(self.dist_map.keys())

    @property
    def outputs(self): return self.circ.outputs

    @property
    def latches(self): return self.circ.latches

    @property
    def latch2init(self): return self.circ.latch2init

    @property
    def aig(self): return self.circ.aig

    @property
    def aigbv(self): return self.circ.aigbv

    def assume(self, aigbv_like) -> PCirc:
        """Return Probabilistic Circuit with new assumption over the inputs."""
        return attr.evolve(self, circ=self.circ.assume(aigbv_like))

    def __rshift__(self, other) -> PCirc:
        circ = self.circ >> canon(other).circ
        return PCirc(circ, other.dist_map + self.dist_map)

    def __lshift__(self, other) -> PCirc:
        return canon(other) >> self

    def __or__(self, other) -> PCirc:
        if set(self.dist_map.keys()) & set(self.dist_map.keys()):
            raise ValueError("Circuits have conflicting dist_maps.")
        circ = self.circ | canon(other).circ
        return PCirc(circ, self.dist_map + other.dist_map)

    def __call__(self, inputs, latches=None, max_tries=1_000):
        inputs = dict(inputs)
        for count in range(max_tries + 1):
            inputs.update({
                k: rejection_sample(k, self.circ, dist)
                for k, dist in self.dist_map.items()
            })

            try:
                return self.circ(inputs=inputs, latches=latches)
            except ValueError:  # Invalid input selected.
                # TODO: build BDD to uniformly sample.
                if count <= max_tries:
                    continue
                raise RuntimeError("Rejection Sampling failed!")

    def __getitem__(self, others) -> PCirc:
        circ = self.circ[others]
        kind, relabels = others

        kwargs = {}
        if kind == 'i':
            evolver = self.dist_map.evolver()
            for old, new in relabels.items():
                if old not in self.dist_map:
                    continue
                evolver[new] = self.dist_map[old]
                del evolver[old]
            kwargs['dist_map'] = evolver.persistent()

        return attr.evolve(self, circ=circ, **kwargs)

    def loopback(self, *wirings) -> PCirc:
        inputs = self.inputs
        assert all(w['input'] in inputs for w in wirings)
        circ = self.circ.loopback(*wirings)
        return attr.evolve(self, circ=circ)

    def unroll(self,
               horizon, *,
               init=True,
               omit_latches=True,
               only_last_outputs=False) -> PCirc:
        circ = self.circ.unroll(
            horizon=horizon,
            init=init,
            omit_latches=omit_latches,
            only_last_outputs=only_last_outputs,
        )
        dist_map = {}
        for timed_name in circ.inputs:
            # Should always match because of unrolling.
            name = TIMED_NAME.match(timed_name).groups()[0]
            if name not in self.dist_map:
                continue
            dist_map[timed_name] = self.dist_map[name]

        return attr.evolve(self, circ=circ, dist_map=dist_map)

    simulator = aiger_discrete.FiniteFunc.simulator
    simulate = aiger_discrete.FiniteFunc.simulate

    def use_faircoins(self) -> PCirc:
        # TODO: return PCirc where
        # 1. every random variable's encoding is i.d.
        # 2. every random distribution is over fair coins.
        pass

    def with_distmap(self, dist_map) -> PCirc:
        """Update the distributions over the inputs."""
        return attr.evolve(self, dist_map=self.dist_map + pmap(dist_map))

def canon(circ) -> PCirc:
    if isinstance(circ, PCirc):
        return circ
    return PCirc(circ)  # Assume aigbv like.

__all__ = ['PCirc']


from mgmt import connection
import json
import requests

__all__ = [
    'AddSubnetLib'
]

class AddSubnetLib():
    def __init__(self):
        self.f = requests.Session()

    def run(self, mgmt_ip, sid, subnet, host_suffix, mask_length):
        '''
        Function used to Add new Network(s) to CheckPoint.
        :param mgmt_ip: IP address of the CheckPoint Management Server.
        :param sid: Session ID provided from the Login Action.
        :param subnet: Network address of the subnet that will be added to CheckPoint.
        :param host_suffix: used to create the hostname (first half) after combining it with the Network address (i.e: Network_, MaliciousNet_, N_..etc)))
        :param mask_length: mask length of the subnet that will be added to CheckPoint.
        :return: the http return status code of the api request to Add a new subnet.
        '''
        t = connection(mgmt_ip)
        url = t.create_api_url("add-network")
        print("API url used in this action: " + url)
        hostname = host_suffix+subnet+"_"+mask_length
        request_headers = {'Content-Type':'application/json', 'X-chkp-sid' : sid}
        my_data = {"name": hostname, "subnet": subnet, "mask-length":mask_length}
        r = self.f.post(url, data=json.dumps(my_data), headers=request_headers, verify=False)
        try:
            '''
            #Un-hash the following 2 lines to see the full response of the API request
            #a = json.loads(r.content)
            #print(a)'''
            return_code = r.status_code
            if return_code == 200:
                print("Subnet add for {}/{} is OK ".format(subnet, mask_length))
                return r.status_code
            else:
                print("Subnet {}/{} add is NOK ".format(subnet, mask_length))
                a = json.loads(r.content)
                print(a)
                return return_code
                '''
                #Un-hash the following 2 lines, if you want to use embedded discard to discard the changes in case of errors instead of using the Stackstorm DiscardSession action in the workflow.
                y = DiscardSessionLib()
                print(y.run(mgmt_ip, sid))
                return return_code, a['errors']'''

        except Exception as e:
            print("Error is :",e)
            '''
            #Un-hash the following 2 lines, if you want to use embedded discard to discard the changes in case of errors instead of using the Stackstorm DiscardSession action in the workflow.
            y = DiscardSessionLib()
            print(y.run(mgmt_ip, sid))
            return return_code, a['errors']'''


###############################################################################
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
###############################################################################
import os
import sys
import onnx
import unittest
import keras2onnx
import numpy as np
from keras2onnx.proto import keras, is_tf_keras
from distutils.version import StrictVersion

working_path = os.path.abspath(os.path.dirname(__file__))
tmp_path = os.path.join(working_path, 'temp')

class TestKerasApplications(unittest.TestCase):

    def setUp(self):
        self.model_files = []

    def tearDown(self):
        for fl in self.model_files:
            os.remove(fl)

    @staticmethod
    def asarray(*a):
        return np.array([a], dtype='f')

    @staticmethod
    def get_temp_file(name):
        if not os.path.exists(tmp_path):
            os.mkdir(tmp_path)
        return os.path.join(tmp_path, name)

    def run_onnx_runtime(self, case_name, onnx_model, data, expected, rtol=1.e-3, atol=1.e-6):
        temp_model_file = TestKerasApplications.get_temp_file('temp_' + case_name + '.onnx')
        onnx.save_model(onnx_model, temp_model_file)
        try:
            import onnxruntime
            sess = onnxruntime.InferenceSession(temp_model_file)
        except ImportError:
            return True

        if not isinstance(expected, list):
            expected = [expected]

        data = data if isinstance(data, list) else [data]
        input_names = sess.get_inputs()
        # to avoid too complicated test code, we restrict the input name in Keras test cases must be
        # in alphabetical order. It's always true unless there is any trick preventing that.
        feed = zip(sorted(i_.name for i_ in input_names), data)
        actual = sess.run(None, dict(feed))
        res = all(np.allclose(expected[n_], actual[n_], rtol=rtol, atol=atol) for n_ in range(len(expected)))
        if res and temp_model_file not in self.model_files:  # still keep the failed case files for the diagnosis.
            self.model_files.append(temp_model_file)

        if not res:
            for n_ in range(len(expected)):
                expected_list = expected[n_].flatten()
                actual_list = actual[n_].flatten()
                diff_list = abs(expected_list - actual_list)
                count_total = len(expected_list)
                count_error = 0

                for e_, a_, d_ in zip(expected_list, actual_list, diff_list):
                    if d_ > atol + rtol * abs(a_):
                        if count_error < 10:  # print the first 10 mismatches
                            print(
                                "case = " + case_name + ", result mismatch for expected = " + str(e_) +
                                ", actual = " + str(a_), file=sys.stderr)
                        count_error = count_error + 1

                print("case = " + case_name + ", " +
                      str(count_error) + " mismatches out of " + str(count_total) + " for list " + str(n_),
                      file=sys.stderr)

        return res

    def _test_keras_model(self, model, model_name='onnx_conversion', rtol=1.e-3, atol=1.e-5, img_size=224):
        preprocess_input = keras.applications.resnet50.preprocess_input
        image = keras.preprocessing.image

        img_path = os.path.join(os.path.dirname(__file__), 'data', 'street.jpg')
        try:
            img = image.load_img(img_path, target_size=(img_size, img_size))
            x = image.img_to_array(img)
            x = np.expand_dims(x, axis=0)
            x = preprocess_input(x)

            preds = model.predict(x)
            onnx_model = keras2onnx.convert_keras(model, model.name)
            self.assertTrue(self.run_onnx_runtime(model_name, onnx_model, x, preds, rtol=rtol, atol=atol))
        except FileNotFoundError:
            self.assertTrue(False, 'The image data does not exist.')

    def test_MobileNet(self):
        mobilenet = keras.applications.mobilenet
        model = mobilenet.MobileNet(weights='imagenet')
        self._test_keras_model(model)

    @unittest.skipIf(StrictVersion(keras.__version__.split('-')[0]) < StrictVersion("2.2.3"),
                     "There is no mobilenet_v2 module before keras 2.2.3.")
    def test_MobileNetV2(self):
        mobilenet_v2 = keras.applications.mobilenet_v2
        model = mobilenet_v2.MobileNetV2(weights='imagenet')
        self._test_keras_model(model)

    def test_ResNet50(self):
        from keras.applications.resnet50 import ResNet50
        model = ResNet50(include_top=True, weights='imagenet')
        self._test_keras_model(model)

    def test_InceptionV3(self):
        from keras.applications.inception_v3 import InceptionV3
        model = InceptionV3(include_top=True, weights='imagenet')
        self._test_keras_model(model, img_size=299)

    def test_DenseNet121(self):
        from keras.applications.densenet import DenseNet121
        model = DenseNet121(include_top=True, weights='imagenet')
        self._test_keras_model(model)

    def test_Xception(self):
        from keras.applications.xception import Xception
        model = Xception(include_top=True, weights='imagenet')
        self._test_keras_model(model, atol=5e-3, img_size=299)

    def test_ACGAN(self):
        # An ACGAN generator from https://github.com/eriklindernoren/Keras-GAN/blob/master/acgan/acgan.py
        latent_dim = 100
        num_classes = 10
        channels = 1
        model = keras.models.Sequential()
        model.add(keras.layers.Dense(128 * 7 * 7, activation="relu", input_dim=latent_dim))
        model.add(keras.layers.Reshape((7, 7, 128)))
        model.add(keras.layers.BatchNormalization(momentum=0.8))
        model.add(keras.layers.UpSampling2D())
        model.add(keras.layers.Conv2D(128, kernel_size=3, padding="same"))
        model.add(keras.layers.Activation("relu"))
        model.add(keras.layers.BatchNormalization(momentum=0.8))
        model.add(keras.layers.UpSampling2D())
        model.add(keras.layers.Conv2D(64, kernel_size=3, padding="same"))
        model.add(keras.layers.Activation("relu"))
        model.add(keras.layers.BatchNormalization(momentum=0.8))
        model.add(keras.layers.Conv2D(channels, kernel_size=3, padding='same'))
        model.add(keras.layers.Activation("tanh"))

        noise = keras.layers.Input(shape=(latent_dim,))
        label = keras.layers.Input(shape=(1,), dtype='int32')
        label_embedding = keras.layers.Flatten()(keras.layers.Embedding(num_classes, 100)(label))

        model_input = keras.layers.multiply([noise, label_embedding])
        img = model(model_input)

        keras_model = keras.models.Model([noise, label], img)
        x = np.random.rand(1, 100).astype(np.float32)
        y = np.random.rand(1, 1).astype(np.int32)

        expected = keras_model.predict([x, y])
        onnx_model = keras2onnx.convert_keras(keras_model, keras_model.name)

        self.assertTrue(self.run_onnx_runtime(onnx_model.graph.name, onnx_model, [x, y], expected))

    def test_BIGAN(self):
        # A BIGAN discriminator model from https://github.com/eriklindernoren/Keras-GAN/blob/master/bigan/bigan.py
        latent_dim = 100
        img_shape = (28, 28, 1)
        z = keras.layers.Input(shape=(latent_dim, ))
        img = keras.layers.Input(shape=img_shape)
        d_in = keras.layers.concatenate([z, keras.layers.Flatten()(img)])

        model = keras.layers.Dense(1024)(d_in)
        model = keras.layers.LeakyReLU(alpha=0.2)(model)
        model = keras.layers.Dropout(0.5)(model)
        model = keras.layers.Dense(1024)(model)
        model = keras.layers.LeakyReLU(alpha=0.2)(model)
        model = keras.layers.Dropout(0.5)(model)
        model = keras.layers.Dense(1024)(model)
        model = keras.layers.LeakyReLU(alpha=0.2)(model)
        model = keras.layers.Dropout(0.5)(model)
        validity = keras.layers.Dense(1, activation="sigmoid")(model)

        keras_model = keras.models.Model([z, img], validity)
        x = np.random.rand(5, 100).astype(np.float32)
        y = np.random.rand(5, 28, 28, 1).astype(np.float32)
        onnx_model = keras2onnx.convert_keras(keras_model, keras_model.name)

        expected = keras_model.predict([x, y])
        self.assertTrue(self.run_onnx_runtime(onnx_model.graph.name, onnx_model, [x, y], expected))

    def test_addition_rnn(self):
        # An implementation of sequence to sequence learning for performing addition
        # from https://github.com/keras-team/keras/blob/master/examples/addition_rnn.py
        DIGITS = 3
        MAXLEN = DIGITS + 1 + DIGITS
        HIDDEN_SIZE = 128
        BATCH_SIZE = 128
        CHARS_LENGTH = 12

        for RNN in [keras.layers.LSTM, keras.layers.GRU, keras.layers.SimpleRNN]:
            model = keras.models.Sequential()
            model.add(RNN(HIDDEN_SIZE, input_shape=(MAXLEN, CHARS_LENGTH)))
            model.add(keras.layers.RepeatVector(DIGITS + 1))
            model.add(RNN(HIDDEN_SIZE, return_sequences=True))
            model.add(keras.layers.TimeDistributed(keras.layers.Dense(CHARS_LENGTH, activation='softmax')))
            onnx_model = keras2onnx.convert_keras(model, model.name)
            x = np.random.rand(BATCH_SIZE, MAXLEN, CHARS_LENGTH).astype(np.float32)
            expected = model.predict(x)
            self.assertTrue(self.run_onnx_runtime(onnx_model.graph.name, onnx_model, x, expected))

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


from flask import Flask, render_template
from data import db_session
from data.users import User
from data.jobs import Jobs
import os

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

@app.route("/")
def main():
    db_session.global_init("db/blogs.db")
    db_sess = db_session.create_session()
    data = []
    for job in db_sess.query(Jobs).all():
        for user in db_sess.query(User).filter(User.id == job.team_leader):
            full_name = f"{user.surname} {user.name}"
            data.append([job.job, full_name, job.work_size, job.collaborators, job.is_finished])
    return render_template('journal.html', data=data)

if __name__ == '__main__':
    app.run(port=8080, host='127.0.0.1')

WEB. Знакомство с flask-sqlalchemy/Журнал работ/main.py


# Create your views here.

from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.shortcuts import redirect, render_to_response, get_object_or_404
from django.template import RequestContext
from django.views.generic.list_detail import object_list

from models import Article, Edit
from forms import ArticleForm, EditForm

@login_required
def add_article(request):
    form = ArticleForm(request.POST or None)
    if form.is_valid():
        article = form.save(commit=False)
        article.author = request.user
        article.save()
        msg = "Article saved successfully"
        messages.success(request, msg, fail_silently=True)
        return redirect(article)
    return render_to_response('wiki/article_form.html',
                              { 'form': form },
                              context_instance=RequestContext(request))

@login_required
def edit_article(request, slug):
    article = get_object_or_404(Article, slug=slug)
    form = ArticleForm(request.POST or None, instance=article)
    edit_form = EditForm(request.POST or None)
    if form.is_valid():
        article = form.save()
        if edit_form.is_valid():
            edit = edit_form.save(commit=False)
            edit.article = article
            edit.editor = request.user
            edit.save()
            msg = "Article updated successfully"
            messages.success(request, msg, fail_silently=True)
            return redirect(article)
    return render_to_response('wiki/article_form.html',
                              {
                                  'form': form,
                                  'edit_form': edit_form,
                                  'article': article,
                              },
                              context_instance=RequestContext(request))

def article_history(request, slug):
    article = get_object_or_404(Article, slug=slug)
    return  object_list(request,
                        queryset=Edit.objects.filter(article__slug=slug),
                        extra_context={'article': article})


# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from collections import defaultdict
from dateutil.relativedelta import relativedelta

from odoo import api, fields, models, SUPERUSER_ID, _
from odoo.osv import expression
from odoo.addons.stock.models.stock_rule import ProcurementException

class StockRule(models.Model):
    _inherit = 'stock.rule'

    @api.model
    def _run_manufacture(self, procurements):
        productions_values_by_company = defaultdict(list)
        errors = []
        for procurement, rule in procurements:
            bom = rule._get_matching_bom(procurement.product_id,
                                         procurement.company_id, procurement.values)
            if not bom:
                msg = _('There is no Bill of Material of type manufacture or kit found for the product %s. Please define a Bill of Material for this product.') % (
                    procurement.product_id.display_name,)
                errors.append((procurement, msg))

            productions_values_by_company[procurement.company_id.id].append(
                rule._prepare_mo_vals(*procurement, bom))

        if errors:
            raise ProcurementException(errors)

        for company_id, productions_values in productions_values_by_company.items():
            # creck if already exists someone mrp production order with the same production
            productions_values_check = []
            for p in productions_values:
                print(">>>>>>>>>>>>>>>>>>>>> p:", p)
                current_qty = p['product_qty']
                mrp_production_ids = self.env['mrp.production'].search([
                    ('product_id', '=', p['product_id']),
                    ('origin', '=', p['origin']),
                    ('state', 'not in', ('cancel', 'done', 'draft'))], order='id')
                if mrp_production_ids:
                    mrp_qty = sum(m.product_qty for m in mrp_production_ids)
                    diff_qty = current_qty-mrp_qty
                    if diff_qty > 0:
                        p['product_qty'] = diff_qty
                        productions_values_check.append(p)
                    else:
                        break
                else:
                    productions_values_check.append(p)

            productions_values = productions_values_check
            print(">>>>>>>>>>>>>>>>>>>>> productions_values_check:", productions_values_check)

            # create the MO as SUPERUSER because the current user may not have the rights to do it (mto product launched by a sale for example)
            productions = self.env['mrp.production'].with_user(
                SUPERUSER_ID).sudo().with_company(company_id).create(productions_values)
            self.env['stock.move'].sudo().create(productions._get_moves_raw_values())
            self.env['stock.move'].sudo().create(productions._get_moves_finished_values())
            productions._create_workorder()
            productions.filtered(lambda p: p.move_raw_ids).action_confirm()

            for production in productions:
                origin_production = production.move_dest_ids and production.move_dest_ids[
                    0].raw_material_production_id or False
                orderpoint = production.orderpoint_id
                if orderpoint:
                    production.message_post_with_view('mail.message_origin_link',
                                                      values={'self': production,
                                                              'origin': orderpoint},
                                                      subtype_id=self.env.ref('mail.mt_note').id)
                if origin_production:
                    production.message_post_with_view('mail.message_origin_link',
                                                      values={'self': production,
                                                              'origin': origin_production},
                                                      subtype_id=self.env.ref('mail.mt_note').id)
        return True
        # res = super(StockRule, self)._run_manufacture(procurements)
        # print res


import arrow
from keg.db import db
from keg_elements.db.mixins import DefaultColsMixin, MethodsMixin
from sqlalchemy.dialects.postgresql import insert as pgsql_insert
from sqlalchemy.inspection import inspect

class EntityMixin(DefaultColsMixin, MethodsMixin):
    @classmethod
    def upsert(cls, values=None, on_conflict_do='update', index_elements=None, **kwargs):
        if index_elements is None:
            index_elements = cls.__upsert_index_elements__

        if values is None:
            values = kwargs

        primary_key_col = inspect(cls).primary_key[0]
        stmt = pgsql_insert(cls.__table__).returning(primary_key_col).values(**values)

        assert on_conflict_do in ('nothing', 'update')
        if on_conflict_do == 'update':
            touching_timestamps = {}
            if hasattr(cls, 'updated_utc'):
                # on_conflict_do_update won't take into account columns onupdate attributes
                touching_timestamps.update({'updated_utc': arrow.utcnow()})
            stmt = stmt.on_conflict_do_update(
                index_elements=index_elements,
                set_={**values, **touching_timestamps}
            )
        else:
            stmt = stmt.on_conflict_do_nothing(index_elements=index_elements)

        result = db.session.execute(stmt)

        return result.scalar()

def tc_relation(kwargs, rel_col_name, rel_ent_cls):
    id_col_name = f'{rel_col_name}_id'
    rel_name_dunder = f'{rel_col_name}__'

    if id_col_name not in kwargs and rel_col_name not in kwargs:
        rel_keys = [key for key in kwargs.keys() if key.startswith(rel_name_dunder)]
        rel_kwargs = {key.replace(rel_name_dunder, '', 1): kwargs.pop(key) for key in rel_keys}
        rel_ent_inst = rel_ent_cls.testing_create(**rel_kwargs)
        kwargs[id_col_name] = rel_ent_inst.id

    return kwargs


"""
OpenVisus conversion module
"""
import os
import rasterio
import argparse
import subprocess
import pathlib
import shutil
from glob import glob
from PIL import Image,ImageOps

from OpenVisus import *
DbModule.attach()

import dask
import distributed
import pandas as pd
from crown_maps.verify import get_site, get_year
import numpy as np
from numba import njit, prange

from OpenVisus.__main__ import MidxToIdx

def match_name(x):
  x = os.path.basename(x)
  return x.replace("image.tif","image_rasterized.tif")

@njit(parallel=True)
def blend_rgb_ann(a, b):
  #a[b[b>0]] = [255,0,0]
  for i in prange(a[0].shape[0]):
    for j in prange(a[0].shape[1]):
      if(b[i][j] > 0):
        a[0][i][j]=255
        a[1][i][j]=0
        a[2][i][j]=0

def blend(rgb_path, annotation_dir, outdir):
  basename = os.path.basename(rgb_path)
  ann_path=annotation_dir+"/"+basename.replace("image.tif", "image_rasterized.tif")
  ageo = rasterio.open(rgb_path)
  a = ageo.read()
  bgeo = rasterio.open(ann_path)
  b = bgeo.read()
  print("Blending ", rgb_path, "and", ann_path, "...")
  blend_rgb_ann(a, b[0])
  out_name = outdir+"/"+basename
  with rasterio.open(
        out_name,
        'w',
        driver='GTiff',
        height=ageo.height,
        width=ageo.width,
        count=3,
        dtype=a.dtype,
        crs='+proj=latlong',
        transform=ageo.transform,
    ) as dst:
        dst.write(a)

  return out_name

def run(rgb_images, dst_directory, annotation_dir):

  #Construct outdir variable from top level savedir and site
  site = get_site(rgb_images[0])
  outdir = os.path.join(dst_directory,site)
  pathlib.Path(outdir+"/temp").mkdir(parents=True, exist_ok=True)

  outname = outdir.split("/")[-1]
  if(outname==""):
    outname = outdir.split("/")[-2]

  # Blend rgb and annotations
  images = []
  for rgb_path in rgb_images:
    images.append(blend(rgb_path, annotation_dir, outdir))

  # find images
  # convert to idx
  sx,sy=1.0, 1.0
  tiles=[]
  for I,filename in enumerate(images):
    metadata =rasterio.open(filename)
    name=os.path.splitext(os.path.basename(filename))[0]
    width,height=metadata.width,metadata.height
    x1,y1,x2,y2=metadata.bounds.left,metadata.bounds.bottom, metadata.bounds.right, metadata.bounds.top

    # compute scaling to keep all pixels
    if I==0:
      sx=width /(x2-x1)
      sy=height/(y2-y1)

    x1,y1,x2,y2=sx*x1,sy*y1,sx*x2,sy*y2
    tile={"name": name, "size" : (width,height), "bounds" : (x1,y1,x2,y2)}
    print("Converting tile...",tile,I,"/",len(images))
    tiles.append(tile)

    # avoid creation multiple times
    if not os.path.isfile(os.path.join(dst_directory,name,"visus.idx")):
      data=Image.open(filename)
      data=ImageOps.flip(data)
      CreateIdx(url=os.path.join(dst_directory,name,"visus.idx"), rmtree=True, dim=2,data=numpy.asarray(data))

  # create midx
  X1=min([tile["bounds"][0] for tile in tiles])
  Y1=min([tile["bounds"][1] for tile in tiles])
  midx_filename=os.path.join(dst_directory,"visus.midx")
  with open(midx_filename,"wt") as file:
    file.writelines([
            "<dataset typename='IdxMultipleDataset'>\n",
                  "\t<field name='voronoi'>output=voronoi()</field>\n",
                  *["\t<dataset url='./{}/visus.idx' name='{}' offset='{} {}'/>\n".format(tile["name"],tile["name"],tile["bounds"][0]-X1,tile["bounds"][1]-Y1) for tile in tiles],
                  "</dataset>\n"
          ])

  # to see automatically computed idx file
  db=LoadDataset(midx_filename)
  print(db.getDatasetBody().toString())
  idx_filename=os.path.join(dst_directory,"visus.idx")
  MidxToIdx(["--midx", midx_filename, "--field","output=voronoi()", "--tile-size","4*1024", "--idx", idx_filename])

if __name__=="__main__":
  #Create dask cluster
  from crown_maps import start_cluster
  client = start_cluster.start(cpus=20,mem_size="40GB")
  client.wait_for_workers(1)

  #Pool of rasterized predictions
  rgb_list = glob.glob("/orange/ewhite/NeonData/**/Mosaic/*image.tif",recursive=True)
  annotation_dir = "/orange/idtrees-collab/rasterized/"
  outdir = "/orange/idtrees-collab/OpenVisus/"
  annotation_list = glob.glob(annotation_dir + "*.tif")

  annotation_names = [os.path.basename(x) for x in annotation_list]
  rgb_list = [x for x in rgb_list if match_name(x) in annotation_names]

  df = pd.DataFrame({"path":rgb_list})
  df["site"] = df.path.apply(lambda x: get_site(x))
  df["year"] = df.path.apply(lambda x: get_year(x))

  #just run
  #df = df[df.site.isin(["ABBY"])]

  #order by site  using only the most recent year
  site_lists = df.groupby('site').apply(lambda x: x[x.year==x.year.max()]).reset_index(drop=True).groupby('site').path.apply(list).values

  ####Scatter and run in parallel
  futures = []
  for site in site_lists:
    site = np.sort(site)
    siteID = get_site(site[0])
    site_dir = "{}/{}".format(outdir, siteID)

    try:
      shutil.rmtree(site_dir)
      os.mkdir(site_dir)
    except:
      os.mkdir(site_dir)

    #run(rgb_images=site[0:20], dst_directory=site_dir, annotation_dir=annotation_dir)
    future = dask.delayed(run)(rgb_images=site[0:20], dst_directory=site_dir, annotation_dir=annotation_dir)
    futures.append(future)

  persisted_values = dask.persist(*futures)
  distributed.wait(persisted_values)
  for pv in persisted_values:
    try:
      print(pv)
    except Exception as e:
      print(e)
      continue


import math

import torch
import torch.nn.functional as F
from torch import optim, Tensor
from torch import nn

import torchvision
import pytorch_lightning as pl

"""
Defines the VGG encoder with beta-TCVAE loss module. As the decoder for the
MNIST and dSprites data, an InfoGAN, and for the OCT data, a DCGAN architecture is
automatically selected. Also the VGG size depends on the data.
"""

class betaTCVAE_VGG(pl.LightningModule):
    def __init__(self,
                 latent_dim=10,
                 input_dim=784,
                 lr=0.001,
                 anneal_steps: int = 200,
                 alpha: float = 1.,
                 beta: float = 1.,
                 gamma: float = 1.,
                 dataset = "mnist",
                 c = 64
                 ):
        super(betaTCVAE_VGG, self).__init__()
        self.save_hyperparameters()
        self.c = c
        self.num_iter = 0
        self.dataset = dataset

        if dataset == "mnist":
            self.scale = 7
            self.trainset_size = 50000
        elif dataset=="dSprites":
            self.scale = 16
            self.trainset_size = 600000
        elif dataset == "OCT":
            self.scale = 50
            self.trainset_size = 85600

        # Encoder

        if dataset == "OCT":
            model = torchvision.models.vgg19_bn()
            model = list(model.features.children())[1:53]
        else:
            model = torchvision.models.vgg16_bn()
            model = list(model.features.children())[1:36]

        self.enc_conv1 = nn.Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        self.vgg = nn.Sequential(*model)
        self.enc_avgpool = nn.AdaptiveAvgPool2d(output_size=(1,1))

        self.enc_fc = nn.Linear(in_features=512, out_features=10*c)
        self.fc_mu = nn.Linear(in_features=10*c, out_features=latent_dim)
        self.fc_logvar = nn.Linear(in_features=10*c, out_features=latent_dim)

        # Decoder

        if dataset == "OCT":
            self.decoder = nn.Sequential(
                nn.ConvTranspose2d(in_channels=latent_dim, out_channels=self.scale * 8, kernel_size=12, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(self.scale * 8),
                nn.ReLU(True),
                nn.ConvTranspose2d(in_channels=self.scale * 8, out_channels=self.scale * 4, kernel_size=12, stride=2, padding=1, bias=False),
                nn.BatchNorm2d(self.scale * 4),
                nn.ReLU(True),
                nn.ConvTranspose2d(in_channels=self.scale * 4, out_channels=self.scale * 2, kernel_size=14, stride=2, padding=1, bias=False),
                nn.BatchNorm2d(self.scale * 2),
                nn.ReLU(True),
                nn.ConvTranspose2d(in_channels=self.scale * 2, out_channels=self.scale, kernel_size=14, stride=2, padding=1, bias=False),
                nn.BatchNorm2d(self.scale),
                nn.ReLU(True),
                nn.ConvTranspose2d(in_channels=self.scale, out_channels=1, kernel_size=14, stride=2, padding=1, bias=False),
                nn.Sigmoid()
            )
        else:
            self.decoder = nn.Sequential(
                nn.BatchNorm1d(latent_dim),
                nn.Linear(in_features=latent_dim, out_features=c*2*self.scale*self.scale),
                nn.BatchNorm1d(c*2*self.scale*self.scale),
                nn.Unflatten(1, (self.c*2, self.scale, self.scale)),
                nn.ConvTranspose2d(in_channels=c*2, out_channels=c, kernel_size=4, stride=2, padding=1),
                nn.ReLU(), nn.BatchNorm2d(c),
                nn.ConvTranspose2d(in_channels=c, out_channels=1, kernel_size=4, stride=2, padding=1),
                nn.Sigmoid()
            )

    def encode(self, x):
        x = self.enc_conv1(x)
        x = self.vgg(x)
        x = self.enc_avgpool(x)

        try:
            x = torch.squeeze(x, dim=3)
            x = torch.squeeze(x, dim=2)
        except IndexError:
            pass

        x = F.relu(self.enc_fc(x))
        mu = self.fc_mu(x)
        log_var = self.fc_logvar(x)
        return mu, log_var

    def sampling(self, mu, log_var):
        std = torch.exp(log_var * 0.5)
        q = torch.distributions.Normal(mu, std)
        z = q.rsample()
        return z

    def decode(self, z):
        if self.dataset=="OCT":
            z = torch.unsqueeze(z,2)
            z = torch.unsqueeze(z,3)

        x = self.decoder(z)
        return x

    def forward(self, x):
        mu, log_var = self.encode(x)
        return mu

    def log_density_gaussian(self, x: Tensor, mu: Tensor, logvar: Tensor):
        norm = - 0.5 * (math.log(2 * math.pi) + logvar)
        log_density = norm - 0.5 * ((x - mu) ** 2 * torch.exp(-logvar))
        return log_density

    def loss(self, recons, x, mu, log_var, z):
        # Inspired by: https://github.com/YannDubs/disentangling-vae/blob/7b8285baa19d591cf34c652049884aca5d8acbca/disvae/models/losses.py#L316
        recons_loss = F.binary_cross_entropy(
            recons.view(-1, self.hparams.input_dim).clamp(0, 1),
            x.view(-1, self.hparams.input_dim),
            reduction='sum')

        log_q_zx = self.log_density_gaussian(z, mu, log_var).sum(dim=1)

        zeros = torch.zeros_like(z)
        log_p_z = self.log_density_gaussian(z, zeros, zeros).sum(dim=1)

        batch_size, latent_dim = z.shape
        mat_log_q_z = self.log_density_gaussian(z.view(batch_size, 1, latent_dim),
                                                mu.view(1, batch_size,
                                                        latent_dim),
                                                log_var.view(1, batch_size, latent_dim))

        # Estimate the three KL terms (log(q(z))) via importance sampling
        strat_weight = (self.trainset_size - batch_size + 1) / \
            (self.trainset_size * (batch_size - 1))

        importance_weights = torch.Tensor(batch_size, batch_size).fill_(
            1 / (batch_size - 1)).to(x.device)

        importance_weights.view(-1)[::batch_size] = 1 / self.trainset_size
        importance_weights.view(-1)[1::batch_size] = strat_weight
        importance_weights[batch_size - 2, 0] = strat_weight
        log_importance_weights = importance_weights.log()

        mat_log_q_z += log_importance_weights.view(batch_size, batch_size, 1)

        log_q_z = torch.logsumexp(mat_log_q_z.sum(2), dim=1, keepdim=False)
        log_prod_q_z = torch.logsumexp(
            mat_log_q_z, dim=1, keepdim=False).sum(1)

        # Three KL Term components
        mi_loss = (log_q_zx - log_q_z).mean()
        tc_loss = (log_q_z - log_prod_q_z).mean()
        kld_loss = (log_prod_q_z - log_p_z).mean()

        if self.training:
            self.num_iter += 1
            anneal_rate = min(0 + 1 * self.num_iter /
                              self.hparams.anneal_steps, 1)
        else:
            anneal_rate = 1

        loss = recons_loss/batch_size + \
            self.hparams.alpha * mi_loss + \
            self.hparams.beta * tc_loss + \
            self.hparams.gamma * anneal_rate * kld_loss

        return loss

    def training_step(self, batch, batch_idx):
        x, _ = batch
        mu, log_var = self.encode(x)
        z = self.sampling(mu, log_var)

        recons = self.decode(z)

        vae_loss = self.loss(recons, x, mu, log_var, z)

        self.log('loss', vae_loss, on_epoch=False, prog_bar=True, on_step=True,
                 sync_dist=True if torch.cuda.device_count() > 1 else False)

        return vae_loss

    def validation_step(self, batch, batch_idx):
        x, _ = batch
        mu, log_var = self.encode(x)
        z = self.sampling(mu, log_var)

        recons = self.decode(z)

        vae_loss = self.loss(recons, x, mu, log_var, z)

        self.log('val_loss', vae_loss, on_epoch=True, prog_bar=True,
                 sync_dist=True if torch.cuda.device_count() > 1 else False)

        return vae_loss

    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=self.hparams.lr)

    def get_progress_bar_dict(self):
        tqdm_dict = super().get_progress_bar_dict()
        if 'v_num' in tqdm_dict:
            del tqdm_dict['v_num']
        return tqdm_dict


from path_finder.utility.Graph import Graph
from django.db.models import Q
from path_finder.models import Routes, Airports

class PathSearcher():
    """
    Queries routes starting at specified origin on a BFS basis until a path
    to the request destination is found.
    """

    def __init__(self):
        self.valid_path_exists = True
        self.graph = None
        self.errors_list = []

    def build_graph(self, origin_str, destination_str):
        self.verify_origin_and_destination(origin_str, destination_str)
        self.graph = Graph(origin_str)
        traversed = []  # Routes that have already been traversed
        next_level_of_paths = self.find_initial_routes_from_origin_string(
            origin_str, )
        if (len(self.errors()) is not 0):
            return
        while next_level_of_paths != []:
            for path in next_level_of_paths:
                traversed.append(path.id)
                self.graph.add_edge(path.origin.IATA, path.destination.IATA)
                if (path.destination.IATA == destination_str):
                    return

            temp = self.find_routes_from_origins(
                next_level_of_paths,
                traversed)
            next_level_of_paths = temp

        # If entire function finishes then there is no solution
        self.log_error("No Route")

    def find_shortest_path(self, origin, destination):
        """
        A BFS that finds the shortest path from origin to destination in the
        in-memory graph.
        """

        graph = self.graph.graph
        queue = []
        queue.append([origin])

        while queue:
            path = queue.pop(0)
            last_node = path[-1]
            if last_node == destination:
                return path

            for adjacent in graph.get(last_node):
                if adjacent not in path:
                    new_path = list(path)
                    new_path.append(adjacent)
                    queue.append(new_path)

    def find_routes_from_origins(self, origins, traversed):
        """
        Queries database for flights origination from an airport in the
        origins list

        Parameters:
        origins [str] - List of IATA codes of origin airport.
        traversed [int] - List of Routes ids that have already been queried.

        Return Value:
        [Route] - List of Routes objects from datbase
        """
        routes = []
        for location in origins:
            new_routes = Routes.objects.filter(
                Q(origin=location.destination) & ~Q(id__in=traversed))
            routes += [route for route in new_routes]
        return routes

    def find_initial_routes_from_origin_string(self, origin_str):
        return Routes.objects.filter(Q(origin=origin_str))

    def verify_origin_and_destination(self, origin_str, destination_str):
        if not Airports.objects.filter(Q(IATA=origin_str)):
            self.log_error("Invalid Origin")
        if not Airports.objects.filter(Q(IATA=destination_str)):
            self.log_error("Invalid Destination")

    def log_error(self, error_msg):
        self.errors_list.append(error_msg)

    def errors(self):
        return self.errors_list


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /Users/svpino/dev/tensorflow-object-detection-sagemaker/todl/tensorflow-object-detection/research/object_detection/builders/losses_builder.py
# Compiled at: 2020-04-05 19:50:57
# Size of source mod 2**32: 9187 bytes
"""A function to build localization and classification losses from config."""
import functools
import object_detection.core as sampler
from object_detection.core import losses
from object_detection.protos import losses_pb2
from object_detection.utils import ops

def build(loss_config):
    """Build losses based on the config.

  Builds classification, localization losses and optionally a hard example miner
  based on the config.

  Args:
    loss_config: A losses_pb2.Loss object.

  Returns:
    classification_loss: Classification loss object.
    localization_loss: Localization loss object.
    classification_weight: Classification loss weight.
    localization_weight: Localization loss weight.
    hard_example_miner: Hard example miner object.
    random_example_sampler: BalancedPositiveNegativeSampler object.

  Raises:
    ValueError: If hard_example_miner is used with sigmoid_focal_loss.
    ValueError: If random_example_sampler is getting non-positive value as
      desired positive example fraction.
  """
    classification_loss = _build_classification_loss(loss_config.classification_loss)
    localization_loss = _build_localization_loss(loss_config.localization_loss)
    classification_weight = loss_config.classification_weight
    localization_weight = loss_config.localization_weight
    hard_example_miner = None
    if loss_config.HasField('hard_example_miner'):
        if loss_config.classification_loss.WhichOneof('classification_loss') == 'weighted_sigmoid_focal':
            raise ValueError('HardExampleMiner should not be used with sigmoid focal loss')
        hard_example_miner = build_hard_example_miner(loss_config.hard_example_miner, classification_weight, localization_weight)
    else:
        random_example_sampler = None
        if loss_config.HasField('random_example_sampler'):
            if loss_config.random_example_sampler.positive_sample_fraction <= 0:
                raise ValueError('RandomExampleSampler should not use non-positivevalue as positive sample fraction.')
            random_example_sampler = sampler.BalancedPositiveNegativeSampler(positive_fraction=(loss_config.random_example_sampler.positive_sample_fraction))
        elif loss_config.expected_loss_weights == loss_config.NONE:
            expected_loss_weights_fn = None
        else:
            if loss_config.expected_loss_weights == loss_config.EXPECTED_SAMPLING:
                expected_loss_weights_fn = functools.partial((ops.expected_classification_loss_by_expected_sampling),
                  min_num_negative_samples=(loss_config.min_num_negative_samples),
                  desired_negative_sampling_ratio=(loss_config.desired_negative_sampling_ratio))
            else:
                if loss_config.expected_loss_weights == loss_config.REWEIGHTING_UNMATCHED_ANCHORS:
                    expected_loss_weights_fn = functools.partial((ops.expected_classification_loss_by_reweighting_unmatched_anchors),
                      min_num_negative_samples=(loss_config.min_num_negative_samples),
                      desired_negative_sampling_ratio=(loss_config.desired_negative_sampling_ratio))
                else:
                    raise ValueError('Not a valid value for expected_classification_loss.')
    return (
     classification_loss, localization_loss, classification_weight,
     localization_weight, hard_example_miner, random_example_sampler,
     expected_loss_weights_fn)

def build_hard_example_miner(config, classification_weight, localization_weight):
    """Builds hard example miner based on the config.

  Args:
    config: A losses_pb2.HardExampleMiner object.
    classification_weight: Classification loss weight.
    localization_weight: Localization loss weight.

  Returns:
    Hard example miner.

  """
    loss_type = None
    if config.loss_type == losses_pb2.HardExampleMiner.BOTH:
        loss_type = 'both'
    if config.loss_type == losses_pb2.HardExampleMiner.CLASSIFICATION:
        loss_type = 'cls'
    if config.loss_type == losses_pb2.HardExampleMiner.LOCALIZATION:
        loss_type = 'loc'
    max_negatives_per_positive = None
    num_hard_examples = None
    if config.max_negatives_per_positive > 0:
        max_negatives_per_positive = config.max_negatives_per_positive
    if config.num_hard_examples > 0:
        num_hard_examples = config.num_hard_examples
    hard_example_miner = losses.HardExampleMiner(num_hard_examples=num_hard_examples,
      iou_threshold=(config.iou_threshold),
      loss_type=loss_type,
      cls_loss_weight=classification_weight,
      loc_loss_weight=localization_weight,
      max_negatives_per_positive=max_negatives_per_positive,
      min_negatives_per_image=(config.min_negatives_per_image))
    return hard_example_miner

def build_faster_rcnn_classification_loss(loss_config):
    """Builds a classification loss for Faster RCNN based on the loss config.

  Args:
    loss_config: A losses_pb2.ClassificationLoss object.

  Returns:
    Loss based on the config.

  Raises:
    ValueError: On invalid loss_config.
  """
    if not isinstance(loss_config, losses_pb2.ClassificationLoss):
        raise ValueError('loss_config not of type losses_pb2.ClassificationLoss.')
    loss_type = loss_config.WhichOneof('classification_loss')
    if loss_type == 'weighted_sigmoid':
        return losses.WeightedSigmoidClassificationLoss()
    if loss_type == 'weighted_softmax':
        config = loss_config.weighted_softmax
        return losses.WeightedSoftmaxClassificationLoss(logit_scale=(config.logit_scale))
    if loss_type == 'weighted_logits_softmax':
        config = loss_config.weighted_logits_softmax
        return losses.WeightedSoftmaxClassificationAgainstLogitsLoss(logit_scale=(config.logit_scale))
    if loss_type == 'weighted_sigmoid_focal':
        config = loss_config.weighted_sigmoid_focal
        alpha = None
        if config.HasField('alpha'):
            alpha = config.alpha
        return losses.SigmoidFocalClassificationLoss(gamma=(config.gamma),
          alpha=alpha)
    config = loss_config.weighted_softmax
    return losses.WeightedSoftmaxClassificationLoss(logit_scale=(config.logit_scale))

def _build_localization_loss(loss_config):
    """Builds a localization loss based on the loss config.

  Args:
    loss_config: A losses_pb2.LocalizationLoss object.

  Returns:
    Loss based on the config.

  Raises:
    ValueError: On invalid loss_config.
  """
    if not isinstance(loss_config, losses_pb2.LocalizationLoss):
        raise ValueError('loss_config not of type losses_pb2.LocalizationLoss.')
    loss_type = loss_config.WhichOneof('localization_loss')
    if loss_type == 'weighted_l2':
        return losses.WeightedL2LocalizationLoss()
    if loss_type == 'weighted_smooth_l1':
        return losses.WeightedSmoothL1LocalizationLoss(loss_config.weighted_smooth_l1.delta)
    if loss_type == 'weighted_iou':
        return losses.WeightedIOULocalizationLoss()
    raise ValueError('Empty loss config.')

def _build_classification_loss(loss_config):
    """Builds a classification loss based on the loss config.

  Args:
    loss_config: A losses_pb2.ClassificationLoss object.

  Returns:
    Loss based on the config.

  Raises:
    ValueError: On invalid loss_config.
  """
    if not isinstance(loss_config, losses_pb2.ClassificationLoss):
        raise ValueError('loss_config not of type losses_pb2.ClassificationLoss.')
    loss_type = loss_config.WhichOneof('classification_loss')
    if loss_type == 'weighted_sigmoid':
        return losses.WeightedSigmoidClassificationLoss()
    if loss_type == 'weighted_sigmoid_focal':
        config = loss_config.weighted_sigmoid_focal
        alpha = None
        if config.HasField('alpha'):
            alpha = config.alpha
        return losses.SigmoidFocalClassificationLoss(gamma=(config.gamma),
          alpha=alpha)
    if loss_type == 'weighted_softmax':
        config = loss_config.weighted_softmax
        return losses.WeightedSoftmaxClassificationLoss(logit_scale=(config.logit_scale))
    if loss_type == 'weighted_logits_softmax':
        config = loss_config.weighted_logits_softmax
        return losses.WeightedSoftmaxClassificationAgainstLogitsLoss(logit_scale=(config.logit_scale))
    if loss_type == 'bootstrapped_sigmoid':
        config = loss_config.bootstrapped_sigmoid
        return losses.BootstrappedSigmoidClassificationLoss(alpha=(config.alpha),
          bootstrap_type=('hard' if config.hard_bootstrap else 'soft'))
    raise ValueError('Empty loss config.')


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import sys
import time
import random
import threading
import logging

from six.moves import xrange

import numpy as np
import tensorflow as tf

flags = tf.app.flags

flags.DEFINE_string("save_path", None, "Directory to write the model and training sammaries.")
flags.DEFINE_string("train_data", None, "Training text file.")
flags.DEFINE_integer("embedding_size", 100, "The embedding dimension size.")
flags.DEFINE_integer("samples_to_train", 1, "Number of samples to train(*Million).")
flags.DEFINE_float("learning_rate", 0.025, "Initial learning rate.")
flags.DEFINE_integer("num_neg_samples", 5, "Negative samples per training example.")
flags.DEFINE_integer("batch_size", 1, "Number of training examples processed per step.")
flags.DEFINE_integer("num_threads", 4, "Number of threads training step.")
flags.DEFINE_integer("min_count", 5, "The min_count of edges for it to be included")
flags.DEFINE_integer("order", 2, "Model type, preserve first-order or second-order proximity.")
FLAGS = flags.FLAGS

class Options(object):
    '''Options used by LINE model.'''

    def __init__(self):
        #Model options.

        #Embedding dimension.
        self.emb_dim = FLAGS.embedding_size

        #training text file.
        self.train_data = FLAGS.train_data

        #Number of Negative samples per example.
        self.num_samples = FLAGS.num_neg_samples

        #The initial learning rate.
        self.learning_rate = FLAGS.learning_rate

        #Number of samples to train.
        self.samples_to_train = FLAGS.samples_to_train * 1000000

        #Number of threads.
        self.num_threads = FLAGS.num_threads

        #Number of examples for one training step.
        self.batch_size = FLAGS.batch_size

        #The minium number of edges for it to be included.
        self.min_count = FLAGS.min_count

        #Where to write out embeddings.
        self.save_path = FLAGS.save_path

        #order, can be 1 or 2.
        self.order = FLAGS.order

class LINE(object):
    '''LINE model.'''

    def __init__(self, options, session):
        self._options = options
        self._session = session
        self._vertex2id = {}
        self._id2vertex = []
        self._vid2degree = []
        self._edge_source_id = []
        self._edge_target_id = []
        self._edge_weight = []
        self._alias = []
        self._prob = []
        self._edge_count = 0
        self._read_data(self._options.train_data)
        self.InitAliasTable()
        self.build_graph()

    def _read_data(self, filename):
        '''Read data from training file.'''
        for line in open(filename):
            chunkes = line.strip().split()
            assert len(chunkes) == 3
            [name_v1, name_v2, weight] = chunkes
            weight = float(weight)
            '''Add current vertex to dict if it is not included yet.'''
            if not self._vertex2id.has_key(name_v1):
                self._vertex2id[name_v1] = len(self._vertex2id)
                self._id2vertex.append(name_v2)
                self._vid2degree.append(0)

            if not self._vertex2id.has_key(name_v2):
                self._vertex2id[name_v2] = len(self._vertex2id)
                self._id2vertex.append(name_v2)
                self._vid2degree.append(0)

            vid = self._vertex2id[name_v1]
            self._vid2degree[vid] += weight
            self._edge_source_id.append(vid)

            vid = self._vertex2id[name_v2]
            self._vid2degree[vid] += weight
            self._edge_target_id.append(vid)

            self._edge_weight.append(weight)

        self._edge_num = len(self._edge_weight)
        self._options.vertex_size = len(self._id2vertex)
        logging.info("Edge number : %d" % (self._edge_num))
        logging.info("Vertex number : %d" % (self._options.vertex_size))

    def InitAliasTable(self):
        '''The alias sampling algo, which is used to sample an edge in O(1) time.'''
        norm_prob = []
        large_block = []
        small_block = []

        sum = 0.0

        for weight in self._edge_weight:
            sum += float(weight)

        for k in xrange(self._edge_num):
            norm_prob.append(self._edge_weight[k] * self._edge_num / sum)

        num_small_block = 0
        num_large_block = 0
        cur_small_block = 0
        cur_large_block = 0

        for k in xrange(self._edge_num - 1, -1, -1):
            if norm_prob[k] < 1:
                small_block.append(k)
            else:
                large_block.append(k)

        num_small_block = len(small_block)
        num_large_block = len(large_block)
        assert (num_small_block + num_large_block == self._edge_num)

        for i in xrange(self._edge_num):
            self._alias.append(0)
            self._prob.append(0.0)

        while True:
            if num_small_block ==0 or num_large_block == 0:
               break
            num_small_block -= 1
            num_large_block -= 1
            cur_small_block = small_block[num_small_block]
            cur_large_block = large_block[num_large_block]
            self._prob[cur_small_block] = norm_prob[cur_small_block]
            self._alias[cur_small_block] = cur_large_block
            norm_prob[cur_large_block] = norm_prob[cur_small_block] + norm_prob[cur_small_block] - 1.0
            if norm_prob[cur_large_block] < 1:
                small_block[num_small_block] = cur_large_block
                num_small_block += 1
            else:
                large_block[num_large_block] = cur_large_block
                num_large_block += 1
        while num_large_block > 0:
            num_large_block -= 1
            self._prob[large_block[num_large_block]] = 1
        while num_small_block > 0:
            num_small_block -= 1
            self._prob[small_block[num_small_block]] = 1
        logging.info("prob table length:%d" % (len(self._prob)))
        logging.info("alias table length:%d" % (len(self._alias)))

    def SampleAnEdge(self):
            '''sample an edge in O(1).'''
            opts = self._options
            self._edge_count += opts.batch_size
            inputs = np.ndarray(shape = (opts.batch_size), dtype = np.int32)
            labels = np.ndarray(shape = (opts.batch_size), dtype = np.int32)
            for i in range(opts.batch_size):
                r_value1 = random.random()
                r_value2 = random.random()
                k = int(self._edge_num * r_value1)
                k = k if r_value2 < self._prob[k] else self._alias[k]
                inputs[i] = self._edge_source_id[k]
                labels[i] = self._edge_target_id[k]
            return inputs, labels

    def forward(self, sources, targets):
        '''Build the graph for the forward pass.'''
        opts = self._options
        #embedding:[vertex_size,emb_dim]
        init_width = 0.5
        #emb_vertex, order=1
        emb_vertex = tf.Variable(tf.random_uniform([opts.vertex_size, opts.emb_dim], -init_width, init_width), name = 'emb_vertex')
        #emb_context, order=2
        emb_context = tf.Variable(tf.zeros([opts.vertex_size, opts.emb_dim], dtype = tf.float32), name = 'emb_context')
        self._emb_vertex = emb_vertex
        self._emb_context = emb_context
        self.global_step = tf.Variable(0, trainable=False, name='global_step',dtype=tf.int32)
        targets_matrix = tf.reshape(tf.cast(targets, dtype = tf.int64), [opts.batch_size, 1])
        #Negative sampling.
        sampled_ids,_,_ = (tf.nn.fixed_unigram_candidate_sampler(
            true_classes = targets_matrix,
            num_true = 1,
            num_sampled = opts.num_samples,
            unique = True,
            range_max=opts.vertex_size,
            distortion = 0.75,
            unigrams = self._vid2degree
            ))
        #embedding for sources
        example_emb = tf.nn.embedding_lookup(emb_vertex, sources)

        #embedding for targets
        if opts.order == 1:
            target_emb = tf.nn.embedding_lookup(emb_vertex, targets)
        elif opts.order == 2:
            target_emb = tf.nn.embedding_lookup(emb_context, targets)
        else:
            logging.error("unsupported order: %d" % (opts.order))
            sys.exit(1)

        #true logits
        true_logits  = tf.reduce_sum(tf.mul(example_emb, target_emb), 1)

        #embeddings for negative-sampled vertex
        if opts.order == 1:
            sampled_emb = tf.nn.embedding_lookup(emb_vertex, sampled_ids)
        else:
            sampled_emb = tf.nn.embedding_lookup(emb_context, sampled_ids)
        #sampled logits
        sampled_logits = tf.matmul(example_emb,
                                  sampled_emb,
                                  transpose_b = True)

        return true_logits, sampled_logits

    def nce_loss(self, true_logits, sampled_logits):
        '''build  the graph for the NCE loss.'''

        #cross-entropy(logits, labels)
        opts = self._options
        true_xent = tf.nn.sigmoid_cross_entropy_with_logits(
            true_logits, tf.ones_like(true_logits))
        sampled_xent = tf.nn.sigmoid_cross_entropy_with_logits(
            sampled_logits, tf.zeros_like(sampled_logits))

        nce_loss_tensor = (tf.reduce_sum(true_xent) +
                          tf.reduce_sum(sampled_xent)) / opts.batch_size
        return nce_loss_tensor

    def optimize(self, loss):
        '''build the graph to optimize the loss function.'''
        opts = self._options
        #update learning rate.
        #self._lr = tf.train.exponential_decay(opts.learning_rate, self.global_step, 10000, 0.96, staircase=True)
        self._lr = opts.learning_rate * (1.0 - tf.cast(self.global_step, tf.float32) / float(opts.samples_to_train))
        optimizer = tf.train.GradientDescentOptimizer(self._lr)
        train = optimizer.minimize(loss,
                                   global_step = self.global_step,
                                   gate_gradients = optimizer.GATE_NONE
                                  )
        self._train = train

    def build_graph(self):
        '''build the graph for the full model.'''
        opts = self._options
        sources = tf.placeholder(tf.int32, shape = [opts.batch_size])
        self._sources = sources
        targets = tf.placeholder(tf.int32, shape = [opts.batch_size])
        self._targets = targets
        true_logits, sampled_logits = self.forward(sources, targets)
        loss = self.nce_loss(true_logits, sampled_logits)
        self._loss = loss
        self.optimize(loss)

        #Initialize all variables.
        tf.initialize_all_variables().run()

        #create a saver.
        self.saver = tf.train.Saver([self._emb_vertex])

    def _train_thread_body(self):
        opts = self._options
        while True:
            _sources, _targets = self.SampleAnEdge()
            feed_dict = {self._sources: _sources, self._targets: _targets}
            self._session.run([self._train], feed_dict=feed_dict)
            if self._edge_count >= opts.samples_to_train:
                break

    def train(self):
        '''trian the model.'''
        opts = self._options
        workers = []
        for _ in xrange(opts.num_threads):
            t = threading.Thread(target=self._train_thread_body)
            t.start()
            workers.append(t)

        #print training info.
        loss_list = []
        last_count, last_time = self._edge_count, time.time()
        while True:
            time.sleep(0.5)
            _sources, _targets = self.SampleAnEdge()
            feed_dict = {self._sources: _sources, self._targets: _targets}
            (lr, loss, _) = self._session.run([self._lr, self._loss, self._train],feed_dict=feed_dict)
            now = time.time()
            rate = (self._edge_count - last_count) / (now - last_time)
            progress = 100 * (self._edge_count) / float(opts.samples_to_train)
            last_time = now
            last_count = self._edge_count
            loss_list.append(loss)
            average_loss = np.mean(np.array(loss_list))
            print ("Learning rate:%5.3f loss:%6.2f average loss: %f edges/sec:%8.0f progress:%f%%\r" % (
                lr, loss, average_loss, rate, progress
            ), end = "")
            sys.stdout.flush()
            if self._edge_count >= opts.samples_to_train:
                break
        print  ("")
        for t in workers:
            t.join()

def main(_):
    logging.basicConfig(level = logging.INFO)
    if not FLAGS.train_data:
        logging.error('no train file.')
        sys.exit(1)
    opts = Options()
    with tf.Graph().as_default(), tf.Session() as session:
        with tf.device("/cpu:0"):
            model = LINE(opts, session)
        model.train()
        model.saver.save(session,
                        os.path.join(opts.save_path,"emb.txt"),global_step=model.global_step)

if __name__ == '__main__':
    '''This is for test.'''
    tf.app.run()


from flask import Flask
from flask import request
from flask import jsonify
from flask import Response
from flask import abort
from flask_cors import CORS
from weather import weather
from api_errors import *
import datetime

app = Flask(__name__)
app.url_map.strict_slashes = False
cors = CORS(app)

@app.errorhandler(ApiError)
def handle_api_error(error):
    response = jsonify(error.to_dict())
    response.status_code = error.status_code
    return response

@app.route('/api/historical', methods=['GET'])
def historical():
    dates = weather.get_historical()
    date_dict_array = [{'DATE':date} for date in dates]
    return jsonify(date_dict_array), 200

@app.route('/api/historical/<date>', methods=['GET'])
def get_date(date):
    validate_date(date)
    date_info = weather.get_date(date)
    return jsonify(date_info), 200

@app.route('/api/historical', methods=['POST'])
def add_date():
    content = request.get_json()
    if 'DATE' not in content or 'TMAX' not in content or 'TMIN' not in content:
        raise ApiError('A parameter is missing.', status_code=400)

    validate_date(content['DATE'])
    date_text = content['DATE']

    tmax = 0.0
    try:
        tmax = float(content['TMAX'])
    except ValueError:
        raise ApiError('TMAX cannot be converted to a float.', status_code=400)

    tmin = 0.0
    try:
        tmin = float(content['TMIN'])
    except ValueError:
        raise ApiError('TMIN cannot be converted to a float.', status_code=400)

    date = weather.add_date(date_text, tmax, tmin)
    return jsonify({'DATE':date['DATE']}), 201

@app.route('/api/forecast/<start_date>', methods=['GET'])
def get_forecast(start_date):
    validate_date(start_date)
    forecast = weather.get_forecast(start_date)
    return jsonify(forecast), 200

@app.route('/api/historical/<date>', methods=['DELETE'])
def delete_date(date):
    validate_date(date)
    weather.delete_date(date)
    return ('', 204)

def validate_date(date_text):
    if type(date_text) is not str:
        raise ApiError("Incorrect data type.", 400)
    try:
        if date_text != datetime.datetime.strptime(date_text, r'%Y%m%d').strftime(r'%Y%m%d'):
            raise ValueError
    except ValueError:
        raise ApiError("Incorrect date format, should be YYYYMMDD.", 400)

# Start flask server when calling on the file
if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=False, port=80)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.utils.translation import gettext_lazy as __
from django.shortcuts import redirect, reverse
from django.shortcuts import render
from django.db import transaction
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django import forms

from bioresources.models.Resource import Collaboration
from bioseq.models.Taxon import  Taxon

@login_required
def submission_start(request):
    return render(request, 'submission/submission_start.html', {})

from django_select2.forms import ModelSelect2Widget

class TaxSelect(ModelSelect2Widget):
    def label_from_instance(self,obj):
        return str(obj.scientific_name())

class TaxChoiceField(forms.ChoiceField):
    def valid_value(self, value):
        return True
    def clean(self, value):
        value = super(self.__class__,self).clean(value)
        if value:
            return Taxon.objects.get(taxon_id=value)
        return None

def form_clean_data(form):
    cleaned_data = super(form.__class__, form).clean()
    qs = form._meta.model.objects.filter(name=cleaned_data["name"])
    # if cleaned_data["ncbi_tax"]:
    #     form.data["ncbi_tax"] = Taxon.objects.get(taxon_id=cleaned_data["ncbi_tax"])
    if "pk" in form.data:
        if qs.exclude(id=form.data["pk"]).exists():
            form._errors['name'] = form._errors.get('name', [])
            form._errors['name'].append(__("%s already exists") % cleaned_data["name"])
    else:
        if qs.exists():
            form._errors['name'] = form._errors.get('name', [])
            form._errors['name'].append(__("%s already exists") % cleaned_data["name"])

def submit_model(form_class, request):
    if request.method == 'POST':
        if "pk" in request.GET:
            obj = form_class._meta.model.objects.get(id=request.GET["pk"])
            form = form_class(request.POST, instance=obj)
        else:
            form = form_class(request.POST)

        if form.is_valid():
            with transaction.atomic():
                obj = form.save()
                if not Collaboration.objects.filter(resource=obj, person=request.user.person).exists():
                    Collaboration.objects.create(resource=obj, person=request.user.person,
                                                 type=Collaboration.COLLABORATION_TYPES.owner)
            return HttpResponseRedirect(reverse("bioresources:" + obj.type_name() + "_view", args=[obj.id]))
    else:
        if "pk" in request.GET:
            resource = form_class._meta.model.objects.get(id=request.GET["pk"])
            form = form_class(instance=resource)
        else:
            form = form_class()

    data = {'form': form}
    if "pk" in request.GET:
        data["pk"] = request.GET["pk"]

    return render(request, 'submission/tool_submission.html', data)


from datetime import timedelta
from functools import update_wrapper
from flask import Flask, json, render_template, request, redirect, jsonify, request, make_response
from catboost import CatBoostRegressor
from flask.globals import current_app
from flask.helpers import make_response
from flask_sqlalchemy import SQLAlchemy
import sqlite3
from flask_cors import CORS
from Python_algo.support import user_details

import pandas as pd
import numpy as np
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///burnout.db'
db = SQLAlchemy(app)
CORS(app)

db_file = "burnout.db"

# Entry class for Db stuff
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    emp_id = db.Column(db.String(25), unique=True, nullable=False)
    date = db.Column(db.String(20), nullable=False)
    Gender = db.Column(db.String(6), nullable=False)
    company = db.Column(db.String(10), nullable=False)
    WFH = db.Column(db.String(10), nullable=False)
    Designation = db.Column(db.Float, nullable=False)
    Resource_Allocation = db.Column(db.Float, nullable=False)

    def __repr__(self):
        return f"User('{self.emp_id}', '{self.date}', '{self.Gender}', {self.Designation}, {self.Resource_Allocation})"

class bout(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    emp_id = db.Column(db.String(25), nullable=False)
    Burn = db.Column(db.Float, nullable=False)

    def __repr__(self):
        return f"User('{self.emp_id}', {self.fat})"

def check_user_db(emp_id):
    conn = sqlite3.connect(db_file)
    cur = conn.cursor()
    names = cur.execute(
        "SELECT emp_id FROM User WHERE emp_id='{emp_id}'".format(emp_id=emp_id))
    nam = names.fetchall()
    for i in nam:
        if(i[0] == emp_id):
            return True
        else:
            return False

from_file = CatBoostRegressor()
model = from_file.load_model("model.pkl")

@app.route('/')
def hello_world():
    return render_template("index.html")

@app.route('/error')
def error():
    return render_template("404.html")

@app.route('/login', methods=['POST'])
def signup():
    req = request.form

    emp1 = str(req.get('employee_id'))
    Men = float(req.get('mental_fatigue'))
    print(emp1, Men)
    respo = "You are not a user, Please sign up"

    details = user_details(emp1, Men)
    burnout = model.predict(details)

    if (check_user_db(emp1)):
        try:
            entry = bout(emp_id=emp1, Burn=burnout)
            db.session.add(entry)
            db.session.commit()

            return str(burnout)
        except Exception as e:
            return str(burnout)
    else:
        return respo

@app.route('/submit', methods=['POST'])
def submit():
    if request.method == 'POST':

        emp = request.form['Employee Id']
        date_emp = (request.form['date'])
        Gender = int(request.form['Gender'])
        Com = int(request.form['Company type'])
        WFH = int(request.form['WFH'])
        Des = float(request.form['Designation'])
        Res = float(request.form['Resorce Allocation'])

        if (not check_user_db(emp)):
            if(Gender == 0):
                G="Female"
            else:
                G="Male"
            if Com == 0:
                c = "Service"
            else:
                c = "Product"
            if WFH == 0:
                w = "Not WFH"
            else:
                w = "WFH"
            # try:
            entry = User(emp_id=emp, date=date_emp, Gender=G, company=c,
                         WFH=w, Designation=Des, Resource_Allocation=Res)
            db.session.add(entry)
            db.session.commit()

            return redirect("/")

        else:
            return "You are already registered! Please login!!"

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


import pygame
import datetime
import maingui
pygame.init()
display_width = 800
display_height = 480
black = (255,255,255)
single_click=True
Display = pygame.display.set_mode((display_width,display_height))
clock = pygame.time.Clock()
def close():
            maingui.main_window()
def wallpaper_window():
    single_click=True
    click = pygame.mouse.get_pressed()
    background = pygame.image.load("/home/pi/Desktop/package/snowboy/icon/wallpaper.jpg").convert()
    font = pygame.font.Font("/home/pi/Desktop/package/snowboy/icon/COMIC.TTF",50)
    currentime = datetime.datetime.time(datetime.datetime.now())
    Date=datetime.date.today().strftime("%A")[:3]+" /"+datetime.date.today().strftime("%B")[:3]+" /"+str(datetime.date.today().strftime("%d"))
    time1= font.render(currentime.strftime("%I:%M %p"), 1, black)
    date=font.render(Date, True, black)
    Display.blit(background,[0,0])
    while True:
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                close()
            elif event.type==pygame.MOUSEBUTTONDOWN:
                close()
        Display.blit(background,[0,0])
        Display.blit(time1,[280,10])
        Display.blit(date,[260,65])
        pygame.display.update()
        clock.tick(5)
if __name__ == "__main__":
     wallpaper_window()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2008 Zuza Software Foundation
#
# This file is part of Spelt.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

from lxml     import etree, objectify
from StringIO import StringIO

from spelt.models.xml_model import XMLModel

class TestXMLModel:
    """Unit test for XMLModel class."""

    xml = StringIO("""
        <person sex="male" race="chinese">
            <height>182</height>
            <weight>70.5</weight>
            <notes></notes>
        </person>
        """)

    def __init__(self):
        self.model = XMLModel(
            'person',
            values=['height', 'weight', 'notes'],
            attribs=['sex', 'race'],
            elem=objectify.parse(TestXMLModel.xml).getroot()
        )

    def test_from_xml(self):
        """
        Test that XMLModel.from_xml() works by checking that members are assigned
        according to the hard-coded values represented in xml.
        """
        assert self.model.sex == 'male'
        assert self.model.race == 'chinese'
        assert self.model.height == '182'
        assert self.model.weight == '70.5'
        assert self.model.notes == ''

    def test_to_xml(self):
        """
        Test that XMLModel.to_xml() works by comparing the source
        lxml.objectify.ObjectifiedElement used to create a XMLModel and the element
        returned by to_xml().
        """
        toroot = self.model.elem

        assert self.model.sex    == toroot.get('sex')
        assert self.model.race   == toroot.get('race')
        assert float(self.model.height) == float(toroot.height)
        assert float(self.model.weight) == float(toroot.weight)
        assert self.model.notes  == toroot.notes

if __name__ == '__main__':
    test = TestXMLModel()
    test.test_from_xml()
    test.test_to_xml()