import numpy as np
import queue

def slide_puzzle(array):
    puzzle = Puzzle(array)
    puzzle.solver()
    return puzzle.steps

class Puzzle():
    def __init__(self, array):
        self.puzzle = np.array(array, dtype=np.object)
        self.solved = np.zeros_like(self.puzzle, dtype=int)
        self.height, self.width = self.puzzle.shape
        self.steps = []

        for i, row in enumerate(self.puzzle):
            for j, value in enumerate(row):
                self.puzzle[i,j] = Case(value)
                self.puzzle[i,j].y, self.puzzle[i,j].x = i, j
                self.solved[i, j] = 1+j+self.width*i
        self.solved[-1, -1] = 0

        # Create all paths
        for row in self.puzzle:
            [case.get_paths(self.puzzle) for case in row]

    def clear(self):
        [case.clear() for case in np.ravel(self.puzzle)]

    def solver(self):
        self.reduce_problem()
        self.solve()

    # Solve the reduced problem
    def solve(self):
        for i in range(24):
            solution = self.puzzle[-3:, -3:].ravel().tolist()[:-1]
            solution = [s.value for s in solution]
            if sorted(solution) == solution:
                return True

            objective = self.find_number(0)
            if i < 12:
                if objective.y == self.height-1 and objective.x == self.width-1:
                    board_piece = self.puzzle[-2, -1]
                if objective.y == self.height-2 and objective.x == self.width-1:
                    board_piece = self.puzzle[-2, -2]
                if objective.y == self.height-2 and objective.x == self.width-2:
                    board_piece = self.puzzle[-1, -2]
                if objective.y == self.height-1 and objective.x == self.width-2:
                    board_piece = self.puzzle[-1, -1]
            if i >= 12:
                if objective.y == self.height-1 and objective.x == self.width-1:
                    board_piece = self.puzzle[-1, -2]
                if objective.y == self.height-2 and objective.x == self.width-1:
                    board_piece = self.puzzle[-1, -1]
                if objective.y == self.height-2 and objective.x == self.width-2:
                    board_piece = self.puzzle[-2, -1]
                if objective.y == self.height-1 and objective.x == self.width-2:
                    board_piece = self.puzzle[-2, -2]
            self.steps.append(board_piece.value)
            objective.swap(board_piece)
        self.steps = None

    # reduce the problem to a 2x2 grid
    def reduce_problem(self):
        for i in range(self.height-2):
            helper = (self.puzzle[i+1, -1], self.puzzle[i+1, -2],
                      self.puzzle[i+2, -1], self.puzzle[i+2, -2])
            self.look_line(self.puzzle[i, :], self.solved[i, :], helper)

            helper = (self.puzzle[-1, i+1], self.puzzle[-2, i+1],
                      self.puzzle[-1, i+2], self.puzzle[-2, i+2])
            self.look_line(self.puzzle[i+1:, i], self.solved[i+1:, i], helper)

        return True

    # For a given line, how it should be
    def look_line(self, line, solutions, helper):
        for case, solution in zip(line[:-2], solutions[:-2]):
            self.solve_number(case, solution)

        self.solve_number(helper[2], solutions[-1], is_solved=False)
        self.solve_number(helper[3], solutions[-2], is_solved=False)

        self.solve_number(line[-1], solutions[-2], is_solved=False)
        self.solve_number(helper[0], solutions[-1], is_solved=False)

        if line[-2] != self.find_number(0):
            self.solve_number(helper[1], line[-2].value, ignore=[helper[0], line[-1]], is_solved=False)

        self.solve_number(line[-2], solutions[-2])
        self.solve_number(line[-1], solutions[-1])

        return True

    def solve_number(self, position, number, ignore=None, is_solved=True):
        while position.value != number:
            origin = self.find_number(0)
            # Find the number that should be at the position
            objective = self.find_number(number)

            # look for the best adjecent case, relative to the
            # current position
            adjacent = objective.best_adjacent(self.puzzle,position)

            # Find the shortest path
            path = origin.astar(self, adjacent, objective, ignore)

            # Make the move
            for one, two in zip(path, path[1:]):
                self.steps.append(two.value)
                one.swap(two)
            else:
                self.steps.append(objective.value)
                path[-1].swap(objective)
        if is_solved:
            position.is_solved = True
        return True

    def find_number(self, num):
        for row in self.puzzle:
            for node in row:
                if node.value == num:
                    return node
        return None

class Case():
    def __init__(self, value):
        self.value = value
        self.is_solved = False
        self.paths = []
        self.distance = np.Infinity
        self.back = None

    def __gt__(self, other):
        return self.distance > other.distance

    def __repr__(self):
        return str(self.value)

    def clear(self):
        self.distance = np.Infinity
        self.back = None

    def distance_to(self, other):
        return abs(self.y-other.y)+abs(self.x-other.x)

    def swap(self, other):
        self.value, other.value = other.value, self.value
        self.is_solved, other.is_solved = other.is_solved, self.is_solved

    def get_paths(self, puzzle):
        height, width = puzzle.shape
        if self.y > 0:
            self.paths.append(puzzle[self.y-1, self.x])
        if self.y < height-1:
            self.paths.append(puzzle[self.y+1, self.x])
        if self.x > 0:
            self.paths.append(puzzle[self.y, self.x-1])
        if self.x < width-1:
            self.paths.append(puzzle[self.y, self.x+1])

    def best_adjacent(self, puzzle, relative):
        possible_paths = []

        for path in self.paths:
            if not path.is_solved:
                possible_paths.append((relative.distance_to(path), path))

        if not possible_paths:
            # Error in the solution
            print(puzzle)

        solution = sorted(possible_paths, key=lambda x:x[0])[0]
        return solution[1]

    def astar(self, puzzle, objective, number, ignore=None):
        ignore = ignore or []

        queuee = queue.PriorityQueue()
        self.distance = 0

        queuee.put((0, self))

        while not queuee.empty():
            _, case = queuee.get()
            if case == objective:
                break

            for pos_b in case.paths:
                if pos_b.back != case:
                    if pos_b != number and not pos_b.is_solved and pos_b not in ignore:
                        if pos_b.distance > 1+case.distance:
                            pos_b.distance = 1+case.distance
                            pos_b.back = case
                            queuee.put((pos_b.distance_to(objective), pos_b))

        node = objective
        path = []

        while(node.back):
            path.append(node)
            node = node.back
        path.append(node)

        puzzle.clear()
        return list(reversed(path))

simple_example = [
        [ 1, 2, 3, 4],
        [ 5, 0, 6, 8],
        [ 9,10, 7,11],
        [13,14,15,12]
    ]
print(slide_puzzle(simple_example))


import tensorflow as tf
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.python.keras import backend as K
from tensorflow.keras.preprocessing import image
from tensorflow.contrib.opt import AdamWOptimizer
from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score, auc, roc_curve, roc_auc_score
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import copy
from sklearn.decomposition import PCA
from sklearn.decomposition import TruncatedSVD
from sklearn.preprocessing import normalize
from sklearn.ensemble import IsolationForest
from sklearn.svm import OneClassSVM
from sklearn.neighbors import NearestNeighbors
from sklearn.metrics import average_precision_score
import models
import data
import os
import sys
from sklearn.neighbors import NearestNeighbors
from sklearn.neighbors import RadiusNeighborsClassifier
import numpy as np
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn import random_projection

dataName = 'multimnist'
batchSize = 32
learnRate = 0.001
positive_ratio = 0.003
maxEpochs=201
repeat = 5
hop = 3
K = 30

P_X,U_X,N_list,N_list1,N_list2,A_list,testX,testY=data.load(dataName)

A_X = P_X[0:int(positive_ratio*len(N_list)) ]
U_shuffle_list = [x for x in range(len(U_X))]
N_shuffle_list = [x for x in range(len(N_list))]
A_shuffle_list = [x for x in range(len(A_X))]
dim_list = [784,128,64,32]

final_auc_history=[]
final_pr_history=[]
best_auc_history=[]

trainX = np.concatenate((U_X,A_X),0)

for rep in range(repeat):
    tf.reset_default_graph()

    with tf.Graph().as_default() as graph:

        config = tf.ConfigProto()
        config.gpu_options.allow_growth=True

        with tf.Session(config=config, graph=graph) as sess:

            model = models.LSAD("LS",hop,dim_list,K)
            model.build_optimizer(learning_rate = learnRate)

            #TPA
            print("\nConverting a set of data points to simplicial complexes\n")
            if K <= 0:
                params={}
                params['TRAIN']=True
                params['K']=1
                train_indices = model.TPA(trainX,params,'persistent-homology')
                train_graph_tensor = tf.SparseTensorValue(train_indices, [1./(params['K'])]*len(train_indices),
                                       (len(trainX),len(trainX)))

                params['TRAIN']=False
                indices = model.TPA(testX,params,'persistent-homology')
                test_indices = np.concatenate( (train_indices,indices),0)
                test_graph_tensor=tf.SparseTensorValue(test_indices, [1./(params['K'])]*len(test_indices),
                                      (len(trainX)+len(testX),len(trainX)+len(testX)))
            else:
                params={}
                params['TRAIN']=True
                params['K']= K+1
                train_indices = model.TPA(trainX,params,'closest-K')
                train_graph_tensor = tf.SparseTensorValue(train_indices, [1./(params['K'])]*len(train_indices),
                                       (len(trainX),len(trainX)))

                params['TRAIN']=False
                indices = model.TPA(testX,params,'closest-K')
                test_indices = np.concatenate( (train_indices,indices),0)
                test_graph_tensor=tf.SparseTensorValue(test_indices, [1./(params['K'])]*len(test_indices),
                                      (len(trainX)+len(testX),len(trainX)+len(testX)))

            sess.run(tf.global_variables_initializer())
            for epoch in range(maxEpochs):
                print("Training: [%s/%s]"%(epoch+1,maxEpochs))

                np.random.shuffle(A_shuffle_list)
                np.random.shuffle(U_shuffle_list)

                trainSize= min( len(U_shuffle_list),len(A_shuffle_list) )

                batch_U_list = np.random.choice( np.arange(len(U_shuffle_list)), int(batchSize/2))
                batch_A_list = np.random.choice( np.arange(len(A_shuffle_list)), int(batchSize/2))

                #train
                sess.run(model.trainStep, feed_dict={
                    model.X : trainX,
                    model.A : train_graph_tensor,
                    model.P_Indices: len(U_X) + np.array(batch_A_list),
                    model.U_Indices: np.array(batch_U_list)

                })

            #Test
            test_predicted_label=sess.run(model.scores,
                                    feed_dict={
                                        model.X: np.concatenate((trainX,testX),0),
                                        model.A: test_graph_tensor,
                                        model.U_Indices: len(trainX)+np.arange(len(testX))
                                    })

            test_AUC = roc_auc_score(testY,test_predicted_label)
            test_PR  = average_precision_score(testY,test_predicted_label)
            print("ROC:%s PR:%s"%(test_AUC,test_PR))

    final_auc_history.append(test_AUC)
    final_pr_history.append(test_PR)

print("--------------------------------------------------")
print("DataName:%s Labeled_anomaly:%s batchSize:%s learningRate:%s DeepGPU K:%s hop:%s" %(dataName,int(positive_ratio*len(N_list)),batchSize,learnRate,K,hop))
print("ROC history %s"%(final_auc_history))
print("PR history %s"%(final_pr_history))
print("averaged ROC stddev averaged PR stddev")
print(np.mean(final_auc_history) , np.std(final_auc_history), np.mean(final_pr_history), np.std(final_pr_history) )


