from __future__ import print_function
from __future__ import division

import gym
import time
import random
import argparse
import numpy as np
import tensorflow as tf
from collections import deque

class Memory(object):
    def __init__(self, capacity, dims):
        self.capacity = capacity
        self.data = np.zeros((capacity, dims))
        self.pointer = 0

    def store_transition(self, currentState, action, reward, nextState):
        transition = np.hstack((currentState, action, [reward], nextState))
        index = self.pointer % self.capacity  # replace the old memory with new memory
        self.data[index, :] = transition
        self.pointer += 1

    def sample(self, num):
        assert self.pointer >= self.capacity, 'Memory has not been fulfilled'
        indices = np.random.choice(self.capacity, size=num)
        return self.data[indices, :]

class DDPG(object):
    def __init__(self, env, args):
        """
        初始化函数

        :param env: 环境
        :param args: 变量列表
        """
        # 初始化环境的参数
        self.statesNum = env.observation_space.shape[0]
        self.action_Num = env.action_space.shape[0]
        self.action_bound = env.action_space.high
        self.discount_rate = args.discount_rate

        # 初始化神经网络参数
        self.actor_lr = args.actor_learning_rate
        self.critic_lr = args.critic_learning_rate
        self.save_path = args.save_path
        self.batchSize = args.batch_size

        # 其他变量
        self.TAU = args.TAU  # soft replacement
        self.experienceReplayBuffer = deque(maxlen=args.buffer_size)
        self.pointer = 0

    def actor_network(self, state, scope, hidden_units=30, trainable=True):
        """
        构建actor神经网络, 用于计算当前状态下,每个行为的可能概率

        :param state: agent所处的state
        :param trainable:是否可以重复训练
        :return:
        """

        with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
            # 权重和偏置项初始化
            init_weight = tf.random_normal_initializer(0.0, 0.3)
            init_biases = tf.constant_initializer(0.1)

            # 网络第一层
            layer1 = tf.layers.dense(inputs=state,
                                     units=hidden_units,
                                     activation=tf.nn.relu,
                                     kernel_initializer=init_weight,
                                     bias_initializer=init_biases,
                                     trainable=trainable,
                                     name="network_layer1")
            # 网络输出层
            action_prob = tf.layers.dense(inputs=layer1,
                                          units=self.action_Num,
                                          activation=tf.nn.tanh,
                                          kernel_initializer=init_weight,
                                          bias_initializer=init_biases,
                                          trainable=trainable,
                                          name="action_prob")

            # 根据行为的概率,对action的值进行修整
            action_value = tf.multiply(action_prob, self.action_bound, name="scaled_action_value")

            # 返回结果
            return action_value

    def critic_network(self, state, action, scope, hidden_units=30, trainable=True):
        """
        构建critic神经网络, 用于评估当前状态下,采取当前行为的好坏

        :param state: agent所处的state
        :param trainable:是否可以重复训练
        :return:
        """
        with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
            # 权重和偏置项初始化
            init_weight = tf.random_normal_initializer(0.0, 0.1)
            init_biases = tf.constant_initializer(0.1)

            # 第一层网络
            state_weight = tf.get_variable(name='state_weight', shape=[self.statesNum, hidden_units], initializer=init_weight, trainable=trainable)
            action_weight = tf.get_variable(name='action_weight', shape=[self.action_Num, hidden_units], initializer=init_weight, trainable=trainable)
            biases = tf.get_variable(name='biases', shape=[1, hidden_units], initializer=init_biases, trainable=trainable)
            layer1 = tf.nn.relu(tf.matmul(state, state_weight) + tf.matmul(tf.reshape(action, [-1,1]), action_weight) + biases)

            # 网络输出层
            critic_ouput = tf.layers.dense(inputs=layer1,
                                          units=1,
                                          trainable=trainable,
                                          name="critic_ouput")

            # 返回结果
            return critic_ouput

    def model(self, gpu=-1):
        """
        构建深度学习模型

        :param gpu:是否使用GPU进行训练。默认值为-1，表示仅使用CPU进行训练
        :return: None
        """
        # 1) 进行session参数配置：是否使用GPU进行模型训练
        if gpu == -1:
            device = "/cpu:0"
            sessionConfig = tf.ConfigProto()
        else:
            device = "/gpu:" + str(gpu)
            sessionConfig = tf.ConfigProto(log_device_placement=True,
                                           allow_soft_placement=True)
            sessionConfig.gpu_options.allow_growth = True  # 控制GPU资源使用率， 允许后续按需慢慢增加GPU容量

        # 2) 定义session
        self.sess = tf.Session(config=sessionConfig)

        # 3) 定义模型的输入、输出、优化器、损失函数，并未各个operate指明命名空间
        with tf.device(device):   # 指明所用的设备（CPU或GPU）
            # 定义当前状态(currentState)的占位符
            with tf.name_scope("currentState"):
                self.currentState = tf.placeholder(dtype=tf.float32, shape=[None, self.statesNum], name="currentState")

            # 定义下一状态(nextState)的占位符
            with tf.name_scope("nextState"):
                self.nextState = tf.placeholder(dtype=tf.float32, shape=[None, self.statesNum], name="nextState")

            # 定义在当前状态下，采取行动，获取的reward
            with tf.name_scope("reward"):
                self.reward = tf.placeholder(dtype=tf.float32, shape=[None, 1], name="reward")

            # 定义Actor神经网络的输出， 根据输入的状态，预测状态的行为值
            with tf.variable_scope("Actor"):
                self.currentStateAction = self.actor_network(state=self.currentState, scope="currentStateAction", trainable=True)
                self.nextStateAction = self.actor_network(state=self.nextState, scope="nextStateAction", trainable=False)

            # 定义Critic神经网络的输出,根据输入的状态(state)和行为(action),评估当前状态下采取当前行为的好坏
            with tf.variable_scope("Critic"):
                self.currentStateActionValue = self.critic_network(state=self.currentState, action=self.currentStateAction, scope='currentStateActionValue', trainable=True)
                self.nextStateActionValue = self.critic_network(state=self.nextState, action=self.nextStateAction, scope='nextStateActionValue', trainable=False)

            # 训练动作估计网络和状态估计网络的参数，而动作现实网络和状态现实网络的参数是由前面两个网络每隔一定的时间复制过去的
            # 使用soft模式,更新神经网络的参数:采用的是一种'soft'模式的target-net网络参数更新，即每一步都对target-net网络中的参数更新一点点，
            # 这种参数更新方式经过试验表明可以大大的提高学习的稳定性
            # 1) 完成参数
            self.actor_eval_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Actor/currentStateAction')
            self.actor_target_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Actor/nextStateAction')
            self.critic_eval_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Critic/currentStateActionValue')
            self.critic_target_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Critic/nextStateActionValue')

            # 将动作估计网络和状态估计网络的参数复制给动作现实网络和状态现实网络
            # agent的Actor值跟新(使用actor_target_params更新actor_eval_params)
            with tf.variable_scope("actor_state_action_replace"):
                self.actor_soft_replace = [tf.assign(target, (1 - self.TAU) * target + self.TAU * eval)
                                     for target, eval in zip(self.actor_target_params, self.actor_eval_params)]

            # agent的Critic值跟新(使用critic_target_params更新critic_eval_params)
            with tf.variable_scope("critic_state_action_replace"):
                self.critic_soft_replace = [tf.assign(target, (1 - self.TAU) * target + self.TAU * eval)
                                     for target, eval in zip(self.critic_target_params, self.critic_eval_params)]

            # 计算Critic的TD_error
            with tf.variable_scope("Critic_TD_error"):
                self.Critic_TD_Error = self.reward + self.discount_rate * self.nextStateActionValue - self.currentStateActionValue

            # 定义Critic的损失函数
            with tf.name_scope("Critic_loss_Function"):
                self.Critic_loss = tf.reduce_mean(tf.square(self.Critic_TD_Error))

            # 定义Critic优化器
            with tf.name_scope("Critic_Training_Operation"):
                self.CriticTrainOperation = tf.train.AdamOptimizer(learning_rate=self.critic_lr).minimize(self.Critic_loss, var_list=self.critic_eval_params)

            # 定义Actor的损失函数
            with tf.name_scope("Actor_loss_Function"):
                self.actor_loss = tf.reduce_mean(-1 * self.currentStateActionValue)

            # 定义Actor的优化器
            with tf.name_scope("Actor_Training_Operation"):
                self.ActorTrainOperation = tf.train.AdamOptimizer(learning_rate=self.actor_lr).minimize(self.actor_loss, var_list=self.actor_eval_params)

            # Session初始化
            self.sess.run(tf.global_variables_initializer())

    def actionSample(self, state):
        """
        为当前state(状态)，计算当前状态行为的可能值

        :param state: agent当前处于的状态
        :return:agent在当前状态下的action值
        """
        action = self.sess.run(self.currentStateAction, feed_dict={self.currentState:state})
        return action

    def learning(self):
        """
        根据agent经历过的experience, 更新模型

        :return: 模型更新
        """
        # soft target replacement
        self.sess.run([self.actor_soft_replace, self.critic_soft_replace])

        # 当经验池中的experience个数达到batchSize的时候，进行模型更新
        if len(self.experienceReplayBuffer) >= self.batchSize:
            self.modelUpdate()

    def store_experience(self, experience):
        """

        :param experience: agent经历过的一个experience
        :return:
        """
        # 解析当前experience
        currentState, currentAction, reward, nextState = experience

        # 将experience存储到队列里面
        self.experienceReplayBuffer.append(np.array([currentState, currentAction, reward, nextState]))
        self.pointer += 1

    def modelUpdate(self):
        """
        当experienceReplayBuffer(经验池)中的经验数等于bathSize时，模型更新

        :return:
        """
        # experience采样， 从经验池中选取batchsize个experience
        minibatch = random.sample(population=self.experienceReplayBuffer, k=self.batchSize)

        # 将minibatch中的数据进行分解,分解为 curStateBatch, curActionBatch, rewardBatch, nextStateBatch
        curStateBatch, curActionBatch, rewardBatch, nextStateBatch = np.array(minibatch).T.tolist()

        # 模型更新
        self.sess.run([self.actor_soft_replace, self.critic_soft_replace])
        self.sess.run(self.ActorTrainOperation, {self.currentState: curStateBatch})
        self.sess.run(self.CriticTrainOperation, {self.currentState: curStateBatch,
                                                  self.currentStateAction: np.asarray(curActionBatch),
                                                  self.reward: np.asarray(rewardBatch).reshape([-1, 1]),
                                                  self.nextState: nextStateBatch})

###############################  training  ####################################
# 定义DDPG的参数的函数
def args_parse():
    # 参数解释器
    parser = argparse.ArgumentParser()

    # 模型保存路径， 默认存储路径为当前目录下的model文件夹
    parser.add_argument("--save_path",
                        default="./models/",
                        help="Path to save a model during training.")

    # GPU使用设置, 默认值为-1，即不使用任何GPU，仅使用CPU进行训练
    parser.add_argument("--gpu",
                        default=-1,
                        help="Runing on a specify GPU, -1 indicates using CPU.")

    # Critic的learning rate
    parser.add_argument("--discount_rate",
                        type=float,
                        default=0.9,
                        help="Discount Rate.")

    # Actor深度神经网络的learning rate
    parser.add_argument("--actor_learning_rate",
                        type=float,
                        default=0.001,
                        help="Actor Deep network Learning rate.")

    # Critic深度神经网络的learning rate
    parser.add_argument("--critic_learning_rate",
                        type=float,
                        default=0.002,
                        help="Critic Deep network Learning rate.")

    # 经验池的大小
    parser.add_argument("--buffer_size",
                        type=int,
                        default=10000,
                        help="Size of experience replay memory buffer.")

    # 深度网络的batchSize
    parser.add_argument("--batch_size",
                        type=int,
                        default=128,
                        help="Size of training batch."
                        )

    # TAU
    parser.add_argument("--TAU",
                        type=float,
                        default=0.01,
                        help="TAU."
                        )

    # 返回结果
    return parser.parse_args()

