import tensorflow as tf
from sklearn.metrics import confusion_matrix
import numpy as np
import load

train_samples, train_labels = load._train_samples, load._train_labels
test_samples, test_labels = load._test_samples, load._test_labels

print('Training set', train_samples.shape, train_labels.shape)
print('    Test set', test_samples.shape, test_labels.shape)

image_size = load.image_size
num_labels = load.num_labels
num_channels = load.num_channels

def get_chunk(samples, labels, chunkSize):
    """
    Iterator/Generator: get a batch of data
    这个函数是一个迭代器/生成器，用于每一次只得到 chunkSize 这么多的数据
    用于 for loop， just like range() function
    """
    if len(samples) != len(labels):
        raise Exception('Length of samples and labels must equal')
    stepStart = 0  # initial step
    i = 0
    while stepStart < len(samples):
        stepEnd = stepStart + chunkSize
        if stepEnd < len(samples):
            yield i, samples[stepStart:stepEnd], labels[stepStart:stepEnd]
            i += 1
        stepStart = stepEnd

class Network():
    def __init__(self, num_hidden, batch_size):
        """
        @num_hidden: 隐藏层的节点数量
        @batch_size：因为我们要节省内存，所以分批处理数据。每一批的数据量。
        """
        self.batch_size = batch_size
        self.test_batch_size = 500

        # Hyper Parameters
        self.num_hidden = num_hidden

        # Graph Related
        self.graph = tf.Graph()
        self.tf_train_samples = None
        self.tf_train_labels = None
        self.tf_test_samples = None
        self.tf_test_labels = None
        self.tf_test_prediction = None

    def define_graph(self):
        """
        定义我的的计算图谱
        """
        with self.graph.as_default():
            # 这里只是定义图谱中的各种变量
            self.tf_train_samples = tf.placeholder(
                tf.float32, shape=(self.batch_size, image_size, image_size, num_channels)
            )
            self.tf_train_labels = tf.placeholder(
                tf.float32, shape=(self.batch_size, num_labels)
            )
            self.tf_test_samples = tf.placeholder(
                tf.float32, shape=(self.test_batch_size, image_size, image_size, num_channels)
            )

            # fully connected layer 1, fully connected
            fc1_weights = tf.Variable(
                tf.truncated_normal([image_size * image_size * num_channels, self.num_hidden], stddev=0.1)
            )
            fc1_biases = tf.Variable(tf.constant(0.1, shape=[self.num_hidden]))

            # fully connected layer 2 --> output layer
            fc2_weights = tf.Variable(
                tf.truncated_normal([self.num_hidden, num_labels], stddev=0.1)
            )
            fc2_biases = tf.Variable(tf.constant(0.1, shape=[num_labels]))

            # 想在来定义图谱的运算
            def model(data):
                # fully connected layer 1
                shape = data.get_shape().as_list()
                print(data.get_shape(), shape)
                reshape = tf.reshape(data, [shape[0], shape[1] * shape[2] * shape[3]])
                print(reshape.get_shape(), fc1_weights.get_shape(), fc1_biases.get_shape())
                hidden = tf.nn.relu(tf.matmul(reshape, fc1_weights) + fc1_biases)

                # fully connected layer 2
                return tf.matmul(hidden, fc2_weights) + fc2_biases

            # Training computation.
            logits = model(self.tf_train_samples)
            self.loss = tf.reduce_mean(
                tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=self.tf_train_labels)
            )
            # print(self.loss)

            # Optimizer.
            self.optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(self.loss)

            # Predictions for the training, validation, and test data.
            self.train_prediction = tf.nn.softmax(logits)
            self.test_prediction = tf.nn.softmax(model(self.tf_test_samples))

    def run(self):
        """
        用到Session
        """

        # private function
        def print_confusion_matrix(confusionMatrix):
            print('Confusion    Matrix:')
            for i, line in enumerate(confusionMatrix):
                print(line, line[i] / np.sum(line))
            a = 0
            for i, column in enumerate(np.transpose(confusionMatrix, (1, 0))):
                a += (column[i] / np.sum(column)) * (np.sum(column) / 26000)
                print(column[i] / np.sum(column), )
            print('\n', np.sum(confusionMatrix), a)

        self.session = tf.Session(graph=self.graph)
        with self.session as session:
            tf.global_variables_initializer().run()

            ### 训练
            print('Start Training')
            # batch 1000
            for i, samples, labels in get_chunk(train_samples, train_labels, chunkSize=self.batch_size):
                _, l, predictions = session.run(
                    [self.optimizer, self.loss, self.train_prediction],
                    feed_dict={self.tf_train_samples: samples, self.tf_train_labels: labels}
                )
                print("loss is :", l)
                # labels is True Labels
                accuracy, _ = self.accuracy(predictions, labels)
                if i % 50 == 0:
                    print('Minibatch loss at step %d: %f' % (i, l))
                    print('Minibatch accuracy: %.1f%%' % accuracy)
            ###

            ### 测试
            print('Start Testing')
            accuracies = []
            confusionMatrices = []
            for i, samples, labels in get_chunk(test_samples, test_labels, chunkSize=self.test_batch_size):
                result = self.test_prediction.eval(feed_dict={self.tf_test_samples: samples})
                accuracy, cm = self.accuracy(result, labels, need_confusion_matrix=True)
                accuracies.append(accuracy)
                confusionMatrices.append(cm)
                print('Test Accuracy: %.1f%%' % accuracy)
            print(' Average  Accuracy:', np.average(accuracies))
            print('Standard Deviation:', np.std(accuracies))
            print_confusion_matrix(np.add.reduce(confusionMatrices))
        ###

    def accuracy(self, predictions, labels, need_confusion_matrix=False):
        """
        计算预测的正确率与召回率
        @return: accuracy and confusionMatrix as a tuple
        """
        _predictions = np.argmax(predictions, 1)
        _labels = np.argmax(labels, 1)
        cm = confusion_matrix(_labels, _predictions) if need_confusion_matrix else None
        # == is overloaded for numpy array
        accuracy = (100.0 * np.sum(_predictions == _labels) / predictions.shape[0])
        return accuracy, cm

if __name__ == '__main__':
    net = Network(num_hidden=128, batch_size=100)
    net.define_graph()
    net.run()


import wave
w1=wave.open("noise.wav",'rb')
w2=wave.open("noise2.wav",'rb')
print(w1.getparams())
print(w2.getparams())
for i in range(10000):
    a=w1.readframes(1)
    a2=w2.readframes(1)
    print((a,a2))
w1.close()
w2.close()


import keras.backend as K
import numpy as np
import collections
from nltk.corpus import sentiwordnet as swn
#from keras.preprocessing.text import Tokenizer
#from keras.preprocessing.sequence import pad_sequences
#from __future__ import absolute_import
#from __future__ import division
#from __future__ import print_function
#import collections
#import math
#import os
#import random
#from tempfile import gettempdir
#import zipfile
#from six.moves import urllib
#from six.moves import xrange
#import tensorflow as tf

GLOVE_DIR='C:/Users/Dell/Documents/MS/Codes/irony_detection/RNNs/keras-attention-mechanism-master/'
inputs_file='C:/Users/Dell/Documents/MS/Codes/irony_detection/RNNs/keras-attention-mechanism-master/SE18-taskA_inputs.txt'
labels_file='C:/Users/Dell/Documents/MS/Codes/irony_detection/RNNs/keras-attention-mechanism-master/SE18-taskA_labels.txt'
embeddings_index = {}
vocab_size=15970
input_dim = 25       # should be changed to 300 or whatever is the word embedding size
time_steps = 20

def build_dataset(words, n_words):
  """Process raw inputs into a dataset."""
  count = [['UNK', -1]]
  count.extend(collections.Counter(words).most_common(n_words - 1))
  dictionary = dict()
  for word, _ in count:
    dictionary[word] = len(dictionary)
  unk_count = 0
  for word in words:
    index = dictionary.get(word, 0)
    if index == 0:  # dictionary['UNK']
      unk_count += 1
  count[0][1] = unk_count
  reversed_dictionary = dict(zip(dictionary.values(), dictionary.keys()))
  print('Dictionary length:');
  print(str(len(dictionary))+' '+str(len(reversed_dictionary)));
  return count, dictionary, reversed_dictionary

# loading Glove model
def loadGloveModel(gloveFile):
    print("Loading Glove Model")
    f = open(gloveFile,'r',encoding="utf-8")
    model = {}
    for line in f:
        splitLine = line.split()
        word = splitLine[0]
        embedding = [float(val) for val in splitLine[1:]]
        model[word] = embedding
    print("Done."+str(len(model))+"words loaded!")
    return model

time_steps=20
max_seq_len=time_steps
words=[]
with open(inputs_file,'r',encoding="utf-8") as inp:
    for line in inp:
        words.extend(line.split());
print('Total number of words: '+str(len(words)))
# data - list of codes (integers from 0 to vocabulary_size-1).
#   This is the original text but words are replaced by their codes
# count - map of words(strings) to count of occurrences
# dictionary - map of words(strings) to their codes(integers)
# reverse_dictionary - maps codes(integers) to words(strings)
count, dictionary, reverse_dictionary = build_dataset(words,vocab_size)
input_data=[]
with open(inputs_file,'r',encoding="utf-8") as inp:
    for line in inp:
        input_data.append([dictionary.get(word, 0) for word in line.split()]);
X=[]
Glove=loadGloveModel(GLOVE_DIR+'glove.twitter.27B.50d.txt');
dummy_vector=[0.0 for i in range(input_dim)];
print('Shape of dummy vector: '+str(len(dummy_vector)));
file_cnt=1;
null_cnt=0;
for i in range(len(input_data)):
    temp=[]
    sentiments=[]
    for j in range(len(input_data[i])):
        if(input_data[i][j]!=0):
            current=Glove.get(reverse_dictionary.get(input_data[i][j],'UNK'),dummy_vector);
            if(not(current==dummy_vector)):
                sentiment.append()
                temp.append(current);
    with open(GLOVE_DIR+"word_embeddings_50d/"+str(file_cnt)+".txt","w+", encoding="utf-8") as out:
        for i in range(len(temp)):
            for j in temp[i]:
                out.write(str(j));
                out.write(' ');
            out.write("\n");
    file_cnt+=1
    null_cnt+=max_seq_len-len(temp);
    for j in range(max_seq_len-len(temp)):
        temp.append(dummy_vector);
    temp=temp[:][0:max_seq_len];
    X.append(temp);
print('Total number of null vectors is: '+str(null_cnt));
for i in range(len(X)):
    assert len(X[i])==max_seq_len
Y=[]
with open(labels_file,'r',encoding="utf-8") as inp, open("labels_50d.txt","w",encoding="utf-8") as out:
    for line in inp:
        Y.append(str(line.split()[0]))
        out.write(str(line.split()[0]));
        out.write("\n");

print('checking input and output sizes')
assert len(Y)==len(X)
#x=np.asarray(X);
#y=np.asarray(Y);
#print('Shapes of inputs and outputs: ');
#print(x.shape);
#print(y.shape);
#return x, y


# -*- coding:utf-8 -*-
import argparse
import sys
sys.path.append('/Users/baby/Documents/GitHub/spartan2/')

