#Declarations******************************************************************************
from __future__ import print_function
import numpy as np
import os
import sys
import tarfile
import math
import random
import sys
from IPython.display import display, Image
from scipy import ndimage
from six.moves.urllib.request import urlretrieve
from six.moves import cPickle as pickle
from funcCNN import *
from crossValB import *
import matplotlib.pyplot as plt

dataFolder='../data/'

#Output:
f=open(dataFolder+'results/outputVector.txt', 'w')
f.write('1\n')
f.write('1\n')
temp=1.0
trueAcc=str(temp)
print(trueAcc)
f.write(trueAcc+'\n')
f.close()
#Parameters*******************************************************************************
#Maximum number of iterations
iterMax=int(sys.argv[1])
#maximum number of iterations
limit=1.00
#regularization on the weights
beta=0.001
#version of the code
version='gen1'
#size of batch
batchSize=50
#Parameters*******************************************************************************
w1=int(sys.argv[2]) #12
w4=int(sys.argv[3]) #196
h1=int(sys.argv[4]) #148
wd1=int(sys.argv[5]) #21
index=int(sys.argv[6]) #0
kfoldIndex=int(sys.argv[7]) #0
generation=int(sys.argv[8]) #0
#Input Data***********************************************************************************
(test_dataset_Flat,oneHot_test_labels,valid_dataset_Flat,oneHot_valid_labels,
	train_dataset_Flat,oneHot_train_labels,labelSize,vectorSize)=get_Info(
	kfoldIndex,dataFolder)
runs=int(len(oneHot_train_labels)/batchSize)
print(runs)
#Model declaration************************************************************************
import tensorflow as tf
#declare interactive session
sess = tf.InteractiveSession()

#INPUT->CONV LAYER->CONV LAYER->CONV LAYER->RECT FLAT->RECT DROPOUT

#function to declare easily the weights only by shape
def weight_variable(shape):
	initial = tf.truncated_normal(shape, stddev=0.1)
	return tf.Variable(initial)
#function to declare easily the bias only by shape
def bias_variable(shape):
	initial = tf.constant(0.1, shape=shape)
	return tf.Variable(initial)

#input variable
x = tf.placeholder(tf.float32, [None, vectorSize])
#keep probability to change from dropout 0.50 to 1.0 in validation and test
keep_prob = tf.placeholder(tf.float32)
#expected outputs variable
y_ = tf.placeholder(tf.float32, [None, labelSize])

#arrange the tensor as an image (1*31029) 1 channel
x_image0 = tf.reshape(x, [-1,1,vectorSize,1])
x_image = tf.transpose(x_image0, perm=[0,3,2,1])
#arrange the tensor into 1 channels (1*31029)

#1 LAYER*************************************************************************************
#1 Convolutional Layer Explicit for regularization of the weights
#weigth first layer 1 input channels, 12 output channels, 1x21 filter window size
W_conv1 = weight_variable([1, wd1, 1, w1])
#bias declaration the size has to be the same as the output channels 12
b_conv1 = bias_variable([w1])
#convolution (input weights) moving 1 step each time with a relu
h_conv1 = tf.nn.relu(tf.nn.conv2d(x_image, W_conv1,
	strides=[1, 1, 1, 1], padding='SAME') + b_conv1)
#max pooling with a 148 width window size, moving 148 in width by step
h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 1, h1, 1],
	strides=[1, 1, h1, 1], padding='SAME')
#output=545/4
#1 LAYER*************************************************************************************

#Rectifier LAYER*****************************************************************************
#calculated coefficient for the flattening from the size of the 3 convolutional layer
coef=int (h_pool1.get_shape()[1]*h_pool1.get_shape()[2]*h_pool1.get_shape()[3])
h_pool2_flat = tf.reshape(h_pool1, [-1, coef])
#declare the weights considering the constants and 256 output
W_fc1 = weight_variable([coef, w4])
b_fc1 = bias_variable([w4])

#rectifier (matmul)
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
#Rectifier LAYER*****************************************************************************

#Rectifier-Dropout LAYER**********************************************************************
#dropout
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
#declare weights with the ouput layer in this case 2 (labelSize)
W_fc2 = weight_variable([w4, labelSize])
b_fc2 = bias_variable([labelSize])
#output
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
#Rectifier-Dropout LAYER**********************************************************************

#Loss Function********************************************************************************
#cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[0]))
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y_conv, labels=y_)+
	beta*tf.nn.l2_loss(W_conv1))
#Optimizer Adam at 1e-5 (literature)**********************************************************
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
#softmax prediction remember we are using one hot labels
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))

trueResult=tf.argmax(y_conv,1)
trueTest=tf.argmax(y_,1)
#accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#Loss Function********************************************************************************
valid_accuracy_global=0.0
test_accuracy_global=0.0

#start
sess.run(tf.initialize_all_variables())
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
saver.restore(sess, "../data/model/model.ckpt")
#Extra to verify sizes************************************************************************
print(x_image.get_shape())
#print(h_conv1.get_shape())
print(h_pool1.get_shape())

print(y_conv.get_shape())
print(y_.get_shape())
#Extra to verify sizes************************************************************************

print('test ',str(oneHot_valid_labels.shape[0]))

xaV,yaV=getBatch(valid_dataset_Flat,oneHot_valid_labels,oneHot_valid_labels.shape[0],vectorSize)
print(xaV[1])
print(yaV[1])

yResult=[]
yTest=[]
#Ouput Data Variables*************************************************************************
name=str(str(kfoldIndex)+'_'+str(iterMax)+'_'+str(w1)+'_'+str(w4)+'_'+str(h1)+'_'+str(wd1))
f = open(dataFolder+'results/'+name+'.txt', 'a')
#Ouput Data Variables*************************************************************************

#Main Loop************************************************************************************
#initialize variables
iter=0
train_accuracy=0.0
valid_accuracy=0.0
test_accuracy=0.0
#best validation accuracy
best=0
validWindow=[0,0,0]
repeatWindow=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
#limit (normally a dummy value 0.80) and iterations...
while ((best<limit) & (iter<iterMax)):
	indexBatch=[]
	for iB in range (0,len(oneHot_train_labels)):
		indexBatch.append(iB)
	random.shuffle(indexBatch)
	for run in range (0,runs):
		#Get data from train set and store in xa (inputs),ya(labels),ca(constants) 100 batch size
		xa,ya=getBatch_run(train_dataset_Flat,oneHot_train_labels,batchSize,run,indexBatch,vectorSize)
		#pass the values and 0.50 dropout (literature)
		train_step.run(feed_dict={x: xa, y_: ya, keep_prob: 0.5})
		#print each 10 iterations
	#calculate train accuracy
	xa,ya=getBatch(train_dataset_Flat,oneHot_train_labels,batchSize,vectorSize)
	train_accuracy = accuracy.eval(feed_dict={
		x:xa, y_: ya, keep_prob: 1.0})
	#calculate validation accuracy
	xaV,yaV=getBatch(valid_dataset_Flat,oneHot_valid_labels,oneHot_valid_labels.shape[0],vectorSize)

	valid_accuracy=accuracy.eval(feed_dict={
		x: xaV, y_: yaV, keep_prob: 1.0})
	#calculate validation loss
	cross_entropyVal=cross_entropy.eval(feed_dict={
		x: xaV, y_: yaV, keep_prob: 1.0})
	#calculate train loss
	cross_entropyTrain=cross_entropy.eval(feed_dict={
		x: xa, y_: ya, keep_prob: 1.0})
	#append values for graphs
	#if valid accuracy is better than the best accuracy then calculate test accuracy
	#if valid_accuracy>best:
	validWindowValue=0
	tempValid=validWindow
	for i in range(0,len(validWindow)-1):
		tempValid[i]=validWindow[i+1]
	for i in range(0,len(validWindow)):
		validWindow[i]=tempValid[i]
	validWindow[len(validWindow)-1]=valid_accuracy
	for i in range(0,len(validWindow)):
		validWindowValue=validWindowValue+validWindow[i]
	validWindowValue=validWindowValue/len(validWindow)
	tempValid=repeatWindow
	for i in range(0,len(repeatWindow)-1):
		tempValid[i]=repeatWindow[i+1]
	for i in range(0,len(repeatWindow)):
		repeatWindow[i]=tempValid[i]
	repeatWindow[len(repeatWindow)-1]=valid_accuracy
	if np.var(repeatWindow)==0 and iter>10:
		iter=iter
	if (validWindowValue)>best:
		best=(validWindowValue)

		xaT,yaT=getBatch(test_dataset_Flat,oneHot_test_labels,oneHot_test_labels.shape[0],vectorSize)
		#calculate test accuracy
		test_accuracy= accuracy.eval(feed_dict={x:xaT,
			y_: yaT, keep_prob: 1.0})
		# Save the variables to disk.
		if (kfoldIndex==0):
			save_path = saver.save(sess, dataFolder+"model/model.ckpt")
		#plotNNFilter(units)
		#calculate the results of the whole model, probabilities in one hot format
		results=correct_prediction.eval(feed_dict={x:xaT, y_: yaT, keep_prob: 1.0})
		yResult=trueResult.eval(feed_dict={x:xaT, y_: yaT, keep_prob: 1.0})
		yTest=trueTest.eval(feed_dict={x:xaT, y_: yaT, keep_prob: 1.0})
		fOut=open(dataFolder+'results/outputVector.txt', 'w')
		fOut.write('1\n')
		fOut.write('1\n')
		temp=1.0-best
		trueAcc=str(temp)
		print(trueAcc)
		fOut.write(trueAcc+'\n')
		fOut.close()
	#append everything to a log for retrieving results
	log="%d	%d	%g	%g	%g	%g	%g	%g"%(iter,kfoldIndex,train_accuracy,valid_accuracy,best,
		test_accuracy,cross_entropyVal,cross_entropyTrain)
	print(log)
	f.write(log+'\n')
	iter=iter+1
#Main Loop************************************************************************************

f.close()
saveVectorInt(dataFolder+'results/results'+name+'.txt',yResult)
saveVectorInt(dataFolder+'results/test'+name+'.txt',yTest)
f = open(dataFolder+'log3.txt', 'a')
name=str(str(index)+'_'+str(kfoldIndex)+'_'+str(iterMax)+'_'+str(test_accuracy)+'_'+str(valid_accuracy)+'_'+str(best)+'_'+str(w1)+'_'+
		 str(w4)+'_'+str(h1)+'_'+str(wd1)+'_'+str(generation))
f.write(name+'\n')
f.close()

#f=open(str(index)+'.index','a')
#name=str(str(index)+'_'+str(kfoldIndex)+'_'+str(iterMax)+'_'+str(test_accuracy)+'_'+str(valid_accuracy)+'_'+str(best)+'_'+str(w1)+'_'+#
#		 str(w4)+'_'+str(h1)+'_'+str(wd1)+'_'+str(generation))
#f.write(name+'\n')
#f.close()
#Input Data***********************************************************************************
#close session
f=open(dataFolder+'results/outputVector.txt', 'w')
f.write('1\n')
f.write('1\n')
temp=1.0-best
trueAcc=str(temp)
print(trueAcc)
f.write(trueAcc+'\n')
f.close()

