# coding: utf-8
#
# Usage:
#   python examples/basic_nmt.py -m train -c configs/basic_nmt.ini
#   python examples/basic_nmt.py -m eval -c configs/basic_nmt.ini
#
# Purpose:
#   Input some sequence, then predict same sequence(+ EOS token).

import argparse
import os
import sys

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

from configs.configs import Configs
from data.data import read_data, read_words, batchnize, build_dictionary, sentence_to_onehot, seq2seq
from utils.early_stopping import EarlyStopper
from utils.monitor import Monitor

def main(args):
  # process config
  c = Configs(args.config)
  ROOT = os.environ['TENSOROFLOW']
  model_path = '%s/examples/model/basic_nmt/model' % ROOT
  PAD = c.const['PAD']
  EOS = c.const['EOS']
  train_step = c.option['train_step']
  max_time = c.option['max_time']
  batch_size = c.option['batch_size']
  vocabulary_size = c.option['vocabulary_size']
  input_embedding_size = c.option['embedding_size']
  hidden_units = c.option['hidden_units']
  source_train_data_path = c.data['source_train_data']
  target_train_data_path = c.data['target_train_data']
  source_valid_data_path = c.data['source_valid_data']
  target_valid_data_path = c.data['target_valid_data']
  source_test_data_path = c.data['source_test_data']
  target_test_data_path = c.data['target_test_data']

  # read data
  source_dictionary, source_reverse_dictionary = build_dictionary(read_words(source_train_data_path), vocabulary_size)
  source_train_datas = [sentence_to_onehot(lines, source_dictionary) for lines in read_data(source_train_data_path)]
  target_dictionary, target_reverse_dictionary = build_dictionary(read_words(target_train_data_path), vocabulary_size)
  target_train_datas = [sentence_to_onehot(lines, target_dictionary) for lines in read_data(target_train_data_path)]

  source_valid_datas = [sentence_to_onehot(lines, source_dictionary) for lines in read_data(source_valid_data_path)]
  target_valid_datas = [sentence_to_onehot(lines, target_dictionary) for lines in read_data(target_valid_data_path)]
  source_test_datas = [sentence_to_onehot(lines, source_dictionary) for lines in read_data(source_test_data_path)]
  target_test_datas = [sentence_to_onehot(lines, target_dictionary) for lines in read_data(target_test_data_path)]

  # placeholder
  encoder_inputs = tf.placeholder(shape=(None, None), dtype=tf.int32, name='encoder_inputs')
  decoder_inputs = tf.placeholder(shape=(None, None), dtype=tf.int32, name='decoder_inputs')
  decoder_labels = tf.placeholder(shape=(None, None), dtype=tf.int32, name='decoder_labels')

  # embed
  embeddings = tf.Variable(tf.random_uniform([vocabulary_size, input_embedding_size], -1.0, 1.0), dtype=tf.float32, name='embeddings')
  encoder_inputs_embedded = tf.nn.embedding_lookup(embeddings, encoder_inputs)
  decoder_inputs_embedded = tf.nn.embedding_lookup(embeddings, decoder_inputs)

  # encoder
  encoder_units = hidden_units
  encoder_cell = tf.contrib.rnn.LSTMCell(encoder_units)
  _, encoder_final_state = tf.nn.dynamic_rnn(
      encoder_cell, encoder_inputs_embedded,
      dtype=tf.float32, time_major=True
  )

  # decoder
  decoder_units = encoder_units
  decoder_cell = tf.contrib.rnn.LSTMCell(decoder_units)
  decoder_output, decoder_final_state = tf.nn.dynamic_rnn(
      decoder_cell, decoder_inputs_embedded,
      initial_state=encoder_final_state,
      scope="plain_decoder",
      dtype=tf.float32, time_major=True
  )

  decoder_logits = tf.contrib.layers.linear(decoder_output, vocabulary_size)
  decoder_prediction = tf.argmax(decoder_logits, 2) # max_time: axis=0, batch: axis=1, vocab: axis=2

  # optimizer
  stepwise_cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
      labels=tf.one_hot(decoder_labels, depth=vocabulary_size, dtype=tf.float32),
      logits=decoder_logits,
  )

  loss = tf.reduce_mean(stepwise_cross_entropy)
  train_op = tf.train.AdamOptimizer().minimize(loss)

  saver = tf.train.Saver()
  batch_idx = {'train': 0, 'valid': 0, 'test': 0}
  with tf.Session() as sess:
    if args.mode == 'train':
      # train
      loss_freq = train_step // 100
      loss_log = []
      loss_suffix = ''
      es = EarlyStopper(max_size=5, edge_threshold=0.1)
      m = Monitor(train_step)
      sess.run(tf.global_variables_initializer())
      for i in range(train_step):
        m.monitor(i, loss_suffix)
        source_train_batch, _ = batchnize(source_train_datas, batch_size, batch_idx['train'])
        target_train_batch, batch_idx['train'] = batchnize(target_train_datas, batch_size, batch_idx['train'])
        batch_data = seq2seq(source_train_batch, target_train_batch, max_time, vocabulary_size)
        feed_dict = {encoder_inputs:batch_data['encoder_inputs'],
                     decoder_inputs:batch_data['decoder_inputs'],
                     decoder_labels:batch_data['decoder_labels']}
        sess.run(fetches=[train_op, loss], feed_dict=feed_dict)
        if i % loss_freq == 0:
          source_valid_batch, _ = batchnize(source_valid_datas, batch_size, batch_idx['valid'])
          target_valid_batch, batch_idx['valid'] = batchnize(target_valid_datas, batch_size, batch_idx['valid'])
          batch_data = seq2seq(source_valid_batch, target_valid_batch, max_time, vocabulary_size)
          feed_dict = {encoder_inputs:batch_data['encoder_inputs'],
                       decoder_inputs:batch_data['decoder_inputs'],
                       decoder_labels:batch_data['decoder_labels']}
          loss_val = sess.run(fetches=loss, feed_dict=feed_dict)
          loss_log.append(loss_val)
          loss_suffix = 'loss: %f' % loss_val
          es_status = es(loss_val)
          if i > train_step // 2 and es_status:
            print('early stopping at step: %d' % i)
            break
      saver.save(sess, model_path)
      print('save at %s' % model_path)
      plt.plot(np.arange(len(loss_log)) * loss_freq, loss_log)
      plt.savefig('%s_loss.png' % model_path)
    elif args.mode == 'eval':
      saver.restore(sess, model_path)
      print('load from %s' % model_path)
    else:
      raise

    # evaluate
    loss_val = []
    input_vectors = None
    predict_vectors = None
    for i in range(len(source_test_datas) // batch_size + 1):
      source_test_batch, _ = batchnize(source_test_datas, batch_size, batch_idx['test'])
      target_test_batch, batch_idx['test'] = batchnize(target_test_datas, batch_size, batch_idx['test'])
      batch_data = seq2seq(source_test_batch, target_test_batch, max_time, vocabulary_size)
      feed_dict = {encoder_inputs:batch_data['encoder_inputs'],
                   decoder_inputs:batch_data['decoder_inputs'],
                   decoder_labels:batch_data['decoder_labels']}
      pred = sess.run(fetches=decoder_prediction, feed_dict=feed_dict)
      if predict_vectors is None:
        predict_vectors = pred.T
      else:
        predict_vectors = np.vstack((predict_vectors, pred.T))
      input_ = batch_data['encoder_inputs']
      if input_vectors is None:
        input_vectors = input_.T
      else:
        input_vectors = np.vstack((input_vectors, input_.T))
      loss_val.append(sess.run(fetches=loss, feed_dict=feed_dict))

    input_sentences = ''
    predict_sentences = ''
    for i, (input_vector, predict_vector) in enumerate(zip(input_vectors[:len(source_test_datas)], predict_vectors[:len(target_test_datas)])):
      input_sentences += ' '.join([source_reverse_dictionary[vector] for vector in input_vector if not vector == PAD])
      predict_sentences += ' '.join([target_reverse_dictionary[vector] for vector in predict_vector if not vector == PAD])
      if i < len(source_test_datas) - 1:
        input_sentences += '\n'
        predict_sentences += '\n'

    evaluate_input_path = '%s.evaluate_input' % model_path
    evaluate_predict_path = '%s.evaluate_predict' % model_path
    with open(evaluate_input_path, 'w') as f1, \
         open(evaluate_predict_path, 'w') as f2:
      f1.write(input_sentences)
      f2.write(predict_sentences)

    print('input sequences at {}'.format(evaluate_input_path))
    print('predict sequences at {}'.format(evaluate_predict_path))
    print('mean of loss: %f' % np.mean(loss_val))

  print('finish.')

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('--mode', '-m', type=str, help='train | eval')
  parser.add_argument('--config', '-c', type=str, help='config file path')
  args = parser.parse_args()
  main(args)


import datetime
import pymongo
from pymongo import MongoClient
from bson.objectid import ObjectId

client = MongoClient()
client = MongoClient('localhost', 27017)
db = client.facebook.accounts

id =   "firstpost"
number1 = {
    "name" : "Marie Elia",
    "pics.img_id": "firstimage"
}
number2 = {
    "name" : "Marie Elia",
    "posts.post_id": id
}

React1 = {
    "reaction_id": "subreactimg",
    "reaction": "Angry",
    "reactor":"Marie Elia"
}
React2 = {
    "reaction_id": "subreactpost",
    "reaction": "Angry",
    "reactor":"Marie Elia"
}

update1 = {
    "$push": {"pics.0.comments.0.subcomments.0.reactions" : React1}
}
update2 = {
    "$push": {"posts.0.comments.0.subcomments.0.reactions" : React2}
}

db.update_one(number1, update1)
db.update_one(number2, update2)


""" Unit tests for performance monitoring

"""
import logging
import os
import unittest

import numpy

from rascil.processing_components.simulation import (
    create_test_image,
)
from rascil.processing_components.util.performance import (
    performance_store_dict,
    performance_qa_image,
    performance_read,
    performance_environment,
)

log = logging.getLogger("rascil-logger")

log.setLevel(logging.WARNING)

class TestPerformance(unittest.TestCase):
    def setUp(self):
        from rascil.data_models.parameters import rascil_path

        self.results_dir = rascil_path("test_results")
        self.json_file = rascil_path("test_results/test_performance.json")

        self.m31image = create_test_image()
        self.persist = os.getenv("RASCIL_PERSIST", False)

    def test_qa_image(self):
        """Test that the QA for an image is written correctly"""
        performance_qa_image(self.json_file, "restored", self.m31image, mode="w")
        performance = performance_read(self.json_file)
        assert "restored" in performance
        assert "max" in performance["restored"]

    def test_qa_file_exception(self):
        """Check for non-existant file

        :return:
        """
        with self.assertRaises(FileNotFoundError):
            performance = performance_read("Doesnotexist.json")

    def test_environment(self):
        """Test that the environment information is written correctly"""
        performance_environment(self.json_file, mode="w")
        performance = performance_read(self.json_file)
        assert "environment" in performance
        assert "git" in performance["environment"]

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


import csv
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from mpl_toolkits.mplot3d import Axes3D
from kmeans import kmeans
from pca import pca

plt.rcParams["figure.figsize"] = (16, 9)

def plotPCAKmean(X, labels, C):
    fig = plt.figure()
    ax = Axes3D(fig)
    ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=labels)
    ax.scatter(C[:, 0], C[:, 1], C[:, 2], marker='*', c='#050505', s=1000)

    ax.set_xlabel('Component 1')
    ax.set_ylabel('Component 2')
    ax.set_zlabel('Component 3')
    ax.set_title('3D PCA', fontsize = 20)

    plt.savefig('pca-1.png')