"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param inorder: A list of integers that inorder traversal of a tree
    @param postorder: A list of integers that postorder traversal of a tree
    @return: Root of a tree
    """
    def buildTree(self, inorder, postorder):
        # write your code here
        def build(ileft, iright, pleft, pright):
            if ileft > iright or pleft > pright: return None
            cur, i = TreeNode(postorder[pright]), 0
            for i in range(ileft, len(inorder)):
                if inorder[i] == cur.val: break
            cur.left = build(ileft, i-1, pleft, pleft+i-ileft-1)
            cur.right = build(i+1, iright, pleft+i-ileft, pright-1)
            return cur
        return build(0, len(inorder)-1, 0, len(postorder)-1)


from argparse import ArgumentParser
from io import BytesIO
from pathlib import Path

from PIL import Image, UnidentifiedImageError
from pony.orm import db_session
from pony.orm.core import commit
from requests import get

from constant.error import INVALID_IMAGE, INVALID_SKIN
from db import Profile

@db_session
def call(program, argv):
    parser = ArgumentParser(prog=program)

    parser.add_argument("dbid", help="DBID of profile", type=int)

    sources = parser.add_mutually_exclusive_group(required=True)
    sources.add_argument("-u", "--url", help="url of skin", dest="url")
    sources.add_argument("-f", help="file path of skin", dest="path")
    sources.add_argument("--delete", help="reset profile skin", action="store_true")

    parser.add_argument("--slim", help="slim model", action="store_true")

    args = parser.parse_args(argv)

    profile: Profile = Profile.get(id=args.dbid)
    if profile is None:
        print("No profile matches that DBID!")
        exit(1)

    if args.url is not None:
        fd = BytesIO(get(args.url).content)
    elif args.path is not None:
        fd = Path(args.path)
    elif not args.delete:
        print("You must specify a file!")
        exit(1)

    if args.delete:
        profile.reset_skin()
    else:
        try:
            image: Image = Image.open(fd)
        except (FileNotFoundError, UnidentifiedImageError, ValueError):
            print(INVALID_IMAGE.message)
            exit(1)

        try:
            profile.update_skin(image, "slim" if args.slim else "")
        except ValueError:
            print(INVALID_SKIN.message)
            exit(1)

    try:
        commit()
    except Exception as e:
        print(e)
        exit(1)

    if not args.delete:
        print(f"Texture name: {profile.profile_skin.name}")


__NetID__ = "joshruff"
__GitHubID__ = "joshruff"
__SelfGrade__ = "5"
__Challenge__ = "4"

"""
Random Signals and Systems
Course: ECEN 303-502
Maximum Grade: 5pt
"""

import random
import math
import numpy
import pylab

TrialNumber = 100000

UniformList = []
RayleighList = []
for trial1 in range(0, TrialNumber):
    UniformList.append(random.uniform(0, 2*math.pi))
    RayleighList.append(numpy.random.rayleigh(1))

pylab.figure()
n, bins, patches = pylab.hist(UniformList, 1000, normed=1, histtype='stepfilled')
pylab.setp(patches, 'facecolor', 'k', 'alpha', 0.75)

pylab.figure()
n, bins, patches = pylab.hist(RayleighList, 1000, normed=1, histtype='stepfilled')
pylab.setp(patches, 'facecolor', 'g', 'alpha', 0.75)

pylab.show()

Sequence1 = []
Sequence2 = []
Sequence3 = []
for trial2 in range(0, TrialNumber):
    Sequence1.append(math.sin(UniformList[trial2]) * RayleighList[trial2])
    Sequence2.append(math.cos(UniformList[trial2]) * RayleighList[trial2])
    Sequence3.append(Sequence1[trial2]**2 + Sequence2[trial2]**2)

pylab.figure()
n, bins, patches = pylab.hist(Sequence1, 1000, normed=1, histtype='stepfilled')
pylab.setp(patches, 'facecolor', 'r', 'alpha', 0.75)

pylab.figure()
n, bins, patches = pylab.hist(Sequence2, 1000, normed=1, histtype='stepfilled')
pylab.setp(patches, 'facecolor', 'g', 'alpha', 0.75)

pylab.figure()
n, bins, patches = pylab.hist(Sequence3, 1000, normed=1, histtype='stepfilled')
pylab.setp(patches, 'facecolor', 'b', 'alpha', 0.75)

print(numpy.cov(Sequence1,Sequence2))

pylab.show()

#
# EDIT
#

"""
What is the type of random variable `Sequence1`?
	-Gaussian
What is its mean and variance?
	-Mean is 0
	-Variance is 1

What is the type of random variable `Sequence2`?
	-Gaussian
What is its mean and variance?
	-Mean is 0
	-Variance is 1

What is the type of random variable `Sequence3`?
	-Exponential
What is its mean and variance?
	-Lambda is 0.5
	-Mean is 1/Lambda = 2
	Variance is 1/Lambda^2 = 4

