import jft
import jftsettings
import time
import mdio

class ksz8031:
    def __init__(self, RST="", CRS_DV="", ANEN_SPEED="", REF_CLK=""):
        self.pin_reset = RST
        self.pin_crs_dv = CRS_DV
        self.pin_anen_speed = ANEN_SPEED
        self.pin_ref_clk = REF_CLK
        self.id1 = 0x0022
        self.id2 = 0x1550
        pass

    def MDIO_Init(self, SDA="", SCL="", Address=0x03):
        self.mdio = mdio.MDIO(SDA=SDA, SCL=SCL, Address=Address)

    def Get_Address(self):
        print("Проверка стандартного адреса микросхемы:\n")
        try:
            jft.DriveLow(self.pin_reset)
        except:
            print("Пин RST неопределен или недоступен!\n")
            exit(1)
        time.Wait_Time(100000)
        try:
            if jft.GetVar(self.pin_crs_dv):
                i2cAddress = 0x03
            else:
                i2cAddress = 0x00
            print(f"Стандартный адрес микросхемы - {hex(i2cAddress)}\n")
        except:
            print("Пин CRS_DV неопределен или недоступен!\n")
            exit(1)
        jft.DriveHigh(self.pin_reset)
        time.Wait_Time(100000)
        return i2cAddress

    def Check_Id(self):
        print("Проверка ID микросхемы:\n")
        id1 = hex(int(self.mdio.Read(0x02), 2))
        id2 = hex(int(self.mdio.Read(0x03), 2))
        if id1.__eq__(self.id1) and (id2 & 0xFC00).__eq__(self.id2):
            print(f"Микросхема опознана. Модель - {id2 & 0x3F0}\n")
            return 0
        else:
            print(f"ID не совпадает с ожидаемым. Считанный ID1 - {id1}, ID2 - {id2 & 0xFC00}\n")
            return 1

    def Check_DefaultMode(self):
        print("Проеерка режима работы по умолчанию\n")
        try:
            jft.DriveLow(self.pin_reset)
        except:
            print("Пин RST неопределен или недоступен!\n")
            exit(1)
        time.Wait_Time(100000)
        try:
            pinValue = jft.GetVar(self.pin_anen_speed)
        except:
            print("Пин ANEN_SPEED неопределен или недоступен!\n")
            exit(1)
        jft.DriveHigh(self.pin_reset)
        time.Wait_Time(100000)
        reg0 = self.mdio.Read(0x00) & 0x3000
        reg4 = self.mdio.Read(0x04) & 0x0180
        if pinValue:
            if reg0.__eq__(0x3000) and reg4.__eq__(0x0180):
                print("Скорость залочена на 100 Мб\с и включено автосогласование\n")
                return 0
            else:
                print("Микросхема не залочилась на желаемом режиме!\n")
                return 1
        else:
            if reg0.__eg__(0) and reg4.__eq__(0):
                print("Скорость залочена на 10 Мб\с и выключено автосогласование\n")
                return 0
            else:
                print("Микросхема не залочилась на желаемом режиме!\n")
                return 1

    def Check_SpeedModeSupport(self, speed=0, duplex=""):
        regValue = 0x0000
        modeReference = ""
        if duplex.__eq__("full"):
            regValue += 0x0100
            modeReference += "1"
        elif duplex.__eq__("half"):
            modeReference += "0"
        else:
            print("Неверное значение аргумента ''duplex''! Выберите ''half'' или ''full''\n")
            exit(1)
        if speed.__eq__(10):
            regValue += 0x0000
            modeReference += "01"
        elif speed.__eq__(100):
            regValue += 0x2000
            modeReference += "10"
        else:
            print("Неверное значение аргумента ''speed''! Выберите 10 или 100\n")
            exit(1)
        self.mdio.Write(0x00, regValue)
        status = self.mdio.Read(0x1E)
        print(f"Линк {'встал' if status[7].__eq__('1') else 'не встал'}\n")
        if status[13:16].__eq__("000"):
            print("Микросхема в режиме автосогласования\n")
        elif status[13:16].__eq__("001"):
            print("Микросхема в режиме 10Base half-duplex\n")
        elif status[13:16].__eq__("010"):
            print("Микросхема в режиме 100Base half-duplex\n")
        elif status[13:16].__eq__("101"):
            print("Микросхема в режиме 10Base full-duplex\n")
        elif status[13:16].__eq__("110"):
            print("Микросхема в режиме 100Base full-duplex\n")
        else:
            print("Микросхема в неизвестном режиме\n")
            return 1
        if modeReference.__ne__(status[13:16]):
            return 1
        return 0

    def Check_RefClock(self):
        try:
            jft.GetVar(self.pin_ref_clk)
        except:
            print("Пин REF_CLK неопределен или недоступен!\n")
            exit(1)
        tickCount = 500
        tickDifference = 0
        while tickCount.__gt__(0):
            tickCount -= 1
            tickDifference += 1 if jft.GetVar(self.pin_ref_clk) else -1
        if abs(tickDifference).__le__(250):
            return 0
        else:
            return 1

    def Check_Cable(self, pair = ""):
        if pair.__ne__("A") or pair.__ne__("B"):
            print("Неверный аргумент! Выберите 'А' или 'В'\n")
            exit(1)
        self.mdio.Write(0x1F, 0x2100) if pair.__eq__("A") else self.mdio.Write(0x1F, 0x6100)
        timeOut = 5
        status = "1"
        self.mdio.Write(0x1D, 0x8000)
        while status[0].__eq__("1") and timeOut.__gt__(0):
            status = self.mdio.Read(0x1D)
            timeOut -= 1
        if timeOut.__ne__(0):
            if status[1:3].__eq__("00"):
                print(f"Пара {pair} в хорошем состоянии")
                return 0
            elif status[1:3].__eq__("01"):
                print(f"В паре {pair} обнаружен разрыв!")
                return 1
            elif status[1:3].__eq__("10"):
                print(f"В паре {pair} обнаружено короткое замыкание!")
                return 1
            else:
                print(f"Проверка пары {pair} не удалась")
                return 1


import math
import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from utils.kfolds import KFolds, read_csv
from utils.prediction import compute_prediction, compute_error
from utils.feature_scaling import scaling, scaling_by_id
from sklearn.linear_model import LogisticRegression
import argparse

def main(path, delimiter, verbose):
	data_frame, all_headers = read_csv(path, delimiter, verbose)

	# selecting headers of interest
	headers = ['id',
		#'hash_email',
		#'hash_email_conversion',
		#'hash_userid',
		'rank',
		'occurrences',
		'lifetime',
		'nb_days',
		'nb_idtags',
		'nb_idtags_site',
		'nb_idtags_media',
		#'click_rate',
		'nb_purchases',
		'last_time',
		'nb_ips']
	headers_to_drop = list(set(all_headers) - set(headers))
	headers_to_scale = headers[:]
	headers_to_scale.remove('id')
	headers_to_scale.remove('rank')

	# K-Fold cross-validation
	nb_folds = 10
	fold = KFolds(data_frame, nb_folds)
	missranked_scores_train = []
	missranked_scores_test = []

	for k in range(nb_folds):
		train, test = fold.get_fold(k)
		train = train.sort_values(by='id')
		test = test.sort_values(by='id')

		# dropping not usefull columns
		for drop in headers_to_drop:
		    train = train.drop(drop, 1)
		    test = test.drop(drop, 1)

		# train set
		train, mean, std = scaling(train, headers_to_scale)
		train = train.reset_index(drop=True)
		X = train[headers_to_scale].values
		Y = train['rank']==1

		# training Logistic regression model
		regularization = 1e10
		logreg = LogisticRegression(C=regularization)
		logreg.fit(X, Y)

		# computing score on train set
		Y_score_train = logreg.predict_proba(X)[:,1]
		Y_predicted_train = compute_prediction(train, Y_score_train, verbose)
		missranked_train, wellranked_train, total_train = compute_error(Y, Y_predicted_train)
		missranked_scores_train.append(missranked_train/total_train)

		# test set
		test = scaling(test, headers_to_scale, mean, std)
		test = test.reset_index(drop=True)
		X_test = test[headers_to_scale].values
		Y_test = test['rank'].values==1

		# computing score on test set
		Y_score_test = logreg.predict_proba(X_test)[:,1]
		Y_predicted_test = compute_prediction(test, Y_score_test, verbose)
		missranked_test, wellranked_test, total_test = compute_error(Y_test, Y_predicted_test)
		missranked_scores_test.append(missranked_test/total_test)

		# printing intermediate results
		if verbose:
			print('\n**** fold ', k, '****')
			print('train set:')
			print('   missranked =', round(missranked_train/total_train, 3))
			print('   wellranked =', round(wellranked_train/total_train, 3))
			print('test set:')
			print('   missranked =', round(missranked_test/total_test, 3))
			print('   wellranked =', round(wellranked_test/total_test, 3))

	# printing final result
	if verbose:
		print('\n******** MEAN over all folds ********')
		print('Train missranked = ', np.mean(missranked_scores_train))
		print(' Test missranked = ', np.mean(missranked_scores_test))
		print('  Train accuracy = ', 1 - np.mean(missranked_scores_train))
		print('   Test accuracy = ', 1 - np.mean(missranked_scores_test))

if __name__ == '__main__':
    # Parse the different arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--dataset', help='path to the dataset', required=True)
    parser.add_argument('--delimiter', help='delimiter used in the dataset', required=True)
    parser.add_argument('--verbose', help='increase output verbosity', action='store_true', required=False)

    # Recover the arguments
    opts = parser.parse_args()

    # Execute the main function
    main(opts.dataset, opts.delimiter, opts.verbose)


from Actor import Actor
from Critic import Critic
from Noise import Noise
from ReplayBuffer import ReplayBuffer

import numpy as np
import random

import torch
import torch.nn.functional as F
import torch.optim as optim

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