import spartan2.basicutil as iatutil
import spartan2.drawutil as drawutil
import spartan2.ioutil as ioutil
import pandas as pd

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="")
    parser.add_argument('input', nargs='?', type=str, default=sys.stdin,
                        help="输入文件名")
    parser.add_argument("--gridsize", type=int, default=100,
                        help="Histogram的网格数，示例格式:100")
    parser.add_argument("--delimeter", type=str, default='\x01',
                        help="输入文件分割符;示例1: , 示例2: \x01")
    parser.add_argument("--timeformat", type=str, default='%Y-%m-%d %H:%M:%S',
                        help="输入数据的时间格式;示例数据%Y-%m-%d %H:%M:%S")
    parser.add_argument("--timeidx", type=int, default=0,
                        help="时间列的id(id从0开始);示例数据：0 (表示第一列)")
    parser.add_argument("--groupids", nargs='*', type=list, default=[0],
                        help=")时间列的id(id从0开始);示例数据：0 (表示第一列)")
    parser.add_argument("--xlabel", type=str, default='IATn',
                        help="Histogram横坐标的标签")
    parser.add_argument("--ylabel", type=str, default='IATn+1',
                        help="Histogram纵坐标的标签")
    parser.add_argument("--x", type=float, default=0,
                        help="Histogram查询点的横坐标")
    parser.add_argument("--y", type=float, default=0,
                        help="Histogram查询点的纵坐标")
    parser.add_argument("--radius", type=float, default=1,
                        help="Histogram的查询半径")
    parser.add_argument('-o1', "--outfig", nargs='?', type=str, default='/source/out.jpg',
                    help="图片的输出文件")
    parser.add_argument('-o2', "--outfile", type=str, default='/source/user.csv',
                    help="用户的输出文件")

    args = parser.parse_args()

    argsinput = args.input
    argsgridsize = args.gridsize
    argsdelimeter = args.delimeter
    argstimeformat = args.timeformat
    argstimeidx = args.timeidx
    argsgroupids = args.groupids
    argsxlabel = args.xlabel
    argsylabel = args.ylabel

    argsx = args.x
    argsy = args.y
    argsradius = args.radius
    argsoutfig = args.outfig
    argsoutfile = args.outfile

    aggts = ioutil.extracttimes(argsinput, outfile=None, timeidx=argstimeidx, timeformat=argstimeformat, delimeter=argsdelimeter,
                                isbyte=True, comments='#', nodetype=str, groupids=argsgroupids)
    instance = iatutil.IAT()
    instance.calaggiat(aggts)
    xs, ys = instance.getiatpairs()

    # rect: instance of class RectHistogram
    rect = drawutil.RectHistogram()
    fig, H, xedges, yedges = rect.draw(xs, ys, outfig=argsoutfig, xlabel=argsxlabel, ylabel=argsylabel)
    iatpairs = rect.find_peak_rect(xs, ys, H, xedges, yedges, x=argsx, y=argsy, radius=argsradius)
    usrlist = instance.find_iatpair_user(iatpairs)
    df = pd.DataFrame({'user':usrlist})
    df.to_csv(argsoutfile, index=False)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#file usage : python mask_tiling.py ../data/gt_data
"""
Created on Tue Apr  2 13:27:27 2019

@author: satish
"""

import sys
import os
import cv2
import math
import numpy as np
import logging
import coloredlogs

# set the logger
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("aviris_data_loader")
coloredlogs.install(level='DEBUG', logger=logger)

cur_dir = os.getcwd()
print(cur_dir)
DIRECTORY = "{}/".format(sys.argv[1])
print(DIRECTORY)
FILES = [x for x in os.listdir(DIRECTORY)]
print(FILES)

#%% creating tiles of the mask (i.e. mask.png)
import shutil

for fname in FILES:
    file_check = f'{DIRECTORY}/{fname}/{fname}_img_mask.png'
    if((os.path.isfile(file_check)) == False):
        continue
    sname = f'{fname}_img'  #spectral data
    hname = f'{sname}.hdr'  # header
    mname = f'{sname}_mask.png'  # manual annotated mask
    png_img = f'{DIRECTORY}/{fname}/{mname}'
    img_img = f'{DIRECTORY}/{fname}/{sname}'
    hdr_file = f'{DIRECTORY}/{fname}/{hname}'

    #if the mask exits then we will make tiles of the image and save in folder created "{name}_titles
    tiles_folder = f'{DIRECTORY}/{fname}/{fname}_img_mask_tiles'
    if not(os.path.isdir(tiles_folder)):
        print("something")
        os.mkdir(tiles_folder)
        print("\nDirectory", tiles_folder ," created.")
    elif os.path.isdir(tiles_folder):
        print("\nDirectory", tiles_folder ," already exists..deleting it")
        shutil.rmtree(tiles_folder)
        os.mkdir(tiles_folder)
        print("\nNew Directory", tiles_folder ," created.")
    print(png_img)
    img = cv2.imread(png_img)
    img_shape = img.shape
    print("shape of mask", img_shape)
    tile_size = (256, 256)
    offset = (128, 128)
    img_name = f'{sname}_img'

    for i in range(int(math.ceil(img_shape[0]/(offset[1] * 1.0)))):
        for j in range(int(math.ceil(img_shape[1]/(offset[0] * 1.0)))):
            cropped_img = img[offset[1]*i:min(offset[1]*i+tile_size[1], \
                              img_shape[0]), offset[0]*j:min(offset[0]*j+tile_size[0], \
                                       img_shape[1])]
            # Debugging the tiles
            tile_name = f'{img_name}_radiance'
            check_tile = f'{tile_name}_{i}_{j}.png'
            if(check_tile not in tiles_folder):
                cv2.imwrite(os.path.join(tiles_folder, (tile_name + "_" + str(i) \
                + "_" + str(j) + ".png")), cropped_img)

    print("Done Tilling mask file", mname)


import os
import boto3
import json
from boto3.dynamodb.conditions import Key, Attr
from auth0.v3.authentication.users import Users
import uuid

dynamodb = boto3.resource('dynamodb')

def new_quiz(event, context):
    table = dynamodb.Table(os.environ['DYNAMODB_TABLE_GAMES'])
    post_body = json.loads(event['body'])
    user_id = "some_user"
    x_access_token = event['headers'].get("x-access-token")
    if x_access_token is not None:
        domain = 'fuzeday-ultron.auth0.com'
        u = Users(domain=domain)
        user_info = json.loads(u.userinfo(x_access_token))
        if user_info is not None:
            user_id = user_info['name']

    game_id = uuid.uuid4()
    item = {
        'game_id': str(game_id),
        'user_id': user_id,
        'quiz': post_body['quiz']
    }

    table.put_item(Item=item)

    response = {
        "statusCode": 200,
        "body": json.dumps(item)
    }

    return response

def get_question(event, context):
    game_id = event['pathParameters']['quiz_id']
    print("Getting questions for game %s" % (game_id))

    quiz = json.load(open("questions/ultron.json"))
    table = dynamodb.Table(os.environ['DYNAMODB_TABLE_SUBMITS'])
    result = table.query(
        IndexName="game_id-index",
        KeyConditionExpression=Key('game_id').eq(game_id)
    )
    print("---------------------")
    asked_questions = map(lambda x: x["question_id"], result["Items"])

    question_to_return = None
    for q in quiz["questions"]:
        if q["id"] not in asked_questions:
            question_to_return = q
            break

    if question_to_return is not None:
        del question_to_return["answer"]

    status = "finished" if question_to_return is None else "in-progress"
    ret = {
        "status": status,
        "question": question_to_return
    }
    response = {
        "statusCode": 200,
        "body": json.dumps(ret)
    }
    return response

def submit_answer(event, context):
    question_id = event['pathParameters']['question_id']
    quiz_id = event['pathParameters']['quiz_id']
    post_body = json.loads(event['body'])
    answer_time = post_body["answerTime"]
    answer = post_body["answer"]

    state_id = str(uuid.uuid4())
    item = {
        "state_id": state_id,
        "game_id": quiz_id,
        "question_id": question_id,
        "answer": answer,
        "answerTime": answer_time
    }

    table = dynamodb.Table(os.environ['DYNAMODB_TABLE_SUBMITS'])
    table.put_item(Item=item)

    response = {
        "statusCode": 200,
        "body": json.dumps({"state": "OK"})
    }
    return response

def get_history(event, context):
    response = {
        "statusCode": 200,
        "body": json.dumps({"state": "OK"})
    }
    return response

def get_result(event, context):
    response = {
        "statusCode": 200,
        "body": json.dumps({"state": "OK"})
    }
    return response


from contextlib import contextmanager

import pytest
from biopsykit.io.eeg import load_eeg_raw_muse
from biopsykit.utils.exceptions import ValidationError

from biopsykit.example_data import _EXAMPLE_DATA_PATH

@contextmanager
def does_not_raise():
    yield

class TestIoEeg:
    @pytest.mark.parametrize(
        "file_path, expected",
        [
            ("eeg_muse_example.csv", does_not_raise()),
            ("cortisol_sample.csv", pytest.raises(ValidationError)),
        ],
    )
    def test_load_eeg_raw_muse(self, file_path, expected):
        with expected:
            load_eeg_raw_muse(_EXAMPLE_DATA_PATH.joinpath(file_path))


import os
os.environ["CUDA_VISIBLE_DEVICES"] = '1'

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

from tensorflow.contrib.learn.python.learn.experiment import Experiment

from tensorflow.contrib.learn.python.learn import learn_runner

from tensorflow.contrib.learn.python.learn.estimators.run_config import RunConfig

from collections import OrderedDict

import argparse
import os
import random
import numpy as np
import sys
from data_helpers import metrics

from data_helpers import Corpus
from data_helpers import Xingfa
from data_helpers import Fatiao

from text_cnn import TextCNN
import tensorflow as tf

parser = argparse.ArgumentParser()

from tensorflow.python.training.basic_session_run_hooks import *
from tensorflow.python.training.basic_session_run_hooks import _as_graph_element

parser.add_argument("--falv_minf",type=int,default=100)
parser.add_argument("--data_dir",'-d',default='data')
parser.add_argument("--file",'-f',default='0')
parser.add_argument("--test-size",'-t',type=int,default=8196)
parser.add_argument("--eval_step",'-e',type=int,default=1)
parser.add_argument("--mode",type=int,default=0)

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5  # 程序最多只能占用指定gpu50%的显存
config.gpu_options.allow_growth = True      #程序按需申请内存
sess = tf.Session(config = config)

args=parser.parse_args()

class Params():
    def __init__(self):
        self.data_dir = args.data_dir
        self.file = args.file
        self.max_length = 1000
        self.embedding_size=200

        self.filter_size=[3,4,5]
        self.num_filters=200
        self.dropout_keep_prob=0.5

        self.eval_per_step = 1

        self.test_size = 8196

        self.falv_minf = 100

        self.shresholds = list(np.array(range(1,10))/10)

params = Params()

print(params.shresholds)

params.test_size = args.test_size
params.eval_per_step = args.eval_step
params.falv_minf = args.falv_minf
corpus = Corpus(os.path.join(params.data_dir,params.file),params.falv_minf,mode=args.mode)

corpus.get_data_loader(train=True)
corpus.get_data_loader(train=False)

