import json
from unittest import mock

from django.test import TestCase
from django.test import Client

from ticketer.recordlocator import views
from .models import Ticket

def mock_generator(length=1):
    """ A mock record locator generator that returns the same record locator
    each time. """

    return 'XXXXXXXX'

class TicketCreationTests(TestCase):
    def test_create_new_ticket(self):
        """ Test the creation of a new ticket. """
        views.create_new_ticket(20852, 'XXYZZYCD')
        ticket = Ticket.objects.filter(record_locator='XXYZZYCD')[0]
        self.assertEqual(ticket.zip_code, '20852')

    def test_generate_backup_locator(self):
        locator = views.generate_backup_locator()
        self.assertTrue('-' in locator)
        self.assertEqual(16, len(locator))

    def test_create_tickets(self):
        tickets, failures = views.create_tickets(20852, 5)
        self.assertEqual(5, len(tickets) + len(failures))

        # Failures should be extremely rare
        self.assertTrue(len(tickets) > 0)

        locators = [t.record_locator for t in tickets]

        # Locators are unique with respect to each other.
        self.assertEqual(len(locators), len(set(locators)))

        for l in locators:
            ticket = Ticket.objects.get(record_locator=l)
            self.assertNotEqual(None, ticket)
            self.assertEqual(ticket.zip_code, '20852')

    @mock.patch(
        'ticketer.recordlocator.views.generator.safe_generate', mock_generator)
    def test_create_unique_ticket(self):
        ticket_one = views.create_unique_ticket(16801)
        self.assertEqual(ticket_one.record_locator, 'XXXXXXXX')

        ticket_two = views.create_unique_ticket(16801)
        self.assertTrue('-' in ticket_two.record_locator)
        self.assertTrue('XXXXXXXX' in ticket_two.record_locator)

        ticket_three = views.create_unique_ticket(20002)
        self.assertTrue('-' in ticket_three.record_locator)
        self.assertTrue('XXXXXXXX' in ticket_three.record_locator)

        self.assertNotEqual(
            ticket_two.record_locator, ticket_three.record_locator)

class RecordLocatorAPITests(TestCase):
    """ Tests for the RecordLocator API. """

    def test_api_generate_locators(self):
        c = Client()
        response = c.get('/api/tickets/issue/?zip=20852')
        self.assertEqual(response.status_code, 200)
        r = json.loads(response.content.decode('utf8'))
        self.assertTrue('locators' in r)
        self.assertEqual(len(r['locators']), 1)

    def test_api_multiple_locators(self):
        c = Client()
        response = c.get('/api/tickets/issue/?zip=20852&num_locators=50')
        self.assertEqual(response.status_code, 200)
        r = json.loads(response.content.decode('utf8'))
        self.assertTrue('locators' in r)
        self.assertEqual(len(r['locators']), 50)

class TicketGeneratorTest(TestCase):
    """ Tests for the RecordLocator API. """

    def test_ticket_zip_match(self):
        c = Client()
        response = c.get('/api/tickets/issue/?zip=91381')
        self.assertEqual(response.status_code, 200)
        r = json.loads(response.content.decode('utf8'))
        self.assertEqual(
            '91381',
            Ticket.objects.get(
                record_locator=r['locators'][0]['record_locator']).zip_code)


#calss header
class _SWAY():
	def __init__(self,):
		self.name = "SWAY"
		self.definitions = [u'to move slowly from side to side: ', u'to cause something to move or change: ', u'to persuade someone to believe or do one thing rather than another: ']

		self.parents = []
		self.childen = []
		self.properties = []
		self.jsondata = {}

		self.specie = 'verbs'

	def run(self, obj1 = [], obj2 = []):
		return self.jsondata


from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    class UserRole:
        USER = 'user'
        ADMIN = 'admin'
        MODERATOR = 'moderator'
        choices = [
            ('user', 'user'),
            ('admin', 'admin'),
            ('moderator', 'moderator'),
        ]

    bio = models.TextField(
        verbose_name='user bio',
        blank=True,
        null=True
    )
    email = models.EmailField(
        verbose_name='user email',
        unique=True,
    )
    role = models.CharField(
        verbose_name='user role',
        max_length=25,
        choices=UserRole.choices,
        default=UserRole.USER,
    )

    class Meta:
        verbose_name = 'User'
        verbose_name_plural = 'Users'

    @property
    def is_admin(self):
        return self.role == self.UserRole.ADMIN

    @property
    def is_moderator(self):
        return self.role == self.UserRole.MODERATOR


from gui.meus_grids import MeuGridColTitles, MeuFormMutt
from entidades import Lancamento, Conta
from repositorios import ContaRepositorio
from funcoes import saldo
from auxiliares import formata_numero, formata, vai_para_inicio_da_lista
import curses, decimal

class SaldoContaList(MeuGridColTitles):
    def __init__(self, *args, **keywords):
        super(SaldoContaList, self).__init__(*args, **keywords)

    def registra_menus(self):
        super(SaldoContaList, self).registra_menus()
        self.adiciona_comando('resultado', self.filtrar_contas_resultado, self.parent.GRUPO == 'PATRIMONIO')
        self.adiciona_comando('patrimonio', self.filtrar_contas_patrimonio, self.parent.GRUPO == 'RESULTADO')
        self.adiciona_comando('todas', self.todas_contas, self.parent.GRUPO is not None)
        self.adiciona_comando('conciliados', self.filtrar_lancamentos_conciliados)
        self.adiciona_comando('listar', self.lancamentos_da_conta)

        self.adiciona_menu(curses.KEY_LEFT, self.ir_contas, 'Contas', principal=True)
        self.adiciona_menu(curses.KEY_RIGHT, self.ir_lancamentos, 'Lançamentos', principal=True)

    def lancamentos_da_conta(self, *args, **keywords):
        self.parent.parentApp.getForm('LANCAMENTOS').conta_filtro = self.get_selecionado()
        self.parent.parentApp.getForm('LANCAMENTOS').atualiza_lista()
        self.parent.parentApp.switchForm('LANCAMENTOS')
        vai_para_inicio_da_lista()

    def todas_contas(self, *args, **keywords):
        self.parent.set_grupo(None)
        self.parent.atualiza_lista()

    def filtrar_contas_resultado(self, *args, **keywords):
        self.parent.set_grupo('RESULTADO')
        self.parent.atualiza_lista()

    def filtrar_contas_patrimonio(self, *args, **keywords):
        self.parent.set_grupo('PATRIMONIO')
        self.parent.atualiza_lista()

    def filtrar_lancamentos_conciliados(self, *args, **keywords):
        if self.parent.SOMENTE_CONCILIADOS:
            self.parent.set_somente_conciliados(False)
        else:
            self.parent.set_somente_conciliados(True)
        self.parent.atualiza_lista()

class SaldoContaListDisplay(MeuFormMutt):
    MAIN_WIDGET_CLASS = SaldoContaList
    GRUPO = 'PATRIMONIO'
    SOMENTE_CONCILIADOS = True

    def __init__(self, *args, **keywords):
        super(SaldoContaListDisplay, self).__init__(*args, **keywords)
        self.wStatus1.value = "Contas e seus Saldos"

    def beforeEditing(self):
        self.atualiza_lista()

    def set_somente_conciliados(self, arg):
        self.SOMENTE_CONCILIADOS = arg

    def set_grupo(self, grupo):
        self.GRUPO = grupo
        self.new_menu()

    def atualiza_legenda_filtro(self):
        self.wStatus2.value = 'Filtros ativos:  '
        self.wStatus2.value += '☑ Conciliados' if self.SOMENTE_CONCILIADOS else '☐ Conciliados'
        self.wStatus2.value += '  🞊 Resultado' if self.GRUPO == 'RESULTADO' else '  🞅 Resultado'
        self.wStatus2.value += '  🞊 Patrimônio' if self.GRUPO == 'PATRIMONIO' else '  🞅 Patrimônio'

    def get_tupla_conciliados(self):
        if self.SOMENTE_CONCILIADOS:
            return (1,)
        return (0, 1,)

    def atualiza_lista(self):
        self.atualiza_legenda_filtro()
        # filtrar os lançamento por grupo de tipo de conta
        self.wMain.col_titles = ['Conta', 'Débito', 'Crédito', 'Saldo']

        try:
            contas = ContaRepositorio().listar_com_saldo(self.get_tupla_conciliados(), self.GRUPO)

            self.wMain.values = []
            for conta in contas:
                saldo_conta = decimal.Decimal(saldo(conta))
                debitos = decimal.Decimal(conta.debitos)
                creditos = decimal.Decimal(conta.creditos)
                nome = "({0}) {1}".format(conta.tipo, conta.nome)
                self.wMain.values.append([formata(nome), formata_numero(debitos), formata_numero(creditos),
                                          formata_numero(saldo_conta), conta])

            self.wMain.display()
            self.display()
        except (Conta.DoesNotExist, Lancamento.DoesNotExist):
            pass


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time :2020/9/9 17:14
# @Author :春衫
# @File :ship.py

from tools.http_request import HttpRequest

def ship(surroundings, orderId, payType, buyer_userId, cookies):
    # 发货
    ship_url = f'http://m.{surroundings}.hobay.com.cn/orders/sendProduct'

    ship_data = {'orderId': orderId, 'payType': payType, 'buyerUserId': buyer_userId, 'type': 1,
                 'logisticsCompany': '德邦物流', 'companyNum': 'debangwuliu', 'logisticsNum': '123456789'}
    ship_headers = {'login': ''}
    ship_res = HttpRequest().http_request(ship_url, 'post', data=ship_data, headers=ship_headers,
                                          cookies=cookies)
    return ship_res


#!/usr/bin/env python
# encoding: utf-8

"""
@version: 0.1
@author: Yang Reid
@license: Apache Licence
@contact: yangtao584@126.com
@site: http://www.phpgao.com
@software: PyCharm Community Edition
@file: new_stock.py
@time: 2018/4/12 18:56
"""
import tushare as ts
import time
import pandas as pd

def func():
    new_stock_df = ts.new_stocks()
    return new_stock_df[['code','name','ipo_date','issue_date']]

if __name__ == '__main__':
    stock_df = func()
    stock_df = stock_df[stock_df['issue_date'] > '2017-12-01']
    all_stocks = stock_df['code'].tolist()
    po_ban_stocks = []
    for stock in all_stocks:
        df = ts.get_hist_data(stock)
        try:
            df = df[['open', 'close', 'p_change']]
            start_date = df[df['p_change'] < 9.8].tail(1).index[0]
            po_ban_price = df.loc[start_date,'close']
            stock_dict = {'code':stock,
                          'date':start_date,
                        'break_price':po_ban_price}
            po_ban_stocks.append(stock_dict)
            # print('Stock:{} opened on Date:{} at {}'.format(stock,start_date,po_ban_price))
        except (IndexError,TypeError):
            pass
    pd.DataFrame(po_ban_stocks).to_excel('po_ban' + time.strftime("_%Y-%m-%d_%H_%M_%S", time.localtime()) + '.xlsx',
                                           index=False)


n = input()
n_list = []
help = [4 , 7]

for i in n:
    n_list.append(int(i))

count_4 = n_list.count(4)
count_7 = n_list.count(7)

if (count_7 + count_4) in help:
    print('YES')
else:
    print('NO')


import random

from environs import Env
import pytimeparse

import ptbot

def format_progress_message(seconds_total, seconds_left):
    return render_progressbar(
        total = seconds_total,
        iteration = seconds_total - seconds_left,
        prefix = f"Осталось {seconds_left} секунд\n"
    )