class Agent():
    """DDQN Agent utilzing an actor/critic nn. """

    def __init__(self, state_size, action_size, memory, id, seed=0):
        """Initialize Agent
        Args:
            state_size: number of possible states
            action_size: number of possible acitons
            seed: random seed
        """
        self.state_size = state_size
        self.action_size = action_size
        self.epsilon = 1
        self.discount_factor = 1e-3
        self.t_step = 0
        self.seed = random.seed(seed)
        self.id = id

        # Actor model
        self.actor_local = Actor(state_size, action_size, seed).to(device)
        self.actor_target = Actor(state_size, action_size, seed).to(device)
        self.actor_optimizer = optim.Adam(self.actor_local.parameters(), lr=1e-3) # Learning rate

        # Critic Model
        self.critic_local = Critic(state_size, action_size, seed).to(device)
        self.critic_target = Critic(state_size, action_size, seed).to(device)
        self.critic_optimizer = optim.Adam(self.critic_local.parameters(), lr=1e-3, weight_decay=0) # Learning rate

        # Add noise
        self.noise = Noise(action_size, seed)

        # Memory buffer
        self.memory = memory

    def reset_noise(self):
        """ Reset noise"""
        self.noise.reset()

    def step(self, state, action, reward, next_state, done, step):
        """Save the experience in the buffer
        Args:
            state: current state
            action: selected action
            reward: reward given
            next_state: next state to advance to
            done: has the memory completed
        """
        # save experience
        self.memory.add(state, action, reward, next_state, done)
        # Learn in batches
        if len(self.memory) > self.memory.batch_size:
            experiences = self.memory.get_random_sample()
            self.learn(experiences, 0.99) # discount factor

    def learn(self, experiences, gamma):
        """Teachs the agent based on the current state, action reward and next state

        Args:
            experiences: tuple of state, actions, rewards, next states and done
            gamma: the discount factor
        """
        states, actions, rewards, next_states, dones = experiences

        # Update critic network
        # Get next state-action and Q values
        actions_next = self.actor_target(next_states)
        if self.id == 0:
            actions_next = torch.cat((actions_next, actions[:,2:]), dim=1)
        else:
            actions_next = torch.cat((actions[:,:2], actions_next), dim=1)

        Q_targets_next = self.critic_target(next_states, actions_next)
        # Compute Q targets for current states
        Q_targets = rewards + (gamma * Q_targets_next * (1 - dones))
        # Get expected Q values from local model
        Q_expected = self.critic_local(states, actions)
        critic_loss = F.mse_loss(Q_expected, Q_targets)
        # Minimize loss
        self.critic_optimizer.zero_grad()
        critic_loss.backward()
        self.critic_optimizer.step()

        # Update actor network
        actions_pred = self.actor_local(states)
        if self.id == 0:
            actions_pred = torch.cat((actions_pred, actions[:,2:]), dim=1)
        else:
            actions_pred = torch.cat((actions[:,:2], actions_pred), dim=1)

        actor_loss = -self.critic_local(states, actions_pred).mean()
        # minimize loss
        self.actor_optimizer.zero_grad()
        actor_loss.backward()
        self.actor_optimizer.step()

        # Updates networks
        self.update(self.critic_local, self.critic_target, 6e-2)
        self.update(self.actor_local, self.actor_target, 6e-2)

        # reduce epsilon per learned event
        self.epsilon -= self.discount_factor
        self.epsilon = max(self.epsilon, 0)
        # Reset noise between learning events
        self.noise.reset()

    def update(self, local_model, target_model, tau):
        """Update the model params
        target = t*local+(1-t)*target

        Args:
            local_model: pytorch model source
            target_model: pytorch model destination
            tau: interpolation param
        """
        for target_param, local_param in zip(target_model.parameters(), local_model.parameters()):
            target_param.data.copy_(tau*local_param.data + (1.0-tau)*target_param.data)

    def get_action(self, state, add_noise=True):
        """Gets the action from the given state
        Args:
            state: current state
            eps: epsilon value for a epsilon-greedy policy
        Returns:
            action selection based on the epsilon-greedy policy
        """
        state = torch.from_numpy(state).float().to(device)
        self.actor_local.eval()

        with torch.no_grad():
            action = self.actor_local(state).cpu().data.numpy()
        self.actor_local.train()
        # Add noise discounted by epsilon
        if add_noise:
            action += self.epsilon * self.noise.sample()
        return np.clip(action, -1, 1)


#!/usr/bin/python

"""файл для асинхронного моделирования с имзмененными исходными данными(много цветов)"""
"""Другой подсчет штрафа в цикле"""
from random import choice
import random
import json
import asyncio
from time import time, sleep

#Метод считывания графа из файла .json
with open("data(color)4.json", 'r', encoding='utf-8') as fh: #открываем файл на чтение
    graph = json.load(fh) #загружаем из файла данные в словарь graph
print("GRAPH=", graph)

resulting_var = 0
class Agent():
    """Класс для создания агента(каждого узла модели)"""
    def __init__(self, name): #name = цифры
        """Конструктор - изначальные аттрибуты агента при создании:"""
        self.name = name
        self.neighbors = []  # Список соседей агента
        self.val = graph["vars"][self.name]["initial"]   # Определение изначального цвета агента(задан в исходных данных)
        self.domen = graph["vars"][self.name]["domain"]  # Домен значений
        self.current_cycle = {}  # Состояние текущего цикла агента
        self.next_cycle = {}    # Состояние следующего цикла агента
        self.current_cost  = 0   # Текущая стоимость(сумма штрафов) агента
        self.res = 0   #Результат работы всех циклов агента
        print(f"Агент {self.name} - создан")
        print(f"Значение агента ({self.name}) = ", self.val)

    def neighbor_search(self):
        """Определение списка соседей для текущего агента по исходным данным:"""
        for name in graph["pairs"]:
            if self.name == name[0]:
                self.neighbors.append(name[1])
            elif self.name == name[1]:
                self.neighbors.append(name[0])
        self.neighbors = list(set(self.neighbors))  # Оставляем только уникальные значения в списке
        print(f"Соседи агента {self.name} = ", self.neighbors)
        print()

    async def send(self, agent):
        """Запись значения текущего агента переданному соседу(запись в его состояния циклов)"""
        if self.name in dict1[agent].neighbors: # Выполняем функцию только если соседи:
            print("------------")
            print(f"Агент {self.name} - сосед агента {agent}")
            print("Выполняем метод send")
            if self.name not in dict1[agent].current_cycle: #Есть ли значение текущего агента в текущем цикле соседа(i)
                print(f"Текущий Агент({self.name}) еще не отсылал свое значение агенту({dict1[agent].name})")
                dict1[agent].current_cycle[self.name] = self.val #Запись в словарь с ключом - имя текущего агента
                print(f"Текущий цикл агента({dict1[agent].name}) равен", dict1[agent].current_cycle) #Состояние текущего цикла соседа(i)
                if len(dict1[agent].neighbors) == len(dict1[agent].current_cycle): #Проверка на законченность цикла соседа
                    print(f"Цикл агента ({dict1[agent].name}) - завершен!")
                    # Расчет лучшего значения для переменной из цикла:
                    await dict1[agent].evaluate_cycle(agent)

            else:   #Если текущий агент уже есть в текущем цикле соседа:
                print(f"Текущий Агент{self.name} уже отправлял значение агенту ({dict1[agent].name})")
                dict1[agent].next_cycle[self.name] = self.val #Запись значения текущего агента в следующий цикл соседа
                print(f"Поэтому записано в следующий цикла агента({dict1[agent].name}) = ", dict1[agent].next_cycle)
            print("------------")
            print()

    async def func(self, val1, val2, name_neighbor):
        """Метод расчета стоимости между двумя значениями переменных агентов"""
        #global res1
        print("--------------")
        print(f"Выполняем метод func между ({self.name}) и ({name_neighbor})")
        res2 = 0
        for function in graph["functions"]:
            if (val1 == function[0] and val2 == function[1]) or (val1 == function[1] and val2 == function[2]):
                res2 = function[2]
        print(f"Значения текущего агента ({self.name})=({val1}), а значение агента ({name_neighbor})=({val2})")
        print(f"Поэтому штраф равен ", res2)
        print("--------------")
        print()
        return res2

    async def evaluate_cycle(self, name):
        """Метод выбора наилучшего значения для переменной агента исходя из минимальной стоимости(выполняется в конце цикла)"""
        #dict1[name].current_cycle[dict1[name].name] = dict1[name].val
        print(f"Текущий цикл агента ({dict1[name].name}) = ", dict1[name].current_cycle)
        print("----------------")
        print(f"Выполняем метод расчета стоимости агента {name}")

        cost_otnosit = {}
        cost_current = 0
        for key, value in dict1[name].current_cycle.items():
            # Расчет стоимости агентом при текущем значении, суммируя стоимости со всеми соседями:
            print("Расчет стоимости при текущем значении")
            cost_current = cost_current + await dict1[name].func(self.val, value, key)
        cost_otnosit[dict1[name].val] = cost_current
        print(f"Стоимость агента() при текущем значении ({dict1[name].val}) равна", cost_otnosit)

        # Создание словаря со стоимостью относительно состояния соседей, подставляя все значения из домена и суммируя их
        for args in dict1[name].domen:
            res1 = 0
            for key, value in dict1[name].current_cycle.items():
                res1 = res1 + await dict1[name].func(args, value, key)
                cost_otnosit[args] = res1
            print("cost_otnosit=", cost_otnosit)

        # Выбор значения для переменной агента исходя из стоимостей
        val_min = 0
        for key, value in cost_otnosit.items():
            if value == min(cost_otnosit.values()):
                print(f"Минимальная стоимость агентом({dict1[name].name}) достигается при цвете", key)
                print(f"Значение агента({dict1[name].name}) меняем на", key)
                val_min = value
                dict1[name].val = key
                print(f"Текущий цвет агента({dict1[name].name})=", dict1[name].val)
                dict1[name].res = dict1[name].res + value # Подсчет общей стоимости агента за всю работу модели
        global resulting_var
        resulting_var = resulting_var + val_min
        print(f"Прибавляем к финальной стоимости штраф цикла агента({dict1[name].name}) = ", val_min)
        print("Финальная стоимость на данный момент равна = ", resulting_var)
        cost_otnosit = {}
        dict1[name].current_cycle = {}  #Обнуляем текущий цикл:
        print("----------------")
        print()

def results():
    """Подсчет общей суммы штрафов всех агентов"""
    summa = 0
    for agent in graph["vars"].keys():
        summa = summa + dict1[agent].res
    for name in graph["vars"].keys():
        print(f"Цвет агента ({dict1[name].name}) - {dict1[name].val}")
    print("----------------------------------------------------")
    print("Общая стоимость модели(сумма всех штрафов) равна " + str(summa))
    print("Общий штраф по новому расчету равен ", resulting_var)

def init_agents():
    """Создание агентов по ключам графа"""
    global dict1
    for i in graph["vars"].keys():
        dict1[i] = Agent(i)
        dict1[i].neighbor_search()

#=======================MAIN==================#

start = time()

#Создание агентов по ключам графа
dict1 = {}
init_agents()

#agent1 = random.choice(list(graph["vars"].keys())) # Случайный агент из всех возможных

tasks = []
c = 0
event_loop = asyncio.get_event_loop()
for i in range(0, 1500):
    for agent in graph["vars"].keys():
        tasks.append(event_loop.create_task(dict1[agent].send(random.choice(list(graph["vars"].keys())))))
        c = c +1
        #print("Действие номер - ", c)
print("Длина очереди асинхронных действий=", len(tasks))
wait_tasks = asyncio.wait(tasks)
event_loop.run_until_complete(wait_tasks)
event_loop.close()

results()

#print(list(dict1.keys()))

#########Варианты для обычных тестов##########
# c = 0
# c1 = 10
# for i in range(1500):
#     for agent in graph["vars"].keys():
#         #c1 = c1 - 1
#         dict1[agent].send(random.choice(list(graph["vars"].keys())))
#         c = c +1
#
# results()

#print(choice(list(dict1.keys())))

print("Кол-во действий = ", c)
print(f"Script work time is {time() - start} s.")


from setuptools import setup

long_description = """
Implementations of classic data structures in Python.
"""

setup(
    name="data-structures",
    version="0.1-dev",
    description="Classic Data Structures",
    long_description=long_description,
    # The project URL.
    url='http://github.com/geekofalltrades/data-structures',
    # Author details
    author='William Dougherty',
    author_email='thegeekofalltrades@gmail.com',
    # Choose your license
    #   and remember to include the license text in a 'docs' directory.
    # license='MIT',
    packages=['data_structures'],
    install_requires=['setuptools', ]
)


# coding=utf-8

from flask import Flask, render_template, request, make_response
from flask import jsonify

import time
import threading
import demo as demo

"""
定义心跳检测函数

"""

def heartbeat():
    print(time.strftime('%Y-%m-%d %H:%M:%S - heartbeat', time.localtime(time.time())))
    timer = threading.Timer(60, heartbeat)
    timer.start()

timer = threading.Timer(60, heartbeat)
timer.start()

"""
ElementTree在 Python 标准库中有两种实现。
一种是纯 Python 实现例如 xml.etree.ElementTree ，
另外一种是速度快一点的 xml.etree.cElementTree 。
 尽量使用 C 语言实现的那种，因为它速度更快，而且消耗的内存更少
"""

app = Flask(__name__, static_url_path="/static")