param = params
def model_fun(features,labels,mode,params):

    x = features['x']

    with tf.variable_scope("embedding",reuse=tf.AUTO_REUSE):
        W = tf.get_variable(name="W",
            shape=[len(corpus.vocaber.vocabulary_), param.embedding_size],
            initializer=tf.contrib.layers.xavier_initializer()
            )
        embedded_chars = tf.nn.embedding_lookup(W, x)
        embedded_chars_expanded  = tf.reshape(embedded_chars,shape = [-1,corpus.max_lenth,param.embedding_size,1])

    pooled_outputs = []
    for i, filter_size in enumerate(param.filter_size):
        with tf.variable_scope("conv-maxpool-%s" % filter_size,reuse=tf.AUTO_REUSE):

            h = tf.layers.conv2d(embedded_chars_expanded,filters=param.num_filters,kernel_size=[filter_size,param.embedding_size],activation=tf.nn.relu)

            pooled = tf.layers.max_pooling2d(inputs=h, pool_size=[h.get_shape().as_list()[1],1], strides=2)
            pooled_outputs.append(pooled)

    num_filters_total = param.num_filters * len(param.filter_size)
    h_pool = tf.concat(pooled_outputs, 3)
    h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total],name="pool")

    with tf.name_scope("dropout"):
        h_drop = tf.layers.dropout(h_pool_flat,rate=param.dropout_keep_prob,training= (mode == tf.estimator.ModeKeys.TRAIN))

    with tf.variable_scope("output",reuse=tf.AUTO_REUSE):
        scores = tf.layers.dense(h_drop,units=len(corpus.xingfa.fatiaos))

    logistic_scores = tf.nn.sigmoid(scores,name="logistic_scores")

    with tf.name_scope("loss"):
        labels = tf.cast(labels,dtype=tf.float32)
        losses = tf.nn.sigmoid_cross_entropy_with_logits(logits=scores, labels=labels)
        loss = tf.reduce_mean(losses)

    if(mode == tf.estimator.ModeKeys.TRAIN):

        optimizer = tf.train.AdamOptimizer()

        train_op = optimizer.minimize(loss = loss,global_step = tf.train.get_global_step())
        spec = tf.estimator.EstimatorSpec(
            mode = mode,
            loss = loss,
            train_op = train_op,
        )
        return spec

    if(mode == tf.estimator.ModeKeys.EVAL):
        a = tf.constant([[1, 1], [1, 1]])

        metric = {"oops":(a,a)}
        spec = tf.estimator.EstimatorSpec(
            mode=mode,
            loss=loss,
        )
        return spec

def evaluate(logistics_scores,lables,threshold):

    print(logistics_scores.shape)

    recalls = []
    macro_recalls = []
    micro_recalls = []
    precisions = []
    macro_precisions = []
    micro_precisions = []
    for t in threshold:
        predict = logistics_scores>t

        true_positive = predict&lables
        true_positive_count = np.sum(true_positive,axis=0)

        true_count = np.sum(lables,axis=0)
        positive_count = np.sum(predict,axis=0)

        print("true_positive_count\n",true_positive_count)
        print("true_count\n",true_count)
        print("positive_count\n",positive_count)

        recall = true_positive_count/true_count
        precision = true_positive_count/positive_count

        recalls.append(np.copy(recall))
        precisions.append(np.copy(precision))

        recall_nan_index = np.nonzero(np.isnan(recall))
        recall[recall_nan_index] = 0
        precision_nan_index = np.nonzero(np.isnan(precision))
        precision[precision_nan_index] = 0

        macro_recall = np.mean(recall)
        macro_precision = np.mean(precision)

        micro_recall = np.sum(true_positive)/np.sum(lables)

        micro_precision = np.sum(true_positive)/np.sum(predict)

        macro_recalls.append(macro_recall)
        macro_precisions.append(macro_precision)
        micro_recalls.append(micro_recall)
        micro_precisions.append(micro_precision)

    return recalls,macro_recalls,micro_recalls,precisions,macro_precisions,micro_precisions

def show_result(eval_result):

    for shreshold,(recall,macro_recall,micro_recall,precision,macro_precision,micro_precision) in zip(param.shresholds,zip(*eval_result)):
        print("---------------------------------threshold:", shreshold, '-----------------------------------')

        for num, i in enumerate(zip(precision, recall)):
            print(corpus.xingfa.index2fatiao[num], ":num: ", corpus.xingfa.counter[corpus.xingfa.index2fatiao[num]],
                  "  recall:", i[1], ",precesion:", i[0])

        print("macro_precision,macro_recall", macro_precision, macro_recall)
        print("micro_precision,micro_recall", micro_precision, micro_recall)

class myLoggingTensorHook(session_run_hook.SessionRunHook):
  """Prints the given tensors every N local steps, every N seconds, or at end.

  The tensors will be printed to the log, with `INFO` severity. If you are not
  seeing the logs, you might want to add the following line after your imports:

  ```python
    tf.logging.set_verbosity(tf.logging.INFO)
  ```

  Note that if `at_end` is True, `tensors` should not include any tensor
  whose evaluation produces a side effect such as consuming additional inputs.
  """

  def __init__(self, tensors, every_n_iter=None, every_n_secs=None,
               at_end=False, formatter=None):
    """Initializes a `LoggingTensorHook`.

    Args:
      tensors: `dict` that maps string-valued tags to tensors/tensor names,
          or `iterable` of tensors/tensor names.
      every_n_iter: `int`, print the values of `tensors` once every N local
          steps taken on the current worker.
      every_n_secs: `int` or `float`, print the values of `tensors` once every N
          seconds. Exactly one of `every_n_iter` and `every_n_secs` should be
          provided.
      at_end: `bool` specifying whether to print the values of `tensors` at the
          end of the run.
      formatter: function, takes dict of `tag`->`Tensor` and returns a string.
          If `None` uses default printing all tensors.

    Raises:
      ValueError: if `every_n_iter` is non-positive.
    """
    only_log_at_end = (
        at_end and (every_n_iter is None) and (every_n_secs is None))
    if (not only_log_at_end and
        (every_n_iter is None) == (every_n_secs is None)):
      raise ValueError(
          "either at_end and/or exactly one of every_n_iter and every_n_secs "
          "must be provided.")
    if every_n_iter is not None and every_n_iter <= 0:
      raise ValueError("invalid every_n_iter=%s." % every_n_iter)
    if not isinstance(tensors, dict):
      self._tag_order = tensors
      tensors = {item: item for item in tensors}
    else:
      self._tag_order = tensors.keys()
    self._tensors = tensors
    self._formatter = formatter
    self._timer = (
        NeverTriggerTimer() if only_log_at_end else
        SecondOrStepTimer(every_secs=every_n_secs, every_steps=every_n_iter))
    self._log_at_end = at_end

    self.logistics_scores = []
    self.lables = []
  def begin(self):
    self._timer.reset()
    self._iter_count = 0
    # Convert names to tensors if given
    self._current_tensors = {tag: _as_graph_element(tensor)
                             for (tag, tensor) in self._tensors.items()}

  def before_run(self, run_context):  # pylint: disable=unused-argument
    self._should_trigger = self._timer.should_trigger_for_step(self._iter_count)
    if self._should_trigger:
      return SessionRunArgs(self._current_tensors)
    else:
      return None

  def _log_tensors(self, tensor_values):
    original = np.get_printoptions()
    np.set_printoptions(suppress=True)
    elapsed_secs, _ = self._timer.update_last_triggered_step(self._iter_count)

    self.logistics_scores.append(tensor_values['logistic_scores'])
    self.lables.append(tensor_values['lables'])

    np.set_printoptions(**original)

  def after_run(self, run_context, run_values):
    _ = run_context
    if self._should_trigger:
      self._log_tensors(run_values.results)

    self._iter_count += 1

  def end(self, session):

    logistics_scores = np.concatenate(self.logistics_scores, axis=0)

    lables = np.concatenate(self.lables, axis=0)

    eval_result = evaluate(logistics_scores, lables, param.shresholds)

    show_result(eval_result)
    if self._log_at_end:

      values = session.run(self._current_tensors)
      self._log_tensors(values)

def experiment_fn(run_config, hparams):
    model = tf.estimator.Estimator(model_fn=model_fun, config=run_config,params=None, model_dir="./data")
    tensors_to_log = {"logistic_scores": "logistic_scores", 'lables': 'IteratorGetNext:1'}
    logging_hook = myLoggingTensorHook(
        tensors=tensors_to_log, every_n_iter=1)
    experiment = Experiment(
        estimator=model,
        train_input_fn=lambda: corpus.get_data_set(train=True, epoch=100, batch_size=64),
        eval_input_fn=lambda: corpus.get_data_set(train=False, batch_size=64, epoch=1),
        min_eval_frequency=1,
        eval_hooks=[logging_hook],
        eval_steps=None
    )
    return experiment

config = RunConfig(model_dir="./data",save_checkpoints_steps=param.eval_per_step)

# learn_runner.run(experiment_fn = experiment_fn,run_config = config)

exp = experiment_fn(run_config = config,hparams=None)
exp.train_and_evaluate()


import os
os.environ['MKL_THREADING_LAYER'] = 'GNU'
import argparse
import json
import train
from elo import elo_change

def get_idx_in_results(ID, results):
    ID_STEPS=0
    for i in range(len(results)):
        if ID == results[i][ID_STEPS].split('_')[0]:
            return i

def get_last_perf_results(stats):
    return stats["performance"][str(stats["last_eval_steps"])]

def recalc_avg_reward(record, is_survivor):
    WINS=1
    LOSSES=2
    GAMES=3
    AVG_REWARD = 4
    AVG_STEPS = 5
    if is_survivor:
        record[AVG_REWARD] = record[AVG_STEPS] / 300
    else:
        record[AVG_REWARD] = (record[WINS] - record[LOSSES])/record[GAMES]

def combine_records(record1, record2, survivor_1=False, survivor_2=False):
    WINS=1
    LOSSES=2
    GAMES=3
    AVG_STEPS=5

    record1[AVG_STEPS] = (record1[AVG_STEPS]*record1[GAMES] + record2[AVG_STEPS]*record2[GAMES])/(record1[GAMES]+record2[GAMES])
    record2[AVG_STEPS] = record1[AVG_STEPS]

    record1_wins = record1[WINS]
    record2_wins = record2[WINS]

    record1[WINS] += record2[LOSSES]
    record2[WINS] += record1[LOSSES]

    record1[LOSSES] += record2_wins
    record2[LOSSES] += record1_wins

    tmp = record1[GAMES]
    record1[GAMES] += record2[GAMES]
    record2[GAMES] += tmp

    recalc_avg_reward(record1, survivor_1)
    recalc_avg_reward(record2, survivor_2)

def consolidate_results(pop, all_stats):
    for i in range(len(pop)):
        #print("Consolidating", pop[i])
        i_results = get_last_perf_results(all_stats[i])
        for j in range(i+1, len(pop)):
            j_results = get_last_perf_results(all_stats[j])
            x = get_idx_in_results(pop[j], i_results)
            y = get_idx_in_results(pop[i], j_results)
            combine_records(i_results[x], j_results[y], survivor_1=all_stats[i]["survivor"], survivor_2=all_stats[j]["survivor"])

def last_elo(stats):
    return stats["elo"][str(stats["last_elo_change_steps"])]

def make_elo_changes(pop, all_stats):
    elo_changes = [0 for _ in pop]
    # Calculate ELO changes for non-exploiters
    for i in range(len(pop)):
        #print("Calculating elo changes for ", pop[i])
        if all_stats[i]["nemesis"] or all_stats[i]["survivor"]:
            continue
        i_results = get_last_perf_results(all_stats[i])
        for (Id_steps, wins, losses, games, avg_reward, avg_steps) in i_results:
            if "-nemesis" in Id_steps or "-survivor" in Id_steps:
                continue
            other_elo = last_elo(all_stats[pop.index(Id_steps.split('_')[0])])
            K=32
            (change, _) = elo_change(last_elo(all_stats[i]), other_elo, K, (avg_reward+1)/2)
            elo_changes[i] += change

    print("ELO Changes:", elo_changes)

    # Apply ELO changes, using matching agent ELO changes for exploiters
    for i in range(len(pop)):
        i_stats = all_stats[i]
        #print("Making elo changes for ", pop[i])
        if i_stats["nemesis"] or i_stats["survivor"]:
            idx = pop.index(i_stats["matching_agent"])
            i_stats["elo"][str(i_stats["last_eval_steps"])] = last_elo(i_stats) + elo_changes[idx]
        else:
            #print(i_stats)
            i_stats["elo"][str(i_stats["last_eval_steps"])] = last_elo(i_stats) + elo_changes[i]
        i_stats["last_elo_change_steps"] = i_stats["num_steps"]