xaT,yaT=getBatch(test_dataset_Flat,oneHot_test_labels,oneHot_test_labels.shape[0],vectorSize)

units = sess.run(h_conv1,feed_dict={x:xaT,
			y_: yaT, keep_prob: 1.0})
print(units.shape)
units = sess.run(h_pool1,feed_dict={x:xaT,
			y_: yaT, keep_prob: 1.0})
print(units.shape)

sess.close()


Import('*')

r300compiler = SConscript('#/src/mesa/drivers/dri/r300/compiler/SConscript')

env = env.Clone()
# add the paths for r300compiler
env.Append(CPPPATH = ['#/src/mesa/drivers/dri/r300/compiler', '#/include', '#/src/mesa'])

r300 = env.ConvenienceLibrary(
    target = 'r300',
    source = [
        'r300_chipset.c',
        'r300_clear.c',
        'r300_context.c',
        'r300_debug.c',
        'r300_emit.c',
        'r300_flush.c',
        'r300_fs.c',
        'r300_query.c',
        'r300_render.c',
        'r300_screen.c',
        'r300_state.c',
        'r300_state_derived.c',
        'r300_state_invariant.c',
        'r300_vs.c',
        'r300_texture.c',
        'r300_tgsi_to_rc.c',
    ] + r300compiler) + r300compiler

Export('r300')


#    Copyright 2014 Rackspace
#
#    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.

from oslo_db.sqlalchemy import models
from oslo_utils import uuidutils
import sqlalchemy as sa
from sqlalchemy.ext import declarative
from sqlalchemy.orm import collections

class OctaviaBase(models.ModelBase):

    __data_model__ = None

    def to_data_model(self, _calling_cls=None):
        """Converts to a data model.

        :param _calling_cls: Used only for internal recursion of this method.
                             Should not be called from the outside.
        """
        if not self.__data_model__:
            raise NotImplementedError
        dm_kwargs = {}
        for column in self.__table__.columns:
            dm_kwargs[column.name] = getattr(self, column.name)
        attr_names = [attr_name for attr_name in dir(self)
                      if not attr_name.startswith('_')]
        for attr_name in attr_names:
            attr = getattr(self, attr_name)
            if isinstance(attr, OctaviaBase
                          ) and attr.__class__ != _calling_cls:
                dm_kwargs[attr_name] = attr.to_data_model(
                    _calling_cls=self.__class__)
            elif isinstance(attr, collections.InstrumentedList):
                dm_kwargs[attr_name] = []
                for item in attr:
                    if isinstance(item, OctaviaBase
                                  ) and attr.__class__ != _calling_cls:
                        dm_kwargs[attr_name].append(
                            item.to_data_model(
                                _calling_cls=self.__class__))
                    elif not isinstance(item, OctaviaBase):
                        dm_kwargs[attr_name].append(item)
        return self.__data_model__(**dm_kwargs)

class LookupTableMixin(object):
    """Mixin to add to classes that are lookup tables."""
    name = sa.Column(sa.String(255), primary_key=True, nullable=False)
    description = sa.Column(sa.String(255), nullable=True)

class IdMixin(object):
    """Id mixin, add to subclasses that have an id."""
    id = sa.Column(sa.String(36), primary_key=True,
                   default=uuidutils.generate_uuid)

class ProjectMixin(object):
    """Tenant mixin, add to subclasses that have a project."""
    project_id = sa.Column(sa.String(36))

BASE = declarative.declarative_base(cls=OctaviaBase)


def optimized_longest_substring(word):
    seen = {}
    # index is current index, starter is left pointer
    # to keep track of the start of the sub string
    index = starter = longest = 0

    while index < len(word):
        curr_char = word[index]
        # check if the char is seen before to slide back the new starter to the left
        # one after to that dubplicated char
        if curr_char in seen:
            starter = max(seen[curr_char], starter)

        cur_sub_len = index - starter + 1
        # check if current substring is greater than longest
        # and return the longer one
        longest = max(longest, cur_sub_len)

        # update the position of current char
        seen[curr_char] = index + 1

        # slide the window to right
        index += 1

    return longest

a = 'abcabcbb'
b = 'dvdf'
c = 'pwwkew'
print(optimized_longest_substring(b))


# 如果a+b+c=1000，且 a^2+b^2=c^2  ,求出a,b,c 可能得组合

import time
start_time = time.time()
# for a in range(1001):
#     for b in range(1001):
#         for c in range(1001):
#             if c**2 == b**2+a**2 and c == 1000-a-b:
#                 print("a,b,c:%d,%d,%d"%(a,b,c))

# 第一次修改
# a,b 确定  c已知
for a in range(1001):
    for b in range(1001):
        c = 1000-a-b
        if c**2 == a**2 + b**2:
            print("a,b,c:%d,%d,%d"%(a,b,c))
end_time = time.time()
print("time:%d"%(end_time-start_time))
print("finished")

# 存储学生信息
# name
# age
# hoetown
# 列表，字典，元祖
[
    ("zhangsan",24,"beijing")
    ("zhangsan",24,"beijing")
    ("zhangsan",24,"beijing")
]

# ADT
# class Stus(object):
#     def adds(self):
#     def pop(self):
#     def sort(self):
#     def modify(self):
#
# for stu in stus:
#     if stu(0)=="zhangsan":
#
# [
#     {
#         "name":"zhangsan",
#         "age":23
#         "hometown":"beijing"
#     },
# ]

{
    "zhangsan":{
        "age":24,
        "hometown":"beijing"
    },
}

# dict 检索信息key-value  比 列表快哦


###############################################################################
#
#   Copyright 2016 Michele Filannino
#
#   Derivated from Christopher Kotfila's i2b2 evaluation scripts:
#       https://github.com/kotfic/i2b2_evaluation_scripts
#
#    This file manages all tag related classes and encapsulates much of the
#    functionality that the rest of this evaluation script works off of.
#    It is based on a class hierarchy and most of the tag specific information
#    is contained in tag specific class attributes. This has the advantage of
#    allowing us to validate tag information as it comes into,  and is saved
#    out of objects instantiated from these classes. Additionally classes are
#    equality and set hashing is determined by functions in these classes
#    which may be dynamically changed at run time (see strict_equality() and
#    fuzzy_end_equality() class methods for examples of what this looks like)
#
#    Class hierarchy reference
#
#    [+]Tag
#       [+]DocumentTag
#       [+]AnnotatorTag
#          [+]PHITag
#             [+]NameTag
#             [+]ProfessionTag
#             [+]LocationTag
#             [+]AgeTag
#             [+]DateTag
#             [+]ContactTag
#             [+]IDTag
#             [+]OtherTag

from lxml import etree
from collections import OrderedDict

class Tag(object):
    """ Base Tag object,  implements conversion of lxml element.tag to self.name
    implements 'magic' functions like __eq__ and __hash__ based on the _get_key
    function. Also defines functions for converting tags to different formats
    like back to lxml Element classes and to just plain attribute dictionaries.
    """
    attributes = OrderedDict()

    def __init__(self, element):
        self.name = element.tag
        try:
            self.id = element.attrib['id']
        except KeyError:
            self.id = ""

    def _get_key(self):
        key = []
        for k in self.key:
            key.append(getattr(self, k).lower())
        return tuple(key)

    def _key_equality(self, other):
        return self._get_key() == other._get_key() and \
            other._get_key() == self._get_key()

    def _key_hash(self):
        return hash(self._get_key())

    def __eq__(self, other):
        return self._key_equality(other)

    def __hash__(self):
        return self._key_hash()

    @classmethod
    def strict_equality(cls):
        """  Allows tags to be switched back to default strict evaluation of
        equality as defined by their class attribute 'key.' We use these
        class methods like this because both __eq__ and __hash__ must be
        changed when we relax equality between tags.
        """
        cls.__eq__ = cls._key_equality
        cls.__hash__ = cls._key_hash

    def is_valid(self):
        for k, validp in self.attributes.items():
            try:
                # If the validating function fails throw false
                if not validp(getattr(self, k)):
                    return False
            except AttributeError:
                # Attribute is not set,  if it is in the key then
                # it is a required attribute and we return false.
                if k in self.key:
                    return False

        return True

    def toElement(self):
        element = etree.Element(self.name)
        for k, validp in self.attributes.items():
            try:
                if validp(getattr(self, k)):
                    element.attrib[k] = getattr(self, k)
                else:
                    element.attrib[k] = getattr(self, k)
                    print("WARNING: Expected attribute '%s' for tag %s was "
                          "not valid ('%s')" % (k, "<%s (%s)>" % (self.name,
                                                                  self.id),
                                                getattr(self, k)))
            except AttributeError:
                if k in self.key:
                    element.attrib[k] = ''
                    print("WARNING: Expected attribute '%s' for tag %s" %
                          (k, "<%s, %s>" % (self.name, self.id)))

        return element

    def __repr__(self):
        return "<{0}: {1}>".format(self.__class__.__name__,
                                   ", ".join(self._get_key()))

    def toXML(self):
        return etree.tostring(self.toElement(), encoding='unicode')

    def toDict(self, attributes=None):
        d = {}
        if attributes is None:
            attributes = ["name"] + [k for k, v in self.attributes.items()]

        for a in attributes:
            try:
                d[a] = getattr(self, a)
            except AttributeError:
                d[a] = None

        return d

def isint(x):
    try:
        int(x)
        return True
    except ValueError:
        return False

class AnnotatorTag(Tag):
    """ Defines the tags that model the general tags produced by annotators.
    AnnotatorTag also implements the functions that convert an annotator tag
    to a DocumentTag - a tag designed to annotate document level information
    rather than specific positional information.
    """
    attributes = OrderedDict()
    attributes["id"] = lambda v: True
    attributes["docid"] = lambda v: True
    attributes["start"] = isint
    attributes["end"] = isint
    attributes["text"] = lambda v: True

    key = ["name"]

    def __repr__(self):
        try:
            return "<{0}: {1} s:{2} e:{3}>".format(self.__class__.__name__,
                                                   ", ".join(self._get_key()),
                                                   self.start, self.end)
        except AttributeError:
            return super(Tag, self).__repr__()

    def __init__(self, element):
        super(AnnotatorTag, self).__init__(element)
        self.id = None

        for k, validp in self.attributes.items():
            if k in element.attrib.keys():
                if validp(element.attrib[k]):
                    setattr(self, k, element.attrib[k])
                else:
                    fstr = "WARNING: Expected attribute '{}' for xml element "
                    fstr += "<{} ({})>  was not valid ('{}')"
                    print(fstr.format(k, element.tag,
                                      element.attrib['id']
                                      if 'id' in element.attrib.keys() else '',
                                      element.attrib[k]))
                    setattr(self, k, element.attrib[k])

            elif k in self.key:
                fstr = "WARNING: Expected attribute '{}' for xml element "
                fstr += "<{} ('{}')>, setting to ''"
                print(fstr.format(k, element.tag, element.attrib['id']
                                  if 'id' in element.attrib.keys() else ''))

                setattr(self, k, '')

    @classmethod
    def fuzzy_end_equality(cls, distance):
        """ Set the __eq__ and __hash__ functions of the cls argument
        to be _fuzzy_end__eq__ and _fuzzy_end__hash__ as defined by this
        function. Scope in the distance paramater which allows us to set
        different number of characters that we allow the end attribute to
        shift before we no longer consider two tags equal.
        """

        def _fuzzy_end__eq__(self, other):
            self_dict = OrderedDict(zip(self.key, self._get_key()))
            other_dict = OrderedDict(zip(other.key, other._get_key()))

            self_end = int(self_dict.pop("end"))
            other_end = int(other_dict.pop("end"))

            if self_dict.values() == other_dict.values() and \
               abs(self_end - other_end) <= distance:
                return True

            return False

        def _fuzzy_end__hash__(self):
            """ Here we effectively ignore the 'end' attribute when hashing.
            Two tags with different endings will hash to the same value and
            then be handled by _fuzzy_end__eq__ when it comes time to do
            comparisons.
            """
            self_dict = OrderedDict(zip(self.key, self._get_key()))

            # if a == b then it MUST be the case that hash(a) == hash(b)
            # but if a != b then the relationship between hash(a) and hash(b)
            # does not need to be defined.
            self_dict['end'] = True
            return hash(tuple(self_dict.values()))

        cls.__eq__ = _fuzzy_end__eq__
        cls.__hash__ = _fuzzy_end__hash__

    def validate(self):
        for k, validp in self.attributes.items():
            try:
                if validp(getattr(self, k)):
                    continue
                else:
                    return False
            except AttributeError:
                if k in self.key:
                    return False

        return True

    def get_document_annotation(self):
        element = etree.Element(self.name)
        for k, v in zip(self.key, self._get_key()):
            element.attrib[k] = v
        return DocumentTag(element)

    def get_start(self):
        try:
            return int(self.start)
        except TypeError:
            return self.start

    def get_end(self):
        try:
            return int(self.end)
        except TypeError:
            return self.end