@app.route('/message', methods=['POST'])
# """定义应答函数，用于获取输入信息并返回相应的答案"""
def reply():
    # 从请求中获取参数信息
    req_msg = request.form['msg']
    if req_msg == 'train()':
        # 执行训练
        res_msg = demo.train()
        res_msg = res_msg.replace(' ', '')
        return jsonify({'text': res_msg})
    res_msg = demo.predict(req_msg)
    # 将语句使用结巴分词进行分词
    # req_msg =" ".join(jieba.cut(req_msg))
    # 调用decode_line对生成回答信息
    # res_msg = execute.decoder_online(sess, conf.gen_config ,model, vocab ,rev_vocab, req_msg )
    # res_msg=generator.get_predicted_sentence(sess,req_msg,vocab,model,conf.gen_config.beam_size,conf.gen_config.buckets)
    # 将unk值的词用微笑符号袋贴
    # res_msg = res_msg.replace('_UNK', '^_^')
    res_msg = res_msg.replace(' ','')
    return jsonify({'text': res_msg})

"""
jsonify:是用于处理序列化json数据的函数，就是将数据组装成json格式返回

http://flask.pocoo.org/docs/0.12/api/#module-flask.json
"""

@app.route("/")
def index():
    return render_template("index.html")

#
'''
初始化seq2seqModel，并进行动作

    1. 调用执行器的主程序
    2. 生成一个在线decode进程，来提供在线聊天服务
'''
# _________________________________________________________________

# _________________________________________________________________

# 启动APP
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8808)


import numpy as np

class Model:
    def __init__(self, data, output):
        """
        :data - Matrix of data: e.g. np.zeros((m, j))
        :output - Actual output for given data: np.zeros((m, 1))
        :theta - Vector of thetas

        where:
            j - number of features
            m - number of examples
        """
        self.data = np.c_[np.ones((len(data), 1)), data] # add bias units
        self.output = output
        self.theta = np.zeros((len(self.data[0]), 1))

    def hypothesis(self, X):
        """
        Returns predicted value depending on X.

        :X - column vector of features.
        """
        return np.c_[np.ones((len(X), 1)), X].dot(self.theta)

    def cost_function(self, theta):
        """
        Calculates how wrong the model is
        in terms of its prediction.
        """
        m = len(self.output)

        predictions = np.dot(self.data, theta)
        cost = (1 / (2 * m)) * np.sum(np.square(predictions - self.output))
        return cost

    def gradient_descent(self, iterations=100, learning_rate=0.01):
        """
        Gradient descent algorithm.
        """
        m = len(self.output)
        J_history = np.zeros((iterations, 1))
        for i in range(iterations):
            prediction = np.dot(self.data, self.theta)
            self.theta = self.theta - learning_rate * ((1 / m) * self.data.T.dot((prediction - self.output)))
            J_history[i][0] = self.cost_function(self.theta)
        return J_history

    def reg_cost_function(self, theta, lambda_val=0.01):
        m = len(self.output)
        predictions = np.dot(self.data, theta)
        reg_value = lambda_val * np.sum(np.square(theta[1:][0]))
        cost = ((1 / (2 * m)) * np.sum(np.square(predictions - self.output))) + reg_value
        return cost

    def reg_gradient_descent(self, iterations=100, learning_rate=0.01, lambda_val=0.01):
        m = len(self.output)
        J_history = np.zeros((iterations, 1))
        for i in range(iterations):
            prediction = np.dot(self.data, self.theta)

            reg_value = 1 - learning_rate * (lambda_val / m)
            self.theta = self.theta * reg_value - learning_rate * (self.data.T.dot((prediction - self.output)) / m)
            J_history[i][0] = self.reg_cost_function(self.theta, lambda_val)
        return J_history


"""
Goldair GECO WiFi Heater device.
"""
try:
    from homeassistant.components.climate import ClimateEntity
except ImportError:
    from homeassistant.components.climate import ClimateDevice as ClimateEntity

from homeassistant.components.climate.const import (
    ATTR_HVAC_MODE,
    HVAC_MODE_HEAT,
    SUPPORT_TARGET_TEMPERATURE,
)
from homeassistant.const import ATTR_TEMPERATURE, STATE_UNAVAILABLE

from ..device import GoldairTuyaDevice
from .const import (
    ATTR_ERROR,
    ATTR_TARGET_TEMPERATURE,
    HVAC_MODE_TO_DPS_MODE,
    PROPERTY_TO_DPS_ID,
)

SUPPORT_FLAGS = SUPPORT_TARGET_TEMPERATURE

class GoldairGECOHeater(ClimateEntity):
    """Representation of a Goldair GECO WiFi heater."""

    def __init__(self, device):
        """Initialize the heater.
        Args:
            device (GoldairTuyaDevice): The device API instance."""
        self._device = device

        self._support_flags = SUPPORT_FLAGS

        self._TEMPERATURE_STEP = 1
        self._TEMPERATURE_LIMITS = {"min": 15, "max": 35}

    @property
    def supported_features(self):
        """Return the list of supported features."""
        return self._support_flags

    @property
    def should_poll(self):
        """Return the polling state."""
        return True

    @property
    def name(self):
        """Return the name of the climate device."""
        return self._device.name

    @property
    def unique_id(self):
        """Return the unique id for this heater."""
        return self._device.unique_id

    @property
    def device_info(self):
        """Return device information about this heater."""
        return self._device.device_info

    @property
    def icon(self):
        """Return the icon to use in the frontend for this device."""
        hvac_mode = self.hvac_mode

        if hvac_mode == HVAC_MODE_HEAT:
            return "mdi:radiator"
        else:
            return "mdi:radiator-disabled"

    @property
    def temperature_unit(self):
        """Return the unit of measurement."""
        return self._device.temperature_unit

    @property
    def target_temperature(self):
        """Return the temperature we try to reach."""
        return self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_TARGET_TEMPERATURE])

    @property
    def target_temperature_step(self):
        """Return the supported step of target temperature."""
        return self._TEMPERATURE_STEP

    @property
    def min_temp(self):
        """Return the minimum temperature."""
        return self._TEMPERATURE_LIMITS["min"]

    @property
    def max_temp(self):
        """Return the maximum temperature."""
        return self._TEMPERATURE_LIMITS["max"]

    async def async_set_temperature(self, **kwargs):
        """Set new target temperatures."""
        if kwargs.get(ATTR_TEMPERATURE) is not None:
            await self.async_set_target_temperature(kwargs.get(ATTR_TEMPERATURE))

    async def async_set_target_temperature(self, target_temperature):
        target_temperature = int(round(target_temperature))

        limits = self._TEMPERATURE_LIMITS
        if not limits["min"] <= target_temperature <= limits["max"]:
            raise ValueError(
                f"Target temperature ({target_temperature}) must be between "
                f'{limits["min"]} and {limits["max"]}.'
            )

        await self._device.async_set_property(
            PROPERTY_TO_DPS_ID[ATTR_TARGET_TEMPERATURE], target_temperature
        )

    @property
    def current_temperature(self):
        """Return the current temperature."""
        return self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_TEMPERATURE])

    @property
    def hvac_mode(self):
        """Return current HVAC mode, ie Heat or Off."""
        dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE])

        if dps_mode is not None:
            return GoldairTuyaDevice.get_key_for_value(HVAC_MODE_TO_DPS_MODE, dps_mode)
        else:
            return STATE_UNAVAILABLE

    @property
    def hvac_modes(self):
        """Return the list of available HVAC modes."""
        return list(HVAC_MODE_TO_DPS_MODE.keys())

    async def async_set_hvac_mode(self, hvac_mode):
        """Set new HVAC mode."""
        dps_mode = HVAC_MODE_TO_DPS_MODE[hvac_mode]
        await self._device.async_set_property(
            PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE], dps_mode
        )

    @property
    def device_state_attributes(self):
        """Get additional attributes that HA doesn't naturally support."""
        error = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_ERROR])

        return {ATTR_ERROR: error or None}

    async def async_update(self):
        await self._device.async_refresh()


# -*- coding: utf-8 -*-
"""
Created on Mon Aug 29 10:49:47 2016

@author: Naveen
"""

import pandas as pd
import numpy as np
#%matplotlib inline
import seaborn as sns
import matplotlib.pyplot as plt
import os
from sklearn.preprocessing import LabelEncoder
from scipy.sparse import csr_matrix, hstack
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import StratifiedKFold
from sklearn.metrics import log_loss

"""LOAD THE FILES"""
from enum import Enum
class Load_Type(Enum):
    readdisk_writeHD5 = 1
    readdisk = 2
    readHD5 = 3

load_type = Load_Type.readdisk
datadir = r'D:\Work\Projects\Programming\Python\Kaggle\TalkingData\Data\Extracted'

if load_type == Load_Type.readdisk or load_type == Load_Type.readdisk_writeHD5:
    gender_age_train_df = pd.read_csv(os.path.join(datadir,'gender_age_train.csv'), index_col = 'device_id')
    gender_age_test_df = pd.read_csv(os.path.join(datadir,'gender_age_test.csv'), index_col = 'device_id')
    label_categories_df = pd.read_csv(os.path.join(datadir, 'label_categories.csv'))
    phone_df = pd.read_csv(os.path.join(datadir, 'phone_brand_device_model.csv'))
    sample_submission_df = pd.read_csv(os.path.join(datadir, 'sample_submission.csv'))
    app_events_df = pd.read_csv(os.path.join(datadir, 'app_events.csv'), usecols=['event_id','app_id','is_active'], dtype={'is_active':bool})
    app_labels_df = pd.read_csv(os.path.join(datadir, 'app_labels.csv'))
    events_df = pd.read_csv(os.path.join(datadir, 'events.csv'), parse_dates=['timestamp'], index_col='event_id')

    if load_type == Load_Type.readdisk_writeHD5:
        store = pd.HDFStore(os.path.join(datadir, 'AllData_TalkingData.h5'))

        store['gender_age_train_df'] = gender_age_train_df
        store['gender_age_test_df'] = gender_age_test_df
        store['label_categories_df'] = label_categories_df
        store['phone_df'] = phone_df
        store['sample_submission_df'] = sample_submission_df
        store['app_events_df'] = app_events_df
        store['app_labels_df'] = app_labels_df
        store['events_df'] = events_df
        store.close()

elif load_type == Load_Type.readHD5:
    store = pd.HDFStore(os.path.join(datadir, 'AllData_TalkingData.h5'),'r')

    gender_age_train_df = store['gender_age_train_df']
    gender_age_test_df = store['gender_age_test_df']
    label_categories_df = store['label_categories_df']
    phone_df = store['phone_df']
    sample_submission_df = store['sample_submission_df']
    app_events_df = store['app_events_df']
    app_labels_df = store['app_labels_df']
    events_df = store['events_df']
    store.close()

print("gender_age_train_df : ")
print(gender_age_train_df.head())
print("gender_age_test_df : ")
print(gender_age_test_df.head())
print("label_categories_df : ")
print(label_categories_df.head())
print("phone_df : ")
print(phone_df.head())
print("sample_submission_df : ")
print(sample_submission_df.head())
print("app_events_df : ")
print(app_events_df.head())
print("app_labels_df : ")
print(app_labels_df.head())
print("events_df : ")
print(events_df.head())

phone_df = phone_df.drop_duplicates('device_id',keep='first').set_index('device_id')

gender_age_train_df['trainrow'] = np.arange(gender_age_train_df.shape[0])
gender_age_test_df['testrow'] = np.arange(gender_age_test_df.shape[0])