if __name__ == "__main__":
    # Setup command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("model_dir", type=str, help="Base directory for agent models")
    args = parser.parse_args()
    print(args, flush=True)

    if not (args.model_dir[-1] == '/' or args.model_dir[-1] == '\\'):
        args.model_dir = args.model_dir + "/"
    if not os.path.isdir(args.model_dir):
        raise FileNotFoundError("Base directory for agent models is not a folder")

    pop = train.load_pop(args.model_dir)
    print("Consolidating training population:", pop, flush=True)
    all_stats = []
    for p in pop:
        all_stats.append(train.load_stats(args.model_dir, p))
    consolidate_results(pop, all_stats)
    make_elo_changes(pop, all_stats)
    for p,s in zip(pop, all_stats):
        train.save_stats(args.model_dir, p, s)


class Estadio: #Estrutura estádio
	nome = ""
	capacidadePublico = 0
	recordPublico = 0

def maior_capacidade(lista):
	maiorEstadio = Estadio()
	for est in lista: #Acessa direto a lista no for
		if est.capacidadePublico > maiorEstadio.capacidadePublico:
			maiorEstadio = est
	return maiorEstadio

def maior_publico(lista):
	maiorPublico = Estadio()
	for i in range(5): #Acessa a lista pelo índice gerado com range
		if lista[i].recordPublico > maiorPublico.recordPublico:
			maiorPublico = lista[i]

	return maiorPublico

estadios = []
for i in range(5):
	print()
	e = Estadio()
	e.nome = input("Informe o nome do estádio: ")
	e.capacidadePublico = int(input("Informe a capacidade: "))
	e.recordPublico = int(input("Informe o record de público: "))
	estadios.append(e)

maiorEstadio = maior_capacidade(estadios)
maiorRecord = maior_publico(estadios)

print()
print("O maior estádio é o {}, com capacidade de {} pessoas.".format(maiorEstadio.nome, maiorEstadio.capacidadePublico))
print("O maior (record) publico foi no estádio {}, com total de {} pessoas.".format(maiorRecord.nome, maiorRecord.recordPublico))


# -*- coding: UTF-8 -*-

from sqlalchemy import *
from migrate import *

#from datetime import datetime

meta=MetaData()

user_table = Table('user',meta,
              Column('id',Integer,primary_key=True),
              Column('first_name',Unicode(40),nullable = False,index=True),
              Column('last_name',Unicode(40),nullable = False,index=True),
              Column('user_name',String(40),nullable = False),
              Column('password',Unicode(256),nullable = False),
              #Column('user_roles',Integer,ForeignKey("kazhal.role.id")),
              Column('SSN',Unicode(30)),
              Column('birth_date',Date),
              Column('balance',Integer,nullable = False),
              Column('cradit',Integer),
              Column('photos_id',Integer,ForeignKey("kazhal.photo.id")),
              Column('deleted', Boolean, nullable = False),
              Column('pending', Boolean, nullable = False),
              Column('contact_methods', String(4)), #Phone,SMS,Email (PSE)
              #Column('tag_id',Integer,ForeignKey("kazhal.user_tag.id"),index=True),
              schema="kazhal",
              )

user_tag_table = Table('user_tag',meta,
                       Column('id',Integer,primary_key=True),
                       Column('tag',Unicode(40),nullable=False),
                       schema="kazhal",
                       )

user_tag_user_table = Table('user_tag_user',meta,
                            Column('tag_id',Integer,ForeignKey("kazhal.user_tag.id")),
                            Column('user_id',Integer,ForeignKey("kazhal.user.id")),
                            schema="kazhal",
                            )

group_table = Table('group',meta,
                   Column('id',Integer,primary_key=True),
                   Column('group',Unicode(40),nullable=False,unique=True),
                   schema="kazhal",
                   )

user_group_table = Table('user_group',meta,
                        Column('user_id',Integer,ForeignKey("kazhal.user.id"),primary_key=True),
                        Column('group_id',Integer,ForeignKey("kazhal.group.id"),primary_key=True),
                        schema="kazhal",
                        )

address_table = Table('address',meta,
                      Column('id',Integer,primary_key=True),
                      Column('city',Unicode(40),nullable = False),
                      Column('add1',Unicode(100),nullable = False,index=True),
                      Column('add2',Unicode(100)),
                      Column('state',Unicode(40),nullable = False),
                      Column('po_code',Unicode(40),index=True),
                      Column('user_id',Integer, ForeignKey("kazhal.user.id"),nullable = False),
                      Column('photo_id',Integer,ForeignKey("kazhal.photo.id")),
                      schema="kazhal",
                      )

phone_table = Table('phone',meta,
                    Column('p_type',Unicode(40),nullable = True),
                    Column('phone_number',Unicode(40), primary_key=True),
                    Column('user_id',Integer, ForeignKey("kazhal.user.id"),nullable = True),
                    schema="kazhal",
                    )

email_table = Table('email',meta,
                    Column('email_address',String(40),primary_key=True),
                    Column('user_id',Integer,ForeignKey("kazhal.user.id"),nullable = True),
                    schema="kazhal",
                    )

product_table = Table('product',meta,
                Column('id',Integer,primary_key=True),
                Column('code',Integer),
                Column('name',Unicode(40),nullable = False,index=True),
                Column('description',Unicode(300),index=True),
                Column('quantity',Integer),
                Column('buy_price',Integer),
                Column('sell_price',Integer),
                Column('wholesale_price',Integer),
                Column('buy_date',Date),
                Column('brand',Unicode(40)),
                Column('deleted',Boolean),
                schema="kazhal",
                )

product_tag_table = Table('product_tag',meta,
                       Column('id',Integer,primary_key=True),
                       Column('tag',Unicode(40),nullable=False),
                       schema="kazhal",
                       )

product_tag_product_table = Table('product_tag_product',meta,
                                  Column('product_id',Integer,ForeignKey("kazhal.product.id")),
                                  Column('tag_id',Integer,ForeignKey("kazhal.product_tag.id")),
                                  schema="kazhal",
                                  )

#product_photo_table = Table('product_photo',meta,
                        #Column('product_id',Integer,ForeignKey("kazhal.product.id"),primary_key=True),
                        #Column('photo_id',Integer,ForeignKey("kazhal.photo.id"),primary_key=True),
                        #schema="kazhal",
                        #)

product_photo_table = Table('product_photo',meta,
                        Column('product_id',Integer,ForeignKey("kazhal.product.id")),
                        Column('photo_id',Integer,ForeignKey("kazhal.photo.id")),
                        schema="kazhal",
                        )

photo_table = Table('photo',meta,
              Column('id',Integer,primary_key=True),
              Column('file_path',String(400)),
              schema="kazhal",
              )

invoice_table = Table('invoice',meta,
                      Column('id',Integer,primary_key=True),
                      Column('date_time',DateTime,nullable = False,index=True),
                      Column('customer_id',Integer,ForeignKey("kazhal.user.id"),nullable = False),
                      Column('staff_id',Integer,ForeignKey("kazhal.user.id"),nullable = True),
                      Column('pending', Boolean, nullable = False),
                      Column('Description',Unicode(300)),
                      Column('total_price',Integer,nullable = False),
                      Column('deleted',Boolean),
                      schema="kazhal",
                      )

invoice_item_table =Table('invoice_item',meta,
                          Column('id',Integer,primary_key=True),
                          Column('product_id',Integer,ForeignKey("kazhal.product.id"),nullable = False),
                          Column('invoice_id',Integer,ForeignKey("kazhal.invoice.id"),nullable = False),
                          Column('quantity',Integer,nullable = False),
                          Column('total_price',Integer,nullable = False),
                          Column('unitprice',Integer),
                          Column('Description',Unicode(300)),
                          schema="kazhal",
                          )

#invoice_item_invoice_table = Table('invoice_intem_invoice', meta,
                                   #Column('invoice_id',Integer,ForeignKey("kazhal.invoice_item.id"),primary_key=True),
                                   #Column('invoice_id',Integer,ForeignKey("kazhal.invoice.id"),primary_key=True),
                                   #schema="kazhal",
                                   #)

permission_table = Table('permission', meta,
    Column('id', Integer, primary_key=True),
    Column('name', Unicode(255), unique=True, nullable=False),
    schema="kazhal"
)

group_permission_table = Table('group_permission', meta,
    Column('group_id', Integer, ForeignKey('kazhal.group.id')),
    Column('permission_id', Integer, ForeignKey('kazhal.permission.id')),
    schema="kazhal"
)

user_confirm_table = Table('user_confirm', meta,
                           Column('confirm_id', Integer, primary_key=True),
                           Column('user_id', ForeignKey("kazhal.user.id"),nullable=False),
                           Column('datetime', DateTime, nullable=False),
                           Column('confirm_code', Unicode(50),unique=True),
                           schema="kazhal"
                           )

def upgrade(migrate_engine):
    # Upgrade operations go here. Don't create your own engine; bind migrate_engine
    # to your metadata
    meta.bind = migrate_engine
    #user_table.create()
    #invoice_item_table.create()
    #photo_table.create()
    #product_table.create()
    #email_table.create()
    #phone_table.create()
    #address_table.create()
    meta.create_all()

def downgrade(migrate_engine):
    # Operations to reverse the above upgrade go here.
    meta.bind = migrate_engine
    #user_table.drop()
    #invoice_item_table.drop()
    #photo_table.drop()
    #product_table.drop()
    #email_table.drop()
    #phone_table.drop()
    #address_table.drop()
    meta.drop_all()


# from __future__ import annotations

import compas_blender

from compas_blender.artists._artist import BaseArtist
from compas.utilities import color_to_colordict as colordict

__all__ = ['MeshArtist']