# 模型训练主函数
def main(args_parse):
    # 设定环境
    env = gym.make('Pendulum-v0')
    env = env.unwrapped

    # 设置显示信息
    MAX_EP_STEPS = 200
    RENDER = False
    DISPLAY_REWARD_THRESHOLD = -200

    # 设置行为噪声的variance
    variance = 3.0

    # 创建Actor的agent, 构建actor的model
    agent = DDPG(env, args_parse)
    agent.model(args_parse.gpu)

    for episode in range(10000):
        # 开始当前循环
        currentState = env.reset()
        episode_step = 0
        episode_return = []
        currentState = currentState[np.newaxis, :]

        # 循环迭代
        while True:
            if RENDER: env.render()

            # 使用Actor,获取当前状态下的一个行为
            action_value = agent.actionSample(currentState)
            currentAction = np.clip(np.random.normal(action_value, variance), -2, 2)  # add randomness to action selection for exploration
            nextState, reward, isTerminal, info = env.step(currentAction)

            # 经验整合
            episode_return.append(reward)
            experience = currentState[0], currentAction[0], reward / 10, nextState.reshape((1, 3))[0]
            agent.store_experience(experience)

            # 当经历的experience大于一定数值时, Agent的模型更新
            if agent.pointer > args_parse.buffer_size:
                variance *= .9995  # decay the action randomness
                agent.learning()
                # print(agent.pointer)

            currentState = nextState.reshape((1, 3))  # 进行格式转换，用于下一轮输入
            episode_step += 1

            if episode_step == MAX_EP_STEPS-1:
                if sum(episode_return) > DISPLAY_REWARD_THRESHOLD:
                    RENDER = True

                print("episode:", episode, "episode_step", episode_step, " reward: ", int(sum(episode_return)), 'Explore: %.2f' % variance)
                break

if __name__ == '__main__':
    main(args_parse())


#Program that utilizes Select module of selenium
#To receive a select element from the webpage
#By using Select("here you find the element by using driver.find_element")
#And then picks a certain option in by using
#select_object.select_by_value("value")
#where "value" is a result of addition of two numbers

import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

link = "http://suninjuly.github.io/selects1.html"
options = Options()
options.add_argument("--no-sandbox")
service = Service("/chromedriver/stable/chromedriver")
try:
    driver = webdriver.Chrome(options=options, service=service)
    driver.get(link)

    num1 = driver.find_element(By.ID,"num1")
    num2 = driver.find_element(By.ID,"num2")
    print(int(num1.text) + int(num2.text))

    answers = Select(driver.find_element(By.ID,"dropdown"))
    answers.select_by_value(str(int(num1.text) + int(num2.text)))

    submit = driver.find_element(By.TAG_NAME,"button")
    submit.click()
finally:
    time.sleep(4)
    driver.quit()


import sys
from shapely.wkt import loads
from shapely import geometry
class Node():
    def __init__(self,kwargs,row):
        keys=kwargs.keys()

        node_id=kwargs['node_id'] if 'node_id' in keys else ''
        if node_id:
            try:
                self.node_id=int(float(node_id))
            except Exception as e:
                print("broken at row{},".format(row),end=' ')
                print(e)
                sys.exit(0)
        else:
            print("node_id is not defined in node.csv, please check it!")
            sys.exit(0)

        ctrl_type=kwargs['ctrl_type'] if 'ctrl_type' in keys else ''
        try:
            self.ctrl_type=int(float(ctrl_type))
        except:
            self.ctrl_type=0
        self.activity_type = kwargs['activity_type'] if 'activity_type' in keys else 'unclassified'

        x_coord=kwargs['x_coord'] if 'x_coord' in keys else ''
        if x_coord:
            try:
                self.x_coord=float(x_coord)
            except Exception as e:
                print('broken at row {},'.format(row),end=' ')
                print(e)
                sys.exit(0)
        else:
            print("x_coord not found in node.csv, please check it!")
            sys.exit(0)
        y_coord=kwargs['y_coord'] if 'y_coord' in keys else ''
        if y_coord:
            try:
                self.y_coord=float(y_coord)
            except Exception as e:
                print('broken at row {},'.format(row), end=' ')
                print(e)
                sys.exit(0)
        else:
            print("y_coord not found in node.csv, please check it!")
            sys.exit(0)

        self.geometry=geometry.Point(self.x_coord,self.y_coord)
        production=kwargs['production'] if 'production' in keys else ''
        try:
            self.production=float(production)
        except:
            self.production=''
        attraction=kwargs['attraction'] if 'attraction' in keys else ''
        try:
            self.attraction=float(attraction)
        except:
            self.attraction=''

        self.out_link_list=[]
        self.in_link_list=[]

class Link():
    def __init__(self,kwargs,row):
        keys=kwargs.keys()

        link_id=kwargs['link_id'] if 'link_id' in keys else ''
        if link_id:
            try:
                self.link_id=int(float(link_id))
            except Exception as e:
                print('broken at row {},'.format(row),end=' ')
                print(e)
                sys.exit(0)
        else:
            self.link_id=None
        from_node_id=kwargs['from_node_id'] if 'from_node_id' in keys else ''
        if from_node_id:
            try:
                self.from_node_id=int(float(from_node_id))
            except Exception as e:
                print('broken at row {},'.format(row), end=' ')
                print(e)
                sys.exit(0)
        else:
            print("from_node_id not found in link.csv, please check it!")
            sys.exit(0)
        to_node_id=kwargs['to_node_id'] if 'to_node_id' in keys else ''
        if to_node_id:
            try:
                self.to_node_id=int(float(to_node_id))
            except Exception as e:
                print('broken at row {},'.format(row), end=' ')
                print(e)
                sys.exit(0)
        else:
            print("to_node_id not found in link.csv, please check it!")
            sys.exit(0)
        length=kwargs['length'] if 'length' in keys else ''
        try:
            self.length =float(length)
        except:
            self.length=''
        lanes=kwargs['lanes'] if 'lanes' in keys else ''
        try:
            self.lanes =int(float(lanes))
        except:
            self.lanes=''

        free_speed=kwargs['free_speed'] if 'free_speed' in keys else ''
        try:
            self.free_speed =float(free_speed)
        except:
            self.free_speed=''
        capacity=kwargs['capacity'] if 'capacity' in keys else ''
        try:
            self.capacity = float(capacity)
        except:
            self.capacity=''
        link_type_name=kwargs['link_type_name'] if 'link_type_name' in keys else ''
        if link_type_name:
            self.link_type_name=link_type_name
        else:
            self.link_type_name='unclassified'
        link_geo=kwargs['geometry'] if 'geometry' in keys else ''
        try:
            self.geometry = loads(link_geo)
        except:
            self.geometry=''
        if 'allowed_uses' in keys:
            allowed_uses=kwargs['allowed_uses']
            if allowed_uses:
                if ',' in allowed_uses:
                    self.allowed_uses=[allowed_use_.lstrip() for allowed_use_ in allowed_uses.split(',')]
                elif ';' in allowed_uses:
                    self.allowed_uses = [allowed_use_.lstrip() for allowed_use_ in allowed_uses.split(';')]
                else:
                    self.allowed_uses = [allowed_uses]
            else:
                self.allowed_uses = ['unclassified']
        else:
            self.allowed_uses=['unclassified']

class Agent():
    def __init__(self,kwargs,row):
        keys=kwargs.keys()
        agent_id=kwargs['agent_id'] if 'agent_id' in keys else ''
        if agent_id:
            self.agent_id=int(float(agent_id))
        else:
            self.agent_id=None
        node_sequence=kwargs['node_sequence'] if 'node_sequence' in keys else ''
        try:
            self.node_sequence=[int(float(id)) for id in node_sequence.split(';')[:-1]]
        except:
            self.node_sequence=''
        agent_geo=kwargs['geometry'] if 'geometry' in keys else ''
        if ',)' in agent_geo:
            agent_geo=agent_geo.replace(',)',')')
        try:
            self.geometry=loads(agent_geo)
        except:
            self.geometry=''
            print("warning: can't load geometry at row{}".format(row))

class Demand():
    def __init__(self,kwargs,row):
        keys=kwargs.keys()
        o_zone_id=kwargs['o_zone_id'] if 'o_zone_id' in keys else ''
        if o_zone_id:
            try:
                self.o_zone_id=int(float(o_zone_id))
            except Exception as e:
                print('broken at row {},'.format(row),end=' ')
                print(e)
                sys.exit(0)
        else:
            print("o_zone_id is not defined in demand.csv, please check it!")
            sys.exit(0)
        d_zone_id=kwargs['d_zone_id'] if 'd_zone_id' in keys else ''
        if d_zone_id:
            try:
                self.d_zone_id=int(float(d_zone_id))
            except Exception as e:
                print('broken at row {},'.format(row),end=' ')
                print(e)
                sys.exit(0)
        else:
            print("d_zone_id is not defined in demand.csv, please check it!")
            sys.exit(0)
        vol=kwargs['volume'] if 'volume' in keys else ''
        if vol:
            try:
                self.volume=float(vol)
            except Exception as e:
                print('broken at row {},'.format(row),end=' ')
                print(e)
                sys.exit(0)
        else:
            print("volume is not defined in demand.csv, please check it!")
            sys.exit(0)
        demand_geo=kwargs['geometry'] if 'geometry' in keys else ''
        if demand_geo:
            try:
                self.geometry=loads(demand_geo)
            except Exception as e:
                print('broken at row {},'.format(row),end=' ')
                print(e)
                sys.exit(0)
        else:
            print("geometry is not defined in demand.csv, please check it!")
            sys.exit(0)

class POI():
    def __init__(self,kwargs,row):
        keys=kwargs.keys()
        poi_id=kwargs['poi_id'] if 'poi_id' in keys else ''
        try:
            self.poi_id=int(float(poi_id))
        except :
            self.poi_id=None
        self.name=kwargs['name'] if 'name' in keys else ''
        building=kwargs['building'] if 'building' in keys else ''
        if building:
            self.building=building.split(';')
        else:
            self.building = ['unclassified']
        poi_geo=kwargs['geometry'] if 'geometry' in keys else ''
        if poi_geo:
            try:
                self.geometry=loads(poi_geo)
            except Exception as e:
                print('broken at row {}'.format(row),end=' ')
                print(e)
                sys.exit(0)
        else:
            print("geometry is not defined in poi.csv, please check it!")
            sys.exit(0)
        centroid=kwargs['centroid'] if 'centroid' in keys else ''
        if centroid:
            try:
                self.centroid=loads(centroid)
            except:
                self.centroid = self.geometry.centroid
        else:
            self.centroid=self.geometry.centroid

        activity_zone_id=kwargs['activity_zone_id'] if 'activity_zone_id' in keys else ''
        try:
            self.activity_zone_id=int(float(activity_zone_id))
        except:
            self.activity_zone_id=''

class POITrip():
    def __init__(self,kwargs,row):
        keys=kwargs.keys()
        building = kwargs['building'] if 'building' in keys else ''
        if building:
            self.building = building.split(';')
        else:
            self.building =['unclassified']
        production_rate1=kwargs['production_rate1'] if 'production_rate1' in keys else ''
        if production_rate1:
            try:
                self.production_rate1=float(production_rate1)
            except:
                self.production_rate1=0
        else:
            print("production_rate1 is not defined in poi_trip_rate.csv, please check it!")
            sys.exit(0)
        attraction_rate1=kwargs['attraction_rate1'] if 'attraction_rate1' in keys else ''
        if attraction_rate1:
            try:
                self.attraction_rate1=float(attraction_rate1)
            except:
                self.attraction_rate1=0
        else:
            print("attraction_rate1 is not defined in poi_trip_rate.csv, please check it!")
            sys.exit(0)