class PHITag(AnnotatorTag):
    valid_TYPE = ["PATIENT", "DOCTOR", "USERNAME", "PROFESSION", "ROOM",
                  "DEPARTMENT", "HOSPITAL", "ORGANIZATION", "STREET", "CITY",
                  "STATE", "COUNTRY", "ZIP", "OTHER", "LOCATION-OTHER", "AGE",
                  "DATE", "PHONE", "FAX", "EMAIL", "URL", "IPADDR", "SSN",
                  "MEDICALRECORD", "HEALTHPLAN", "ACCOUNT", "LICENSE",
                  "VEHICLE", "DEVICE", "BIOID", "IDNUM"]
    attributes = OrderedDict(AnnotatorTag.attributes.items())
    attributes['TYPE'] = lambda v: v.upper() in PHITag.valid_TYPE

    key = AnnotatorTag.key + ["start", "end", "TYPE"]

    def _get_key(self):
        key = []
        for k in self.key:
            key.append(getattr(self, k).upper())
        return tuple(key)

    def exact_equals(self, other):
        pass

    def overlap_equals(self, other):
        pass

class NameTag(PHITag):
    valid_TYPE = ['PATIENT', 'DOCTOR', 'USERNAME']
    attributes = OrderedDict(PHITag.attributes.items())
    attributes['TYPE'] = lambda v: v.upper() in NameTag.valid_TYPE

class ProfessionTag(PHITag):
    valid_TYPE = ["PROFESSION"]
    attributes = OrderedDict(PHITag.attributes.items())
    attributes['TYPE'] = lambda v: v.upper() in ProfessionTag.valid_TYPE

class LocationTag(PHITag):
    valid_TYPE = ['ROOM', 'DEPARTMENT', 'HOSPITAL', 'ORGANIZATION', 'STREET',
                  'CITY', 'STATE', 'COUNTRY', 'ZIP', 'LOCATION-OTHER']
    attributes = OrderedDict(PHITag.attributes.items())
    attributes['TYPE'] = lambda v: v.upper() in LocationTag.valid_TYPE

class AgeTag(PHITag):
    valid_TYPE = ['AGE']
    attributes = OrderedDict(PHITag.attributes.items())
    attributes['TYPE'] = lambda v: v.upper() in AgeTag.valid_TYPE

class DateTag(PHITag):
    valid_TYPE = ['DATE']
    attributes = OrderedDict(PHITag.attributes.items())
    attributes['TYPE'] = lambda v: v.upper() in DateTag.valid_TYPE

class ContactTag(PHITag):
    valid_TYPE = ['PHONE', 'FAX', 'EMAIL', 'URL', 'IPADDR']
    attributes = OrderedDict(PHITag.attributes.items())
    attributes['TYPE'] = lambda v: v.upper() in ContactTag.valid_TYPE

class IDTag(PHITag):
    valid_TYPE = ['SSN', 'MEDICALRECORD', 'HEALTHPLAN', 'ACCOUNT',
                  'LICENSE', 'VEHICLE', 'DEVICE', 'BIOID', 'IDNUM']
    attributes = OrderedDict(PHITag.attributes.items())
    attributes['TYPE'] = lambda v: v.upper() in IDTag.valid_TYPE

class OtherTag(PHITag):
    valid_TYPE = 'OTHER'
    attributes = OrderedDict(PHITag.attributes.items())
    attributes['TYPE'] = lambda v: v.upper() in OtherTag.valid_TYPE

PHITag.tag_types = {
    "PHI": PHITag,
    "NAME": NameTag,
    "PROFESSION": ProfessionTag,
    "LOCATION": LocationTag,
    "AGE": AgeTag,
    "DATE": DateTag,
    "CONTACT": ContactTag,
    "ID": IDTag,
    "OTHER": OtherTag}

PHI_TAG_CLASSES = [NameTag,
                   ProfessionTag,
                   LocationTag,
                   AgeTag,
                   DateTag,
                   ContactTag,
                   IDTag,
                   OtherTag]

# Comment should be last in tag order,  so add it down here
# that way all other sub tags have had their attributes set first
for c in PHI_TAG_CLASSES:
    c.attributes["comment"] = lambda v: True


import pygame, sys, random
from pygame.locals import *
pygame.init()
tickTock = pygame.time.Clock()
FPS = 30
WINLENGTH = 500
WINBREADTH = 500
BOXSIZE = 40
GAPSIZE = 7
BOARDLENGTH = 5
BOARDBREADTH = 5
XMARGIN = (WINLENGTH - (BOXSIZE + GAPSIZE) * BOARDLENGTH) / 2
YMARGIN = (WINBREADTH - (BOXSIZE + GAPSIZE) * BOARDBREADTH) / 2
assert XMARGIN > 0 and YMARGIN > 0, 'The margins can never be negetive'
#Colors---------------------(  R,   G,   B)
RED =           pygame.Color(255,   0,   0)
LIGHTRED =      pygame.Color(255, 138, 138)
GREEN =         pygame.Color(  0, 255,   0)
LIGHTGREEN =    pygame.Color(138, 255, 138)
BLUE =          pygame.Color(  0,   0, 255)
LIGHTBLUE =     pygame.Color(138, 138, 255)
BKGD =          pygame.Color(255, 255, 255)  #The yellow Color
ALLCOLORS = (RED, GREEN, BLUE, LIGHTRED, LIGHTGREEN, LIGHTBLUE)
R = G = B = 0
def getFuckingBoard():
    global R, G, B
    COLORS = ALLCOLORS[:3]
    result = []
    for x in range(BOARDLENGTH):
        col = []
        for y in range(BOARDBREADTH):
            rand = random.randint(0, 1000) % 3
            col.append(COLORS[rand])
            if rand == 0:
                R += 1
            elif rand == 1:
                G += 1
            else:
                B += 1
        result.append(col)
    return result

def showFuckingBoard():
    for x in range(BOARDLENGTH):
        for y in range(BOARDBREADTH):
            COORDINATE = getXYofBox(x, y)
            pygame.draw.rect(DISPLAY, BOARD[x][y], (COORDINATE[0], COORDINATE[1], BOXSIZE, BOXSIZE))

def getXYofBox(x, y):
   return (XMARGIN + x * (BOXSIZE + GAPSIZE), YMARGIN + y * (BOXSIZE + GAPSIZE))

def getBoxAtPixel(mousex, mousey):
    for x in range(BOARDLENGTH):
        for y in range(BOARDBREADTH):
            fuckinRect = pygame.Rect(XMARGIN + x * (BOXSIZE + GAPSIZE), YMARGIN + y * (BOXSIZE + GAPSIZE), BOXSIZE, BOXSIZE)
            if fuckinRect.collidepoint((mousex, mousey)):
                return (x, y)
    return (None, None)

def highLightBox(BOXX, BOXY):
    if BOARD[BOXX][BOXY] == RED:
        BOARD[BOXX][BOXY] = LIGHTRED
    elif BOARD[BOXX][BOXY] == GREEN:
       BOARD[BOXX][BOXY] = LIGHTGREEN
    elif BOARD[BOXX][BOXY] == BLUE:
        BOARD[BOXX][BOXY] = LIGHTBLUE

def changeFuckingColor(BOXX, BOXY):
    if BOARD[BOXX][BOXY] == LIGHTRED:
        BOARD[BOXX][BOXY] = LIGHTGREEN
    elif BOARD[BOXX][BOXY] == LIGHTGREEN:
        BOARD[BOXX][BOXY] = LIGHTBLUE
    elif BOARD[BOXX][BOXY] == LIGHTBLUE:
        BOARD[BOXX][BOXY] = LIGHTRED
### Mouse hover currently indicates which color to print
### Reuse this program to have three basic sqaures that will show the color of them and speak the color
def resetFuckingBoard():
    for BOXX in range(BOARDLENGTH):
        for BOXY in range(BOARDBREADTH):
            if BOARD[BOXX][BOXY] == LIGHTRED:
                BOARD[BOXX][BOXY] = RED
                print("The Color was Red")
            elif BOARD[BOXX][BOXY] == LIGHTGREEN:
                BOARD[BOXX][BOXY] = GREEN
                print("The Color was green")
            elif BOARD[BOXX][BOXY] == LIGHTBLUE:
                BOARD[BOXX][BOXY] = BLUE
                print("The Color Is Blue")

def hasWon():
    BASE = BOARD[0][0]
    for x in range(BOARDLENGTH):
        for y in range(BOARDBREADTH):
            if BASE != BOARD[x][y]:
                return False
    return True

def wonAnimation():
    pass

def predatorTry():
   stepsR = 2 * G + B
   stepsG = 2 * B + R
   stepsB = 2 * R + G
   steps = [stepsR, stepsG, stepsB]
   steps.sort()
   return steps[0]

def main():
    global BOARD, DISPLAY
    DISPLAY = pygame.display.set_mode((WINLENGTH, WINBREADTH))
    pygame.display.set_caption("Color Puzzle")
    DISPLAY.fill(BKGD)
    BOARD = getFuckingBoard()
    predator_try = predatorTry()
    showFuckingBoard()
    pygame.display.update()
    PREVIOUS = (None, None)
    mousex, mousey = 0, 0
    while True:
        CLICKED = False
        DISPLAY.fill(BKGD)
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEMOTION:
                mousex, mousey = event.pos
            elif event.type == MOUSEBUTTONUP:
                mousex, mousey = event.pos
                CLICKED = True
        BOXX, BOXY = getBoxAtPixel(mousex, mousey)
        if BOXX != None and BOXY != None:
            PREVIOUS = (BOXX, BOXY)
            highLightBox(BOXX, BOXY)
            if CLICKED:
                changeFuckingColor(BOXX, BOXY)
                highLightBox(BOXX, BOXY)
        else:
            resetFuckingBoard()

        showFuckingBoard()
        pygame.display.update()
        if hasWon():
            wonAnimation()
            print ('You Won!!!, The Predator would have done it in just %d tries' %(predator_try))
            BOARD = getFuckingBoard()