def exportLabeledData(X, labels):
    iterator = iter(labels)
    with open("data/personality-labeled-1.csv", "w") as csvfile:
        filewriter = csv.writer(csvfile, delimiter=",")
        filewriter.writerow(["Component1","Component2","Component3","Label"])
        for line in X:
            line = np.append(line, [next(iterator)])
            filewriter.writerow(line)

def exportTestData(X):
    with open("data/predict.csv", "w") as csvfile:
        filewriter = csv.writer(csvfile, delimiter=",")
        filewriter.writerow(["Component1","Component2","Component3"])
        for line in X:
            filewriter.writerow(line)

# pca
#file = "data/personality.csv"
file = "data/predict-nopca.csv"
X = pca(file)
# kmeans

#n = 5
#labels, C = kmeans(X, n)

# Plot
#plotPCAKmean(X, labels, C)
# Export labeled personality data
#exportLabeledData(X, labels)
exportTestData(X)


from django.http import JsonResponse
import time
from django.shortcuts import render
import requests
# 查询文章
# def select_article(request):
#     return JsonResponse({"status":True})

from TestBlog.models import Article
# PageNotAnInteger 判断页面传入的参数异常
# EmptyPage  判断不存在的页面
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
import random
"""
插入测试数据
"""
def selectAll(request):
    #获取前端传入的页码
    n = request.GET.get('num',1)
    num = int(n)
    # 查询所有数据
    result = Article.objects.filter()
    # 设置一个分页，
    page = Paginator(result,6)

    # 获取当前页的数据
    # 获取异常
    try:
        page_data = page.page(num)
    except PageNotAnInteger:
        page_data = page.page(1)
    except EmptyPage:
        page_data = page.page(page.num_pages)

    arr = []
    for i in page_data:
        content = {'id': i.id, 'title': i.title, 'content': i.content,'createdata': i.createTime,'group': i.group}
        arr.append(content)
    print(arr)

    # 返回上一页下一页
    if num < 2 :
        previous_page = 1
    else:
        previous_page = num - 1
    if num > page.num_pages -1:
        next_page = page.num_pages
    else:
        next_page = num +1

    rejson = {
        'data':arr,
        'page_count':page.num_pages,
        'previous_page':previous_page,
        'next_page': next_page,
        'status':True
    }
    return JsonResponse(rejson)

# 根据关键字查询
def selectKeyword(request):
    keyword = request.GET['keyword']
    # 如果搜索为空，返回全部内容
    if keyword == "":
        # 查询所有数据
        result = Article.objects.filter()
        print(result)
        """
        result为<class 'django.db.models.query.QuerySet'>的对象
        需要进行数据处理
        """
        arr = []
        for i in result:
            content = {'id': i.id, 'title': i.title, 'content': i.content, 'createdata': i.createTime, 'group': i.group}
            arr.append(content)
        print(arr)
        rejson = {
            'data': arr,
            'status': True
        }
        return JsonResponse(rejson)
    if keyword !="":
        # 模糊查询
        result = Article.objects.filter(title__contains=keyword)

        arr = []
        for i in result:
            content = {'id': i.id, 'title': i.title, 'content': i.content, 'createdata': i.createTime, 'group': i.group}
            arr.append(content)
        print(arr)
        rejson = {
            'data': arr,
            'status': True
        }
        return JsonResponse(rejson)

# 按前端传递的分组查询
def selectGroup(request):
    group = request.GET['group']
    result = Article.objects.filter(group=group)
    """
    result为<class 'django.db.models.query.QuerySet'>的对象
    需要进行数据处理
    """
    arr = []
    for i in result:
        content = {'id': i.id, 'title': i.title, 'content': i.content,'createdata': i.createTime,'group': i.group}
        arr.append(content)
    print(arr)
    rejson = {
        'data':arr,
        'status':True
    }
    return JsonResponse(rejson)

def installArticle(request):
    title = request.GET['title']
    centen = request.GET['centen']
    group = request.GET['group']
    article = Article()
    article.title = title
    article.content = centen
    article.group = group
    datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    article.createTime = datetime
    # 保存数据
    article.save()
    return JsonResponse({"code":200})

# 跳转到新增页面
def index(request):
    return render(request,'index.html')

# 通过文章id删除
def deleteArticle(request):
    id = request.GET['id']
    Article.objects.filter(id=id).delete()
    return JsonResponse({"code":200})

# 跳转到删除页面
def delete(request):
    return render(request, 'delete.html')