class Zone():
    def __init__(self,kwargs,row):
        keys=kwargs.keys()
        self.name=kwargs['name'] if 'name' in keys else ' '
        activity_zone_id=kwargs['activity_zone_id'] if 'activity_zone_id' in keys else ''
        if activity_zone_id:
            try:
                self.activity_zone_id=int(float(activity_zone_id))
            except Exception as e:
                print("broken at row{}".format(row),end=' ')
                print(e)
                sys.exit(0)
        else:
            print("activity_zone_id is not defined in zone.csv, please check it!")
            sys.exit(0)
        centroid_x=kwargs['centroid_x'] if 'centroid_x' in keys else ''
        if centroid_x:
            try:
                self.centroid_x=float(centroid_x)
            except Exception as e:
                print("broken at row{}".format(row),end=' ')
                print(e)
                sys.exit(0)
        else:
            print("centroid_x is not defined in zone.csv, please check it!")
            sys.exit(0)
        centroid_y=kwargs['centroid_y'] if 'centroid_y' in keys else ''
        if centroid_y:
            try:
                self.centroid_y=float(centroid_y)
            except Exception as e:
                print("broken at row{}".format(row), end=' ')
                print(e)
                sys.exit(0)
        else:
            print("centroid_y is not defined in zone.csv, please check it!")
            sys.exit(0)
        zone_geo=kwargs['geometry'] if 'geometry' in keys else ''
        if zone_geo:
            try:
                self.geometry=loads(zone_geo)
            except Exception as e:
                print("broken at row{}".format(row), end=' ')
                print(e)
                sys.exit(0)
        else:
            self.geometry=''
        centroid=kwargs['centroid'] if 'centroid' in keys else ''
        if centroid:
            try:
                self.centroid=loads(centroid)
            except Exception as e:
                print("broken at row{}".format(row), end=' ')
                print(e)
                sys.exit(0)
        else:
            print("centroid is not defined in zone.csv, please check it!")
            sys.exit(0)
        total_poi_count=kwargs['total_poi_count'] if 'total_poi_count' in keys else ''
        try:
            self.total_poi_count=float(total_poi_count)
        except:
            self.total_poi_count=0
        residential_poi_count=kwargs['residential_poi_count'] if 'residential_poi_count' in keys else ''
        try:
            self.residential_poi_count=float(residential_poi_count)
        except:
            self.residential_poi_count=0
        office_poi_count=kwargs['office_poi_count'] if 'office_poi_count' in keys else ''
        try:
            self.office_poi_count=float(office_poi_count)
        except:
            self.office_poi_count=0
        shopping_poi_count=kwargs['shopping_poi_count'] if 'shopping_poi_count' in keys else ''
        try:
            self.shopping_poi_count=float(shopping_poi_count)
        except:
            self.shopping_poi_count=0
        school_poi_count=kwargs['school_poi_count'] if 'school_poi_count' in keys else ''
        try:
            self.school_poi_count=float(school_poi_count)
        except:
            self.school_poi_count=0
        parking_poi_count=kwargs['parking_poi_count'] if 'parking_poi_count' in keys else ''
        try:
            self.parking_poi_count=float(parking_poi_count)
        except:
            self.parking_poi_count=0
        boundary_node_count=kwargs['boundary_node_count'] if 'boundary_node_count' in keys else ''
        try:
            self.boundary_node_count=float(boundary_node_count)
        except:
            self.boundary_node_count=0
        total_production=kwargs['total_production'] if 'total_production' in keys else ''
        try:
            self.total_production=float(total_production)
        except:
            self.total_production=0
        total_attraction=kwargs['total_attraction'] if 'total_attraction' in keys else ''
        try:
            self.total_attraction=float(total_attraction)
        except:
            self.total_attraction=0

class Network():
    def __init__(self):
        self.node_dict={}

        self.link_dict={}
        self.agent_dict={}
        self.demand_dict={}
        self.poi_dict={}
        self.poi_trip_dict={}
        self.zone_dict={}

        self.number_of_node=0
        self.number_of_link=0
        self.number_of_agent=0
        self.number_of_demand=0
        self.number_of_poi=0
        self.number_of_zone=0
        self.number_of_poi_type=0

        self.node_coords=[]
        self.link_coords=[]
        self.poi_coords=[]

        self.range_of_zone_ids=[]

        self.min_lat=-90
        self.max_lat=90
        self.min_lng=-180
        self.max_lng=180

    def get_avl_node_attrs(self):
        self.node_attr_dict = {
            'ctrl_type': 'int',
            'activity_type': 'str',
            'production': 'float',
            'attraction': 'float',
        }
        print('%-30s%-20s' % ('attr', 'type'))
        for k, v in self.node_attr_dict.items():
            print('%-30s%-20s' % (k, v))

    def get_avl_link_attrs(self):
        self.link_attr_dict = {
            'length': 'float',
            'lanes': 'int',
            'free_speed': 'float',
            'capacity': 'float',
            'link_type_name': 'str',
            'allowed_uses': 'str',
        }
        print('%-30s%-20s' % ('attr', 'type'))
        for k, v in self.link_attr_dict.items():
            print('%-30s%-20s' % (k, v))

    def get_avl_poi_attrs(self):
        self.poi_attr_dict = {
            'building': 'str',
            'activity_zone_id': 'int'
        }
        print('%-30s%-20s' % ('attr', 'type'))
        for k, v in self.poi_attr_dict.items():
            print('%-30s%-20s' % (k, v))

    def get_avl_range_of_zone_ids(self):
        if self.number_of_zone==0:
            print("zone.csv doesn't exist")
        else:
            print('%-20s%-20s' % ('min zone id', 'max zone id'))
            print('%-20s%-20s' % (self.range_of_zone_ids[0],self.range_of_zone_ids[1]))


# 06-azure-model-registration.py
from azureml.core import Workspace
from azureml.core import Model

if __name__ == "__main__":
    ws = Workspace.from_config(path='./.azureml',_file_name='config.json')

    model = Model.register(model_name='mon_rcv_model',
                           tags={'area': 'udea_training','scoring': 0.80},
                           model_path='outputs/rcv_model.pkl',
                           workspace = ws)
    print(model.name, model.id, model.version, sep='\t')


"""
A frequency analysis tool developed to aid in decrypting ciphers.
:author: Nick Patel nsp5488@rit.edu
:language: python3.7
"""

CIPHER_FILE = "./Resources/CipherTexts/cipher3"
SUBSTITUTION_FILE = './Resources/subs'

def report(dct, n, type):
    """
    Handles the generation and printing of the report.
    :param dct: Maps a given key to the number of occurrences in the ciphertext. (Map[symbol->frequency])
    :param n: The number of symbol->frequency pairs to print.
    :param type: The type of symbol (monogram, digrams, trigrams...)
    :return: None
    """
    most_freq = []
    for k in dct:
        most_freq.append((k, dct[k]))

    most_freq.sort(key=lambda x: x[1], reverse=True)

    print("The", n, "most frequent", type, "are: ", end='')
    for i in range(n):
        print(most_freq[i], end=' ')

def substitute(string, key):
    """
    Substitutes ciphertext according to :key:.
    :param string: The ciphertext to substitute in to.
    :param key: The key to substitute into the ciphertext.
    :return: The ciphertext with the substitutions from :key: made.
    """
    new_string = ''
    for ch in string:
        if ch in key:
            new_string += key[ch]
        else:
            new_string += ch
    return new_string

def freq(cipherfile):
    """
    Counts occurrences of monograms, digrams, trigrams, and quadgrams in :cipherfile: and generates a report for each.
    :param cipherfile: Encrypted text.
    :return: None
    """
    prev = []
    totals = dict()
    digrams = dict()
    trigrams = dict()
    quadgrams = dict()
    with open(cipherfile) as file:
        for line in file:
            line = line.strip()
            lst = line.split(' ')
            for e in lst:
                for ch in e:
                    if len(prev) > 1:
                        if prev[len(prev) - 1] + ch in digrams:
                            digrams[prev[len(prev) - 1] + ch] += 1
                        else:
                            digrams[prev[len(prev) - 1] + ch] = 1
                    if len(prev) > 2:
                        if prev[len(prev) - 2] + prev[len(prev) - 1] + ch in trigrams:
                            trigrams[prev[len(prev) - 2] + prev[len(prev) - 1] + ch] += 1
                        else:
                            trigrams[prev[len(prev) - 2] + prev[len(prev) - 1] + ch] = 1
                    if len(prev) > 3:
                        if prev[len(prev)-3] + prev[len(prev) - 2] + prev[len(prev) - 1] + ch in trigrams:
                            quadgrams[prev[len(prev)-3] + prev[len(prev) - 2] + prev[len(prev) - 1] + ch] += 1
                        else:
                            quadgrams[prev[len(prev)-3] + prev[len(prev) - 2] + prev[len(prev) - 1] + ch] = 1

                    if ch in totals:
                        totals[ch] += 1
                    else:
                        totals[ch] = 1

                    prev.append(ch)
    report(totals, 10, 'monograms')
    print()
    report(digrams, 10, 'digrams')
    print()
    report(trigrams, 10, 'trigrams')
    print()
    report(quadgrams, 10, 'quadgrams')

def main():
    """
    Driver for frequency.py
    :return: None
    """
    freq(CIPHER_FILE)
    string = ''
    with open(CIPHER_FILE) as file:
        for line in file:
            string += line.replace(' ', '').strip()

    key = dict()

    with open(SUBSTITUTION_FILE) as subs:
        for line in subs:
            lst = line.strip().split(' ')
            key[lst[0]] = lst[1]

    print()
    print('original:   ', string)
    print('substituted:', substitute(string, key))

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
from config import db

class WholeService(db.Model):
    __tablename__ = "data_whole_service"

    id = db.Column(db.Integer, primary_key=True, nullable=False)
    task_id = db.Column(db.Integer)
    service_id = db.Column(db.Integer)
    customer_name = db.Column(db.String(20))
    service_start_time = db.Column(db.DateTime)
    service_agency = db.Column(db.String(50))
    service_person = db.Column(db.String(100))
    policy_financing_adviser = db.Column(db.String(100))
    company_profile = db.Column(db.Text)
    declare_project = db.Column(db.Text)
    policy_service = db.Column(db.Text)
    zzh_service = db.Column(db.Text)
    qualification_back = db.Column(db.Text)
    declare_recommend = db.Column(db.Text)
    declare_proposal = db.Column(db.Text)
    aidance_type = db.Column(db.SmallInteger)
    remark = db.Column(db.String(200))
    pdf_path = db.Column(db.String(255))
    create_person = db.Column(db.String(255))
    create_time = db.Column(db.DateTime)
    execute_person = db.Column(db.String(20))
    execute_time = db.Column(db.DateTime)
    is_done = db.Column(db.SmallInteger)

    def __init__(self, task_id, service_id, customer_name, service_start_time, service_agency, service_person,
                 policy_financing_adviser, company_profile, declare_project, policy_service, zzh_service,
                 qualification_back, declare_recommend, declare_proposal, aidance_type, remark, pdf_path, create_person,
                 create_time, execute_person, execute_time, is_done):
        '''Constructor'''
        self.task_id = task_id
        self.service_id = service_id
        self.customer_name = customer_name
        self.service_start_time = service_start_time
        self.service_agency = service_agency
        self.service_person = service_person
        self.policy_financing_adviser = policy_financing_adviser
        self.company_profile = company_profile
        self.declare_project = declare_project
        self.policy_service = policy_service
        self.zzh_service = zzh_service
        self.qualification_back = qualification_back
        self.declare_recommend = declare_recommend
        self.declare_proposal = declare_proposal
        self.aidance_type = aidance_type
        self.remark = remark
        self.pdf_path = pdf_path
        self.create_person = create_person
        self.create_time = create_time
        self.execute_person = execute_person
        self.execute_time = execute_time
        self.is_done = is_done

    def __repr__(self):
        return 'id : %s' % self.id