class MeshArtist(BaseArtist):
    """A mesh artist defines functionality for visualising COMPAS meshes in Blender.

    Parameters
    ----------
    mesh : :class:`compas.datastructures.Mesh`
        A COMPAS mesh.
    settings : dict, optional
        A dict with custom visualisation settings.

    Attributes
    ----------
    mesh : :class:`compas.datastructures.Mesh`
        The COMPAS mesh associated with the artist.
    settings : dict
        Default settings for color, scale, tolerance, ...

    Examples
    --------
    .. code-block:: python

        import compas
        from compas.datastructures import Mesh
        from compas_blender.artists import MeshArtist

        mesh = Mesh.from_obj(compas.get('faces.obj'))

        MeshArtist(mesh).draw()

    """

    def __init__(self, mesh, settings=None):
        super(MeshArtist, self).__init__()
        self._collection = None
        self._vertexcollection = None
        self._edgecollection = None
        self._facecollection = None
        self._object_vertex = {}
        self._object_edge = {}
        self._object_face = {}
        self.mesh = mesh
        self.settings = {
            'color.vertices': (255, 255, 255),
            'color.edges': (0, 0, 0),
            'color.faces': (210, 210, 210),
            'show.vertices': True,
            'show.edges': True,
            'show.faces': True}
        if settings:
            self.settings.update(settings)

    @property
    def collection(self):
        if not self._collection:
            self._collection = compas_blender.create_collection('Mesh')
        return self._collection

    @property
    def vertexcollection(self):
        if not self._vertexcollection:
            self._vertexcollection = compas_blender.create_collections_from_path('Mesh::Vertices')[1]
        return self._vertexcollection

    @property
    def edgecollection(self):
        if not self._edgecollection:
            self._edgecollection = compas_blender.create_collections_from_path('Mesh::Edges')[1]
        return self._edgecollection

    @property
    def facecollection(self):
        if not self._facecollection:
            self._facecollection = compas_blender.create_collections_from_path('Mesh::Faces')[1]
        return self._facecollection

    @property
    def object_vertex(self):
        """Map between Blender object objects and mesh vertex identifiers."""
        return self._object_vertex

    @object_vertex.setter
    def object_vertex(self, values):
        self._object_vertex = dict(values)

    @property
    def object_edge(self):
        """Map between Blender object objects and mesh edge identifiers."""
        return self._object_edge

    @object_edge.setter
    def object_edge(self, values):
        self._object_edge = dict(values)

    @property
    def object_face(self):
        """Map between Blender object objects and mesh face identifiers."""
        return self._object_face

    @object_face.setter
    def object_face(self, values):
        self._object_face = dict(values)

    # ==========================================================================
    # clear
    # ==========================================================================

    def clear(self):
        """Clear all objects previously drawn by this artist.
        """
        objects = []
        objects += list(self.object_vertex.keys())
        objects += list(self.object_edge.keys())
        objects += list(self.object_face.keys())
        compas_blender.delete_objects(objects)
        self._object_vertex = {}
        self._object_edge = {}
        self._object_face = {}

    # ==========================================================================
    # components
    # ==========================================================================

    def draw(self, settings=None):
        """Draw the mesh using the chosen visualisation settings.

        Parameters
        ----------
        settings : dict, optional
            Dictionary of visualisation settings that will be merged with the settings of the artist.

        """
        self.clear()
        if not settings:
            settings = {}
        self.settings.update(settings)
        if self.settings['show.vertices']:
            self.draw_vertices()
        if self.settings['show.faces']:
            self.draw_faces()
        if self.settings['show.edges']:
            self.draw_edges()

    def draw_mesh(self):
        """Draw the mesh."""
        vertices, faces = self.mesh.to_vertices_and_faces()
        obj = compas_blender.draw_mesh(vertices, faces, name=self.mesh.name, collection=self.collection)
        return [obj]

    def draw_vertices(self, vertices=None, color=None):
        """Draw a selection of vertices.

        Parameters
        ----------
        vertices : list
            A list of vertex keys identifying which vertices to draw.
            Default is ``None``, in which case all vertices are drawn.
        color : rgb-tuple or dict of rgb-tuple
            The color specififcation for the vertices.

        Returns
        -------
        list of :class:`bpy.types.Object`

        """
        vertices = vertices or list(self.mesh.vertices())
        vertex_color = colordict(color, vertices, default=self.settings['color.vertices'], colorformat='rgb', normalize=False)
        points = []
        for vertex in vertices:
            points.append({
                'pos': self.mesh.vertex_coordinates(vertex),
                'name': "{}.vertex.{}".format(self.mesh.name, vertex),
                'color': vertex_color[vertex],
                'radius': 0.01})

        objects = compas_blender.draw_points(points, self.vertexcollection)
        self.object_vertex = zip(objects, vertices)
        return objects

    def draw_faces(self, faces=None, color=None):
        """Draw a selection of faces.

        Parameters
        ----------
        faces : list
            A list of face keys identifying which faces to draw.
            The default is ``None``, in which case all faces are drawn.
        color : rgb-tuple or dict of rgb-tuple
            The color specififcation for the faces.

        Returns
        -------
        list of :class:`bpy.types.Object`

        """
        faces = faces or list(self.mesh.faces())
        face_color = colordict(color, faces, default=self.settings['color.faces'], colorformat='rgb', normalize=False)
        facets = []
        for face in faces:
            facets.append({
                'points': self.mesh.face_coordinates(face),
                'name': "{}.face.{}".format(self.mesh.name, face),
                'color': face_color[face]})

        objects = compas_blender.draw_faces(facets, self.facecollection)
        self.object_face = zip(objects, faces)
        return objects

    def draw_edges(self, edges=None, color=None):
        """Draw a selection of edges.

        Parameters
        ----------
        edges : list
            A list of edge keys (as uv pairs) identifying which edges to draw.
            The default is ``None``, in which case all edges are drawn.
        color : rgb-tuple or dict of rgb-tuple
            The color specififcation for the edges.

        Returns
        -------
        list of :class:`bpy.types.Object`

        """
        edges = edges or list(self.mesh.edges())
        edge_color = colordict(color, edges, default=self.settings['color.edges'], colorformat='rgb', normalize=False)
        lines = []
        for edge in edges:
            lines.append({
                'start': self.mesh.vertex_coordinates(edge[0]),
                'end': self.mesh.vertex_coordinates(edge[1]),
                'color': edge_color[edge],
                'name': "{}.edge.{}-{}".format(self.mesh.name, *edge)})

        objects = compas_blender.draw_lines(lines, self.edgecollection)
        self.object_edge = zip(objects, edges)
        return objects

# ==============================================================================
# Main
# ==============================================================================

if __name__ == "__main__":
    pass


from django.contrib import admin

from edc.base.modeladmin.admin import BaseModelAdmin

from ..forms import CorrectConsentForm
from ..models import CorrectConsent, SubjectConsent

class CorrectConsentAdmin(BaseModelAdmin):

    form = CorrectConsentForm

    fields = (
        'subject_consent',
        'report_datetime',
        'old_last_name',
        'new_last_name',
        'old_first_name',
        'new_first_name',
        'old_initials',
        'new_initials',
        'old_dob',
        'new_dob',
        'old_gender',
        'new_gender',
        'old_guardian_name',
        'new_guardian_name',
        'old_may_store_samples',
        'new_may_store_samples',
        'old_is_literate',
        'new_is_literate',
        )

    list_display = ('subject_consent', 'report_datetime')

    list_filter = ('report_datetime', 'created', 'modified')

    search_fields = ('subject_consent__subject_identifier', 'new_first_name', 'old_first_name', 'new_last_name', 'old_last_name')

    radio_fields = {
        'old_gender': admin.VERTICAL,
        'new_gender': admin.VERTICAL,
        'old_is_literate': admin.VERTICAL,
        'new_is_literate': admin.VERTICAL,
        'old_may_store_samples': admin.VERTICAL,
        'new_may_store_samples': admin.VERTICAL,
        }

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "subject_consent":
            kwargs["queryset"] = SubjectConsent.objects.filter(id__exact=request.GET.get('subject_consent', 0))
        return super(CorrectConsentAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
admin.site.register(CorrectConsent, CorrectConsentAdmin)


import unittest

# alphabetical rotation point
# https://www.interviewcake.com/question/python/find-rotation-point?course=fc1&section=sorting-searching-logarithms

def find_rotation_point(words):
    # Find the rotation point in the list

    if len(words) < 2:
        raise IndexError('At least two words are required')

    first_word = words[0]
    floor_index = 0
    ceiling_index = len(words) - 1

    while floor_index < ceiling_index:

        guess_index = floor_index + ((ceiling_index - floor_index) // 2)

        # If guess comes after the first word or is the first word,
        # then we are still in a sequence starting with the
        # first word and continuing through the current word.
        # The rotation point (alphabetically first word) must appear later.
        # Must use >= in case floor index = guess index, as in 0 = (1-0//2)
        if words[guess_index] > first_word:

            # Look to the right by advancing floor_index to guess
            floor_index = guess_index

        else:
            # current word appears after the rotation point.
            # Look to the left by bringing ceiling to guess.
            ceiling_index = guess_index

        if floor_index + 1 == ceiling_index:

            if words[-1] > words[0]:
                return 0

            else:

                return ceiling_index

# Tests
class Test(unittest.TestCase):

    def test_small_list(self):
        actual = find_rotation_point(['cape', 'cake'])
        expected = 1
        self.assertEqual(actual, expected)

    def test_medium_list(self):
        actual = find_rotation_point(['grape', 'orange', 'plum',
                                      'radish', 'apple'])
        expected = 4
        self.assertEqual(actual, expected)

    def test_large_list(self):
        actual = find_rotation_point(['ptolemaic', 'retrograde', 'supplant',
                                      'undulate', 'xenoepist', 'asymptote',
                                      'babka', 'banoffee', 'engender',
                                      'karpatka', 'othellolagkage'])
        expected = 5
        self.assertEqual(actual, expected)

    def test_sorted_list(self):
        actual = find_rotation_point(['a', 'b', 'c', 'd', 'e'])
        expected = 0
        self.assertEqual(actual, expected)

    def test_rotation_at_end(self):
        actual = find_rotation_point(['u', 'v', 'w', 'x', 'a'])
        expected = 4
        self.assertEqual(actual, expected)

    def test_too_small_list(self):
        with self.assertRaises(IndexError):
            find_rotation_point(['a'])

    # Are we missing any edge cases?

if __name__ == '__main__':
    unittest.main(verbosity=2)


import os
import numpy as np

def breakdown(batch_dir=None, batch_filename=None,xstart=None,ystart=None,\
    xsteps=None,ysteps=None,xstepsize=None,ystepsize=None,zposition=None,\
    acqtime=None,numrois=None,xbasestep=39,ybasestep=39):
    '''
    helper function for hf2dxrf_xybath
    takes a large range with uniform step size and breaks it into chunks

    batch_dir (string): directory for the input batch file
    batch_filename (string): text file name that defines the set points for batch scans
    xstart (float): starting x position
    ystart (float): starting y position
    xsteps (int): steps in X
    ysteps (int): steps in Y
    xstepsize (float): scan step in X
    ystepsize (float): scan step in Y
    zposition (float or list of floats): position(s) in z
    acqtime (float): acquisition time
    numrois (int): number or ROIs
    xbasestep (int): number of X steps in each atomic sub-scan
    ybasestep (int): number of Y steps in each atomic sub-scan
    '''
    xchunks=np.ceil((xsteps+1)/(xbasestep+1))
    ychunks=np.ceil((ysteps+1)/(ybasestep+1))
    xoverflow=np.mod((xsteps+1),(xbasestep+1))-1
    yoverflow=np.mod((ysteps+1),(ybasestep+1))-1
    print('xdimension = '+str(xchunks))
    print('ydimension = '+str(ychunks))
    print('xoverflow = '+str(xoverflow))
    print('yoverflow = '+str(yoverflow))

    if zposition is None:
        zposition=[hf_stage.z.position]
    if zposition.__class__ is not list:
        zposition=[zposition]
    mylist=list()
    for k in zposition:
        for j in range(0,int(ychunks),1):
            for i in range(0,int(xchunks),1):
                xs= xstart+(xbasestep+1)*i*xstepsize
                ys= ystart+(ybasestep+1)*j*ystepsize
                if (ychunks > 1):
                    if ((j==ychunks-1) and (yoverflow >= 0)):
                        ysteps=yoverflow
                    else:
                        ysteps=ybasestep
                if (xchunks>1):
                    if((i==xchunks-1) and (xoverflow >= 0)):
                        xsteps=xoverflow
                    else:
                        xsteps=xbasestep

                mylist.append([k,xs,xsteps,xstepsize,ys,ysteps,ystepsize,\
                acqtime,numrois])
    if batch_dir is None:
        batch_dir = os.getcwd()
        print("No batch_dir was assigned, using the current directory")
    else:
        if not os.path.isdir(batch_dir):
            raise Exception(\
            "Please provide a valid batch_dir for the batch file path.")
    if batch_filename is None:
        raise Exception(\
        "Please provide a batch file name, e.g. batch_file = 'xrf_batch_test.txt'.")
    batchfile = batch_dir+'/'+batch_filename

    with open(batchfile, 'w') as batchf:
        for item in mylist:
            for entry in item:
                batchf.write('%s '%entry)
            batchf.write('\n')
    return mylist


# coding: utf-8
import collections, copy, heapq, itertools, math, sys

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7

def II(): return int(input())
def ILI(): return list(map(int, input().split()))
def IAI(LINE): return [ILI() for __ in range(LINE)]
def IDI(): return {key: value for key, value in ILI()}

def read():
    S = str(input())
    return (S,)

def solve(S):
    d_count = collections.defaultdict(int)
    for s in S:
        d_count[s] += 1
    l_q = list("abcdefghijklm")
    l_num_0 = []
    l_num_1 = []
    l_num_2 = []
    num_other = 0
    for q in l_q:
        if d_count[q] == 0:
            l_num_0.append(q)
        elif d_count[q] == 1:
            l_num_1.append(q)
        elif d_count[q] == 2:
            l_num_2.append(q)
        else:
            num_other += 1

    ans = ""
    if num_other != 0:
        ans = "Impossible"
    else:
        if len(l_num_0) == 1 and len(l_num_2) == 1:
            ans = "".join(l_num_0)
        elif len(l_num_1) == 13:
            ans = "\n".join(sorted(l_num_1))
        else:
            ans = "Impossible"

    return ans

def main():
    params = read()
    print(solve(*params))

if __name__ == "__main__":
    main()


from tkinter import *
from webbrowser import open

__autor__ = 'TohaPlayer'
__doc__ = """
Python Pong
"""

class NewGame(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)
        self.button_new = Button(self, text='New Game', command=self.printer, font=font_menu_buttons, height=1,
                                 width=10)
        self.button_new.pack()

    # Тут ты подключаешь свой фрейм на игру
    @staticmethod
    def printer():
        print("New game")

class AboutWindow(Frame):
    # О игре/FAQ|Документацию можно слепить всю сюда
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)
        self.button = Button(self, text="About", command=self.create_window, font=font_menu_buttons, height=1, width=10)
        self.button.pack()

    @staticmethod
    def hyperlink_vk():
        open('https://vk.com/black_hat_lab')

    @staticmethod
    def hyperlink_git():
        open('https://github.com/TohaPlayer/PyPong')

    def create_window(self):
        top_window = Toplevel(self)
        top_window.wm_title("About")
        top_window.wm_geometry('300x100')
        top_window.wm_maxsize(width=300, height=100)
        top_window.wm_minsize(width=300, height=100)
        label_author = Label(top_window, text="The %s created this game in april 2016" % __autor__)
        label_author.pack(side=TOP)
        image_vk = PhotoImage(file="vk.png")
        image_git = PhotoImage(file='github.png')
        vk_button = Button(top_window, image=image_vk, command=self.hyperlink_vk, compound=CENTER)
        vk_button.image = image_vk
        vk_button.place(in_=top_window, anchor='c', relx=.4, rely=.5)
        git_button = Button(top_window, image=image_git, command=self.hyperlink_git, compound=CENTER)
        git_button.image = image_git
        git_button.place(in_=top_window, anchor='c', relx=.6, rely=.5)

class ExitGame(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)
        self.button = Button(self, text='Exit', command=lambda: root.destroy(), font=font_menu_buttons, height=1,
                             width=10)
        self.button.pack()

if __name__ == "__main__":
    # your font located here
    font_menu_buttons = 'fixedsys 20'

    root = Tk()

    # menu background image
    back_image = PhotoImage(file="ping_pong.png")
    w = back_image.width()
    h = back_image.height()
    root.geometry("%dx%d+0+0" % (w, h))
    # Максимальный и минимальный размеры чтобы ничего не поехало
    root.maxsize(width=400, height=400)
    root.minsize(width=400, height=400)
    root.title("PyPong")
    main_window = Label(root, image=back_image)
    main_window.pack(side='top', fill='both', expand='yes')
    new_game_button = NewGame(main_window)
    about_button = AboutWindow(main_window)
    exit_button = ExitGame(main_window)

    new_game_button.place(in_=main_window, anchor='c', relx=.5, rely=.3)
    about_button.place(in_=main_window, anchor='c', relx=.5, rely=.42)
    exit_button.place(in_=main_window, anchor='c', relx=.5, rely=.54)

    main_window.image = back_image

    root.mainloop()


import boto3
import json
import decimal

# Helper class to convert a DynamoDB item to JSON.

class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            if abs(o) % 1 > 0:
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)

dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

table = dynamodb.Table('dataLogger')

response = table.put_item(
    Item={
        'date': '5-17-2019:10:53',
        'temp': decimal.Decimal(67.5),
    }
)

print("PutItem succeeded:")
print(json.dumps(response, indent=4, cls=DecimalEncoder))


# -*- coding: utf-8

from nose.tools import eq_, raises

from tespy.connections import connection
from tespy.components import (basics, heat_exchangers, nodes, piping,
                              turbomachinery)

from tespy.networks.networks import network
from tespy.networks.network_reader import load_network
from tespy.tools.helpers import TESPyNetworkError

import numpy as np
import shutil
import os

class network_tests:

    def setup(self):
        self.nw = network(['CH4'],
                          T_unit='C', p_unit='bar', v_unit='m3 / s')
        self.source = basics.source('source')
        self.sink = basics.sink('sink')

    def test_network_delete_conns(self):
        """
        Test deleting a network's connection.
        """
        a = connection(self.source, 'out1', self.sink, 'in1')
        self.nw.add_conns(a)
        self.nw.check_network()
        msg = ('After the network check, the .checked-property must be True.')
        eq_(self.nw.checked, True, msg)

        self.nw.del_conns(a)
        msg = ('A connection has been deleted, the network consistency check '
               'must be repeated (.checked-property must be False).')
        eq_(self.nw.checked, False, msg)

    def test_network_missing_connection_in_init_path(self):
        """
        Test debug message for missing connection in init_path.
        """
        IF = basics.subsystem_interface('IF')
        a = connection(self.source, 'out1', self.sink, 'in1')
        self.nw.add_conns(a)
        self.nw.solve('design', init_only=True)
        self.nw.save('tmp')
        msg = ('After the network check, the .checked-property must be True.')
        eq_(self.nw.checked, True, msg)

        self.nw.del_conns(a)
        a = connection(self.source, 'out1', IF, 'in1')
        b = connection(IF, 'out1', self.sink, 'in1')
        self.nw.add_conns(a, b)
        self.nw.solve('design', init_path='tmp', init_only=True)
        msg = ('After the network check, the .checked-property must be True.')
        eq_(self.nw.checked, True, msg)

        shutil.rmtree('./tmp', ignore_errors=True)

    def test_network_export_no_chars_busses(self):
        """
        Test export of network without characteristics or busses.
        """
        a = connection(self.source, 'out1', self.sink, 'in1')
        self.nw.add_conns(a)
        self.nw.solve('design', init_only=True)
        self.nw.save('tmp')
        msg = ('The exported network does not contain any char_line, there '
               'must be no file char_line.csv!')
        eq_(os.path.isfile('tmp/comps/char_line.csv'), False, msg)

        msg = ('The exported network does not contain any char_map, there '
               'must be no file char_map.csv!')
        eq_(os.path.isfile('tmp/comps/char_map.csv'), False, msg)

        msg = ('The exported network does not contain any busses, there '
               'must be no file bus.csv!')
        eq_(os.path.isfile('tmp/comps/bus.csv'), False, msg)
        shutil.rmtree('./tmp', ignore_errors=True)

    def test_network_reader_no_chars_busses(self):
        """
        Test import of network without characteristics or busses.
        """
        a = connection(self.source, 'out1', self.sink, 'in1')
        self.nw.add_conns(a)
        self.nw.solve('design', init_only=True)
        self.nw.save('tmp')

        imported_nwk = load_network('tmp')
        imported_nwk.solve('design', init_only=True)
        msg = ('If the network import was successful the network check '
               'should have been successful, too, but it is not.')
        eq_(imported_nwk.checked, True, msg)
        shutil.rmtree('./tmp', ignore_errors=True)

    def test_network_reader_deleted_chars(self):
        """
        Test import of network with missing characteristics.
        """
        comp = turbomachinery.compressor('compressor')
        a = connection(self.source, 'out1', comp, 'in1')
        b = connection(comp, 'out1', self.sink, 'in1')
        self.nw.add_conns(a, b)
        self.nw.solve('design', init_only=True)
        self.nw.save('tmp')

        # # remove char_line and char_map folders
        os.unlink('tmp/comps/char_line.csv')
        os.unlink('tmp/comps/char_map.csv')

        # import network with missing files
        imported_nwk = load_network('tmp')
        imported_nwk.solve('design', init_only=True)
        msg = ('If the network import was successful the network check '
               'should have been successful, too, but it is not.')
        eq_(imported_nwk.checked, True, msg)
        shutil.rmtree('./tmp', ignore_errors=True)

@raises(TESPyNetworkError)
def offdesign_TESPyNetworkError(nw, **kwargs):
    nw.solve('offdesign', **kwargs)

def test_network_missing_data_in_design_case_files():
    """
    Test for missing data in design case files.
    """
    nw = network(['water'])
    source = basics.source('source')
    pipe = piping.pipe('pipe', Q=0, pr=0.95, design=['pr'], offdesign=['zeta'])
    sink = basics.sink('sink')
    a = connection(source, 'out1', pipe, 'in1', m=1, p=1e5, T=293.15,
                   fluid={'water': 1})
    b = connection(pipe, 'out1', sink, 'in1')
    nw.add_conns(a, b)
    nw.solve('design')
    nw.save('tmp')
    nw.save('tmp2')

    inputs = open('./tmp/conn.csv')
    all_lines = inputs.readlines()
    all_lines.pop(len(all_lines) - 1)
    inputs.close()

    with open('./tmp2/conn.csv', 'w') as out:
        for line in all_lines:
            out.write(line.strip() + '\n')

    offdesign_TESPyNetworkError(nw, design_path='tmp2', init_only=True)

    shutil.rmtree('./tmp', ignore_errors=True)
    shutil.rmtree('./tmp2', ignore_errors=True)

def test_network_missing_data_in_individual_design_case_file():
    """
    Test for missing data in individual design case files.
    """
    nw = network(['water'])
    source = basics.source('source')
    pipe = piping.pipe('pipe', Q=0, pr=0.95, design=['pr'], offdesign=['zeta'])
    sink = basics.sink('sink')
    a = connection(source, 'out1', pipe, 'in1', m=1, p=1e5, T=293.15,
                   fluid={'water': 1})
    b = connection(pipe, 'out1', sink, 'in1', design_path='tmp2')
    nw.add_conns(a, b)
    nw.solve('design')
    nw.save('tmp')
    nw.save('tmp2')

    inputs = open('./tmp/conn.csv')
    all_lines = inputs.readlines()
    all_lines.pop(len(all_lines) - 1)
    inputs.close()

    with open('./tmp2/conn.csv', 'w') as out:
        for line in all_lines:
            out.write(line.strip() + '\n')

    offdesign_TESPyNetworkError(nw, design_path='tmp', init_only=True)

    shutil.rmtree('./tmp', ignore_errors=True)
    shutil.rmtree('./tmp2', ignore_errors=True)