"""empty message

Revision ID: 554afcdabefe
Revises: 1b3472d30676
Create Date: 2019-11-21 22:46:51.119392

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '554afcdabefe'
down_revision = '1b3472d30676'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('product',
    sa.Column('product_ID', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(), nullable=True),
    sa.Column('product_type', sa.String(), nullable=True),
    sa.Column('size', sa.Numeric(), nullable=True),
    sa.Column('alcohol_content', sa.Numeric(), nullable=True),
    sa.Column('nutritional_value', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('product_ID')
    )
    op.create_table('stock',
    sa.Column('warehouse_ID', sa.Integer(), nullable=False),
    sa.Column('product_ID', sa.String(), nullable=True),
    sa.Column('quantity', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('warehouse_ID')
    )
    op.create_table('warehouse',
    sa.Column('warehouse_ID', sa.Integer(), nullable=False),
    sa.Column('capacity', sa.Numeric(), nullable=True),
    sa.Column('street_address', sa.String(), nullable=True),
    sa.Column('city', sa.String(), nullable=True),
    sa.Column('state', sa.String(), nullable=True),
    sa.Column('zip_code', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('warehouse_ID')
    )
    op.create_table('staff',
    sa.Column('staff_id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(), nullable=True),
    sa.Column('title', sa.String(), nullable=True),
    sa.Column('salary', sa.Integer(), nullable=True),
    sa.Column('address_line1', sa.String(), nullable=True),
    sa.Column('city', sa.String(), nullable=True),
    sa.Column('state', sa.String(), nullable=True),
    sa.Column('zip_code', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['staff_id'], ['users.user_id'], ),
    sa.PrimaryKeyConstraint('staff_id')
    )
    op.create_table('order',
    sa.Column('order_ID', sa.Integer(), nullable=False),
    sa.Column('customer_ID', sa.Integer(), nullable=True),
    sa.Column('card_number', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['customer_ID'], ['customer.customer_ID'], ),
    sa.PrimaryKeyConstraint('order_ID')
    )
    op.create_table('order_items',
    sa.Column('order_ID', sa.Integer(), nullable=False),
    sa.Column('product_ID', sa.Integer(), nullable=False),
    sa.Column('quantity', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['order_ID'], ['order.order_ID'], ),
    sa.ForeignKeyConstraint(['product_ID'], ['product.product_ID'], ),
    sa.PrimaryKeyConstraint('order_ID', 'product_ID')
    )
    op.create_table('shipping_to',
    sa.Column('order_ID', sa.Integer(), nullable=False),
    sa.Column('street_address', sa.Integer(), nullable=False),
    sa.Column('city', sa.String(), nullable=True),
    sa.Column('state', sa.String(), nullable=True),
    sa.Column('zip_code', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['order_ID'], ['order.order_ID'], ),
    sa.ForeignKeyConstraint(['street_address'], ['product.product_ID'], ),
    sa.PrimaryKeyConstraint('order_ID', 'street_address')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('shipping_to')
    op.drop_table('order_items')
    op.drop_table('order')
    op.drop_table('staff')
    op.drop_table('warehouse')
    op.drop_table('stock')
    op.drop_table('product')
    # ### end Alembic commands ###


import sys
import os
import signal
import pygame
import pygame.surface
import pygame.joystick
import explorerhat

# Setup PyGame and create the link to the Steam controller

os.environ["SDL_VIDEODRIVER"] = "dummy"

signal.signal(signal.SIGINT, signal.default_int_handler)

pygame.init()
os.putenv('SDL_VIDEODRIVER', 'fbcon')
pygame.display.init()

pygame.joystick.quit()
pygame.joystick.init()

joystick_count = pygame.joystick.get_count()

if joystick_count < 1:
    print("No joysticks found. sc-xbox.py not running?")
    sys.exit()

print("Found " + str(joystick_count) + " joysticks.")

joy = pygame.joystick.Joystick(0)
joy.init()

# Global control variables

trimming = 0.2
powerScalar = 0.5

steer = 0
drive = 0

endProg = False

# Loop until the 'A' button is pressed
print("")
print("Controls:")
print("The `A` button exits the script.")
print("The joystick controls left/right steering.")
print("Left trigger applies forward drive.")
print("Right trigger applies backward drive.")

while not endProg:
    try:
        timeStart = pygame.time.get_ticks()

        for event in pygame.event.get():
            # A button pressed?
            if event.type == pygame.JOYBUTTONDOWN and joy.get_button(0) == 1:
                endProg = True

        # Left and Right triggers
        joydrive = (joy.get_axis(5) * 0.5 + 0.5) - (joy.get_axis(2) * 0.5 + 0.5)

        # Joystick steering control
        joysteer = joy.get_axis(0)

        steer = joysteer
        drive = joydrive

        left = min(1.0, max(-1.0, drive * 0.5 - drive * steer * 0.5))
        right = min(1.0, max(-1.0, drive * 0.5 + drive * steer * 0.5))

        sendLeftMotor = left
        sendRightMotor = right

        trimMulLeft = 1.0 - max(0.0, -trimming)
        trimMulRight = 1.0 - max(0.0, trimming)

        explorerhat.motor[0].speed(int(sendLeftMotor * trimMulLeft * powerScalar * 100))
        explorerhat.motor[1].speed(-int(sendRightMotor * trimMulRight * powerScalar * 100))

        timeEnd = pygame.time.get_ticks()

        deltaTime = timeEnd - timeStart

        pygame.time.delay(max(0, 100 - deltaTime))

    except KeyboardInterrupt:
        endProg = True
        break

pygame.quit()


from collections import defaultdict, namedtuple
import numpy as np
from vermouth.parser_utils import SectionLineParser
from vermouth.log_helpers import StyleAdapter, get_logger

LOGGER = StyleAdapter(get_logger(__name__))

PersistenceSpecs = namedtuple("persistence", ["model", "lp", "start", "stop", "mol_idxs"])

class BuildDirector(SectionLineParser):
    """
    Parser for the build file which dictates additional information
    about how to generate the system in the random-walk.
    """
    COMMENT_CHAR = ';'

    def __init__(self, molecules, topology):
        super().__init__()
        self.topology = topology
        self.molecules = molecules
        self.current_molidxs = []
        self.build_options = defaultdict(list)
        self.current_molname = None
        self.rw_options = dict()
        self.persistence_length = {}

    @SectionLineParser.section_parser('molecule')
    def _molecule(self, line, lineno=0):
        """
        Parses the lines in the '[molecule]'
        directive and stores it.
        """
        tokens = line.split()
        self.current_molname = tokens[0]
        self.current_molidxs = np.arange(float(tokens[1]), float(tokens[2]), 1., dtype=int)
        for idx in self.current_molidxs:
            if idx not in self.topology.mol_idx_by_name[tokens[0]]:
                LOGGER.warning("parsing build file: could not find molecule with name {name} and index {index}.",
                              **{"index": idx, "name": tokens[0]})

    @SectionLineParser.section_parser('molecule', 'cylinder', geom_type="cylinder")
    @SectionLineParser.section_parser('molecule', 'sphere', geom_type="sphere")
    @SectionLineParser.section_parser('molecule', 'rectangle', geom_type="rectangle")
    def _parse_geometry(self, line, lineno, geom_type):
        """
        Parses the lines in the '[<geom_type>]'
        directive and stores it. The format of the directive
        is as follows:

        <resname><resid_start><resid_stop><x><y><z><parameters>

        parameters depends on the geometry type and contains
        for:

        spheres - radius
        cylinder - radius and 1/2 d
        reactangle - 1/2 a, b, c which are the lengths
        """
        tokens = line.split()
        geometry_def = self._base_parser_geometry(tokens, geom_type)
        for idx in self.current_molidxs:
            self.build_options[(self.current_molname, idx)].append(geometry_def)

    @SectionLineParser.section_parser('molecule', 'rw_restriction')
    def _rw_restriction(self, line, lineno=0):
        """
        Restrict random walk in specific direction.
        """
        tokens = line.split()
        geometry_def = {"resname": tokens[0],
                        "start": int(tokens[1]),
                        "stop":  int(tokens[2]),
                        "parameters": [np.array(tokens[3:6], dtype=float),
                                       float(tokens[6])]}

        for idx in self.current_molidxs:
            self.rw_options[(self.current_molname, idx)] = geometry_def

    @SectionLineParser.section_parser('molecule', 'distance_restraints')
    def _distance_restraints(self, line, lineno=0):
        """
        Node distance restraints.
        """
        tokens = line.split()
        nodes = tuple(map(int, tokens[:2]))
        dist = float(tokens[2])
        # parse tolerance if given
        if len(tokens) == 4:
            tol = float(tokens[3])
        else:
            tol = 0.0

        for idx in self.current_molidxs:
            msg = "Could not find atom {node} in molecule {molname} with index {idx}."
            if nodes[0] not in self.topology.molecules[idx]:
                raise IOError(msg.format(node=nodes[0], idx=idx, molname=self.current_molname))
            elif nodes[1] not in self.topology.molecules[idx]:
                raise IOError(msg.format(node=nodes[1], idx=idx, molname=self.current_molname))

            self.topology.distance_restraints[(self.current_molname, idx)][nodes] = (dist, tol)

    @SectionLineParser.section_parser('molecule', 'persistence_length')
    def _persistence_length(self, line, lineno=0):
        """
        Generate a distribution of end-to-end distance restraints based on a set
        persistence length.
        """
        tokens = line.split()
        model = tokens.pop(0)
        persistence_length = float(tokens.pop(0))
        start, stop = list(map(int, tokens))
        specs = PersistenceSpecs(*[model, persistence_length, start, stop, self.current_molidxs])
        self.topology.persistences.append(specs)

    def finalize(self, lineno=0):
        """
        Tag each molecule node with the chirality and build options
        if that molecule is mentioned in the build file by name.
        """
        for mol_idx, molecule in enumerate(self.molecules):

            if (molecule.mol_name, mol_idx) in self.build_options:
                for option in self.build_options[(molecule.mol_name, mol_idx)]:
                    self._tag_nodes(molecule, "restraints", option, molecule.mol_name)

            if (molecule.mol_name, mol_idx)  in self.rw_options:
                self._tag_nodes(molecule, "rw_options",
                                self.rw_options[(molecule.mol_name, mol_idx)],
                                molecule.mol_name)

        super().finalize

    @staticmethod
    def _tag_nodes(molecule, keyword, option, molname=""):
        resids = np.arange(option['start'], option['stop'], 1.)
        resname = option["resname"]
        for node in molecule.nodes:
            if molecule.nodes[node]["resid"] in resids\
            and molecule.nodes[node]["resname"] == resname:
                molecule.nodes[node][keyword] = molecule.nodes[node].get(keyword, []) +\
                                                [option['parameters']]
            # broadcast warning if we find the resid but it doesn't match the resname
            elif molecule.nodes[node]["resid"] in resids and not\
                 molecule.nodes[node]["resname"] == resname:
                 msg = "parsing build file: could not find resid {resid} with resname {resname} in molecule {molname}."
                 LOGGER.warning(msg, **{"resid": molecule.nodes[node]["resid"], "resname": resname,
                                          "molname": molname})

            # broadcast warning if we find the resname but it doesn't match the resid
            elif molecule.nodes[node]["resname"] == resname and not\
                 molecule.nodes[node]["resid"]:
                 msg = "parsing build file: could not find residue {resname} with resid {resid} in molecule {molname}."
                 LOGGER.warning(msg, **{"resid": molecule.nodes[node]["resid"], "resname": resname,
                                        "molname": molname})

    @staticmethod
    def _base_parser_geometry(tokens, _type):
        geometry_def = {}
        geometry_def["resname"] = tokens[0]
        geometry_def["start"] = float(tokens[1])
        geometry_def["stop"] = float(tokens[2])

        point = np.array([float(tokens[4]), float(tokens[5]), float(tokens[6])])
        parameters = [tokens[3], point]

        for param in tokens[7:]:
            parameters.append(float(param))

        parameters.append(_type)
        geometry_def["parameters"] = parameters
        return geometry_def

def read_build_file(lines, molecules, topology):
    """
    Parses `lines` of itp format and adds the
    molecule as a block to `force_field`.

    Parameters
    ----------
    lines: list
        list of lines of an itp file
    force_field: :class:`vermouth.forcefield.ForceField`
    """
    director = BuildDirector(molecules, topology)
    return list(director.parse(iter(lines)))


import numpy as np
import pygame as pg
import random
from raycaster import RayCastEngine
from screens import LoadUp, MenuScreen

class Level:
    def __init__(self, screen):
        self.screen = screen
        self.play_game = False

        # Engine Instance
        self.engine = RayCastEngine()
        self.loading_screen = LoadUp(screen)
        self.menu_screen = MenuScreen(screen)

        # Render Resolution
        self.h_res = 200
        self.half_v_res = 150
        self.frame = np.random.uniform(0, 1, (self.h_res, self.half_v_res * 2, 3))

        # FOV Scaling
        self.fov = self.h_res / 60

        # Map Settings
        self.map_size = 40
        self.pos_x, self.pos_y, self.rot, self.map_h = self.engine.gen_map(self.map_size)

        # Vertical Pan
        self.rot_v = 0
        self.offset = int(0.8 * self.half_v_res) * 2

        # Texture Loading
        sky = pg.image.load("assets/environment.png").convert()
        self.sky = pg.surfarray.array3d(pg.transform.scale(sky, (360, self.half_v_res * 2 + self.offset))) / 255
        self.floor = pg.surfarray.array3d(pg.image.load("assets/floor.png").convert()) / 255
        self.wall = pg.surfarray.array3d(pg.image.load("assets/wall.jpg").convert()) / 255

        # Sound Effects
        self.ambience = pg.mixer.Sound("sounds/ambient.mp3")
        self.game_music = pg.mixer.Sound("sounds/game_music.mp3")
        self.menu_music = pg.mixer.Sound("sounds/menu_music.mp3")
        self.feet1 = pg.mixer.Sound("sounds/footsteps.mp3")
        self.feet2 = pg.mixer.Sound("sounds/footsteps2.mp3")
        self.ambience.set_volume(0.8)
        self.game_music.set_volume(0.1)
        self.menu_music.set_volume(0.35)
        self.feet1.set_volume(0.7)
        self.feet2.set_volume(0.4)
        self.menu_music.play(-1)

        # Custom Events
        self.PLAY_STEPS = pg.USEREVENT + 1

    def update(self, dt, events):
        # Getting New Frame Render
        self.frame = self.engine.new_frame(self.pos_x, self.pos_y, self.rot, self.frame, self.sky, self.floor,
                                           self.h_res, self.half_v_res, self.fov, self.map_h, self.map_size,
                                           self.wall, self.rot_v)

        # Inputs
        keys = pg.key.get_pressed()
        mouse_rel = pg.mouse.get_rel()

        # Scale Frame To Window map_size
        surf = pg.surfarray.make_surface(self.frame * 255)
        scaled_frame = pg.transform.scale(surf, (800, 600))

        # Blit Frame On Screen
        self.screen.blit(scaled_frame, (0, 0))

        # Manage Movements
        speed = 1 if keys[pg.K_LSHIFT] else 0.5
        self.pos_x, self.pos_y, self.rot, self.rot_v = self.engine.movement(self.pos_x, self.pos_y, self.rot,
                                                                            self.map_h, dt * speed,
                                                                            keys, self.rot_v, mouse_rel)

        # Startup
        if self.loading_screen.alpha_value > 0:
            self.loading_screen.update(dt)
        else:
            # Menu Screen
            if not self.play_game:
                self.menu_screen.update(dt)
                if keys[pg.K_RETURN]:
                    self.menu_music.fadeout(1500)
                    self.ambience.play(-1)
                    self.game_music.play(-1)
                    pg.time.set_timer(self.PLAY_STEPS, 10000)
                    self.play_game = True
            else:
                # Cue Footstep Sounds
                for ev in events:
                    if ev.type == self.PLAY_STEPS:
                        random_sound = random.randint(1, 2)
                        if random_sound == 1:
                            self.feet1.play()
                        elif random_sound == 2:
                            self.feet2.play()

                        # Reschedule Steps At Random Time Interval
                        random_play = random.randint(10000, 20000)
                        pg.time.set_timer(self.PLAY_STEPS, random_play)


#!/usr/bin/env python3

import numpy as np
import scipy
import csv
import glob
from scipy import signal
import copy
import matplotlib.pylab as plt
plt.ion()
np.set_printoptions(precision=3)

import datetime
import time

markersize=6
full_dataset = np.load('stolaf_full_dataset.npy')
'''
0 = Air_temperature_snapshot_room
1 = Oral Temperature
2 = Quick Oral Temperature
3 = Surface canthus temperature inside tent at end of 10-minutes
4 = delta time between end of 10 minutes and snapshot
    for using the last measured env temp and last equil image this must be <2 minutes
    np.sum(full_dataset[:,:,4]>120)/112 is 15% of the data
5 = effective air/environmental temperature in last 100 seconds of tent
6 = Surface canthus temperature outside tent during secondary scans
7 = Device #5 surface temperature
8 = Device #2 Body temperature
9 = Device #3 Body temperature
10 = Device #4 Scan Body temperature
11 = air temperature (not adjusted with background, just raw air) over last 100 seconds in tent
'''

plt.figure()
plt.subplot(1,2,1)
cdata=np.genfromtxt('cargill_data.csv', delimiter=',')
cdata=cdata[1:,:]
oral = cdata[:,1]
fi_75F = cdata[:,2]
fi_85F = cdata[:,6]
fi_85F[-1]=round(np.nanmean(fi_85F),1)

# product correction factor F/(1+F) used at time of pilot study
unphysio = lambda core, amb: core - 0.191*(core-amb)
surf_85F = unphysio(fi_85F, 85)
surf_75F = unphysio(fi_75F, 75)
surf_45F = np.loadtxt('cargill_45F.txt')

# ambient data not recorded beyond air probe at time of study
alldata_ambients = np.concatenate([np.ones(len(surf_75F))*75, np.ones(len(surf_85F))*85])
alldata_surfs = np.concatenate([surf_75F, surf_85F])

p=np.polyfit(alldata_ambients, alldata_surfs,1)
#plt.plot(np.linspace(70, 90, 100), np.polyval(p, np.linspace(70, 90, 100)))
plt.plot(np.ones(len(surf_45F))*45, surf_45F,'k.')
plt.plot(np.linspace(40, 90, 100), np.polyval(p, np.linspace(40, 90, 100)), 'k--')
plt.plot(np.ones(len(surf_75F))*75, surf_75F,'k.')
plt.plot(np.ones(len(surf_85F))*85, surf_85F,'k.')
plt.legend(['Surface Data', 'Fit to 75, 85F, F=0.234'])
plt.ylabel('Surface (F)', fontsize=12)
plt.xlabel('Ambient (F)', fontsize=12)

axa=plt.subplot(1,2,2)
axa.yaxis.set_label_position("right")
effectives=full_dataset[:,:,5].flatten()
airs=full_dataset[:,:,11].flatten()
surfs=full_dataset[:,:,3].flatten()
T_oral=(full_dataset[:,:,1].flatten()-32)/1.8
orals=T_oral.flatten()
offset=orals - surfs
orals=orals[np.isnan(airs)==False]
offset=offset[np.isnan(airs)==False]
effectives=effectives[np.isnan(airs)==False]
surfs=surfs[np.isnan(airs)==False]
airs=airs[np.isnan(airs)==False]
# fit against the effective air temperature
p=np.polyfit(effectives, offset, 1)
f=-p[0]
F = f/(1-f)
physio_correction = lambda surf, amb: surf + (surf-amb)*F
un_physio_correction = lambda core, amb: core - (core-amb)*f
print('Fit of F-value to elevated tent temperatures: ', F)

plt.plot(effectives*1.8+32, orals*1.8+32, 'b.', markersize=markersize)
plt.plot(effectives*1.8+32, surfs*1.8+32, 'ks', markersize=markersize)
plt.plot(effectives*1.8+32, physio_correction(surfs, effectives)*1.8+32, 'r.', markersize=markersize)
mn=np.min(effectives)*1.8+32
mx=np.max(effectives)*1.8+32
x=np.linspace(mn, mx, 20)
po=np.polyfit(effectives*1.8+32, orals*1.8+32, 1)
ps=np.polyfit(effectives*1.8+32, surfs*1.8+32, 1)
pb=np.polyfit(effectives*1.8+32, physio_correction(surfs, effectives)*1.8+32, 1)
y=np.polyval(po, x)
plt.plot(x, y, 'b--')
y=np.polyval(ps, x)
plt.plot(x, y, 'k--')
y=np.polyval(pb, x)
plt.plot(x, y, 'r--')
plt.xlabel('Ambient (F)', fontsize=12)
plt.ylabel('Measured (F)', fontsize=12)
plt.legend(['Oral Thermometry','Surface Temperature', 'Corrected Body, F=%.3f'%F])
plt.savefig('F_value_pilot_and_study.png', bbox_inches='tight',pad_inches = 0.1)


import pygame

def main():
    pygame.init()

    white = (255, 255, 255)
    yellow = (255, 255, 102)
    black = (0, 0, 0)
    red = (213, 50, 80)
    green = (0, 255, 0)
    blue = (50, 153, 213)

    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()

    radius = 15
    x = 0
    y = 0
    mode = 'blue'
    points = []

    colors = [white, yellow, black, red, green, blue]
    v = 0

    mouse_pressed = False

    pick_form = 0
    screen.fill((255, 255,255))
    while True:

        pressed = pygame.key.get_pressed()

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                return
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    return

                # determine if a letter key was pressed
                if event.key == pygame.K_q:
                    pick_form = 0
                    points = []
                elif event.key == pygame.K_a:
                    pick_form = 1
                    points = []
                elif event.key == pygame.K_z:
                    pick_form = 2
                    points = []
                elif event.key == pygame.K_UP:
                    radius = min(200, radius + 1)
                elif event.key == pygame.K_DOWN:
                    radius = max(1, radius - 1)
            if event.type == pygame.MOUSEBUTTONDOWN: #нажатие кнопки мыши
                if event.button == 1:
                    mouse_pressed = True

            elif event.type == pygame.MOUSEBUTTONUP: #отпускание кнопки мыши
                if event.button == 1:
                    mouse_pressed = False

            if event.type == pygame.MOUSEMOTION: #перемещение курсора мыши

                position = event.pos
                if mouse_pressed:
                    points = points + [position]
                    points = points[-256:]
                else:
                    points=[]

            if event.type == pygame.MOUSEWHEEL: #кручение колесика мыши
                for j in colors:
                    j[0] += 1

        # draw all points
        i = 0
        while i < len(points) - 1:
            if pick_form == 0:
                drawLineBetween(screen, i, points[i], points[i + 1], radius, mode, pick_form,mouse_pressed)
            if pick_form == 1:
                drawLineRect(screen, i, points[i], points[i + 1], radius, mode, pick_form,mouse_pressed)
            if pick_form == 2:
                drawLineEraser(screen, i, points[i], points[i + 1], radius, mode, pick_form,mouse_pressed)
            i += 1

        pygame.display.flip()

        clock.tick(60)

def drawLineBetween(screen, index, start, end, width, color_mode, pick_form, mouse_pressed):

    dx = start[0] - end[0]
    dy = start[1] - end[1]
    iterations = max(abs(dx), abs(dy))

    for i in range(iterations):
        progress = 1.0 * i / iterations
        aprogress = 1 - progress
        x = int(aprogress * start[0] + progress * end[0])
        y = int(aprogress * start[1] + progress * end[1])
        if mouse_pressed:
            pygame.draw.circle(screen, color_mode, (x, y), width)

def drawLineRect(screen, index, start, end, width, color_mode, pick_form, mouse_pressed):

    dx = start[0] - end[0]
    dy = start[1] - end[1]
    iterations = max(abs(dx), abs(dy))

    for i in range(iterations):
        progress = 1.0 * i / iterations
        aprogress = 1 - progress
        x = int(aprogress * start[0] + progress * end[0])
        y = int(aprogress * start[1] + progress * end[1])
        if mouse_pressed:
            pygame.draw.rect(screen, color_mode, [x, y, width, width])

def drawLineEraser(screen, index, start, end, width, color_mode,pick_form, mouse_pressed):

    dx = start[0] - end[0]
    dy = start[1] - end[1]
    iterations = max(abs(dx), abs(dy))

    for i in range(iterations):
        progress = 1.0 * i / iterations
        aprogress = 1 - progress
        x = int(aprogress * start[0] + progress * end[0])
        y = int(aprogress * start[1] + progress * end[1])
        if mouse_pressed:
            pygame.draw.circle(screen, 'white', (x, y), width)
main()


import serial.tools.list_ports

ports=serial.tools.list_ports.comports()

serialInst=serial.Serial()

portList=[]

for onePort in ports:
    portList.append(str(onePort))
    print(str(onePort))

val=input('Select Port: COM')
print(val)

for x in range (0,len(portList)):
    if portList[x].startswith('COM'+str(val)):
        portVar='COM'+str(val)
        print(portList[x])

serialInst.baudrate=115200
serialInst.port=portVar
serialInst.open()

while True:
    if serialInst.in_waiting:
        packet=serialInst.readline()
        #capturar()  # capturar datos para poner en archivo txt
        #mostrar los datos del archivo
        print(packet.decode('utf-8'))


from pathlib import Path

def get_runinfo(logdir):
    p = Path(logdir)
    dirs = sorted(f for f in p.rglob('*') if f.is_dir())
    res = {}
    files = [f for f in p.glob('*')]
    if files:
        res['.'] = p.absolute()
    for dir in dirs:
        res[dir.name] = dir.absolute()
    return res

def is_available_flie(filename):
    filename = Path(filename)
    return True if filename.suffix == '.json' or "events" in filename.name\
                   or "projector" in filename.name else False


#!/usr/bin/env python

from __future__ import absolute_import, division, print_function, unicode_literals
import sys
import os
import argparse
import tarfile
import gzip
import json
import requests
import shutil
import subprocess
from amptk import amptklib

try:
    from urllib.request import urlopen
except ImportError:
    from urllib2 import urlopen

class MyFormatter(argparse.ArgumentDefaultsHelpFormatter):
    def __init__(self, prog):
        super(MyFormatter, self).__init__(prog, max_help_position=50)

def main(args):
    parser = argparse.ArgumentParser(
        prog="amptk-install.py",
        description="""Script to download preformatted databases""",
        epilog="""Written by Jon Palmer (2019) nextgenusfs@gmail.com""",
        formatter_class=MyFormatter,
    )
    parser.add_argument(
        "-i",
        "--input",
        nargs="+",
        required=True,
        choices=["ITS", "16S", "LSU", "COI", "PR2"],
        help="Install Databases",
    )
    parser.add_argument(
        "-f", "--force", action="store_true", help="Overwrite existing databases"
    )
    parser.add_argument(
        "-l", "--local", action="store_true", help="Use local downloads.json for links"
    )
    args = parser.parse_args(args)

    parentdir = os.path.join(os.path.dirname(amptklib.__file__))

    # downd from github to get most recent databases
    if not args.local:
        try:
            print("Retrieving download links from GitHub Repo")
            URL = json.loads(
                requests.get(
                    "https://raw.githubusercontent.com/nextgenusfs/amptk/master/amptk/downloadsv1.6.0.json"
                ).text
            )
        except:
            print(
                "Unable to download links from GitHub, using funannotate version specific links"
            )
            with open(
                os.path.join(os.path.dirname(__file__), "downloadsv1.6.0.json")
            ) as infile:
                URL = json.load(infile)
    else:
        with open(
            os.path.join(os.path.dirname(__file__), "downloadsv1.6.0.json")
        ) as infile:
            URL = json.load(infile)

    for x in args.input:
        udbfile = os.path.join(parentdir, "DB", x + ".udb")
        if os.path.isfile(udbfile):
            if not args.force:
                print(
                    "A formated database was found, to overwrite use '--force'. You can add more custom databases by using the `amptk database` command."
                )
                sys.exit(1)
        # download
        if not x in URL:
            if args.force:
                continue
            print("%s not valid, choices are ITS, 16S, LSU, COI, PR2" % x)
            sys.exit(1)
        print("Downloading %s pre-formatted database" % x)
        # getting where some files need to be split, so check here if is a list or not
        # list of tar.gz files must be in proper order
        address = URL.get(x)
        if isinstance(address, list):
            dloads = []
            for i, addy in enumerate(address):
                dloadname = "{}.part{}.tar.gz".format(x, i + 1)
                if not os.path.isfile(dloadname):
                    amptklib.download(addy, dloadname)
                    dloads.append(dloadname)
            concat_cmd = ["cat"] + dloads
            with open(x + ".amptk.tar.gz", "wb") as outfile:
                subprocess.call(concat_cmd, stdout=outfile)
            for f in dloads:
                os.remove(f)
        elif isinstance(address, str):
            if not os.path.isfile(x + ".amptk.tar.gz"):
                amptklib.download(address, x + ".amptk.tar.gz")
        # now extract and install
        tfile = tarfile.open(x + ".amptk.tar.gz", "r:gz")
        tfile.extractall(x)
        for file in os.listdir(x):
            shutil.move(os.path.join(x, file), os.path.join(parentdir, "DB", file))
        shutil.rmtree(x)
        os.remove(x + ".amptk.tar.gz")
        print("Extracting FASTA files for {:}".format(x))
        extracted = os.path.join(parentdir, "DB", x + ".extracted.fa")
        cmd = ["vsearch", "--udb2fasta", udbfile, "--output", extracted]
        amptklib.runSubprocess5(cmd)
        print(
            "{:} taxonomy database installed to {:}".format(
                x, os.path.join(parentdir, "DB")
            )
        )

if __name__ == "__main__":
    main()


#!/usr/bin/python

import csv
import json
import hashlib
import sys
import os

filename = sys.argv[1]

header = []
rows = []

with open(filename, 'r') as csvfile:
    csvreader = csv.reader(csvfile)

    first = next(csvreader)
    first.append("")
    header = next(csvreader)
    header.append("Hash")

    team = first[0].capitalize()
    for i in range(20):
        aRow = next(csvreader)
        seriesNum = aRow[0]
        fileName = aRow[1]
        name = aRow[2]
        description = aRow[3]
        gender = aRow[4]
        uuid = aRow[6]
        toJson = {
            "format": "CHIP-007",
            "name": name,
            "minting_tool": team,
            "sensitive_content": False,
            "series_number": seriesNum,
            "series_total": 526,
            "uuid": uuid,
            "attributes": {
                "trait_type": "gender",
                "value": gender
            },
            "collection": {
                "name": "Zuri NFT Tickets for Free Lunch",
                "id": "b774f676-c1d5-422e-beed-00ef5510c64d",
                "attributes": [
                    {
                        "type": "description",
                        "value": "Rewards for accomplishments during HNGi9."
                    }
                ]
            }
        }
        jsonName = f"{fileName}.json"
        with open(jsonName, "w") as outfile:
            json.dump(toJson, outfile)
        sha256_hash = hashlib.sha256()
        hash = ""
        with open(jsonName, "rb") as f:
            for byte_block in iter(lambda: f.read(4096), b""):
                sha256_hash.update(byte_block)
            hash = sha256_hash.hexdigest()
        aRow.append(hash)
        rows.append(aRow)

    for row in csvreader:
        if row[1] == "" or row[2] == "":
            row.append("")
            continue
        if row[0].lower().startswith("team"):
            team = row[0]
            row.append("")
            continue
        team = team.capitalize()
        seriesNum = row[0]
        fileName = row[1]
        name = row[2]
        description = row[3]
        gender = row[4]
        uuid = row[6]
        toJson = {
            "format": "CHIP-007",
            "name": name,
            "minting_tool": team,
            "sensitive_content": False,
            "series_number": seriesNum,
            "series_total": 526,
            "uuid": uuid,
            "attributes": {
                "trait_type": "gender",
                "value": gender
            },
            "collection": {
                "name": "Zuri NFT Tickets for Free Lunch",
                "id": "b774f676-c1d5-422e-beed-00ef5510c64d",
                "attributes": [
                    {
                        "type": "description",
                        "value": "Rewards for accomplishments during HNGi9."
                    }
                ]
            }
        }
        jsonName = f"{fileName}.json"
        with open(jsonName, "w") as outfile:
            json.dump(toJson, outfile)
        sha256_hash = hashlib.sha256()
        hash = ""
        with open(jsonName, "rb") as f:
            for byte_block in iter(lambda: f.read(4096), b""):
                sha256_hash.update(byte_block)
            hash = sha256_hash.hexdigest()
        row.append(hash)
        rows.append(row)

        oldFileName = os.path.splitext(filename)[0]
        newFileName = f"{oldFileName}.output.csv"
        with open(newFileName, "w") as csvfile:
            csvwriter = csv.writer(csvfile)

            #csvwriter.writerow(first)

            csvwriter.writerow(header)

            for row in rows:
            	csvwriter.writerow(row)

print("Done")


import torch
import albumentations as A
from albumentations.pytorch import ToTensorV2
import os

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

TRAIN_DIR = os.path.join("data", "train")
TRAIN_CARTOON_DIR = os.path.join(TRAIN_DIR, "cartoon")
TRAIN_CARTOON_EDGE_DIR = os.path.join(TRAIN_DIR, "cartoon_edge_pair")
TRAIN_PHOTO_DIR = os.path.join(TRAIN_DIR, "photo")

VAL_DIR = os.path.join("data", "val")
VAL_PHOTO_DIR = os.path.join(VAL_DIR, "photo")

RESULT_TRAIN_DIR = os.path.join("results", "train")
RESULT_TEST_DIR = os.path.join("results", "test")

BATCH_SIZE = 1
LEARNING_RATE = 2e-4

LAMBDA_ADV = 1
LAMBDA_CONTENT = 10

NUM_WORKERS = 4
IMAGE_SIZE = 256

PRETRAIN_EPOCHS = 10
NUM_EPOCHS = 100

LOAD_MODEL = True
SAVE_MODEL = True
SAVE_MODEL_FREQ = 5

CHECKPOINT_FOLDER = "checkpoints"
CHECKPOINT_DISC = "disc.pth.tar"
CHECKPOINT_GEN = "gen.pth.tar"
VGG_WEIGHTS = "vgg19-dcbb9e9d.pth"

LOAD_CHECKPOINT_DISC = "i_disc.pth.tar"
LOAD_CHECKPOINT_GEN = "i_gen.pth.tar"

SAVE_IMG_PER_STEP = 200

transform_cartoon_pairs = A.Compose(
    #additional_targets: apply same augmentation on both images
    [
        #A.RandomCrop(width=IMAGE_SIZE*1.2, height=IMAGE_SIZE*1.2),
        A.Resize(width=IMAGE_SIZE, height=IMAGE_SIZE),
        #A.HorizontalFlip(p=0.5),
        A.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5], max_pixel_value=255),
        ToTensorV2(),
    ],
    additional_targets={"image0": "image"},
)

transform_input = A.Compose(
    [
        A.Resize(width=IMAGE_SIZE, height=IMAGE_SIZE),
        #A.HorizontalFlip(p=0.5),
        #A.RandomBrightnessContrast(p=0.2),
        A.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5], max_pixel_value=255),
        ToTensorV2(),
    ]
)

transform_test = A.Compose(
    [
        A.Resize(width=IMAGE_SIZE, height=IMAGE_SIZE),
        A.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5], max_pixel_value=255),
        ToTensorV2(),
    ]
)


import numpy as np
import scipy as sc
import matplotlib.pyplot as plt
import sys
from utilities import EDM, mask, error, add_error
from functional import cost_function,gradient,hessian
from pointset import generate, update
from reconstruct import rec

"""
Simulation of 10 points moving in a square of side 10.m with 1 anchor at eanch
vertex. The aim of the simulation is to estimate for each step the point
configuration. As initial guess I've used the result of the estimation of the
previous one.
"""

n_step = 20 #number of step
shape = (2,10) # matrix containing the position of the 10 sensor in 2-dim
side = 10. #side of the square in which the sensors are generated

OR = np.zeros((2,14), dtype = np.float64) #original point set
PR = np.zeros((2,14), dtype = np.float64) #estimated point set
RME = np.zeros(n_step, dtype = np.float64)#relative mean error

#define the anchors position, which is fixed and known
A = np.array([[0.,0.,10.,10.],[0.,10.,0.,10.]], dtype = np.float64)

#generate the initial point configuration
TAG = generate(shape,side)

#Initzialize original and reconstructed pointset
OR[:,0:4] = A
OR[:,4:]  = TAG
PR[:,0:4] = A
PR[:,4:]  = TAG

#compute the EDM and add error
D = EDM(OR)#
E = error(2.,(14,14))
D = add_error(D,E)
#define the mask. Only distances between sensors are missing
W = np.ones((14,14),dtype = np.float64)
W[4:,4:] = np.zeros((10,10), dtype = np.float64)

my_dpi = 96#constant for figure size

#init the progress bar
toolbar_width = n_step
sys.stdout.write("[%s]" % (" " * toolbar_width))
sys.stdout.flush()
sys.stdout.write("\b" * (toolbar_width+1)) # return to start of line, after

#start the simulatin
for i in range(n_step):

    #store the old pointset
    P0 = PR
    #update the current pointset
    OR = update(OR,1.)
    OR[:,:4] = A #enforce anchor locations
    #compute the new edm
    D = EDM(OR)
    E = error(2.,(14,14))
    D = add_error(D,E)

    #reconstruct the pointset
    PR = rec(P0,D,W,0.0001)
    #enforce anchor locations
    PR[:,:4] = A

    #conpute the RME
    RME[i] += np.linalg.norm(OR - PR)/np.linalg.norm(OR)

    #save the results
    fig = plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
    plt.scatter(OR[0],OR[1], c = "b")
    plt.scatter(PR[0,:4], PR[1,:4], c="g", marker = "s")
    plt.scatter(PR[0,4:], P0[1,4:], c ="r")
    plt.xlim(-3, 15)
    plt.ylim(-3, 15)
    plt.gca().set_title("step "+str(i))
    plt.gca().set_xlabel("x(m)")
    plt.gca().set_ylabel("y(m)")
    plt.legend(['Actual','Anchors', 'Estimated'])
    filename='step'+str(i)+'.png'
    plt.savefig(filename, dpi=96)
    plt.gca()

    sys.stdout.write("-")
    sys.stdout.flush()

np.savetxt("RME.txt", RME)


#encoding=utf-8

from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.db.models.signals import post_save

if "mailer" in settings.INSTALLED_APPS:
    from mailer import send_mail
    MAILER_INSTALLED = True
else:
    from django.core.mail import send_mass_mail
    MAILER_INSTALLED = False

PRIORITIES = (
    ("high", _("high")),
    ("medium",_("medium")),
    ("low", _("low")),
    ("deferred", _("deferred")),
)

class MailMessage(models.Model):
    subject = models.CharField(_('subject'),max_length=200)
    recipients = models.TextField(_('recipients'),help_text=_('separate receipient addresses by comma.'))
    message = models.TextField(_('message'))
    from_email = models.EmailField(_('from_email'),blank=True,null=True)
    priority = models.CharField(max_length=10, choices=PRIORITIES, default="medium")
    create_at = models.DateTimeField(_('create_at'),auto_now_add=True)
    user = models.ForeignKey(User,verbose_name=_('user'),null=True)

    class Meta:
        verbose_name = _('Mail Message')
        verbose_name_plural = _('Mail Messages')

    def __unicode__(self):
        return self.subject

def do_send_email(sender,instance,created,**kwargs):
    if not created:
        return
    # send emails
    if MAILER_INSTALLED:
        # send email by mailer
        for address in instance.recipients.split(','):
            send_mail(instance.subject,instance.message,instance.from_email or None,[address],instance.priority)
        if not getattr(settings,'MAILER_DAEMON_RUNNING',False):
            # if there is not a mailer cron job runnig,invoke the send_all() function to send mails.
            from mailer.engine import send_all
            send_all()
    else:
        # send mail by django
        data = []
        for address in instance.recipients.split(','):
            data.append((instance.subject,instance.message,instance.from_email or None,[address]))
        send_mass_mail(data,True)

post_save.connect(do_send_email,sender=MailMessage,dispatch_uid='send_email_listener')


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import math
from functools import partial

nonlinearity = partial(F.relu, inplace=True)
from torch.nn import init
from collections import OrderedDict

def weights_init_normal(m):
    classname = m.__class__.__name__
    # print(classname)
    if classname.find('Conv') != -1:
        init.normal_(m.weight.data, 0.0, 0.02)
    elif classname.find('Linear') != -1:
        init.normal_(m.weight.data, 0.0, 0.02)
    elif classname.find('BatchNorm') != -1:
        init.normal_(m.weight.data, 1.0, 0.02)
        init.constant_(m.bias.data, 0.0)

def weights_init_xavier(m):
    classname = m.__class__.__name__
    # print(classname)
    if classname.find('Conv') != -1:
        init.xavier_normal_(m.weight.data, gain=1)
    elif classname.find('Linear') != -1:
        init.xavier_normal_(m.weight.data, gain=1)
    elif classname.find('BatchNorm') != -1:
        init.normal_(m.weight.data, 1.0, 0.02)
        init.constant_(m.bias.data, 0.0)

def weights_init_kaiming(m):
    classname = m.__class__.__name__
    # print(classname)
    if classname.find('Conv') != -1:
        init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
    elif classname.find('Linear') != -1:
        init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
    elif classname.find('BatchNorm') != -1:
        init.normal_(m.weight.data, 1.0, 0.02)
        init.constant_(m.bias.data, 0.0)

def weights_init_orthogonal(m):
    classname = m.__class__.__name__
    # print(classname)
    if classname.find('Conv') != -1:
        init.orthogonal_(m.weight.data, gain=1)
    elif classname.find('Linear') != -1:
        init.orthogonal_(m.weight.data, gain=1)
    elif classname.find('BatchNorm') != -1:
        init.normal_(m.weight.data, 1.0, 0.02)
        init.constant_(m.bias.data, 0.0)

def init_weights(net, init_type='normal'):
    # print('initialization method [%s]' % init_type)
    if init_type == 'normal':
        net.apply(weights_init_normal)
    elif init_type == 'xavier':
        net.apply(weights_init_xavier)
    elif init_type == 'kaiming':
        net.apply(weights_init_kaiming)
    elif init_type == 'orthogonal':
        net.apply(weights_init_orthogonal)
    else:
        raise NotImplementedError('initialization method [%s] is not implemented' % init_type)

class Basconv(nn.Sequential):
    def __init__(self, in_channels, out_channels, is_batchnorm=False, kernel_size=3, stride=1, padding=1):
        super(Basconv, self).__init__()
        if is_batchnorm:
            self.conv = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding),
                                      nn.BatchNorm2d(out_channels), nn.ReLU(inplace=True))
        else:
            self.conv = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding),
                                      nn.ReLU(inplace=True))

        # initialise the blocks
        for m in self.children():
            init_weights(m, init_type='kaiming')

    def forward(self, inputs):
        x = inputs
        x = self.conv(x)
        return x

class GCN(nn.Module):
    def __init__(self, num_state, num_node, bias=False):
        super(GCN, self).__init__()
        self.conv1 = nn.Conv1d(num_node, num_node, kernel_size=1, padding=0,
                               stride=1, groups=1, bias=True)
        self.relu = nn.LeakyReLU(0.2, inplace=True)
        self.conv2 = nn.Conv1d(num_state, num_state, kernel_size=1, padding=0,
                               stride=1, groups=1, bias=bias)

    def forward(self, x):
        h = self.conv1(x.permute(0, 2, 1).contiguous()).permute(0, 2, 1)
        h = h + x
        h = self.relu(h)
        h = self.conv2(h)
        return h

class GloRe_Unit(nn.Module):

    def __init__(self, num_in, num_mid, stride=(1, 1), kernel=1):
        super(GloRe_Unit, self).__init__()

        self.num_s = int(2 * num_mid)
        self.num_n = int(1 * num_mid)
        kernel_size = (kernel, kernel)
        padding = (1, 1) if kernel == 3 else (0, 0)
        # reduce dimension
        self.conv_state = Basconv(num_in, self.num_s, is_batchnorm=True, kernel_size=kernel_size, padding=padding)
        # generate projection and inverse projection functions
        self.conv_proj = Basconv(num_in, self.num_n, is_batchnorm=True, kernel_size=kernel_size, padding=padding)
        self.conv_reproj = Basconv(num_in, self.num_n, is_batchnorm=True, kernel_size=kernel_size, padding=padding)
        # reasoning by graph convolution
        self.gcn1 = GCN(num_state=self.num_s, num_node=self.num_n)
        self.gcn2 = GCN(num_state=self.num_s, num_node=self.num_n)
        # fusion
        self.fc_2 = nn.Conv2d(self.num_s, num_in, kernel_size=kernel_size, padding=padding, stride=(1, 1),
                              groups=1, bias=False)
        self.blocker = nn.BatchNorm2d(num_in)

    def forward(self, x):
        batch_size = x.size(0)
        # generate projection and inverse projection matrices
        x_state_reshaped = self.conv_state(x).view(batch_size, self.num_s, -1)
        x_proj_reshaped = self.conv_proj(x).view(batch_size, self.num_n, -1)
        x_rproj_reshaped = self.conv_reproj(x).view(batch_size, self.num_n, -1)
        # project to node space
        x_n_state1 = torch.bmm(x_state_reshaped, x_proj_reshaped.permute(0, 2, 1))
        x_n_state2 = x_n_state1 * (1. / x_state_reshaped.size(2))
        # graph convolution
        x_n_rel1 = self.gcn1(x_n_state2)
        x_n_rel2 = self.gcn2(x_n_rel1)
        # inverse project to original space
        x_state_reshaped = torch.bmm(x_n_rel2, x_rproj_reshaped)
        x_state = x_state_reshaped.view(batch_size, self.num_s, *x.size()[2:])
        # fusion
        out = x + self.blocker(self.fc_2(x_state))

        return out

class MGR_Module(nn.Module):
    def __init__(self, in_channels, out_channels):
        super(MGR_Module, self).__init__()

        self.conv0_1 = Basconv(in_channels=in_channels, out_channels=out_channels, kernel_size=3, padding=1)
        self.glou0 = nn.Sequential(
            OrderedDict([("GCN%02d" % i, GloRe_Unit(out_channels, out_channels, kernel=1)) for i in range(1)]))

        # self.conv1_1 = Basconv(in_channels=in_channels, out_channels=out_channels, kernel_size=3, padding=1)
        # self.pool1 = nn.MaxPool2d(kernel_size=[2, 2], stride=2)
        # self.conv1_2 = Basconv(in_channels=out_channels, out_channels=out_channels, kernel_size=3, padding=1)
        # self.glou1 = nn.Sequential(
        #     OrderedDict([("GCN%02d" % i, GloRe_Unit(out_channels, out_channels, kernel=1)) for i in range(1)]))
        #
        # self.conv2_1 = Basconv(in_channels=in_channels, out_channels=out_channels, kernel_size=3, padding=1)
        # self.pool2 = nn.MaxPool2d(kernel_size=[3, 3], stride=3)
        # self.conv2_2 = Basconv(in_channels=out_channels, out_channels=out_channels, kernel_size=3, padding=1)
        # self.glou2 = nn.Sequential(
        #     OrderedDict([("GCN%02d" % i, GloRe_Unit(out_channels, int(out_channels / 2), kernel=1)) for i in range(1)]))
        #
        # self.conv3_1 = Basconv(in_channels=in_channels, out_channels=out_channels, kernel_size=3, padding=1)
        # self.pool3 = nn.MaxPool2d(kernel_size=[5, 5], stride=5)
        # self.conv3_2 = Basconv(in_channels=out_channels, out_channels=out_channels, kernel_size=3, padding=1)
        # self.glou3 = nn.Sequential(
        #     OrderedDict([("GCN%02d" % i, GloRe_Unit(out_channels, int(out_channels / 2), kernel=1)) for i in range(1)]))
        #
        # self.f1 = Basconv(in_channels=4 * out_channels, out_channels=in_channels, kernel_size=1, padding=0)

    def forward(self, x):
        self.in_channels, h, w = x.size(1), x.size(2), x.size(3)

        x0 = self.conv0_1(x)
        g0 = self.glou0(x0)

        return g0#self.f1(out)


from django.template.loader import render_to_string
from django.core.exceptions import ObjectDoesNotExist
from django.core import serializers

from dajax.core import Dajax
from dajaxice.decorators import dajaxice_register

from models import Area, Location, SubLocation, Inscription

MAX_LOCATIONS_ON_INDEX = 15

def get_area_and_locations(area_id):
    area = None
    locations = None

    if area_id is not None:
        id = int(area_id)
        try:
            area = Area.objects.get(id=id)
            if 1 != id:
                locations = area.location_set.all()
            else:
                # randomly get locations at most
                locations = Location.objects.all().order_by('?')
                locations = locations[:MAX_LOCATIONS_ON_INDEX]

        except ObjectDoesNotExist:
            area = None

        return [area, locations]

@dajaxice_register
def display_area_detail(request, area_id):
    area, locations = get_area_and_locations(area_id)

    dajax = Dajax()
    if locations is not None:
        data = serializers.serialize("json", locations)
        dajax.add_data(data, 'area_detail_add_locations')
        dajax.add_data(None, 'zoom_to_show_all_markers')

    return dajax.json()

@dajaxice_register
def display_area(request, area_id):
    area, locations = get_area_and_locations(area_id)

    render_area = render_to_string('inscription/intra_area_info.html',
                                   {'area': area})
    render_photos = render_to_string('inscription/intra_photos.html',
                                     {'photos': area.photos.all()})
    dajax = Dajax()
    if locations is not None:
        data = serializers.serialize("json", locations)
        dajax.add_data(data, 'add_locations')
        dajax.add_data(None, 'zoom_to_show_all_markers')
    dajax.assign('#area_or_location_info', 'innerHTML', render_area)
    dajax.assign('#photos_or_sublocation_info', 'innerHTML', render_photos)

    return dajax.json()

@dajaxice_register
def display_location(request, location_id):
    id = int(location_id)
    location = None

    try:
        location = Location.objects.get(id=id)
    except ObjectDoesNotExist:
        location = None

    render_location = render_to_string('inscription/intra_location_info.html',
                                       {'location': location})
    render_photos = render_to_string('inscription/intra_photos.html',
                                     {'photos': location.photos.all()})

    dajax = Dajax()
    dajax.assign('#area_or_location_info', 'innerHTML', render_location)
    dajax.assign('#photos_or_sublocation_info', 'innerHTML', render_photos)

    return dajax.json()

@dajaxice_register
def display_sublocation(request, sublocation_id):
    id = int(sublocation_id)
    sublocation = None

    try:
        sublocation = SubLocation.objects.get(id=id)
    except ObjectDoesNotExist:
        sublocation = None

    render = render_to_string('inscription/intra_sublocation_info.html',
                              {'sublocation': sublocation})

    dajax = Dajax()
    dajax.assign('#photos_or_sublocation_info', 'innerHTML', render)
    return dajax.json()

@dajaxice_register
def display_inscription(request, inscription_id):
    id = int(inscription_id)
    inscription = None

    try:
        inscription = Inscription.objects.prefetch_related('photos').get(id=id)
    except ObjectDoesNotExist:
        inscription = None

    render = render_to_string('inscription/intra_inscription_info.html',
                              {'inscription': inscription})

    dajax = Dajax()
    dajax.assign('#inscription_info', 'innerHTML', render)
    return dajax.json()


from selenium import webdriver

from src.datapullers.DataExtractor import DataExtractor
from src.pages.FilterMenu import FilterMenu

class SalesDataExtractor(DataExtractor):
    def __init__(self, driver: webdriver, download_directory: str):
        super().__init__(driver, download_directory)

    def get_data(self, search_criteria: str, home_types: list, sold_within: str):
        super().go_to_homepage()
        super().search_location(search_criteria)

        filter_menu = FilterMenu(self.driver)
        filter_menu.open_menu()
        filter_menu.select_sold_data()
        filter_menu.select_home_types(home_types)
        filter_menu.select_sold_within(sold_within)
        filter_menu.select_foreclosures_checkbox()
        filter_menu.close_menu()

        data = super().read_data()
        return data


import os
from typing import Optional

from src.analyzer import Analyzer
from src.currency_exchange import Exchanger
from src.data_collector import DataCollector
from src.parser import Settings
from src.predictor import Predictor

CACHE_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "cache")
SETTINGS_PATH = "settings.json"

class ResearcherHH:
    """Main class for searching vacancies and analyze them."""

    def __init__(self, config_path: str = SETTINGS_PATH, no_parse: bool = False):
        self.settings = Settings(config_path, no_parse=no_parse)
        self.exchanger = Exchanger(config_path)
        self.collector: Optional[DataCollector] = None
        self.analyzer: Optional[Analyzer] = None
        self.predictor = Predictor()

    def update(self, **kwargs):
        self.settings.update_params(**kwargs)
        if not any(self.settings.rates.values()) or self.settings.update:
            print("[INFO]: Trying to get exchange rates from remote server...")
            self.exchanger.update_exchange_rates(self.settings.rates)
            self.exchanger.save_rates(self.settings.rates)

        print(f"[INFO]: Get exchange rates: {self.settings.rates}")
        self.collector = DataCollector(self.settings.rates)
        self.analyzer = Analyzer(self.settings.save_result)

    def __call__(self):
        print("[INFO]: Collect data from JSON. Create list of vacancies...")
        vacancies = self.collector.collect_vacancies(
            query=self.settings.options, refresh=self.settings.refresh, num_workers=self.settings.num_workers
        )
        print("[INFO]: Prepare dataframe...")
        df = self.analyzer.prepare_df(vacancies)
        print("\n[INFO]: Analyze dataframe...")
        self.analyzer.analyze_df(df)
        print("\n[INFO]: Predict None salaries...")
        # total_df = self.predictor.predict(df)
        # self.predictor.plot_results(total_df)
        print("[INFO]: Done! Exit()")

if __name__ == "__main__":
    hh_analyzer = ResearcherHH()
    hh_analyzer.update()
    hh_analyzer()


import pygame
import random

windowSize = (1200, 850)
ticks = 100
foodPerRound = 15
ballsInFirstGeneration = 10
foodSize = 5
stepLambda = lambda ball: 2 - ball.size/64
visionLambda = lambda ball: 90 + ball.size*4

pygame.init()

clock = pygame.time.Clock()

screen = pygame.display.set_mode(windowSize)
pygame.display.set_caption("Genetic Balls")
font = pygame.font.Font('freesansbold.ttf', 15)

class Ball:
    def __init__(self, color, size, invDirectionChangeChance):
        self.x = windowSize[0]/2
        self.y = windowSize[1]/2
        self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) if color == None else color
        self.size = random.randint(5, 50) if size == None else size
        self.direction = random.randint(0, 7)
        self.invDirectionChangeChance = random.randint(1,200) if invDirectionChangeChance == None else invDirectionChangeChance
        self.foodEaten = 0

        self.step = stepLambda(self)
        self.vision = visionLambda(self)

    def getRandomBall():
        return Ball((0, 0, 255), None, None)

    def getFitness(self):
        return self.foodEaten

    def reproduce(self, secondParent):
        color = self.color if random.randint(0,1)==0 else secondParent.color
        size = self.size if random.randint(0,1)==0 else secondParent.size
        invDirectionChangeChance = self.invDirectionChangeChance if random.randint(0,1)==0 else secondParent.invDirectionChangeChance

        babyBall = Ball(color, size, invDirectionChangeChance)
        return self.mutate(babyBall)

    def mutate(self, ball):
        if random.randint(0,10) == 0:
            ball.color = (random.randint(10, 255), random.randint(10, 255), random.randint(10, 255))
            ball.size += random.normalvariate(0,15)
            ball.size = ball.size if ball.size > 1 else 1
            self.step = stepLambda(self)
            self.vision = visionLambda(self)

        if random.randint(0,10) == 0:
            ball.color = (random.randint(10, 255), random.randint(10, 255), random.randint(10, 255))
            ball.invDirectionChangeChance += random.normalvariate(0,20)
            ball.invDirectionChangeChance = ball.invDirectionChangeChance if ball.invDirectionChangeChance > 0 else 1

        return ball

    # 4 3 2
    # 5   1
    # 6 7 0
    def goInDirection(self, direction):
        if 0 <= direction <= 2:
            self.x += self.step
        elif 4 <= direction <= 6:
            self.x -= self.step
        if 2 <= direction <= 4:
            self.y += self.step
        elif 6 <= direction <= 7 or direction == 0:
            self.y -= self.step

    def isInside(self):
        return self.x >= 0 and self.x <= windowSize[0] and self.y >= 0 and self.y <= windowSize[1]

    def goBackIn(self):
        if self.x < 0:
            self.x = 0
        elif self.x > windowSize[0]:
            self.x = windowSize[0]
        if self.y < 0:
            self.y = 0
        elif self.y > windowSize[1]:
            self.y = windowSize[1]

class Food:
    def __init__(self):
        self.x = random.randint(0, windowSize[0])
        self.y = random.randint(0, windowSize[1])

class Round:
    def __init__(self, foodCount, balls, generation, board):
        self.foodList = []
        self.balls = balls
        self.generation = generation
        self.board = board

        self.foodList = [Food() for _ in range(0,foodCount)]

    def nextStep(self):
        for ball in self.balls:
            closestFood = lambda food: ((food.x - ball.x)**2 + (food.y - ball.y)**2)**0.5
            self.foodList.sort(key=closestFood)

            if ball.x - ball.size - foodSize/2 <= self.foodList[0].x <= ball.x + ball.size + foodSize/2 and ball.y - ball.size - foodSize/2 <= self.foodList[0].y <= ball.y + ball.size + foodSize/2:
                ball.foodEaten += 1
                self.foodList.remove(self.foodList[0])

            if len(self.foodList) == 0:
                return Round(foodPerRound, self.nextGeneration(), self.generation+1, self.board)

            if ((self.foodList[0].x - ball.x)**2 + (self.foodList[0].y - ball.y)**2)**0.5 > ball.vision:
                if random.randint(0,int(ball.invDirectionChangeChance)) == 0:
                    ball.direction += random.randint(-1,1)
                    ball.direction %= 8

                ball.goInDirection(ball.direction)

                if not ball.isInside():
                    ball.goBackIn()
                    ball.direction += random.randint(-1,1)
                    ball.direction %= 8
                    ball.goInDirection(ball.direction)

            else:
                if self.foodList[0].x > ball.x:
                    ball.x += ball.step
                elif self.foodList[0].x < ball.x:
                    ball.x -= ball.step
                if self.foodList[0].y > ball.y:
                    ball.y += ball.step
                elif self.foodList[0].y < ball.y:
                    ball.y -= ball.step

        self.board.draw(self)
        return self

    def nextGeneration(self)->Ball:
        newBalls = []
        self.orderBalls(lambda ball: ball.getFitness(), True)

        isSecondParent = False
        for index,ball in enumerate(self.balls):
            if ball.getFitness() == 0:
                return newBalls

            newBalls.append(Ball(ball.color, ball.size, ball.invDirectionChangeChance))
            if isSecondParent:
                newBalls.append(ball.reproduce(self.balls[index-1]))
                isSecondParent = False
            else:
                isSecondParent = True
        return newBalls

    def orderBalls(self, key,reverse):
        self.balls.sort(key=key, reverse=reverse)

class Board:
    def draw(self, round: Round):
        screen.fill((0, 0, 0))

        for ball in round.balls:
            pygame.draw.circle(screen, ball.color, (ball.x, ball.y), ball.size)
            pygame.draw.ellipse(screen, (60, 60, 60), (ball.x - ball.vision, ball.y - ball.vision, ball.vision*2, ball.vision*2), 1)

        for food in round.foodList:
            pygame.draw.circle(screen, (255, 0, 0), (food.x, food.y), 5)

        board.drawText("Generation: " + str(round.generation))
        board.drawText("No balls: " + str(len(round.balls)), y=30)
        board.drawText("Avg size: " + str(sum(b.size for b in round.balls)/len(round.balls))[:6],y=50)
        board.drawText("Avg invDirChange: " + str(sum(b.invDirectionChangeChance for b in round.balls)/len(round.balls))[:6],y=70)

        board.drawText("Ticks: " + str(ticks), x=windowSize[0]-90)

        pygame.display.flip()

    def drawText(self,text,x=10,y=10):
        textRen = font.render(text, True, (255, 255, 255))
        screen.blit(textRen, (x, y))

def handleEvents(ticks):
    running = True
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
            ticks += 100
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
            ticks -= 100
    return running, ticks

if __name__ == "__main__":
    balls = [Ball.getRandomBall() for i in range(0, ballsInFirstGeneration)]
    board = Board()
    round = Round(foodPerRound, balls, 1, board)

    running = True
    while running:
        running, ticks = handleEvents(ticks)
        round = round.nextStep()
        clock.tick(ticks)
    pygame.quit()


from common.log import logger
from datahub.databus.shippers.factory import ShipperFactory
from datahub.databus.task import clean, task
from datahub.databus.task.puller_task import process_puller_task
from datahub.databus.task.pulsar import task as pulsar_task
from datahub.databus.task.pulsar import topic as pulsar_topic
from datahub.databus.task.pulsar.topic import get_pulsar_channel_token
from datahub.databus.task.task_utils import (
    generate_connector_name,
    generate_puller_connector_name,
    get_available_puller_cluster_name,
)

from datahub.databus import model_manager, rawdata, rt, settings

def topic_to_partition_topic(raw_data_id, component):
    """
    拉取任务迁移脚本, 从kafka迁移到pulsar
    :param raw_data_id: raw_data_id
    :param component: 组件
    """
    raw_data = model_manager.get_raw_data_by_id(raw_data_id, False)
    channel_info = model_manager.get_channel_by_id(raw_data.storage_channel_id, False)
    channel_host = "{}:{}".format(channel_info.cluster_domain, channel_info.cluster_port)
    topic = rawdata.get_topic_name(raw_data)

    if pulsar_topic.exist_partition_topic(channel_host, topic):
        logger.info("ignore partition_topic %s" % topic)
        return

    # stop puller
    task_name = generate_puller_connector_name(component, raw_data_id)
    logger.info("stop puller %s" % raw_data_id)
    cluster_name = get_available_puller_cluster_name(channel_info.cluster_type, component)
    pulsar_task.delete_task(cluster_name, task_name)

    # stop clean and eslog
    clean_infos = model_manager.get_clean_by_raw_data_id(raw_data_id)
    for clean_info in clean_infos:
        rt_id = clean_info.processing_id
        rt_info = rt.get_databus_rt_info(rt_id)

        logger.info("stop clean %s" % rt_id)
        task.stop_databus_task(rt_info, ["kafka"])
        task_name = generate_connector_name(settings.COMPONENT_CLEAN, rt_id)
        task_info = model_manager.get_connector_route(task_name)
        if task_info:
            pulsar_task.delete_task(task_info.cluster_name, task_name)

        # stop eslog
        task_name = generate_connector_name(settings.COMPONENT_ESLOG, rt_id)
        task_info = model_manager.get_connector_route(task_name)
        if task_info:
            logger.info("stop eslog %s" % rt_id)
            task.stop_databus_task(rt_info, ["es"])
            pulsar_task.delete_task(task_info.cluster_name, task_name)

    # rm topic
    logger.info("delete %s" % topic)
    pulsar_topic.delete_topic(channel_host, topic, get_pulsar_channel_token(channel_info))

    # create new topic
    logger.info("create %s" % topic)
    pulsar_topic.create_partition_topic(channel_host, topic, 3, get_pulsar_channel_token(channel_info))

    # start puller
    logger.info("start http puller %s" % raw_data_id)
    process_puller_task(raw_data_id)

    # start clean
    for clean_info in clean_infos:
        rt_id = clean_info.processing_id
        logger.info("start pulsar clean %s" % rt_id)
        rt_info = rt.get_databus_rt_info(rt_id)
        clean.process_clean_task_in_kafka(rt_info)

        # start eslog
        task_name = generate_connector_name(settings.COMPONENT_ESLOG, rt_id)
        task_info = model_manager.get_connector_route(task_name)
        if task_info:
            logger.info("start pulsar eslog %s" % rt_id)
            shipper_task = ShipperFactory.get_shipper_factory().get_shipper("eslog")(rt_id, "admin", "")
            shipper_task.start()