if __name__ == '__main__':
    main()


import io
import random

t = 'hello,baby'
s = io.StringIO(t)
print(s)
print(s.getvalue())
s.seek(7)
s.write('h')
print(s.getvalue())

dit = {
    'name': '刘欢',
    'age': 19,
    'sex': '男'
}

a, b, c = dit.values()  # 获取键值对的值
d, e, f = dit.items()  # 获取键值对的其中一对
g, h, i = dit  # 获取键值对的键
print(a, b, c, d, e, f, g, h, i)

lst2 = [{'id': 111, 'name': '网站'},
        {'id': 112, 'name': '营销工具'},
        {'id': 113, 'name': '公众号'},
        {'id': 119, 'name': '网报小程序'},
        {'id': 101, 'name': '自然口碑'},
        {'id': 102, 'name': '营销活动'},
        {'id': 103, 'name': '营销口碑'},
        {'id': 114, 'name': '地推'},
        {'id': 115, 'name': '广告'},
        {'id': 116, 'name': '户外广告'}]
for i in lst2:  # 先把列表里的字典取出来
    for x in i:  # 再把字典里的键取出来
        print(i[x])  # 最后通过字典取值的办法把字典里的值取出来

print(lst2)
w, q = random.sample(lst2, 1)[0].values()
print(w, q)
lst3 = [x.get('id') for x in lst2]
print(lst3)

name = ['王以太', '张锋', '李琴']
age = [12, 23, 17]
for x, y in zip(name, age):
    print('这是{}，他已经{}岁了'.format(x, y))


"""
Read file into texts and calls.
It's ok if you don't understand how to read files.
"""
import csv
with open('texts.csv', 'r') as f:
    reader = csv.reader(f)
    texts = list(reader)

with open('calls.csv', 'r') as f:
    reader = csv.reader(f)
    calls = list(reader)

"""
TASK 1:
How many different telephone numbers are there in the records?
"There are <count> different telephone numbers in the records."
"""

all_non_unique_numbers =  []

## getting all phone records from calls and appending the # in an array
for record in calls:
    all_non_unique_numbers.append(record[0])
    all_non_unique_numbers.append(record[1])

## getting all phone records from texts and appending the # in an array
for record in texts:
    all_non_unique_numbers.append(record[0])
    all_non_unique_numbers.append(record[1])

## function to help remove duplicates from the list of phone numbers
def remove_duplicates(non_unique_list):
    unique_list = []
    unique_list.append(non_unique_list[0])
    for element in non_unique_list:
        counter = 0
        for number in unique_list:
            counter += 1
            if element != number and counter == len(unique_list):
                unique_list.append(element)
            elif element != number:
                continue
            else:
                break
    return unique_list

## solution
count_of_numbers = len(remove_duplicates(all_non_unique_numbers))
print(f'There are {count_of_numbers} different telephone numbers in the records.')
## Big O: At the worst case this solution runs at O(n2) - Quadratic Time,
## because for each element in the input we perform a search operation and a comparison operation

## Test cases
def test_cases():
    assert remove_duplicates([1, 1, 2, 2, 2, 3, 3, 3,]) == [1, 2, 3]
    assert remove_duplicates([2, 2, 2,]) == [2]
    assert remove_duplicates([10]) == [10]
    assert remove_duplicates([190, 190, 190, 1]) == [190, 1]

test_cases()


# Removes duplicates from a file.

prev_ref = " "

file_name = input("Enter file name: ")
out_file = input("Enter output file name: ")
with open(file_name) as file:
    for line in file:
        try:
            (ref, length) = line.split()
            if not (ref == prev_ref):
                print(ref + " " + length, file=open(out_file, "a"))
            prev_ref = ref
        except:
            next(file)


#encoding: utf-8
from OpenOrange import *

ParentComputer = SuperClass("Computer","Master",__file__)
class Computer(ParentComputer):

    buffer = RecordBuffer("Computer")

    @classmethod
    def isPos(objclass):
        from LocalSettings import LocalSettings
        from Computer import Computer
        ls = LocalSettings.bring()
        comp = Computer.bring(ls.Computer)
        posf = False
        if (comp):
           posf = comp.POSflag
        return posf


# This script gets the error bars for the individual points when measuring the differential scintillation
import numpy as np
import math

def ipartial(real, imaginary):
    return (1/(real*(1 + (imaginary/real)**2)))**2

def rpartial(real, imaginary):
    return (1/(real**2*(1 +(imaginary/real)**2)))**2

def GetPlot(cross, xlim):
    """Return the plot  errorbars for the associated points as a one-dimensional numpy array

    Cross: The cross secondary spectrum, already binned. First dimension assumed to be
    doppler frequency, second dimension is time delay. Only positive time delays should be
    plotted.
    """

    copy = RemoveNans(cross)
    mean = np.nanmean(copy, axis=0)
    real = np.real(mean)
    imaginary = np.imag(mean)
    realstd = np.nanstd(np.real(copy), axis=0)/np.sqrt(copy.shape[0])
    imagstd = np.nanstd(np.imag(copy), axis=0)/np.sqrt(copy.shape[0])
    std = np.sqrt(ipartial(real,imaginary)*(np.abs(imagstd))**2 + \
                      rpartial(real,imaginary)*(np.abs(realstd))**2)
    domain = np.linspace(start=-xlim, stop=xlim, num=std.shape[0])
    return domain, np.angle(mean), std

def GetPlot_Modified(cross, xlim, off_std):
    """Return a modified plot errorbars for the associated points as a one-dimensional numpy
    array. The difference in power between the parabola and outside the parabola is removed
    in the calculation of the error by dividing out the standard deviation of the off gate
    divided by route n ()
    Cross: The cross secondary spectrum, already binned. First dimension assumed to be doppler
    frequency, second dimension is time delay. Only positive time delays should be plotted"""

    copy = RemoveNans(cross)
    mean = np.nanmean(copy, axis=0) # Average along the doppler time axis, collapsing frequency
    real = np.real(mean)
    imaginary = np.imag(mean)
    power_diff = off_std/np.sqrt(copy.shape[0]) #
    realstd = np.nanstd(np.real(copy)/power_diff, axis=0)/np.sqrt(copy.shape[0])
    imagstd = np.nanstd(np.imag(copy)/power_diff, axis=0)/np.sqrt(copy.shape[0])
    std = np.sqrt(ipartial(real,imaginary)*(np.abs(imagstd))**2 + \
                      rpartial(real,imaginary)*(np.abs(realstd))**2)
    domain = np.linspace(start=-xlim, stop=xlim, num=std.shape[0])
    return domain, np.angle(mean), std

def RemoveNans(cross, xlim=None):
    """Remove vertical axis whose values are all nan and return new matrix"""
    # Temporary locations of all nan values in the vertical and horizatonal axis
    hnans, vnans = [], []
    for i in range(cross.shape[0]):
        if (np.isnan(cross[i,:])).all():
            hnans.append(i)
    for j in range(cross.shape[1]):
        if (np.isnan(cross[:,j])).all():
            vnans.append(j)
    # Delete nan axis
    copy = np.delete(cross, vnans, axis=1)
    copy = np.delete(copy, hnans, axis=0)
    length = math.ceil(cross.shape[1]/2)
    # Make the domain axis if the x axis limit is given
    if xlim is not None:
        domain = np.arange(-length, length) + 0.5
        domain *= (xlim/domain[-1])
        domain = np.delete(domain, vnans)
        return copy, domain
    else:
        return copy


from validator import Validator
from settings import *

class RotorTypeError(Exception):
    def __init__(self, rotor_type):
        super().__init__("{} is not a valid rotor type".format(rotor_type))

class RotorListRangeError(Exception):
    def __init__(self, rotor_type, list_type):
        super().__init__("{0} {1} is incorrect length, must be 26 characters in length"
                         .format(rotor_type, list_type))

class RotorListCharError(Exception):
    def __init__(self, rotor_type, list_type):
        super().__init__("{0} {1} must contain all letters or all numbers not both"
                         .format(rotor_type, list_type))

class RotorListTypeError(Exception):
    def __init__(self, rotor_type, list_type):
        super().__init__("{0} {1} elements must be of type char only"
                         .format(rotor_type, list_type))

class RotorListRepeatError(Exception):
    def __init__(self, rotor_type, list_type):
        super().__init__("{0} {1} all characters must be unique, Same char cannot be repeated"
                         .format(rotor_type, list_type))

class RotorTurnoverListError(Exception):
    def __init__(self, rotor_type):
        super().__init__("{} turnover list characters must also be in ring characters list"
                         .format(rotor_type))

class RotorRingCharError(Exception):
    def __init__(self, character, correct_char_type):
        self.required_type = None
        if correct_char_type.isalpha():
            self.required_type = "numeric"
        else:
            self.required_type = "letter"
        super().__init__("{0} is not a valid ring character. Required character must be a {1}"
                         .format(character, self.required_type))