def test_network_missing_connection_in_design_path():
    """
    Test for missing connection data in design case files.
    """
    nw = network(['water'])
    source = basics.source('source')
    pipe = piping.pipe('pipe', Q=0, pr=0.95, design=['pr'], offdesign=['zeta'])
    sink = basics.sink('sink')
    a = connection(source, 'out1', pipe, 'in1', m=1, p=1e5, T=293.15,
                   fluid={'water': 1})
    b = connection(pipe, 'out1', sink, 'in1')
    nw.add_conns(a, b)
    nw.solve('design')
    nw.save('tmp')

    inputs = open('./tmp/conn.csv')
    all_lines = inputs.readlines()
    all_lines.pop(len(all_lines) - 1)
    inputs.close()

    with open('./tmp/conn.csv', 'w') as out:
        for line in all_lines:
            out.write(line.strip() + '\n')

    offdesign_TESPyNetworkError(nw, design_path='tmp')

    shutil.rmtree('./tmp', ignore_errors=True)

class test_network_individual_offdesign:

    def setup_network_individual_offdesign(self):
        """
        Set up network for individual offdesign tests.
        """
        self.nw = network(['H2O'], T_unit='C', p_unit='bar', v_unit='m3 / s')

        so = basics.source('source')
        sp = nodes.splitter('splitter', num_out=2)
        self.pump1 = turbomachinery.pump('pump 1')
        self.sc1 = heat_exchangers.solar_collector('collector field 1')
        v1 = piping.valve('valve1')
        self.pump2 = turbomachinery.pump('pump 2')
        self.sc2 = heat_exchangers.solar_collector('collector field 2')
        v2 = piping.valve('valve2')
        me = nodes.merge('merge', num_in=2)
        si = basics.sink('sink')

        self.pump1.set_attr(eta_s=0.8, design=['eta_s'],
                            offdesign=['eta_s_char'])
        self.pump2.set_attr(eta_s=0.8, design=['eta_s'],
                            offdesign=['eta_s_char'])
        self.sc1.set_attr(pr=0.95, lkf_lin=3.33, lkf_quad=0.011, A=1252, E=700,
                          Tamb=20, eta_opt=0.92, design=['pr'],
                          offdesign=['zeta'])
        self.sc2.set_attr(pr=0.95, lkf_lin=3.5, lkf_quad=0.011, A=700, E=800,
                          Tamb=20, eta_opt=0.92, design=['pr'],
                          offdesign=['zeta'])

        fl = {'H2O': 1}
        inlet = connection(so, 'out1', sp, 'in1', T=50, p=3, fluid=fl)
        outlet = connection(me, 'out1', si, 'in1', p=3)

        self.sp_p1 = connection(sp, 'out1', self.pump1, 'in1')
        self.p1_sc1 = connection(self.pump1, 'out1', self.sc1, 'in1')
        self.sc1_v1 = connection(self.sc1, 'out1', v1, 'in1', p=3.1, T=90)
        v1_me = connection(v1, 'out1', me, 'in1')

        self.sp_p2 = connection(sp, 'out2', self.pump2, 'in1')
        self.p2_sc2 = connection(self.pump2, 'out1', self.sc2, 'in1')
        self.sc2_v2 = connection(self.sc2, 'out1', v2, 'in1', p=3.1, m=0.1)
        v2_me = connection(v2, 'out1', me, 'in2')

        self.nw.add_conns(inlet, outlet, self.sp_p1, self.p1_sc1, self.sc1_v1,
                          v1_me, self.sp_p2, self.p2_sc2, self.sc2_v2, v2_me)

    def test_individual_design_path_on_connections_and_components(self):
        """
        Test individual design path specification.
        """
        self.setup_network_individual_offdesign()
        self.nw.solve('design')
        self.sc2_v2.set_attr(m=0)
        self.nw.solve('design')
        self.nw.save('design1')
        v1_design = self.sc1_v1.v.val_SI
        zeta_sc1_design = self.sc1.zeta.val

        self.sc2_v2.set_attr(T=95, m=np.nan)
        self.sc1_v1.set_attr(T=np.nan, m=0.001)
        self.nw.solve('design')
        self.nw.save('design2')
        v2_design = self.sc2_v2.v.val_SI
        zeta_sc2_design = self.sc2.zeta.val

        self.sc1_v1.set_attr(m=np.nan)
        self.sc1_v1.set_attr(design=['T'], offdesign=['v'], state='l')
        self.sc2_v2.set_attr(design=['T'], offdesign=['v'], state='l')

        self.sc2.set_attr(design_path='design2')
        self.pump2.set_attr(design_path='design2')
        self.sp_p2.set_attr(design_path='design2')
        self.p2_sc2.set_attr(design_path='design2')
        self.sc2_v2.set_attr(design_path='design2')
        self.nw.solve('offdesign', design_path='design1')

        self.sc1.set_attr(E=500)
        self.sc2.set_attr(E=950)

        self.nw.solve('offdesign', design_path='design1')
        self.sc2_v2.set_attr(design_path=np.nan)

        # volumetric flow comparison
        msg = ('Design path was set to None, is ' +
               str(self.sc2_v2.design_path) + '.')
        eq_(None, self.sc2_v2.design_path, msg)

        # volumetric flow comparison
        msg = ('Value of volumetric flow must be ' + str(v1_design) + ', is ' +
               str(self.sc1_v1.v.val_SI) + '.')
        eq_(round(v1_design, 5), round(self.sc1_v1.v.val_SI, 5), msg)

        msg = ('Value of volumetric flow must be ' + str(v2_design) + ', is ' +
               str(self.sc2_v2.v.val_SI) + '.')
        eq_(round(v2_design, 5), round(self.sc2_v2.v.val_SI, 5), msg)

        # zeta value of solar collector comparison
        msg = ('Value of zeta must be ' + str(zeta_sc1_design) + ', is ' +
               str(self.sc1.zeta.val) + '.')
        eq_(round(zeta_sc1_design, 0), round(self.sc1.zeta.val, 0), msg)

        msg = ('Value of zeta must be ' + str(zeta_sc2_design) + ', is ' +
               str(self.sc2.zeta.val) + '.')
        eq_(round(zeta_sc2_design, 0), round(self.sc2.zeta.val, 0), msg)

        shutil.rmtree('./design1', ignore_errors=True)
        shutil.rmtree('./design2', ignore_errors=True)

    def test_local_offdesign_on_connections_and_components(self):
        """
        Test local offdesign feature.
        """
        self.setup_network_individual_offdesign()
        self.nw.solve('design')
        self.sc2_v2.set_attr(m=0)
        self.nw.solve('design')
        self.nw.save('design1')
        v1_design = self.sc1_v1.v.val_SI
        zeta_sc1_design = self.sc1.zeta.val

        self.sc1_v1.set_attr(design=['T'], offdesign=['v'], state='l')
        self.sc2_v2.set_attr(design=['T'], offdesign=['v'], state='l')

        self.sc1.set_attr(local_offdesign=True, design_path='design1')
        self.pump1.set_attr(local_offdesign=True, design_path='design1')
        self.sp_p1.set_attr(local_offdesign=True, design_path='design1')
        self.p1_sc1.set_attr(local_offdesign=True, design_path='design1')
        self.sc1_v1.set_attr(local_offdesign=True, design_path='design1')
        self.sc1.set_attr(E=500)

        self.sc2_v2.set_attr(T=95, m=np.nan)
        self.nw.solve('design')
        self.nw.save('design2')

        # connections and components on side 1 must have switched to offdesign

        msg = ('Solar collector outlet temperature must be different from ' +
               'design value ' + str(round(self.sc1_v1.T.design - 273.15, 1)) +
               ', is ' + str(round(self.sc1_v1.T.val, 1)) + '.')
        eq_(round(self.sc1_v1.T.design, 1) > round(self.sc1_v1.T.val, 1),
            True, msg)

        msg = ('Parameter eta_s_char must be set for pump one.')
        eq_(self.pump1.eta_s_char.is_set, True, msg)

        msg = ('Parameter v must be set for connection from solar collector1 '
               'to pump1.')
        eq_(self.sc1_v1.v.val_set, True, msg)

        shutil.rmtree('./design1', ignore_errors=True)
        shutil.rmtree('./design2', ignore_errors=True)

    def test_missing_design_path_local_offdesign_on_connections(self):
        """
        Test missing design path specification on connections in local
        offdesign mode.
        """
        self.setup_network_individual_offdesign()
        self.nw.solve('design')
        self.sc2_v2.set_attr(m=0)
        self.nw.solve('design')
        self.nw.save('design1')
        v1_design = self.sc1_v1.v.val_SI
        zeta_sc1_design = self.sc1.zeta.val

        self.sc1_v1.set_attr(design=['T'], offdesign=['v'], state='l')
        self.sc2_v2.set_attr(design=['T'], offdesign=['v'], state='l')

        self.sc1.set_attr(local_offdesign=True, design_path='design1')
        self.pump1.set_attr(local_offdesign=True, design_path='design1')
        self.sp_p1.set_attr(local_offdesign=True, design_path='design1')
        self.p1_sc1.set_attr(local_offdesign=True, design_path='design1')
        self.sc1_v1.set_attr(local_offdesign=True)
        self.sc1.set_attr(E=500)

        self.sc2_v2.set_attr(T=95, m=np.nan)
        try:
            self.nw.solve('design', init_only=True)
        except TESPyNetworkError:
            pass

        shutil.rmtree('./design1', ignore_errors=True)


from PyQt5 import QtCore, QtGui, Qsci, QtWidgets
from PyQt5.uic import loadUi
import SplitsAndCompress
import splitAndJoin
import os,sys
import MyDropbox
#For DevConsole

class SplitsAndCompressCls(QtWidgets.QMainWindow):

	def __init__(self,parent):
		self.parent=parent
		self.settings=self.parent.settings
		self.uiFile=SplitsAndCompress.__file__.replace(".py",".ui")
		super(SplitsAndCompressCls, self).__init__(self.parent)
		loadUi(self.uiFile, self)
		self.dp = MyDropbox.MyDropboxCls(self.parent)
		self.pushButton.clicked.connect(self.doCompress)
		self.pushButton_2.clicked.connect(self.doExtract)
		self.pushButton_3.clicked.connect(self.doSplit)
		self.pushButton_4.clicked.connect(self.doJoin)
		self.pushButton_5.clicked.connect(self.doDropbox)

		self.pushButton_6.clicked.connect(self.doDBRefresh)
		self.pushButton_7.clicked.connect(self.doDBDownload)
		self.pushButton_8.clicked.connect(self.doDBUpload)

		self.splitAndJoinObj = splitAndJoin.splitAndJoinCls(self.parent)
		p = os.path.join(os.getcwd(),'Downloaded')
		self.lineEdit_9.setText(p)

	def showEvent(self, *arg):
		#Compresss
		path1 = self.settings.path
		path2 = os.path.join(self.settings.desktopPath,'splitJoin','PACKED.tar.gz')

		#Extract
		path3 = path2
		path4 = os.path.join(self.settings.desktopPath,'splitJoin','EXTRACTED')

		#Split
		path5 = path2
		path6 = os.path.join(self.settings.desktopPath,'splitJoin','Splitted')

		#Join
		path7 = path6
		path8 =  os.path.join(self.settings.desktopPath,'splitJoin','Joined','PACKED.tar.gz')

		#Dropbox
		path9 = os.path.join(self.settings.desktopPath,'dropBox','DOWNLOADED')
		path10 = path2

		self.lineEdit.setText(path1)
		self.lineEdit_2.setText(path2)

		self.lineEdit_3.setText(path3)
		self.lineEdit_4.setText(path4)

		self.lineEdit_5.setText(path5)
		self.lineEdit_6.setText(path6)

		self.lineEdit_7.setText(path7)
		self.lineEdit_8.setText(path8)

		self.lineEdit_9.setText(path9)
		self.lineEdit_10.setText(path10)

	def doDBDownload(self):
		dst = self.lineEdit_9.text()
		if not os.path.exists(dst):		  # caller handles errors
			os.makedirs(dst)				# make dir, read/write parts
		lst = self.listWidget.selectedItems()
		for each in lst:
			self.dp.downloadFile(each.text(),dst)

	def doDBUpload(self):
		pass

	def doDBRefresh(self):
		self.listWidget.clear()
		lst = self.dp.getFiles()
		for each in lst:
			itm = QtWidgets.QListWidgetItem(str(each))
			self.listWidget.addItem(itm)

	def doDropbox(self):
		destination = self.lineEdit_2.text()
		self.dp.uploadFile(destination)

	def doExtract(self):
		source = self.lineEdit_3.text()
		destination = self.lineEdit_4.text()
		self.splitAndJoinObj.extract(source,destination)

	def doCompress(self):
		source = self.lineEdit.text()
		destination = self.lineEdit_2.text()
		dpath = os.path.dirname(destination)
		if not os.path.exists(dpath):
			os.makedirs(dpath)
		self.splitAndJoinObj.compress(source,destination)

	def doJoin(self):
		source = self.lineEdit_7.text()
		destination = self.lineEdit_8.text()
		dpath = os.path.dirname(destination)
		if not os.path.exists(dpath):
			os.makedirs(dpath)
		self.splitAndJoinObj.join(source,destination)

	def doSplit(self):
		source = self.lineEdit_5.text()
		destination = self.lineEdit_6.text()
		if not os.path.exists(destination):
			os.makedirs(destination)
		self.splitAndJoinObj.split(source,destination)