# Client and database attributes dictionary
clinetHead = ['id', 'taskId', 'serviceId', 'customerName', 'serviceStartTime', 'serviceAgency', 'servicePerson',
              'policyFinancingAdviser', 'companyProfile', 'declareProject', 'policyService', 'zzhService',
              'qualificationBack', 'declareRecommend', 'declareProposal', 'aidanceType', 'remark', 'pdfPath',
              'createPerson', 'createTime', 'executePerson', 'executeTime', 'isDone']
WholeServiceChangeDic = {
    "id": "id",
    "taskId": "task_id",
    "serviceId": "service_id",
    "customerName": "customer_name",
    "serviceStartTime": "service_start_time",
    "serviceAgency": "service_agency",
    "servicePerson": "service_person",
    "policyFinancingAdviser": "policy_financing_adviser",
    "companyProfile": "company_profile",
    "declareProject": "declare_project",
    "policyService": "policy_service",
    "zzhService": "zzh_service",
    "qualificationBack": "qualification_back",
    "declareRecommend": "declare_recommend",
    "declareProposal": "declare_proposal",
    "aidanceType": "aidance_type",
    "remark": "remark",
    "pdfPath": "pdf_path",
    "createPerson": "create_person",
    "createTime": "create_time",
    "executePerson": "execute_person",
    "executeTime": "execute_time",
    "isDone": "is_done"
}

intList = ['id', 'taskId', 'serviceId', 'aidanceType', 'isDone']

# db.create_all()


#coding=utf-8
# ycat			2017-08-11	  create
# 列表显示
import os,sys
import qtawesome as qta

import setup
if __name__ == '__main__':
	setup.setCurPath(__file__)
import PyQt5
from PyQt5.QtCore import Qt,QEvent,QPoint,QSize
from PyQt5.QtGui import QPalette,QColor
from PyQt5.QtWidgets import QApplication, QListWidgetItem,QDialog,QListWidget,QVBoxLayout, QPushButton,QListView
import ui.popDlg
import enhance

class listWidget(PyQt5.QtWidgets.QWidget):
	def __init__(self):
		super(listWidget, self).__init__()
		self.list = QListWidget()
		#TODO：未完成
		#self.list.setCursor(Qt.PointingHandCursor)
		#self.list.itemClicked.connect(self._itemClicked)
		#self.list.verticalScrollBar().setStyleSheet("QScrollBar{width:18px;}")
		#self.list.horizontalScrollBar().setStyleSheet("QScrollBar{height:18px;}")
		self.layout = QVBoxLayout()
		self.layout.addWidget(self.list)
		self.setLayout(self.layout)

	def addColumn(self):
		pass

	def addRow(self,*params):
		pass

#用法:
#注册clicked事件，取出点的button的值
#menu.clicked.connect(self.onClick)
#def onClick(self,value):
#	print(value,menu.result)
class listDlg(ui.popDlg.popDlg):
	def __init__(self, fontSize=10,isIconMode = False,iconSize=48,parent=None):
		super(listDlg, self).__init__(isTransparent=False,parent=None)
		self.iconSize = iconSize
		self.fontSize = fontSize

		self.list = QListWidget()
		self.list.setCursor(Qt.PointingHandCursor)
		self.list.itemClicked.connect(self._itemClicked)
		self.list.verticalScrollBar().setStyleSheet("QScrollBar{width:18px;}")
		self.list.horizontalScrollBar().setStyleSheet("QScrollBar{height:18px;}")
		if isIconMode:
			self.list.setViewMode(QListView.IconMode)
			self.list.setMovement(QListView.Static)
			self.list.setIconSize(QSize(iconSize,iconSize))

		self.layout = QVBoxLayout()
		self.layout.addWidget(self.list)
		self.setLayout(self.layout)

		self.result = None
		self.selected = enhance.event()

	def clear(self):
		self.list.clear()

	def add(self,value,text,icon):
		i = QListWidgetItem(text)
		if icon:
			i.setIcon(icon)
		i.value = value
		font = i.font()
		font.setPointSize(self.fontSize)
		i.setFont(font)
		self.list.addItem(i)
		return i

	def selectItem(self,value):
		for i in range(self.list.count()):
			if self.list.item(i).value == value:
				self.list.scrollToItem(self.list.item(i))
				self.list.setCurrentItem(self.list.item(i))
				break

	#QListWidgetItem
	def _itemClicked(self,item):
		self.result = item.value
		self.close()
		self.selected.emit(self.result)

####################################################
def runTest():
	class dlg(QDialog):
		def __init__(self):
			super(dlg, self).__init__()
			list = listDlg(isIconMode=True,fontSize=30)
			self.list = list
			for i in range(100):
				list.add(1,"test",qta.icon("fa.edit",color="green"))
			list.add(2,"test",qta.icon("fa.edit",color="green"))
			list.add(3,"test",qta.icon("fa.edit",color="green"))
			list.add(4,"test",qta.icon("fa.edit",color="green"))
			list.selectItem(4)

			layout = QVBoxLayout()
			b = QPushButton("ggggggg")
			b.clicked.connect(list.onClick)
			layout.addWidget(b)

			b = QPushButton("ggggggg")
			b.clicked.connect(list.onClick)
			layout.addWidget(b)

			aa = listWidget()
			layout.addWidget(aa)

			list.selected.connect(self.onClick)

			self.setLayout(layout)
			self.setWindowTitle("QT测试")
			self.setWindowFlags(self.windowFlags()& ~Qt.WindowMaximizeButtonHint& Qt.WindowMinimizeButtonHint)
			self.setMinimumSize(400,400)

		def onClick(self,value):
			print(value,self.list.result)

	app = QApplication(sys.argv)
	d = dlg()
	d.show()
	sys.exit(app.exec_())

if __name__ == '__main__':
	runTest()


# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：       LogUtils.py on CPLID_Detect
   Description :
   Author :          Wayne
   Date:             2018/5/21
   Create by :       PyCharm
   Check status:     https://waynehfut.github.io