class Rotor:
    def __init__(self, rotor_characters,
                 ring_characters,
                 turnover_characters,
                 rotor_type=None):
        """Rotor is initialized with four arguments. rotor_characters is a list of \
        the characters that define the cross wiring within the rotor. ring_characters \
        is a list of characters that are displayed around the ring of the rotor. \
        kickover_characters is a list of characters that repressent the rotor settings \
        at which the left neighbouring rotor will turnover. During initialization all \
        rotor lists are validated and if a non empty turnover list is provided then \
        turnover is set to True"""

        self.validator = Validator()
        self._rotor_type = rotor_type
        self._rotor_characters = self._validate_rotor_lists(rotor_characters, "rotor characters")
        self._ring_characters = self._validate_rotor_lists(ring_characters, "ring characters")
        self._absolute_characters = [chr(i) for i in range(65, 91)]
        self._turnover_characters = self._validate_turnover_list(turnover_characters)
        self._default_rotor_position = ring_characters[0]
        self._initial_rotor_character = rotor_characters[0]
        self.can_turnover = True
        if len(self._turnover_characters) == 0:
            self.can_turnover = False

    # PUBLIC METHODS -----------------------------------------------------------------

    def rotor_type(self):
        """rotor_type returns the rotor type"""

        return self._rotor_type

    def output_to_reflector(self, index):
        """output_to_reflector takes an index as an argument. Returns the output \
        index of the character in the rotor_characters list for the same character \
        in the absolute_characters list at the argument index"""

        char = self._rotor_characters[index]
        output_index = self._absolute_characters.index(char)
        return output_index

    def output_from_reflector(self, index):
        """output_from_reflector takes an index as an argument. Returns the output \
        index of the character in the absolute_characters list for the same character \
        in the rotor_characters list at the argument index"""

        char = self._absolute_characters[index]
        output_index = self._rotor_characters.index(char)
        return output_index

    def turnover(self):
        """turnover returns True if current ring character is a turnover character, \
        else returns False"""

        for character in self._turnover_characters:
            if character == self.get_rotor_setting():
                return True

        return False

    def rotor_inc(self):
        """rotor_inc moves all values in _ring_characters, _rotor_characters and \
        _absolute_characters one index up the list and the last character to the \
        first index of the list"""

        self.ring_characters_inc()
        self._rotor_characters_inc()
        self._absolute_characters_inc()

    def rotor_dec(self):
        """rotor_dec moves all values in _ring_characters, _rotor_characters and \
        _absolute_characters one index down the list and the first character to \
        the last index of the list"""

        self.ring_characters_dec()
        self._rotor_characters_dec()
        self._absolute_characters_dec()

    def keyed_rotor(self):
        """keyed_rotor increments rotor. Returns True if turnover else returns False"""

        if self.turnover():
            self.rotor_inc()
            return True
        else:
            self.rotor_inc()
            return False

    def set_default_rotor_position(self):
        """set_default_rotor_setting calls set_rotor_position and sets rotor \
        position to 'A' if _ring_characters are letters or '1' if _ring_characters \
        are numeric"""

        self.set_rotor_setting(self._default_rotor_position)

    def set_rotor_setting(self, character):
        """set_rotor_setting takes a character as an argument and rotates the rotor \
        position until the current rotor setting is equal to the argument character \
        and then returns the current rotor setting"""

        character = self.validator.valid_character(character)
        while self.get_rotor_setting() != character:
            self.rotor_inc()
        return self.get_rotor_setting()

    def get_rotor_setting(self):
        """get_rotor_setting returns the character at _ring_characters[0] the \
        character that would be displayed in the rotor window of an enigma machine"""

        return self._ring_characters[0]

    def set_default_rotor_settings(self):
        """set_default_rotor_settings sets rotor to default position and then sets \
        the ring setting to default"""

        self.set_default_rotor_position()
        self.set_default_ring_setting()
        return (self.get_ring_setting(), self.get_rotor_setting())

    def set_ring_setting(self, character):
        """set_ring_setting takes a character as an argument. If character is valid \
        rotor is rotated until _absolute_characters[0] is equal to _default rotor \
        position. _ring_characters are rotated until the _ring_characters[0] is \
        equal to character and then the rotor is rotated back to its original rotor \
        position"""

        try:
            character = self.valid_ring_character(character)
        except RotorRingCharError as e:
            raise e
        else:
            current_ring_setting = self._ring_characters[0]
            if self._default_rotor_position not in self._absolute_characters:
                default_rotor_position = self._convert_ring_character(self._default_rotor_position)
            else:
                default_rotor_position = self._default_rotor_position
            while self._absolute_characters[0] != default_rotor_position:
                self.rotor_inc()
            while self._ring_characters[0] != character:
                self.ring_characters_inc()
            while self._ring_characters[0] != current_ring_setting:
                self.rotor_inc()
            return character

    def get_ring_setting(self):
        """get_ring_setting gets the index of the _default_rotor_position character \
        in the _absolute_characters list and then gets the character at that index \
        in the _ring_characters list. This character is the current ring setting \
        and is returned"""

        try:
            index = self._absolute_characters.index(self._default_rotor_position)
        except ValueError:
            default_rotor_position = self._convert_ring_character(self._default_rotor_position)
            index = self._absolute_characters.index(default_rotor_position)
        ring_setting = self._ring_characters[index]
        return ring_setting

    def valid_ring_character(self, character):
        """valid_ring_character takes a character as an argument and returns \
        True if the character is in the ring_characters list else returns False"""

        try:
            character = self.validator.valid_character(character)
        except ValueError:
            raise RotorRingCharError(character, self._ring_characters[0])
        else:
            if character in self._ring_characters:
                return character
            elif "{:0>2}".format(character) in self._ring_characters:
                return "{:0>2}".format(character)
            else:
                raise RotorRingCharError(character, self._ring_characters[0])

    def ring_character_type(self):

        if self._ring_characters[0].isalpha():
            return "letter"
        else:
            return "number"

    def ring_characters_inc(self):
        """ring_characters_inc moves all the values in _ring_characters one index \
        up the list and the last value to the first index of the list"""

        self._ring_characters = Rotor._inc_list(self._ring_characters)

    def ring_characters_dec(self):
        """ring_characters_dec moves all the values in _ring_characters one index \
        down the list and the first value to the last index of the list"""

        self._ring_characters = Rotor._dec_list(self._ring_characters)

    def set_default_ring_setting(self):
        """set_default_ring_setting sets the ring setting to the same value as \
        _default_rotor_position"""

        self.set_ring_setting(self._default_rotor_position)

    def get_rotor_dict(self):
        """get_rotor_dict returns a dictionary with the rotor type, ring characters, \
        rotor characters, absolute characters and turnover characters"""

        rotor_dict = {}
        rotor_dict["ROTOR_TYPE"] = self.rotor_type()
        rotor_dict["ROTOR_SETTING"] = self.get_rotor_setting()
        rotor_dict["RING_SETTING"] = self.get_ring_setting()
        rotor_dict["RING_CHARACTERS"] = self._ring_characters.copy()
        rotor_dict["ROTOR_CHARACTERS"] = self._rotor_characters.copy()
        rotor_dict["ABSOLUTE_CHARACTERS"] = self._absolute_characters.copy()
        rotor_dict["TURNOVER_CHARACTERS"] = self._turnover_characters.copy()
        return rotor_dict

    # PRIVATE METHODS ---------------------------------------------------------------

    def _rotor_characters_inc(self):
        """_rotor_characters_inc moves all the values in rotor_characters one index \
        up the list and the last value to the first index of the list"""

        self._rotor_characters = Rotor._inc_list(self._rotor_characters)

    def _rotor_characters_dec(self):
        """_rotor_characters_dec moves all the values in rotor_characters one index \
        down the list and the first value to the last index of the list"""

        self._rotor_characters = Rotor._dec_list(self._rotor_characters)

    def _absolute_characters_inc(self):
        """_absolute_characters_inc moves all the values in absolute_characters one \
        index up the list and the last value to the first index of the list"""

        self._absolute_characters = Rotor._inc_list(self._absolute_characters)

    def _absolute_characters_dec(self):
        """_absolute_characters_dec moves all the values in absolute_characters one \
        index down the list and the first value to the last index of the list"""

        self._absolute_characters = Rotor._dec_list(self._absolute_characters)

    @staticmethod
    def _inc_list(_list):
        """_inc_list takes a list as an argument. all values in the list are moved \
        one index up the list and the last value to the first index of the list"""

        first_char = _list[0]
        new_list = list(range(26))
        new_list[0:25] = _list[1:]
        new_list[25] = first_char
        return new_list

    @staticmethod
    def _dec_list(_list):
        """_dec_list takes a list as an argument. all values in the list are moved \
        one index down the list and the first value to the last index of the list"""

        last_char = _list[-1]
        new_list = list(range(26))
        new_list[1:] = _list[0:25]
        new_list[0] = last_letter
        return new_list

    def _validate_rotor_lists(self, _list, list_type):
        """_validate_rotor_lists takes a list and a list_type string as an argument. \
        If the length of the list is not 26 RotorListRangeError is raised. If all \
        the elements are not unique RotorListRepeatError is raised. If any element \
        in the list is not an alphanumeric character RotorListTypeError is raised. \
        If all elements in the list are not alpha or not numeric RotorListCharError \
        is raised. If the list is valid a valid list with uppercase characters is \
        returned"""

        if len(_list) != 26:
            raise RotorListRangeError(self.rotor_type(), list_type)

        if not self.validator.unique_list(_list):
            raise RotorListRepeatError(self.rotor_type(), list_type)

        valid_list = []
        for i in range(len(_list)):
            if not _list[i].isalnum():
                raise RotorListTypeError(self.rotor_type(), list_type)
            elif _list[i].isalpha() and _list[0].isalpha():
                valid_list.append(_list[i].upper())
            elif _list[i].isnumeric() and _list[0].isnumeric():
                valid_list.append(_list[i])
            else:
                raise RotorListCharError(self.rotor_type(), list_type)
        return valid_list

    def _convert_turnover_list(self, turnover_characters):

        converted_list = []
        letters_list = LETTERS
        numbers_list = NUMBERS
        for character in turnover_characters:
            if character in self._ring_characters:
                converted_list.append(character)
            elif character in letters_list:
                index = letters_list.index(character)
                number = numbers_list[index]
                converted_list.append(number)
        return converted_list

    def _validate_turnover_list(self, turnover_list):
        """_validate_turnover_list takes a list as an argument. Checks that each \
        character in the turnover_list is also in the ring_characters list. If a \
        turnover character does not match any character in the ring_characters \
        list RotorTurnoverListError is raised. If list is valid a valid list \
        containing uppercase characters is returned. If turnover_list is empty \
        an empty list is returned"""

        turnover_list = self._convert_turnover_list(turnover_list)
        valid_turnover_list = []
        for turnover_char in turnover_list:
            for char in self._ring_characters:
                if turnover_char.upper() == char.upper():
                    valid_turnover_list.append(turnover_char.upper())
        if len(valid_turnover_list) != len(turnover_list):
            raise RotorTurnoverListError(self._rotor_type)
        else:
            return valid_turnover_list

    def _convert_ring_character(self, character):

        index = NUMBERS.index(character)
        default_rotor_position = LETTERS[index]
        return default_rotor_position

if __name__ == "__main__":
    rotor_type = 'Rotor 1'
    rotor_characters = ['E', 'K', 'M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S',
                        'P', 'A', 'I', 'B', 'R', 'C', 'J']
    ring_characters_alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                             'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    ring_characters_numeric = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15',
                               '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26']
    turnover_characters_alpha = ['Q']
    turnover_characters_numeric = ['15']

    rotor1 = Rotor(rotor_characters, ring_characters_alpha, turnover_characters_alpha, rotor_type)
    print(rotor1.rotor_type())
    print(rotor1.get_rotor_setting())
    rotor1.keyed_rotor()
    print(rotor1.get_rotor_setting())
    print("ring setting ", rotor1.get_ring_setting())
    rotor1.set_default_rotor_position()
    print(rotor1.get_rotor_setting())
    rotor1.set_ring_setting('C')
    print("ring setting ", rotor1.get_ring_setting())
    print(rotor1.set_default_rotor_settings())

    rotor2 = Rotor(rotor_characters, ring_characters_numeric, turnover_characters_numeric, rotor_type)
    print(rotor2.rotor_type())
    print(rotor2.get_rotor_setting())
    rotor2.keyed_rotor()
    print(rotor2.get_rotor_setting())
    print("ring setting ", rotor2.get_ring_setting())
    rotor2.set_default_rotor_position()
    print(rotor2.get_rotor_setting())
    rotor2.set_ring_setting("03")
    print("ring setting ", rotor2.get_ring_setting())
    print(rotor2.set_default_rotor_settings())