What is the empirical covariance between `Sequence1` and `Sequence2`? 0
Do you think they are independent? Justify your answer. Yes. The sum of the expectation of sequence 1 and sequence 2 equals the expectation of the sums.
"""


import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import argparse

from tensorflow.contrib import learn

import os
import sys
sys.path.insert(0, os.path.abspath('..'))
from model.lstm_model import lstm_model
from bin.nnTools import read_dataset, read_config, build_params

tf.logging.set_verbosity(tf.logging.INFO)

if __name__ == "__main__":
    parser = argparse.ArgumentParser('Training a neural net')
    parser.add_argument('in_file', help='The input config file')
    parser.add_argument('-e', '--ephocs', type=int, default=10000, help='The number of epochs')
    args = parser.parse_args()
    CONFIG_PATH = args.in_file

    configData= read_config(CONFIG_PATH)
    filePathTrain = configData.loc[0,'TRAIN_PATH']
    filePathTest = configData.loc[0,'TEST_PATH']
    LOG_DIR = configData.loc[0,'LOG_DIR']

    epochs = args.ephocs
    saveSteps = epochs/100 if epochs > 10000 else epochs/10
    model_params = build_params(configData, epochs)

    # Read in the training data and get back a list of (ins, labels) which is the
    # training data and training labels
    # (train_input_fn, instrain, labelstrain) = read_dataset(filePathTrain, model_params,
                                                 # mode=tf.contrib.learn.ModeKeys.TRAIN)
    # (test_input_fn, ins,  labels) = read_dataset(filePathTest, model_params,
                                                 # mode=tf.contrib.learn.ModeKeys.INFER)

    training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
        filename=filePathTrain,
        target_dtype=np.int,
        features_dtype=np.float32)
    # Build 3 layer DNN with 10, 20, 10 units respectively.
    feature_columns = [tf.feature_column.numeric_column("x", shape=[256])]

    # Need to add weightColumn which captures the boosting weights during instantiation of this classifier
    classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
                                            hidden_units=[10, 20, 10],
                                            n_classes=2,
                                            model_dir="./test/")

    # Define the training inputs
    train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": np.array(training_set.data)},
        y=np.array(training_set.target),
        num_epochs=None,
        shuffle=False)
    classifier.train(input_fn=train_input_fn, steps=200)
    train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": np.array(training_set.data)},
        y=np.array(training_set.target),
        num_epochs=1,
        shuffle=False)
    predictions = classifier.predict(input_fn=train_input_fn);
    arrs = [pred['class_ids'][0] for pred in predictions]
    # print(arrs)
    np.savetxt("foo.csv", arrs, delimiter=",")
    exit()
    for pred in predictions:
        print(pred['class_ids'][0])

    training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
        filename=filePathTest,
        target_dtype=np.int,
        features_dtype=np.float32)
    test_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": np.array(training_set.data)},
        y=np.array(training_set.target),
        num_epochs=1,
        shuffle=False)
    # Evaluate accuracy.
    accuracy_score = classifier.evaluate(input_fn=test_input_fn)["accuracy"]

    print("\nTest Accuracy: {0:f}\n".format(accuracy_score))

    # # Classify two new flower samples.
    # new_samples = np.array(
        # [[6.4, 3.2, 4.5, 1.5],
         # [5.8, 3.1, 5.0, 1.7]], dtype=np.float32)
    # predict_input_fn = tf.estimator.inputs.numpy_input_fn(
        # x={"x": new_samples},
        # num_epochs=1,
        # shuffle=False)

    # predictions = list(classifier.predict(input_fn=predict_input_fn))
    # predicted_classes = [p["classes"] for p in predictions]

    # print(
        # "New Samples, Class Predictions:    {}\n"
        # .format(predicted_classes))


# Copyright (C) 2017 Beijing Didi Infinity Technology and Development Co.,Ltd.
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

import sys
import tensorflow as tf
from sklearn import metrics

import utils

class tf_roc():

  def __init__(self, predict_label_file, true_label_id, threshold_num,
               save_dir):
    '''
        file format: dataid,predict_score,label
        Args:
          predict_score should be between [0, 1]
          label should be {0 , 1}
          threshold_num: number of threshold will plot
        '''
    self.threshold_num = threshold_num
    self.true_label_id = true_label_id  # True exmaple label id, 0 or 1

    self.trues = 0  #total of True labels
    self.fpr = []  #false positive
    self.tpr = []  #true positive
    self.ths = []  #thresholds

    self.save_dir = save_dir
    self.writer = tf.summary.FileWriter(self.save_dir)

    #load predict_label_file to predicts and labels
    self.predicts = []
    self.labels = []
    self.total = 0
    fd = open(predict_label_file)
    fdl = fd.readline()
    while len(fdl) > 0:
      val = fdl.split(',')  # comma seperate
      self.predicts.append(float(val[-1]))  # postive score
      self.labels.append(
          True if int(val[-2]) == self.true_label_id else False)  # True label
      fdl = fd.readline()
      self.total += 1
    fd.close()

  def roc_pr(self):
    '''
        tn, fp, fn, tp = metrics.confusion_matrix(self.labels, self.predicts)
        fpr = tp / (fp + tn)
        fnr = fn / (fn + tp)
        utils.plot_det(fpr, fnr)
        '''

    # using True to replace POS exmaple
    # ROC curve
    self.fpr, self.tpr, self.ths = metrics.roc_curve(
        y_true=self.labels, y_score=self.predicts, pos_label=1)
    print('fpr', self.fpr)
    print('tpr', self.tpr)
    print('ths', self.ths)
    self.auc = metrics.auc(self.fpr, self.tpr)
    print('AUC', self.auc)

    utils.plot_roc(
        self.fpr, self.tpr, self.ths, self.auc, save_path='output/roc.png')

    # PR-curve
    self.precision, self.recall, thresholds = metrics.precision_recall_curve(
        y_true=self.labels, probas_pred=self.predicts, pos_label=1)
    print("precision len", len(self.precision))
    print("recall len", len(self.recall))
    print("thresholds len", len(thresholds))
    utils.plot_pr(
        self.precision, self.recall, thresholds, save_path='output/pr.png')

  def calc(self):
    for label in self.labels:
      if label:
        self.trues += 1
    threshold_step = 1. / self.threshold_num
    for t in range(self.threshold_num + 1):
      th = 1 - threshold_step * t  # from high to low
      tn, tp, fp, fpr, tpr = self._calc_once(th)
      self.fpr.append(fpr)
      self.tpr.append(tpr)
      self.ths.append(th)
      self._save(fpr, tpr)

  def _save(self, fpr, tpr):
    summt = tf.Summary()
    summt.value.add(tag="roc", simple_value=tpr)
    self.writer.add_summary(summt, fpr * 100)  #for tensorboard step drawable
    self.writer.flush()

  def _calc_once(self, t):
    fp = 0
    tp = 0
    tn = 0

    for i in range(self.total):
      if not self.labels[i]:  # label False
        if self.predicts[i] >= t:  # predict Postive
          fp += 1
        else:  # predict Negtive
          tn += 1
      elif self.predicts[i] >= t:  # label True
        tp += 1  # predict Postive

    print(tp, fp, tn)

    tpr = tp / float(self.trues)
    #fpr = fp / float(fp + tn) #precision
    fpr = fp / float(fp + tp)  #detection
    return tn, tp, fp, fpr, tpr

if __name__ == '__main__':
  predict_label_file = sys.argv[1]
  save_dir = 'ckpt'
  true_label_id = 1
  threshold_num = 20

  roc = tf_roc(predict_label_file, true_label_id, int(threshold_num), save_dir)
  roc.roc_pr()


# coding: utf-8

# In[33]:

#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
import glob
import os
import scipy.misc
import time
import argparse
import sys
import cv2

caffe_root = '/home/osboxes/caffe'

sys.path.insert(0, os.path.join(caffe_root , 'python'))
import caffe

# In[19]:

#this window size needs to be exactly the same size as that used to extract the patches from the matlab version
wsize = 32
hwsize= int(wsize/2)

BASE='/home/osboxes/docmac/TrainingTissueFinder/DL_DIGITALPATHOLOGY/DL_DigitalPathology/DL_tutorial_Code/case4_lymphocyte'
FOLD=2

#Locations of the necessary files are all assumed to be in subdirectoires of the base file
MODEL_DIR   = os.path.join(BASE,'models')
COMMON_DIR  = os.path.join('/home/osboxes/docmac/TrainingTissueFinder/DL_DIGITALPATHOLOGY/DL_DigitalPathology/DL_tutorial_Code/','common')

MODEL_FILE = os.path.join(COMMON_DIR,'deploy_train32.prototxt')
PRETRAINED = '%s/models/%d_caffenet_train_w32_iter_600000.caffemodel' % (BASE,FOLD)
IMAGE_DIR= '%s/images/' % BASE
OUTPUT_DIR= '%s/images/%d/' % (BASE,FOLD)

# In[3]:

OUTPUT_DIR

# In[16]:

#if our output directory doesn't exist, lets create it. each fold gets a numbered directory inside of the image directory
if not os.path.exists(OUTPUT_DIR):
    os.makedirs(OUTPUT_DIR)

# In[17]:

#load our mean file and reshape it accordingly
a = caffe.io.caffe_pb2.BlobProto()
file = open('%s/DB_train_w32_%d.binaryproto' % (MODEL_DIR,FOLD) ,'rb')
data = file.read()
a.ParseFromString(data)
means = a.data
means = np.asarray(means)
means = means.reshape(3, 32, 32)

# In[28]:

#make sure we use teh GPU otherwise things will take a very long time
#caffe.set_mode_gpu()
caffe.set_mode_cpu()
#load the model
net = caffe.Classifier(MODEL_FILE, PRETRAINED,
                       mean=means,
                       channel_swap=(2, 1, 0),
                       raw_scale=255,
                       image_dims=(32, 32))

#see which files we need to produce output for in this fold
#we look at the parent IDs in the test file and only compute those images
#as they've been "held out" from the training set
#files=open('%s/test_w32_parent_%d.txt'%(COMMON_DIR,FOLD),'rb')

start_time = time.time()
start_time_iter=0

#go into the image directory so we can use glob a bit easier
os.chdir(IMAGE_DIR)

# In[124]:

## One image test version

testDataDir = 'HER2/lymphocyte/data'
fname = os.path.join(BASE,testDataDir,'im15.tif')

newfname_class = fname.replace('.tif','_class.tif') #create the new files
newfname_prob = fname.replace('.tif','_prob.tif') #create the new files

outputimage = np.zeros(shape=(10, 10))
#first thing we do is save a file to let potential other workers know that this
#file is being worked on and it should be skipped
scipy.misc.imsave(newfname_class, outputimage)

image = caffe.io.load_image(fname) #load our image
image = caffe.io.resize_image(image, [image.shape[0]*4,image.shape[1]*4]) #if you need to resize or crop, do it here
image = np.lib.pad(image, ((hwsize, hwsize), (hwsize, hwsize), (0, 0)), 'symmetric') #mirror the edges so that we can compute the full image
imageShape = image.shape
#image = cv2.resize(image,(imageShape[0]*4,imageShape[1]*4), interpolation = cv2.INTER_CUBIC)
imageFull = image
#image = image[0:140,0:150,0:3]

#plt.imshow(image)
#plt.show()
#plt.imshow(imageFull)
#plt.show()

sizeWind   = 64

outputimage_probs = np.zeros(shape=(sizeWind,sizeWind,3)) #make the output files where we'll store the data
outputimage_class = np.zeros(shape=(sizeWind,sizeWind))
final_outputimage_probs = np.zeros(shape=(imageFull.shape[0],imageFull.shape[1],3)) #make the output files where we'll store the data
final_outputimage_class = np.zeros(shape=(imageFull.shape[0],imageFull.shape[1]))

indexGridX = image.shape[0]/sizeWind
indexGridY = image.shape[1]/sizeWind

for ix in range(int(indexGridX)):
    for iy in range(int(indexGridY)):
        print (ix,iy)

        xStart  = (ix*sizeWind)
        xEnd    = ((ix+1)*sizeWind)
        yStart  = (iy * sizeWind)
        yEnd    = ((iy + 1) * sizeWind)
        image   = imageFull[xStart:xEnd,yStart:yEnd,:]

        for rowi in xrange(hwsize+1,image.shape[0]-hwsize):
            #print "%s\t (%.3f,%.3f)\t %d of %d" % (fname,time.time()-start_time,time.time()-start_time_iter,rowi,image.shape[0]-hwsize)
            start_time_iter = time.time()
            patches=[] #create a set of patches, oeprate on a per column basis

            for coli in xrange(hwsize+1,image.shape[1]-hwsize):
                patches.append(image[rowi-hwsize:rowi+hwsize, coli-hwsize:coli+hwsize,:])

            prediction = net.predict(patches) #predict the output
            pclass = prediction.argmax(axis=1) #get the argmax
            outputimage_probs[rowi,hwsize+1:image.shape[1]-hwsize,0:2]=prediction #save the results to our output images
            outputimage_class[rowi,hwsize+1:image.shape[1]-hwsize]=pclass

        #outputimage_probs = outputimage_probs[hwsize:-hwsize, hwsize:-hwsize, :] #remove the edge padding
        #outputimage_class = outputimage_class[hwsize:-hwsize, hwsize:-hwsize]

        final_outputimage_probs[xStart:xEnd,yStart:yEnd] = outputimage_probs
        final_outputimage_class[xStart:xEnd,yStart:yEnd] = outputimage_class

scipy.misc.imsave(newfname_prob,final_outputimage_probs) #save the files
scipy.misc.imsave(newfname_class,final_outputimage_class)

# In[127]:

512/64.0

# In[126]:

plt.imshow(final_outputimage_probs[:,:,1])
plt.show()

# In[ ]:


"""This is a simple implementation of [Large-Scale Study of Curiosity-Driven Learning](https://arxiv.org/abs/1808.04355)"""

import numpy as np
import gym
import random
from collections import deque
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Activation, Flatten
from keras.optimizers import Adam
import matplotlib.pyplot as plt
import pickle
import argparse

class Curiosity:

    def __init__(self, numberOfActions, dimStates):
        self.numberOfActions = numberOfActions
        self.dimStates = dimStates
        self.batchSize = 128
        self.learningRate = 0.01
        self.gamma = 0.95
        self.epsilon = 0.95
        self.memorySize = 10000
        self.minMemorySize = 500
        self.targetUpdateCounter = 0
        self.targetUpdateThreshold = 10
        self.inverseNetUpdateThreshold = 1000
        self.inverseNetUpdateCounter = 0

        self.memory = deque(maxlen = self.memorySize)
        self.policyNet = self.createPolicyNet()
        self.targetPolicyNet = self.createPolicyNet()
        self.targetPolicyNet.set_weights(self.policyNet.get_weights())
        self.inverseNet = self.createInverseNet()

    def createPolicyNet(self):
        model = Sequential()
        model.add(Dense(64, input_dim = self.dimStates, kernel_initializer = "uniform", activation = "relu"))
        model.add(Dense(self.numberOfActions))
        model.compile(loss = "mse", optimizer = Adam(lr = self.learningRate), metrics = ['accuracy'])
        return model

    def createInverseNet(self):
        inputSize = self.dimStates + 1
        model = Sequential()
        model.add(Dense(64, input_dim = inputSize, kernel_initializer = "uniform", activation = "relu"))
        model.add(Dense(self.dimStates))
        model.compile(loss = "mse", optimizer = Adam(lr = self.learningRate), metrics = ['accuracy'])
        return model

    def updateMemory(self, transition):
        self.memory.append(transition)

    def getAction(self, state):
        # print(state.shape,  self.dimStates, type(state))
        # print('YYYYY')
        if random.uniform(0, 1) < self.epsilon:
            action = np.argmax(self.policyNet.predict(np.array(state).reshape(-1, len(state))))
        else:
            action = np.random.randint(0, self.numberOfActions)
        return action

    def getInverseNetPredictionError(self, states, actions):
        # print(states.shape, actions.shape)
        inputToInverseNet = np.concatenate((states, actions.reshape(self.batchSize, 1)), axis = 1)
        predictedNextStates = self.inverseNet.predict(inputToInverseNet)
        temp = np.array(states) - np.array(predictedNextStates)
        return np.mean(temp**2, axis = 1)

    def train(self, isTerminalState):
        if(len(self.memory) < self.minMemorySize):
            return

        minibatch = random.sample(self.memory, self.batchSize)

        currentStates = np.array([transition[0] for transition in minibatch])
        actions = np.array([transition[1] for transition in minibatch])
        currentQList = self.targetPolicyNet.predict(currentStates)

        newStates = np.array([transition[3] for transition in minibatch])
        futureQList = self.policyNet.predict(newStates)

        intrinsicReward = self.getInverseNetPredictionError(currentStates, actions)

        X = []
        Y = []

        for index, (currentState, action, reward, newState, done) in enumerate(minibatch):
            # print(intrinsicReward[index])
            if not done:
                maxFutureQ = np.max(futureQList[index])
                newQ = intrinsicReward[index] + reward + self.gamma*maxFutureQ
            else:
                newQ = intrinsicReward[index] + reward

            currentQs = currentQList[index]
            currentQs[action] = newQ

            X.append(currentState)
            Y.append(currentQs)

        self.policyNet.fit(np.array(X), np.array(Y), batch_size = self.batchSize, verbose = 0, shuffle = False)

        if isTerminalState:
            self.targetUpdateCounter += 1
        self.inverseNetUpdateCounter += 1

        if(self.targetUpdateCounter % self.targetUpdateThreshold == 0):
            self.targetPolicyNet.set_weights(self.policyNet.get_weights())
            self.targetUpdateCounter = 0

        if(self.inverseNetUpdateCounter % self.inverseNetUpdateThreshold == 0):
            self.inverseNet.fit(np.concatenate((currentStates, actions.reshape(self.batchSize, 1)), axis = 1), newStates, batch_size = self.batchSize, verbose = 0, shuffle = False)

def getStateDiff(a):
    return (abs(a[0]-0.5))

def introduceNoisyTV(state, tvIsOn):
    if(tvIsOn):
        state = np.append(state, [random.uniform(-2, 2)])
    else:
        state = np.append(state, [0])
    return state

env = gym.make('MountainCar-v0')
env = env.unwrapped

parser = argparse.ArgumentParser()
parser.add_argument("--alpha", default=1, type=int)
parser.add_argument("--noisy", default=0, type=int)
args = parser.parse_args()

curios_model = args.noisy
while curios_model > 2:
    curios_model = int(input("Enter a number between 0-2"))

curiosity = Curiosity(3, 3)
ep_steps = []
tvFlag =  False
alpha = args.alpha

for epi in range(1, 101):
    s = env.reset()
    steps = 1
    while True:
        #env.render()   #This line can be commented or uncommented for the visual running of the curiosity model
        if(len(s) == 2):
            s = introduceNoisyTV(s, False)
        else:
            s[2] = 0
        a = curiosity.getAction(s)
        s_, r, done, info = env.step(a)
        r += alpha*abs(1/getStateDiff(s) - 1/getStateDiff(s_))
        if(epi >= 25 and epi < 75):
            if curios_model == 1:
                s_[1] = random.uniform(-2, 2)
                s_[0] = random.uniform(-2, 2)
            if curios_model == 2: tvFlag = True

        if done:
            r = 10
        else: r = 0

        # Storing in Memory
        if(tvFlag):
            s_ = introduceNoisyTV(s_, True)
        else:
            s_ = introduceNoisyTV(s_, False)
        # print(s, s_)
        curiosity.updateMemory((s, a, r, s_, done))
        curiosity.train(done)
        if done:
            #r=r+22
            print('Epi: ', epi, "| steps: ", steps)
            ep_steps.append(steps)
            break
        s = s_
        steps += 1

with open('foo.pkl', 'wb') as f:
    pickle.dump(ep_steps, f)

print('Average Steps: ' + str(np.mean(ep_steps)))
print('Average Steps During Noisy TV: ' + str(np.mean(ep_steps[25:75])))
plt.plot(ep_steps)
plt.ylabel("steps")
plt.xlabel("episode")
plt.show()


from django.core.management import BaseCommand
from example import models

class Command(BaseCommand):
    def handle(self, *args, **options):
        self.stdout.write("Creating Groups...")
        red = models.Group.objects.create(name="Red Group", description="Group for angry faces")
        blue = models.Group.objects.create(name="Blue Group", description="Group for happy faces")
        yellow = models.Group.objects.create(name="Yellow Group", description="Group for sad faces")
        green = models.Group.objects.create(name="Green Group", description="Group for anxious faces")
        self.stdout.write("Successfully Created Groups.")

        self.stdout.write("Creating Users...")
        michael = models.User.objects.create_user("michael", "michael@naavi.com", first_name="Michael",
                                                  last_name="Bates", owning_group=yellow)
        blake = models.User.objects.create_user("blake", "blake@naavi.com", first_name="Blake",
                                                  last_name="Seufert", owning_group=green)
        louise = models.User.objects.create_user("louise", "louise@naavi.com", first_name="Louise",
                                                  last_name="Krins", owning_group=red)
        stacey = models.User.objects.create_user("stacey", "stacey@naavi.com", first_name="Stacey",
                                                  last_name="Hipta", owning_group=blue)
        self.stdout.write("Successfully Created Users.")

        self.stdout.write("Adding Users to Groups...")
        michael.groups.add(red, blue)
        blake.groups.add(blue, yellow)
        louise.groups.add(yellow, green)
        stacey.groups.add(red, green)
        self.stdout.write("Successfully Added Users to Groups.")

        self.stdout.write("Setting Group Owners...")
        red.created_by = michael
        red.save()
        blue.created_by = blake
        blue.save()
        yellow.created_by = louise
        yellow.save()
        self.stdout.write("Successfully Set Group Owners.")


# USAGE:
# python3 parse_for_arduino.py > output_for_arduino.txt

import os
from os import listdir
from os.path import isfile
files = [f for f in os.listdir('./resources') if (os.path.isfile(f'./resources/{f}')) and ("properties" in f) and not f.startswith("keyboard")]

# Exclude Russian (it is a bit longer than others and for my project I need to keep encoding array lenght under 100 bytes)
# With Russian - Array length = 3x 119
# Without Russian - Array length = 3x 78
del files[files.index("ru.properties")]

def get_keys():
    keys_dict = {}
    with open("resources/keyboard.properties", "rt") as f:
        for line in f.readlines():
            if line.startswith("MOD") or line.startswith("KEY"):
                key = line.split("=")[0].strip()
                value = line.split("=")[1].strip()
                base = 16 if "x" in value else 10
                keys_dict[key] = int(value, base)
    return keys_dict

def add_modifiers(modifiers):
    binary_sum = 0
    for m in modifiers:
        binary_sum |= m
    return binary_sum

languages = []

for f_name in files:
    keys_dict = get_keys()
    key_map = []
    ascii_values = []
    key_codes = []
    modifiers = []
    with open(f'./resources/{f_name}', "rt") as f:
        lines = f.readlines()
        for line in lines:
            if line.startswith("KEY"):
                key = line.split("=")[0].strip()
                value = line.split("=")[1].strip()
                base = 16 if "x" in value else 10
                # if "BACKSLASH" in key:
                #     print("BACKSLASH")
                #     print("BASE =", base)
                #     print("value=", int(value,base))
                # if "HASH" in key:
                #     print("HASH")
                #     print("BASE =", base)
                #     print("value=", int(value,base))
                keys_dict[key] = int(value, base)

        for line in lines:
            if line.startswith("ASCII") or line.startswith("ISO") and "BITS" not in line and "KEY_11" not in line: # line.startswith("UNICODE") or
                key = line.split("=")[0].strip()
                ASCII = int(key.split("_")[-1], 16)
                value = line.split("=")[1].strip()
                keys_to_press = [keys_dict[v.strip()] for v in value.split(",")]
                ascii_values.append(ASCII)
                key_codes.append(keys_to_press[0])
                modifiers.append(0 if len(keys_to_press) == 1 else add_modifiers(keys_to_press[1:]))

    # print(f_name)
    # print("byte encoding[{}] =".format(len(ascii_values)), "{")
    # print("    {", ",".join(str(a) for a in ascii_values), "},")
    # print("    {", ",".join(str(k) for k in key_codes), "},")
    # print("    {", ",".join(str(m) for m in modifiers), "},")
    # print("};")

    # print("Tranmission strings:")
    # print("ENC,D:{},end".format("".join(["{:X}".format(v) for v in ascii_values])))
    # print("ENC,U:{},end".format("".join(["{:X}".format(v) for v in key_codes])))
    # print("ENC,M:{},end".format("".join(["{:X}".format(v) for v in modifiers])))

    languages.append([ascii_values, key_codes, modifiers])

# check what characters are typed exactly the same way on every language (to save space in Arduino code - smaller array)
# That list can be saved to PROGMEM, it can also replace ascii_map from Keyboard library (Keyboard.cpp)
# Or just type these keys using the original Keyboard.press()
def get_ascii_that_is_always_typed_the_same_way():
    ascii_list = []
    base_lang = languages[9] # 9th had the most values (119)
    for ascii_val, key_code, modifier in zip(*base_lang):
        the_same = True
        for i, lang in enumerate(languages):
            if not (ascii_val in lang[0]):
                the_same = False
            else:
                ind = lang[0].index(ascii_val)
                if lang[1][ind] != key_code or lang[2][ind] != modifier:
                    the_same = False
        if the_same:
            ascii_list.append(ascii_val)
            # print(ascii_val, key_code, modifier)
    return ascii_list

ascii_that_does_not_need_encoding = get_ascii_that_is_always_typed_the_same_way()

def filter_encoding():
    for ascii_val in ascii_that_does_not_need_encoding:
        for lang in languages:
            if not ascii_val in lang[0]:
                continue
            else:
                ind = lang[0].index(ascii_val)
                del lang[0][ind]
                del lang[1][ind]
                del lang[2][ind]

filter_encoding()

max_length = 0
for lang in languages:
    if len(lang[0]) > max_length:
        max_length = len(lang[0])

print('''
Structure is:
byte encoding[3][max_length_of_any_lang_data] = {
    { ASCII characters that need to be typed },
    { USB HID scan codes },
    { USB HID scan codes - modifiers },
};
// USB HID scan codes reference: https://gist.github.com/MightyPork/6da26e382a7ad91b5496ee55fdc73db2

''')

for lang, f_name in zip(languages, files):
    ascii_values = lang[0]
    key_codes = lang[1]
    modifiers = lang[2]

    print(f_name)
    print("byte encoding[3][{}] =".format(max_length + 1), "{")
    print("    {", ",".join(str(a) for a in ascii_values), "},")
    print("    {", ",".join(str(k) for k in key_codes), "},")
    print("    {", ",".join(str(m) for m in modifiers), "},")
    print("};")

    print()
    print("Tranmission strings:")
    print("ENC,D:{},end".format("".join(["{:0{}X}".format(v,2) for v in ascii_values])))
    print("ENC,U:{},end".format("".join(["{:0{}X}".format(v,2) for v in key_codes])))
    print("ENC,M:{},end".format("".join(["{:0{}X}".format(v,2) for v in modifiers])))

    print()
    print("{},{},{},{}".format(
            f_name.split(".")[0],
            "".join(["{:0{}X}".format(v,2) for v in ascii_values]),
            "".join(["{:0{}X}".format(v,2) for v in key_codes]),
            "".join(["{:0{}X}".format(v,2) for v in modifiers])
            )
        )

    print("\n\n")


#calss header
class _CARPETBAGGER():
	def __init__(self,):
		self.name = "CARPETBAGGER"
		self.definitions = [u'someone who tries to be elected as a politician in a place away from their home because they think there is a greater chance of succeeding there', u'someone who invests in a financial organization that is owned by its members, in order to make a profit if it is sold: ']

		self.parents = []
		self.childen = []
		self.properties = []
		self.jsondata = {}

		self.specie = 'nouns'

	def run(self, obj1 = [], obj2 = []):
		return self.jsondata


import pandas as pd
import tensorflow as tf
from tensorflow import keras
from geopy.geocoders import Nominatim
import folium
import os
from folium.plugins import MiniMap

package_dir = os.path.dirname(__file__)
print(package_dir)

imp = pd.read_csv(package_dir +'/data/impressionism_museum_list.csv',index_col=0)
exp = pd.read_csv(package_dir +'/data/expressionism_museum_list.csv',index_col=0)
cub = pd.read_csv(package_dir +'/data/cubism_museum_list.csv',index_col=0)
pop = pd.read_csv(package_dir +'/data/pop_museum_list.csv',index_col=0)

def train_art_model(modelname):

    image_size = (224, 224)
    batch_size = 48

    train_ds = tf.keras.preprocessing.image_dataset_from_directory(
        "data/training",
        validation_split=0.2,
        subset="training",
        seed=1337,
        image_size=image_size,
        batch_size=batch_size,
        label_mode='categorical'
    )

    val_ds = tf.keras.preprocessing.image_dataset_from_directory(
        "data/training",
        validation_split=0.2,
        subset="validation",
        seed=1337,
        image_size=image_size,
        batch_size=batch_size,
        label_mode='categorical'
    )

    # preprocess the data
    print("preprocess the data")
    augmented_train_ds = train_ds.map(lambda x, y: (keras.applications.mobilenet_v2.preprocess_input(x), y))
    augmented_val_ds = val_ds.map(lambda x, y: (keras.applications.mobilenet_v2.preprocess_input(x), y))

    # build a model
    print("build a model")
    base_model = keras.applications.mobilenet_v2.MobileNetV2(
                                        weights='imagenet', # use imagnet weights
                                        pooling='avg', # to flatten after covnet layers
                                        include_top=False, # only want the base of the model
                                        input_shape=(224,224,3),
                                        alpha=0.35 # the amount of filters from original network, here we only use 35%
    )
    base_model.trainable = False
    model=keras.Sequential()
    model.add(base_model)
    model.add(keras.layers.Dense(128, activation='relu'))
    model.add(keras.layers.BatchNormalization())
    model.add(keras.layers.Dense(64, activation='relu'))
    model.add(keras.layers.BatchNormalization())
    model.add(keras.layers.Dropout(0.5))
    model.add(keras.layers.Dense(4, activation='softmax'))
    model.compile(optimizer=keras.optimizers.Adam(),
              loss=keras.losses.categorical_crossentropy,
              metrics=[keras.metrics.categorical_accuracy])

    # train the model
    print("train the model")
    epochs = 50
    callback = keras.callbacks.EarlyStopping(monitor='val_loss',patience=3)

    results = model.fit(
        augmented_train_ds, epochs=epochs, verbose=2, callbacks=[callback], validation_data=augmented_val_ds,
    )

    print('the prediction order is:',train_ds.class_names)

    # save the model
    print("save the model as",modelname,"in ./models/")
    model.save('./models/')

    print('done')

def art_map(art_loc_list):
    '''
    combine multiple rows (artists) into a single row (artist_list) with pandas

    '''

    # Create a map using the Map() function and the coordinates for Boulder, CO
    loc = Nominatim(user_agent="my_map").geocode('Mali')
    df_loc = pd.DataFrame(loc.raw)
    df_loc.drop([1,2,3], inplace=True)
    lat = float(df_loc['lat'][0])
    lon = float(df_loc['lon'][0])
    m = folium.Map(location=[lat, lon],zoom_start=2.3)

    # mark the museums
    art_loc = art_loc_list.groupby(['museum_name','lat','lon'])['artist'].apply(','.join).reset_index()
    art_loc['link']= art_loc_list.groupby(['museum_name','lat','lon'])['museum_link'].apply(','.join).reset_index()['museum_link']

    for lat,lon,name,link,artist in zip(art_loc['lat'],art_loc['lon'],art_loc['museum_name'],art_loc['link'],art_loc['artist']):
        links = link.split(",")
        artists = artist.split(",")
        artist_links = []
        for link,artist in zip(links,artists):
            artist_link = f'{artist}'
            artist_links.append(artist_link)
            nl = '\n'
        folium.Marker(
            location =[lat, lon], # coordinates for the marker (Earth Lab at CU Boulder)
            popup=f'{name}{nl}{[artist_link+nl for artist_link in artist_links]}', # pop-up label for the marker
            #icon=folium.Icon()
        ).add_to(m)

    minimap = MiniMap()
    m.add_child(minimap)

    return m

def get_model():
    model = keras.models.load_model(package_dir +'/models/art_styles_model3.h5')
    return model

if __name__ == '__main__':

    # train_art_model
    #train_art_model('testmodel')

    # test art_map function
    #print(cub)
    #map = art_map(pop)
    #print(map)
    #print('done')

    model = get_model()
    print(model)


# -*- coding: utf-8 -*-
from __future__ import division
from django.db import models
from django.db import connection
from datetime import datetime, timedelta
from django.db.models import Sum, Avg
from django.contrib.auth.models import User
import heapq
import numpy
import sys
import re

# -----------------------------------------------------------------------------------------------
#
# 	INDEX.-
#
# -----------------------------------------------------------------------------------------------
class AVG():

	# Constructor.-
	def __init__(self,array=None):
		self.array = []
		if array: self.array = array

	# Return Length.-
	def len(self):
		return len(self.array)

	# To String.-
	def __str__(self):
		matrix = " %-20.20s %-20.20s \n" % ( "Value", "Weight" )
		for a in self.array:
			matrix += " %-20.20s %-20.20s \n" % ( unicode(a['value']), unicode(a['weight']), )
		matrix +=  " TOTAL=%d AVG=%.2f WAVG=%.2f \n" % ( self.len(), self.avg(), self.wavg() )
		return matrix

	# Add Element to array.-
	def add(self,value,weight=1):
		m = { "value":float(value), "weight":float(weight), }
		self.array.append(m)

	# Total Weight
	def tot_weight(self):
		t = 0
		for a in self.array:
			t += a['weight']
		return t

	# Normal Average
	def avg(self):
		numerator   = 0
		denominator = self.len()
		for a in self.array:
			numerator += a['value']
		if denominator: return float( numerator / denominator )
		else:		return 0

	# Ponderated Average
	def wavg(self):
		numerator   = 0
		denominator = 0
		for a in self.array:
			numerator   += a['value'] * a['weight']
			denominator += a['weight']
		if denominator: return float( numerator / denominator )
		else:		return 0


# -*- coding: utf-8 -*-

from __future__ import unicode_literals

from flask import url_for

from tests.base import BaseTestCase

class GuestTestCase(BaseTestCase):

    def test_index_page(self):
        response = self.client.get(url_for('front.index'))
        data = response.get_data(as_text=True)
        self.assertIn('Sign in Now', data)
        self.assertIn('LOGIN', data)

    def test_about_page(self):
        response = self.client.get(url_for('front.about'))
        data = response.get_data(as_text=True)
        self.assertIn('About Us', data)
        self.assertIn('This is about page, make you know more about us.', data)

    def test_author_page(self):
        self.client.get(url_for('front.author'))

    def test_about_page(self):
        self.client.get(url_for('front.organization'))

    def test_chinese_language(self):
        self.runner.invoke(args=['translate', 'compile'])

        self.client.get(url_for('front.set_locale', locale='zh_Hans_CN'))
        response = self.client.get(url_for('front.index'))
        data = response.get_data(as_text=True)
        self.assertIn('首页', data)
        self.assertIn('关于', data)
        self.assertIn('登录', data)

    def test_english_language(self):
        self.runner.invoke(args=['translate', 'compile'])

        self.client.get(url_for('front.set_locale', locale='en_US'))
        response = self.client.get(url_for('front.index'))
        data = response.get_data(as_text=True)
        self.assertIn('HOME', data)
        self.assertIn('ABOUT', data)
        self.assertIn('LOGIN', data)


from path_planner import Planner
from state_v2 import State
from controller_v2 import Controller
from grid_positioning import GridPositioning
from explorer import Explorer
import numpy as np
import rospy
import cv2
import random
import time
start_time = time.time()

def sample(d):
    x = []
    for i in range(len(d)):
        if d[i]: x.append(i)
    randi = random.randint(0,len(x)-1)
    return x[randi]
soln = [ [0,0], [1,0], [1,1], [2,1], [3,1], [3,2], [3,3], [4,3], [5,3], [5,2], [6,2], [7,2],
        [8,2], [8,3], [8,4], [7,4], [6,4], [6,5], [5,5], [5,6], [6,6], [7,6], [7,7] ]
soln_prats = [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [6, 0], [5, 0], [4, 0], [3, 0], [2, 0], [1, 0], [1, 1], [2, 1], [3, 1], [3, 2], [4, 2], [3, 2], [3, 3], [4, 3], [5, 3], [6, 3], [7, 3], [6, 3], [5, 3], [5, 4], [5, 3], [4, 3], [4, 4], [3, 4], [3, 5], [4, 5], [3, 5], [3, 4], [4, 4], [4, 3], [5, 3], [5, 2], [6, 2], [7, 2], [8, 2], [8, 3], [8, 4], [8, 5], [8, 4], [7, 4], [6, 4], [6, 5], [5, 5], [5, 6], [6, 6], [7, 6], [7, 7]]
soln = soln_prats
pre = 0

rospy.init_node("testing_version_2")
cv2.namedWindow("Testing_v2", 100)

# pp = Planner()
gp = GridPositioning()
# state = pp.state
dora = Explorer([1,0])
state = gp.state
tar , sam = soln[0], 0
# tar , sam = [1,0], 0
tar_ar = []
f = False
while not rospy.is_shutdown():
    # gp.state.display()
    # print(" -- -- -- -- ")
    k = cv2.waitKey(30)
    if k == 27: break
    k = k - 48
    if gp.current != None: print(" c: ", gp.find_gridC(gp.current))
    if f : k = 0
    if k == 0:
        f = True
        """
        if tar == None: tar = [pp.state.position.x, pp.state.position.y]
        done = pp.run(tar)
        print("done: ", done)
        if done:
            tar = [tar[0]+0.17, tar[1]+0.02 ]
            if tf == 2: break
            tf += 1
            print("############ TARGET RESET ############")
        """
        # gp.state.display()
        print(" -- -- -- -- ")
        res, nxt, cur = gp.run(tar)
        print("TAR: ", tar, " CUR: ", cur)
        print("NXT: ", nxt)
        if res:
            print("NEXT:(NEWS) ", nxt, "CUR: ", cur)
            # mv = sample(nxt)
            if soln[pre] == cur and tar == cur:
                pre += 1
                if pre == len(soln): break
                tar = soln[pre]
            # if tar == cur:
            #     tar = dora.run(cur, nxt)
            #     tar_ar.append([cur,tar,nxt])

            """
            if cur[0] != tar[0] and cur[1] != tar[1]:
                continue
            mv = int(input("NEXT MOVE: "))
            if mv == 6 : tar = [cur[0], cur[1]+1]
            elif mv == 8 : tar = [cur[0]-1, cur[1]]
            elif mv == 4 : tar = [cur[0], cur[1]-1]
            else: tar = [cur[0]+1, cur[1]]
            """
            sam += 1
            # tar_ar.append([cur,tar,nxt])
    else:
        # pp.car.key_run(k)
        gp.car.key_run(k)
    print(" -- -- -- -- ", k, " sam: ", sam )

print("t: ", tar_ar)
print("## end ##")
print("TIME: ", (time.time() - start_time)/60.0 )
# dora.show_map()


# coding: utf8

from __future__ import unicode_literals
from nltk.chunk import RegexpParser, tree2conlltags

class Chunker(RegexpParser):
	"""
		>>> tree2brackets(chunker.parse([('نامه', 'Ne'), ('۱۰', 'NUM'), ('فوریه', 'Ne'), ('شما', 'PRO'), ('را', 'POSTP'), ('دریافت', 'N'), ('داشتم', 'V'), ('.', 'PUNC')]))
		'[نامه ۱۰ فوریه شما را NP] [دریافت داشتم VP] .'

		# VP

		>>> tree2brackets(chunker.parse([('تقدیم', 'Ne'), ('حضورتان', 'N'), ('نمایم', 'V')]))
		'[تقدیم حضورتان NP] [نمایم VP]'

		>>> tree2brackets(chunker.parse([('به', 'P'), ('سه', 'NUM'), ('رأی', 'N'), ('افزایش', 'N'), ('می‌یابد', 'V')]))
		'[به PP] [سه رأی NP] [افزایش می‌یابد VP]'

		>>> tree2brackets(chunker.parse([('عملیات', 'Ne'), ('نظامی', 'AJe'), ('روسیه', 'N'), ('را', 'POSTP'), ('در', 'P'), ('چچن', 'N'), ('محکوم', 'AJ'), ('کردند', 'V')]))
		'[عملیات نظامی روسیه را NP] [در PP] [چچن NP] [محکوم کردند VP]'

		>>> tree2brackets(chunker.parse([('در', 'P'), ('مورد', 'Ne'), ('پیوستن', 'Ne'), ('آنها', 'PRO'), ('به', 'P'), ('اتحادیه', 'N'), ('است', 'V')]))
		'[در PP] [مورد پیوستن آنها NP] [به PP] [اتحادیه NP] [است VP]'

		# ADJP

		>>> tree2brackets(chunker.parse([('از', 'P'), ('خط', 'Ne'), ('جبهه', 'Ne'), ('اولیه', 'AJ'), ('ناگزیر', 'AJ'), ('به', 'P'), ('فرود', 'Ne'), ('اجباری', 'AJ'), ('شده‌اند', 'V')]))
		'[از PP] [خط جبهه اولیه NP] [ناگزیر به ADJP] [فرود اجباری NP] [شده‌اند VP]'

		>>> tree2brackets(chunker.parse([('پیروزی', 'Ne'), ('مشترکمان', 'AJ'), ('بر', 'P'), ('ستمگران', 'Ne')]))
		'[پیروزی مشترکمان NP] [بر PP] [ستمگران NP]'

	"""

	def __init__(self):
		grammar = r"""

			VP:
				<N|AJ|POSTP>{<N|AJ><V>}
				{<V>}

			ADVP:
				{<ADV><AJ>?}

			ADJP:
				<.*[^e]>{<AJe?><Pe?>}

			NP:
				{<DETe?|Ne?|NUMe?|AJe|PRO|CL|RESe?><DETe?|Ne?|NUMe?|AJe?|PRO|POSTP|CL|RESe?>*}
				<N>}{<Ne?>

			ADJP:
				{<AJe?>}

			PP:
				{<Pe?>+}

		"""

		super(Chunker, self).__init__(grammar=grammar)

def tree2brackets(tree):
	str, tag = '', ''
	for item in tree2conlltags(tree):
		if item[2][0] in {'B', 'O'} and tag:
			str += tag +'] '
			tag = ''

		if item[2][0] == 'B':
			tag = item[2].split('-')[1]
			str += '['
		str += item[0] +' '

	if tag:
		str += tag +'] '

	return str.strip()


#%%
import cv2
import numpy as np
import os
import matplotlib.pyplot as plt

img = 53010
mov = 3

movie = os.listdir('./left')
path = f'./left/{movie[mov]}/'

image = os.listdir(path)[img]
previous = os.listdir(path)[img-1]

img = cv2.imread(path+image,  cv2.IMREAD_GRAYSCALE)
color_img = cv2.imread(path+image)
prev = cv2.imread(path+previous,  cv2.IMREAD_GRAYSCALE)

output = cv2.calcOpticalFlowFarneback(img, prev, None, \
    0.5, 2, 15, 1, 5, 1.1, 0)

plt.imshow(output[:,:,0])
plt.show()
plt.imshow(output[:,:,1])
plt.show()
plt.imshow(color_img, 'gray')
plt.show()

# %%


"""
Celery tasks to monitor and emmit updates
"""
import os
from flask_socketio import SocketIO
from app import create_celery_app
from app.api.sites import test_sites

celery = create_celery_app()

@celery.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    """
    Creates periodic tasks after celery is initialized
    :param sender: Celery app
    """
    sender.add_periodic_task(30.0, test_domains, name='Every 30s check domains')

@celery.task
def test_domains():
    """
    Tests domains and emits messages to all clients trough SocketIO
    """
    socket_io = SocketIO(message_queue=os.environ.get('CELERY_BROKER'))
    socket_io.emit('sites_event', {
        'results': [v for k, v in test_sites()]
    }, broadcast=True)


import socket
import pyDes
from hashlib import sha256
from time import time
from getpass import getpass
from random import SystemRandom  # Crypto secure random
from base64 import b64encode
from base64 import b64decode

AS_PORT = 2222
TGS_PORT = 3333

def main():
	socket.setdefaulttimeout(3)
	while True:
		try:
			# ----------------------------------------------------------------------
			# Gathering user input
			# ----------------------------------------------------------------------

			user = input("\n\nType your username: ").strip('\n').encode('ascii')
			# Get key, encode ascii, hash it with sha256, get the first 8 chars of the hex of the hash and encode that
			key = sha256(getpass("Type your key: ").strip('\n').encode('ascii')).hexdigest()[:8].encode('ascii')
			Kc = pyDes.des(key, pyDes.CBC, b"\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
			resource = input("Type which resource you want to access: ").strip('\n').encode('ascii')

			# ----------------------------------------------------------------------
			# Prepare message to send AS
			# ----------------------------------------------------------------------

			# Connect to AS on port 2222
			try:
				sck = socket.create_connection(address=('localhost', AS_PORT))
			except socket.timeout:
				print("AS appears to be down. Try again later.")
				continue

			rn1 = str(SystemRandom().randrange(10000)).encode('ascii')
			T_R = str(int(time())).encode('ascii')
			msgToSendAS = user + b',' + b64encode(Kc.encrypt(b','.join([resource, T_R, rn1])))

			if __debug__:
				print("Sending to AS:\n{}".format(msgToSendAS))

			# ----------------------------------------------------------------------
			# Talking to AS
			# ----------------------------------------------------------------------

			sck.send(msgToSendAS)
			try:
				answer = sck.recv(1024)
			except socket.timeout:
				print("AS server didn't answer.\nDid you input your username and key correctly?")
				sck.close()
				continue
			sck.close()

			if __debug__:
				print("AS answered:\n{}\n".format(answer))

			if not answer:
				print("AS server didn't answer.\nDid you input your username and key correctly?")
				sck.close()
				continue

			infoAS, T_c_tgs = answer.split(b',')
			info = Kc.decrypt(b64decode(infoAS))

			# if the format is incorrect, abort
			if info.count(b',') != 1:
				print("Something weird happened while decrypting, aborting")
				continue
			K_c_tgs_key, n1 = info.split(b',')
			if n1 != rn1:
				print("Something weird happened while decrypting, aborting")
				continue

			input("Everything ok with AS...\nPress any key to connect to TGS")

			# ----------------------------------------------------------------------
			# Prepare message to send TGS
			# ----------------------------------------------------------------------

			# Connect to TGS on port 3333
			try:
				sck = socket.create_connection(address=('localhost', TGS_PORT))
			except socket.timeout:
				print("TGS appears to be down. Try again later.")
				continue

			rn2 = str(SystemRandom().randrange(10000)).encode('ascii')
			K_c_tgs = pyDes.des(K_c_tgs_key, pyDes.CBC, b"\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
			msgToSendTGS = b64encode(K_c_tgs.encrypt(b','.join([user, resource, T_R, rn2]))) + b',' + T_c_tgs

			if __debug__:
				print("Sending to TGS:\n{}".format(msgToSendTGS))

			# ----------------------------------------------------------------------
			# Talking to TGS
			# ----------------------------------------------------------------------

			sck.send(msgToSendTGS)
			try:
				answer = sck.recv(1024)
			except socket.timeout:
				print("TGS server didn't answer.\nDid you input a valid resource?")
				sck.close()
				continue

			if __debug__:
				print("TGS answered:\n{}\n".format(answer))

			if not answer:
				print("TGS server didn't answer.\nDid you input a valid resource?")
				sck.close()
				continue

			infoTGS, T_c_s = answer.split(b',')
			info = K_c_tgs.decrypt(b64decode(infoTGS))

			# if the format is incorrect, abort
			if info.count(b',') != 2:
				print("Something weird happened while decrypting, aborting")
				continue
			K_c_s_key, T_A, n2 = info.split(b',')
			if rn2 != n2:
				print("Something weird happened while decrypting, aborting")
				continue

			input("Everything ok with TGS...\nPress any key to connect to resource")

			# ----------------------------------------------------------------------
			# Prepare message to send resource
			# ----------------------------------------------------------------------

			# Connect to resource on it's port (same as resource id)
			try:
				sck = socket.create_connection(address=('localhost', int(resource)))
			except socket.timeout:
				print("TGS appears to be down. Try again later.")
				continue

			rn3 = str(SystemRandom().randrange(10000)).encode('ascii')
			K_c_s = pyDes.des(K_c_s_key, pyDes.CBC, b"\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
			msgToSendService = b64encode(K_c_s.encrypt(b','.join([user, T_A, resource, rn3]))) + b',' + T_c_s

			if __debug__:
				print("Sending to Service:\n{}".format(msgToSendService))

			# ----------------------------------------------------------------------
			# Talking to resource
			# ----------------------------------------------------------------------

			sck.send(msgToSendService)
			try:
				answer = sck.recv(1024)
			except socket.timeout:
				print("Service didn't answer.\nAre you trying something fishy?")
				sck.close()
				continue

			if __debug__:
				print("Service answered:\n{}\n".format(answer))

			infoService = K_c_s.decrypt(b64decode(answer))
			if infoService.count(b',') != 1:
				print("Something weird happened while decrypting, aborting")
				continue
			answerService, n3 = infoService.split(b',')

			if n3 != rn3:
				print("Something weird happened while decrypting, aborting")
				continue

			# ----------------------------------------------------------------------
			# Finishing
			# ----------------------------------------------------------------------

			if answerService == b"OK":
				print("\n* ============ *\n*** SUCCESS! ***\n* ============ *\n")
				continue
			else:
				print("Authentication failure! Service did not recognize you!")

		except KeyboardInterrupt:
			print("\n")
			break

if __name__ == "__main__":
	main()


from tkinter import *
# Константа
CANVAS_SIZE = 700

# список вопросов и ответов к ним
q_a = {
    '2+2': 4,
    '5+7': 12,
    '9+2': 11,
    '8+11': 19,
    '6+7': 13,
}
root = Tk()
canvas = Canvas(root, height=CANVAS_SIZE, width=CANVAS_SIZE, bg='gray94', borderwidth=3)
canvas.pack()

class Start_page():
    '''
    Это титульная страница, используемая для предоставления пользователю выбор между учеником и учителем
    '''
    def __init__(self):
        canvas.create_text(360, 150, text='Ты учитель или ученик?', fill='black', font='Arial 25')
        self.button_teacher = Button(canvas, padx=17.5, pady=30, width=15, height=5, text='Учитель', bg='maroon2', bd=0, font='Arial 9', activebackground='maroon1', command=self.teacher_button)
        self.button_student = Button(canvas, width=15, height=5, padx=20, pady=30, text='Ученик', bg='spring green', bd=0, font='Arial 9', activebackground='lawn green', command=self.student_button)
        self.button_teacher.place(x=385, y=300)
        self.button_student.place(x=185, y=300)

    def teacher_button(self):
        '''
        Этя функция "удаляет" титульную страницу, и запускает страницу учителя
        '''
        self.button_teacher.destroy()
        self.button_student.destroy()
        canvas.delete(0, ALL)
        Teacher_page()

    def student_button(self):
        '''
        Эта функция "удаляет" титульную страницу, и запускает страницу ученика
        '''
        self.button_teacher.destroy()
        self.button_student.destroy()
        canvas.delete(0, ALL)
        Student_page()

class Student_page():
    '''
    Это страница ученика, где пользователь должен отвечать на примеры по умолчанию, а также примеры, добавленные в режиме учителя
    '''
    def __init__(self):
        canvas.create_text(360, 100, text='Реши пример:', fill='black', font='Arial 25')
        self.entry_field = Entry(canvas, width=40, borderwidth=3)
        self.send_answer = Button(canvas, width=7, height=4, text='Отправить', command=self.send_answer, bg='lightblue', font='Arial 6')
        self.exit =  Button(canvas, bg='tomato', text='В главное меню', font='Arial 6', command=self.go_to_menu)

        self.entry_field.place(x=235, y=200)
        self.send_answer.place(x=333, y=240)
        self.exit.place(x=318, y=680)
        self.mark_q = len(q_a) - 1
        self.true_answers = 0
        self.questions = list(q_a.keys())
        self.answers = list(q_a.values())
        self.start_math()
    def start_math(self):
        '''
        Эта функция выводит на экран решаемый пример
        '''
        if self.mark_q == -1:
            self.end()
        try:
            self.text_math.destroy()
        except:
            pass
        printed_text = f'{self.questions[self.mark_q]}'
        self.text_math = Label(canvas, text=f'{printed_text}', fg='black', font='Arial 15')
        self.text_math.place(x=335, y=150)
        self.mark_q -= 1
        if self.mark_q == -2:
            self.text_math.destroy()
    def send_answer(self):
        '''
        Эта функция запоминает ввод пользователя, и запускает проверку правильности ответа, а также смену примера
        '''
        self.user_answer = self.entry_field.get()
        self.entry_field.delete(0, END)
        self.start_math()
        self.check_true()
    def check_true(self):
        '''
        Эта функция проверяет ответ, и если он правильный, зачисляет +1 к правильным ответам
        '''
        try:
            if int(self.user_answer) == int(self.answers[self.mark_q + 2]):
                self.true_answers += 1

        except:
            pass
    def go_to_menu(self):
        '''
        Эта функция "удаляет" страницу ученика - выход из режима ученика на титульную страницу
        '''
        self.text_math.destroy()
        self.entry_field.destroy()
        self.send_answer.destroy()
        self.exit.destroy()
        canvas.delete(0, ALL)
        Start_page()

    def end(self):
        self.entry_field.destroy()
        self.send_answer.destroy()
        self.text_math.destroy()
        self.true_answers += 1
        canvas.delete(0, ALL)
        canvas.create_text(355, 340, text=f'Правильных ответов: {self.true_answers}', font='Arial 25')

class Teacher_page():
    '''
    Это страница учителя, где пользователь может добавлять примеры к списку по умолчанию, для решения их в режиме ученика
    '''
    def __init__(self):
        canvas.create_text(360,150, text='Хотите добавить пример?', fill='black', font='Arial 25')
        self.btn_yes = Button(canvas, width=15, height=5, padx=20, pady=30, text='Да', bg='spring green', bd=0, font='Arial 9', activebackground='lawn green', command=self.btn_yes)
        self.btn_not = Button(canvas, width=15, height=5, padx=20, pady=30, text='Нет', bg='red', bd=0, font='Arial 9', activebackground='maroon3', command=self.btn_not)
        self.btn_yes.place(x=185, y=300)
        self.btn_not.place(x=385, y=300)

    def btn_yes(self):
        '''
        Эта функция выполняет роль отказа от перехода в режим учителя - возвращает пользователя на титульную страницу
        '''
        self.btn_yes.destroy()
        self.btn_not.destroy()
        canvas.delete(0, ALL)
        self.question_page()

    def btn_not(self):
        '''
        Эта функция выполняет роль соглашения перехода в режим учителя - переходит на страницу добавления примеров
        '''
        self.btn_yes.destroy()
        self.btn_not.destroy()
        canvas.delete(0, ALL)
        Start_page()

    def question_page(self):
        '''
        Это страница для добавления примеров, выполняет роль отрисовки интерфейса
        '''
        canvas.create_text(360, 100, text='Напишите свой пример', fill='black', font='Arial 25')
        canvas.create_text(485, 125, text='без ответа', fill='gray', font='Arial 12')
        self.entry_field = Entry(canvas, width=40, borderwidth=3)
        self.entry_field.place(x=235, y=200)
        self.send_question = Button(canvas, width=7, height=4, text='Отправить', bg='lightblue', font='Arial 6', command=self.send_question)
        self.exit =  Button(canvas, bg='tomato', text='В главное меню', font='Arial 6', command=self.go_to_menu)
        self.exit.place(x=316, y=680)
        self.send_question.place(x=330, y=240)

    def go_to_menu(self):
        '''
        Эта функция "удаляет" страницу учителя, выход из режима на титульную страницу
        '''
        self.entry_field.destroy()
        self.send_question.destroy()
        self.exit.destroy()
        try:
            self.text_error.destroy()
        except:
            pass
        canvas.delete(0, ALL)
        Start_page()

    def send_question(self):
        '''
        Эта функция проверяет наличие примера в списке по умолчанию
        '''
        self.exam = self.entry_field.get()
        self.entry_field.delete(0, END)
        try:
            for q in q_a:
                if self.exam == q:
                    self.text_error = Label(canvas, text='Данный пример уже имеется', fg='red', font='Arial 10')
                    self.text_error.place(x=275, y=140)
                if self.exam not in q_a:
                    self.change_dict()
        except:
            pass

    def change_dict(self):
        '''
        Эта функция изменяет список примеров - добавляет пример, написанный в режима учителя
        '''
        q_a[self.exam] = eval(f'{self.exam}')
# Тестинг
start = Start_page()

root.mainloop()


#coding=utf8

import thread,socket

HOST = '127.0.0.1'
PORT = 21567
BUFSIZE =1024
ADDR = (HOST,PORT)

def on_new_client(clientsocket,addr):
    while True:
        data = clientsocket.recv(BUFSIZE)
        if not data:
            print (u'%s 关闭了连接' % addr)
            break

        print ('%s %s' % (addr,data))
        clientsocket.send(data)

Serversocket = socket.socket()
Serversocket.bind((HOST,PORT))
Serversocket.listen(5)
while True:
    clientsocket,addr = Serversocket.accept()
    addr =str(addr)
    print (u'从%s来的连接' % addr)
    thread.start_new_thread(on_new_client,(clientsocket,addr))
    Serversocket.close()


from typing import Any, List, Tuple, Union

from eth_abi.abi import encode_abi

from .exceptions import InputDataError

__all__ = (
    "get_constructor_type",
    "get_types_names",
    "hex_to_bytes",
    "detect_constructor_arguments",
)

def get_constructor_type(abi: List[dict]) -> dict:
    for type_def in abi:
        if type_def["type"] == "constructor":
            return type_def
    raise InputDataError("Constructor is not found in ABI")

def get_types_names(type_def: dict) -> Tuple[List[str], List[str]]:
    types = [t["type"] for t in type_def]
    names = [t["name"] for t in type_def]
    return types, names

def hex_to_bytes(data: Union[str, bytes]) -> bytes:
    if isinstance(data, str):
        if data.startswith("0x"):
            data = data[2:]
        data = bytes.fromhex(data)
    return data

def detect_constructor_arguments(
    abi: List[dict],
    tx_input_with_bytecode: Union[str, bytes],
) -> bytes:
    type_def = get_constructor_type(abi)["inputs"]
    types, _ = get_types_names(type_def)
    default_values: List[Any] = []
    for t in types:
        if "int" in t:
            default_values.append(0)
        elif "[]" in t:
            raise InputDataError(
                "Unable to detect arguments including array. "
                "Please provide the bytecode."
            )
        elif "string" == t:
            default_values.append("")
        elif "bool" == t:
            default_values.append(False)
        elif "address" == t:
            default_values.append("0x0000000000000000000000000000000000000000")
        else:
            raise NotImplementedError(f"Type {t} is not implemented yet")

    default_args = encode_abi(types, default_values)
    return hex_to_bytes(tx_input_with_bytecode)[-len(default_args) :]


#!/usr/bin/env python
"""
Keras model mixins for dealing with npz directories and Spark.
"""
import time
import time
import multiprocessing
import threading
try:
    import queue
except ImportError:
    import Queue as queue
from functools import partial
from itertools import chain
from pprint import pprint
from copy import deepcopy

import numpy as np
from keras.models import Model
from keras.engine.training import GeneratorEnqueuer
from keras import backend as K
from keras import models
from pyspark.sql import types as T

from . npz import lazynpz_generator, parse_meta, iterator_to_npz
from . utils import zip_with_index

class GeneratorModelMixin(object):
    def predict_generator2(self, generator, batch_size=10,
                           val_samples=None, max_q_size=10, nb_worker=1,
                           pickle_safe=False, **kwargs):
        """
        This is based on `predict_generator` in Keras, but a priority
        queue is added so that the predictions come out in the same
        order they were put in.

        It is recommended to use `predict_generator_with_ref` instead.
        """
        # TODO: This deadlocks sometimes if the queue is too small.
        indexed_generator = zip_with_index(generator)

        self._make_predict_function()

        processed_samples = 0
        yielded_samples = 0
        wait_time = 0.01
        batch_queue = queue.PriorityQueue(maxsize=max_q_size)
        last_batch = -1

        enqueuer = None

        try:
            enqueuer = GeneratorEnqueuer(indexed_generator, pickle_safe=pickle_safe)
            enqueuer.start(nb_worker=nb_worker, max_q_size=max_q_size * 10)

            while processed_samples < val_samples:
                generator_output = None
                while enqueuer.is_running():
                    if not enqueuer.queue.empty():
                        index, generator_output = enqueuer.queue.get()
                        break
                    else:
                        time.sleep(wait_time)

                if isinstance(generator_output, tuple):
                    if len(generator_output) == 2:
                        x, y = generator_output
                        sample_weight = None
                    elif len(generator_output) == 3:
                        x, y, sample_weight = generator_output
                    else:
                        raise ValueError('output of generator should be a tuple '
                                         '(x, y, sample_weight) '
                                         'or (x, y). Found: ' +
                                         str(generator_output))
                else:
                    x = generator_output

                outs = self.predict_on_batch(x)

                def count_samples(x):
                    if isinstance(x, list):
                        nb_samples = len(x[0])
                    elif isinstance(x, dict):
                        nb_samples = len(list(x.values())[0])
                    else:
                        nb_samples = len(x)

                    return nb_samples

                nb_samples = count_samples(x)

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

                if len(outs) == 1:
                    preds = outs[0]
                else:
                    preds = outs

                # We will clear the queue if we have yielded the previous
                # batches already.
                while True:
                    if not batch_queue.empty():
                        # Get the batch with the lowest batch number
                        # from the queue.
                        prev_index, prev_preds = batch_queue.get(False)

                        if prev_index == last_batch + 1:
                            # This batch comes after the last batch.
                            yield prev_preds
                            yielded_samples += count_samples(prev_preds)
                            last_batch += 1
                        else:
                            # We are still missing a batch, so we put
                            # the predictions back to the queue.
                            batch_queue.put((prev_index, prev_preds))
                            break
                    else:
                        # Queue is empty.
                        break

                if index == last_batch + 1:
                    # This batch comes after the last, we don't need
                    # to queue it.
                    yield preds
                    yielded_samples += count_samples(preds)
                    last_batch += 1
                else:
                    # We are still missing a batch, so we put this
                    # one on the queue and wait.
                    batch_queue.put((index, preds))

                processed_samples += nb_samples

            # There may still be samples that we have processed but not
            # yielded.
            while yielded_samples < val_samples:
                if not batch_queue.empty():
                    index, preds = batch_queue.get(False)

                    if index == last_batch + 1:
                        # This batch comes after the last, we don't need
                        # to queue it.
                        yield preds
                        yielded_samples += count_samples(preds)
                        last_batch += 1
                    else:
                        # We are still missing a batch, so we put this
                        # one on the queue and wait.
                        batch_queue.put((index, preds))

                wait_time_mine = wait_time
                time.sleep(wait_time_mine)

        finally:
            if enqueuer is not None:
                enqueuer.stop()

    def predict_generator_all(self, generator, val_samples,
                              batch_size=10, **kwargs):
        """
        Similary to `predict_generator`, but returns all predictions
        in an array.
        """
        # TODO: Remove this?
        nb_samples = val_samples

        all_outs = []
        processed_samples = 0

        preds = predict_generator2(generator=generator,
                                   val_samples=val_samples,
                                   batch_size=batch_size,
                                   **kwargs)

        for outs in preds:
            if not isinstance(outs, list):
                outs = [outs]

            nb_samples_in_batch = len(outs[0])

            if len(all_outs) == 0:
                for out in outs:
                    shape = (nb_samples, ) + out.shape[1:]
                    all_outs.append(np.zeros(shape, dtype=K.floatx()))

            for i, out in enumerate(outs):
                all_outs[i][processed_samples:(processed_samples
                                              + nb_samples_in_batch)] = out

            processed_samples += nb_samples_in_batch

        if len(all_outs) == 1:
            return all_outs[0]
        return all_outs

class NPZModelMixin(GeneratorModelMixin):
    def fit_lazynpz(self, npz_dir, batch_size=10, nb_epoch=10,
                   validation_data=None, nb_val_samples=None,
                   equal_sized_batches=True, shuffle="all", **kwargs):
        """
        Fits the model to batches of lazily generated examples.

        :param npz_dir: directory with npz files and metadata
        """
        input_names = [l.name for l in self.input_layers]
        output_names = [l.name for l in self.output_layers]

        meta = parse_meta(npz_dir)
        count = meta["count"]

        # Set up the validation data generator
        if validation_data is not None:
            meta_val = parse_meta(validation_data)
            count_val = meta_val["count"]

            if nb_val_samples is not None:
                if nb_val_samples > count_val:
                    raise ValueError("nb_val_samples={} exceeds the number of "
                                     "examples in the validation data ({})".format
                                     (nb_val_samples, count_val))
            else:
                nb_val_samples = count_val

            validation_data = lazynpz_generator(npz_dir,
                                                run_infinitely=True,
                                                input_names=input_names,
                                                output_names=output_names,
                                                batch_size=batch_size,
                                                equal_sized_batches=equal_sized_batches,
                                                shuffle=False)

        generator = lazynpz_generator(npz_dir,
                                      run_infinitely=True,
                                      input_names=input_names,
                                      output_names=output_names,
                                      batch_size=batch_size,
                                      shuffle=shuffle)

        return self.fit_generator(generator=generator,
                                  samples_per_epoch=count,
                                  nb_epoch=nb_epoch,
                                  validation_data=validation_data,
                                  nb_val_samples=nb_val_samples,
                                  **kwargs)

    def _create_lazynpz_generator(self, npz_dir, val_samples=None,
                                  batch_size=None):
        input_names = [l.name for l in self.input_layers]
        output_names = [l.name for l in self.output_layers]
        meta = parse_meta(npz_dir)
        count = meta["count"]

        if val_samples is None:
            val_samples = count
        else:
            if val_samples > count:
                raise ValueError("val_samples={} exceeds the number of "
                                 "examples in the validation data ({})".format
                                 (nb_val_samples, count))

        generator = lazynpz_generator(npz_dir,
                                      run_infinitely=True,
                                      shuffle=False,
                                      input_names=input_names,
                                      output_names=output_names,
                                      batch_size=batch_size)

        return generator, count

    def predict_lazynpz(self, npz_dir, batch_size=10,
                        val_samples=None, **kwargs):
        """
        Similar to `predict_lazynpz_generator`, but returns an array
        of all predictions.
        """
        generator, count = self._create_lazynpz_generator(npz_dir=npz_dir,
                                                          val_samples=val_samples,
                                                          batch_size=batch_size)

        return self.predict_generator_all(generator=generator,
                                          batch_size=batch_size,
                                          val_samples=count,
                                          **kwargs)

    def predict_lazynpz_generator(self, npz_dir, batch_size=10,
                                  val_samples=None, **kwargs):
        """
        Returns a generator of predictions from a path of
        npz files.
        """
        generator, count = self._create_lazynpz_generator(npz_dir=npz_dir,
                                                          val_samples=val_samples,
                                                          batch_size=batch_size)

        predictions = self.predict_generator2(generator=generator,
                                              batch_size=batch_size,
                                              val_samples=count,
                                              **kwargs)
        for prediction in predictions:
            yield prediction

def _predict_spark(iterator,
                   bc_model_config,
                   bc_model_weights,
                   bc_batch_size,
                   bc_dtypes,
                   bc_pass_through):
    """
    Helper function that is executed on worker nodes.
    """
    K.set_learning_phase(0)

    # We need deepcopy to work around bugs in Keras
    model_config = deepcopy(bc_model_config.value)
    model_weights = bc_model_weights.value
    dtypes = bc_dtypes.value
    batch_size = bc_batch_size.value
    pass_through = bc_pass_through.value

    npzs = iterator_to_npz(iterator, dtypes)

    model = Model.from_config(model_config)
    model.set_weights(model_weights)

    preds = model.predict(npzs, batch_size=100)

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

    n_outputs = len(preds)

    for i in range(preds[0].shape[0]):
        outs = (preds[o][i].tolist() for o in range(n_outputs))
        pt = (npzs[name][i].tolist() for name in pass_through)

        yield tuple(chain(pt, outs))

class SparkModelMixin(object):
    def predict_spark(self, data, dtypes, batch_size=10, pass_through=None):
        """
        Predicts the given spark data frame.
        """
        sc = data._sc

        K.set_learning_phase(0)
        model_config = self.get_config()
        model_weights = self.get_weights()

        bc_model_config = sc.broadcast(model_config)
        bc_model_weights = sc.broadcast(model_weights)
        bc_batch_size = sc.broadcast(batch_size)
        bc_dtypes = sc.broadcast(dtypes)
        if pass_through is None:
            pass_through = []
        pass_through_names = tuple(p[0] for p in pass_through)
        bc_pass_through = sc.broadcast(pass_through_names)

        output_names = tuple(l.name for l in self.output_layers)

        schema = T.StructType(list(chain(
            (T.StructField(name, t) for name, t in pass_through),
            (T.StructField(name, T.ArrayType(T.FloatType())) for name in output_names)
        )))

        preds = (data
                .rdd
                .mapPartitions(partial(_predict_spark,
                                       bc_model_config=bc_model_config,
                                       bc_model_weights=bc_model_weights,
                                       bc_batch_size=bc_batch_size,
                                       bc_dtypes=bc_dtypes,
                                       bc_pass_through=bc_pass_through))
                .toDF(pass_through_names + output_names))

        return preds

class SparkModel(Model, NPZModelMixin, SparkModelMixin):
    pass

# Patch in convenience functions so that we can do
#   from sparkeras.model import load_model
CUSTOM_OBJECTS = {
    "SparkModel": SparkModel
}

for fun in (getattr(models, f) for f in dir(models)
            if hasattr(getattr(models, f), "__code__")):
    if "custom_objects" in fun.__code__.co_varnames:
        vars()[fun.__name__] = partial(fun, custom_objects=CUSTOM_OBJECTS)

if __name__ == "__main__":
    # Run using: python -m sparkeras.model
    from keras import backend as K
    from sparkeras.tests.utils import get_2to1_model
    from sparkeras.utils import get_hdfs
    from sparkeras.npz import npz_from_dir

    get_hdfs("/iaaexpenv/nlkv/project/foo/foo.npz", "foo.npz",
             overwrite=True)
    data = npz_from_dir("foo.npz", shuffle=False)

    model1 = get_2to1_model(SparkModel)
    model1.fit_lazynpz("foo.npz",
                      max_q_size=50,
                      validation_data="foo.npz",
                      nb_epoch=2,
                      batch_size=400)
    model1.save("tests/model.h5")
    K.set_learning_phase(0)

    predict_kwargs = {"batch_size": 558, "nb_worker": 12}
    preds0 = model1.predict(data, batch_size=predict_kwargs["batch_size"])

    xx = list(model1.predict_lazynpz_generator("foo.npz", **predict_kwargs))

    preds1 = np.concatenate(xx)

    #preds2 = model1.predict_lazynpz("foo.npz", **predict_kwargs)

    # TODO
    # If we set nb_worker > 1, the next statement will not hold
    # because the order seems to be random.
    print(preds0.shape)
    print(preds1.shape)
    #print(preds2.shape)

    if not (preds0.shape == preds1.shape):
        import IPython
        IPython.embed()


maxGames = 50000
rho = 0.2
epsilonDecayRate = 0.9999
epsilon = 1.0
graphics = True
showMoves = not graphics

outcomes = np.zeros(maxGames)
epsilons = np.zeros(maxGames)
Q = {}

if graphics:
    fig = plt.figure(figsize=(10, 10))

for nGames in range(maxGames):
    epsilon *= epsilonDecayRate
    epsilons[nGames] = epsilon
    step = 0
    board = np.array([' '] * 9)  # empty board
    done = False

    while not done:
        step += 1

        # X's turn
        move = epsilonGreedy(epsilon, Q, board)
        boardNew = copy(board)
        boardNew[move] = 'X'
        if (tuple(board), move) not in Q:
            Q[(tuple(board), move)] = 0  # initial Q value for new board,move
        if showMoves:
            printBoard(boardNew)

        if winner(boardNew):
            # X won!
            if showMoves:
                print('        X Won!')
            Q[(tuple(board), move)] = 1
            done = True
            outcomes[nGames] = 1

        elif not np.any(boardNew == ' '):
            # Game over. No winner.
            if showMoves:
                print('        draw.')
            Q[(tuple(board), move)] = 0
            done = True
            outcomes[nGames] = 0

        else:
            # O's turn.  O is a random player!
            moveO = np.random.choice(np.where(boardNew == ' ')[0])
            boardNew[moveO] = 'O'
            if showMoves:
                printBoard(boardNew)
            if winner(boardNew):
                # O won!
                if showMoves:
                    print('        O Won!')
                Q[(tuple(board), move)] += rho * (-1 - Q[(tuple(board), move)])
                done = True
                outcomes[nGames] = -1

        if step > 1:
            Q[(tuple(boardOld), moveOld)] += rho * (Q[(tuple(board), move)] - Q[(tuple(boardOld), moveOld)])

        boardOld, moveOld = board, move  # remember board and move to Q(board,move) can be updated after next steps
        board = boardNew

        if graphics and (nGames % (maxGames / 10) == 0 or nGames == maxGames - 1):
            fig.clf()
            plotOutcomes(outcomes, epsilons, maxGames, nGames - 1)
            clear_output(wait=True)
            display(fig);

if graphics:
    clear_output(wait=True)
print('Outcomes: {:d} X wins {:d} O wins {:d} draws'.format(np.sum(outcomes == 1), np.sum(outcomes == -1),
                                                            np.sum(outcomes == 0)))

def epsilonGreedy(epsilon, Q, board):
    validMoves = np.where(board == ' ')[0]
    if np.random.uniform() < epsilon:
        # Random Move
        return np.random.choice(validMoves)
    else:
        # Greedy Move
        Qs = np.array([Q.get((tuple(board), m), 0) for m in validMoves])
        return validMoves[np.argmax(Qs)]

epsilonGreedy(0.8, Q, board)

def plotOutcomes(outcomes,epsilons,maxGames,nGames):
    if nGames==0:
        return
    nBins = 100
    nPer = int(maxGames/nBins)
    outcomeRows = outcomes.reshape((-1,nPer))
    outcomeRows = outcomeRows[:int(nGames/float(nPer))+1,:]
    avgs = np.mean(outcomeRows,axis=1)
    plt.subplot(3,1,1)
    xs = np.linspace(nPer,nGames,len(avgs))
    plt.plot(xs, avgs)
    plt.xlabel('Games')
    plt.ylabel('Mean of Outcomes\n(0=draw, 1=X win, -1=O win)')
    plt.title('Bins of {:d} Games'.format(nPer))
    plt.subplot(3,1,2)
    plt.plot(xs,np.sum(outcomeRows==1,axis=1),'g-',label='Wins')
    plt.plot(xs,np.sum(outcomeRows==-1,axis=1),'r-',label='Losses')
    plt.plot(xs,np.sum(outcomeRows==0,axis=1),'b-',label='Draws')
    plt.legend(loc="center")
    plt.ylabel('Number of Games\nin Bins of {:d}'.format(nPer))
    plt.subplot(3,1,3)
    plt.plot(epsilons[:nGames])
    plt.ylabel('$\epsilon$')