-------------------------------------------------
"""
__author__ = 'Wayne'
import logging
import time

class LogUtils():
    def __init__(self, isprint=1):
        logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s %(levelname)s %(message)s',
                            datefmt='%a, %d %b %Y %H:%M:%S',
                            filename='./Log/log_at_' + time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) + '.log',
                            filemode='w')
        self.isprint = isprint

    def info(self, info):
        logging.info(info)
        if self.isprint == 1:
            print(info)

    def warning(self, warning):
        logging.warning(warning)
        if self.isprint == 1:
            print(warning)

    def exception(self, exception):
        logging.exception(exception)
        if self.isprint == 1:
            print(exception)

    def stopLogging(self):
        logging.getLogger().handlers = []
        if self.isprint == 1:
            print("Stop logging")

    def report(self, info):
        logging.info("#####----Report----#####")
        logging.info(info)
        if self.isprint == 1:
            print(info)
        logging.info("#####----Report----#####")


from django.conf import settings
from django.db import models

class LeadingRoleDropdown(models.Model):
    name = models.CharField(
        verbose_name='name',
        max_length=20,
    )

    def __str__(self):
        return self.name

class LeadingTeamDropdown(models.Model):
    name = models.CharField(
        verbose_name='name',
        max_length=20,
    )

    def __str__(self):
        return self.name

class ProjectResponsibilityDropdown(models.Model):
    name = models.CharField(
        verbose_name='name',
        max_length=20,
    )

    def __str__(self):
        return self.name

class OverallPMTeamDropdown(models.Model):
    name = models.CharField(
        verbose_name='name',
        max_length=20,
    )

    def __str__(self):
        return self.name

class PlannerControlDropdown(models.Model):
    name = models.CharField(
        verbose_name='name',
        max_length=20,
    )

    def __str__(self):
        return self.name

class ConstructionManagementDropdown(models.Model):
    name = models.CharField(
        verbose_name='name',
        max_length=20,
    )

    def __str__(self):
        return self.name

class IllustratorDropdown(models.Model):
    name = models.CharField(
        verbose_name='name',
        max_length=20,
    )

    def __str__(self):
        return self.name

class CommunicationsDropdown(models.Model):
    name = models.CharField(
        verbose_name='name',
        max_length=20,
    )

    def __str__(self):
        return self.name

"""
Main
"""

class ProjectAssignment(models.Model):
    project = models.OneToOneField(
        verbose_name='project name',
        to='project_data.ProjectData',
        related_name='project_assignment',
        on_delete=models.CASCADE,
        null=True,
    )

    leading_role = models.ForeignKey(
        verbose_name='leading role',
        help_text='Federführende Stelle',
        on_delete=models.SET_NULL,
        to='project_assignment.LeadingRoleDropdown',
        blank=True,
        null=True,
    )

    leading_team = models.ForeignKey(
        verbose_name='leading team',
        help_text='Federführende Fachgruppe',
        on_delete=models.SET_NULL,
        to='project_assignment.LeadingTeamDropdown',
        blank=True,
        null=True,
    )

    project_responsibility = models.ForeignKey(
        verbose_name='project responsibility',
        help_text='Projektverantwortung',
        on_delete=models.SET_NULL,
        to='project_assignment.ProjectResponsibilityDropdown',
        blank=True,
        null=True,
    )

    overall_pm_team = models.ForeignKey(
        verbose_name='overall pm team',
        help_text='Gesamtprojektleitung (Koordinationsteam)',
        on_delete=models.SET_NULL,
        to='project_assignment.OverallPMTeamDropdown',
        blank=True,
        null=True,
    )

    project_management = models.ManyToManyField(
        verbose_name='project management',
        help_text='Projektleitung',
        to=settings.AUTH_USER_MODEL,
        related_name='project_assignments',
        blank=True,
    )

    planner_control = models.ForeignKey(
        verbose_name='planner control',
        help_text='Planerleistung',
        on_delete=models.SET_NULL,
        to='project_assignment.PlannerControlDropdown',
        blank=True,
        null=True,
    )

    construction_management = models.ForeignKey(
        verbose_name='construction management',
        help_text='Bauleitung (Baubegleitung)',
        on_delete=models.SET_NULL,
        to='project_assignment.ConstructionManagementDropdown',
        blank=True,
        null=True,
    )

    illustrator = models.ForeignKey(
        verbose_name='illustrator',
        help_text='ZeichnerIn',
        on_delete=models.SET_NULL,
        to='project_assignment.IllustratorDropdown',
        blank=True,
        null=True,
    )

    communications = models.ForeignKey(
        verbose_name='communications',
        help_text='Kommunikation',
        on_delete=models.SET_NULL,
        to='project_assignment.CommunicationsDropdown',
        blank=True,
        null=True,
    )

    def __str__(self):
        return f'{self.project}'


# Copyright (c) 2012 Zach Byerly
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

AnnotationAtts = AnnotationAttributes()
AnnotationAtts.axes2D.visible = 1
AnnotationAtts.axes2D.autoSetTicks = 1
AnnotationAtts.axes2D.autoSetScaling = 1
AnnotationAtts.axes2D.lineWidth = 0
AnnotationAtts.axes2D.tickLocation = AnnotationAtts.axes2D.Outside  # Inside, Outside, Both
AnnotationAtts.axes2D.tickAxes = AnnotationAtts.axes2D.BottomLeft  # Off, Bottom, Left, BottomLeft, All
AnnotationAtts.axes2D.xAxis.title.visible = 1
AnnotationAtts.axes2D.xAxis.title.font.font = AnnotationAtts.axes2D.xAxis.title.font.Courier  # Arial, Courier, Times
AnnotationAtts.axes2D.xAxis.title.font.scale = 1
AnnotationAtts.axes2D.xAxis.title.font.useForegroundColor = 1
AnnotationAtts.axes2D.xAxis.title.font.color = (0, 0, 0, 255)
AnnotationAtts.axes2D.xAxis.title.font.bold = 1
AnnotationAtts.axes2D.xAxis.title.font.italic = 1
AnnotationAtts.axes2D.xAxis.title.userTitle = 0
AnnotationAtts.axes2D.xAxis.title.userUnits = 0
AnnotationAtts.axes2D.xAxis.title.title = "X-Axis"
AnnotationAtts.axes2D.xAxis.title.units = ""
AnnotationAtts.axes2D.xAxis.label.visible = 1
AnnotationAtts.axes2D.xAxis.label.font.font = AnnotationAtts.axes2D.xAxis.label.font.Courier  # Arial, Courier, Times
AnnotationAtts.axes2D.xAxis.label.font.scale = 1
AnnotationAtts.axes2D.xAxis.label.font.useForegroundColor = 1
AnnotationAtts.axes2D.xAxis.label.font.color = (0, 0, 0, 255)
AnnotationAtts.axes2D.xAxis.label.font.bold = 1
AnnotationAtts.axes2D.xAxis.label.font.italic = 1
AnnotationAtts.axes2D.xAxis.label.scaling = 0
AnnotationAtts.axes2D.xAxis.tickMarks.visible = 1
AnnotationAtts.axes2D.xAxis.tickMarks.majorMinimum = 0
AnnotationAtts.axes2D.xAxis.tickMarks.majorMaximum = 1
AnnotationAtts.axes2D.xAxis.tickMarks.minorSpacing = 0.02
AnnotationAtts.axes2D.xAxis.tickMarks.majorSpacing = 0.2
AnnotationAtts.axes2D.xAxis.grid = 0
AnnotationAtts.axes2D.yAxis.title.visible = 1
AnnotationAtts.axes2D.yAxis.title.font.font = AnnotationAtts.axes2D.yAxis.title.font.Courier  # Arial, Courier, Times
AnnotationAtts.axes2D.yAxis.title.font.scale = 1
AnnotationAtts.axes2D.yAxis.title.font.useForegroundColor = 1
AnnotationAtts.axes2D.yAxis.title.font.color = (0, 0, 0, 255)
AnnotationAtts.axes2D.yAxis.title.font.bold = 1
AnnotationAtts.axes2D.yAxis.title.font.italic = 1
AnnotationAtts.axes2D.yAxis.title.userTitle = 0
AnnotationAtts.axes2D.yAxis.title.userUnits = 0
AnnotationAtts.axes2D.yAxis.title.title = "Y-Axis"
AnnotationAtts.axes2D.yAxis.title.units = ""
AnnotationAtts.axes2D.yAxis.label.visible = 1
AnnotationAtts.axes2D.yAxis.label.font.font = AnnotationAtts.axes2D.yAxis.label.font.Courier  # Arial, Courier, Times
AnnotationAtts.axes2D.yAxis.label.font.scale = 1
AnnotationAtts.axes2D.yAxis.label.font.useForegroundColor = 1
AnnotationAtts.axes2D.yAxis.label.font.color = (0, 0, 0, 255)
AnnotationAtts.axes2D.yAxis.label.font.bold = 1
AnnotationAtts.axes2D.yAxis.label.font.italic = 1
AnnotationAtts.axes2D.yAxis.label.scaling = 0
AnnotationAtts.axes2D.yAxis.tickMarks.visible = 1
AnnotationAtts.axes2D.yAxis.tickMarks.majorMinimum = 0
AnnotationAtts.axes2D.yAxis.tickMarks.majorMaximum = 1
AnnotationAtts.axes2D.yAxis.tickMarks.minorSpacing = 0.02
AnnotationAtts.axes2D.yAxis.tickMarks.majorSpacing = 0.2
AnnotationAtts.axes2D.yAxis.grid = 0
AnnotationAtts.axes3D.visible = 0
AnnotationAtts.axes3D.autoSetTicks = 1
AnnotationAtts.axes3D.autoSetScaling = 1
AnnotationAtts.axes3D.lineWidth = 0
AnnotationAtts.axes3D.tickLocation = AnnotationAtts.axes3D.Inside  # Inside, Outside, Both
AnnotationAtts.axes3D.axesType = AnnotationAtts.axes3D.ClosestTriad  # ClosestTriad, FurthestTriad, OutsideEdges, StaticTriad, StaticEdges
AnnotationAtts.axes3D.triadFlag = 0
AnnotationAtts.axes3D.bboxFlag = 0
AnnotationAtts.axes3D.xAxis.title.visible = 1
AnnotationAtts.axes3D.xAxis.title.font.font = AnnotationAtts.axes3D.xAxis.title.font.Arial  # Arial, Courier, Times
AnnotationAtts.axes3D.xAxis.title.font.scale = 1
AnnotationAtts.axes3D.xAxis.title.font.useForegroundColor = 1
AnnotationAtts.axes3D.xAxis.title.font.color = (0, 0, 0, 255)
AnnotationAtts.axes3D.xAxis.title.font.bold = 0
AnnotationAtts.axes3D.xAxis.title.font.italic = 0
AnnotationAtts.axes3D.xAxis.title.userTitle = 0
AnnotationAtts.axes3D.xAxis.title.userUnits = 0
AnnotationAtts.axes3D.xAxis.title.title = "X-Axis"
AnnotationAtts.axes3D.xAxis.title.units = ""
AnnotationAtts.axes3D.xAxis.label.visible = 1
AnnotationAtts.axes3D.xAxis.label.font.font = AnnotationAtts.axes3D.xAxis.label.font.Arial  # Arial, Courier, Times
AnnotationAtts.axes3D.xAxis.label.font.scale = 1
AnnotationAtts.axes3D.xAxis.label.font.useForegroundColor = 1
AnnotationAtts.axes3D.xAxis.label.font.color = (0, 0, 0, 255)
AnnotationAtts.axes3D.xAxis.label.font.bold = 0
AnnotationAtts.axes3D.xAxis.label.font.italic = 0
AnnotationAtts.axes3D.xAxis.label.scaling = 0
AnnotationAtts.axes3D.xAxis.tickMarks.visible = 1
AnnotationAtts.axes3D.xAxis.tickMarks.majorMinimum = 0
AnnotationAtts.axes3D.xAxis.tickMarks.majorMaximum = 1
AnnotationAtts.axes3D.xAxis.tickMarks.minorSpacing = 0.02
AnnotationAtts.axes3D.xAxis.tickMarks.majorSpacing = 0.2
AnnotationAtts.axes3D.xAxis.grid = 0
AnnotationAtts.axes3D.yAxis.title.visible = 1
AnnotationAtts.axes3D.yAxis.title.font.font = AnnotationAtts.axes3D.yAxis.title.font.Arial  # Arial, Courier, Times
AnnotationAtts.axes3D.yAxis.title.font.scale = 1
AnnotationAtts.axes3D.yAxis.title.font.useForegroundColor = 1
AnnotationAtts.axes3D.yAxis.title.font.color = (0, 0, 0, 255)
AnnotationAtts.axes3D.yAxis.title.font.bold = 0
AnnotationAtts.axes3D.yAxis.title.font.italic = 0
AnnotationAtts.axes3D.yAxis.title.userTitle = 0
AnnotationAtts.axes3D.yAxis.title.userUnits = 0
AnnotationAtts.axes3D.yAxis.title.title = "Y-Axis"
AnnotationAtts.axes3D.yAxis.title.units = ""
AnnotationAtts.axes3D.yAxis.label.visible = 1
AnnotationAtts.axes3D.yAxis.label.font.font = AnnotationAtts.axes3D.yAxis.label.font.Arial  # Arial, Courier, Times
AnnotationAtts.axes3D.yAxis.label.font.scale = 1
AnnotationAtts.axes3D.yAxis.label.font.useForegroundColor = 1
AnnotationAtts.axes3D.yAxis.label.font.color = (0, 0, 0, 255)
AnnotationAtts.axes3D.yAxis.label.font.bold = 0
AnnotationAtts.axes3D.yAxis.label.font.italic = 0
AnnotationAtts.axes3D.yAxis.label.scaling = 0
AnnotationAtts.axes3D.yAxis.tickMarks.visible = 1
AnnotationAtts.axes3D.yAxis.tickMarks.majorMinimum = 0
AnnotationAtts.axes3D.yAxis.tickMarks.majorMaximum = 1
AnnotationAtts.axes3D.yAxis.tickMarks.minorSpacing = 0.02
AnnotationAtts.axes3D.yAxis.tickMarks.majorSpacing = 0.2
AnnotationAtts.axes3D.yAxis.grid = 0
AnnotationAtts.axes3D.zAxis.title.visible = 1
AnnotationAtts.axes3D.zAxis.title.font.font = AnnotationAtts.axes3D.zAxis.title.font.Arial  # Arial, Courier, Times
AnnotationAtts.axes3D.zAxis.title.font.scale = 1
AnnotationAtts.axes3D.zAxis.title.font.useForegroundColor = 1
AnnotationAtts.axes3D.zAxis.title.font.color = (0, 0, 0, 255)
AnnotationAtts.axes3D.zAxis.title.font.bold = 0
AnnotationAtts.axes3D.zAxis.title.font.italic = 0
AnnotationAtts.axes3D.zAxis.title.userTitle = 0
AnnotationAtts.axes3D.zAxis.title.userUnits = 0
AnnotationAtts.axes3D.zAxis.title.title = "Z-Axis"
AnnotationAtts.axes3D.zAxis.title.units = ""
AnnotationAtts.axes3D.zAxis.label.visible = 1
AnnotationAtts.axes3D.zAxis.label.font.font = AnnotationAtts.axes3D.zAxis.label.font.Arial  # Arial, Courier, Times
AnnotationAtts.axes3D.zAxis.label.font.scale = 1
AnnotationAtts.axes3D.zAxis.label.font.useForegroundColor = 1
AnnotationAtts.axes3D.zAxis.label.font.color = (0, 0, 0, 255)
AnnotationAtts.axes3D.zAxis.label.font.bold = 0
AnnotationAtts.axes3D.zAxis.label.font.italic = 0
AnnotationAtts.axes3D.zAxis.label.scaling = 0
AnnotationAtts.axes3D.zAxis.tickMarks.visible = 1
AnnotationAtts.axes3D.zAxis.tickMarks.majorMinimum = 0
AnnotationAtts.axes3D.zAxis.tickMarks.majorMaximum = 1
AnnotationAtts.axes3D.zAxis.tickMarks.minorSpacing = 0.02
AnnotationAtts.axes3D.zAxis.tickMarks.majorSpacing = 0.2
AnnotationAtts.axes3D.zAxis.grid = 0
AnnotationAtts.axes3D.setBBoxLocation = 0
AnnotationAtts.axes3D.bboxLocation = (0, 1, 0, 1, 0, 1)
AnnotationAtts.userInfoFlag = 0
AnnotationAtts.userInfoFont.font = AnnotationAtts.userInfoFont.Arial  # Arial, Courier, Times
AnnotationAtts.userInfoFont.scale = 1
AnnotationAtts.userInfoFont.useForegroundColor = 1
AnnotationAtts.userInfoFont.color = (0, 0, 0, 255)
AnnotationAtts.userInfoFont.bold = 0
AnnotationAtts.userInfoFont.italic = 0
AnnotationAtts.databaseInfoFlag = 1
AnnotationAtts.timeInfoFlag = 1
AnnotationAtts.databaseInfoFont.font = AnnotationAtts.databaseInfoFont.Times  # Arial, Courier, Times
AnnotationAtts.databaseInfoFont.scale = 1
AnnotationAtts.databaseInfoFont.useForegroundColor = 1
AnnotationAtts.databaseInfoFont.color = (0, 0, 0, 255)
AnnotationAtts.databaseInfoFont.bold = 0
AnnotationAtts.databaseInfoFont.italic = 0
AnnotationAtts.databaseInfoExpansionMode = AnnotationAtts.File  # File, Directory, Full, Smart, SmartDirectory
AnnotationAtts.databaseInfoTimeScale = 1
AnnotationAtts.databaseInfoTimeOffset = 0
AnnotationAtts.legendInfoFlag = 0
AnnotationAtts.backgroundColor = (0, 0, 0, 255)
AnnotationAtts.foregroundColor = (255, 255, 255, 255)
AnnotationAtts.gradientBackgroundStyle = AnnotationAtts.Radial  # TopToBottom, BottomToTop, LeftToRight, RightToLeft, Radial
AnnotationAtts.gradientColor1 = (0, 0, 255, 255)
AnnotationAtts.gradientColor2 = (0, 0, 0, 255)
AnnotationAtts.backgroundMode = AnnotationAtts.Solid  # Solid, Gradient, Image, ImageSphere
AnnotationAtts.backgroundImage = ""
AnnotationAtts.imageRepeatX = 1
AnnotationAtts.imageRepeatY = 1
AnnotationAtts.axesArray.visible = 1
AnnotationAtts.axesArray.ticksVisible = 1
AnnotationAtts.axesArray.autoSetTicks = 1
AnnotationAtts.axesArray.autoSetScaling = 1
AnnotationAtts.axesArray.lineWidth = 0
AnnotationAtts.axesArray.axes.title.visible = 1
AnnotationAtts.axesArray.axes.title.font.font = AnnotationAtts.axesArray.axes.title.font.Arial  # Arial, Courier, Times
AnnotationAtts.axesArray.axes.title.font.scale = 1
AnnotationAtts.axesArray.axes.title.font.useForegroundColor = 1
AnnotationAtts.axesArray.axes.title.font.color = (0, 0, 0, 255)
AnnotationAtts.axesArray.axes.title.font.bold = 0
AnnotationAtts.axesArray.axes.title.font.italic = 0
AnnotationAtts.axesArray.axes.title.userTitle = 0
AnnotationAtts.axesArray.axes.title.userUnits = 0
AnnotationAtts.axesArray.axes.title.title = ""
AnnotationAtts.axesArray.axes.title.units = ""
AnnotationAtts.axesArray.axes.label.visible = 1
AnnotationAtts.axesArray.axes.label.font.font = AnnotationAtts.axesArray.axes.label.font.Arial  # Arial, Courier, Times
AnnotationAtts.axesArray.axes.label.font.scale = 1
AnnotationAtts.axesArray.axes.label.font.useForegroundColor = 1
AnnotationAtts.axesArray.axes.label.font.color = (0, 0, 0, 255)
AnnotationAtts.axesArray.axes.label.font.bold = 0
AnnotationAtts.axesArray.axes.label.font.italic = 0
AnnotationAtts.axesArray.axes.label.scaling = 0
AnnotationAtts.axesArray.axes.tickMarks.visible = 1
AnnotationAtts.axesArray.axes.tickMarks.majorMinimum = 0
AnnotationAtts.axesArray.axes.tickMarks.majorMaximum = 1
AnnotationAtts.axesArray.axes.tickMarks.minorSpacing = 0.02
AnnotationAtts.axesArray.axes.tickMarks.majorSpacing = 0.2
AnnotationAtts.axesArray.axes.grid = 0
SetAnnotationAttributes(AnnotationAtts)


"""
Write csv file of sensor data to json per car timeslot.
"""

import csv
import json

INFILE = "Data/sensor_data.csv"
OUTFILE = open("Data/sensor_data_per_timeslot.json", "w")

def convert2json():
    # outputs json file in current directory
    csvfile = open(INFILE, 'r')
    reader = csv.DictReader(csvfile)

    dict = {}
    # loop over rows and add key per day
    for row in reader:
        day = row['timestamp'].split()[0]
        dict[day] = {}

    csvfile = open(INFILE, 'r')
    reader = csv.DictReader(csvfile)

    # loop over rows and add hour key per hour to days
    for row in reader:
        day = row['timestamp'].split()[0]
        time = row['timestamp'].split()[1]
        hour = time.split(":")[0] + ":00"
        dict[day][hour] = {}

    csvfile = open(INFILE, 'r')
    reader = csv.DictReader(csvfile)

    # loop over rows and add dicts with car-id's to hours and the route the car has taken
    for row in reader:
        day = row['timestamp'].split()[0]
        time = row['timestamp'].split()[1]
        hour = time.split(":")[0] + ":00"
        dict[day][hour][row['car-id']] = {}
        dict[day][hour][row['car-id']]['car-type'] = row['car-type']
        dict[day][hour][row['car-id']]['entrance'] = []
        dict[day][hour][row['car-id']]['route'] = {}

    csvfile = open(INFILE, 'r')
    reader = csv.DictReader(csvfile)

    # loop over rows and add gate name per time slot
    for row in reader:
        day = row['timestamp'].split()[0]
        time = row['timestamp'].split()[1]
        hour = time.split(":")[0] + ":00"
        dict[day][hour][row['car-id']]['route'][time] = row['gate-name']

    json.dump(dict, OUTFILE, indent=4, separators=(',', ': '))
    OUTFILE.write('\n')

if __name__ == '__main__':
    convert2json()

Pythonfiles for data analysis/json_per_time.py


import warnings
import torch

def kmeans(
    points,
    k,
    global_start=1,
    max_iter=None,
    reinitialize_empty_clusters=True,
    verbose=False,
):
    '''Naive K-means with parallelized global start.

    Arguments:
        points (Tensor): Number of points times number of features.
        k (int): Number of clusters.
        global_start (int): Different intializations that are run in parallel.
            Default 1.
        reinatialize_empty_clusters (bool): If empty clusters are reinitialized.
            Default True.
        verbose (bool): Controls verbosity. Default True.

    Returns:
        i_cluster (LongTensor): What cluster each point belongs to.
    '''
    if len(points.shape) > 2:
        raise NotImplementedError("Batches not implemented.")

    emb_dim = points.shape[-1]
    n_points = points.shape[0]
    device = points.device

    if n_points < k:
        raise ValueError(f"Number of clusters ({k}) exceed number of points ({n_points})")

    # Initialize centroids randomly from points
    rng_idx = torch.randperm(global_start * n_points, device=device) % n_points
    centroids = points[rng_idx].reshape(global_start, *points.shape)[:, :k, :]
    points = points.unsqueeze(0).tile((global_start, 1, 1))

    converged = False
    iter = 0
    while not converged:
        if verbose:
            print("Iter", iter, end=" ")
        if max_iter and (iter > max_iter):
            warnings.warn("Algorithm finished prematurely. Consider increasing max_iter.", RuntimeWarning)
            break
        # Assign to clusters
        distances = torch.cdist(points, centroids).pow(2)
        distances, i_cluster = distances.min(-1)
        i_cluster = i_cluster.unsqueeze(2).tile((1, 1, emb_dim))

        ## Remove duplicate parallel searches
        #i_cluster, ix = torch.unique(i_cluster, dim=0, return_index=True)
        #points = points[ix, :, :]
        #centroids = centroids[ix, :, :]

        # Move centroids
        for i in range(k):
            is_in_cluster = (i_cluster == i)
            #assert (is_in_cluster.all(dim=-1)==is_in_cluster.any(dim=-1)).all()
            old_centroids = centroids.clone()
            centroids[:, i, :] = (
                (is_in_cluster.float() * points).sum(dim=1)
                / is_in_cluster[:, :, :1].sum(dim=1)
            )

            # Reinitialize unused centroids
            if reinitialize_empty_clusters:
                i_empty_cluster = (is_in_cluster.sum((1, 2)) == 0).nonzero(
                    as_tuple=True
                )[0]
                n_new_centroids = i_empty_cluster.nelement()
                i_random_points = torch.randint(
                    n_points,
                    (n_new_centroids,),
                    device=device,
                )
                new_centroids = points[i_empty_cluster, i_random_points, :]
                centroids[i_empty_cluster, i, :] = new_centroids

        # Check convergence
        cluster_converged = torch.isclose(centroids, old_centroids).flatten(1).all(1)
        if iter > 0:
            ssd_converged = torch.isclose(distances.sum(1), old_distances.sum(1))
            assert ssd_converged.shape == cluster_converged.shape
            cluster_converged |= ssd_converged
        old_distances = distances.clone()

        n_converged = cluster_converged.int().sum().item()
        if verbose:
            print(f"{n_converged}/{global_start} have converged.")
        converged = (n_converged == global_start)
        iter += 1

    assert (i_cluster.diff(dim=2)==0).all(), "This should've been tiled. Hmm."

    # Pick best clusters
    i_best = distances.sum(-1).argmin()
    i_cluster = i_cluster[i_best, :, 0]
    return i_cluster


# import modules

import os
import csv
csvpath = os.path.join("Resources", "election_data.csv")
output_path= os.path.join("Analysis","Final_Analysis.txt")

with open(csvpath) as csvfile_poll:
    csvreader = csv.reader(csvfile_poll, delimiter=',')
    # Read the header row first (skip this step if there is now header)
    csv_header = next(csvreader)

    # Read each row of data after the header
    data = []

    for row in csvreader:
        data.append(row)
    print("Election Results")
    print("----------------------------")

# The total number of votes cast
    print(f'Total Voters Cast: {len(data)}')
    print("----------------------------")

# A complete list of candidates who received votes
    khan=0
    correy=0
    li=0
    tooley=0
    for b in data:
        if b[2]=="Khan":
            khan=khan+1
        elif b[2]=="Correy":
            correy=correy+1
        elif b[2]=="Li":
            li=li+1
        elif b[2]=="O'Tooley":
            tooley=tooley+1
    print(f'Khan: {(khan/len(data)*100):.3f}% ({khan})')
    print(f'Correy: {(correy/len(data)*100):.3f}% ({correy})')
    print(f'Li: {(li/len(data)*100):.3f}% ({li})')
    print(f"O'Tooley: {(tooley/len(data)*100):.3f}% ({tooley})")
    print("----------------------------")

    winner= ""
    winner_votes=0
    if winner_votes<khan:
        winner_votes=khan
        winner="Khan"
    if winner_votes<correy:
        winner_votes=correy
        winner="Correy"
    if winner_votes<li:
        winner_votes=li
        winner="Li"
    if winner_votes<tooley:
        winner_votes=tooley
        winner="O'Tooley"
    print(f'Winner: {winner}')
    print("----------------------------")

with open(output_path,"w",newline='') as writer:
    writer.write("Election Results")
    writer.write("----------------------------\n")
    writer.write(f'Total Voters Cast: {len(data)}\n')
    writer.write("----------------------------\n")
    writer.write(f'Khan: {(khan/len(data)*100):.3f}% ({khan})\n')
    writer.write(f'Correy: {(correy/len(data)*100):.3f}% ({correy})\n')
    writer.write(f'Li: {(li/len(data)*100):.3f}% ({li})\n')
    writer.write(f"O'Tooley: {(tooley/len(data)*100):.3f}% ({tooley})\n")
    writer.write("----------------------------\n")
    writer.write(f'Winner: {winner}\n')
    writer.write("----------------------------\n")


# -*- coding: utf-8 -*-
"""
Created on Mon May  8 12:19:55 2017
@author: Eduardo Juan
"""
from random import random,choice,shuffle
from math import sqrt,ceil

def TSP(seq,G):
    costo=G.getArco(seq[-1],seq[0]).capacidad
    act = seq.pop(0)
    while len(seq) > 0:
        sig = seq.pop(0)
        costo += G.getArco(act, sig).capacidad
        act = sig
    return costo

def DFS(grafo):
    'búsqueda por profundidad del grafo a partir del nodo inicio'
    inicio=choice([x for x in G.ady])
    pila=[inicio]
    secuencia=[]
    while(len(pila)>0):
#        print(pila)
        x=pila.pop()
#        print(x)
        if x not in secuencia:
            secuencia.append(x)
#        print(visitados)
        temp=grafo.ady[x]
        shuffle(temp)
        for a in temp:
            if a.final not in secuencia:
                pila+=[a.final]

    return secuencia

class Arco:
    def __init__(self,i,j,c,res=None):
        self.inicio=i
        self.final=j
        self.capacidad=c
        self.capacidad_original=c
        self.residual=res
    def __lt__(self,other):
        return self.capacidad<other.capacidad
    def __repr__(self):
        return "(%d,%d):%d"%(self.inicio,self.final,self.capacidad)

class Nodo:
    def __init__(self,i,x,y):
        self.id=i
        self.x=x
        self.y=y
    def __repr__(self):
        return "Nodo: %d"%(self.id)
#    def __hash__(self):
#        return hash(self.id)

class GrafoSimple:
    ady={}
    def __init__(self,textFile=None):
        self.ady={}
        if textFile is not None:
            #leer instancia
            input=open(textFile,'r')
            for linea in input:
                linea = linea.strip()
                if len(linea) == 0:
                    break
                pedazo = linea.split()
                if pedazo[0] == 'n:':
                    self.AgregaNodo(int(pedazo[1]))
                elif pedazo[0] == 'a:':
                    self.AgregaArco(int(pedazo[1]),int(pedazo[2]),int(pedazo[3]))
            input.close()

    def __repr__(self):
        string=''
        for n in self.ady:
            string+="%s => "%n
            for a in self.ady[n]:
                string+="%d(%d) "%(a.final ,a.capacidad)
            string+='\n'
        return string

    def AgregaNodo(self,nodo):
        self.ady[nodo]=[]

    def AgregaArco(self,i,j,c=0):
        assert i is not j
#        assert i in self.ady
#        assert j in self.ady
        ij=Arco(i,j,c)
#        ji=Arco(j,i,0,ij)
#        ij.residual=ji
        self.ady[i].append(ij)
#        self.ady[j].append(ji)

    def getArco(self,i,j):
        for a in self.ady[i]:
            if a.final == j:
                return a
    def BuscaCamino(self,s,t,camino):
        'Encuentra un camino desde s a t'
        global operaciones
        operaciones+=1
        if s is t:
            return camino
        try:
            for a in self.ady[s]:
                operaciones+=3
                if a.capacidad>0 and not a in camino and not a.residual in camino:
                    temp= self.BuscaCamino(a.final,t,camino+[a])
                    if temp is not None:
                        return temp
        except:
            print(G)
            input()

def genera_instancia(numNodos,textFile=None):
    ''''genera un grafo de numNodos nodos,
        el numero de arcos incipientes de cada nodo se distribuye exponencialmente con media mediaDensidadArcos,
        se supone la capacidad de los arcos proporcional a la longitud '''

    global puntos

    puntos=[(20*random(),20*random()) for i in range(numNodos)] #aqui no hay seguridad de la lejania de los puntos

    #calcular distancias
    D=[]
    for x1,y1 in puntos:
        temp=[]
        for x2,y2 in puntos:
            temp.append(ceil(sqrt((x1-x2)**2+(y1-y2)**2)))
        D.append(temp)

    #crear grafo
    G=GrafoSimple()
    nodos=range(numNodos)
    for n in nodos:
        G.AgregaNodo(n)
#        print(G.ady)
    for n in nodos:
        arcos=[x for x in nodos if x != n]

        for arco in arcos:
            G.AgregaArco(n,arco,D[n][arco])
#    #Asegurar conexidad
#    grupos=componentes_conexas(G)
##    print(grupos)
#    if len(grupos)>1:#hay que agregar algunos arcos para unir las componentes
#        grupo=grupos.pop()
#        for g in grupos:
#            numArcos=ceil(expovariate(1/2*mediaDensidadArcos)*numNodos)
#            if numArcos>len(grupos) or  numArcos>len(g):
#                numArcos=1
#
#            inicio=sample(list(grupo),numArcos)
#            fin=sample(list(g),numArcos)
#
#
#            for i in range(numArcos):
#                x,y=inicio[i],fin[i]
#                G.AgregaArco(x,y,10*int(sqrt((puntos[x][0]-puntos[y][0])**2+(puntos[x][1]-puntos[y][1])**2)))

    return G

def MST(G):
    arbol=GrafoSimple()
    Arcos=[]
    for n in G.ady:
        arbol.AgregaNodo(n)
        Arcos+=G.ady[n]
    Arcos.sort()

#    arbol = set()
    peso = 0
    comp = dict()

    for arista in Arcos:
        u,v=arista.inicio,arista.final
        c = comp.get(v, {v})
        if u not in c:
            arbol.AgregaArco(u,v,arista.capacidad)
            arbol.AgregaArco(v,u,arista.capacidad)
            peso += arista.capacidad
            nuevo = c.union(comp.get(u, {u}))
            for w in nuevo:
                comp[w] = nuevo

#    #hacer grafo no dirigido
#    for n in arbol.ady:
#        for arco in arbol.ady[n]:
#            arbol.AgregaArco(arco.final,arco)

    return arbol

G=genera_instancia(5)
print(G)
arbol=MST(G)
print(arbol)
for k in range(5):
    seq=DFS(arbol)
    print(seq, TSP(list(seq),G))
#print(DFS(arbol))


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

import argparse
import csv
import hashlib

###########################

SCRIPT_VERSION = "2020.06.18.a"

parser = argparse.ArgumentParser()
parser.add_argument('-v', '--version', action='version', version=f'%(prog)s {SCRIPT_VERSION}')
parser.add_argument('-o', '--output_file', help="CSV file output name (default: output.csv)", default="output.csv")
parser.add_argument('-i', '--input_file', help="CSV file containing the unhashed event data", required=True)
args = parser.parse_args()

print("Processing:")
print("  input_file:", args.input_file)
print("  output_file:", args.output_file)

###########################

def normalizeHash(x):
    s = str(x).encode('utf-8')
    s = s.strip()
    s = s.lower()
    return hashlib.sha256(s).hexdigest()

def hashMatchKeys(columns):
    # replaces columns with the hashed value if it starts with a certain string
    for header in columns:
        if header.startswith("match_key."):
            columns[header] = [normalizeHash(x) for x in columns[header]]
    return columns

def readInput(inputFile):
    """
    if source csv is:
    header1,header2,header3
    value1a,value2a,value3a
    value1b,value2b,value3b

    then returns
    headers=[header1,header2,header3]
    columns={
        header1:[value1a,value1b],
        header2:[value2a,value2b],
        header3:[value3a,value3b],
    }
    """
    with open(inputFile) as inFile:
        reader = csv.reader(inFile)

        headers = next(reader, None)

        column = {}
        for h in headers:
            column[h] = []

        for row in reader:
            for h, v in zip(headers, row):
                column[h].append(v)

        return headers, column

def writeOutput(outputFile, headers, columns):
    with open(outputFile, "w") as outFile:
        outwriter = csv.writer(outFile)

        # add header
        outwriter.writerow(headers)

        # add contents
        numRows = len(columns[headers[0]])
        for i in range(numRows):
            row = []
            for header in headers:
                row.append(columns[header][i])
            outwriter.writerow(row)

def run():
    headers, columns = readInput(args.input_file)
    processedColumns = hashMatchKeys(columns)
    writeOutput(args.output_file, headers, processedColumns)

run()


from datetime import timedelta
from django.core.management import call_command
from django.test import TestCase
from django.utils import timezone

from django_future.jobs import schedule_job
from django_future.models import ScheduledJob

class RunScheduledJobsCommandTest(TestCase):

    def setUp(self):
        self.schedule_at = timezone.now() - timedelta(days=1)

        self.jobs = [
            schedule_job(self.schedule_at, 'math.pow', args=(2, 3)),
            schedule_job(self.schedule_at, 'math.pow', args=(5, 2))
        ]

    def test_cmd_noargs(self):
        """
        Test invocation of command with no arguments. Ensure the scheduled jobs
        are marked as completed.
        """
        self.assertEqual(
            2,
            ScheduledJob.objects.filter(
                status=ScheduledJob.STATUS_SCHEDULED).count()
        )

        call_command('runscheduledjobs')

        self.assertEqual(
            2,
            ScheduledJob.objects.filter(
                status=ScheduledJob.STATUS_COMPLETE).count()
        )

    def test_cmd_delete_completed(self):
        """
        Test invocation of command with '-d' argument to delete completed jobs.
        Ensure the scheduled jobs are removed after.
        """
        self.assertEqual(
            2,
            ScheduledJob.objects.filter(
                status=ScheduledJob.STATUS_SCHEDULED).count()
        )

        call_command('runscheduledjobs', '-d')

        self.assertEqual(0, ScheduledJob.objects.count())

    def test_cmd_ignore_errors(self):
        """
        Test invocation of command with '-i' argument to keep processing jobs
        even if a job fails. Ensure the non-failing jobs are marked as
        completed and the error job is marked as failed.
        """
        schedule_at = self.schedule_at - timedelta(days=1)
        error_job = schedule_job(schedule_at, 'math.funky_error')

        self.assertEqual(
            3,
            ScheduledJob.objects.filter(
                status=ScheduledJob.STATUS_SCHEDULED).count()
        )

        call_command('runscheduledjobs', '-i')

        error_job.refresh_from_db()
        self.assertEqual(error_job.status, ScheduledJob.STATUS_FAILED)

        self.assertEqual(
            2,
            ScheduledJob.objects.filter(
                status=ScheduledJob.STATUS_COMPLETE).count()
        )


import tcrtypes
from sys import argv
from math import sqrt

if len(argv) != 1+4:
	print("args: <CLONES FILE> <OFVF> <SRC UIS> <SNK UIS>")
	exit()

clonespath = argv[1]
ofvfpath = argv[2]
srcuispath = argv[3]
snkuispath = argv[4]

with open(clonespath, "r") as f:
	clones = tcrtypes.CLONES.from_file(f)

srcs = {}
snks = {}
for c in clones.clones:
	if not c.src1 in srcs:
		srcs[c.src1] = []
	srcs[c.src1].append(c)
	if not c.is_src_single():
		if not c.src2 in srcs:
			srcs[c.src2] = []
		srcs[c.src2].append(c)
	if not c.snk1 in snks:
		snks[c.snk1] = []
	snks[c.snk1].append(c)
	if not c.is_snk_single():
		if not c.snk2 in snks:
			snks[c.snk2] = []
		snks[c.snk2].append(c)

print("Shared srcs: ", end="")
for src in srcs:
	if len(srcs[src]) > 1:
		print(src, end=",")
print("")

print("Shared snks: ", end="")
for snk in snks:
	if len(snks[snk]) > 1:
		print(snk, end=",")
print("")

with open(srcuispath, "r") as f:
	srcuis = tcrtypes.UIS.from_file(f)
with open(snkuispath, "r") as f:
	snkuis = tcrtypes.UIS.from_file(f)
with open(ofvfpath, "r") as f:
	ofvf = tcrtypes.OFVF.from_file(f)

srccls = ofvf.classes[0]
snkcls = ofvf.classes[1]

W = ofvf.F
E = [[0 for i in range(W+1)] for j in range(W+1)]
S = [[0 for i in range(W+1)] for j in range(W+1)]
for i in range(0, W):
	for j in range(i, W):
		E[i][j] = (i / W) * j
		E[j][i] = E[i][j]
		Var = (i/W) * ((W-i)/(W-1)) * (j - (1/W)*j*j)
		S[i][j] = sqrt(Var)
		S[j][i] = S[i][j]

# Identify well sharing between sink well sets
nonuissrc = 0
nonuissnk = 0
uiscnt = 0
avgsnkSIM = 0
avgSIM = 0
SIMcnt = 0
snkSIMs = []
for srccid in srcs:
	if len(srcs[srccid]) == 1:
		continue
	# Get sink well sets in clones this source well set is found in
	snkcids = srcs[srccid]
	# Get source well set
	if not srcuis.has_cid(srccid):
		nonuissrc += 1
		continue
	srcwsid = srcuis.cid_to_ws(srccid)
	srcws = set(srccls[srcwsid].wells)
	# Compile first sink well set
	if not snkuis.has_cid(snkcids[0].snk1) or not snkuis.has_cid(snkcids[0].snk2):
		nonuissnk += 1
		continue
	snkws1id1 = snkuis.cid_to_ws(snkcids[0].snk1)
	snkws1id2 = snkuis.cid_to_ws(snkcids[0].snk2)
	snkws1 = set(snkcls[snkws1id1].wells) | set(snkcls[snkws1id2].wells)
	# Compile second sink well set
	if not snkuis.has_cid(snkcids[1].snk1) or not snkuis.has_cid(snkcids[1].snk2):
		nonuissnk += 1
		continue
	snkws2id1 = snkuis.cid_to_ws(snkcids[1].snk1)
	snkws2id2 = snkuis.cid_to_ws(snkcids[1].snk2)
	snkws2 = set(snkcls[snkws2id1].wells) | set(snkcls[snkws2id2].wells)
	# Compute similarity of sink well sets
	snkI = len(snkws1 & snkws2)
	snkU = len(snkws1 | snkws2)
	snkSIM = snkI / snkU
	snkSIMs.append(snkSIM)
	avgsnkSIM += snkSIM
	print(len(snkws1), len(snkws2), snkI, snkSIM)
	# Compute similarity of sink well sets together with source well set
	snkws = snkws1 | snkws2
	I = len(snkws & srcws)
	U = len(snkws | srcws)
	SIM = I / U
	SIM1 = len(snkws1 & srcws) / len(snkws1 | srcws)
	SIM2 = len(snkws2 & srcws) / len(snkws2 | srcws)
	avgSIM += SIM1 + SIM2
	SIMcnt += 2
	p = tcrtypes.pair_probability(W, len(srcws), len(snkws), I)
	print(len(srcws), len(snkws), I, SIM, p, SIM1, SIM2)
	print("--------------------")
	uiscnt += 1
print("FAILED AT SRC UIS:", nonuissrc)
print("FAILED AT SNK UIS:", nonuissnk)
print("AVERAGE SIM OF SUB SNKS TO SRC:", avgSIM / SIMcnt)

snkSIMs.sort()
low = snkSIMs[len(snkSIMs)-1]
qtl1 = snkSIMs[int(len(snkSIMs)*0.75)]
qtl2 = snkSIMs[int(len(snkSIMs)*0.50)]
qtl3 = snkSIMs[int(len(snkSIMs)*0.25)]
high = snkSIMs[0]
print("MIN", low)
print("25%", qtl1)
print("50%", qtl2)
print("75%", qtl3)
print("MAX", high)

midcnt = 0
allcnt = 0
for j in range(0, len(snkcls)):
	if snkcls[j].capacity == 1 and snkuis.is_single(j):
		ws1 = set(snkcls[j].wells)
		for k in range(j+1, len(snkcls)):
			if snkcls[k].capacity == 1 and snkuis.is_single(k):
				ws2 = set(snkcls[k].wells)
				I = len(ws1 & ws2)
				m,n = [len(ws1),len(ws2)]
				U = ws1 | ws2
				sim = I / len(U)
				if sim <= low and sim >= high:
					allcnt += 1
				if sim <= qtl1 and sim >= qtl2:
					midcnt += 1
print("NUM SRCS: ", len(srccls))
print("NUM SNKS: ", len(snkcls))
print("PAIRS TO GET ALL: ", allcnt)
print("PAIRS TO GET MIDDLE 50%: ", midcnt)

exit()

cands = []
for i in range(0, len(srccls)):
	if srccls[i].capacity == 1:
		print(i/len(srccls))
		srcws = set(srccls[i].wells)
		for pair in snkpairs:
			j,k,m,n,pairI,pairsim,pairZ,pairws = pair
			I = len(srcws & pairws)
			U = len(srcws | pairws)
			sim = I / U
			if sim > 0.2:
				p = tcrtypes.pair_probability(W, len(srcws), len(pairws), I)
				cands.append([i,j,k,pairI,pairsim,pairZ,sim,p])

Ecnt = 200
cands.sort(key=lambda c: c[7], reverse=True)
if len(cands) > Ecnt:
	cands = cands[:Ecnt]

print("SHARING")
for c in cands:
	print(c)


#
# functionality: class for parsing eventcli options
# Author:  Dlo Bagari
# created Date: 24-09-2019

import argparse
from .event import Event

from .createservice import CreateService
from lib_event.config import Config

class CliArgs:
    def __init__(self, pro_name):
        self.__parser = argparse.ArgumentParser(prog=pro_name)
        self.__config = Config()
        self.event = Event(self.__parser, self, self.__config)
        self.create_service = CreateService(self.__parser)
        # self.generator = Generator(self.__parser, self.__config)
        self.sender = None

        self._build_args()

    def _build_args(self):
        """
        Build the parser
        :return: None
        """
        # create two sub commands: service, events
        subparsers = self.__parser.add_subparsers(title="sub-commands")
        service_cli = subparsers.add_parser("service", description="create Systemd Service for okta.service")
        events_cli = subparsers.add_parser("events", description="Pull events from okta org and send them to FBA")
        """
        # the generator sub command is used only for generating events.
        event_generator_cli = subparsers.add_parser("generator", description="Generate events for test purpose only "
                                                                             "DO NOT USE THIS IN REAL ENVIRONMENT")
        event_generator_cli.add_argument("--number", "-n", action="store", dest="number",
                                         type=int, help="The number of events to generate", required=True)
        event_generator_cli.add_argument("--multi-locations", "-ml", action="store", type=int, dest="multi_location",
                                         help="Number of events to be generated from different locations")
        event_generator_cli.add_argument("--view", "-v", action="store_true",  dest="view", default=False,
                                         help="Display selected timestamps and some more information")
        event_generator_cli.add_argument("--config-file", "-c", action="store",
                                         dest="config_file", help="config file path", required=True)
        event_generator_cli.set_defaults(function=self.generator)
        """
        # create service eventcli
        service_cli.add_argument("--start", "-s",  action="store_true",
                                 default=False, dest="start", help="Start systemd service(okta_event.service)")
        service_cli.add_argument("--name", "-n", action="store",
                                 default="okta_event", dest="name",
                                 help="the service name, default name is 'okta_event'")
        service_cli.add_argument("--config-file", "-c", action="store",
                                 required=True, dest="config_file",
                                 help="the config file path")
        service_cli.set_defaults(function=self.create_service)

        # create options for events sub-command

        events_cli.add_argument("--view", "-v",
                                action="store_true",
                                default=False,
                                dest="view",
                                help="Display the process. Use this only to view the process.")
        events_cli.add_argument("--map-okta", "-m",
                                action="store_true",
                                default=True,
                                dest="map_okta",
                                help="Map okta logs schema to RIM schema")
        events_cli.add_argument("--frequency", "-f",
                                type=int, default=5,
                                dest="frequency", help="Time to sleep between each two requests in seconds "
                                                       "the default value is 5 seconds")
        events_cli.add_argument("--buffer-max", "-b",
                                type=int, default=100,
                                dest="buffer", help="the number of logs which can be stored on disk "
                                                    "the default value is 100 seconds")
        events_cli.add_argument("--config-file", "-c", action="store",
                                dest="config_file", help="config file path", required=True)
        events_cli.set_defaults(function=self.event)

    def get_parser(self):
        """
        return the built parser
        :return: parser
        """
        return self.__parser


#!python3
# -*- coding:utf-8 -*-
import math
import numpy as np
import pandas as pd
import re

from sklearn.model_selection import train_test_split

# Tool Fuction group
class FuncTool:
    '''
        Here we'll define some useful function that will be applied
     to the following preprocessing.
    '''
    ####################################################################
    # If you'd like to add some other functions, you can do like this: #
    #   def func(self,*args):                                          #
    #       pass                                                       #
    # And call it in other child class by: self.func                   #
    ####################################################################
    def __init__(self):
        self.needclean = []
        self.addColList = []
        self.dict2 = {}

    def needClean(self, fea,col):
        #Func
        #   ----------------
        #   To judge whether we need to clean this feature or not.

        #Param
        #   ----------------
        #   fea: name of the feature
        #   col: pandas.Series, the column of feature.

        #Return
        #   ----------------
        #   boolean: True or False
        #
        if type(col[0]).__name__ == 'str':
            # Judge whether requires transfromation
            if np.sum(col.apply(lambda x:x[-1]==')')*1) != 0:
                self.needclean.append(fea)

                return True

        return False

    # Tool Function applied to columns of dataframe
    # Replace the error with ground truth
    def correct(self, value):
        # To make sure if there needs to be corrected.
        if value.endswith(')'):
            # continuous type or discrete type
            if value[0].isdigit():
                l = re.findall(r'\d+',value)

                return int(l[1])
            else:
                v = value[:-1].split('(')
                value = v[-1].split(',')

                return value[0]

        return value

    # Tool Function applied to columns of dataframe
    def number(self, value):
        # To make sure if there needs to be corrected.
        if value.endswith(')'):
            # continuous type or discrete type
            if value[0].isdigit():
                l = re.findall(r'\d+',value)

                return int(l[2])
            else:
                v = value[:-1].split('(')
                value = v[-1].split(',')

                return value[1]

        return 0

# Main method to process dataset containing split and transformation.
class DataClean(FuncTool):
    '''
        Dataset should be type of dataframe. See more detail in demo.ipynb.
    '''

    # The way we select samples and split train and test.
    def split(self, data, K, split_prob=0.3):
        '''
        #Param
        #   ----------------
        #      dataset: pandas.DataFrame (whole dataset);
        #            K: int or float, given to tell the number or rate of samples;
        #   split_prob: float, given to tell the size of test_set.
        #
        #Output
        #   ----------------
        #   X_train,X_test,y_train,y_test: pandas.DataFrame.
        '''
        if K<1:
            sample_set = data.sample(frac=K, axis=0)
            print('{:.2f} % will be selected as sample set.'.format(K*100))
        else:
            sample_set = data.sample(n=K, axis=0)
            print('{} rows will be selected as sample set.'.format(K))

        print('-'*30)
        print('{} rows will be selected as train set.'.format(math.floor(K*(1-split_prob))))
        print('-'*30)

        value = sample_set[sample_set.columns[:-1]]
        label = sample_set[sample_set.columns[-1]]

        return train_test_split(value, label, test_size=split_prob, random_state=0)

    def transform(self, X):
        '''
        #Param
        #   -----------------
        #   X: pandas.DataFrame, value of the sampleset.
        #
        #Output
        #   -----------------
        #   cleandata: pandas.DataFrame, X after processing.
        '''
        # Make a copy. We should always avoid to processing on the origin dataset.
        Xclean = X[:]

        # First we need to judge whether the feature needs clean.
        # If it requires, we correct mistake in the copy.
        # And then create a new column in copy to record the repeat number.
        print('Now Start Transformation!')
        print('-'*30)
        for fea in X.columns:
            if self.needClean(fea, X[fea]):
                Xclean[fea] = X[fea].apply(self.correct)
                Xclean[fea+'_repnum'] = X[fea].apply(self.number)

                self.addColList.append(fea+'_repnum')

        for fea in X.columns[1:]:
            if type(Xclean.loc[0,fea]).__name__ == 'int64':
                n = X.shape[0]
                sigma_number = np.sum(1/Xclean[fea+'_repnum'])

                d = n/sigma_number
                Xclean[fea] = (Xclean[fea]*d) / Xclean[fea+'_repnum']

        return Xclean[X.columns]