"""
Description: Validate a STAC item or catalog against the STAC specification.

Usage:
    stac_validator <stac_file> [--version STAC_VERSION] [--timer] [--recursive] [--log_level LOGLEVEL] [--custom CUSTOM] [--update] [--force] [--extension EXTENSION] [--core] [--legacy]

Arguments:
    stac_file  Fully qualified path or url to a STAC file.

Options:
    -v, --version STAC_VERSION   Version to validate against. [default: missing]
    -h, --help                   Show this screen.
    --timer                      Reports time to validate the STAC. (seconds)
    --update                     Migrate to newest STAC version (1.0.0-beta.2) for testing
    --log_level LOGLEVEL         Standard level of logging to report. [default: CRITICAL]
    --custom CUSTOM              Validate against a custom schema whether local or remote
    --force                      Set version='0.9.0' and fix missing id for older objects to force validation
    --recursive                  Recursively validate an entire collection or catalog.
    --extension EXTENSION        Validate an extension
    --core                       Validate on core only
    --legacy                     Validate on older schemas, must be accompanied by --version
"""

import json
import logging
import os
import shutil
import sys
import tempfile
import pystac
import requests
import jsonschema
from json.decoder import JSONDecodeError
from timeit import default_timer
from typing import Tuple
from urllib.parse import urlparse
from urllib.error import HTTPError
from docopt import docopt
from pystac.serialization import identify_stac_object
from pystac.validation import STACValidationError
from pystac import Item, Catalog, Collection
from jsonschema import RefResolutionError, RefResolver, validate
from jsonschema.exceptions import ValidationError

logger = logging.getLogger(__name__)

class VersionException(Exception):
    pass

class ExtensionException(Exception):
    pass

class LegacyVersionException(Exception):
    pass

class StacValidate:
    def __init__(
        self,
        stac_file: str,
        extension: str = "",
        version: str = "missing",
        log_level: str = "CRITICAL",
        update: bool = False,
        force: bool = False,
        recursive: bool = False,
        core: bool = False,
        legacy: bool = False,
        custom: str = "",
    ):
        """Validate a STAC file.

        :param stac_file: File to validate
        :type stac_file: str
        :param extension: extension to validate against
        :type extension: str, optional
        :param version: STAC version to validate against
        :type version: str, optional
        :param log_level: Level of logging to report, defaults to "CRITICAL"
        :type log_level: str, optional
        :param update: bool, optional
        :type upate: bool
        :param force: bool, optional
        :type force: bool
        :param recursive: bool, optional
        :type recursive: bool
        :param core: bool, optional
        :type core: bool
        :param legacy: bool, optional
        :type legacy: bool
        :param custom: custom schema to validate against
        :type custom: str, optional
        """
        numeric_log_level = getattr(logging, log_level.upper(), None)
        if not isinstance(numeric_log_level, int):
            raise ValueError("Invalid log level: %s" % log_level)

        logging.basicConfig(
            format="%(asctime)s : %(levelname)s : %(message)s",
            datefmt="%m/%d/%Y %H:%M:%S",
            level=numeric_log_level,
        )
        logging.info("STAC Validator Started.")
        self.stac_file = stac_file.strip()
        self.dirpath = tempfile.mkdtemp()
        self.message = []
        self.version = version
        self.update = update
        self.force = force
        self.recursive = recursive
        self.extension = extension
        self.core = core
        self.legacy = legacy
        self.custom = custom

    def fix_version(self, version: str ) -> str:
        """remove v from stac_version field
        add a 'v' to the front of the version
        :param version: STAC version
        :type str: str
        :return: updated STAC version
        :rtype: str
        """
        if version[0] in ['m','d','v']:
            version = version[1:]
        return version

    def fix_stac_missing(self, stac_content: dict) -> dict:
        """add stac_version='0.9.0' and/ or a temporary id field if it's missing
        :param stac_content: STAC object
        :type str: dict
        :return: updated STAC content
        :rtype: Dict
        """
        # add stac version field if there isn't one
        if not 'stac_version' in stac_content:
            stac_content['stac_version'] = '0.9.0'
        if(stac_content['stac_version'] != '0.9.0'):
            stac_content['stac_version'] = '0.9.0'

        # add id field if there isn't one
        if not 'id' in stac_content:
            stac_content['id'] = 'temporary'

        return stac_content

    def get_stac_version(self, stac_content: dict) -> str:
        """Identify the STAC object type

        :param stac_content: STAC content dictionary
        :type stac_content: dict
        :return: STAC object type
        :rtype: str
        """
        stac_object = identify_stac_object(stac_content)
        return stac_object.version_range.max_version

    def get_stac_type(self, stac_content: dict) -> str:
        """Identify the STAC object type

        :param stac_content: STAC content dictionary
        :type stac_content: dict
        :return: STAC object type
        :rtype: str
        """
        stac_object = identify_stac_object(stac_content)
        return stac_object.object_type.lower()

    @staticmethod
    def is_valid_url(url: str) -> bool:
        """Check if path is URL or not.

        :param url: Path to check
        :return: Boolean
        """
        try:
            result = urlparse(url)
            if result.scheme in ("http", "https"):
                return True
            else:
                return False
        except Exception as e:
            return False

    @staticmethod
    def create_err_msg(err_type: str, err_msg: str) -> dict:
        """Format error message dictionary

        :param err_type: Error type
        :type err_type: str
        :param err_msg: Error message
        :type err_msg: str
        :return: Formatted message
        :rtype: dict
        """
        return {"valid_stac": False, "error_type": err_type, "error_message": err_msg}

    def fetch_and_parse_file(self, input_path: str) -> Tuple[dict, dict]:
        """Fetch and parse STAC file

        :param input_path: Path to STAC file
        :type str: str
        :return: STAC content and error message, if necessary
        :rtype: Tuple[dict, dict]
        """

        err_message = {}
        data = None

        try:
            if self.is_valid_url(input_path):
                logger.info("Loading STAC from URL")
                resp = requests.get(input_path)
                data = resp.json()
            else:
                with open(input_path) as f:
                    logger.info("Loading STAC from filesystem")
                    data = json.load(f)

        except JSONDecodeError as e:
            logger.exception("JSON Decode Error")
            err_message = self.create_err_msg("InvalidJSON", f"{input_path} is not Valid JSON")

        except FileNotFoundError as e:
            logger.exception("STAC File Not Found")
            err_message = self.create_err_msg("FileNotFoundError", f"{input_path} cannot be found")

        return data, err_message

    def migrate(self, stac_content) -> dict:
        """Migrate STAC to newest version 1.0.0-beta.2

        :param stac_content: STAC object
        :type str: dict
        :return: updated STAC content
        :rtype: Dict
        """

        identify = pystac.serialization.identify_stac_object(stac_content)
        stac_content = pystac.serialization.migrate.migrate_to_latest(stac_content, identify)
        self.version = self.fix_version(stac_content[0]['stac_version'])

        return stac_content[0]

    # http://code.activestate.com/recipes/576644-diff-two-dictionaries/
    def diff(self, d1, d2, NO_KEY='<KEYNOTFOUND>'):
        """Output difference between two dictionaries

        :param d1, d2: dictionaries
        :type str: dict
        :return: dict representing the difference
        :rtype: str: dict
        """
        both = d1.keys() & d2.keys()
        diff = {k:(d1[k], d2[k]) for k in both if d1[k] != d2[k]}
        diff.update({k:(d1[k], NO_KEY) for k in d1.keys() - both})
        diff.update({k:(NO_KEY, d2[k]) for k in d2.keys() - both})
        return diff

    def validate_legacy(self, stac_content):
        """Validate legacy schemas

        :param stac_content: STAC object
        :type str: dict
        :return: string representing schema location
        :rtype: str
        """
        root_schema = 'https://cdn.staclint.com/'

        if self.version[0] not in ['v']:
            self.version = 'v' + self.version
        valid_versions = ['v0.4.0','v0.4.1','v0.5.0','v0.5.1','v0.5.2','v0.6.0', 'v0.6.0-rc1',
            'v0.6.0-rc2','v0.6.1','v0.6.2','v0.7.0','v0.8.0','v0.8.0-rc1','v0.8.1','v0.9.0',
            'v0.9.0-rc1','v0.9.0-rc2','v1.0.0-beta.1']
        if self.version in valid_versions and self.stac_type in ['item', 'collection', 'catalog']:
            schema_example, _ = self.fetch_and_parse_file(root_schema + f'{self.version}/{self.stac_type}.json')
            jsonschema.validate(stac_content, schema_example)
        if self.version not in valid_versions:
            raise LegacyVersionException
        return(root_schema + f'{self.version}/{self.stac_type}.json')

    def run(self):

        """
        Entry point.
        :return: message json
        """

        message = {"path": self.stac_file}

        stac_content, err_message = self.fetch_and_parse_file(self.stac_file)

        if err_message:
            message.update(err_message)
            self.message = [message]
            return json.dumps(self.message)

        self.stac_type = self.get_stac_type(stac_content)

        message["asset_type"] = self.stac_type

        try:
            if(self.custom):
                schema, _ = self.fetch_and_parse_file(self.custom)
                jsonschema.validate(stac_content, schema)
                self.message.append(message)
                message['schema'] = self.custom
                message["custom"] = True
                message["valid_stac"] = True
                return json.dumps(self.message)

            if(self.legacy):
                schema = self.validate_legacy(stac_content)
                self.message.append(message)
                message['schema'] = schema
                message["legacy"] = True
                message['validated_version'] = self.version
                message["valid_stac"] = True
                return json.dumps(self.message)

            if(self.force):
                if 'stac_version' in stac_content:
                        self.version = stac_content['stac_version']
                message["original_version"] = self.version
                message["force"] = True
                stac_content = self.fix_stac_missing(stac_content)
                self.version = stac_content['stac_version']

            message["id"] = stac_content["id"]

            if(self.version!='missing'):
                self.version = self.fix_version(self.version)
            else:
                self.version = self.fix_version(stac_content['stac_version'])

            if(self.update):
                message["original_verson"] = self.version
                message["update"] = True
                stac_content_migrated = self.migrate(stac_content)

                diffy = self.diff(stac_content, stac_content_migrated)
                message["diff"] = diffy

                stac_content = stac_content_migrated

            message['validated_version'] = self.version

            if(self.legacy == False):
                version_list = ['0.8.0', '0.8.1', '0.9.0', '1.0.0-beta.2']
                if self.version not in version_list:
                    raise VersionException

            extension_list = ['checksum', 'collection-assets', 'datacube', 'eo', 'item-assets', 'label', 'pointcloud',
                'projection', 'sar', 'sat', 'scientific', 'single-file-stac', 'tiled-assets', 'timestamps', 'version', 'view']
            if(self.extension):
                if self.extension not in extension_list:
                    raise ExtensionException

            if(self.recursive):
                message["recursive"] = True
                rootlink = stac_content["links"][0]["href"]
                pystac.validation.validate_all(stac_content, rootlink)
            elif(self.extension):
                message["extension_flag"] = self.extension
                stacschema = pystac.validation.JsonSchemaSTACValidator()
                self.stac_type = self.stac_type.upper()
                stacschema.validate_extension(stac_dict=stac_content, stac_object_type=self.stac_type, stac_version=self.version, extension_id=self.extension)
            elif(self.core):
                message["core"] = True
                stacschema = pystac.validation.JsonSchemaSTACValidator()
                self.stac_type = self.stac_type.upper()
                stacschema.validate_core(stac_dict=stac_content, stac_object_type=self.stac_type, stac_version=self.version)
            else:
                root_schema = 'https://schemas.stacspec.org/v1.0.0-beta.2/'
                if self.version == '1.0.0-beta.2' and self.stac_type in ['item', 'collection', 'catalog']:
                    schema_example, _ = self.fetch_and_parse_file(root_schema + f'{self.stac_type}-spec/json-schema/{self.stac_type}.json')
                    jsonschema.validate(stac_content, schema_example)
                pystac.validation.validate_dict(stac_content, stac_version=self.version)

            message["valid_stac"] = True

        except KeyError as e:
            err_msg = ("Key Error: " + str(e))
            message["valid_stac"] = False
            message.update(self.create_err_msg("KeyError", err_msg))
        except VersionException as e:
            err_msg = ("Version Not Valid (try --update): " + self.version)
            message["valid_stac"] = False
            message.update(self.create_err_msg("VersionError", err_msg))
        except LegacyVersionException as e:
            err_msg = ("Version Not Valid (set --version): " + self.version)
            message["valid_stac"] = False
            message.update(self.create_err_msg("LegacyVersionError", err_msg))
        except ExtensionException as e:
            err_msg = ("Extension Not Valid: " + self.extension)
            message["valid_stac"] = False
            message.update(self.create_err_msg("ExtensionError", err_msg))
        except HTTPError as e:
            err_msg = (str(e) + " (Possible cause, can't find schema, try --update)")
            message["valid_stac"] = False
            message.update(self.create_err_msg("HTTP", err_msg))
        except RefResolutionError as e:
            err_msg = ("JSON Reference Resolution Error.")
            message["valid_stac"] = False
            message.update(self.create_err_msg("RefResolutionError", err_msg))
        except ValidationError as e:
            if e.absolute_path:
                err_msg = (
                    f"{e.message}. Error is in {' -> '.join([str(i) for i in e.absolute_path])}"
                )
            else:
                err_msg = f"{e.message} of the root of the STAC object"
            message.update(self.create_err_msg("ValidationError", err_msg))
        except pystac.validation.STACValidationError as e:
            err_msg = ("STAC Validation Error: " + str(e))
            message["valid_stac"] = False
            message.update(self.create_err_msg("STACValidationError", err_msg))

        self.message.append(message)

        return json.dumps(self.message)