if (__name__=="__main__"):

	if(not hasattr(dev,'SplitsAndCompressClsObj')  or dev.devMode):
		dev.SplitsAndCompressClsObj = SplitsAndCompressCls(dev)
		print(dev.devMode)
	dev.SplitsAndCompressClsObj.show()
	dev.SplitsAndCompressClsObj.raise_()


import os
import sys
import numpy as np
import pickle
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Agg')

def sort_spawns(angle_data, angle_key, dihedral_data, dihe_key, qy_data):

    tbf_keys  = angle_data['tbf_keys']
    tbf_states = angle_data['tbf_states']
    angles    = angle_data['angles_state_specific']
    dihedrals = dihedral_data['dihedrals_state_specific']
    populations = angle_data['populations']
    tgrid = angle_data['tgrid']

    trans_keys = qy_data['trans_keys']
    cis_keys = qy_data['cis_keys']

    ''' Check 1st point of spawned TBFs '''
    spawn_ds = {}
    spawn_as = {}
    spawn_ps = {}
    gs_keys = [ x for x in tbf_keys if tbf_states[x]==0 ]
    for key in gs_keys:
        angs = angles[key][angle_key]
        dihs = dihedrals[key][dihe_key]
        pop  = populations[key][-1]
        ''' Find first non-NaN index '''
        ind = np.where(np.isnan(angs))[0][-1] + 1
        spawn_as[key] = angs[ind]
        spawn_ds[key] = dihs[ind]
        spawn_ps[key] = pop

    matplotlib.rcParams.update({'figure.autolayout': True})
    labelsize = 16
    ticksize = 14
    plt.rc('xtick',labelsize=ticksize)
    plt.rc('ytick',labelsize=ticksize)

    fig = plt.figure(figsize=(6,5))
    for i, key in enumerate(trans_keys):
        size = spawn_ps[key] * 500
        plt.scatter(spawn_as[key], spawn_ds[key], alpha=0.6, color='lightsteelblue', s=size, linewidths=0)
    for i, key in enumerate(cis_keys):
        size = spawn_ps[key] * 500
        plt.scatter(spawn_as[key], spawn_ds[key], alpha=0.6, color='plum', s=size, linewidths=0)
    ''' Scatter points outside of plot range for labeling purposes. '''
    plt.scatter(600, 600, alpha=0.6, color='plum', s=50, linewidths=0, label='Cis')
    plt.scatter(600, 600, alpha=0.6, color='lightsteelblue', s=50, linewidths=0, label='Trans')
    s1 = plt.scatter(600, 600, alpha=0.6, color='lightgray', linewidths=0, s=100, label='0.2 Population')
    s2 = plt.scatter(600, 600, alpha=0.6, color='lightgray', linewidths=0, s=250, label='0.5 Population')
    s3 = plt.scatter(600, 600, alpha=0.6, color='lightgray', linewidths=0, s=400, label='0.8 Population')
    plt.legend(loc='best', fontsize=ticksize)
    plt.axis([95, 165, 175, 285])
    plt.xlabel('%s Angle' %angle_key, fontsize=labelsize)
    plt.ylabel('%s Dihedral Angle' %dihe_key, fontsize=labelsize)

    if not os.path.isdir('./figures'):
        os.mkdir('./figures')
    plt.savefig('./figures/spawn_scatter.pdf', dpi=300)

if __name__=='__main__':
    angle_data = pickle.load(open('../angles/data/angles.pickle', 'rb'))
    angle_keys = angle_data['angle_names']
    dihedral_data = pickle.load(open('../dihedrals/data/dihedrals.pickle', 'rb'))
    dihe_keys = dihedral_data['dihedral_names']
    qy_data = pickle.load(open('../dihedrals/data/qy.pickle', 'rb'))
    sort_spawns(angle_data, angle_keys[0], dihedral_data, dihe_keys[0], qy_data)


# Reads in the CahierIndex .csvs and produces a map:
# [Bailliage] -> [Departements, Nobility source, Third Estate source, City source, Joint source]

import csv
import codecs
import re
import pickle

ap_reg = r'([0-9]+) (A|B) to ([0-9]+) (A|B)'
ap_vol_reg = r'Vol\. ([0-9])'

def processIndex(index_file):
    # Create a data structure with all of the cahier source info, compiling
    # summary statistics along the way
    cahier_map = {}
    num_nob = 0
    num_te = 0
    num_city = 0
    city_list = []
    num_joint = 0
    cur_bailliage = ""
    with codecs.open(index_file, "r", "utf-8") as f:
        reader = csv.reader(f)
        for row in reader:
            if row[0] == "":
                continue
            if row[0].startswith("BAILLIAGE"):
                # Bailliage row
                bailliage_name = row[0].replace("BAILLIAGE:","")
                bailliage_name = bailliage_name.replace("BAILLIAGE PRINCIPALE","")
                bailliage_name = bailliage_name.replace(",","")
                bailliage_name = bailliage_name.replace(".","")
                bailliage_name = bailliage_name.strip()
                print(bailliage_name)
                cur_bailliage = bailliage_name
                # Get the departements
                departements = row[1].strip()
                cahier_map[cur_bailliage] = {}
                cahier_map[cur_bailliage]["departements"] = departements
            # We have to check if it's a joint cahier first, since we can't use
            # these to compare Nobles to Third Estate
            if "JOINT" in row[0]:
                num_joint += 1
                continue
            elif "NOBILITY" in row[0]:
                num_nob += 1
                nobility_source = row[1].strip()
                nobility_source = nobility_source.replace("SOURCE:","")
                print("NOB: " + nobility_source)
                if "AP," in nobility_source:
                    ap_vol = re.findall(ap_vol_reg, nobility_source)[0]
                    print("*** vol: " + ap_vol)
                    ap_data = re.findall(ap_reg, nobility_source)[0]
                    ap_start = ap_data[0]
                    ap_end = ap_data[2]
                    print("*** start: " + ap_start)
                    print("*** end: " + ap_end)
                    cahier_map[cur_bailliage]["nob_ap_vol"] = ap_vol
                    cahier_map[cur_bailliage]["nob_ap_start"] = ap_start
                    cahier_map[cur_bailliage]["nob_ap_end"] = ap_end
                #print("--> Nobility: " + row[0])
            elif "THIRD ESTATE" in row[0]:
                num_te += 1
                te_source = row[1].strip()
                te_source = te_source.replace("SOURCE:","")
                print("TE: " + te_source)
                if "AP," in te_source:
                    ap_vol = re.findall(ap_vol_reg, te_source)[0]
                    print("*** vol: " + ap_vol)
                    ap_data = re.findall(ap_reg, te_source)[0]
                    ap_start = ap_data[0]
                    ap_end = ap_data[2]
                    print("*** start: " + ap_start)
                    print("*** end: " + ap_end)
                    cahier_map[cur_bailliage]["te_ap_vol"] = ap_vol
                    cahier_map[cur_bailliage]["te_ap_start"] = ap_start
                    cahier_map[cur_bailliage]["te_ap_end"] = ap_end
            elif "CITY" in row[0]:
                num_city += 1
                city_list.append(cur_bailliage)
                city_source = row[1].strip()
                city_source = city_source.replace("SOURCE:","")
                print("CITY: " + city_source)
                if "AP," in city_source:
                    ap_vol = re.findall(ap_vol_reg, city_source)[0]
                    print("*** vol: " + ap_vol)
                    ap_data = re.findall(ap_reg, city_source)[0]
                    ap_start = ap_data[0]
                    ap_end = ap_data[2]
                    print("*** start: " + ap_start)
                    print("*** end: " + ap_end)
                    cahier_map[cur_bailliage]["city_ap_vol"] = ap_vol
                    cahier_map[cur_bailliage]["city_ap_start"] = ap_start
                    cahier_map[cur_bailliage]["city_ap_end"] = ap_end
                #print("--> Third Estate: " + row[0])
    # Print summary stats
    print(num_nob, num_te, num_city, num_joint)
    print(city_list)
    return cahier_map

def saveMap(the_map, filename):
    pickle.dump(the_map, open(filename,"wb"))

def main():
    cahier_map1 = processIndex("GeneralCahierIndex.csv")
    saveMap(cahier_map1, "GeneralCahierIndex.pkl")
    #print(cahier_map)

if __name__ == "__main__":
    main()


from typing import Any, Dict, Generator, Tuple

from .config_source import ConfigSource

class AWSSource(ConfigSource):
    """
    A ConfigSource that loads values recursively from an AWS SSM path.

    Warning:
        In order to use this source you must have
        [boto3](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html)
        installed either directly or by including the "aws" extra when installing this package
        (e.g. `poetry install flex-config -E aws`).

    Args:
        path (str): a prefix in AWS SSM to read from.

    Info:
        You can pass any keyword arguments to this class that you would to boto3 directly (e.g. secret key).

    Example:
        ```python
        from flex_config import AWSSource, FlexConfig

        aws_source = AWSSource(f"my_app/dev")
        my_config = FlexConfig()
        my_config.load_sources(aws_source)
        ```

    """

    def __init__(self, path: str, **kwargs: Any) -> None:
        import boto3

        self.ssm = boto3.client("ssm", "us-east-1", **kwargs)
        self.path = path

    def items(self) -> Generator[Tuple[str, Any], None, None]:
        """ Returns a generator for getting all path, value pairs """
        kwargs: Dict[str, Any] = {}
        while True:
            result = self.ssm.get_parameters_by_path(
                Path=f"/{self.path}/", Recursive=True, WithDecryption=True, **kwargs
            )

            for param in result["Parameters"]:
                key = param["Name"].replace(f"/{self.path}/", "")  # Don't repeat SSM path in key
                yield key, param["Value"]

            kwargs["NextToken"] = result.get("NextToken")
            if kwargs["NextToken"] is None:  # That's the last of the values
                break


from django.conf.urls import patterns, include, url
from api import ArticleResource

article_resource = ArticleResource()

urlpatterns = patterns('',
    url(r'^home/$','article.views.articles'),
    url(r'^article/(?P<article_id>\d+)/$','article.views.article'),
    url(r'^api/',include(article_resource.urls)),
)