def render_progressbar(total, iteration, prefix='', suffix='', length=30, fill='█', zfill='░'):
    iteration = min(total, iteration)
    percent = "{0:.1f}"
    percent = percent.format(100 * (iteration / float(total)))
    filled_length = int(length * iteration // total)
    pbar = fill * filled_length + zfill * (length - filled_length)
    return '{0} |{1}| \n{2}% {3}'.format(prefix, pbar, percent, suffix)

def notify_progress(seconds_left, seconds_total, message_id):
    bot.update_message(
        tg_chat_id, message_id, format_progress_message(seconds_total, seconds_left)
    )

def notify_time_is_over():
    bot.send_message(tg_chat_id, "Время вышло!")

def reply(time):
    parsed_time = pytimeparse.parse(time)
    if parsed_time is None:
        error_message = 'Я так не понимаю.\nНапишите что-то вроде "5s" или "1.2 minutes"'
        bot.send_message(tg_chat_id, error_message)
        return
    message_id = bot.send_message(
        tg_chat_id, format_progress_message(parsed_time, parsed_time)
    )
    bot.create_countdown(parsed_time, notify_progress, parsed_time, message_id)
    bot.create_timer(parsed_time, notify_time_is_over)

if __name__ == "__main__":
    env = Env()
    env.read_env()

    tg_token = env.str("tg_token")
    tg_chat_id = env.int("tg_chat_id")

    bot = ptbot.Bot(tg_token)
    bot.send_message(tg_chat_id, "На какое время поставить таймер?")
    bot.reply_on_message(reply)
    bot.run_bot()


"""
Knowledge graph dataset for Relational-GCN
"""

from __future__ import print_function
from __future__ import absolute_import
import numpy as np
import scipy.sparse as sp
import os
import pandas as pd

np.random.seed(123)

class RGCNUnaryDataset(object):
    """RGCN Unary Classification dataset

    Each instance of unary template is regarded as a node
    its label is the template it satisfying

     An object of this class has 11 member attributes needed for unary
    classification:

    num_nodes: int
        number of entities of knowledge base
    num_rels: int
        number of relations (including reverse relation) of knowledge base
    num_classes: int
        number of classes/labels that of entities in knowledge base
    edge_src: numpy.array
        source node ids of all edges
    edge_dst: numpy.array
        destination node ids of all edges
    edge_type: numpy.array
        type of all edges
    edge_norm: numpy.array
        normalization factor of all edges
    labels: numpy.array
        labels of node entities
    train_idx: numpy.array
        ids of entities used for training
    valid_idx: numpy.array
        ids of entities used for validation
    test_idx: numpy.array
        ids of entities used for testing
    features: numpy.array
        input features of nodes

    When loading data, besides specifying dataset name, user can provide two
    optional arguments:

    Parameters
    ----------
    bfs_level: int
        prune out nodes that are more than ``bfs_level`` hops away from
        labeled nodes, i.e., nodes won't be touched during propagation. If set
        to a number less or equal to 0, all nodes will be retained.
    relabel: bool
        After pruning, whether or not to relabel all nodes with consecutive
        node ids
    """

    def __init__(self, name, type):
        self.name = name
        self.type = type
        self.dir = os.path.join(os.path.abspath('.'), 'dataset')
        self.dir = os.path.join(self.dir, self.type)
        self.dir = os.path.join(self.dir, self.name)

    def load(self, bfs_level=2, relabel=False):
        self.num_nodes, edges, self.num_rels, self.labels, labeled_nodes_idx, self.features = _load_data(self.name, self.dir)

        # bfs to reduce edges
        if bfs_level > 0:
            print("removing nodes that are more than {} hops away".format(bfs_level))
            row, col, edge_type = edges.transpose()
            A = sp.csr_matrix((np.ones(len(row)), (row, col)), shape=(self.num_nodes, self.num_nodes))
            bfs_generator = _bfs_relational(A, labeled_nodes_idx)
            lvls = list()
            lvls.append(set(labeled_nodes_idx))
            for _ in range(bfs_level):
                lvls.append(next(bfs_generator))
            to_delete = list(set(range(self.num_nodes)) - set.union(*lvls))
            eid_to_delete = np.isin(row, to_delete) + np.isin(col, to_delete)
            eid_to_keep = np.logical_not(eid_to_delete)
            self.edge_src = row[eid_to_keep]
            self.edge_dst = col[eid_to_keep]
            self.edge_type = edge_type[eid_to_keep]

            if relabel:  # False, so not run
                uniq_nodes, edges = np.unique((self.edge_src, self.edge_dst), return_inverse=True)
                self.edge_src, self.edge_dst = np.reshape(edges, (2, -1))
                node_map = np.zeros(self.num_nodes, dtype=int)
                self.num_nodes = len(uniq_nodes)
                node_map[uniq_nodes] = np.arange(self.num_nodes)
                self.labels = self.labels[uniq_nodes]
                self.train_idx = node_map[self.train_idx]
                self.test_idx = node_map[self.test_idx]
                print("{} nodes left".format(self.num_nodes))
        else:
            self.edge_src, self.edge_dst, self.edge_type = edges.transpose()

        # normalize by src degree, compute degrees according to edge_type
        _, inverse_index, count = np.unique((self.edge_dst, self.edge_type), axis=1, return_inverse=True, return_counts=True)
        degrees = count[inverse_index]  #c_{i,r} for each relation type
        self.edge_norm = np.ones(len(self.edge_dst), dtype=np.float32) / degrees.astype(np.float32)

        # convert to pytorch label format
        self.num_classes = self.labels.shape[1]
        #self.labels = np.argmax(self.labels, axis=1)

class RGCNBinaryDataset(object):
    """RGCN link prediction dataset

    Each binary template is regard as a binary relation between two entities (nodes)

    An object of this class has 5 member attributes needed for link
    prediction:

    num_nodes: int
        number of entities of rule base
    num_rels: int
        number of relations (including reverse relation) of rule base
    data: numpy.array
        all relation triplets (src, rel, dst)
    relation_features: dateframe

    Usually, user don't need to directly use this class. Instead, DGL provides
    wrapper function to load data (see example below).

    """
    def __init__(self, name):
        self.name = name
        self.dir = os.path.join(os.path.abspath('.'), 'binary_data')
        self.dir = os.path.join(self.dir, self.name)

    def load(self):
        entity_path = os.path.join(self.dir, 'nodes.dict')
        relation_path = os.path.join(self.dir, 'edges.dict')
        data_path = os.path.join(self.dir, 'binary_template_triples.txt')  # all triples
        node_feature_path = os.path.join(self.dir, 'node_features.csv')
        relation_feature_path = os.path.join(self.dir, 'relation_features.csv')
        self.node_features = pd.read_csv(node_feature_path, sep=',', encoding='utf-8')
        self.relation_features = pd.read_csv(relation_feature_path, sep=',', encoding='utf-8')
        entity_dict = _read_dictionary(entity_path)
        relation_dict = _read_dictionary(relation_path)
        self.complete_data = np.array(_read_triplets_as_list(data_path, entity_dict, relation_dict))  # (n_id, r_id, n_id)
        self.num_nodes = len(entity_dict)
        print("# entities: {}".format(self.num_nodes))
        self.num_rels = len(relation_dict)
        print("# relations: {}".format(self.num_rels))
        print("# edges: {}".format(len(self.complete_data)))   # total number of edges

def load_unary(dataset, feature_type, bfs_level, relabel):
    data = RGCNUnaryDataset(dataset, feature_type)
    data.load(bfs_level, relabel)
    return data

def load_binary(dataset):
    data = RGCNBinaryDataset(dataset)
    data.load()
    return data

def _sp_row_vec_from_idx_list(idx_list, dim):
    """Create sparse vector of dimensionality dim from a list of indices."""
    shape = (1, dim)
    data = np.ones(len(idx_list))
    row_ind = np.zeros(len(idx_list))
    col_ind = list(idx_list)
    return sp.csr_matrix((data, (row_ind, col_ind)), shape=shape)

def _get_neighbors(adj, nodes):
    """Takes a set of nodes and a graph adjacency matrix and returns a set of neighbors."""
    sp_nodes = _sp_row_vec_from_idx_list(list(nodes), adj.shape[1])
    sp_neighbors = sp_nodes.dot(adj)
    neighbors = set(sp.find(sp_neighbors)[1])  # convert to set of indices
    return neighbors

def _bfs_relational(adj, roots):
    """
    BFS for graphs with multiple edge types. Returns list of level sets.
    Each entry in list corresponds to relation specified by adj_list.
    """
    visited = set()
    current_lvl = set(roots)

    next_lvl = set()

    while current_lvl:

        for v in current_lvl:
            visited.add(v)

        next_lvl = _get_neighbors(adj, current_lvl)
        next_lvl -= visited  # set difference

        yield next_lvl

        current_lvl = set.union(next_lvl)

def _load_sparse_csr(filename):
    loader = np.load(filename)
    return sp.csr_matrix((loader['data'], loader['indices'], loader['indptr']),
                         shape=loader['shape'], dtype=np.float32)

def _save_sparse_csr(filename, array):
    np.savez(filename, data=array.data, indices=array.indices,
             indptr=array.indptr, shape=array.shape)

def _load_data(dataset_str='wine', dataset_path=None):
    """

    :param dataset_str:
    :param rel_layers:
    :param limit: If > 0, will only load this many adj. matrices
        All adjacencies are preloaded and saved to disk,
        but only a limited a then restored to memory.
    :return:
    """

    print('Loading dataset', dataset_str)
    fea_file = 'node_features.csv'
    feature_file = os.path.join(dataset_path, fea_file)  # node_features: word embedding + analogy space
    edge_file = os.path.join(dataset_path, 'edges_subset.npz')    # {edges: (s, t, r) / n: num_nodes / nrel : num_rels}
    labels_file = os.path.join(dataset_path, 'labels_subset.npz')  # n * l sparse matrix

    # load node features
    node_feature = pd.read_csv(feature_file, sep=',', encoding='utf-8')

    # load precomputed adjacency matrix and labels
    all_edges = np.load(edge_file)
    num_node = all_edges['n'].item()
    edge_list = all_edges['edges']
    num_rel = all_edges['nrel'].item()

    print('Number of nodes: ', num_node)
    print('Number of edges: ', len(edge_list))
    print('Number of relations: ', num_rel)

    labels = _load_sparse_csr(labels_file)
    labeled_nodes_idx = list(labels.nonzero()[0])

    print('Number of classes: ', labels.shape[1])

    return num_node, edge_list, num_rel, labels, labeled_nodes_idx, node_feature

# key = template name, value = id
def _read_dictionary(filename):
    d = {}
    with open(filename, 'r+') as f:
        for line in f:
            line = line.strip().split('\t')
            d[line[1]] = int(line[0])
    return d

# key = id, value = template name
def read_dictionary(filename):
    d = {}
    with open(filename, 'r+') as f:
        for line in f:
            line = line.strip().split('\t')
            d[int(line[0])] = line[1]
    return d

def _read_triplets(filename):
    with open(filename, 'r+') as f:
        for line in f:
            processed_line = line.strip().split('\t')
            yield processed_line

def _read_triplets_as_list(filename, entity_dict, relation_dict):
    l = []
    for triplet in _read_triplets(filename):
        s = entity_dict[triplet[0]]
        r = relation_dict[triplet[1]]
        o = entity_dict[triplet[2]]
        l.append([s, r, o])
    return l


from osv import osv, fields, orm

class gimmel_account_invoice(osv.osv):

    _inherit = "account.invoice.line"

    def _get_internal_number(self, cr, uid, ids, name, args, context=None):

        if context is None:

            context = {}

        name = {}

        for id in ids:

            name[id] = ''

        for ail in self.pool.get('account.invoice.line').browse(cr,uid,ids,context=context):

            ai = self.pool.get('account.invoice').browse(cr,uid,ail.invoice_id.id,context=context)

            if ai.internal_number:

                name[ail.id] = ai.internal_number

        return name

    _columns = {

                "internal_number": fields.function(

                                  _get_internal_number,

                                  string='Fatura',type='char')

                }

gimmel_account_invoice()


import re
import subprocess
import os
import sys

if os.geteuid() > 0:
	print("You must be root !")
	sys.exit(1)

device_re = re.compile("Bus\s+(?P<bus>\d+)\s+Device\s+(?P<device>\d+).+ID\s(?P<id>\w+:\w+)\s(?P<tag>.+)$", re.I)
df = subprocess.check_output("lsusb")
devices = []
for i in df.split('\n'):
    if i:
        info = device_re.match(i)
        if info:
            dinfo = info.groupdict()
            dinfo['device'] = '/dev/bus/usb/%s/%s' % (dinfo.pop('bus'), dinfo.pop('device'))
            devices.append(dinfo)

count = 0
for i in devices:
	if i['id']=="04e8:61b3":
		count = 1

if count == 0:
	os.system("sudo reboot")


from algorithms.mDSDI.src.models.mnistnet import MNIST_CNN, Color_MNIST_CNN
from algorithms.mDSDI.src.models.resnet import ResNet

nets_map = {
    'mnistnet': MNIST_CNN,
    'cmnistnet': Color_MNIST_CNN,
    'resnet50': ResNet
}

def get_model(name):
    if name not in nets_map:
        raise ValueError('Name of model unknown %s' % name)

    def get_model_fn(**kwargs):
        return nets_map[name](**kwargs)

    return get_model_fn


from django.conf import settings
from django.db import utils
from django.utils import timezone
from keystone_wrapper.client import KeystoneClient
from project_billing.models import RawStatistics
from project_billing.models import RawStatisticsIndex
from project_billing.ceilometer_fetcher import CeilometerStats

class StatisticsIndexBuilder(object):

    def __init__(self):
        self.ks_client = False
        self.project_ids = False

    def _get_time_range(self):
        from_ts = self.date or timezone.now() - timezone.timedelta(days=1)
        return {
            'from_ts': from_ts.replace(hour=0,
                                       minute=0,
                                       second=0,
                                       microsecond=0),
            'until_ts': (from_ts +
                         timezone.timedelta(days=1)).replace(
                             hour=0,
                             minute=0,
                             second=0,
                             microsecond=0)}

    def _list_ks_project_ids(self):
        if not self.project_ids:
            self.project_ids = [
                x.id for x in KeystoneClient.get_client().tenants.list()]
        return self.project_ids

    def _merge_indexing_data(self):
        # define data collectors in dc
        dc = {'meters': settings.BILLABLE_RESOURCE_TYPES.keys(),
              'project_ids': self._list_ks_project_ids(),
              'time_range': self._get_time_range()}

        # create every possible combination of projectid, meter and date
        return [dict({'project_id': project_id, 'meter': meter}.items() +
                     dc['time_range'].items())
                for project_id in dc['project_ids']
                for meter in dc['meters']]

    @staticmethod
    def _save_index(index_data):
        for index_element in index_data:
            try:
                RawStatisticsIndex.objects.create(**index_element)
            except utils.IntegrityError:
                # in case the previous run of this job has been aborted,
                # just continue where we stopped
                pass

    def build(self, date=None):
        self.date = date
        self._save_index(self._merge_indexing_data())

class UnfetchedStatisticsFetcher(object):

    @classmethod
    def _fetch_from_cm(cls, index):
        return CeilometerStats.get_stats(
            meter=index.meter,
            project_id=index.project_id,
            from_ts=index.from_ts,
            until_ts=index.until_ts)

    @classmethod
    def fetch(cls):
        for index in RawStatisticsIndex.objects.filter(fetched=False):
            data = cls._fetch_from_cm(index)
            if data.has_data:
                RawStatistics.objects.create(statistics_index=index,
                                             data=data.pickle())
                index.has_data = True
            index.fetched = True
            index.save()


# coding=utf-8
"""
Восстановление даты после перезагрузки.
"""
import os
import datetime
import time
from pyutils.stopablethread import StopableThread, Singleton
import logging

log = logging.getLogger("date_recovery")

class DateRecovery(StopableThread):
    __metaclass__ = Singleton

    # Период сохранения текущего времени
    UPDATE_PERIOD = 600
    DELAY_PERIOD = 10
    PATH_RECOVER_FILE = "./.date_recovery"

    DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'

    def __init__(self):
        super(DateRecovery, self).__init__()
        self._callbackRecovery = None
        self.start(delay=0)

    def _recover(self):
        """Вызываем каллбек восстановления"""
        if self._callbackRecovery:
            self._callbackRecovery()

    def worker(self, *args, **kwargs):
        """Перезаписываем последний файл"""
        current_date = datetime.datetime.now()
        if os.path.isfile(DateRecovery.PATH_RECOVER_FILE):
            with open(DateRecovery.PATH_RECOVER_FILE, "rb") as f:
                date = f.read()
            old_date = datetime.datetime.strptime(date, self.DATETIME_FORMAT)
            delta = current_date - old_date
            delta_seconds = delta.seconds
            # проверка на ошибочность
            if delta_seconds > self.UPDATE_PERIOD + self.DELAY_PERIOD:
                lag = delta_seconds - (self.UPDATE_PERIOD + self.DELAY_PERIOD)
                log.warning("Delta period is not correctly. Lag is %s second. Run recovery callback." % lag)
                self._recover()
        # добавим новую запись
        with open(DateRecovery.PATH_RECOVER_FILE, "wb") as f:
            t = current_date
            f.write(t.strftime(self.DATETIME_FORMAT))
        return self.UPDATE_PERIOD

    def setRecoveryCallback(self, callback):
        """Вызывать функцию в случае непредвиденного обстоятельства"""
        self._callbackRecovery = callback

if __name__ == "__main__":
    DateRecovery()
    while True:
        time.sleep(1)


"""
Implementação básica do algoritmo de Dijkstra
"""

# calcula o caminho mais curto entre o ponto de origem e o ponto de destino
def menor_caminho(grafo, origem, destino, historico=[], distancias={}, pais={}):

    # se o destino já foi alcançado
    if origem == destino:
        caminho = []
        pai = destino
        while pai is not None:
            caminho.append(pai)
            pai = pais.get(pai, None)
        print('menor caminho: ' + str(caminho[::-1]) + ' custo: ' + str(distancias[destino]))

    # se o destino ainda não foi alcançado
    else:

        # se esta é a primeira execução, inicializar o custo em 0
        if not historico:
            distancias[origem] = 0

        # percorrendo os vizinhos
        for vizinho in grafo[origem]:
            if vizinho not in historico:
                nova_distancia = distancias[origem] + grafo[origem][vizinho]
                if nova_distancia < distancias.get(vizinho, float('inf')):
                    distancias[vizinho] = nova_distancia
                    pais[vizinho] = origem

        # registrando os vizinhos percorridos no histórico
        historico.append(origem)

        # selecionar o nó "x" com a menor distância ainda não visitado
        # executar função com origem = "x"
        nao_visitado = {}
        for k in grafo:
            if k not in historico:
                nao_visitado[k] = distancias.get(k, float('inf'))

        # "x" recebe o nó que possuir o menor custo
        x = min(nao_visitado, key=nao_visitado.get)
        menor_caminho(grafo, x, destino, historico, distancias, pais)

# criando o grafo
if __name__ == "__main__":
    grafo = {'a': {'b': 10, 'c': 30},
         'b': {'c': 1, 'd': 2},
         'c': {'b': 4, 'd': 8, 'e': 2},
         'd': {'e': 7},
         'e': {'d': 9}}
    menor_caminho(grafo, 'a', 'e')


from flask_classy import FlaskView, route
from flask import render_template, redirect, session, request, flash
from utils.twitter_oauth import TwitterOauth
from flask_login import login_user
from models import User

class OauthView(FlaskView):
    route_base = '/oauth/'

    # /oauth/twitter
    def twitter(self):
        twitter = TwitterOauth()
        token, secret, url = twitter.request_parmas()

        # callback先で使うのでsessionに保存しておく
        session['request_token'] = token
        session['request_token_secret'] = secret

        return redirect(url)

    # /oauth/twitter/callback
    @route('/twitter/callback')
    def twitter_callback(self):
        twitter = TwitterOauth()
        user_infomation = twitter.fetch_access_token(
            request.args.get('oauth_token'),
            session.get('request_token_secret'),
            request.args.get('oauth_verifier')
        )

        # セッションから削除
        session.pop('request_token_secret', None)
        session.pop('request_token', None)

        user = User.query.filter_by(twitter_user_id=user_infomation['twitter_user_id']).first()

        if user:
            # 既存ユーザー
            flash("ログインしました")
        else:
            # 新規ユーザーを登録
            user = User(
                screen_name=user_infomation['screen_name'],
                twitter_user_id=user_infomation['twitter_user_id'],
                access_token=user_infomation['access_token'],
                access_token_secret=user_infomation['access_token_secret']
            )
            user.add().commit()
            flash("登録しました")

        # login情報の保存
        login_user(user, True)

        return redirect('/')


# Copyright (c) 2014 Josh Blum
# SPDX-License-Identifier: BSL-1.0

import Pothos

class Forwarder(Pothos.Block):
    def __init__(self, dtype):
        Pothos.Block.__init__(self)
        self.setupInput("0", dtype)
        self.setupOutput("0", dtype)

    def activate(self):
        print('activate called')

    def deactivate(self):
        print('deactivate called')

    def work(self):

        if self.workInfo().minElements:
            print('minElements %s'%self.workInfo().minElements)
            print('minInElements %s'%self.workInfo().minInElements)
            print('minOutElements %s'%self.workInfo().minOutElements)

        #forward message
        if self.input(0).hasMessage():
            m = self.input(0).popMessage()
            print('msg %s'%m)
            self.output(0).postMessage(m)

        #forward labels
        while True:
            loop = False
            for l in self.input(0).labels():
                print(l.index)
                print(l.data)
                self.output(0).postLabel(l)
                self.input(0).removeLabel(l)
                loop = True
                break
            if not loop: break

        #forward buffer
        if self.input(0).elements():
            print(self.input(0).dtype())
            print(self.input(0).buffer())

            out0 = self.output(0).buffer()
            in0 = self.input(0).buffer()
            n = min(len(out0), len(in0))
            out0[:n] = in0[:n]
            self.input(0).consume(n)
            self.output(0).produce(n)

    def propagateLabels(self, input):
        print('propagateLabels')
        print(self.input(0).totalElements())
        for l in input.labels():
            print(l.index)
            print(l.data)
            #self.output(0).postLabel(l)

class SimpleSignalEmitter(Pothos.Block):
    def __init__(self):
        Pothos.Block.__init__(self)
        self.registerSignal("activateCalled")

    def activate(self):
        self.emitSignal("activateCalled", "hello")

class SimpleSlotHandler(Pothos.Block):
    def __init__(self):
        Pothos.Block.__init__(self)
        self.registerSlot("activateHandler")

    def activateHandler(self, word):
        self._lastWord = word

    def getLastWord(self):
        return self._lastWord


import os
import re
import numpy as np
from scipy.signal import argrelmin, argrelmax
from FT import DFT, IDFT

class Folder:
    def __init__(self, path):
        self.path = path
        self.filesData = dict()
        self.filesDFT = dict()
        self.filesIDFT = dict()
        self.filesExtremumIntervals = dict()

    def getCountFilesForMask(self, mask=".*.txt"):
        count = 0
        for filename in os.listdir(self.path):
            if re.match(mask, filename):
                count+=1
        return count

    def read(self, mask= ".*.txt"):
        for filename in os.listdir(self.path):
            if re.match(mask, filename):
                with open(os.path.join(self.path, filename), 'r') as f:
                    self.filesData[filename] = []
                    for line in f:
                        self.filesData[filename].append(float(line.replace(",", ".")))

    def getDFT(self):
        for filename in self.filesData:
            ft = DFT(self.filesData[filename])
            self.filesDFT[filename] = ft
            f = open(os.path.join(self.path, "DFT-" + filename), "a")
            for x in ft:
                f.write(str(x.real) + " " + str(x.imag) + "\n")
            f.close()

    def getIntervalIDFT(self, start = 0, end = 0):
        for filename in self.filesDFT:
            intervalDFT = np.copy(self.filesDFT[filename])
            suffix = ""
            if start != 0 or end != 0:
                suffix = str(start) + "-" + str(end) + "-" + filename
                intervalDFT[:start] = 0+0j
                intervalDFT[end:] = 0+0j
            else:
                suffix = "-full-" + filename

            ift = IDFT(intervalDFT)
            self.filesIDFT[filename] = ift
            np.savetxt(os.path.join(self.path, "IDFT-" + suffix),
                       np.real(ift), delimiter='\n')

            minima = argrelmin(self.filesIDFT[filename])
            maxima = argrelmax(self.filesIDFT[filename])
            np.savetxt(os.path.join(self.path, "MIN-" + suffix),
                       np.diff(minima), delimiter='\n', fmt='%i')
            np.savetxt(os.path.join(self.path, "MAX-" + suffix),
                       np.diff(maxima), delimiter='\n', fmt='%i')


import tensorflow as tf

class NALU(object):
	def __init__(self, output_len,initializer=tf.contrib.layers.xavier_initializer()):
		assert output_len > 0
		self.output_len = output_len
		self.initializer = initializer
		self._gate_size = output_len

	def __call__(self,input,scope="NALU"):
		with tf.variable_scope(scope,reuse=tf.AUTO_REUSE):
			feature_size = input.get_shape().as_list()[-1]
			self._W = tf.get_variable("W_hat",shape=[feature_size,self.output_len],dtype=tf.float32,
										initializer=self.initializer,trainable=True)
			self._M = tf.get_variable("M_hat",shape=[feature_size,self.output_len],dtype=tf.float32,
										initializer=self.initializer,trainable=True)
			self._G = tf.get_variable("G_hat",shape=[feature_size,self._gate_size],dtype=tf.float32,
										initializer=self.initializer,trainable=True)

			self._const_W = tf.nn.tanh(self._W) * tf.nn.sigmoid(self._M)

			a = tf.matmul(input,self._const_W)
			m = tf.exp(tf.matmul(tf.log(tf.abs(input) + 0.00001),self._const_W))
			g = tf.nn.sigmoid(tf.matmul(input,self._G))

			return g * a + (1-g) * m

	def to_json(self,sess):
		curr_layer = {}
		Wval, Gval = sess.run([self._const_W,self._G])
		curr_layer['W'] = Wval.tolist()
		curr_layer['G'] = Gval.tolist()
		curr_layer['in_dim'] = Wval.shape[0]
		curr_layer['out_dim'] = Wval.shape[1]
		curr_layer['type'] = "NALU"
		return curr_layer


from src.utilities import PrepUtility
from src.rules import rules, slot_list, extractSlot, confirmations
from src.slotDetection import find_connection, find_dates, find_class_type, find_name, find_trip, get_place
from datetime import datetime
import random
import os
import pathlib
from src.utilities import read_json, write_json

root = pathlib.Path(os.path.abspath(__file__)).parent.parent
test_tag_path = str(root) + '/model_tmp/test_results/tagging.test.hyp.txt'

slot_checked = []

def get_response(message):
    user_in = PrepUtility.create_test_seq_in(message)
    tag_ls, word_ls = extract_information(test_tag_path)
    customer_name = find_name(message)
    place_from, place_to, isNewPlace = get_place(tag_ls, word_ls)
    date_departure, date_return, isNewDate = find_dates(message)
    is_round_trip, isNewTrip = find_trip(tag_ls, word_ls)
    if date_departure and date_return:
        is_round_trip = True
    connection_limit, isNewConnection = find_connection(tag_ls, word_ls)
    class_type, isNewClass = find_class_type(tag_ls, word_ls)
    informationDict = dict(date_departure=date_departure, date_return=date_return,
                    place_from=place_from, place_to=place_to, is_round_trip=is_round_trip,
                    customer_name=customer_name, connection_limit=connection_limit, class_type=class_type,
                           isNewPlace = isNewPlace, isNewDate = isNewDate,
                           isNewConnection = isNewConnection, isNewClass = isNewClass)
    checkSlot(place_from, place_to, date_departure, date_return, is_round_trip, connection_limit, customer_name, class_type)

    #print("[INFO] user in = ", user_in)
    print('[INFO] slot_checked before: ', slot_checked)
    slot = extractSlot(user_in)
    if (slot != None) and (slot[0] not in slot_checked):
        slotIndex = slot_list.index(slot[0])
        slot_checked.extend(slot)
        message_response = random.choice(rules[slotIndex]['questions'])
        response_list = prepareResponse(slot[0], message_response, informationDict)
    else:
        slot_left = [s for s in slot_list if s not in slot_checked]
        if slot_left:
            slot_to_check = slot_left[0]
            slot_checked.append(slot_to_check)
        else:
            slot_to_check = 'defaulterror'
        slotIndex = slot_list.index(slot_to_check)
        if slot_to_check == 'searchResult':
            if is_round_trip == True and connection_limit == 'zero':
                message_response = rules[slotIndex]['questions']['roundwaywithout']
            if is_round_trip == True and connection_limit != 'zero':
                message_response = rules[slotIndex]['questions']['roundwaywithconnection']
            if is_round_trip == False and connection_limit == 'zero':
                message_response = rules[slotIndex]['questions']['onewaywithout']
            if is_round_trip == False and connection_limit != 'zero':
                message_response = rules[slotIndex]['questions']['onewaywithconnection']
        else:
            message_response = random.choice(rules[slotIndex]['questions'])
        response_list = prepareResponse(slot_to_check, message_response, informationDict)

    is_new_from_place = True if isNewPlace and place_from else False
    is_new_to_place = True if isNewPlace and place_to else False
    is_new_from_date = True if isNewDate and date_departure else False
    is_new_return_date = True if isNewDate and date_return else False
    is_new_round_trip = None
    if isNewTrip and is_round_trip:
        is_new_round_trip = True
    if isNewTrip and not is_round_trip:
        is_new_round_trip = False

    print('[INFO] slot_checked after: ', slot_checked)
    response = dict(messages=response_list, date_departure=date_departure, date_return=date_return,
                    place_from=place_from, place_to=place_to, is_round_trip=is_round_trip,
                    customer_name=customer_name, class_type=class_type, connection_limit=connection_limit,
                    is_new_from_place = str(is_new_from_place), is_new_to_place = str(is_new_to_place),
                    is_new_from_date = str(is_new_from_date), is_new_return_date = str(is_new_return_date),
                    is_new_round_trip = str(is_new_round_trip),
                    is_new_connection = str(isNewConnection), is_new_class = str(isNewClass))
    return response

def extract_information(test_tag_path):
    f = open(test_tag_path, "r")
    lines = f.readlines()
    tag_ls = []
    word_ls = []
    for line in lines:
        if line.strip().split()[0] == 'EOS':
            break
        elif line.strip().split()[0] != 'BOS':
            word_ls.append(line.strip().split()[0])
            tag_ls.append(line.strip().split()[1])
    print('[INFO] extract_information tag_ls: ', tag_ls, 'word_ls: ', word_ls)
    return tag_ls, word_ls

def checkSlot(place_from, place_to, date_departure, date_return, is_round_trip, connection_limit, customer_name, class_type):
    if 'place_from' not in slot_checked and place_from:
        slot_checked.append('place_from')
        if 'place'not in slot_checked:
            slot_checked.append('place')
    if 'place_to' not in slot_checked and place_to:
        slot_checked.append('place_to')
        if 'place'not in slot_checked:
            slot_checked.append('place')

    if 'date_departure' not in slot_checked and date_departure:
        slot_checked.append('date_departure')
        if 'date' not in slot_checked:
            slot_checked.append('date')
    if 'date_return' not in slot_checked and (date_return or is_round_trip==False):
        slot_checked.append('date_return')
        if 'date' not in slot_checked:
            slot_checked.append('date')

    if 'trip_type' not in slot_checked and (is_round_trip is not None):
        slot_checked.append('trip_type')

    if 'connection_limit' not in slot_checked and connection_limit:
        slot_checked.append('connection_limit')

    if 'name' not in slot_checked and customer_name:
        slot_checked.append('name')
        slot_checked.append('greeting')

    if 'class' not in slot_checked and class_type:
        slot_checked.append('class')

def prepareResponse(slot, response, informationDict):
    response_list = []
    customer_name = informationDict.get('customer_name', '')
    place_from = informationDict.get('place_from', None)
    place_to = informationDict.get('place_to', None)
    date_departure = informationDict.get('date_departure', None)
    date_return = informationDict.get('date_return', None)
    is_round_trip = informationDict.get('is_round_trip', None)
    connection_limit = informationDict.get('connection_limit', None)
    class_type = informationDict.get('class_type', None)
    isNewPlace = informationDict.get('isNewPlace')
    isNewDate = informationDict.get('isNewDate')
    #isNewTrip = informationDict.get('isNewTrip')
    isNewConnection = informationDict.get('isNewConnection')
    isNewClass = informationDict.get('isNewClass')
    if slot == 'greeting':
        response = response.format(name = ' ' + customer_name)
    if slot == 'searchResult':
        if is_round_trip == True:
            if connection_limit == 'zero':
                response = response.format(name = customer_name, class_type = class_type, place_from=place_from, place_to=place_to,
                                       date_departure = date_departure, date_return = date_return)
            else:
                response = response.format(name = customer_name, class_type = class_type, place_from=place_from,
                                       place_to=place_to, connection_limit=connection_limit,
                                       date_departure = date_departure, date_return = date_return)
        if is_round_trip == False:
            if connection_limit == 'zero':
                response = response.format(name = customer_name, class_type = class_type, place_from=place_from,
                                       place_to=place_to, date_departure = date_departure)
            else:
                response = response.format(name = customer_name, class_type = class_type, place_from=place_from,
                                       place_to=place_to, connection_limit=connection_limit,
                                       date_departure = date_departure)

    summary_response = []
    checkList = [isNewPlace, isNewDate, isNewClass, isNewConnection]
    if any(checkList):
        summary_response.append(random.choice(confirmations['opening1']))
        summary_response.append(random.choice(confirmations['opening2']))
        if is_round_trip is None:
            summary_response.append(confirmations['ticket'])
            if date_departure and isNewDate:
                summary_response.append(confirmations['one_date'].format(date_departure=date_departure))
        if is_round_trip == True:
            summary_response.append(confirmations['round'])
            if date_departure and date_return and isNewDate:
                summary_response.append(
                    confirmations['round_date'].format(date_departure=date_departure, date_return=date_return))
        elif is_round_trip == False:
            summary_response.append(confirmations['oneway'])
            if date_departure and isNewDate:
                summary_response.append(confirmations['one_date'].format(date_departure=date_departure))

    if place_from and place_to and isNewPlace:
        summary_response.append(confirmations['place'].format(place_from=place_from, place_to=place_to))
    # if is_round_trip == True and date_departure and date_return and isNewDate:
    #     summary_response.append(confirmations['round_date'].format(date_departure=date_departure, date_return=date_return))
    # if is_round_trip == False and date_departure and isNewDate:
    #     summary_response.append(confirmations['one_date'].format(date_departure=date_departure))
    if connection_limit and isNewConnection:
        if connection_limit == 'zero':
            summary_response.append(confirmations['direct'])
        else:
            summary_response.append(confirmations['connection'].format(connection_limit=connection_limit))
    if class_type and isNewClass:
        summary_response.append(confirmations['class'].format(class_type=class_type))
    if len(summary_response)>=1:
        summary_response.append(confirmations['ending'])
        summary = ' '.join(summary_response)
        response_list.append(dict(message=summary, time=format_current_time()))

    response_list.append(dict(message=response, time=format_current_time()))
    return response_list

def format_current_time():
    return datetime.now().strftime("%H:%M:%S")


#!/usr/bin/env python
from pandas_datareader import data
import tensorflow as tf
import numpy as np
import shutil
from tensorflow.examples.tutorials.mnist import input_data
import os
from PIL import Image
import time
import os
from scipy.misc import imread, imresize

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

#Parametros
tiempo_inicio = time.time()
tam_lote = 3
num_clases = 2
epoch_entrenamiento = 49  #numero de epoch

def inic_pesos(forma):
 var = tf.Variable(tf.random_normal(forma, stddev=0.01))
 return var

def modelo(imagen, w1, w4, w_salida, prob_conv, prob_oc):
 conv1 = tf.nn.conv2d(imagen, w1, strides=[1, 1, 1, 1], padding='SAME')
 conv1_a = tf.nn.relu(conv1)
 conv1 = tf.nn.max_pool(conv1_a, ksize=[1, 2, 2, 1] ,strides=[1, 2, 2, 1],  padding='SAME')
 conv1 = tf.nn.dropout(conv1, prob_conv)

 capa_cc = tf.reshape(conv1, [-1, w4.get_shape().as_list()[0]])
 capa_cc = tf.nn.dropout(capa_cc, prob_conv)

 capa_s = tf.nn.relu( tf.matmul(capa_cc, w4) + bias)
 capa_s = tf.nn.dropout(capa_s, prob_oc, name="dropout_2")

 resultado = tf.matmul(capa_s, w_salida, name="MatMul_1") + bias2

 return resultado

# Ruta del dataset
cwd = os.getcwd()
ruta_dataset = cwd + "/data/custom_data2.npz"
l = np.load(ruta_dataset)

# Cargado dataset
trainimg = l['trainimg']
train = l['train_2']
validationimg = l['validationimg']
validation = l['validation_2']
testimg = l['testimg']
test = l['test_2']
imgsize = l['imgsize']
use_gray = l['use_gray']

ntrain = trainimg.shape[0]

#Reshape imagenes
img_entrenamiento = trainimg.reshape(-1, 800, 400, 3) # 28x28x1 input img
etq_entrenamiento = train
img_val = validationimg.reshape(-1, 800, 400, 3) # 28x28x1 input img
etq_val = validation
img_test = testimg.reshape(-1, 800, 400, 3) # 28x28x1 input img
etq_test = test

#Placeholders para entrenamiento/validacion/prueba
imagen = tf.placeholder("float", [None, 800, 400, 3])
etiqueta = tf.placeholder("float32", [None, num_clases])

prob_conv = tf.placeholder("float")
prob_oc = tf.placeholder("float")

#Pesos y bias
w = inic_pesos([7, 7, 3, 12 ])
w4 = inic_pesos([(int)(12*(800/2)*(400/2)), 32]) #(400/2)*(200/2)
w_salida = inic_pesos([32, num_clases])
bias = tf.Variable(tf.random_normal([32]))
bias2 = tf.Variable(tf.random_normal([num_clases]))

#Calculos metricas
modelo = modelo(imagen, w, w4, w_salida, prob_conv, prob_oc)
y = tf.nn.softmax_cross_entropy_with_logits_v2(logits=modelo ,labels=etiqueta)
coste= tf.reduce_mean(y)
opt = tf.train.AdamOptimizer(learning_rate=0.00005).minimize(coste)
probabilidades = tf.nn.softmax(modelo)
predic = tf.argmax(modelo, 1)
correctas = tf.equal(predic, tf.argmax(etiqueta,1)) # Count corrects
precision = tf.reduce_mean(tf.cast(correctas, tf.float32)) # Accuracy

#Metricas a seguir en Tensorboard
tf_coste_ph = tf.placeholder(tf.float32,shape=None,name='coste_summary')
tf_coste_summary = tf.summary.scalar('coste', tf_coste_ph)

tf_precision_ph = tf.placeholder(tf.float32,shape=None, name='precision_summary')
tf_precision_summary = tf.summary.scalar('precision', tf_precision_ph)

summaries = tf.summary.merge([tf_coste_summary,tf_precision_summary])
summaries2 = tf.summary.merge([tf_coste_summary,tf_precision_summary])

# Variables entrenamiento
o_id= 0
id    = tam_lote-1
num_lotes = int(round(ntrain/tam_lote))

# Inicializacion sesion, grafo y writers Tensorboard
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
summ_writer = tf.summary.FileWriter(os.path.join('summaries','first'), sess.graph)
summ_writer2 = tf.summary.FileWriter(os.path.join('summaries','second'), sess.graph)
write_op = tf.summary.merge_all()

# Inicio entrenamiento
for epoch in range(epoch_entrenamiento):
    coste_medio = 0
    precision_media = 0
    print ("Epoch: %03d/%03d" % (epoch+1, epoch_entrenamiento))

    # TRAINING
    for i in range(num_lotes):
     if i != 0:
      o_id=id+1
      id=id+tam_lote
     lote_x = img_entrenamiento[o_id:id, :]
     lote_y = etq_entrenamiento[o_id:id, :]

    # Ejecucion entrenamiento
     sess.run(opt, feed_dict={imagen: lote_x, etiqueta: lote_y, prob_conv: 0.8, prob_oc: 0.5})

    # Calculo coste y precision
     c,e_prec = sess.run([coste,precision], feed_dict={imagen: lote_x, etiqueta: lote_y, prob_conv:1.0, prob_oc: 1.0})
     coste_medio+= c
     precision_media+= e_prec
    precision_media = precision_media/num_lotes
    coste_medio = coste_medio/num_lotes
    print (" Precision entrenamiento: %.3f - Coste entrenamiento: %.3f" % (precision_media,coste_medio))

    if epoch<epoch_entrenamiento:
      o_id= 0
      id    = tam_lote-1
    # VALIDATION
    val_prec,c2 = sess.run([precision,coste], feed_dict={imagen: img_val, etiqueta: etq_val, prob_conv: 1.0, prob_oc: 1.0})
    print (" Precision validacion: %.3f - Coste validacion: %.3f " % (val_prec,c2))

    train = sess.run(summaries, feed_dict={tf_coste_ph:coste_medio, tf_precision_ph: precision_media})
    summ_writer.add_summary(train,epoch)
    summ_writer.flush()

    val = sess.run(summaries2, feed_dict={tf_coste_ph:c2, tf_precision_ph: val_prec})
    summ_writer2.add_summary(val, epoch)
    summ_writer2.flush()

# TESTING
test_prec,c3 = sess.run([precision,coste], feed_dict={imagen: img_test, etiqueta: etq_test, prob_conv: 1.0, prob_oc: 1.0})
print (" Precision pruebas: %.3f - Coste pruebas: %.3f " % (val_prec,c2))

# Guardado modelo
print('\nGuardando...')
ruta = os.path.join(cwd, 'Redes/2')
shutil.rmtree(ruta, ignore_errors=True)
inputs_dict = {
	"imagenes_ph": imagen,
	"etiqueta_ph": etiqueta
	}
outputs_dict = {
    "logits": modelo,
    "probabilidades": probabilidades
    }
tf.saved_model.simple_save(
                sess, ruta, inputs_dict, outputs_dict
            )
print('Ok. Guardado.')
sess.close()
print("--- %s segundos ---" % (time.time() - tiempo_inicio))


from consolemenu import *
from calc.calcFunction import *

def show_expression(expression):
    print("expression: " + expression)
    Screen().input('Press [Enter] to continue')

def get_expression():
    print("Brakuje systemu sprawdzania i poprawy składni, wiec jak użyjesz samo e a nie e() to jebnie, tak samo jebnie jak wstawisz inne znaki, no i czasem minus traktuje źle, np (12+12)-12 jebnie, można naprawic (12+12)-(12) \npozdrawiam\n")
    expr = Screen().input('Enter an expression (using +,-,*,/,^, sin(), cos(), e(), pi()): \n')
    print(solve_expression(expr))
    Screen().input('Press [Enter] to continue')

def is_bracket(char):
    if char == '(' or char == '[' or char == '{' or char == ')' or char == ']' or char == '}':
        return True
    else:
        return False

def reverse_bracket(bracket):
    if bracket == '(':
        return ')'
    if bracket == ')':
        return '('
    if bracket == '[':
        return ']'
    if bracket == ']':
        return '['
    if bracket == '{':
        return '}'
    if bracket == '}':
        return '{'
    return ''

def is_open_bracket(bracket):
    if bracket == '(':
        return True
    if bracket == '[':
        return True
    if bracket == '{':
        return True
    return False

def check_bracket(list_expr):
    stack = []
    if not list_expr:
        return False

    for elem in list_expr:
        if 'bracket' in elem:
            br = elem['bracket']
            if is_open_bracket(br):
                stack.append(br)
            else:
                if not stack:
                    return False
                rbr = stack.pop()
                if not br == reverse_bracket(rbr):
                    return False
    if stack:
        #         stack is not empty
        return False
    else:
        return True

# this function convert str to list dictionary with keys: value, expr, bracket
def str_to_list_expr(string):
    string.replace(" ", "")
    string = "0+" + string + "()END0"
    last_char = 0
    result = []

    if string[0].isdigit():
        expr = False
    else:
        expr = True
    expression = ''
    try:
        while expression != 'END' and last_char < len(string):
            if not expr:
                if string[last_char] == "-":
                    i = last_char + 1
                else:
                    i = last_char

                while i < len(string) and string[i].isdigit():
                    i = i + 1
                if i < len(string) and string[i] == '.':
                    i += 1
                    while i < len(string) and string[i].isdigit():
                        i = i + 1

                if string[last_char:i] == '-':
                    #     only - in string
                    result.append({'expr': '-'})
                else:
                    value = float(string[last_char:i])
                    result.append({'value': value})
                last_char = i
                expr = True
            # else if expr == False
            else:
                if string[last_char] == "-":
                    i = last_char + 1
                else:
                    i = last_char

                while i < len(string) and not string[i].isdigit() and string[i] != '-':
                    i = i + 1
                expression = string[last_char:i]
                # find bracket and add to list all
                j = 0
                r = 0
                tym = ''
                while j < len(expression):
                    if is_bracket(expression[j]):
                        if r > 0:
                            result.append({'expr': expression[j - r: j]})
                            tym = expression[j - r: j]
                            r = 0
                        result.append({'bracket': expression[j]})
                    else:
                        r += 1
                    j += 1
                if r > 0:
                    result.append({'expr': expression[j - r: j]})
                    tym = expression[j - r: j]
                expression = tym

                last_char = i
                expr = False

    except IndexError:
        return None
    except ValueError:
        return None

    return result

# function below add to operation param:
#   name function to execute
#   type operation(one or two arguments operation)
#   operator precedence
def add_to_expr_param(list_expr):
    new_list = []
    for elem in list_expr:
        if 'expr' in elem:
            symbol = elem['expr']
            expr = get_expr(symbol)
            if expr:
                new_list.append(expr)
            else:
                expr = get_expr(symbol[0])
                expr2 = get_expr(symbol[1:])
                if expr and expr2:
                    new_list.append(expr)
                    new_list.append(expr2)

        else:
            new_list.append(elem)

    return new_list

def convert_to_rnp(list_expr):
    list_expr = add_to_expr_param(list_expr)

    converted = []
    oper_stack = []

    for elem in list_expr:
        if 'value' in elem:
            converted.append(elem)

        if 'expr' in elem:
            e_prec = elem['prec']
            if not oper_stack:
                oper_stack.append(elem)
            else:
                if 'prec' in oper_stack[-1]:
                    s_prec = oper_stack[-1]['prec']
                    if s_prec < e_prec:
                        oper_stack.append(elem)
                    else:
                        #             else gdy s_prec >= e_prec
                        while oper_stack and 'expr' in oper_stack[-1] and oper_stack[-1]['prec'] >= elem['prec']:
                            converted.append(oper_stack.pop())
                        #         nie jestem pewny co to tego
                        oper_stack.append(elem)
                else:
                    #             there on stack is bracket
                    oper_stack.append(elem)

        if 'bracket' in elem:
            if is_open_bracket(elem['bracket']):
                oper_stack.append(elem)
            else:
                while oper_stack:
                    if 'bracket' in oper_stack[-1]:
                        if is_open_bracket(oper_stack[-1]['bracket']):
                            oper_stack.pop()
                            break
                    else:
                        converted.append(oper_stack.pop())
    while oper_stack:
        converted.append(oper_stack.pop())

    return converted

def solve_expression(expression):
    list_expr = str_to_list_expr(expression)

    if not list_expr:
        return "Expression structure error"
    if not check_bracket(list_expr):
        return "Bracket error"

    rnp = convert_to_rnp(list_expr)

    try:
        while len(rnp) > 1:
            i = 0
            while i < len(rnp) and 'expr' not in rnp[i]:
                i += 1
            if i < len(rnp):
                oper = rnp[i]
                if oper['type'] == 2:
                    value = oper['func'](rnp[i - 2]['value'], rnp[i - 1]['value'])
                    del rnp[i - 2:i + 1]
                    rnp.insert(i - 2, {'value': value})
                if oper['type'] == 1:
                    value = oper['func'](rnp[i - 1]['value'])
                    del rnp[i - 1:i + 1]
                    rnp.insert(i - 1, {'value': value})
                if oper['type'] == 0:
                    value = oper['func']()
                    del rnp[i:i + 1]
                    rnp.insert(i, {'value': value})
            else:
                return "Something wrong with expression"

    except IndexError:
        return "Something wrong with expression"
    except ArithmeticError:
        return "Calculation error"

    return 'Result: ' + str(rnp[0]['value'])


'''
Created on 23 mar 2016

@author: Igor Deplano
'''

import pickle
import random
import numpy as np

class DataSuite(object):
    '''
    classdocs
    '''

    pickle_file = 'notMNIST.pickle'

    def __init__(self):
        try:
            f=open(self.pickle_file,'rb')
            self.databank=pickle.load(f)
            f.close()
        except Exception as e:
            import prepare_NotMNIST_data
            try:
                f=open(self.pickle_file,'rb')
                self.databank=pickle.load(f)
                f.close()
            except Exception as e:
                print('unable to load data', self.pickle_file, ':',e)
                raise

    def next_train_batch(self,batch_size):
        return self.__next_batch(self.databank['train_dataset'], self.databank['train_labels'], batch_size)

    def validationSet(self):
        validate_xs=np.array(self.databank['valid_dataset']).reshape(len(self.databank['valid_dataset']),784)#.transpose()
        validate_ys=self.cnv21_hot(self.databank['valid_labels'],10)
        return validate_xs,validate_ys

    def testSet(self):
        test_xs=self.databank['test_dataset'].reshape(len(self.databank['test_dataset']),784)#.transpose()
        test_ys=self.cnv21_hot(self.databank['test_labels'],10)
        return test_xs,test_ys

    def cnv21_hot(self,codes,limit):
        batch_size=len(codes)
        label=np.array(np.zeros([batch_size,limit]))
        for i in range(batch_size):
            label[i][codes[i]]=1
        return label

    def __next_batch(self,x,y,batch_size):
        base=random.randint(0, len(x)-batch_size)
        inputs=x[base:base+batch_size]
        perm=[ i for i in range(batch_size)]
        np.random.shuffle(perm)
        batch_tmp=y[base:base+batch_size]
        return inputs[perm],batch_tmp[perm]


# -*- coding: utf-8 -*-
"""
Created on Thu Mar 19 09:33:43 2020

@author: pc
"""
# this script is used to construct the framework of U-net

# import package
import tensorflow as tf
import utils as ut

# the framework is the same as brain paper
def FCN(name, in_data, cat_num, cha_num, is_train, reuse = False):

    # construction
    with tf.variable_scope(name, reuse = reuse):
        # layer 1
        net = ut.conv_layer(in_data, [3, 3, 3, 1, cha_num], 1, 'SAME', is_train)
        net = ut.conv_layer(net, [3, 3, 3, cha_num, 2*cha_num], 1, 'SAME', is_train)
        net = ut.max_pools3d(net, 2, 2, 'SAME')

        # layer 2
        net = ut.conv_layer(net, [3, 3, 3, 2*cha_num, 2*cha_num], 1, 'SAME', is_train)
        net = ut.conv_layer(net, [3, 3, 3, 2*cha_num, 4*cha_num], 1, 'SAME', is_train)
        net = ut.max_pools3d(net, 2, 2, 'SAME')

        # layer 3
        net = ut.conv_layer(net, [3, 3, 3, 4*cha_num, 4*cha_num], 1, 'SAME', is_train)
        net = ut.conv_layer(net, [3, 3, 3, 4*cha_num, 8*cha_num], 1, 'SAME', is_train)
        net = ut.max_pools3d(net, 2, 2, 'SAME')

        # layer 4
        w4 = ut.weight_variable([6, 6, 6, 8*cha_num, cat_num])
        net = ut.conv3d(net, w4, 1, 'VALID')
        net = tf.layers.batch_normalization(net, training = is_train)
        net = tf.nn.relu(net)

        # softmax
        net = tf.nn.softmax(net)

        return net

def FCN_ours(name, in_data, cat_num, is_train, kp1, kp2, reuse = False):
    # construction
    with tf.variable_scope(name, reuse = reuse):

        return


"""
A super-simple minidom recipe for translating a dict to xml. It's simple
to add any feature (recursive conversion, pickle etc) to this if you need.

>>> dict2xml({"ok": 123, 12323: "haha"}).toprettyxml()
<?xml version="1.0" ?>
<dictionary>
    <12323>
        haha
    </12323>
    <ok>
        123
    </ok>
</dictionary>

>>> verbose_dict2xml({"ok": 123, 12323: "haha"}).toprettyxml()
<?xml version="1.0" ?>
<dictionary>
    <item>
        <name>
            12323
        </name>
        <value>
            haha
        </value>
    </item>
    <item>
        <name>
            ok
        </name>
        <value>
            123
        </value>
    </item>
</dictionary>

"""
from xml2dict import object_dict
from xml.dom.minidom import Document

def dict2xml(dictionary, name="dictionary"):
    "The keys are element names and the values are text"
    xml = Document()
    items = xml.createElement(name)
    xml.appendChild(items)
    for key, val in dictionary.items():
        node = xml.createElement(str(key))
        node.appendChild(xml.createTextNode(str(val)))
        items.appendChild(node)
    return xml

def typeCheck(item):
    if (str(type(item)) == "<class 'object_dict.object_dict'>"
        or str(type(item)) == "<type 'dict'>"
        or str(type(item)) == "<class 'object_dict'>"
        or str(type(item)) == "<class '__main__.object_dict'>"):
        return True
    else:
        return False

def recurseXML(dictionary, items, xml):
    for key, val in dictionary.items():
#        print "\tVal Type2", type(val)
        if typeCheck(val):
            recurseXML(val, items, xml)
        else:
            item = xml.createElement("item")
            items.appendChild(item)
            name_node = xml.createElement("name")
            name_node.appendChild(xml.createTextNode(str(key)))
            value_node = xml.createElement("value")
            value_node.appendChild(xml.createTextNode(str(val)))
            item.appendChild(name_node)
            item.appendChild(value_node)

def verbose_dict2xml(dictionary, name="dictionary"):
    "2 nodes per item - one for the name and one for the value"
    xml = Document()
    items = xml.createElement(name)
    xml.appendChild(items)
    for key, val in dictionary.items():
        if typeCheck(val):
            recurseXML(val, items, xml)
        else:
            item = xml.createElement("item")
            items.appendChild(item)
            name_node = xml.createElement("name")
            name_node.appendChild(xml.createTextNode(str(key)))
            value_node = xml.createElement("value")
            value_node.appendChild(xml.createTextNode(str(val)))
            item.appendChild(name_node)
            item.appendChild(value_node)
    return xml

if __name__ == "__main__":
    print("haha")
    xml = dict2xml({"ok": 123, 12323: "haha"})
    print(xml.toprettyxml())
    xml = verbose_dict2xml({"ok": 123, 12323: {"haha":"Go Joe", "Try Again":{"hahaha":"Super Efficiency", 'gaga':"Tired"}}})
    print(xml.toprettyxml())
    print("done")


import torch
from torch import nn
from torch.nn import functional as F
from tqdm import tqdm
from torch.autograd import Variable
import numpy as np
## takes in a module and applies the specified weight initialization
def weights_init_normal(m):
    '''Takes in a module and initializes all linear layers with weight
        values taken from a normal distribution.'''

    classname = m.__class__.__name__
    # for every Linear layer in a model
    if classname.find('Linear') != -1:
        y = m.in_features
    # m.weight.data shoud be taken from a normal distribution
        m.weight.data.normal_(0.0,1/np.sqrt(y))
    # m.bias.data should be 0
        m.bias.data.fill_(0)

class GaussianNoise(nn.Module):
    def __init__(self,sigma=0.1):
        super(GaussianNoise, self).__init__()
        self.sigma = sigma
        self.noise = torch.tensor(0.0).to('cuda')

    def forward(self,x):
        x = x.to('cuda')
        if self.training and self.sigma!=0:
            scale = self.sigma*x.detach()
            sampled_noise = self.noise.repeat(*x.size()).normal_()*scale
            x = x + sampled_noise
        return x

class AAE(nn.Module):
    def __init__(self, input_dim, z_dim=10, y_dim = 2, h_dim=500):
        super(AAE, self).__init__()
        self.z_dim = z_dim
        self.y_dim = y_dim

        self.encoder = nn.Sequential(
            nn.Linear(input_dim,h_dim),
            nn.ReLU(),
            nn.Linear(h_dim,h_dim),
            nn.ReLU()
        )
        self.encode_y = nn.Linear(h_dim,y_dim)
        self.encode_z = nn.Linear(h_dim,z_dim)

        self.decode_y = nn.Linear(y_dim,h_dim,bias=False)
        self.decode_z = nn.Linear(z_dim,h_dim,bias=False)
        self.merged_bias = nn.Parameter(torch.ones(h_dim))

        self.decoder = nn.Sequential(
            nn.Linear(h_dim,h_dim),
            nn.ReLU(),
            nn.Linear(h_dim,input_dim),
            nn.Sigmoid()
        )

        self.discriminator_z = nn.Sequential(
            GaussianNoise(),
            nn.Linear(z_dim,h_dim),
            nn.ReLU(),
            nn.Linear(h_dim,h_dim),
            nn.ReLU(),
            nn.Linear(h_dim,2)
        )

        self.discriminator_y = nn.Sequential(
            GaussianNoise(),
            nn.Linear(y_dim,h_dim),
            nn.ReLU(),
            nn.Linear(h_dim,h_dim),
            nn.ReLU(),
            nn.Linear(h_dim,2)
        )

    def encode_yz(self, x, apply_softax_y = True):
        internal = self.encoder(x)
        y = self.encode_y(internal)
        z = self.encode_z(internal)
        if apply_softax_y:
            y = nn.functional.softmax(y,dim=1)
        return y,z

    def discriminate_z(self,z,apply_softax=False):
        logit = self.discriminator_z(z)
        if apply_softax:
            return nn.functional.softmax(logit)
        return logit

    def discriminate_y(self,y,apply_softax=False):
        logit = self.discriminator_y(y)
        if apply_softax:
            return nn.functional.softmax(logit)
        return logit

    def decode_yz(self,y,z):
        merged = self.decode_y(y)
        merged += self.decode_z(z)
        merged += self.merged_bias
        merged = nn.ReLU(merged)
        return self.decoder(merged)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 22 20:55:22 2018

@author: hhanchan
"""

import numpy as np
from numpy import linalg as LA
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
# (c)
A=np.matrix([[-1,0,0,0,0,0],[0,0.5,0,0,0,0],[0,0,0.5,0,0,0],[0,0,0,0.125,0.125,0],[0,0,0,0.125,0.125,0],[0,0,0,0,0,0.25]])
eta1=0.5
b=np.matrix([[1,1,1,1,1,1]]).transpose()
g=list(range(1001))
x=list(range(1001))
x[0]=np.matrix([[0,0,0,0,0,0]]).transpose()
g[0]=(LA.norm(A*x[0]-b))*(LA.norm(A*x[0]-b))
for k in range(1,1001):
    x[k]=x[k-1]+eta1*A.transpose()*(b-A*x[k-1])
    g[k]=(LA.norm(A*x[k]-b))*(LA.norm(A*x[k]-b))

v=np.matrix([[0,0,0,1,-1,0]]).transpose()
xhat=x[1000]+v
print (g[1000])
ghat=(LA.norm(A*xhat-b))*(LA.norm(A*xhat-b))
print (ghat)

print (LA.norm(xhat)*LA.norm(xhat))
print (LA.norm(x[1000])*LA.norm(x[1000]))

# (d)
eta2=0.75
y=list(range(101))
y[0]=np.matrix([[0,0,0,0,0,0]]).transpose()
for k in range(1,100):
    y[k]=y[k-1]+eta2*A.transpose()*(b-A*y[k-1])

kindex=np.arange(1,101,1)
f1=g[1:101]
f2=list(range(100))
f2[0]=(LA.norm(A*y[0]-b))*(LA.norm(A*y[0]-b))
for k in range(1,100):
    f2[k]=(LA.norm(A*y[k]-b))*(LA.norm(A*y[k]-b))
plt.plot(kindex,f1,'g-.',kindex,f2,'ro')
green=mpatches.Patch(color='green',label='$||Ax^{(k)}-b||_2^2$')
red=mpatches.Patch(color='red',label='$||Ay^{(k)}-b||_2^2$')
plt.legend(handles=[green,red])
plt.xlabel('k')
plt.ylabel('g(x)')
plt.title('g(x): Richardson Iteration')
plt.show()


"""
Using Harlem J Brothers' formulae for e"""

from decimal import *
from math import pow, factorial
def nthe(n):
    getcontext().prec = n
    final = Decimal(0)
    for i in range(n):
        final += Decimal((2*i+1)/factorial(2*i))
    print(final)
    return final


"""
    Lesson 002 - Pt7: String Methods II

    Python's ability to manipulate strings is one of the language's
    greatest strengths. The index() and len() methods, when combined
    with Python's slice syntax, makes extracting and/or manipulating
    string methods relatively painless. However, there are a number
    of other important methods that you'll want to know before we move
    on. Let's cover the:

        - replace()
        - find()
        - upper()
        - lower()

    and:

        - capitalize()

    methods.

"""

# Let's continue from our last lesson! In addition to extracting strings,
# you can utilize index(), len(), and slice syntax to modify specific substrings.
# For example:
#
#   original_string = "One Two Three"
#
#   search_string = "Three"
#
#   search_string_start = original_string.index(search_string)
#
#   new_string = original_string[:search_string_start] + "Four"
#
#   print(new_string)
#
# Try it out down below. Replace the substring "username" with "user_name", and
# then print the modified string out below.
#

original_string = "full_username"

### CODE HERE: ###

search_string = "username"

search_string_start = original_string.index(search_string)

new_string = original_string[:search_string_start] + "user_name"

print(new_string)

# You've just completed a relatively tricky bit of footwork. Thankfully, there are easier
# means of replacing a substring within a larger string. Python offers strings a simple,
# elegant method called:
#
#   replace()
#
# that replaces *all* occurences of the string passed to the method's arguments. Go
# ahead and uncomment the code below and note that we get the exact same result as
# the previous exercise!
#

# original_string = "full_username"

# original_string = original_string.replace("username", "user_name")

# print(original_string)
#

# Perfect! As said before, the important thing to remember here is that the
#
#   replace()
#
# method will replace *all* occurences of the string provided to the method.
# So if we had a string where we only wanted to replace a specific occurence
# (for example, the first occurence), the previous method of replacing a substring
# is ideal.
#
# Let's try below. Using the:
#
#   replace()
#
# method, replace the duplicate substring "Too" with the string "Three", then
# print the result below:
#

substring = "Too"

count_string = "One Two Too"

### CODE HERE: ###

count_string = count_string.replace(substring, "Three")

print("Fixed string:", count_string)

# Let's move on to our second method:
#
#   find()
#
# One of the cases we haven't considered is that if index() fails to find the
# substring passed to it, Python will throw an exception (an error). There are
# plentiful instances where this behavior is undesirable. Rather than simply
# erroring out, the:
#
#   find()
#
# method returns an integer type value of:
#
#   -1
#
# This is far less disruptive, and when used in tandem with conditional
# control structures (if/elif/else, we'll get to these soon), is preferable
# and incredibly powerful. Below, call the:
#
#   find()
#
# method on the:
#
#   original_string = "This is my test string."
#
# variable below, passing in the variable:
#
#   query_string = "string"
#
# as the argument, and storing the result in a new variable called:
#
#   starting_index
#
# to be printed below:
#

original_string = "This is my test string."

query_string = "string"

### CODE HERE: ###

starting_index = original_string.find(query_string)

print("Found query string at index:", starting_index)

# Finally, let's cover three methods that allow you to manipulate the
# capitalization of a string, beginning with:
#
#   upper()
#
# This method takes a Python string, and returns that string with all
# characters being upper case.  For example, calling:
#
#   upper()
#
# on the string variable below:
#
#   hulk_string = "Hulk angry."
#
# i.e.:
#
#   hulk_string = hulk_string.upper()
#
# and then printing:
#
#   print(hulk_string)
#
# would return:
#
#   "HULK ANGRY."
#
# Let's try it below. Call the:
#
#   upper()
#
# method on the string below, storing that result back in the variable (in-place
# assignment) and then printing the result. Note that the method takes *no arguments*.
#

store_status = "open"

### CODE HERE: ###

store_status = store_status.upper()

print("Uppercased output:", store_status)

# Of course, if:
#
#    upper()
#
# capitalizes all characters of a string, then the:
#
#   lower()
#
# method makes all characters in a string lower case. Like:
#
#   upper()
#
# the:
#
#   lower()
#
# method takes no arguments. Go ahead and call the:
#
#   lower()
#
# method on the:
#
#   store_status
#
# variable from above, storing the result back in the variable
# and printing the result.
#

### CODE HERE: ###

store_status = store_status.lower()

print("Back to normal casing:",store_status)

# Finally, let's take a look at the:
#
#   capitalize()
#
# method. Where as the:
#
#   upper()
#
# method makes all characters upper case, the:
#
#   capitalize()
#
# method only mutates the first character of a string. For example, while
# calling:
#
#   upper()
#
# on:
#
#   first_name = "John"
#
# would return:
#
#   "JOHN"
#
# calling:
#
#   capitalize()
#
# on:
#
#   first_name
#
# as below:
#
#   first_name.capitalize()
#
# returns:
#
#   "John"
#
# which is what we (more realistically) intend. Try it down below!
# Call:
#
#   capitalize()
#
# on both the:
#
#   first_name
#
# and:
#
#   last_name
#
# variables, then concatenate these two variables (seperated by a space)
# storing the result in a variable called:
#
#   full_name
#
# and printing:
#
#   full_name
#
# to console.
#

first_name = "betty"

last_name = "white"

### CODE HERE: ###

first_name = first_name.capitalize()

last_name = last_name.capitalize()

full_name = first_name + ' ' + last_name

print("Full name is:", full_name)


import math
import numpy as np

O_H = 0.9584

#decide accuracy in this function
def my_round(n):
    return round(n,7)

def O_T_vector(OList,TiList):
    vector = [TiList[1]-OList[1],TiList[2]-OList[2],TiList[3]-OList[3]]
    return map(my_round,vector)

def O_H_vector(vector):
    mag = math.sqrt(vector[0]**2 + vector[1]**2 + vector[2]**2)
    n_vector = map(lambda x:x*O_H/mag,vector) #normalize for O-H distance
    inv_vector = map(lambda x:-x,n_vector)
    return map(my_round,inv_vector)

def addH(O,HList):
    Hxyz = O_H_vector(O[5])
    H = ['H',Hxyz[0]+O[1],Hxyz[1]+O[2],Hxyz[2]+O[3]]
    theta = math.radians(76)
    rot76 = np.matrix([[math.cos(theta),(-1)*math.sin(theta)],[math.sin(theta),math.cos(theta)]])
    xy = np.matrix([[Hxyz[0]],[Hxyz[1]]])
    afterRot = np.dot(rot76,xy)
    Point = afterRot.tolist()
    H[1] = my_round(Point[0][0]) + O[1] #x,i don't know but result of tolist() is list in list
    H[2] = my_round(Point[1][0]) + O[2] #y
    HList.append(H)

#rotate O-H (104 degrees)
#O is imaginary Origin
#at last, add O coordinate
def my_round(n):
    return round(n,7)

def xyzDist(a,b,c,x,y,z):
    return float(math.sqrt((a-x)**2+(b-y)**2+(c-z)**2))

def countElem(Tis,Os,Hs):
    return len(Tis)+len(Os)+len(Hs)

def writexyz(Tis,Os,Hs):
    f = open("out.xyz",'w')
    f.write(str(countElem(Tis,Os,Hs))+'\n')
    f.write('#\n') #line for comment
    for Ti in Tis:
        f.write(str(Ti[0])+'\t\t')
        f.write(str(Ti[1])+'\t\t')
        f.write(str(Ti[2])+'\t\t')
        f.write(str(Ti[3])+'\t\t')
        f.write('\n')
    for O in Os:
        f.write(str(O[0])+'\t\t')
        f.write(str(O[1])+'\t\t')
        f.write(str(O[2])+'\t\t')
        f.write(str(O[3])+'\t\t')
        f.write('\n')
    for H in Hs:
        f.write(str(H[0])+'\t\t')
        f.write(str(H[1])+'\t\t')
        f.write(str(H[2])+'\t\t')
        f.write(str(H[3])+'\t\t')
        f.write('\n')
    f.close()


def helper(result, config, s, word_dict, begin, can_break):
    if begin == len(s):
        result.append(' '.join(config[:]))
        return

    for i in range(begin, len(s)):
        word = s[begin:i+1]
        if word in word_dict and can_break[i+1]:
            config.append(word)
            l = len(result)
            helper(result, config, s, word_dict, i + 1, can_break)
            can_break[i + 1] = l != len(result)
            config.pop()

def word_break(s, word_dict):

    can_break = [True] * (len(s) + 1)

    result = []
    helper(result, [], s, word_dict, 0, can_break)
    return result


import pygame

import settings

import levelgenerator

from cluster import Cluster
from gameobjects.actor import Actor
from gameobjects.brick import Brick
from gameobjects.coin import Coin
from gameobjects.monster import Monster
from gameobjects.exit import Exit

# Map van levelmap-id naar klasse voor game object.
ID_TO_OBJECT_CLASS = {}
ID_TO_OBJECT_CLASS[levelgenerator.BRICK] = Brick
ID_TO_OBJECT_CLASS[levelgenerator.COIN] = Coin
ID_TO_OBJECT_CLASS[levelgenerator.MONSTER] = Monster
ID_TO_OBJECT_CLASS[levelgenerator.EXIT] = Exit

class Level(object):
	"""Object voor een level.

		Hierin staat opgeslagen waar alle bricks zitten.

	"""

	def __init__(self):

		# Player object.
		self.player = None

		# Boolean voor of je het level hebt gehaald.
		self.finished = False

		# Bepaal de rect van het gehele level.
		self.rect = pygame.Rect(0, 0, settings.TILE_WIDTH*settings.LEVEL_WIDTH, settings.TILE_HEIGHT*settings.LEVEL_HEIGHT)

		# Bepaal de lijst met alle game objects van dit level.
		#
		# Dit doen we door eerst een levelmap te genereren met een level-
		# generator. Vervolgens lopen we de level map door en maken we alle game
		# objects aan.
		#
		levelMap = levelgenerator.generateLevelMap()
		gameObjectList = self.getGameObjectListFromLevelMap(levelMap)

		# Maak cluster aan waar alle game objects in worden opgeslagen.
		staticObjectList = [k for k in gameObjectList if isinstance(k, Actor) == False]
		self.cluster = Cluster(self, staticObjectList, self.rect)

		# Maak lijst aan met alle actors.
		self.actorList = [k for k in gameObjectList if isinstance(k, Actor) == True]

	def getGameObjectListFromLevelMap(self, levelMap):
		"""Return lijst met alle game objects beschreven in 'levelMap'.
		"""
		# De lijst met game objects die we gaan returnen.
		gameObjectList = []

		# Loop door alle tiles heen.
		for y in xrange(len(levelMap)):
			for x in xrange(len(levelMap[0])):

				# Bepaal de x/y positie.
				posX = x * settings.TILE_WIDTH
				posY = y * settings.TILE_HEIGHT

				# Haal tile op.
				tile = levelMap[y][x]

				# Loop door alle game object id's in de tile heen.
				for gameObjectId in tile:

					# Bepaal de class van het game object.
					gameObjectClass = ID_TO_OBJECT_CLASS[gameObjectId]

					# Maak aan en zet in de lijst.
					gameObjectRect = pygame.Rect(posX, posY, settings.TILE_WIDTH, settings.TILE_HEIGHT)
					gameObject = gameObjectClass(self, gameObjectRect)
					gameObjectList.append(gameObject)

		# Return de game objects.
		return gameObjectList

	def getGameObjectList(self, clusterCollisionRect):
		"""Return alle game objects.
			Er worden alleen statische game objects gereturned die colliden met
			'clusterCollisionRect'. Daarnaast worden alle actors gereturned.
		"""
		gameObjectList = self.cluster.getGameObjectList(clusterCollisionRect)
		gameObjectList += self.getActorList()
		return gameObjectList

	def removeGameObject(self, gameObject):
		"""Verwijder een game object uit het level.
		"""
		if isinstance(gameObject, Actor):
			self.actorList.remove(gameObject)
		else:
			self.cluster.removeGameObject(gameObject)

	def getActorList(self):
		"""Return lijst met alle actors.
		"""
		return self.actorList[:]

	def setFinished(self, finished):
		"""Set of het level is gehaald.
		"""
		self.finished = finished

	def isFinished(self):
		"""Return of je het level hebt gehaald.
		"""
		return self.finished

	def getPlayer(self):
		"""Return het player object.
		"""
		return self.player

	def setPlayer(self, player):
		"""Set het player object.
		"""
		self.player = player


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 11 20:07:23 2018

@author: augustus
"""

import numpy as np

print("Part (a): The program is now reading in the data given to us."+ "\n")
'Load the test data from the csv into the code'
testdata = np.loadtxt(open("hw3_X.csv", "rb"), delimiter=",", skiprows=0)
original = np.loadtxt(open("hw3_X.csv", "rb"), delimiter=",", skiprows=0)
holdermatrix = np.loadtxt(open("hw3_X.csv", "rb"), delimiter=",", skiprows=0)
intermediate = np.loadtxt(open("hw3_Y.csv", "rb"), delimiter=",", skiprows=0)
y_data=np.transpose([intermediate])
numcolumns=len(testdata[0])
numrows=len(testdata)

'End of part a'

print("Part (b): Creating matrix phi now...")

i=0
for i in range(numcolumns):
    'Step 1: select the i-1-th column of the matrix and transpose it'
    t=np.transpose([original[:,i]])
    'Step 2: multiply X and the column that you picked out element-wise'
    intermediate=np.multiply(original,t)
    a=np.delete(intermediate,np.s_[0:i], axis=1)
    'Step 3: add this resultant matrix to phi'
    holdermatrix=np.hstack((holdermatrix,a))
    derlength=len(a[0])
    'Step 4: delete the first column that was selected in Step 1 from the matrix'

    i+=1;

phi=holdermatrix
print("Done creating matrix phi from the dataset X.")
'Im also reassigning my column counter and row counter for phi'
numcolumns = len(phi[0])
D =len(phi[0])
numrows=len(phi)
print('The matrix has '+ str(D) + ' columns.'+ '\n')
'We want every fourth row looped into phi_training, thus the number of rows of the training set should be'
'one-fourth the number of rows of the original data set'
num_rows_train=numrows/4
num_train=numrows/4
phi_train = np.empty([0,numcolumns])
differ = numrows-(numrows/4)
phi_test = np.empty([0,numcolumns])
i=0

'End of part b'

print("Part (c): Creating phi_test set and phi_train set from phi now...")
'Below I split Phi into the training and test sets'
for i in range(numrows):
    d=i+1
    if d%4==0:
        phi_test=np.vstack([phi_test,phi[i]])
    else:
        phi_train =np.vstack([phi_train,phi[i]])
    i+=1;
print("Done creating phi_test set and phi_train set.")
print("Doing the same for y, creating y_train and y_test...")
'Now I need to do the same for y'
y_train = np.empty([0,1])
y_test = np.empty([0,1])
ynumrows=np.size(y_data)
i=0
for i in range(ynumrows):
    d=i+1
    if d%4 == 0:
        y_test=np.vstack([y_test,y_data[i]])
    else:
        y_train =np.vstack([y_train,y_data[i]])
    i+=1;

print("Done creating y_train and y_test sets from y."+ '\n')

'End of part c'

print("Part (d): Centering y_train set now...")

n_test = len(y_test)
'Now we proceed to part d:'
'I center y_train'

len_yt = len(y_train)
sum_yt = np.sum(y_train)
'ill need this variable for part f'
y_bar = float(sum_yt/len_yt)
new_ytrain = y_train-y_bar
print("Done centering y_train set.")

'I center phi_train below'
'First I need to make indeces based off the dimensions of phi_train'
row_len = len(phi_train[0])
column_len = len(phi_train)
n_train = len(phi_train)
D = len(phi_train[0])

'I also make a list to store phi_train averages, as ill need them for part f'

phi_avg_bucket = np.zeros(D)
i=0
print("Centering phi_train set now...")
for i in range(D):
    sum_column=np.sum(phi_train[:,i])
    avg= (sum_column/n_train)
    phi_avg_bucket[i] = avg
    phi_train[:,i]=phi_train[:,i]-avg
    i +=1;
print("Done centering the phi_train set. Scaling phi_train now...")

'I scale phi_train below.'
'I also need to store these zeta_j for part f'
zeta_bucket = np.zeros(D)

i=0
for i in range(D):
    zeta = np.linalg.norm(phi_train[:,i])
    zeta_bucket[i]=zeta
    phi_train[:,i]=np.divide(phi_train[:,i],zeta)
    i+=1;
y_train
print("Done centering and scaling phi_train." + "\n")

'End of part d'

print("Part (e): Now to compute the singular value decomposition of phi_train.")

u, s, tv = np.linalg.svd(phi_train, full_matrices = False)
'I transpose tv to obtain V'
v = tv.transpose()

ucolumnlen=len(u[0])
urowlen=len(u)
slen=len(s)
vrowlen=len(v)
vcolumnlen=len(v[0])

'Now to make the ridge lamda'
lamda = np.zeros(45)
exponent = -13.0

print("Now the code creates the list of 2's to the respective powers")
i=0
while exponent <=9:
    l = 2**(exponent)
    lamda[i]=l
    exponent = exponent + 0.5
    i+=1;

'Create diagonal matrix S_lamda'
'step 1: create array where we square all elements of the matrix'
'this is the same as multiplying an array by itself elementwise'
s_sqrd= np.multiply(s,s)

'step 2: pick an arbritray value from Lambda_ridge and add to s^2 values'
p=np.random.randint(0,46)
l = lamda[p]
denom= np.add(s_sqrd,l)

'step 3: create the new vector to be diagonalized'
vector_s_lamda = np.divide(s,denom)
s_lamda = np.diag(vector_s_lamda)

'Now to show the equality, we calcualate'
'(phi_transpose*phi + lamdaI)^(-1)*phi_transpose*ytrain'

phi_transpose = np.transpose(phi_train)
identity = np.identity(3080)
u_t = np.transpose(u)
lamda_mult_identity= identity * l

aa = np.linalg.inv(np.matmul(phi_transpose, phi_train) + lamda_mult_identity)
bb = np.matmul(phi_transpose, y_train)
w_ridge1 = np.matmul(aa,bb)

'Now to calculate V*S_lambda*U_transpose*y_train'
mult1 = np.matmul(u_t, y_train)
mult2 = np.dot(s_lamda,mult1)
w_ridge2 = np.matmul(v,mult2)

print('After calculating the two expressions in part (2), we take their difference and '
      + 'conclude equality if it returns the zero vector or a vector near-zero for all elements. Looping through '
      + ' the first 6 elements of the resultant difference vector gives:' )

diff_ridge = w_ridge1 - w_ridge2
rrange = len(diff_ridge)

i=0
for i in range(6):
    e = diff_ridge[i]
    print(e)
    i+=1;
print("This concludes part e." + "\n" )

'End of Part e'

print("Part (f): The test error of ridge regression as lambda varies:")
'Now for part f'
'We borrow the steps from the last problem and create w_ridge for each lamda'
s_sqrd= np.multiply(s,s)

'pick the first value from Lambda_ridge and add to s^2 values'

for l in lamda:

    denom= np.add(s_sqrd,l)

    'step 3: create the new vector to be diagonalized'
    vector_s_lamda = np.divide(s,denom)
    s_lamda = np.diag(vector_s_lamda)
    'Now to calculate V*S_lambda*U_transpose*y_train=our w_ridge of interest'
    mult1 = np.matmul(u_t, y_train)
    mult2 = np.dot(s_lamda,mult1)
    w_ridge = np.matmul(v,mult2)

    "Now we begin altering each element of w_ridge, multiplying by phi_bar_j and dividing by zeta_j..."
    'step 1: Divide the columns w_ridge_tilda and zeta element-wise'

    w_ridge_tilda = np.full((3080,1), 1)
    w_ridge_tilda2 = np.full((3080,1), 1)
    i=0
    for i in range(D):
        w_ridge_tilda[i] = w_ridge[i] / zeta_bucket[i]
        i +=1;

    'Now multiply each element by the corresponding average phi_j'
    i=0
    for i in range(D):
        w_ridge_tilda2[i] = w_ridge[i] * phi_avg_bucket[i]
        i +=1;

    sigma = np.sum(w_ridge_tilda2)
    'Done creating each w_ridge. Now we sum them all element-wise to obtain '
    w_sub_0_ridge = y_bar - sigma;

    'The value of w_0,ridge for the given lambda l is w_sub_0_ridge'
    "Finally to compute the test error."
    'Now actually compute the test error of ridge regression'
    one_vector = np.full((2000,1), 1)
    one_vector_mult_w = np.multiply(one_vector,w_sub_0_ridge)
    term3 = phi_test.dot(w_ridge_tilda2)
    o1 = y_test - one_vector_mult_w - term3
    'Now i calculate the 2 norm below'
    i=0
    o2 = np.linalg.norm(o1)
    test_error = o2 / (n_test)
    print('The test error is ' + str(test_error) + ' given a lambda value of ' + str(l));


#Setting up functions

def check_valid(rows): #Returns True/False if the new formation works
    for row in range(len(rows)):
        for item in range (len(rows[row])):
            if (item > 0 and rows[row][item] < rows[row][item-1]) or  (row > 0 and rows[row][item] < rows[row-1][item]):
                return False
    return True

def rotate(rows): #Rotates the list given by 90 degrees and returns new list
    rotates_lis = []
    for item in range (len(rows[0])-1, -1, -1):
        next_row = []
        for row in range(len(rows)):
            next_row.append(rows[row][item])
        rotates_lis.append(next_row)
    return rotates_lis

def figure_out(rows): #Return the first correct version of the rows
    if check_valid(rows):
        return rows
    for a in range(3):
        rows = rotate(rows)
        if check_valid(rows):
            return rows
#Gather data
rows = [[int(e) for e in input().split()] for a in range(int(input()))]

#Get and print answer
correct = figure_out(rows)
for row in correct:
    print(" ".join([str(e) for e in row]))


from nltk import word_tokenize
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
import json

def data_processing(dataset):
    dataset_lower = [data.lower() for data in dataset]
    dataset_tokenized = [word_tokenize(data) for data in dataset_lower]
    stop_words = stopwords.words('english')
    dataset_filtered = [words for words in dataset_tokenized if words not in stop_words]
    english_styled_punctuations = [',', '.''?', '/', ':', '<', '>', '[', ']', '(', ')', '*', '&', '^', '%', '$', '#',
                                   '@', '!', '~', '`', '+', '_', '-', '“', '”', '~~', '``', '`', '{', '}', str(''), '"',
                                   '\\', '…', '\'', '\"', '\'\'', '...']
    dataset_with_no_punctuations = [[words for words in data if words not in english_styled_punctuations] for data in
                                    dataset_filtered]
    return dataset_with_no_punctuations

def data_stemming(dataset):
    ps = PorterStemmer()
    dataset_stemmed = [ps.stem(word) for word in dataset]
    return dataset_stemmed

def dict_construction(path):
    contents = []
    with open(path) as f:
        content = f.readlines()
        contents.append(content)
    tweets = []
    a = contents[0]
    for i in range(len(a)):
        dict = {'tweet NO': i}
        dataset = [a[i]]
        dataset = data_processing(dataset)
        dataset = dataset[0]
        data = dataset[dataset.index('username') + 1:dataset.index('clusterno')]
        if 'http' not in dataset:
            text = dataset[dataset.index('text') + 1:dataset.index('timestr')]
        else:
            del dataset[dataset.index('http'):dataset.index('timestr')]
            text = dataset[dataset.index('text') + 1:dataset.index('timestr')]
        text = data_stemming(text)
        data.extend(text)
        time = dataset[dataset.index('timestr') + 1:dataset.index('+0000')]
        data.extend(time)
        dict['data'] = data
        tweets.append(dict)
    return tweets

def build_inverted_index(tweets):
    dict_inv = {}
    for i in tweets:
        for j in i['data']:
            if j not in dict_inv.keys():
                dict_inv[j] = [i['tweet NO']]
            else:
                dict_inv[j].append(i['tweet NO'])
    return dict_inv

def merge(dict_inv, query):
    query = query_process(query)
    merge_list = []
    for i in query:
        merge_list.append(dict_inv[i])
    while len(merge_list) != 1:
        merge_list_small = []
        for i in range(1, len(merge_list)):
            temp = []
            for item1 in merge_list[0]:
                for item2 in merge_list[i]:
                    if item1 == item2:
                        temp.append(item1)
                        break
            merge_list_small.append(temp)
        merge_list = merge_list_small
        print(len(merge_list))
    return merge_list

def OR(dict_inv, word1, word2):
    list1 = dict_inv[word1]
    list2 = dict_inv[word2]
    not_list = [word for word in list1 if word in list2]
    return not_list

def NOT(dict_inv, word1, word2):
    list1 = dict_inv[word1]
    list2 = dict_inv[word2]
    or_list = [word for word in list1 if word not in list2]
    return or_list

def query_process(query):
    query = data_processing([query])[0]
    query = data_stemming(query)
    return query

if __name__ == '__main__':
    path = 'C:\\Users\\97653\\Desktop\\人工智能\\IR&DM\\Homework3-tweets\\tweets.txt'
    tweets = dict_construction(path)
    dict_inv = build_inverted_index(tweets)
    query = 'Ron Weasley birthday'
    result = merge(dict_inv, query)
    print(result)