"""BRAND MODEL"""
brandencoder = LabelEncoder().fit(phone_df.phone_brand)
phone_df['brand'] = brandencoder.transform(phone_df['phone_brand'])
gender_age_train_df['brand'] = phone_df['brand']
gender_age_test_df['brand'] = phone_df['brand']
Xtr_brand = csr_matrix((np.ones(gender_age_train_df.shape[0]),
                       (gender_age_train_df.trainrow, gender_age_train_df.brand)))
Xte_brand = csr_matrix((np.ones(gender_age_test_df.shape[0]),
                       (gender_age_test_df.testrow, gender_age_test_df.brand)))
print('Brand features: train shape {}, test shape {}'.format(Xtr_brand.shape, Xte_brand.shape))

"""DEVICE MODEL"""
m = phone_df.phone_brand.str.cat(phone_df.device_model)
modelencoder = LabelEncoder().fit(m)
phone_df['model'] = modelencoder.transform(m)
gender_age_train_df['model'] = phone_df['model']
gender_age_test_df['model'] = phone_df['model']
Xtr_model = csr_matrix((np.ones(gender_age_train_df.shape[0]),
                       (gender_age_train_df.trainrow, gender_age_train_df.model)))
Xte_model = csr_matrix((np.ones(gender_age_test_df.shape[0]),
                       (gender_age_test_df.testrow, gender_age_test_df.model)))
print('Model features: train shape {}, test shape {}'.format(Xtr_model.shape, Xte_model.shape))

"""APP FEATURES"""
appencoder = LabelEncoder().fit(app_events_df.app_id)
app_events_df['app'] = appencoder.transform(app_events_df.app_id)
napps = len(appencoder.classes_)
deviceapps = (app_events_df.merge(events_df[['device_id']], how='left',left_on='event_id',right_index=True)
                       .groupby(['device_id','app'])['app'].agg(['size'])
                       .merge(gender_age_train_df[['trainrow']], how='left', left_index=True, right_index=True)
                       .merge(gender_age_test_df[['testrow']], how='left', left_index=True, right_index=True)
                       .reset_index())
deviceapps.head()

d = deviceapps.dropna(subset=['trainrow'])
Xtr_app = csr_matrix((np.ones(d.shape[0]), (d.trainrow, d.app)),
                      shape=(gender_age_train_df.shape[0],napps))
d = deviceapps.dropna(subset=['testrow'])
Xte_app = csr_matrix((np.ones(d.shape[0]), (d.testrow, d.app)),
                      shape=(gender_age_test_df.shape[0],napps))
print('Apps data: train shape {}, test shape {}'.format(Xtr_app.shape, Xte_app.shape))

"""APP LABEL FEATURES"""
app_labels_df = app_labels_df.loc[app_labels_df.app_id.isin(app_events_df.app_id.unique())]
app_labels_df['app'] = appencoder.transform(app_labels_df.app_id)
labelencoder = LabelEncoder().fit(app_labels_df.label_id)
app_labels_df['label'] = labelencoder.transform(app_labels_df.label_id)
nlabels = len(labelencoder.classes_)

devicelabels = (deviceapps[['device_id','app']]
                .merge(app_labels_df[['app','label']])
                .groupby(['device_id','label'])['app'].agg(['size'])
                .merge(gender_age_train_df[['trainrow']], how='left', left_index=True, right_index=True)
                .merge(gender_age_test_df[['testrow']], how='left', left_index=True, right_index=True)
                .reset_index())
devicelabels.head()

d = devicelabels.dropna(subset=['trainrow'])
Xtr_label = csr_matrix((np.ones(d.shape[0]), (d.trainrow, d.label)),
                      shape=(gender_age_train_df.shape[0],nlabels))
d = devicelabels.dropna(subset=['testrow'])
Xte_label = csr_matrix((np.ones(d.shape[0]), (d.testrow, d.label)),
                      shape=(gender_age_test_df.shape[0],nlabels))
print('Labels data: train shape {}, test shape {}'.format(Xtr_label.shape, Xte_label.shape))

"""CONCATENATE ALL FEATURES """
Xtrain = hstack((Xtr_brand, Xtr_model, Xtr_app, Xtr_label), format='csr')
Xtest =  hstack((Xte_brand, Xte_model, Xte_app, Xte_label), format='csr')
print('All features: train shape {}, test shape {}'.format(Xtrain.shape, Xtest.shape))

"""CROSS VALIDATION"""
targetencoder = LabelEncoder().fit(gender_age_train_df.group)
y = targetencoder.transform(gender_age_train_df.group)
nclasses = len(targetencoder.classes_)

def score(clf, random_state = 0):
    kf = StratifiedKFold(y, n_folds=5, shuffle=True, random_state=random_state)
    pred = np.zeros((y.shape[0],nclasses))
    for itrain, itest in kf:
        Xtr, Xte = Xtrain[itrain, :], Xtrain[itest, :]
        ytr, yte = y[itrain], y[itest]
        clf.fit(Xtr, ytr)
        pred[itest,:] = clf.predict_proba(Xte)
        # Downsize to one fold only for kernels
        return log_loss(yte, pred[itest, :])
        print("{:.5f}".format(log_loss(yte, pred[itest,:])), end=' ')
    print('')
    return log_loss(y, pred)

#Cs = np.logspace(-3,0,4)
#Cs = np.arange(0.01,0.025,0.001)
res = []
for C in Cs:
    #res.append(score(LogisticRegression(C = C)))
    res.append(score(LogisticRegression(C=C, multi_class='multinomial',solver='lbfgs')))
plt.semilogx(Cs, res,'-o');

clf = LogisticRegression(C=0.019, multi_class='multinomial',solver='lbfgs')
clf.fit(Xtrain, y)
pred = pd.DataFrame(clf.predict_proba(Xtest), index = gender_age_test_df.index, columns=targetencoder.classes_)
pred.head()
pred.to_csv('logreg_subm.csv',index=True)

# Get rid of duplicate device ids in phone
#==============================================================================
# events = pd.read_csv(os.path.join(datadir,'events.csv'),
#                      parse_dates=['timestamp'], index_col='event_id')
# appevents = pd.read_csv(os.path.join(datadir,'app_events.csv'),
#                         usecols=['event_id','app_id','is_active'],
#                         dtype={'is_active':bool})
# applabels = pd.read_csv(os.path.join(datadir,'app_labels.csv'))
#
# gatrain = pd.DataFrame(gender_age_train_df, index='device_id')
#==============================================================================

# Get rid of duplicate device ids in phone
#phone = phone.drop_duplicates('device_id',keep='first').set_index('device_id')
#events = pd.read_csv(os.path.join(datadir,'events.csv'),#
#                     parse_dates=['timestamp'], index_col='event_id')
#appevents = pd.read_csv(os.path.join(datadir,'app_events.csv'),
#                        usecols=['event_id','app_id','is_active'],
#                        dtype={'is_active':bool})
#applabels = pd.read_csv(os.path.join(datadir,'app_labels.csv'))


#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import division
import sys

"""
Author: Felix Hieber, Heidelberg University
counts the number of query terms found in the actual mate to see if retrieval can be any good.
"""
from collections import Counter

USAGE = "%s <score file>\n"%sys.argv[0]

def loadVector(s):
	return set([x.split(":")[0] for x in s.strip().split()])

def loadQueries(fname):
	Q = []
	f = open(fname)
	for l in f:
		try:
			qid,len,v = l.strip().split("\t")
			v = loadVector(v)
		except ValueError:
			qid,len = l.strip().split("\t")
			v = set()
		qid = int(qid)
		Q.append((qid,v))
	return Q

if __name__=='__main__':
	if len(sys.argv) != 2:
		sys.stderr.write("%s <queries> < docs\n"%sys.argv[0])
		sys.exit(1)

	Q = loadQueries(sys.argv[1])
	sys.stderr.write("%d queries loaded\n"%len(Q))

	s = 0.0
	qi = 0
	for l in sys.stdin:
		try:
			docid, dlen, v = l.strip().split("\t")
		except ValueError:
			continue
		docid = int(docid)
		if docid < Q[qi][0]:
			continue
		else:
			assert docid == Q[qi][0]
			v = loadVector(v)
			intersect = Q[qi][1].intersection(v)
			t = len(Q[qi][1].intersection(v))
			s += t
			#sys.stdout.write("%d\n"%t)
			qi += 1
		if qi == len(Q):
			break

	sys.stderr.write("Average number of overlapping terms: %.7f\n"%(s/len(Q)))


class Solution:
    def numUniqueEmails(self, emails):
        """
        :type emails: List[str]
        :rtype: int
        """

        # remove .
        for i, e in enumerate(emails):
          es = e.split('@')
          es[0] = es[0].replace('.', '').split('+')[0]
          emails[i] = "@".join(es)

        return len(set(emails))


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as seabornInstance
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsRegressor
from sklearn import metrics

def readCSV():
    dataSet = pd.read_csv("LinearMultivariateInput.csv")
    return dataSet