def main():

    args = docopt(__doc__)
    stac_file = args.get("<stac_file>")
    version = args.get("--version")
    timer = args.get("--timer")
    log_level = args.get("--log_level", "DEBUG")
    update = args.get("--update")
    force = args.get("--force")
    recursive = args.get("--recursive")
    extension = args.get("--extension")
    core = args.get("--core")
    legacy = args.get("--legacy")
    custom = args.get("--custom")

    if timer:
        start = default_timer()

    stac = StacValidate(stac_file, extension, version, log_level, update, force, recursive, core, legacy, custom)

    _ = stac.run()
    shutil.rmtree(stac.dirpath)

    print(json.dumps(stac.message, indent=4))

    if timer:
        print(f"Validator took {default_timer() - start:.2f} seconds")

if __name__ == "__main__":
    main()


# env /bin/python
# Kotsur Denis

def max_pyr_sum(pyramid):
    max_sum = list(pyramid[-1])
    for row in range(len(pyramid) - 2, -1, -1):
        for index, value in enumerate(pyramid[row]):
            max_sum[index] = value + max(max_sum[index], max_sum[index + 1])
    return max_sum[0]

def main():
    pyramid = (
        (1,),
        (2, 1),
        (1, 2, 1),
        (1, 2, 1, 1),
        (1, 2, 1, 1, 1),
        (1, 2, 1, 1, 1, 1),
        (1, 2, 1, 1, 1, 9, 1)
    )
    result = max_pyr_sum(pyramid)
    print("Max sum is %s" % result)

if __name__ == '__main__':
    main()


# --------------------------------------------------------
# RuleBase Diagnosis using Temperature
# Written by SeonWoo Lee
# --------------------------------------------------------

import xml.etree.ElementTree as ET
import os
import _pickle as cPickle
import numpy as np
from analyze_image import DiagnosisRule
import cv2
import json
from tqdm import tqdm
def parse_rec(filename):
    """ Parse a PASCAL VOC xml file """
    tree = ET.parse(filename)
    objects = []
    for obj in tree.findall('object'):
        obj_struct = {}
        obj_struct['name'] = obj.find('name').text
        obj_struct['pose'] = obj.find('pose').text
        obj_struct['truncated'] = int(obj.find('truncated').text)
        obj_struct['difficult'] = int(obj.find('difficult').text)
        bbox = obj.find('bndbox')
        obj_struct['bbox'] = [int(bbox.find('xmin').text),
                            int(bbox.find('ymin').text),
                            int(bbox.find('xmax').text),
                            int(bbox.find('ymax').text)]
        objects.append(obj_struct)
    return objects
    '''
        온도 범위
        ITC, NAVY, NETA, NMAC의 온도범위에 따른 대응활동과
        대응활동의 권장사항에 대하여 나열하고자 함
        |Diagnosis-------- |ITC--|NAVY--|NETA|NMAC--|Recommanded|
        |Advisory(주의)----|<5C--|10~24C|1~3C|0.3~8C|예방정비 시 점검
        |Intermediate(중급)|N/A--|25~39C|4~15|9~28C-|우선정비(기회가 되는대로)
        |Serious(심각)-----|30~80C|40~69C|N/A-|29~56C|가능한 한 신속 정비작업
        |Immediate(긴급조치)|>80C-|>70---|>15C|>56C--|긴급정비
        전기설비에 대한 시각도 평가 기준
    '''

def rulebase_temp(ary): #입력: 관심영역
    TEMP_LIST=[]
    itc_factor = 0.4   #ITC Impact Factor
    navy_factor = 0.3 #NAVY Impact Factor
    neta_factor = 0.2 #NETA Impact Factor
    nmac_factor = 0.1 #NMAC Impact Factor
    ITC_Adversory = np.count_nonzero(np.where(ary<5.0, ary, 0))*itc_factor
    TEMP_LIST.append(('ITC_Adversory',ITC_Adversory))
    ITC_Serious = np.count_nonzero(np.where((ary<=30.0) & (ary>=5.0), ary, 0))*itc_factor
    TEMP_LIST.append(('ITC_Serious', ITC_Serious))
    ITC_Immediate = np.count_nonzero(np.where(ary>80, ary, 0))*itc_factor
    TEMP_LIST.append(('ITC_Immediate', ITC_Immediate))

    NAVY_Adversory = np.count_nonzero(np.where((ary>=10.0) & (ary<=24.0), ary, 0))*navy_factor
    TEMP_LIST.append(('NAVY_Adversory', NAVY_Adversory))
    NAVY_Intermediate = np.count_nonzero(np.where((ary>=25.0) & (ary<40.0), ary, 0))*navy_factor
    TEMP_LIST.append(('NAVY_Intermediate', NAVY_Intermediate))
    NAVY_Serious = np.count_nonzero(np.where((ary>=40.0) & (ary< 70.0), ary, 0))*navy_factor
    TEMP_LIST.append(('NAVY_Serious', NAVY_Serious))
    NAVY_Immediate = np.count_nonzero(np.where(ary>=70.0, ary, 0))*navy_factor
    TEMP_LIST.append(('NAVY_Immediate', NAVY_Immediate))

    NETA_Adversory = np.count_nonzero(np.where((ary>=1) & (ary<4), ary, 0))*neta_factor
    TEMP_LIST.append(('NETA_Adversory', NETA_Adversory))
    NETA_Intermediate = np.count_nonzero(np.where((ary>=9) & (ary<15.0), ary, 0))*neta_factor
    TEMP_LIST.append(('NETA_Intermediate', NETA_Intermediate))
    NETA_Immediate = np.count_nonzero(np.where(ary>=15.0, ary, 0))*neta_factor
    TEMP_LIST.append(('NETA_Immediate', NETA_Immediate))

    NMAC_Adversory = np.count_nonzero(np.where((ary>=0.3) & (ary<9.0), ary, 0))*nmac_factor
    TEMP_LIST.append(('NMAC_Adversory', NMAC_Adversory))
    NMAC_Intermediate = np.count_nonzero(np.where((ary>=9) & (ary<28.0), ary, 0))*nmac_factor
    TEMP_LIST.append(('NMAC_Intermediate', NMAC_Intermediate))
    NMAC_Serious = np.count_nonzero(np.where((ary>=29.0 )& (ary<56.0), ary, 0))*nmac_factor
    TEMP_LIST.append(('NMAC_Serious', NMAC_Serious))
    NMAC_Immediate = np.count_nonzero(np.where(ary>=56.0, ary, 0))*nmac_factor
    TEMP_LIST.append(('NMAC_Immediate', NMAC_Immediate))

    TEMP_LIST.sort(key=lambda t:t[1], reverse=True)
    result= {'Adversory':0,'Intermediate':0,'Serious': 0, 'Immediate':0}
    top3 = TEMP_LIST[:3]
    for top in top3:
        if top[0].find('Adversory') != -1:
            result['Adversory']+=top[1]
        elif top[0].find('Intermediate') !=-1:
            result['Intermediate'] +=top[1]
        elif top[0].find('Serious')!=-1:
            result['Serious'] +=top[1]
        elif top[0].find('Immediate')!=-1:
            result['Immediate'] +=top[1]

    res = list(result.items())
    res.sort(key=lambda t:t[1], reverse=True)
    return res

def rule_base_analysis(filename, obj_list):
    if not filename.endswith('csv'):
        print('{} is not csv file format'.format(filename))
        exit()
    csv_data = np.loadtxt(filename, dtype=float, delimiter=',')
    '''
    data_json= "./data/diagnosis_rule.json"
    if not os.path.exists(data_json):
        print("Json File {} is not Exit!".format(data_json))
        exit()
    rule = DiagnosisRule(data_json)
    '''
    json_list = []
    for obj in obj_list:
        json_data = {}
        object_class = obj['name']
        limit_table={'Motor': 125,
                            'ESWP_B':125,
                            'CB': 60,
                            'Pt_1': 90,
                            'E': 100,
                            'BTCG_TR':85,
                            'BTCG_BUSBAR':100,
                            'TRDR':100,
                            'RT_TR':100,
                            'FUSE':80}
        json_data['filename'] = filename
        json_data['object_name'] = object_class
        json_data["xmin"] = obj['bbox'][0]
        json_data["ymin"] = obj['bbox'][1]
        json_data["xmax"] = obj['bbox'][2]
        json_data["ymax"] = obj['bbox'][3]

        roi = csv_data[json_data['ymin']:json_data['ymax'],\
                                json_data['xmin']:json_data['xmax']]
        json_data["tmax"] = np.max(roi)
        json_data["tmin"] = np.min(roi)
        json_data["tmean"] = np.average(roi)
        json_data["class"] = object_class
        try:
            json_data['limit_temp'] = limit_table[object_class]
            # print(object_class)
            # print(obj['bbox'])
            json_data['diagnosis'] = rulebase_temp(roi - limit_table[object_class])
        except:
            print("{} cannot have Limit Class".format(object_class))

        # roi2 = roi.astype(dtype=np.uint8)
        # roi2 = cv2.cvtColor(roi2, cv2.COLOR_GRAY2BGR)
        # json_list.append(json_data)
        # cv2.imshow('test', roi2)
        # cv2.waitKey(0)
        # exit()
        json_list.append(json_data)
    return json_list

if __name__ == '__main__':
    #xml_path = os.path.join('..','data','VOCdevkit','20_FirsQuarter_readymade_data','wulsung3','annotation')
    xml_path = "C:\\Users\\hci-lab01\\Desktop\\새 폴더\\2335-811-E-TR03M(MAIN XFMR) (우선 구현)\\T660\\Annotations"
    #csv_path = os.path.join('..','data','VOCdevkit','20_FirsQuarter_readymade_data','wulsung3', 'csv')
    csv_path = 'C:\\Users\\hci-lab01\\Desktop\\새 폴더\\2335-811-E-TR03M(MAIN XFMR) (우선 구현)\\T660\\Csvs'
    xml_lists = os.listdir(xml_path)
    csv_lists = os.listdir(csv_path)
    json_list = []
    for xml_name, csv_name in tqdm(list(zip(xml_lists, csv_lists))):
        xml = os.path.join(xml_path, xml_name)
        objects = parse_rec(xml)

        csv = os.path.join(csv_path, csv_name)
        _json = rule_base_analysis(filename=csv, obj_list=objects)
        json_list.append(_json)
    #with open('./output/result_json.json', 'w') as f:
    with open('C:\\Users\\hci-lab01\\Desktop\\새 폴더\\2335-811-E-TR03M(MAIN XFMR) (우선 구현)\\T660\\Results\\result_json.json', 'w') as f:
        json.dump(json_list, f, indent=4)

    pass


from random import choice, choices, randint

def place(count=1):
    elements = [
        "battlement",
        "bridge",
        "castle",
        "cave",
        "cove",
        "crag",
        "crypt",
        "den",
        "domain",
        "dungeon",
        "gate",
        "hall",
        "haven",
        "hideout",
        "hill",
        "house",
        "inn",
        "keep",
        "labyrinth",
        "lair",
        "landing",
        "manor",
        "nest",
        "oasis",
        "pit",
        "rest",
        "river",
        "road",
        "rookery",
        "roost",
        "sanctum",
        "shrine",
        "star",
        "temple",
        "throne",
        "tomb",
        "tower",
        "valley",
        "vault",
        "wood",
    ]
    if count > 1:
        return choices(elements, k=count)
    else:
        return choice(elements)


#
# Fashion MNIST Klassifikation mit TensorFlow
#
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

from sklearn.utils import shuffle
from tensorflow.examples.tutorials.mnist import input_data

tf.logging.set_verbosity(tf.logging.DEBUG)
# tf.logging.set_verbosity(tf.logging.INFO)

# Fashion Klassen
fashion_class_labels = {
    0: "T-shirt/top",
    1: "Trouser",
    2: "Pullover",
    3: "Dress",
    4: "Coat",
    5: "Sandal",
    6: "Shirt",
    7: "Sneaker",
    8: "Bag",
    9: "Ankle boot"
}

# Mit dem Parameter one_hot
# wird eine Zahl als Array mit 10 Elementen transformiert z.B. 8 wird als
# [0 0 0 0 0 0 1 0] kodiert

# Lädt die labels und Daten
def load_fashion_data():
    data = input_data.read_data_sets('data/fashion', one_hot=True)

    # Trainings daten
    train_data = data.train.images
    train_labels = data.train.labels

    # Evaluations daten
    eval_data = data.test.images
    eval_labels = data.test.labels
    # Wir benutzen die shuffle-Funktion, damit die Daten durchgemischt werden
    # und zufällig ausgewählt werden.

    eval_data, eval_labels = shuffle(eval_data, eval_labels)
    return (train_data, train_labels, eval_data, eval_labels)

# Definition des Modells
def fashion_model(weights, bias, X):
    return tf.matmul(X, weights) + bias

train_data, train_labels, eval_data, eval_labels = load_fashion_data()

# Variablen
dimension = 784  # 28x28 Pixels
number_of_classes = 10  # die Elemente in fashion_class_labels

# Eingabe (Pixels)
X = tf.placeholder(tf.float32, [None, dimension], name="X")

# Ausgabe (Klasse bzw. Kategorie)
Y = tf.placeholder(tf.float32, [None, number_of_classes], name="Y")

# Gewichtungen
w = tf.Variable(tf.random_normal([dimension, number_of_classes], stddev=0.01), name="weights", dtype=tf.float32)

# Bias
b = tf.Variable(tf.random_normal([number_of_classes]), name="bias", dtype=tf.float32)

predict_input = tf.placeholder(tf.float32, [None, dimension], name="predict_input")

# Aufruf des Modells
model_out = fashion_model(w, b, X)

# Optionale Ausgabe eines Bildes aus dem Fashion MNIST
input_picture = eval_data[0].reshape(28, 28)
input_label = fashion_class_labels[np.argmax(eval_labels[0], axis=None, out=None)]
print("Ausgewählt: {}".format(input_label))
plt.title(input_label)
plt.imshow(input_picture, cmap='Greys')

# Auskommentieren
plt.show()

# Definition der Kosten/Loss-Funktion
#
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=model_out, labels=Y))
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

epochs = range(0, 1000)

# Trainingsschleife
with tf.Session() as sess:
    saver = tf.train.Saver()
    sess.run(tf.global_variables_initializer())
    for i in epochs:
        sess.run(train_op, feed_dict={X: train_data, Y: train_labels})
        loss = sess.run(cost, feed_dict={X: train_data, Y: train_labels})
        accuracy = np.mean(
            np.argmax(sess.run(model_out, feed_dict={X: train_data, Y: train_labels}), axis=1) == np.argmax(
                train_labels, axis=1))

        if (i % 10 == 0):
            print("Epoch {} // Accuracy: {} Loss:{}".format(i, accuracy, loss))

    # Ausgabe der Vorhersage für das erste Element von eval_data
    predictions = sess.run(model_out, feed_dict={X: [eval_data[0]]})
    index = int(np.argmax(predictions, axis=1))
    print("Gefundene Fashion Kategorie: {}".format(fashion_class_labels[index]))

    saver.save(sess, "./fashion_model")
    print("Modell wurde gespeichert")


import numpy as np
import requests
import pandas as pd
import datetime

def calcSortino(returns, rMar):
    ner = []
    er = []

    for i in range(len(returns)):
        er.append(returns[i] - rMar)
        if((returns[i] - rMar) < 0):
            ner.append((returns[i] - rMar)**2)
        else:
            ner.append(0)

    downRisk = np.sqrt(sum(ner)/len(returns))
    meanReturn = np.mean(er)
    return meanReturn/downRisk

def calcSharpe(dailyReturn, annualBenchReturn):
    rMar = annualBenchReturn
    exDailyRet = [r - rMar for r in dailyReturn]
    sharpe = np.mean(exDailyRet) / np.std(exDailyRet, ddof=1)
    return sharpe

def getHistPriceData(coinTo, ccFrom, limit, exchange):
    curl = 'https://min-api.cryptocompare.com/data/histoday?fsym={}&tsym={}&limit={}&e={}'\
            .format(coinTo.upper(), ccFrom.upper(), limit, exchange)
    data = requests.get(curl, headers={'User-Agent': 'Mozilla/5.0'}).json()['Data']
    df = pd.DataFrame(data)
    df['date'] = [datetime.datetime.fromtimestamp(d).strftime('%Y-%m-%d') for d in df.time]
    return df


# This file contains utility functions used elswhere in the code
import numpy as np
import pyro.distributions as dist
from pyro.distributions import TorchDistribution
from pyro.distributions.torch import MultivariateNormal
import torch
from numbers import Number

def make_grid_adjacency_matrix(rows, cols):
    """Function taken from:
    https://stackoverflow.com/questions/16329403/
    how-can-you-make-an-adjacency-matrix-which-would-emulate-a-2d-grid

    Returns the adjacency matrix representing a grid with rows and cols in args
    Notes: The matrix is unfolded among rows"""
    n = rows * cols
    M = np.zeros((n, n))
    for r in range(rows):
        for c in range(cols):
            i = r * cols + c
            # Two inner diagonals
            if c > 0:
                M[i - 1, i] = M[i, i - 1] = 1.0
            # Two outer diagonals
            if r > 0:
                M[i - cols, i] = M[i, i - cols] = 1.0
    return M

def make_sparse_representation_matrix(rows, cols):
    """
    Returns sparse representation of the adjacency matrix of a grid.
    In particular, returns two arrays indicated the nonzero indices
    """

    D = make_grid_adjacency_matrix(rows, cols)
    A1, A2 = np.nonzero(D)

    # Removes duplicates
    subset = A1 < A2
    A1 = np.expand_dims(A1[subset], 0)
    A2 = np.expand_dims(A2[subset], 0)
    return np.concatenate((A1, A2), 0)

class FastCar(TorchDistribution):
    r"""Fast implementation of the the distribution for a CAR model.
    Reference for full implementation can be found in
    https://mc-stan.org/users/documentation/case-studies/mbjoseph-CARStan.html
    Alpha,tau,n should be integers
    eigne, Dpsarse and should be tensors with dtype float"""

    support = MultivariateNormal.support

    def __init__(self, alpha, tau, n, eigen, D_sparse, W_sparse):
        self.alpha = alpha
        self.tau = tau
        self.n = n
        self.eigen = eigen
        self.D_sparse = D_sparse
        self.W_sparse = W_sparse
        self.W_n = len(self.W_sparse[0])
        self.partial_sum = self.n * torch.log(self.tau) + torch.sum(
            torch.log(1 - self.alpha * self.eigen)
        )
        self._normal = dist.MultivariateNormal(torch.zeros(n), torch.eye(n))

        batch_shape = (
            torch.Size() if isinstance(alpha, Number) else self.alpha.size()
        )
        event_shape = torch.Size([n])
        super().__init__(batch_shape, event_shape)

    def sample(self, sample_shape=()):
        return self._normal.sample()

    def log_prob(self, phi):
        phit_D = phi * self.D_sparse
        phit_W = torch.zeros(self.n, dtype=phit_D.dtype)
        phit_W[self.W_sparse[0]] = (
            phit_W[self.W_sparse[0]] + phi[self.W_sparse[1]]
        )
        phit_W[self.W_sparse[1]] = (
            phit_W[self.W_sparse[1]] + phi[self.W_sparse[0]]
        )
        result = (1 / 2) * (
            self.partial_sum
            + self.tau
            * (torch.sum(phit_D * phi) - self.alpha * torch.sum(phit_W * phi))
        )
        return result

# alpha = torch.tensor(0.1)
# tau = torch.tensor(2)
# rows = 20
# cols = 20
# W = torch.Tensor(make_grid_adjacency_matrix(rows, cols))
# W_sparse = make_sparse_representation_matrix(rows,cols)
# D_sparse = torch.sum(W,1)
# D_invsq = torch.diag(1/torch.sqrt(D_sparse))
# D = torch.diag(torch.sum(W, 1))
# B = D_invsq @ W @ D_invsq
# eigenvalues, _ = torch.eig(B)
# eigen = eigenvalues[:,0]
# d = FastCar(alpha, tau, 20 * 20, eigen, D_sparse, W_sparse)
# a = dist.MultivariateNormal(torch.zeros(400), torch.eye(400)).sample()