def LinearModel(historicWindow, predictionWindow, dataSet):

    returnList = []

    print(dataSet.describe())

    X = dataSet[['year', 'month', 'day', 'hour', 'minute', 'PAH', historicWindow]].values
    y = dataSet[predictionWindow].values

    selFeat = ['year', 'month', 'day', 'hour', 'minute', 'PAH', historicWindow]

    # plt.figure(figsize=(15, 10))
    # plt.tight_layout()
    # seabornInstance.distplot(dataSet['PAH'])
    # plt.show()

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

    regressor = KNeighborsRegressor(n_neighbors=7)
    regressor.fit(X_train, y_train)

    y_pred = regressor.predict(X_test)

    df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
    df1 = df.head(25)
    print(df1)

    df1.plot(kind='bar', figsize=(10, 8))
    plt.grid(which='major', linestyle='-', linewidth='0.5', color='green')
    plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
    #plt.show()

    # print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
    # print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
    # print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
    #returnList.append(movingAverage)
    returnList.append(historicWindow)
    returnList.append(predictionWindow)
    returnList.append(metrics.mean_absolute_error(y_test, y_pred))
    returnList.append(metrics.mean_squared_error(y_test, y_pred))
    returnList.append(np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
    return returnList

def main():
    dataSet = readCSV()
    df = pd.DataFrame(columns=['HistoricWindow', 'PredictionWindow', 'MAE', 'MSE', 'RMSE'])
    HistoricList = ['PAH-24', 'PAH-12', 'PAH-6', 'PAH-3', 'PAH-1']
    PredictionList = ['quarter', 'half', 'PAH+1', 'PAH+2', 'PAH+3', 'PAH+4', 'PAH+6', 'PAH+12', 'PAH+24']
    movingAverage = ['MPAH-6', 'MPAH-3']

    for i in HistoricList:
        for j in PredictionList:
            output = LinearModel(i, j, dataSet)
            print(output)
            df.loc[len(df)] = output

    df.to_csv("LinearImputationkNNOutput.csv", index=False, header=['HistoricWindow', 'PredictionWindow', 'MAE', 'MSE', 'RMSE'])
    print("done")

if __name__ == "__main__":
    main()


from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.externals import joblib
from sklearn.metrics import classification_report
from sklearn.svm import SVC
import pandas as pd
import numpy as np
from numpy.random import shuffle
import os
import sys
import argparse
class train_svm():

    def __init__(self, keypoints_path, npsave_path, command, model_path):

        self.keypoints_folder = keypoints_path
        self.command = command
        self.model_path = model_path
        files = os.listdir(self.keypoints_folder)
        first_time = True
        for filename in files:
            self.keypoints_path = self.keypoints_folder + filename + '/keypoints.txt'
            self.filename = filename
            self.npsave_path = self.keypoints_folder + filename + '/np_keypoints.txt'
            Feature, self.lenx, self.leny = self.feature_generate()
            Label = self.label_generate()
            if first_time == True:
                self.feature = Feature
                self.label = Label
                first_time = False
            else:
                self.feature = np.insert(self.feature, 0, Feature, axis=0)
                self.label = np.insert(self.label, 0, Label, axis=0)
            #print(self.feature[array])
            #print(type(self.feature))
        np.savetxt('/home/hts/hts2019/openpose/svm/feature.txt', self.feature, fmt = '%f')
        print('---loading feature done!---')
        print('feature sample size', np.shape(self.feature) )
        print()
            #self.label.append(self.label.generate())
        np.savetxt('/home/hts/hts2019/openpose/svm/label.txt', self.label, fmt = '%f')
        print('---loading label done!---')
        print('label size', np.shape(self.label))
        print()
        self.keyboard_control()

    def feature_generate(self):

        with open(self.keypoints_path, 'r') as file:
            my_data = file.readlines()
            count = 0
            array1 = np.zeros(33)
            array2 = np.zeros((1,33))
            for line in my_data:
                #print(line)
                count += 1
                if count == 34:
                    count = 0
                    #print('ok')
                    #print(array1)
                    array2 = np.insert(array2, 0, array1, axis=0)
                else:
                    array1[count-1] = line
                    #print(array1[count-1])
            #print(array2)
            array2 = np.delete(array2, -1, axis=0)
            #print(array2)
            axisx = np.shape(array2)[0]
            axisy = np.shape(array2)[1]
            #print(axisx, axisy)
            np.savetxt(self.npsave_path, array2, fmt = '%d')
            return array2, axisx, axisy

    def label_generate(self):

        array3 = np.empty((self.lenx, 1) )
        for index in range(self.lenx):
            if self.filename == 'a':
                array3[index] = 1
            if self.filename == 'b':
                array3[index] = 2
            if self.filename == 'c':
                array3[index] = 3
            if self.filename == 'd':
                array3[index] = 4
            if self.filename == 'e':
                array3[index] = 5
        #print(array3)
        return array3

    def svm_training(self):

        print('---start training svm---')
        #split dataset in two equal parts
        #print(np.shape(self.feature), np.shape(self.label))
        X_train, X_test, Y_train, Y_test = train_test_split(self.feature, self.label, test_size = 0.5, random_state = 0)
        np.savetxt('/home/hts/hts2019/openpose/svm/X_train.txt', X_train, fmt = '%f')
        np.savetxt('/home/hts/hts2019/openpose/svm/X_test.txt', X_test, fmt = '%f')
        np.savetxt('/home/hts/hts2019/openpose/svm/Y_train.txt', Y_train, fmt = '%d')
        np.savetxt('/home/hts/hts2019/openpose/svm/Y_test.txt', Y_test, fmt = '%d')
        print('---split data done!---')
        print()
        #set the parameters by cross-validation
        tuned_parameters = [{'kernel': ['rbf'], 'gamma': np.logspace(-1, 2, 4, endpoint = True),
                             'C': np.logspace(-1, 2, 4, endpoint = True)},
                             {'kernel': ['linear'],'C': np.logspace(-1, 2, 4, endpoint = True)},
                             {'kernel': ['poly'], 'C': np.logspace(-1, 2, 4, endpoint = True), 'degree': [1, 2, 3], 'gamma': np.logspace(-1, 2, 4, endpoint = True)}]
        scores = ['precision', 'recall']

        for score in scores:

            print ('tuning hyper-parameters for %s' % score)
            clf = GridSearchCV(SVC(decision_function_shape='ovr'), tuned_parameters, cv = 5, scoring = '%s_weighted' % score)
            print(np.shape(X_train), np.shape(Y_train.ravel()))
            clf.fit(X_train, Y_train.ravel())

        print("Best parameters set found on development set:")
        print()
        print(clf.best_params_)
        print()
        print("Grid scores on development set:")
        print()
        means = clf.cv_results_['mean_test_score']
        stds = clf.cv_results_['std_test_score']
        for mean, std, params in zip(means, stds, clf.cv_results_['params']):
            print("%0.3f (+/-%0.03f) for %r"
                % (mean, std * 2, params))
        # for params, mean_score, scores in clf.cv_results_:
        #     print("%0.3f (+/-%0.03f) for %r"
        #         % (mean_score, scores.std() * 2, params))
        print()

        print("Detailed classification report:")
        print()
        print("The model is trained on the full development set.")
        print("The scores are computed on the full evaluation set.")
        print()
        target_names = ['lying', 'lie on the side', 'sitting', 'standing']
        Y_true, Y_pred = Y_test.ravel(), clf.predict(X_test)
        np.savetxt('/home/hts/hts2019/openpose/svm/Y_true.txt', Y_true, fmt = '%d')
        np.savetxt('/home/hts/hts2019/openpose/svm/Y_pred.txt', Y_pred, fmt = '%d')
        print(classification_report(Y_true, Y_pred, target_names = target_names))
        print()

        print('SVM model saving ......')
        model_save_path = '/home/hts/hts2019/openpose/svm/train_madel.m'
        joblib.dump(clf, model_save_path)

        return

    def predict_pic(self):

        my_clf = joblib.load(self.model_path)
        my_pre = my_clf.predict(self.feature)
        target_names = ['lying', 'lie on the side', 'sitting', 'standing']
        #print(my_pre, target_names[my_pre])
        print(my_pre)

        return

    def predict_video(self):

        my_clf = joblib.load(self.model_path)
        print("Detailed classification report:")
        print()
        target_names = ['lying', 'lie on the side', 'sitting', 'standing']
        Y_true, Y_pred = self.label.ravel(), my_clf.predict(self.feature)
        np.savetxt('/home/hts/hts2019/openpose/svm/Y_true.txt', Y_true, fmt = '%d')
        np.savetxt('/home/hts/hts2019/openpose/svm/Y_pred.txt', Y_pred, fmt = '%d')
        print(classification_report(Y_true, Y_pred, target_names = target_names))

        return

    def keyboard_control(self):

        command = self.command
        try:
            if command == 'predict_pic':
                self.predict_pic()
            elif command == 'svm_training':
                self.svm_training()
            elif command == 'predict_video':
                self.predict_video()
            else:
                print("---Invalid Command!---")
        except Exception as e:
            print(e)

# read_data('/home/hts/hts2019/openpose/video_res/Res_keypoints/a/keypoints.txt')
if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("--keypoints_path", type = str, default = None,
                        help = "path to the folder for the json to be processed (default: None)")
    parser.add_argument("--npsave_path", type = str, default = None,
                        help = "path to the folder for the results to be saved (default: None)")
    parser.add_argument("--command", type = str, default = 'svm_training',
                        help = "next command(default: 'svm_training')")
    parser.add_argument("--model_path", type = str, default = None,
                        help="path to the model to be loaded (default: None)")
    # parser.add_argument("--label", type=str, default=None,
    #                     help="label (default: None)")
    args = parser.parse_args()
    keypoints_path = args.keypoints_path
    npsave_path = args.npsave_path
    command = args.command
    model_path = args.model_path
    # label = args.label
    train_svm(keypoints_path, npsave_path, command, model_path)


from adbk.book_app.models import Addressee, Phone, Mail
import csv

with open('99-contacts-addressees.csv', encoding='utf-8') as f:
    reader = csv.reader(f, delimiter=';')
    next(reader)
    objs = []
    for row in reader:
        obj = Addressee()
        obj.name = row[0]
        obj.sex = row[1]
        obj.live = row[2]
        obj.photo = row[3]
        obj.birth = row[4]
        objs.append(obj)
    Addressee.objects.bulk_create(objs)

with open('99-contacts-phones.csv', encoding='utf-8') as f:
    reader = csv.reader(f, delimiter=';')
    next(reader)
    objs = []
    for row in reader:
        obj = Phone()
        obj.user_id = Addressee.objects.get(pk=row[0])
        obj.phone_number = row[1]
        obj.phone_type = row[2]
        objs.append(obj)
    Phone.objects.bulk_create(objs)

with open('99-contacts-mails.csv', encoding='utf-8') as f:
    reader = csv.reader(f, delimiter=';')
    next(reader)
    objs = []
    for row in reader:
        obj = Mail()
        obj.user_id = Addressee.objects.get(pk=row[0])
        obj.mail_address = row[1]
        obj.mail_type = row[2]
        objs.append(obj)
    Mail.objects.bulk_create(objs)

# exec(open('import_script.py').read())


from obspy import read

from seisnn.data.io import write_pkl
from seisnn.utils import get_dir_list
from seisnn.plot import plot_dataset

pick_pkl = "/mnt/tf_data/catalog/new_pick.dataset"
predict_pkl_dir = "/mnt/tf_data/dataset/2018_02_18_predict"
predict_pkl_list = get_dir_list(predict_pkl_dir)

new_pick = []
try:
    for i, pkl in enumerate(predict_pkl_list):
        trace = read(pkl).traces[0]
        start_time = trace.stats.starttime
        for p in trace.picks:
            if p.evaluation_mode == "automatic":
                if not p.evaluation_status == "confirmed" and not p.evaluation_status == "rejected":
                    pick_time = p.time - start_time
                    plot_dataset(trace, xlim=(pick_time - 2, pick_time + 4))
                    key_in = input("Confirmed pick? [y/N]")
                    if key_in == "y" or key_in == "Y":
                        print("Current file... %d out of %d" % (i, len(predict_pkl_list)))
                        p.evaluation_mode = "manual"
                        p.evaluation_status = "reviewed"
                        new_pick.append(p)

        if i % 100 == 0:
            print("Current file... %d out of %d" % (i, len(predict_pkl_list)))

except KeyboardInterrupt:
    print("Keyboard interrupt.")
finally:
    write_pkl(new_pick, pick_pkl)
    print("Pick saved.")


import csv
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

from math import sqrt
from scipy.stats import norm

priceE = []
dateE = []
priceA = []
dateA = []

tahun = 2001
with open('AIR.PA bulanan.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    sementara = 0
    for row in csv_reader:
        if line_count == 0:
            #print(f'Column names are {", ".join(row)}')
            line_count += 1
        else:

            if line_count % 12 == 0:
                sementara = float(sementara / 12)
                priceE.append(sementara)
                dateE.append(tahun)
                tahun += 1
                sementara = 0
            else:
                sementara += float(row[1])
            #print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
            line_count += 1

tahun  = 2001
with open('BA bulanan.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    sementara = 0
    for row in csv_reader:
        if line_count == 0:
            #print(f'Column names are {", ".join(row)}')
            line_count += 1
        else:

            if line_count % 12 == 0:
                sementara = float(sementara / 12)
                priceA.append(sementara)
                dateA.append(tahun)
                tahun += 1
                sementara = 0
            else:
                sementara += float(row[1])
            #print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
            line_count += 1

def brownian(x0, n, dt, delta, out=None):

    x0 = np.asarray(x0)

    # For each element of x0, generate a sample of n numbers from a
    # normal distribution.
    r = norm.rvs(size=x0.shape + (n,), scale=delta*sqrt(dt))

    # If `out` was not given, create an output array.
    if out is None:
        out = np.empty(r.shape)

    # This computes the Brownian motion by forming the cumulative sum of
    # the random samples.
    np.cumsum(r, axis=-1, out=out)

    # Add the initial condition.
    out += np.expand_dims(x0, axis=-1)

    return out

import numpy
from pylab import plot, show, grid, xlabel, ylabel

# The Wiener process parameter.
deltaE = 10
deltaA = 50
# Total time.
T = 5.0
# Number of steps.
N = 5
# Time step size
dt = T/N
# Number of realizations to generate.
m = 4
# Create an empty array to store the realizations.
x = np.empty((m,N+1))
y = np.empty((m,N+1))
# Initial values of x.
x[:, 0] = priceE[len(priceE)-1]
y[:, 0] = priceA[len(priceA)-1]

brownian(x[:,0], N, dt, deltaE, out=x[:,1:])
brownian(y[:,0], N, dt, deltaA, out=y[:,1:])

tE = numpy.linspace(dateE[len(dateE)-1], dateE[len(dateE)-1]+5, N+1)
tA = numpy.linspace(dateA[len(dateA)-1], dateA[len(dateA)-1]+5, N+1)

# plotting the points
plt.plot(dateE,priceE)
plt.plot(dateA,priceA)
plt.xlabel('Bulan')
plt.ylabel('Harga (USD) and (EUR)')
plt.title('Saham Boeing (Oranye) dan Airbus (Biru) per Bulan'
          '\ndari September 2001 hingga Desember 2019'
          '\nDisertakan prediksi dengan metode Geometric Brownian Motion')
for k in range(m):
    plot(tE, x[k])
    plot(tA, y[k])
xlabel('t', fontsize=16)
ylabel('x', fontsize=16)
grid(True)
show()


'''
Crawl https://www.eia.gov/state/seds/seds-data-complete.php?sid=US
and retrieve all relevant data.
Store data in csv files in './data'.
Note: We ended up not using all the csv files scraped. The unused files
were moved to '../data/webscraped data not used'.
'''

import re
import os
import requests
from bs4 import BeautifulSoup
import pandas as pd

DATA_DIR = os.path.dirname(__file__)
OUTDIR = os.path.join(DATA_DIR, 'data')

def scrape_state_data():
    '''
    Scrapes data from webpage and outputs a csv file for each webpage.
    '''
    if not os.path.exists(OUTDIR):
        os.mkdir(OUTDIR)
    url_dict = make_url_dict()
    for k, v in url_dict.items():
        crawl_through_url(v).to_csv(os.path.join(OUTDIR, '{}.csv')\
            .format(k), encoding='utf-8', index=None, header=None)

def make_url_dict():
    '''
    Generates a dictionary of urls to scrape.
    '''
    url = 'https://www.eia.gov/state/seds/seds-data-complete.php?sid=US'
    page_request = requests.get(url)
    soup = BeautifulSoup(page_request.text, "html5lib")
    table_body = soup.find("table", {"class": "contable"})
    starting_url = 'https://www.eia.gov/statimport requestse/seds/'
    url_dict = dict()

    for row in table_body.findAll('tr'):
        html_link = row.find('a', href=True, text='\nHTML')
        if html_link:
            html_title = html_link['title'].lower().strip().replace(" ", "_")
            if ('energy' in html_title) and ('all_states' not in html_title):
                url_dict[html_title] = starting_url + html_link['href']

    return url_dict

def crawl_through_url(url):
    '''
    Crawls through given webpage, generating a table containing scraped data.

    Input:
        - url: url of webpage to scrape
    Output:
        - table containing data scraped from webpage
    '''
    page_request = requests.get(url)
    soup = BeautifulSoup(page_request.text, "html5lib")
    # state names dict
    state_dict, colnames = create_state_info_dict(soup)

    return create_state_table(state_dict, colnames)

def create_state_info_dict(soup):
    '''
    Creates dictionary containing state data.

    Input:
        - soup: beautiful soup object for a url
    Outputs:
        - state_dict: dictionary of state data scraped from webpage
        - colnames: column names corresponding with dictionary columns
    '''
    state_table = soup.find("table", {"class": "L2_toggle_table"})

    states = []
    for state in state_table.find_all('a', href=True):
        states.append(re.sub(r"\s+", " ", state.string).strip())
    state_dict = {state : {} for state in states}

    # map index to column name
    table = soup.find("table", {"class": "basic_table tpl"})
    table_body = table.find('tbody')

    colnames = []
    for x in table_body.findAll("td"):
        if x.attrs == {'colspan': '2'}:
            colnames.append(x.text)
    col_dict = dict(zip([1+i*2 for i in range(0, len(colnames), 1)], colnames))

    for row in table_body.findAll('tr'):
        row_list = [t.text.strip() for t in row.findAll('td')[1:]]
        if row_list and row_list[0] in state_dict:
            for i, r in enumerate(row_list):
                if i % 2 == 1:
                    state_dict[row_list[i-1]].update({col_dict[i]: r})

    state_dict = {k: v for k, v in state_dict.items() if len(v) > 0}

    return state_dict, colnames

def create_state_table(state_dict, colnames):
    '''
    Create table from dictionary and column names.

    Inputs:
        - state_dict: dictionary containing data
        - colnames: column names corresponding with dictionary columns

    Output:
        - state_df: dataframe containing data scraped from webpage
    '''
    state_dict = {k: v for k, v in sorted(state_dict.items(),
                                          key=lambda x: x[0])}
    state_df = pd.DataFrame(columns=['State'] + colnames)
    state_df['State'] = [state for state in state_dict]

    for colname in colnames:
        state_df[colname] = [col[colname] for col in state_dict.values()]

    return state_df

if __name__ == "__main__":
    scrape_state_data()


from django.shortcuts import render
from django.contrib.auth import authenticate, login

def main_page(request):

    '''
    The site is first loaded, or if JS is disabled and full page loads are required.
    '''

    if not "focused_content" in context.keys():
        context["focused_content"] = "primary"

    # Render primary content.
    if not "primary_content" in context.keys():
        if request.user.is_authenticated:
            context["primary_content"] = "find_friends/find_friends.html"
            context["find_friend_form"] = FindFriendForm()
        else:
            context["primary_content"] = "accounts/user_login.html"
            context["login_form"] = LoginForm()

    # Render secondary content.
    if not "secondary_content" in context.keys():
        try:
            context["secondary_content"] = request.session["secondary_content"]
        except:
            context["secondary_content"] = "content/about.html"
    if not "secondary_content_title" in context.keys():
        try:
            context["secondary_content_title"] = request.session["secondary_content_title"]
        except:
            context["secondary_content_title"] = "About SJMS"

    return render(request, "main_page/main_page.html", context)


#!/usr/bin/env python
import roslib; roslib.load_manifest('cob_mmcontroller')
import rospy
import actionlib
import time
from pr2_controllers_msgs.msg import *
from cob_srvs.srv import *
from cob_actions.msg import *
from cob_msgs.msg import *
from trajectory_msgs.msg import *

rospy.init_node('dual_arm_script')
rospy.sleep(1.0)

cob_lbr_client = actionlib.SimpleActionClient('/lbr_controller/joint_trajectory_action', JointTrajectoryAction)
lwr_client = actionlib.SimpleActionClient('/lwr/lbr_controller/joint_trajectory_action', JointTrajectoryAction)

cob_sdh_client = actionlib.SimpleActionClient('/sdh_controller/joint_trajectory_action', JointTrajectoryAction)
lwr_sdh_client = actionlib.SimpleActionClient('/lwr/sdh_controller/joint_trajectory_action', JointTrajectoryAction)

rospy.sleep(2.0)

lbr_joint_names = ["lbr_1_joint","lbr_2_joint","lbr_3_joint","lbr_4_joint","lbr_5_joint","lbr_6_joint","lbr_7_joint"]
sdh_joint_names = ["sdh_thumb_2_joint", "sdh_thumb_3_joint", "sdh_finger_11_joint", "sdh_finger_12_joint", "sdh_finger_13_joint", "sdh_finger_21_joint", "sdh_finger_22_joint", "sdh_finger_23_joint"]

home = JointTrajectory()
home.joint_names = lbr_joint_names
point=JointTrajectoryPoint()
point.positions=[0,0,0,0,0,0,0]
point.time_from_start=rospy.Duration(3)
home.points.append(point)

grasp = JointTrajectory()
grasp.joint_names = lbr_joint_names
point=JointTrajectoryPoint()
point.positions=[0.5, 0.8, 0.8, -0.8, 0.8, 0.8, -0.3]
point.time_from_start=rospy.Duration(3)
grasp.points.append(point)

cylOpen = JointTrajectory()
cylOpen.joint_names = sdh_joint_names
point=JointTrajectoryPoint()
point.positions=[-0.7854,1.0472,0.0,-0.7854,1.0472,0.0,-0.7854,1.0472]
point.time_from_start=rospy.Duration(2)
cylOpen.points.append(point)

cylClose = JointTrajectory()
cylClose.joint_names = sdh_joint_names
point=JointTrajectoryPoint()
point.positions=[0.0,1.0472,0.0,0.0,1.0472,0.0,0.0,1.0472]
point.time_from_start=rospy.Duration(2)
cylClose.points.append(point)

goal = JointTrajectoryGoal()
sdhgoal = JointTrajectoryGoal()

while not rospy.is_shutdown():
    goal.trajectory = home
    goal.trajectory.header.stamp = rospy.Time.now()
    cob_lbr_client.send_goal(goal)
    lwr_client.send_goal(goal)
    cob_lbr_client.wait_for_result()
    lwr_client.wait_for_result()

    sdhgoal.trajectory = cylOpen
    sdhgoal.trajectory.header.stamp = rospy.Time.now()
    cob_sdh_client.send_goal(sdhgoal)
    lwr_sdh_client.send_goal(sdhgoal)
    lwr_sdh_client.wait_for_result()
    cob_sdh_client.wait_for_result()

    goal.trajectory = grasp
    goal.trajectory.header.stamp = rospy.Time.now()
    cob_lbr_client.send_goal(goal)
    lwr_client.send_goal(goal)
    cob_lbr_client.wait_for_result()
    lwr_client.wait_for_result()

    sdhgoal.trajectory = cylClose
    sdhgoal.trajectory.header.stamp = rospy.Time.now()
    cob_sdh_client.send_goal(sdhgoal)
    lwr_sdh_client.send_goal(sdhgoal)
    lwr_sdh_client.wait_for_result()
    cob_sdh_client.wait_for_result()


import random

perguntas = [
    ("Quanto é 5 + 3? ", 8),
    ("Quanto é a raiz quadrada de 16? ", 4),
    ("Quanto é 3 * 3? ", 9),
    ("Quano é 28 - 13? ", 15),
    ("Quanto é 20 / 4? ", 5),
    ("Quanto é 5 ^ 3? ", 125)
]
respostas = []
perguntasAleatorias = random.choices(perguntas, k = 3)

for x in range(len(perguntasAleatorias)):
    respostas.append(int(input(perguntasAleatorias[x][0])))

print("Gabarito:")
for x in range(len(perguntasAleatorias)):
    print(perguntasAleatorias[x])

acertos = 0
for x in range(len(perguntasAleatorias)):
    if perguntasAleatorias[x][1] == respostas[x]:
        acertos = acertos + 1

print("Você respondeu", acertos, "perguntas corretamente.")

if acertos >= 2:
    print("Você foi APROVADO!")
else:
    print("Você foi REPROVADO!")


# Write a program that asks for two numbers
# Thw first number represents the number of girls that comes to a party, the
# second the boys
# It should print: The party is exellent!
# If the the number of girls and boys are equal and there are more people coming than 20
#
# It should print: Quite cool party!
# It there are more than 20 people coming but the girl - boy ratio is not 1-1
#
# It should print: Average party...
# If there are less people coming than 20
#
# It should print: Sausage party
# If no girls are coming, regardless the count of the people

girls = int(input('How many girls came to the party?: '))
boys = int(input('How many boys came to the party?: '))

#if int(girls) == int (boys) and int(girls) + int (boys) >= 20:
#    print('The party is exellent!')
#elif int(girls) != int (boys) and int(girls) + int (boys) >= 20:
#    print('Quite cool party!')
#elif int(girls) + int (boys) < 20:
#    print('Average party...')
#elif int(girls) == 0 and int(boys) != 0:
#    print('Sausage party')

if int(girls) == 0 and int(boys) != 0:
    print('Sausage party')
elif int(girls) + int (boys) < 20 and int(girls) != 0:
    print('Average party...')
elif int(girls) != int (boys) and int(girls) + int (boys) >= 20 and int(girls) != 0:
     print('Quite cool party!')
else:
    print('The party is exellent!')


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

def main():
  while(True):
    n = int(input())
    res = {}
    strg = ""
    flag = True
    for i in range(n):
      strg = input()
      if "=" not in strg: continue
      temp = strg.split("=")
      if len(temp) < 2: continue
      left = temp[0]
      right = temp[1]
      right_l = []
      right_l = right.split("+")
      val = 0
      s = ""
      for ele in right_l:
        s = ele.replace(' ','')
        if s.isdigit(): break
        if s in res: val += res[s]
        else:
          print("NA")
          flag = False
      k = left.replace(' ','')
      if val == 0 and s.isdigit(): val = int(s)
      if i == n-1 and flag: print(val)
      res[k] = val

if __name__ == '__main__':
  main()


from similarity_ratings_tests import *
from subset_experiments import *
import sys
import os
import datetime
# Used to compute all tests in a single run

now = datetime.datetime.now()

RESULTS_PATH = 'results/'

def perform_complete_test_suite(subset_test_filename, zero_shot_filename, spearman_filename, table_filename, subset = False, zero = False, spearman = False, tables = False, projection_modes = 'best', verbosity = 0):
    """
    Computes all test necessary for the ba thesis
    :param subset_test_filename: Path for saving subset tests.
    :param zero_shot_filename: Path for saving zero shot tests.
    :param spearman_filename: Path for spearman tests.
    :param table_filename: Path for saving explicit tables.
    :param subset: Flag to include or exclude subset tests from the test suite.
    :param zero: Flag to include or exclude zero shot tests from the test suite.
    :param spearman: Flag to include or exclude spearman tests from the test suite.
    :param tables: Flag to include or exclude explicit tables from the test suite.
    :param projection_modes: certain keywords for specific list of projection modes to test.
    :param verbosity: determines if errors are printed or not.
    :return: null
    """
    print("Subset = {}, Zero = {}, Spearman = {}, Table = {}, projection_modes = {}, Verbosity = {}".format(subset,zero,spearman,tables,projection_modes,verbosity))
    result_dir = RESULTS_PATH + '{}_{}_{}'.format(now.year,now.month,now.day)

    if not '{}_{}_{}'.format(now.year,now.month,now.day) in os.listdir(RESULTS_PATH):
            os.mkdir(result_dir)

    projection_mode_list = PROJECTION_MODE_LIST
    if projection_modes == 'best':
        projection_mode_list = SMALL_PROJECTION_MODE_LSIT
    elif projection_modes == 'medium':
        projection_mode_list = MEDIUM_PROJECTION_LIST
    elif projection_modes == 'test':
        projection_mode_list = TEST_PROJECTION_MODE_LIST

    check_attribute_set_sizes()

    if subset:
        sys.stdout = open(result_dir + '/' + subset_test_filename, 'w')

        sys.stdout.flush()

        print("\n\n----------------------Subset-Tests----------------------".upper())
        perform_subset_tests(train_subsets=ATTRIBUTE_SETS,
                             test_subsets=ATTRIBUTE_SETS,
                             tables=False, quantitive=True,
                             proj_mode_list=projection_mode_list,
                             train_test_exclusivity=False,
                             verbosity=verbosity)

        sys.stdout.flush()

    if zero:
        sys.stdout = open(result_dir + '/' + zero_shot_filename, 'w')

        print("\n\n----------------------Subset-Tests (Testmenge explizit ohne die Attribute aus der Trainingsmenge)----------------------".upper())
        perform_subset_tests(train_subsets=[attribute_set for attribute_set in ATTRIBUTE_SETS if attribute_set[0] != 'ALL'],
                             test_subsets=[ALL_ATTRIBUTES],
                             tables=False, quantitive=True,
                             proj_mode_list=projection_mode_list,
                             train_test_exclusivity=True,
                             verbosity=verbosity)

        sys.stdout.flush()

    if spearman:
        sys.stdout = open(result_dir + '/' + spearman_filename + '.txt', 'w')
        print("\n\n----------------------spearman tests----------------------".upper())
        for attribute_set in ATTRIBUTE_SETS:
            print("\n---------------------Teste mit Attribut-Menge {}----------------".format(attribute_set[0]))
            similarity_experiment(attribute_set,attribute_set,
                                  ['add','nn_weighted_adjective_noun_identity','mult','mitchell_lapata_reversed_2','mitchell_lapata_2'], rating_cutoff_list=[4,5,6],
                                  verbosity=verbosity)

        sys.stdout.flush()

        sys.stdout = open(result_dir + '/' + spearman_filename + '_no_tables.txt', 'w')
        print("\n\n----------------------spearman tests----------------------".upper())
        for attribute_set in ATTRIBUTE_SETS:
            print("\n---------------------Teste mit Attribut-Menge {}----------------".format(attribute_set[0]))
            similarity_experiment(attribute_set,attribute_set,
                                  ['add','nn_weighted_adjective_noun_identity','mult','mitchell_lapata_reversed_2','mitchell_lapata_2'], rating_cutoff_list=[0,1,2,3,4,5,6],
                                  tables=False,
                                  verbosity=verbosity)

        sys.stdout.flush()

    if tables:
        sys.stdout = open(result_dir + '/' + table_filename, 'w')

        print("\n\n----------------------Tabellen----------------------".upper())
        perform_subset_tests(train_subsets=ATTRIBUTE_SETS,
                             test_subsets=ATTRIBUTE_SETS,
                             tables=True, quantitive=False,
                             proj_mode_list=projection_mode_list,
                             train_test_exclusivity=False,
                             verbosity=verbosity)

arglist = sys.argv[1:]

# print(arglist)

subset, zero, spearman, tables, verbosity, projection_modes = False, False, False, False, 0, 'all'

if arglist[0] == 'True':
    # print(arglist[0])
    subset = True
if arglist[1] == 'True':
    # print(arglist[1])
    zero = True
if arglist[2] == 'True':
    # print(arglist[2])
    spearman = True
if arglist[3] == 'True':
    # print(arglist[3])
    tables = True

if arglist[4]:
    verbosity = int(arglist[4])

if arglist[5]:
    projection_modes = arglist[5]

# perform_complete_test_suite('subset_results.txt','zero_shot_results.txt','spearman_results','tables.txt', subset=subset, zero=zero, spearman=spearman, tables=tables, projection_modes=projection_modes, verbosity = verbosity)

check_attribute_set_sizes()


'''
Created on Feb 6, 2011

@author: jehan
'''

import pyexiv2
import mimetypes
from filemovers import mover
from filemovers.mover import Mover
import os, re
import datetime

class DumpToRenameMover(Mover):
    _config = None
    def __init__(self,config):
        self._config = config
        mimetypes.init()

    def handleFile(self,handledFilePath):
        print("handleFile", handledFilePath)

        # extract the date of the file
        imgDatetime = self.getImageDate(handledFilePath)

        # no date? Ignore the file.
        if None != imgDatetime:
            print("Handling",os.path.basename(handledFilePath),imgDatetime)
            dateFolderPath = self.makeFolder(imgDatetime, handledFilePath)
            mover.moveFile(handledFilePath,
                           os.path.join(dateFolderPath,os.path.basename(handledFilePath)))

        # do some trailing work
#        for root,folders,files in os.walk(self._config.dump_folder): #@UnusedVariable
#            for fold in folders:
#                if 0 == len(os.listdir(os.path.join(root,fold))):
#                    os.rmdir(os.path.join(root,fold))

    def makeFolder(self, imgDatetime, handledFilePath):
        print("making a new folder")
        dateFolders = mover.findDateFolders(self._config.rename_folder)

        if mover.mkDateFolderName(imgDatetime) in dateFolders:
            print("folder exists")
            dateFolderPath = dateFolders[mover.mkDateFolderName(imgDatetime)][1]
        else:
            print("creating new folder")
            # make dir if it does not exist
            dateFolderPath = os.path.join(self._config.rename_folder,
                                   mover.mkDateFolderName(imgDatetime))

            if self._config.use_original_folder_names:
                print("import original folder name")
                originalFolderName =  os.path.basename(os.path.dirname(handledFilePath))
                dateFolderPath += ' '+ originalFolderName
            print("make dir",dateFolderPath)

            os.mkdir( dateFolderPath )

            if self._config.make_fusioned_folder_names:
                print("try to merge folders")
                self.makeRangeFolders(imgDatetime, dateFolders)
            else:
                print("I won't try merging folders")

        return dateFolderPath

    def moveFile(self,dateFolderPath,handledFilePath):
        print("moving...")
        fileName = os.path.splitext(os.path.basename(handledFilePath))
        dest  = os.path.join(dateFolderPath,fileName[0]+fileName[1])
        i = 0
        while os.path.exists(dest):
            i+=1
            dest  = os.path.join(dateFolderPath,fileName[0]+'_'+str(i)+fileName[1])
            if 100 == i:
                print("Tried renaming the file up to "+fileName[0]+'_'+str(i)+fileName[1]+" without success.")
                print("Giving up since we might be in a loop. File not moved:",handledFilePath)
                return
        os.rename(handledFilePath, dest)
        print("moved")

    def getImageDate(self,handledFilePath,useFilesystem=True):
        print("Reading metadata:",handledFilePath)
        imgDatetime = None
        try:
            image = pyexiv2.ImageMetadata(handledFilePath)
            image.read()
            #print image.exifKeys()
            try:
                imgDatetime = image['Exif.Photo.DateTimeOriginal']
                #print "found DateTimeOriginal"
            except KeyError:
                imgDatetime = image['Exif.Image.DateTime']
                #print "found DateTime"
            imgDatetime = imgDatetime.value
            if type(imgDatetime) != datetime.datetime:
                if type(imgDatetime) == str:
                    print("pyexif returned a string rather than a datetime object")
                    dateParse = re.match(r"^(\d{4}):(\d{2}):(\d{2}).*",imgDatetime).groups()
                    imgDatetime = datetime.datetime(int(dateParse[0]),int(dateParse[1]),int(dateParse[2]));
                else:
                    print("pyexif returned nothing worthwhile")
                    raise Exception()
        except Exception as e:
            if type(e) == IOError or type(e) == KeyError:
                print("Probably no EXIF data :",e)
                if useFilesystem:
                    print("is it an image or a video?")
                    t = mimetypes.guess_type(handledFilePath)
                    print("Mime : ",t)
                    if type(t[0]) == type("") and (
                            t[0].startswith('video') or  t[0].startswith('image')
                            ):
                        print("It's an image or a video. Trying the last modif date")
                        imgDatetime = datetime.datetime.fromtimestamp(os.path.getmtime(handledFilePath))
                        print("got:", imgDatetime)
            else: raise
        print("date time is",imgDatetime)
        return imgDatetime

    def makeRangeFolders(self,imgDatetime,dateFolders):
        # append to a prevRange if dir exists for previous or next day
        # search for possible matches
        prevDay = imgDatetime-datetime.timedelta(days=1)
        #prev = os.path.join(self._config.rename_folder,mkDateFolderName(prevDay))

        nextDay = imgDatetime+datetime.timedelta(days=1)
        #next = os.path.join(self._config.rename_folder,mkDateFolderName(nextDay))

        rangeFolders = mover.findRangeFolders(self._config.rename_folder)

        prevRange = None

        if mover.mkDateFolderName(prevDay) in dateFolders:
            print("prev exists")
            if len(rangeFolders):
                for start,finish,rangeFoldCurrentPath in rangeFolders: #@UnusedVariable
                    if mover.sameDay(finish,prevDay):
                        print("prevRange with prev")
                        prevRange = rangeFoldCurrentPath
                        break

            if None != prevRange:
                newPrevRange = mover.mkRangeFolderName(start, imgDatetime,self._config.rename_folder)
                print("we have a prevRange. Rename from",prevRange,"to",newPrevRange)
                os.rename(prevRange,newPrevRange)
                prevRange = newPrevRange
                prevRangeStart = start
            else:
                prevRange = mover.mkRangeFolderName(prevDay,imgDatetime,self._config.rename_folder)
                print("No prevRange. We make it from ",prevDay,"and",imgDatetime,":",prevRange)
                os.mkdir(prevRange)
                prevRangeStart = prevDay

        nextRange = None
        if mover.mkDateFolderName(nextDay) in dateFolders:
            print("next exists")
            if len(rangeFolders):
                for start,finish,rangeFoldCurrentPath in rangeFolders: #@UnusedVariable
                    if mover.sameDay(start,nextDay):
                        print("nextRange with next")
                        nextRange = rangeFoldCurrentPath
                        break

            if None != nextRange:
                print("We have a nextRange",nextRange)
                if None == prevRange:
                    newNextRange = mover.mkRangeFolderName(imgDatetime,finish,self._config.rename_folder)
                    print("but no prev: rename nextRange from ",nextRange,"to",newNextRange)
                    os.rename(nextRange,newNextRange)
                else:
                    mergeRange = mover.mkRangeFolderName(prevRangeStart,finish,self._config.rename_folder)
                    print("with prev: We rename from ",prevRange,"to",mergeRange)
                    os.rename(prevRange,mergeRange)
                    print("we delete",nextRange)
                    os.rmdir(nextRange)
            else:
                if None == prevRange:
                    nextRange = mover.mkRangeFolderName(imgDatetime,nextDay,self._config.rename_folder)
                    print("No nextRange. We make it:",nextRange)
                    os.mkdir(nextRange)
                else:
                    print("No nextRange but we have a prevRange: we merge")
                    mergeRange = mover.mkRangeFolderName(prevRangeStart,nextDay,self._config.rename_folder)
                    print("with prev: We rename from ",prevRange,"to",mergeRange)
                    os.rename(prevRange,mergeRange)
        # link in prevRange now that we have one


import ev3dev.ev3 as ev3
import robot_controller as robo
import time

class DataContainer(object):
    """ Helper class that might be useful to communicate between
    different callbacks."""

    def __init__(self):
        self.running = True

def main():
    """First, performs an arm calibration, then robot maintains a straight
    path unless it detects a human motion with the IR sensor or it detects
    a color with the color sensor. If the color
    sensor sees green, the robot will stop moving."""

    print("--------------------------------------------")
    print("------------Identify Plant------------------")
    print("--------------------------------------------")
    ev3.Sound.speak("Identifying plant").wait()
    print("Press Back to exit this program.")

    robot = robo.Snatch3r()
    robot.arm_calibration()

    while True:
        if robot.ir_sensor.proximity < 10:
            ev3.Sound.beep()
            robot.right(50, 50)  # swerves away from human
            time.sleep(2.0)
        if robot.color_sensor.color == 3:  # Green color = id: 3
            robot.stop()
            ev3.Sound.speak("Identified plant")
            break

        robot.forward(50, 50)

def handle_shutdown(button_state, dc):
    """Exit the program."""
    if button_state:
        dc.running = False

# ----------------------------------------------------------------------
# Calls  main  to start the ball rolling.
# ----------------------------------------------------------------------
main()


# Discord bot code!
# Quick info
# Use: Runs a Discord bot
# Libraries: JSON, Requests, Discord, Asyncio
# Indent type: 4 spaces

# Links
# http://discordpy.readthedocs.io/en/latest/api.html - Discord.py API reference, lots of commands here.
# https://discordapp.com/developers/applications/me - My apps page on Discord.
# https://leovoel.github.io/embed-visualizer/ - Lets you generate code for posting Embeds. Click generate and select Discord.py from the dropdown.
# https://discordapi.com/permissions.html - Generates bot invites, straightforward.

#Intro code, no need to modify unless for librarys (Eg. Time)

print('Importing JSON')        #JSON library, allows you to get variables from JSON files online. NOT NEEDED
import json
print('Importing REQUESTS')    #Lets you send stuff to the internet for response, used for random cats. NOT NEEDED
import requests
print('Importing DISCORD')     #Discord.py library, allows you to interface with Discord API easily. NEEDED
import discord
print('Importing ASYNCIO')     #Asyncio library, bundled with Discord.py, allows you to multiprocess, I think, NEEDED HOWEVER.
import asyncio

#Even more technical wizardry, even I don't understand this.

client = discord.Client()

@client.event
async def on_ready():
    print('Signed in as ' + client.user.name + '! ID is ' + client.user.id + '!') #What it says in the command line when it opens

    await client.change_presence(game=discord.Game(name='>>>help for help')) #Use this to set a game status.

@client.event

#End of intro code.

#Asyncio first thing, senses when a message comes in. !test and !sleep are the originals from Discord.py's github

async def on_message(message):
    if message.content.startswith('!test'):  #Checks if the message begins with test. If you want it to be equal, replace it with [ if message.content == '!test': ]
        counter = 0
        tmp = await client.send_message(message.channel, 'Calculating messages...') #Sends a message to a channel, message.channel is where the command was posted.
        async for log in client.logs_from(message.channel, limit=100):               #I have no idea for this, only the guy who made it does, but it counts messages.
            if log.author == message.author:
                counter += 1

        await client.edit_message(tmp, 'You have {} messages.'.format(counter))    #.format() replaces any instance of {} in a string with the thing in (), useful, huh.

    elif message.content.startswith('!sleep'):               #This makes the bot sleep, like time.sleep
        await asyncio.sleep(5) #Sleep for 5 seconds
        await client.send_message(message.channel, 'Done sleeping')  #Send to the channel from which the command was posted 'done sleeping'

    if message.content == ('>>>catme'):          #The catme command from Harpy, useful to have and experiment with.
       print(str(message.author) + ' just requested a cat in ' + str(message.server) + ' : ' + str(message.channel))   #Writes in console that someone wanted a cat.
       #import from catimages.py, complex!
       response = (requests.get("http://random.cat/meow"))
       response = str(response.content)
       print(response)
       response = response.replace("b'","")
       response = response.replace("'","")
       response = response.replace("\\","")
       print(response)
       data = json.loads(response)
       print (data["file"])
       #import end. Complexity END!

       line = data["file"] #Sets a variable with the variable file from the JSON

       await client.send_message(message.channel, 'Meow! ' + data["file"])  #Sends a message with the cat link.
       print('Responded!')

    if message.content.startswith('>>>whereami'):  #If a command starts with >>>whereami, an example, it should be message.content == ('>>>catme')
       print(str(message.author) + ' just requested location in ' + str(message.server) + ' : ' + str(message.channel)) #Posts in console
       await client.send_message(message.channel, 'User: ' + str(message.author)) #Sends to Discord the username of the request
       await client.send_message(message.channel, 'Channel: ' + str(message.channel)) #Sends the channel of the request
       await client.send_message(message.channel, 'Server: ' + str(message.server)) #Sends the server of the request

       print('Responded!') #Posts in console

    if message.content == ('>>>testme'):  #If a command is testme
       print(str(message.author) + ' just requested help in ' + str(message.server) + ' : ' + str(message.channel)) #post to console
       await client.send_message(message.channel, '''This is a line.
This is another line.''')                                                       #This posts in the channel
       print('Responded!') #Post in console.

client.run('token') #runs the bot, replace the 'token' with your bot token, which is on the bot's page under 'token:'


import turtle

def draw():
    window = turtle.Screen()
    window.title("The Star!!!! | 5805001111")
    window.bgcolor("red")

    shape =  turtle.Turtle()
    shape.shape('circle')
    shape.pensize(4)
    shape.speed(1)
    shape.shapesize(5,5)

    shape.goto(0, 200)
    shape.goto(-150, -150)
    shape.goto(200, 100)
    window.bgcolor("pink")
    shape.goto(-200, 100)
    shape.goto(150, -150)
    shape.goto(0, 200)
    shape.end_fill()

    text =  turtle.Turtle()
    text.write('The Star', move=False, align='center', font=("Arial", 60, "normal"))

    shape.shapesize(0.1,0.1)
    window.bgcolor("purple")
    window.mainloop()

    window.exitonclick()

draw()


import re
import locale
from grab.spider import Spider, Task
from datetime import datetime

class MySpider(Spider):

    initial_urls = []

    def prepare(self):
        locale.setlocale(locale.LC_TIME, 'ru_RU.UTF-8')

        self.date_time = []

    def task_initial(self, grab, task):
        pages_nmb = grab.doc.select('//a[@class="block br3 brc8 large tdnone lheight24"]')[-1].text()

        for i in range(int(pages_nmb)):
        # for i in range(2):
            url = '{}?page={}'.format(self.initial_urls[0], i+1)
            yield Task('page', url=url)

    def task_page(self, grab, task):
        ads_list = grab.doc.select('//a[@class="marginright5 link linkWithHash detailsLink"]')

        for elem in ads_list:
            yield Task('ad', url=elem.attr('href'))

    def task_ad(self, grab, task):
        date_time_info = grab.doc.select('//div[@class="offer-titlebox__details"]/em')

        for elem in date_time_info:
            text = elem.text()

            time = re.search(r'\d{2}:\d{2}', text)[0]
            date = re.search(r'\d{1,2} [а-я]+ \d{4}', text)[0]
            date_time = '{} {}'.format(date, time)

            self.date_time.append(datetime.strptime(date_time, '%d %B %Y %H:%M'))


"""Input and output helpers to load in data.
"""
import numpy as np

def read_dataset_tf(path_to_dataset_folder,index_filename):
    """ Read dataset into numpy arrays with preprocessing included
    Args:
        path_to_dataset_folder(str): path to the folder containing samples and indexing.txt
        index_filename(str): indexing.txt
    Returns:
        A(numpy.ndarray): sample feature matrix A = [[1, x1],
                                                     [1, x2],
                                                     [1, x3],
                                                     .......]
                                where xi is the 16-dimensional feature of each sample

        T(numpy.ndarray): class label vector T = [[y1],
                                                  [y2],
                                                  [y3],
                                                   ...]
                             where yi is 1/0, the label of each sample
    """
    ###############################################################
    # Fill your code in this function
    ###############################################################
    # Hint: open(path_to_dataset_folder+'/'+index_filename,'r')
    indexFile = open(path_to_dataset_folder + '/' + index_filename, 'r')
    index = indexFile.read().split()
    indexFile.close()
    # print(index)
    # print(type(index))
    T = []
    A = []
    # print(len(index))
    length = int(len(index) / 2)
    for i in range(0, length):
        row = [1]

        label = index[2 * i]
        if int(label) == -1:
            label = 0
        T.append(int(label))
        dataFile = open(path_to_dataset_folder + '/' + index[2 * i + 1], 'r')
        data = dataFile.read().split()
        dataFile.close()
        # print(data)
        for ele in data:
            # print(ele)
            # print(type(ele))
            # print('ele=', float(ele))
            # print(row)
            row.append(float(ele))
        A.append(row)
        dataFile.close()
    # print(T)
    T = np.array(T)
    A = np.array(A)

    # print('T=', T)
    # print('A=', A)

    return A, T

# a,t = read_dataset_tf(path_to_dataset_folder='data/trainset', index_filename='indexing.txt')
# print(t)