'''
Build a tweet sentiment analyzer
'''

from __future__ import print_function
import six.moves.cPickle as pickle
import time
from collections import OrderedDict
import sys
import time
from sys import argv
import numpy
import theano
from theano import config
import theano.tensor as tensor
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams

import imdb
from imdb import masklist

import scipy.io as sio
datanamee = 'sat'
datasets = {datanamee: imdb.load_data}

# Set the random number generators' seeds for consistency
SEED = 123
numpy.random.seed(SEED)

def numpy_floatX(data):
    # return numpy.asarray(data, dtype=config.floatX)
    return numpy.asarray(data, dtype=config.floatX)

def get_minibatches_idx(n, minibatch_size, shuffle=False):
    """
    Used to shuffle the dataset at each iteration.
    """

    idx_list = numpy.arange(n, dtype="int32")

    if shuffle:
        numpy.random.shuffle(idx_list)

    minibatches = []
    minibatch_start = 0
    for i in range(n // minibatch_size):
        minibatches.append(idx_list[minibatch_start:
                                    minibatch_start + minibatch_size])
        minibatch_start += minibatch_size

    if (minibatch_start != n):
        # Make a minibatch out of what is left
        minibatches.append(idx_list[minibatch_start:])
    # print('minibatches', minibatches)
    return zip(range(len(minibatches)), minibatches)

def get_dataset(name):
    return datasets[name]

def zipp(params, tparams):
    """
    When we reload the model. Needed for the GPU stuff.
    """
    for kk, vv in params.items():
        tparams[kk].set_value(vv)

def unzip(zipped):
    """
    When we pickle the model. Needed for the GPU stuff.
    """
    new_params = OrderedDict()
    for kk, vv in zipped.items():
        new_params[kk] = vv.get_value()
    return new_params

def dropout_layer(state_before, use_noise, trng):
    proj = tensor.switch(use_noise,
                         (state_before *
                          trng.binomial(state_before.shape,
                                        p=0.5, n=1,
                                        dtype=state_before.dtype)),
                         state_before * 0.5)
    return proj

def _p(pp, name):
    return '%s_%s' % (pp, name)

def init_params(options):
    """
    Global (not LSTM) parameter. For the embeding and the classifier.
    """
    params = OrderedDict()
    # embedding
    # randn = numpy.random.rand(options['n_words'],
    #                          options['dim_proj'])
    # params['Wemb'] = (0.01 * randn).astype(config.floatX)
    params = get_layer(options['encoder'])[0](options,
                                              params,
                                              prefix=options['encoder'])
    # classifie
    for i in range(recyl_maxlen):
        # params for label prediction
        params['U_' + str(i)] =  (numpy.random.randn((i + 1) * options['dim_proj'],
                                                int(options['ydim'])).astype(config.floatX) /
                                                numpy.sqrt((i + 1) * options['dim_proj']))
        params['b_' + str(i)] = numpy.zeros((int(options['ydim']),)).astype(config.floatX)

        # params for modal prediction
        params['U_seq_' + str(i)] =  (numpy.random.randn( options['dim_proj'],
                                                int( options['maxlen'])).astype(config.floatX)
                                                / numpy.sqrt(options['dim_proj']))
        params['b_seq_' + str(i)] = numpy.zeros((int(options['maxlen']),)).astype(config.floatX)

    return params

def load_params(path, params):
    pp = numpy.load(path)
    for kk, vv in params.items():
        if kk not in pp:
            raise Warning('%s is not in the archive' % kk)
        params[kk] = pp[kk]

    return params

def init_tparams(params):
    tparams = OrderedDict()
    for kk, pp in params.items():
        tparams[kk] = theano.shared(params[kk], name=kk)
    return tparams

def get_layer(name):
    fns = layers[name]
    return fns

def ortho_weight(ndim):
    W = numpy.random.randn(ndim, ndim)
    u, s, v = numpy.linalg.svd(W)
    return u.astype(config.floatX)

def param_init_lstm(options, params, prefix='lstm'):
    """
    Init the LSTM parameter:

    :see: init_params
    """
    W = numpy.concatenate([ortho_weight(options['dim_proj']),
                           ortho_weight(options['dim_proj']),
                           ortho_weight(options['dim_proj']),
                           ortho_weight(options['dim_proj'])], axis=1)
    params[_p(prefix, 'W')] = W
    U = numpy.concatenate([ortho_weight(options['dim_proj']),
                           ortho_weight(options['dim_proj']),
                           ortho_weight(options['dim_proj']),
                           ortho_weight(options['dim_proj'])], axis=1)
    params[_p(prefix, 'U')] = U
    b = numpy.zeros((4 * options['dim_proj'],))
    params[_p(prefix, 'b')] = b.astype(config.floatX)

    return params

def lstm_layer(tparams, state_below, options, prefix='lstm', mask=None,h_before=None,c_before=None):
    nsteps = state_below.shape[0]
    if state_below.ndim == 3:
        n_samples = state_below.shape[1]
    else:
        n_samples = 1

    assert mask is not None

    def _slice(_x, n, dim):
        if _x.ndim == 3:
            return _x[:, :, n * dim:(n + 1) * dim]
        return _x[:, n * dim:(n + 1) * dim]

    def _step(m_, x_, h_, c_):
        preact = tensor.dot(h_, tparams[_p(prefix, 'U')])
        preact += x_

        i = tensor.nnet.sigmoid(_slice(preact, 0, options['dim_proj']))
        f = tensor.nnet.sigmoid(_slice(preact, 1, options['dim_proj']))
        o = tensor.nnet.sigmoid(_slice(preact, 2, options['dim_proj']))
        c = tensor.tanh(_slice(preact, 3, options['dim_proj']))

        c = f * c_ + i * c
        c = m_[:, None] * c + (1. - m_)[:, None] * c_

        h = o * tensor.tanh(c)
        h = m_[:, None] * h + (1. - m_)[:, None] * h_

        return h, c

    state_below = (tensor.dot(state_below, tparams[_p(prefix, 'W')]) +
                   tparams[_p(prefix, 'b')])

    dim_proj = options['dim_proj']
    [h,c] = _step(mask, state_below,h_before,c_before)
    return h,c

# ff: Feed Forward (normal neural net), only useful to put after lstm
#     before the classifier.
layers = {'lstm': (param_init_lstm, lstm_layer)}

def sgd(lr, tparams, grads, x, x3,y2, mask, y, cost,modal_cost, max_cost):
    """ Stochastic Gradient Descent

    :note: A more complicated version of sgd then needed.  This is
        done like that for adadelta and rmsprop.

    """
    # New set of shared variable that will contain the gradient
    # for a mini-batch.
    gshared = [theano.shared(p.get_value() * 0., name='%s_grad' % k)
               for k, p in tparams.items()]
    gsup = [(gs, g) for gs, g in zip(gshared, grads)]

    # Function that computes gradients for a mini-batch, but do not
    # updates the weights.
    # f_grad_shared = theano.function([idxs, x, mask, y], cost, updates=gsup,
    #                                 name='sgd_f_grad_shared')
    f_grad_shared = theano.function([mask, y, x, x3,y2, modal_cost, max_cost], cost, updates=gsup,
                                    name='sgd_f_grad_shared')

    pup = [(p, p - lr * g) for p, g in zip(tparams.values(), gshared)]

    # Function that updates the weights from the previously computed
    # gradient.
    f_update = theano.function([lr], [], updates=pup,
                               name='sgd_f_update')

    return f_grad_shared, f_update

def adadelta(lr, tparams, grads, x, x3,y2, mask, y, cost, modal_cost,max_cost):
    """
    An adaptive learning rate optimizer

    Parameters
    ----------
    lr : Theano SharedVariable
        Initial learning rate
    tpramas: Theano SharedVariable
        Model parameters
    grads: Theano variable
        Gradients of cost w.r.t to parameres
    x: Theano variable
        Model inputs
    mask: Theano variable
        Sequence mask
    y: Theano variable
        Targets
    cost: Theano variable
        Objective fucntion to minimize

    Notes
    -----
    For more information, see [ADADELTA]_.

    .. [ADADELTA] Matthew D. Zeiler, *ADADELTA: An Adaptive Learning
       Rate Method*, arXiv:1212.5701.
    """

    zipped_grads = [theano.shared(p.get_value() * numpy_floatX(0.),
                                  name='%s_grad' % k)
                    for k, p in tparams.items()]
    running_up2 = [theano.shared(p.get_value() * numpy_floatX(0.),
                                 name='%s_rup2' % k)
                   for k, p in tparams.items()]
    running_grads2 = [theano.shared(p.get_value() * numpy_floatX(0.),
                                    name='%s_rgrad2' % k)
                      for k, p in tparams.items()]

    zgup = [(zg, g) for zg, g in zip(zipped_grads, grads)]
    rg2up = [(rg2, 0.95 * rg2 + 0.05 * (g ** 2))
             for rg2, g in zip(running_grads2, grads)]

    f_grad_shared = theano.function([mask, y, x, x3, y2, modal_cost, max_cost], cost, updates=zgup + rg2up,
                                    name='adadelta_f_grad_shared', on_unused_input='ignore', allow_input_downcast=True)
    # f_grad_shared = theano.function([idxs, x, mask, y], cost, updates=zgup + rg2up,
    #                                 name='adadelta_f_grad_shared')

    updir = [-tensor.sqrt(ru2 + 1e-6) / tensor.sqrt(rg2 + 1e-6) * zg
             for zg, ru2, rg2 in zip(zipped_grads,
                                     running_up2,
                                     running_grads2)]
    ru2up = [(ru2, 0.95 * ru2 + 0.05 * (ud ** 2))
             for ru2, ud in zip(running_up2, updir)]
    param_up = [(p, p + ud) for p, ud in zip(tparams.values(), updir)]

    f_update = theano.function([lr], [], updates=ru2up + param_up,
                               on_unused_input='ignore',
                               name='adadelta_f_update', allow_input_downcast=True)

    return f_grad_shared, f_update

def rmsprop(lr, tparams, grads, x, mask, y, cost):
    """
    A variant of  SGD that scales the step size by running average of the
    recent step norms.

    Parameters
    ----------
    lr : Theano SharedVariable
        Initial learning rate
    tpramas: Theano SharedVariable
        Model parameters
    grads: Theano variable
        Gradients of cost w.r.t to parameres
    x: Theano variable
        Model inputs
    mask: Theano variable
        Sequence mask
    y: Theano variable
        Targets
    cost: Theano variable
        Objective fucntion to minimize

    Notes
    -----
    For more information, see [Hint2014]_.

    .. [Hint2014] Geoff Hinton, *Neural Networks for Machine Learning*,
       lecture 6a,
       http://cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf
    """

    zipped_grads = [theano.shared(p.get_value() * numpy_floatX(0.),
                                  name='%s_grad' % k)
                    for k, p in tparams.items()]
    running_grads = [theano.shared(p.get_value() * numpy_floatX(0.),
                                   name='%s_rgrad' % k)
                     for k, p in tparams.items()]
    running_grads2 = [theano.shared(p.get_value() * numpy_floatX(0.),
                                    name='%s_rgrad2' % k)
                      for k, p in tparams.items()]

    zgup = [(zg, g) for zg, g in zip(zipped_grads, grads)]
    rgup = [(rg, 0.95 * rg + 0.05 * g) for rg, g in zip(running_grads, grads)]
    rg2up = [(rg2, 0.95 * rg2 + 0.05 * (g ** 2))
             for rg2, g in zip(running_grads2, grads)]

    # f_grad_shared = theano.function([x, mask, y], cost,
    #                                 updates=zgup + rgup + rg2up,
    #                                 name='rmsprop_f_grad_shared')
    f_grad_shared = theano.function([mask, y, x], cost,
                                    updates=zgup + rgup + rg2up,
                                    name='rmsprop_f_grad_shared')

    updir = [theano.shared(p.get_value() * numpy_floatX(0.),
                           name='%s_updir' % k)
             for k, p in tparams.items()]
    updir_new = [(ud, 0.9 * ud - 1e-4 * zg / tensor.sqrt(rg2 - rg ** 2 + 1e-4))
                 for ud, zg, rg, rg2 in zip(updir, zipped_grads, running_grads,
                                            running_grads2)]
    param_up = [(p, p + udn[1])
                for p, udn in zip(tparams.values(), updir_new)]
    f_update = theano.function([lr], [], updates=updir_new + param_up,
                               on_unused_input='ignore',
                               name='rmsprop_f_update')

    return f_grad_shared, f_update

def build_model(tparams, options):
    '''
    x: traing data
    y: traing label
    x3: neighbor data, datanum * neighbornum * featuredim
    y2: neighbor label
    '''
    trng = RandomStreams(SEED)
    # Used for dropout.
    use_noise = theano.shared(numpy_floatX(0.))
    maskl = tensor.matrix('maskl', dtype=config.floatX)
    y = tensor.vector('y', dtype='int32')
    x = tensor.matrix('x', dtype=config.floatX)
    n_samples = x.shape[0]
    dim_proj = x.shape[1]
    maxlen = options['maxlen']
    x3 = tensor.tensor3('x3', dtype=config.floatX)
    y2 = tensor.matrix('y2', dtype='int32')
    neigh_num = x3.shape[1]
    x_nerghbors = tensor.reshape(x3, [n_samples * neigh_num, dim_proj])
    modal_cost = tensor.vector('modal_cost', dtype=config.floatX)
    max_cost = tensor.scalar('max_cost', dtype=config.floatX)
    h = tensor.alloc(numpy_floatX(0.),n_samples,dim_proj)
    c = tensor.alloc(numpy_floatX(0.),n_samples,dim_proj)
    h_n = tensor.alloc(numpy_floatX(0.),n_samples * neigh_num,dim_proj)
    c_n = tensor.alloc(numpy_floatX(0.),n_samples * neigh_num,dim_proj)
    cost = 0
    cost1_mean = []
    cost2_mean = []
    cost3_mean = []
    next_mean = []
    mask = tensor.ones_like(x[:,0], dtype= config.floatX) # maks whether instance enter the  ith iter
    mask_n = tensor.ones_like(x_nerghbors[:,0], dtype= config.floatX)
    masks = []
    projs = []
    masks.append(mask)
    next_modal = tensor.zeros_like(x[:,0], dtype= 'int32')
    next_modal_n = tensor.zeros_like(x_nerghbors[:,0], dtype= 'int32')
    # cost_vector = tensor.alloc(numpy_floatX(0.),n_samples,1)
    cost_vector = tensor.alloc(numpy_floatX(0.),1, n_samples)
    f_pred_set = []
    f_pred_seq_set = []
    f_pred_seq_prob_set = []
    f_get_fea_set = []
    f_fea_other_set = []
    def get_other3(x, next_modal):
        fea_other = tensor.tile(x,(maxlen,1))
        fea_other = x.T
        fea_single = fea_other[:,next_modal]
        return fea_other,fea_single

    def get_other(x):
        # change the feature x from dim to the form of maxlen * dim
        fea_other = []
        for i in range(maxlen):
            fea_other.append(x * maskl[i])
        return tensor.stack(fea_other)
    def get_single(x,next_modal):
        # get the current modal' feature
        fea_single = x * maskl[next_modal]
        return fea_single

    def compute_dist(neighbor, pred_neighbor, fea_single, pred, mask, y, y2):
        '''
        minimize same label neighbor's distance, maximize different label neighbor's distance
        neighbor: neighbor's feature
        pred_neighbor: neighbor's netmodal's prediction
        fea_single: current instance's feature
        pred: current instance's prediction
        mask: whether current instance stops
        y: current instance's label
        y2: neighbor instance's label
        '''
        loss = 0
        if mask:
            ifsamelabel = -1
            for i in range(3):
                if y == y2[i]:
                    ifsamelabel = 1
                else:
                    ifsamelabel = -1
                dist = tensor.dot(get_other(neighbor[i]).T, pred_neighbor[i]) - tensor.dot(get_other(fea_single).T,pred)
                loss += ifsamelabel * tensor.dot(dist , dist.T)
        return loss/3

    costs = tensor.tile(modal_cost,(n_samples,1))
    xs = []
    for i in range(recyl_maxlen):
        # set high cost for modal that has been used to prevent predict same modal
        costs = tensor.set_subtensor(costs[tensor.arange(n_samples), next_modal], 1)
        feas, update = theano.scan(fn = get_single,
                            sequences=[x, next_modal],
                            )
        fea_single_n, update_n = theano.scan(fn = get_single,
                            sequences=[x_nerghbors, next_modal_n],
                            )
        fea_single = feas
        max_coefficients_supported = 10000

        xs.append(fea_single)

        [h,c] = get_layer(options['encoder'])[1](tparams, fea_single, options,
                                                prefix=options['encoder'],
                                                mask=mask,h_before = h,c_before = c)
        [h_n,c_n] = get_layer(options['encoder'])[1](tparams, fea_single_n, options,
                                                prefix=options['encoder'],
                                                mask=mask_n,h_before = h_n,c_before = c_n)
        proj = h
        proj_n = h_n
        projs.append(proj)
        projsmatrix = tensor.stack(projs)
        proj_pred = tensor.stack(projs) * tensor.stack(masks)[:, :, None]
        proj_pred = tensor.transpose(proj_pred,(1,0,2))
        proj_pred = tensor.reshape(proj_pred,[projsmatrix.shape[1],projsmatrix.shape[0] * projsmatrix.shape[2]])
        # print('h_n.shape', h_n.shape)
        if options['use_dropout']:
            proj_pred = dropout_layer(proj_pred, use_noise, trng)
        pred = tensor.nnet.softmax(tensor.dot(proj_pred, tparams['U_' + str(i)]) + tparams['b_' + str(i)])

        print('i', i)

        f_pred_prob = theano.function([ x, maskl,modal_cost, max_cost], pred,
                                                name='f_pred_prob',on_unused_input='ignore', allow_input_downcast=True)
        f_pred = theano.function([x, maskl, modal_cost, max_cost], pred.argmax(axis=1),
                                                name='f_pred',on_unused_input='ignore', allow_input_downcast=True)
        f_pred_set.append(f_pred)

        off = 1e-8
        if pred.dtype == 'float16':
            off = 1e-6

        pred_seq = tensor.nnet.softmax(tensor.dot(proj, tparams['U_seq_' + str(i)]) + tparams['b_seq_' + str(i)])
        pred_seq_n = tensor.nnet.softmax(tensor.dot(proj_n, tparams['U_seq_' + str(i)]) + tparams['b_seq_' + str(i)])

        f_pred_seq = theano.function([x, maskl, modal_cost,max_cost], pred_seq.argmax(axis=1),
                        name='f_pred_seq',on_unused_input='ignore',allow_input_downcast=True)

        f_pred_seq_set.append(f_pred_seq)

        pred_seq_index = pred_seq.argmax(axis=1)
        next_modal = pred_seq_index
        next_modal_n = pred_seq_n.argmax(axis=1)
        next_mean.append(next_modal)
        cost1_vector = tensor.log(pred[tensor.arange(n_samples), y] + off)
        cost1 = ( cost1_vector * mask).sum() / (mask.sum() + 1)

        pred_seq_n3 = tensor.reshape(pred_seq_n, [n_samples , neigh_num, maxlen])
        result_loss2, update = theano.scan(fn = compute_dist,
                            sequences=[x3, pred_seq_n3, x, pred_seq, mask, y, y2],
                            )
        cost2 = result_loss2.mean()
        cost3 = (costs * pred_seq).mean()
        cost1_mean.append(cost1)
        cost2_mean.append(cost2)
        cost3_mean.append(cost3)
        lamda1 = 0.001
        lamda2 = 0.1
        if i == recyl_maxlen - 1:
            lamda1 = 0.000000001
            lamda2 = 0.000000001
        cost += -cost1 +  lamda1 * cost2 + lamda2 * cost3
        # cost += -cost1
        # f_fea_other = theano.function([x, x3, y, maskl, modal_cost, max_cost],[nnext, D,cost1,cost2,cost3,mask.sum(),next_modal, fea_single, fea_other, fea_single3, fea_other3], on_unused_input='ignore')
        # f_fea_other_set.append(f_fea_other)
        result, update = theano.scan(lambda b,a: a[b],
                                sequences = pred_seq_index,
                                non_sequences = modal_cost )
        if i == 0:
            cost_vector = result
        else:
            cost_vector += result
        # mask the instance if its cost larger than max_cost
        choice = tensor.nonzero(tensor.gt(-cost_vector,-max_cost))[0]
        mask = tensor.zeros_like(x[:,0], dtype = config.floatX)
        mask = theano.tensor.set_subtensor(mask[choice],1.)
        masks.append(mask)
        if i < recyl_maxlen:
            cost -= (2 * (1-mask) * cost1_vector).sum() / (mask.sum() + 1)
        else:
            cost -= cost1
    f_fea_other = theano.function([x, x3,y2, y, maskl, modal_cost, max_cost],
            [tensor.stack(cost1_mean), tensor.stack(cost2_mean), tensor.stack(cost3_mean)], on_unused_input='ignore')

    return use_noise, x,x3, y2, maskl, y, cost,modal_cost,max_cost,f_pred_set,f_pred_seq_set, f_fea_other

def pred_error(maxlen, f_pred_set,f_pred_seq_set, data, iterator,maskl, model_len, modal_cost, max_cost,verbose=False):
    """
    Just compute the error
    f_pred: Theano fct computing the prediction
    prepare_data: usual prepare_data for that dataset.
    """
    valid_err = 0
    length = 0
    meancost = 0
    for _, valid_index in iterator:
        feax = [data[0][t] for t in valid_index]
        feax = numpy.array(feax)
        targets = [data[1][t] for t in valid_index]
        n_samples = len(valid_index)
        mask = numpy.ones_like(feax[:,0], dtype= 'int32')
        next_modal = numpy.zeros_like(feax[:,0], dtype= 'int32')
        cost_vector = numpy.zeros(n_samples)
        next_modals = numpy.zeros([n_samples,maxlen])
        mask_matrix = numpy.zeros([n_samples, maxlen])
        preds_s = numpy.zeros([n_samples, maxlen])
        preds = numpy.zeros(n_samples)
        costs = numpy.tile(modal_cost,(n_samples,1))
        for i in range(recyl_maxlen):
            cost_vector += costs[range(n_samples), next_modal]
            costs[range(n_samples), next_modal] = 0
            mask_matrix[:,i] = mask
            next_modal = f_pred_seq_set[i](feax, maskl, modal_cost, max_cost)
            next_modals[:,i] = next_modal
            mask = numpy.zeros_like(feax[:,0], dtype= 'int32')
            choice = numpy.greater(-cost_vector,-max_cost)
            mask[choice] = 1
            preds_s[:,i] = f_pred_set[i](feax, maskl, modal_cost, max_cost)
        meancost += cost_vector.sum()
        for i in range(n_samples):
            prem = False
            for j in range(maxlen):
                if mask_matrix[i,j] == 0:
                    prem = True
                    preds[i] = preds_s[i, j-1]
                    break
            if prem == False:
                preds[i] =  preds_s[i, -1]
        valid_err += (preds == targets).sum()
        length += len(preds)
    valid_err = 1. - numpy_floatX(valid_err) / length

    return valid_err, meancost / length

def get_neighbor(sam_traininx, neighinx, trainx, trainy):
    neighs = []
    neiy = []
    trainy = numpy.array(trainy)
    for i in range(len(sam_traininx)):
        ninx = neighinx[sam_traininx[i]]
        neighs.append(trainx[ninx,:])
        neiy.append(trainy[ninx])
    return numpy.array(neighs), numpy.array(neiy)

def train_lstm(
    dim_proj=36,  # word embeding dimension and LSTM number of hidden units.
    patience=15,  # Number of epoch to wait before early stop if no progress
    max_epochs=5000,  # The maximum number of epoch to run
    dispFreq=10,  # Display to stdout the training progress every N updates
    decay_c=0.,  # Weight decay for the classifier applied to the U weights.
    lrate=0.01,  # Learning rate for sgd (not used for adadelta and rmsprop)
    optimizer=adadelta,  # sgd, adadelta and rmsprop available, sgd very hard to use, not recommanded (probably need momentum and decaying learning rate).
    #  optimizer = sgd,
    encoder='lstm',  # TODO: can be removed must be lstm.
    saveto='lstm_model_best.npz',  # The best model will be saved there
    validFreq=100,  # Compute the validation error after this number of update.
    saveFreq=1110,  # Save the parameters after every saveFreq updates
    maxlen=4,  # Sequence longer then this get ignored
    batch_size=64,  # The batch size during training.
    valid_batch_size=256,  # The batch size used for validation/test set.
    dataset=datanamee,
    modal_costs = [0.1, 0.1, 0.1, 0.1], # the cost for each modal
    # model_lens = model_len,
    # Parameter for extra option
    noise_std=0.,
    use_dropout=True,  # if False slightly faster, but worst test error
                       # This frequently need a bigger model.
    reload_model=None,  # Path to a saved model we want to start from.
    test_size=-1,  # If >0, we keep only this number of test example.
    max_costs = 50, # max cost for each instance to use
):

    # Model options
    model_options = locals().copy()
    print("model options", model_options)

    load_data = get_dataset(dataset)

    print('Loading data')
    train, valid, test, mask, model_len, train_n3 = load_data()
    '''
    train: (train data, train label)
    valid: (valid data, valid label)
    test: (test data, test label)
    mask: mask each modal's feature
    model_len: each model's length
    train_n3: each traing data's 3 neighbor's index
    '''
    ydim = numpy.max(train[1]) + 1
    #
    model_options['ydim'] = ydim
    print('ydim', ydim)
    print('numpy.min(train[1])', numpy.min(train[1]))
    print('Building model')
    # This create the initial parameters as numpy ndarrays.
    # Dict name (string) -> numpy ndarray
    params = init_params(model_options)
    if reload_model:
        load_params('lstm_model.npz', params)

    # This create Theano Shared Variable from the parameters.
    # Dict name (string) -> Theano Tensor Shared Variable
    # params and tparams have different copy of the weights.
    tparams = init_tparams(params)
    (use_noise, x, x3, y2, mask, y, cost,
            modal_cost,max_cost,f_pred_set, f_pred_seq_set, f_fea_other) = build_model(tparams, model_options)
    if decay_c > 0.:
        decay_c = theano.shared(numpy_floatX(decay_c), name='decay_c')
        weight_decay = 0.
        weight_decay += (tparams['U'] ** 2).sum()
        weight_decay *= decay_c
        cost += weight_decay
    print('starting compute grad...')
    print(time.asctime( time.localtime(time.time()) ) )
    grads = tensor.grad(cost, wrt=list(tparams.values()))
    f_grad = theano.function([mask, y, x,x3, y2, modal_cost,max_cost], grads, name='f_grad',on_unused_input='ignore')
    lr = tensor.scalar(name='lr')
    print('starting optimizer')
    print(time.asctime( time.localtime(time.time()) ) )
    f_grad_shared, f_update = optimizer(lr, tparams, grads,
                                        x, x3, y2, mask, y, cost, modal_cost,max_cost)
    kf_valid = get_minibatches_idx(len(valid[0]), valid_batch_size)
    kf_test = get_minibatches_idx(len(test[0]), valid_batch_size)

    print("%d train examples" % len(train[0]))
    print("%d valid examples" % len(valid[0]))
    print("%d test examples" % len(test[0]))

    history_errs = []
    best_p = None
    bad_count = 0

    if validFreq == -1:
        validFreq = len(train[0]) // batch_size
    if saveFreq == -1:
        saveFreq = len(train[0]) // batch_size

    uidx = 0  # the number of update done
    estop = False  # early stop
    start_time = time.time()
    try:
        for eidx in range(max_epochs):
            n_samples = 0

            # Get new shuffled index for the training set.
            kf = get_minibatches_idx(len(train[0]), batch_size, shuffle=False)

            for _, train_index in kf:
                feax3, feay2 = get_neighbor(train_index, train_n3, train[0], train[1])
                '''
                feax3: this mini-batch's data's feature
                feay2: this mini-batch's data's label
                '''
                uidx += 1
                use_noise.set_value(1.)

                # Select the random examples for this minibatch
                y = [train[1][t] for t in train_index]
                feax = [train[0][t] for t in train_index]
                n_samples += len(feax)
                maskl = masklist(model_len, dim_proj)

                cost = f_grad_shared(maskl, y, feax, feax3,feay2, modal_costs,max_costs)
                f_update(lrate)
                if numpy.isnan(cost) or numpy.isinf(cost):
                    print('bad cost detected: ', cost)
                    return 1., 1., 1.

                if numpy.mod(uidx, dispFreq) == 0:
                    print('Epoch ', eidx, 'Update ', uidx, 'Cost ', cost)

                if saveto and numpy.mod(uidx, saveFreq) == 0:
                    print('Saving...')

                    if best_p is not None:
                        params = best_p
                    else:
                        params = unzip(tparams)
                    numpy.savez(saveto, history_errs=history_errs, **params)
                    pickle.dump(model_options, open('%s.pkl' % saveto, 'wb'), -1)
                    print('Done')

                if numpy.mod(uidx, validFreq) == 0:
                    use_noise.set_value(0.)
                    maskls = masklist(model_len, dim_proj)
                    train_err, tracost = pred_error(maxlen, f_pred_set, f_pred_seq_set, train, kf,maskls, model_len, modal_costs, max_costs)
                    print('~~~~~~~~~~~train_err',train_err)
                    print('~~~~~~~~~~~tracost',tracost)
                    kf_valid = get_minibatches_idx(len(valid[0]), valid_batch_size)
                    kf_test = get_minibatches_idx(len(test[0]), valid_batch_size)
                    valid_err, valcost = pred_error(maxlen, f_pred_set, f_pred_seq_set, valid, kf_valid,maskls, model_len, modal_costs, max_costs)
                    test_err, tstcost = pred_error(maxlen, f_pred_set, f_pred_seq_set, test, kf_test,maskls, model_len, modal_costs, max_costs)
                    history_errs.append([valid_err, test_err, valcost, tstcost])
                    print('history_errs)', history_errs)
                    if (best_p is None or
                        valid_err <= numpy.array(history_errs)[:,
                                                               0].min()):

                        best_p = unzip(tparams)
                        bad_counter = 0

                    print( ('Train ', train_err, 'Valid ', valid_err,
                           'Test ', test_err) )
                    print( ('Traincost ', tracost, 'Validcost ', valcost,
                           'Testcost ', tstcost) )

                    if (len(history_errs) > patience and
                        valid_err >= numpy.array(history_errs)[:-patience,
                                                               0].min()):
                        bad_counter += 1
                        if bad_counter > patience:
                            print('Early Stop!')
                            estop = True
                            break

            print('Seen %d samples' % n_samples)

            if estop:
                break

    except KeyboardInterrupt:
        print("Training interupted")

    end_time = time.time()
    if best_p is not None:
        zipp(best_p, tparams)
    else:
        best_p = unzip(tparams)
    print('best valid',numpy.array(history_errs)[:,
                                           0].min())
    print('best valid',numpy.array(history_errs)[:,
                                           1].min())
    use_noise.set_value(0.)
    kf_train_sorted = get_minibatches_idx(len(train[0]), batch_size)
    train_err, tracost = pred_error(maxlen, f_pred_set, f_pred_seq_set, train, kf_train_sorted,maskls, model_len, modal_costs, max_costs)

    kf_valid = get_minibatches_idx(len(valid[0]), valid_batch_size)
    kf_test = get_minibatches_idx(len(test[0]), valid_batch_size)
    valid_err, valcost = pred_error(maxlen, f_pred_set, f_pred_seq_set, valid, kf_valid,maskls, model_len, modal_costs, max_costs)

    test_err,tstcost = pred_error(maxlen, f_pred_set, f_pred_seq_set, test, kf_test,maskls, model_len, modal_costs, max_costs)

    print( 'Train ', train_err, 'Valid ', valid_err, 'Test ', test_err )
    print( ('Traincost ', tracost, 'Validcost ', valcost,
           'Testcost ', tstcost) )
    if saveto:
        numpy.savez(saveto, train_err=train_err,
                    valid_err=valid_err, test_err=test_err,
                    history_errs=history_errs, **best_p)
    print('The code run for %d epochs, with %f sec/epochs' % (
        (eidx + 1), (end_time - start_time) / (1. * (eidx + 1))))
    print( ('Training took %.1fs' %
            (end_time - start_time)), file=sys.stderr)
    return train_err, valid_err, test_err

if __name__ == '__main__':
    # See function train for all possible parameter and there definition.
    recyl_maxlen = int(argv[1]) # the max madal can been used for one instance
    train_lstm(
        max_epochs=100,
        test_size=500,
    )


from collections import Counter

import numpy as np
import pandas
import pprint

# noinspection PyUnresolvedReferences
from utils import tokenize

# nltk.download('stopwords')

# importing corpus as resume
resume_file = open('../assets/resume.txt', 'r')
resume = resume_file.read().lower()
resume_file.close()

# tokenizing the resume
tokens = tokenize(resume)

# dividing corpus into 6 documents
k = len(tokens) // 6
documents = []
for i in range(5):
    documents.append(tokens[i * k: (i + 1) * k])
documents.append(tokens[5 * k:])

# calculating most common 5 tokens from each document
most_common = set()
for document in documents:
    frequencies = Counter(document)
    for word, frequency in frequencies.most_common(5):
        most_common.add(word)

# creating one hot vector for each word in most common
vectors = {}
for word in most_common:
    vector = [0] * 6
    for index, document in enumerate(documents):
        vector[index] = int(word in document)
    vectors[word] = vector

pprint.pp(vectors)

# one hot vector representation
table = pandas.DataFrame(data=vectors)

# writing the table in a text file to view output
file = open('../assets/one-hot-vector.txt', 'w')
file.write(table.to_string())
file.close()


import argparse
import socket

from pretenders.server.base import in_parent_process, save_pid_file
from pretenders.server.log import get_logger
from pretenders.client import BossClient
from pretenders.common.constants import RETURN_CODE_PORT_IN_USE

LOGGER = get_logger("pretenders.server.pretender")

class Pretender(object):
    def __init__(self, uid, host, port, boss_port):
        self.uid = uid
        self.boss_port = boss_port
        self.host = host
        self.port = port
        self.boss_api_handler = BossClient(host, boss_port).boss_access

        if in_parent_process():
            save_pid_file("pretenders-mock-{0}.pid".format(uid))

    def run(self):
        raise NotImplementedError("run() not defined in {0}".format(self.__class__))

    @classmethod
    def start(cls):
        server = cls.from_command_line_args()
        try:
            server.run()
        except socket.error as e:
            LOGGER.exception("Socket error", exc_info=e)
            LOGGER.info(f"QUITTING {server.uid} {server.__class__.__name__}")
            import sys

            sys.exit(RETURN_CODE_PORT_IN_USE)

    @classmethod
    def from_command_line_args(cls):
        """Default parser for mock server scripts.

        Parse command line args and return the parsed object.
        """
        parser = argparse.ArgumentParser(description="Start the server")
        parser.add_argument(
            "-H",
            "--host",
            dest="host",
            default="localhost",
            help="host/IP to run the server on (default: localhost)",
        )
        parser.add_argument(
            "-p",
            "--port",
            dest="port",
            type=int,
            default=8001,
            help=("port number to run the " "server on (default: 8001)"),
        )
        parser.add_argument(
            "-b",
            "--boss",
            dest="boss_port",
            default="8000",
            help="port for accessing the Boss server.",
        )
        parser.add_argument(
            "-d",
            "--debug",
            dest="debug",
            default=False,
            action="store_true",
            help="start a build right after creation",
        )
        parser.add_argument("-i", "--uid", dest="uid")
        args = parser.parse_args()
        return cls(
            uid=args.uid, host=args.host, port=args.port, boss_port=args.boss_port
        )

    def store_history_retrieve_preset(self, body):
        return self.boss_api_handler.http(
            "POST", url="/replay/{0}".format(self.uid), body=body
        )


d1 = int(input())
d2 = int(input())
m = int(input())
f = (10 * m) / (d1 + d2)
print(f'Scar conseguiu criar uma frustração {f:.2f} na turma')
if f >= 4:
    print('Eu matei Mufasa')
elif f > 2:
    print('Consegui lacaios preciosos')
else:
    print('Mais um fracasso...')


import pandas as pd
from stable_baselines3.ppo import MlpPolicy
from stable_baselines3.common.callbacks import CheckpointCallback
from stable_baselines3 import PPO
from torch import nn
from tqdm import trange

from golds.contracts import Currency, Stock, Option, OptionFlavor, OptionStyle, Holdings
from golds.env import AmericanOptionEnv
from golds.mkt_data import PricingSource, SingleStockGBMMarketDataSource
from golds.params import GBMParams
from golds.reward_functions import NaiveHedgingRewardFunction
from golds.tcost import NaiveTransactionCostModel

REWARD_KAPPA = 100
INITIAL_WEALTH = 1e7
SELF_FINANCING_LAMBDA = 1000
TRADING_DAYS_IN_YEAR = 252

def main():
    aapl = Stock(ticker="AAPL", is_tradable=True)
    warrant = Option(
        strike=100,
        expiry_time=1.,
        underlying=aapl,
        flavor=OptionFlavor.CALL,
        style=OptionStyle.EUROPEAN,
        is_tradable=False
    )
    cash = Currency(code="USD", is_tradable=False)

    initial_holdings: Holdings = {
        aapl: 0.,
        warrant: 100.,
        cash: INITIAL_WEALTH,
    }

    universe = list(initial_holdings.keys())

    gbm_params = GBMParams(mu=0.005, sigma=0.2, risk_free_rate=0.)

    mkt_data_source = SingleStockGBMMarketDataSource(universe, gbm_params, data_reuse_num_episodes=5*3000)
    tcost_model = NaiveTransactionCostModel(universe)
    pricing_source = PricingSource(mkt_data_source, tcost_model)

    env = AmericanOptionEnv(
        episode_length=TRADING_DAYS_IN_YEAR,
        pricing_source=pricing_source,
        reward_function=NaiveHedgingRewardFunction(kappa=REWARD_KAPPA, initial_holdings=initial_holdings),
        actions_config=list(range(-100, 101))
    )

    # TODO experiment with gamma, gae_lambda, ent_coef, vf_coef, max_grad_norm (kwargs to PPO.__init__)
    # TODO experiment with batch size (how to do this?)
    # TODO Lerrel says entropy related to exploration -- increase ent_coef if agent is not exploring enough
    # TODO experiment with different number of hidden nodes per layer in "net_arch" (64? 128? more?)
    policy_kwargs = {"activation_fn": nn.ReLU, "net_arch": [32]*5}
    model = PPO(MlpPolicy, env, verbose=1, learning_rate=1e-4, policy_kwargs=policy_kwargs)
    N_YEARS_TRAINING = 5_000_000
    checkpoint_callback = CheckpointCallback(save_freq=1000, save_path='./checkpoints/', name_prefix='hedging_model')
    model.learn(total_timesteps=TRADING_DAYS_IN_YEAR*N_YEARS_TRAINING, callback=checkpoint_callback)

    # TODO should log the training output here somehow (loss over time)
    # with open("american_option_env_5e6.pkl", "w+b") as f:
    #     pickle.dump(env, f)
    # model.save("trained_hedging_model_5e6")

    # with open("american_option_env.pkl", "rb") as f:
    #     env = pickle.load(f)
    # model = PPO.load("trained_hedging_model")

    NUM_YEARS_OUT_OF_SAMPLE = 10000
    print(f"Done training. Evaluating on {NUM_YEARS_OUT_OF_SAMPLE} years of data.")

    evaluation_histories = []
    reward_histories = []

    for i in trange(NUM_YEARS_OUT_OF_SAMPLE):
        evaluation_records = []
        obs = env.reset()
        for day in range(TRADING_DAYS_IN_YEAR):
            prices = env.observation_array_to_dict(obs)
            # print(f"Observed prices: {prices}")
            action, _states = model.predict(obs)
            trade = env.action_array_to_dict(action)
            # print(f"Executed trade: {trade}")
            obs, rewards, dones, info = env.step(action)
            # print(f"Received reward: {rewards}")
            evaluation_records.append({"obs": prices, "action": trade, "reward": rewards})
            env.render()

        evaluation_history = pd.DataFrame.from_records(evaluation_records)
        reward_history = pd.DataFrame.from_records(env.reward_function._reward_records)

        evaluation_history["run_number"] = i
        reward_history["run_number"] = i

        evaluation_histories.append(evaluation_history)
        reward_histories.append(reward_history)

    evaluation_history = pd.concat(evaluation_histories, ignore_index=True)
    reward_history = pd.concat(reward_histories, ignore_index=True)

    evaluation_history.to_hdf("evaluation_history.h5", key="df", mode="w")
    reward_history.to_hdf("reward_history.h5", key="df", mode="w")

    '''
    evaluation_records = []
    obs = env.reset()
    for day in range(TRADING_DAYS_IN_YEAR):
        prices = env.observation_array_to_dict(obs)
        print(f"Observed prices: {prices}")
        action, _states = model.predict(obs)
        trade = env.action_array_to_dict(action)
        print(f"Executed trade: {trade}")
        obs, rewards, dones, info = env.step(action)
        print(f"Received reward: {rewards}")
        evaluation_records.append({"obs": prices, "action": trade, "reward": rewards})
        env.render()

    pd.DataFrame.from_records(evaluation_records).to_hdf("rl_results.h5", key="df", mode="w")

    env.reward_function.persist_history_to_hdf("reward_history.h5", key="df", mode="w")
    '''

if __name__ == '__main__':
    main()


import argparse

import pytorch_lightning as pl
import torch
from torch.nn import functional as F

import constants
from lightling_wrapper import BaseTorchLightlingWrapper, SpeechCommandDataModule
from models.bc_resnet.bc_resnet_model import BcResNetModel
from models.bc_resnet.mel_spec_dataset import MelSpecDataSet, mel_collate_fn
from models.simple_conv.base_dataset import AudioArrayDataSet, simconv_collate_fn
from models.simple_conv.simple_conv_model import SimpleConv

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", type=str, default="conv")
    parser.add_argument("--pretrain", type=str)

    return parser.parse_args()

if __name__ == "__main__":
    args = parse_args()

    if args.model == "conv":
        core_model = SimpleConv(
            n_channel=constants.N_CHANNEL, kernel_size_l1=constants.KERNEL_SIZE_L1)
        loss_fn = F.nll_loss
        collate_fn = simconv_collate_fn
        dataset_fn = AudioArrayDataSet
    elif args.model == "bc_resnet":
        core_model = BcResNetModel(
            scale=constants.SCALE_BC_RESNET, dropout=constants.DROPOUT)
        loss_fn = F.nll_loss
        collate_fn = mel_collate_fn
        dataset_fn = MelSpecDataSet
    else:
        raise ValueError("Invalid model name")

    model = BaseTorchLightlingWrapper(
        core_model=core_model,
        loss_fn=loss_fn,
        learning_rate=constants.LEARNING_RATE,
    )

    data_module = SpeechCommandDataModule(
        dataset_fn, collate_fn, batch_size=constants.BATCH_SIZE
    )

    if torch.cuda.is_available():
        trainer = pl.Trainer(
            accelerator="gpu", devices=1
        )
    else:
        trainer = pl.Trainer()

    trainer.test(model, data_module, ckpt_path=args.pretrain)


import numpy as np
import matplotlib.pyplot as plt

class Kmeans(object):

    def __init__(self, k=1):
        self.k = k

    def train(self, data, verbose=1):

        shape = data.shape

        ranges = np.zeros((shape[1], 2))
        centroids = np.zeros((shape[1], 2))

        for dim in range(shape[1]):
            ranges[dim, 0] = np.min(data[:,dim])
            ranges[dim, 1] = np.max(data[:,dim])

        if verbose == 1:
            print('Ranges: ')
            print(ranges)

        centroids = np.zeros((self.k, shape[1]))
        for i in range(self.k):
            for dim in range(shape[1]):
                centroids[i, dim] = np.random.uniform(ranges[dim, 0], ranges[dim, 1], 1)

        if verbose == 1:
            print('Centroids: ')
            print(centroids)

            plt.scatter(data[:,0], data[:,1])
            plt.scatter(centroids[:,0], centroids[:,1], c = 'r')
            plt.show()

        count = 0
        while count < 100:
            count += 1
            if verbose == 1:
                print('-----------------------------------------------')
                print('Iteration: ', count)

            distances = np.zeros((shape[0],self.k))
            for ix, i in enumerate(data):
                for ic, c in enumerate(centroids):
                    distances[ix, ic] = np.sqrt(np.sum((i-c)**2))

            labels = np.argmin(distances, axis = 1)

            new_centroids = np.zeros((self.k, shape[1]))
            for centroid in range(self.k):
                temp = data[labels == centroid]
                if len(temp) == 0:
                    return 0
                for dim in range(shape[1]):
                    new_centroids[centroid, dim] = np.mean(temp[:,dim])

            if verbose == 1:
                plt.scatter(data[:,0], data[:,1], c = labels)
                plt.scatter(new_centroids[:,0], new_centroids[:,1], c = 'r')
                plt.show()

            if np.linalg.norm(new_centroids - centroids) < np.finfo(float).eps:
                print("DONE!")
                break

            centroids = new_centroids
        self.centroids = centroids
        self.labels = labels
        if verbose == 1:
            print(labels)
            print(centroids)
        return 1

    def getAverageDistance(self, data):

        dists = np.zeros((len(self.centroids),))
        for ix, centroid in enumerate(self.centroids):
            temp = data[self.labels == ix]
            dist = 0
            for i in temp:
                dist += np.linalg.norm(i - centroid)
            dists[ix] = dist/len(temp)
        return dists

    def getLabels(self):
        return self.labels


# -*- coding: utf-8 -*-
'''
    :codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
# Import Python libs
from __future__ import absolute_import

# Import Salt Testing Libs
from salttesting import skipIf, TestCase
from salttesting.mock import (
    NO_MOCK,
    NO_MOCK_REASON,
    MagicMock,
    patch
)

from salttesting.helpers import ensure_in_syspath

ensure_in_syspath('../../')

# Import Salt Libs
from salt.states import rabbitmq_plugin

rabbitmq_plugin.__opts__ = {}
rabbitmq_plugin.__salt__ = {}

@skipIf(NO_MOCK, NO_MOCK_REASON)
class RabbitmqPluginTestCase(TestCase):
    '''
    Test cases for salt.states.rabbitmq_plugin
    '''
    # 'enabled' function tests: 1

    def test_enabled(self):
        '''
        Test to ensure the RabbitMQ plugin is enabled.
        '''
        name = 'some_plugin'

        ret = {'name': name,
               'changes': {},
               'result': True,
               'comment': ''}

        mock = MagicMock(side_effect=[True, False])
        with patch.dict(rabbitmq_plugin.__salt__,
                        {'rabbitmq.plugin_is_enabled': mock}):
            comt = ('Plugin some_plugin is already enabled')
            ret.update({'comment': comt})
            self.assertDictEqual(rabbitmq_plugin.enabled(name), ret)

            with patch.dict(rabbitmq_plugin.__opts__, {'test': True}):
                comt = ('Plugin some_plugin is set to be enabled')
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(rabbitmq_plugin.enabled(name), ret)

    # 'disabled' function tests: 1

    def test_disabled(self):
        '''
        Test to ensure the RabbitMQ plugin is disabled.
        '''
        name = 'some_plugin'

        ret = {'name': name,
               'changes': {},
               'result': True,
               'comment': ''}

        mock = MagicMock(side_effect=[False, True])
        with patch.dict(rabbitmq_plugin.__salt__,
                        {'rabbitmq.plugin_is_enabled': mock}):
            comt = ('Plugin some_plugin is not enabled')
            ret.update({'comment': comt})
            self.assertDictEqual(rabbitmq_plugin.disabled(name), ret)

            with patch.dict(rabbitmq_plugin.__opts__, {'test': True}):
                comt = ('Plugin some_plugin is set to be disabled')
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(rabbitmq_plugin.disabled(name), ret)

if __name__ == '__main__':
    from integration import run_tests
    run_tests(RabbitmqPluginTestCase, needs_daemon=False)


import sys
import heapq

class PriorityQueue:
    """
      Implements a priority queue data structure.
    """
    def  __init__(self):
        self.heap = []
        self.count = 0
        self.items = []

    def push(self, item, priority):
        entry = (priority, self.count, item)
        heapq.heappush(self.heap, entry)
        self.items.append(item)
        self.count += 1

    def pop(self):
        (_, _, item) = heapq.heappop(self.heap)
        self.items.remove(item)
        return item

    def isEmpty(self):
        return len(self.heap) == 0

    def __contains__(self, item):
        return item in self.items

    def update(self, item, priority):
        for index, (p, c, i) in enumerate(self.heap):
            if i == item:
                if p <= priority:
                    break
                del self.heap[index]
                self.heap.append((priority, c, item))
                heapq.heapify(self.heap)
                break
        else:
            self.push(item, priority)

class Graph:
    def __init__(self, vertices, edges):
        self.vertices =  vertices
        self.edges = {}
        self.weights = {}
        for vertex in vertices:
            self.edges[vertex] = []
        for edge in edges:
            self.edges[edge[0]].append(edge[1])
            self.edges[edge[1]].append(edge[0])
            self.weights[(min(edge[0], edge[1]),max(edge[0], edge[1]))] = edge[2]

    def neighbors(self, node):
        return self.edges[node]

    def get_nodes(self):
        return self.vertices

    def get_cost(self, from_node, to_node):
        key = (min(from_node, to_node), max(from_node, to_node))
        return self.weights[key]

def read_file():
    # Read in the number of vertices (n) and edges (m)
    edges, queries = [], []
    vertices =set()
    n = int(input())
    m = int(input())

    # Read the edges from stdin.
    for _ in range(m):
        arr = input().split()
        start = int(arr[0])
        end = int(arr[1])
        vertices.add(start)
        vertices.add(end)
        cost = float(arr[2])
        edges.append((start, end, cost))

    # Read the A edges. You may want to use a different data-structure.
    q = int(input())
    for i in range(q):
        arr = input().split()
        v = int(arr[0])
        w = int(arr[1])
        queries.append((min(v,w), max(v,w)))

    return vertices, edges, queries

# Print the weight of the mst to two decimal-places.

def prim_mst(graph, edges):
    marked = {}
    pq = PriorityQueue()
    mst = []
    nodes = graph.get_nodes()
    visit(graph, nodes[0], marked, pq, edges)
    while not pq.isEmpty():
        edge = pq.pop()
        v = edge[0]
        w = edge[1]
        if v in marked and w in marked:
            continue
        mst.append(edge)
        if v not in marked:
            visit(graph, v, marked,pq, edges)
        if w not in marked:
            visit(graph, w, marked, pq, edges)
    return mst

def visit(graph,v , marked, pq, edges):
    marked[v] = True
    for neighbor in graph.neighbors(v):
        edge = (min(v, neighbor), max(v, neighbor))
        cost = graph.get_cost(v, neighbor)
        if edge in edges:
            cost = 0
        if neighbor not in marked:
            pq.push(edge, cost)

if __name__ == "__main__":
    vertices, edges, queries = read_file()
    vertices = list(vertices)
    graph = Graph(vertices, edges)
    path = prim_mst(graph, queries)
    total_weight = 0
    for edge in path:
        total_weight += graph.get_cost(edge[0], edge[1])
    print("%.2f" % total_weight)


import logging

from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver

from timeline.models import Timeline

# Models
class SubscribeRecord(models.Model):
    is_read = models.BooleanField(
        default=False,
        verbose_name='Запись прочитана пользователем'
    )
    record = models.OneToOneField(
        'blog.Record',
        on_delete=models.CASCADE,
        verbose_name='Запись',
    )

    class Meta:
        verbose_name = 'Запись из блога на которую подписан пользователь'
        verbose_name_plural = 'Записи из блога на которую подписан пользователь'

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

class SubscribeByBlog(models.Model):
    timeline = models.ForeignKey(
        'timeline.Timeline',
        on_delete=models.CASCADE,
        verbose_name='Лента записей'
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='Дата создания'
    )
    subscribes_record = models.ManyToManyField(
        'blog.SubscribeRecord',
        verbose_name='Подписка на новость',
    )

    class Meta:
        verbose_name = 'Подписка на блог'
        verbose_name_plural = 'Подписки на блоги'

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

class Blog(models.Model):
    name = models.fields.CharField(
        max_length=150,
        verbose_name='Название'
    )
    user = models.OneToOneField(
        'auth.User',
        on_delete=models.CASCADE,
        verbose_name='Пользователь'
    )
    subscribes_by_blog = models.ManyToManyField(
        'blog.SubscribeByBlog',
        verbose_name='Подписки на блог',
    )

    class Meta:
        verbose_name = 'Блог'
        verbose_name_plural = 'Блоги'

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

class Record(models.Model):
    title = models.fields.CharField(
        max_length=150,
        verbose_name='Заголовок'
    )
    text = models.TextField(
        verbose_name='Текст'
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='Дата создания'
    )
    blog = models.ForeignKey(
        'blog.Blog',
        on_delete=models.CASCADE,
        verbose_name='Блог',
    )

    class Meta:
        verbose_name = 'Запись'
        verbose_name_plural = 'Записи'

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

# Signals for models
@receiver(post_save, sender=User)
def create_blog(sender, instance, created, **kwargs):
    if created:
        Blog.objects.create(
            name=f'Блог пользователя {instance.username}',
            user=instance,
        )

@receiver(post_save, sender=Record)
def create_subscribe_record(sender, instance, created, **kwargs):
    from manager_tasks.tasks import send_notify
    if created:
        subscribes_by_blog = instance.blog.subscribes_by_blog.all()
        if subscribes_by_blog:
            send_notify.delay(instance.id, instance.blog.id)
            for subscribe_by_blog in subscribes_by_blog:
                subscribe_by_blog.subscribes_record.add(SubscribeRecord.objects.create(record=instance))

@receiver(post_delete, sender=Record)
def delete_subscribe_record(sender, instance, **kwargs):
    subscribes_by_blog = instance.blog.subscribes_by_blog.all()
    if subscribes_by_blog:
        for subscribe_by_blog in subscribes_by_blog:
            subscribe_by_blog.subscribes_record.filter(record=instance).delete()


import os

from PySide6 import QtWidgets
from PySide6.QtGui import QFont, QIcon
from PySide6.QtWidgets import (
    QCheckBox,
    QComboBox,
    QFileDialog,
    QFrame,
    QLabel,
    QLineEdit,
    QPushButton,
    QRadioButton,
    QSizePolicy,
    QSpacerItem,
    QTabWidget,
    QTextEdit,
    QWidget,
)

from nadeocr.GUI.functions.utils.extra import edit_config_ini, get_data, read_config_ini
from nadeocr.GUI.widgets.input_hotkey_widget import MainHotkeyExecution

_ROOT = os.path.abspath(os.path.dirname(__file__))

class OptionsWidget(QWidget):
    def __init__(self, parent):
        self.parent = parent
        super(OptionsWidget, self).__init__()
        self.setWindowTitle("Opciones")
        self.setFixedSize(425, 310)
        icon_path = get_data(_ROOT, "../../resources/assets", "icon.ico")

        self.setWindowIcon(QIcon(icon_path))

        self.config_reader = read_config_ini()

        layout = QtWidgets.QGridLayout()
        self.setLayout(layout)

        self.tabs = QTabWidget()
        self.tab_general = QWidget()
        self.tab_advanced = QWidget()
        self.tab_about = QWidget()

        self.tabs.addTab(self.tab_general, "General")
        self.tabs.addTab(self.tab_advanced, "Avanzado")
        self.tabs.addTab(self.tab_about, "Acerca de")

        # Create first tab layout
        self.tab_general.layout = QtWidgets.QGridLayout(self)
        self.tab_advanced.layout = QtWidgets.QGridLayout(self)
        self.tab_about.layout = QtWidgets.QGridLayout(self)

        # Add tabs to main widget
        layout.addWidget(self.tabs)
        self.tab_general.setLayout(self.tab_general.layout)
        self.tab_advanced.setLayout(self.tab_advanced.layout)
        self.tab_about.setLayout(self.tab_about.layout)

        # General Tab
        self.label_interface_user = QLabel("Interfaz de usuario")
        self.label_interface_user.setFont(QFont("Arial", 9, weight=QFont.Bold))
        self.tab_general.layout.addWidget(self.label_interface_user, 0, 0, 1, 5)

        self.label_language = QLabel("Idioma")
        self.tab_general.layout.addWidget(self.label_language, 1, 0)

        self.box_language = QComboBox()
        self.box_language.addItem("Español")
        self.box_language.addItem("Inglés")
        self.tab_general.layout.addWidget(self.box_language, 1, 1, 1, 5)

        self.label_notification_pos = QLabel("Posición notificación")
        self.tab_general.layout.addWidget(self.label_notification_pos, 2, 0, 1, 2)

        spanish_to_pos_text = {
            "TopLeft": "Esquina superior izquierda",
            "TopRight": "Esquina superior derecha",
            "BottomLeft": "Esquina inferior izquierda",
            "BottomRight": "Esquina inferior derecha",
        }

        notification_pos = self.config_reader["user_settings"]["notification_pos"]
        self.box_notification_pos = QComboBox()
        self.box_notification_pos.addItem("Esquina superior izquierda")
        self.box_notification_pos.addItem("Esquina superior derecha")
        self.box_notification_pos.addItem("Esquina inferior izquierda")
        self.box_notification_pos.addItem("Esquina inferior derecha")
        self.box_notification_pos.setCurrentText(spanish_to_pos_text[notification_pos])
        self.tab_general.layout.addWidget(self.box_notification_pos, 2, 1, 1, 5)

        self.separatorLine = QFrame(frameShape=QFrame.HLine)
        self.separatorLine.setLineWidth(0)
        self.separatorLine.setMidLineWidth(5)
        self.separatorLine.setStyleSheet("font: 9pt; color: grey;")
        self.tab_general.layout.addWidget(self.separatorLine, 4, 0, 1, 6)

        self.label_preferences_user = QLabel("Preferencias")
        self.label_preferences_user.setFont(QFont("Arial", 9, weight=QFont.Bold))
        self.tab_general.layout.addWidget(self.label_preferences_user, 5, 0)

        self.checkbox_run_startup = QCheckBox("Ejecutar al arranque", self)
        self.tab_general.layout.addWidget(self.checkbox_run_startup, 6, 0, 1, 2)

        hotkey = self.config_reader["user_settings"]["shortcut_key"]

        self.label_preference_hotkey = QLabel("Atajo para escaneo")
        self.tab_general.layout.addWidget(self.label_preference_hotkey, 7, 0, 1, 2)
        self.parent.button_input_hotkey = QPushButton(self)
        self.parent.button_input_hotkey.setText(hotkey)
        self.tab_general.layout.addWidget(self.parent.button_input_hotkey, 7, 1, 1, 5)
        self.parent.button_input_hotkey.clicked.connect(self.on_button_hotkey_click)

        self.label_preference_scan = QLabel("Motor de escaneo preferido")
        self.tab_general.layout.addWidget(self.label_preference_scan, 8, 0, 1, 3)

        self.radio_button_google = QRadioButton("Google (recomendado)")
        self.tab_general.layout.addWidget(self.radio_button_google, 9, 0)

        self.label_separator = QLabel("           ")
        self.tab_general.layout.addWidget(self.label_separator, 9, 1)
        self.radio_button_mangaocr = QRadioButton("MangaOCR")
        self.tab_general.layout.addWidget(self.radio_button_mangaocr, 9, 2)
        self.radio_button_paddleocr = QRadioButton("PaddleOCR")
        self.tab_general.layout.addWidget(self.radio_button_paddleocr, 9, 3)
        self.radio_button_paddleocr.setDisabled(True)

        ocr_provider = self.config_reader["provider_settings"]["ocr_provider"]

        # OCR client selection based on the config file
        if ocr_provider == "Google":
            self.radio_button_google.setChecked(True)

        elif ocr_provider == "MangaOCR":
            self.radio_button_mangaocr.setChecked(True)

        self.spaceItem = QSpacerItem(100, 10, QSizePolicy.Expanding)
        self.tab_general.layout.addItem(self.spaceItem, 10, 0)

        self.button_accept = QPushButton("Aceptar", self)
        self.button_cancel = QPushButton("Cancelar", self)
        self.button_accept.clicked.connect(self.accept_button)
        self.button_cancel.clicked.connect(self.cancel_button)

        self.tab_general.layout.addWidget(self.button_accept, 16, 2, 2, 1)
        self.tab_general.layout.addWidget(self.button_cancel, 16, 3, 2, 2)

        # Tab Advanced
        self.label_paths_user = QLabel("Rutas de archivos")
        self.label_paths_user.setFont(QFont("Arial", 9, weight=QFont.Bold))
        self.tab_advanced.layout.addWidget(self.label_paths_user, 0, 0, 1, 4)

        self.label_path_google = QLabel("Credenciales Google")
        self.tab_advanced.layout.addWidget(self.label_path_google, 1, 0, 1, 2)

        self.line_edit_google = QLineEdit(self)
        self.tab_advanced.layout.addWidget(self.line_edit_google, 2, 0, 1, 5)

        self.button_accept_google = QPushButton("...", self)
        self.tab_advanced.layout.addWidget(self.button_accept_google, 2, 5, 1, 1)
        self.button_accept_google.clicked.connect(self.accept_button_path_google)

        path_string_google = self.config_reader["path_settings"]["credentials_google"]
        if path_string_google.strip() == "":
            self.line_edit_google.setText("No se ha definido")
        else:
            self.line_edit_google.setText(path_string_google)

        self.button_accept2 = QPushButton("Aceptar", self)
        self.button_cancel2 = QPushButton("Cancelar", self)
        self.button_accept2.clicked.connect(self.accept_button)
        self.button_cancel2.clicked.connect(self.cancel_button)

        self.tab_advanced.layout.addWidget(self.button_accept2, 16, 4, 2, 1)
        self.tab_advanced.layout.addWidget(self.button_cancel2, 16, 5, 2, 1)

        self.labelAbout = QLabel(
            "NadeOCR v1.0.1 (10/10/2022)
An easy and fast-to-use tool for scanning text anywhere with Google's Vision API and other third party services.

"
            "This project wouldn't be possible without:
- Google's Vision API for detecting and recognising a wide variety of languages including, but not limited to, English, Japanese and Spanish.
- The awesome Manga-OCR model by Maciej Budyś for recognizing Japanese characters in manga.

"
            "More information about this project can be found here."
        )
        self.labelAbout.setWordWrap(True)
        self.labelAbout.setOpenExternalLinks(True)
        self.tab_about.layout.addWidget(self.labelAbout, 0, 0, 1, 6)

        self.button_accept3 = QPushButton("Cerrar", self)
        self.button_accept3.clicked.connect(self.cancel_button)
        self.tab_about.layout.addWidget(self.button_accept3, 16, 5, 2, 1)

        self.tab_general.layout.setRowStretch(16, 5)
        self.tab_advanced.layout.setRowStretch(15, 5)
        self.tab_about.layout.setRowStretch(15, 5)

        self.show()

    def accept_button_path_google(self):
        path_google_fdialog = QFileDialog.getOpenFileName(
            self, "Abrir archivo", "", "Credenciales (*.json)", ""
        )
        self.line_edit_google.setText(path_google_fdialog[0])
        edit_config_ini("path_settings", "credentials_google", path_google_fdialog[0])

    def accept_button(self):
        if self.radio_button_google.isChecked():
            edit_config_ini("provider_settings", "ocr_provider", "Google")
        elif self.radio_button_mangaocr.isChecked():
            edit_config_ini("provider_settings", "ocr_provider", "MangaOCR")

        if self.box_notification_pos.currentText() == "Esquina superior izquierda":
            edit_config_ini("user_settings", "notification_pos", "TopLeft")
        elif self.box_notification_pos.currentText() == "Esquina superior derecha":
            edit_config_ini("user_settings", "notification_pos", "TopRight")
        elif self.box_notification_pos.currentText() == "Esquina inferior derecha":
            edit_config_ini("user_settings", "notification_pos", "BottomRight")
        elif self.box_notification_pos.currentText() == "Esquina inferior izquierda":
            edit_config_ini("user_settings", "notification_pos", "BottomLeft")

        self.close()

    def cancel_button(self):
        self.close()

    def on_button_hotkey_click(self):
        self.input_hotkey_window = MainHotkeyExecution(parent=self.parent)


class Solution:
    # @param A : string
    # @return an integer
    def solve(self, A):
        count=0
        vowels = ["a","e","i","o","u"]
        for i in range(len(A)):
            if A[i].lower() in vowels:
                count=count+len(A)-i
        return (count%10003)


import heapq
from collections import defaultdict

class HuffmanNode:
    def __init__(self, symbol, freq):
        self.symbol = symbol
        self.freq = freq
        self.left = None
        self.right = None

    def __lt__(self, other):
        return self.freq < other.freq

def build_huffman_tree(frequencies):
    heap = []
    for symbol, freq in frequencies.items():
        heapq.heappush(heap, HuffmanNode(symbol, freq))

    while len(heap) > 1:
        node1 = heapq.heappop(heap)
        node2 = heapq.heappop(heap)
        merged = HuffmanNode(None, node1.freq + node2.freq)
        merged.left = node1
        merged.right = node2
        heapq.heappush(heap, merged)

    return heap[0]

def traverse_huffman_tree(node, code, code_dict):
    if node.symbol is not None:
        code_dict[node.symbol] = code
        return

    traverse_huffman_tree(node.left, code + '0', code_dict)
    traverse_huffman_tree(node.right, code + '1', code_dict)

def huffman_encoding(text):
    text = text.replace(" ", "")  # Usunięcie spacji z tekstu
    frequencies = defaultdict(int)
    for symbol in text:
        frequencies[symbol] += 1

    huffman_tree = build_huffman_tree(frequencies)
    code_dict = {}
    traverse_huffman_tree(huffman_tree, '', code_dict)

    encoded_text = [(symbol, code_dict[symbol]) for symbol in text]

    return encoded_text, frequencies, code_dict

def main():
    text = input("Podaj tekst do zakodowania: ")
    encoded_text, frequencies, code_dict = huffman_encoding(text)
    print("Znak\t| Liczba wystąpień\t| Kodowanie")
    print("------------------------------------------")
    for symbol, count in sorted(frequencies.items(), key=lambda x: x[1]):
        code = code_dict[symbol]
        print(f"{symbol}\t| {count}\t\t| {code}")
    encoded_string = ''.join(code for symbol, code in encoded_text)
    print("Zakodowany tekst:", encoded_string)

main()


temperaturas = []

continuar = True

while continuar == True:
  temperatura = int(input("Temperatura que deseja informar: "))
  temperaturas.append(temperatura)
  if input("Deseja continuar (s/n)? ") == 's':
    continuar = True
  else:
    continuar = False

valor_limiar = int(input("Digite um valor limiar(média): "))

acima_da_media = []
for temperatura in temperaturas:
  if temperatura > valor_limiar:
    acima_da_media.append(temperatura)

percentual = (len(acima_da_media)*100)/len(temperaturas)

if percentual >= 20 and percentual <45:
  print("Dentro do esperado")
elif percentual >=45 and percentual <70:
  print("Acima do esperado")
elif percentual >=70 and percentual <100:
  print("Muitos valores acima do limiar")
else:
  print("Sem problemas aqui")

print("Temperaturas: ", sorted(temperaturas))
print("Valor Limiar: ", valor_limiar)
print("Acima da média: ", sorted(acima_da_media))
print("Percentual: ", percentual)


from test_cases import PseudocodeConverter

filename = input("What is the name of the file? ")

line_series = [line.rstrip('\n') for line in open(filename + '.py')]

converter = PseudocodeConverter(line_series)
converted_lines = converter.get_converted_lines()

with open(filename + " pseudocode.txt", 'w') as f:
    for line in converted_lines:
        text = (" " * line["indents"]) + line["content"]
        f.write(text)
        f.write("\n")  # Create new lines


import os

from qgis.PyQt import QtWidgets, uic
from qgis.PyQt.QtCore import pyqtSignal
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QApplication
from qgis.utils import iface
from qgis.core import (
    QgsProject,
    QgsPointXY,
    QgsCoordinateReferenceSystem,
    QgsCoordinateTransform,
)

from qgis.gui import QgsVertexMarker

# using utils
from .utils import (
    draw_rect_bound,
    icon,
    get_nlp,
    bk_10000,
    bk_2500,
    bk_1000,
    bk_500,
    bk_250,
    dialogBox,
)
from .maptools import MapTool

FORM_CLASS, _ = uic.loadUiType(
    os.path.join(os.path.dirname(__file__), "../ui/gambar_nlp.ui")
)

# constants
skala = ["1:10000", "1:2500", "1:1000", "1:500", "1:250"]

# constants for NLP
x_origin = 32000
y_origin = 282000
grid_10rb = 6000
grid_2500 = 1500
grid_1000 = 500
grid_500 = 250
grid_250 = 125

class DrawNLPDialog(QtWidgets.QDialog, FORM_CLASS):
    """Dialog for NLP Dialog"""

    closingPlugin = pyqtSignal()

    def __init__(self, parent=iface.mainWindow()):
        self.iface = iface
        self.canvas = iface.mapCanvas()
        super(DrawNLPDialog, self).__init__(parent)
        self.setupUi(self)
        self.project = QgsProject()
        self.setWindowIcon(icon("icon.png"))

        copy_icon = QIcon(":/images/themes/default/mActionEditCopy.svg")

        self.project.instance().crsChanged.connect(self.set_epsg)

        # Clipboard
        self.clipboard = QApplication.clipboard()

        self.ambil_titik.checked = False
        self.point = None

        # setup map tool
        self.previousMapTool = self.canvas.mapTool()
        self.epsg = self.project.instance().crs().authid()
        self.crs_tm3.setText(self.project.instance().crs().description())

        # copy to clipboard
        self.copyTeksNLP.setIcon(copy_icon)
        self.copyTeksNLP.clicked.connect(self.copy_clicked)

        # self.skala_peta.currentIndexChanged.connect(self.get_nlp_text())
        self.ambil_titik.clicked.connect(self.on_pressed)

        self.skala_peta.addItems(skala)

    def closeEvent(self, event):
        try:
            self.closingPlugin.emit()
            self.iface.actionPan().trigger()
            MapTool(self.canvas, self.vm).clear_drawing()
            event.accept()
        except Exception as e:
            pass

    def copy_clicked(self, button_index):
        text = self.nlp.text()
        self.clipboard.setText(text)
        self.iface.statusBarIface().showMessage(
            "Nomor lembar peta berhasil disalin", 3000
        )

    def createMapTool(self):
        self.canvas.setMapTool(self.myMapTool)

    def deactivateMapTool(self):
        self.point_tool.isEmittingPoint = False
        self.point_tool.deleteLater()
        self.canvas.scene().removeItem(self.vm)
        self.canvas.setMapTool(self.previousMapTool)

    def set_epsg(self):
        self.epsg = self.project.instance().crs().authid()
        self.crs_tm3.setText(self.project.instance().crs().description())
        # print("changing epsg now into", self.epsg)

    def on_pressed(self):
        self.check_is_tm3()
        self.ambil_titik.checked = True
        try:
            self.canvas.scene().removeItem(self.vm)
            self.canvas.scene().removeItem(self.rb)
            MapTool(self.canvas, self.vm).clear_drawing()
        except:  # noqa
            pass
        self.vm = self.create_vertex_marker()
        self.point_tool = MapTool(self.canvas, self.vm)

        self.point_tool.map_clicked.connect(self.update_titik)

        self.point_tool.isEmittingPoint = True
        self.canvas.setMapTool(self.point_tool)

    def check_is_tm3(self):
        if int(self.epsg.split(":")[1]) in range(23830, 23846):
            return True
            # print("EPSG Tercatat", self.epsg.split(":")[1])
        else:
            dialogBox("Anda belum mengatur sistem proyeksi TM-3 Project")
            # print("EPSG Tercatat", self.epsg.split(":")[1])
            self.ambil_titik.checked = False
            return False

    def update_titik(self, x, y):
        self.ambil_titik.setChecked(False)
        self.point = QgsPointXY(x, y)

        # check point bounds against TM-3 Boundary
        source_crs = QgsCoordinateReferenceSystem("EPSG:4326")
        crs = QgsCoordinateReferenceSystem(self.epsg)
        transform = QgsCoordinateTransform(source_crs, crs, QgsProject.instance())
        crs_box = transform.transformBoundingBox(crs.bounds())
        if not crs_box.contains(self.point):
            dialogBox("Anda memilih titik di luar zona TM-3 Project")
        else:
            self.koordinat.setText(str(round(x, 3)) + "," + str(round(y, 3)))
            self.canvas.unsetMapTool(self.point_tool)
            self.deactivateMapTool()
            self.get_nlp_text()

    def create_vertex_marker(self, type="CROSS"):
        vm = QgsVertexMarker(self.canvas)

        if type == "BOX":
            icon_type = QgsVertexMarker.ICON_BOX
        elif type == "CIRCLE":
            icon_type = QgsVertexMarker.ICON_CIRCLE
        elif type == "CROSS":
            icon_type = QgsVertexMarker.ICON_CROSS
        else:
            icon_type = QgsVertexMarker.ICON_X

        vm.setIconType(icon_type)
        vm.setPenWidth(3)
        vm.setIconSize(7)
        return vm

    def get_nlp_text(self):
        skala_now = self.skala_peta.currentText()
        if self.point is not None:
            x, y = self.point
            self.nlp.setText(get_nlp(skala_now[2:], x, y))
        if self.checkBoxNLP.isChecked():
            self.draw_nlp()

    def draw_nlp(self):
        xMin = xMax = yMin = yMax = None
        skala_now = self.skala_peta.currentText()
        if self.point is not None:
            x, y = self.point

        def rect10rb():
            k_10rb, b_10rb = bk_10000(x, y)
            xMin = x_origin + (k_10rb - 1) * grid_10rb
            yMin = y_origin + (b_10rb - 1) * grid_10rb
            xMax = x_origin + (k_10rb) * grid_10rb
            yMax = y_origin + (b_10rb) * grid_10rb
            return [xMin, yMin, xMax, yMax]

        def rect2500():
            k_2500, b_2500 = bk_2500(x, y)
            ori_10rb_x, ori_10rb_y, p, q = rect10rb()
            xMin = ori_10rb_x + (k_2500 - 1) * grid_2500
            yMin = ori_10rb_y + (b_2500 - 1) * grid_2500
            xMax = ori_10rb_x + (k_2500) * grid_2500
            yMax = ori_10rb_y + (b_2500) * grid_2500
            return [xMin, yMin, xMax, yMax]

        def rect1000():
            k_1000, b_1000 = bk_1000(x, y)
            ori_2500_x, ori_2500_y, p, q = rect2500()
            xMin = ori_2500_x + (k_1000 - 1) * grid_1000
            yMin = ori_2500_y + (b_1000 - 1) * grid_1000
            xMax = ori_2500_x + (k_1000) * grid_1000
            yMax = ori_2500_y + (b_1000) * grid_1000
            return [xMin, yMin, xMax, yMax]

        def rect500():
            k_500, b_500 = bk_500(x, y)
            ori_1000_x, ori_1000_y, p, q = rect1000()
            xMin = ori_1000_x + (k_500 - 1) * grid_500
            yMin = ori_1000_y + (b_500 - 1) * grid_500
            xMax = ori_1000_x + (k_500) * grid_500
            yMax = ori_1000_y + (b_500) * grid_500
            return [xMin, yMin, xMax, yMax]

        def rect250():
            k_250, b_250 = bk_250(x, y)
            ori_500_x, ori_500_y, p, q = rect500()
            xMin = ori_500_x + (k_250 - 1) * grid_250
            yMin = ori_500_y + (b_250 - 1) * grid_250
            xMax = ori_500_x + (k_250) * grid_250
            yMax = ori_500_y + (b_250) * grid_250
            return [xMin, yMin, xMax, yMax]

        if skala_now == skala[0]:
            xMin, yMin, xMax, yMax = rect10rb()
        elif skala_now == skala[1]:
            xMin, yMin, xMax, yMax = rect2500()
        elif skala_now == skala[2]:
            xMin, yMin, xMax, yMax = rect1000()
        elif skala_now == skala[3]:
            xMin, yMin, xMax, yMax = rect500()
        elif skala_now == skala[4]:
            xMin, yMin, xMax, yMax = rect250()

        if xMin is not None:
            draw_rect_bound(xMin, yMin, xMax, yMax, self.epsg)


#! python 3
"""
Program that reads in text files and lets users replace some keywords. Lastly, save as new file
"""

from pathlib import Path
import re

### read in text file
file_name = input("Enter relative path of file (eg. folder/file.txt): ")
p = Path(Path.cwd() / file_name)
input_file = open(p)
txt = input_file.read()
print(txt)

### find following keywords
kwRegex = re.compile(r'ADJECTIVE|NOUN|VERB|ADVERB')
kwList = kwRegex.findall(txt)

### get user to input string to replace
for i in kwList:
    replaceWord = input(f'Enter a {i}:')
    txt = txt.replace(i, replaceWord, 1)

### save updated text to new file
print(txt)

# consider identifying how many different parents levels are there
out_file_name = input("Enter relative path of new file (eg. folder/file.txt): ")
new_file = open(f"{Path.cwd() / out_file_name}", 'w')
new_file.write(txt)

input_file.close()
new_file.close()


"""
Fix binary png images so that they're in a consistent binary format
Used for ground truth (_fg) images which can be saved by some image programs as 8 bit RGB pngs
rather than binary ones.
"""
import cv2
import os
import io

for img in os.listdir("/home/carter/Desktop/our-data/training/imgs"):
    if "_fg" in img:
        print(img)
        #read image as grey scale
        img_grey = cv2.imread("/home/carter/Desktop/our-data/training/imgs/"+img, cv2.IMREAD_GRAYSCALE)

        # define a threshold, 128 is the middle of black and white in grey scale
        thresh = 128

        # threshold the image
        img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1]

        #save image
        cv2.imwrite("/home/carter/Desktop/our-data/training/imgs/"+img, img_binary)


input = open("input").read().splitlines()

register = 1
cycle = 0
total = 0
sprite = "###....................................."

for instruction in input:
    op = instruction.split()

    if op[0] == "noop":
        cycles = 1
    elif op[0] == "addx":
        cycles = 2

    for i in range(cycles):
        cycle += 1
        if cycle == 20 or (cycle + 20) % 40 == 0:
            total += cycle * register
        print(sprite[(cycle - 1) % 40], end="")
        if cycle % 40 == 0:
            print()

    if op[0] == "addx":
        register += int(op[1])
        sprite = ["."] * 40
        try:
            sprite[register] = "#"
            sprite[register - 1] = "#"
            sprite[register + 1] = "#"
        except:
            pass
        sprite = "".join(sprite)

print(total)


import tests_suite

import unittest
from cpu import CPU
from ram import RAM
from rom import ROM

class tests_ld_rr_nn(unittest.TestCase):

    def test_ld_BC_nn_correctly_stores_value_to_BC(self):
        cpu = CPU(ROM(b'\x01\xba\xab'))
        cpu.readOp()
        self.assertEqual(0xabba, cpu.BC)

    def test_ld_DE_nn_correctly_stores_value_to_DE(self):
        cpu = CPU(ROM(b'\x11\xde\xc0'))
        cpu.readOp()
        self.assertEqual(0xc0de, cpu.DE)

    def test_ld_HL_nn_correctly_stores_value_to_HL(self):
        cpu = CPU(ROM(b'\x21\xfe\xca'))
        cpu.readOp()
        self.assertEqual(0xcafe, cpu.HL)

    def test_ld_SP_nn_correctly_stores_value_to_SP(self):
        cpu = CPU(ROM(b'\x31\x37\x13'))
        cpu.readOp()
        self.assertEqual(0x1337, cpu.SP)

    def test_ld_BC_addr_nn_correctly_loads_value_to_BC(self):
        ram = RAM()
        ram[0x2130] = 0x65
        ram[0x2131] = 0x78
        cpu = CPU(ROM(b'\xED\x4b\x30\x21'), ram)
        cpu.readOp()
        self.assertEqual(0x7865, cpu.BC)

    def test_ld_DE_addr_nn_correctly_loads_value_to_DE(self):
        ram = RAM()
        ram[0x2130] = 0x65
        ram[0x2131] = 0x78
        cpu = CPU(ROM(b'\xED\x5b\x30\x21'), ram)
        cpu.readOp()
        self.assertEqual(0x7865, cpu.DE)

    def test_ld_HL_addr_nn_correctly_loads_value_to_HL(self):
        ram = RAM()
        ram[0x2130] = 0x65
        ram[0x2131] = 0x78
        cpu = CPU(ROM(b'\xED\x6b\x30\x21'), ram)
        cpu.readOp()
        self.assertEqual(0x7865, cpu.HL)

    def test_ld_SP_addr_nn_correctly_loads_value_to_SP(self):
        ram = RAM()
        ram[0x2130] = 0x65
        ram[0x2131] = 0x78
        cpu = CPU(ROM(b'\xED\x7b\x30\x21'), ram)
        cpu.readOp()
        self.assertEqual(0x7865, cpu.SP)

    def test_ld_BC_addr_nn_takes_6_m_cycles(self):
        ram = RAM()
        ram[0x2130] = 0x65
        ram[0x2131] = 0x78
        cpu = CPU(ROM(b'\xED\x4b\x30\x21'), ram)
        cpu.readOp()
        self.assertEqual(6, cpu.m_cycles)

    def test_ld_BC_addr_nn_takes_20_t_states(self):
        ram = RAM()
        ram[0x2130] = 0x65
        ram[0x2131] = 0x78
        cpu = CPU(ROM(b'\xED\x4b\x30\x21'), ram)
        cpu.readOp()
        self.assertEqual(20, cpu.t_states)


from __future__ import print_function
import wave
import numpy as np
from struct import unpack

class WaveWrap(object):
    def __init__(self, filename, output_window_size, window_overlap, unpack_fmt="<{}h"):
        self.wav_buffer_size = output_window_size - window_overlap
        self.wave_file = wave.open(filename, 'r')
        self.length = self.wave_file.getnframes()
        self.output_window_size = output_window_size
        self.unpack_fmt = unpack_fmt.format(self.wav_buffer_size)

        self.cursor = 1
        self.n_windows = self.length / self.wav_buffer_size

        self.wav_data = np.zeros(self.output_window_size)
        self.fft_input = np.zeros(self.output_window_size)
        self.hamming = np.hamming(self.output_window_size)

    def next_pcm_window(self):
        try:
            wave_data = self.wave_file.readframes(self.wav_buffer_size)
        except Exception as e:
            print(e.message)
            wave_data = np.zeros(self.wav_buffer_size)
        self.cursor += 1
        return unpack(self.unpack_fmt, wave_data)

    def next_fft_input_window(self):
        np.roll(self.wav_data, -self.wav_buffer_size)
        self.wav_data[-self.wav_buffer_size:] = self.next_pcm_window()
        return self.wav_data * self.hamming

    def next_fft_output_window(self):
        fft = np.abs(np.fft.rfft(self.next_fft_input_window()))
        max_energy = max(fft)
        fft /= max_energy
        return fft

window_size = 4096 * 4
overlap = (window_size / 4) * 3

wav_wrap = WaveWrap(filename='bee.wav', output_window_size=window_size, window_overlap=overlap)

bin_size = wav_wrap.wave_file.getframerate() / float(window_size)
print('bin size', bin_size)

d = np.array([wav_wrap.next_fft_output_window() for _ in range(wav_wrap.n_windows)])

print('len(d) = ', len(d))

import matplotlib.pyplot as plt

plt.imshow(np.flip(d.T, 0), interpolation='nearest', aspect='auto')

plt.show()


#################################################################################
# Description:  Class for generating augmented data for training of
#               neural network and SVM
#
# Authors:      Petr Buchal         <petr.buchal@lachub.cz>
#               Martin Ivanco       <ivancom.fr@gmail.com>
#               Vladimir Jerabek    <jerab.vl@gmail.com>
#
# Date:     2019/04/13
#
# Note:     This source code is part of project created on UnIT extended 2019.
#################################################################################

import random
import argparse
from datetime import datetime

import cv2
import numpy as np
from matplotlib import pyplot

from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator

from image import Image
from tools import parse_data

def get_args():
    """
    method for parsing of arguments
    """
    parser = argparse.ArgumentParser()

    parser.add_argument('--batches', type="int", action="store", default=100,
                        help='Number of generated batch of size 32.')

    args = parser.parse_args()

    return args

#Class storing data for augmentation
class DataSet(object):
    def __init__(self, ImageList):
        self.Images = ImageList
        self.p_images, self.labels, self. grand_truths = DataSet.strip_futilities(ImageList)
        random.seed(datetime.now())

    #Function for augmentation images
    #@param: imageArr   - is numpy array of images of shape (n, 1, height, width)
    #                                                        ^-- number of images
    #@param: grandArr   - is numpy array of grand truths images of shape (n, 1, height, width)
    #@param: batchSize  - is integer, which is less or equal 'n'
    #@return: new_img   - array of augmented images of shape (batchSize, 1, height, width)
    #@return: new_g_t   - array of augmented grand truths images of shape (batchSize, 1, height, width)
    @staticmethod
    def augmentImage(imageArr, grandArr, batchSize):

        for imgs in [imageArr, grandArr]:
            for img in imgs:
                h, w = img[0].shape
                img[0,0,:] = 0
                img[0,h-1,:] = 0
                img[0,:,0] = 0
                img[0,:,w-1] = 0

        shift = 0.2
        data_gen_args = dict(   data_format="channels_first",
                                rotation_range=90,
                                height_shift_range=shift,
                                width_shift_range=shift,
                                zoom_range=0.2,
                                horizontal_flip=True,
                                vertical_flip=True)
        #creating Image generator same for image data and grand truths data
        img_dataGen = ImageDataGenerator(**data_gen_args)
        g_tr_dataGen = ImageDataGenerator(**data_gen_args)

        seed = random.randint(0,65535)
        for new_img in img_dataGen.flow(imageArr, seed=seed, batch_size=batchSize):
            new_img = np.uint16(new_img)
            break
        for new_g_t in g_tr_dataGen.flow(grandArr, seed=seed, batch_size=batchSize):
            new_g_t = np.uint16(new_g_t)
            break

        return new_img, new_g_t

    #Function for striping unneeded data from Image class
    #@param: data   - List of Image class       ^-- for more info see image.py
    #@return: images        - numpy array of images of shape (n, 1, height, width)      %note: 16-bit grayscale
    #@return: labels        - numpy array of booleans of shape (n,1),
    #                           - 0 mean, that i-th image is doesn't contain ellipse
    #                           - 1 mean, that i-th image contains ellipse
    #@return: grand_truths  - numpy array of images of shape (n, 1, height, width)      %note: 16-bit grayscale
    @staticmethod
    def strip_futilities(data):
        images = []
        labels = []
        grand_truths = []
        for i, item in enumerate(data):
            images.append(np.array([item.processed_image]))
            labels.append(np.array([item.ellipse]))
            grand_truths.append(np.array([item.processed_ground_truths]))

        return np.array(images), np.array(labels), np.array(grand_truths)

    #Method for getting augmented data for training neural netowrk
    #@param batchSize   - nuber of images in one training epoche
    def getBatch(self, batchSize, isClassNet=True):
        img, g_truths = DataSet.augmentImage(self.p_images, self.grand_truths, batchSize)
        labels = []

        if batchSize > len(self.p_images):
            while len(img) != batchSize:
                curSize = batchSize - len(img)
                img2, g_truths2 = DataSet.augmentImage(self.p_images, self.grand_truths, curSize)
                img = np.concatenate((img, img2), axis=0)
                g_truths = np.concatenate((g_truths, g_truths2), axis=0)

        #get labels
        for i in range(len(img)):
            unique, counts = np.unique(g_truths[i], return_counts=True)
            if unique[-1] > 60000:
                labels.append(np.array([1]))
            else:
                labels.append(np.array([0]))

        if isClassNet:
            return img, np.array(labels)
        else:
            return img, g_truths

if __name__ == "__main__":
    args = parse_args()

    trn_data = parse_data("./data_training/ground_truths_develop.csv", "./data_training/images/", "./data_training/ground_truths/")
    myData = DataSet(trn_data)

    if not os.path.exists("./images_png"):
        os.makedirs("./images_png")

    counter = 0
    for e in range(args.batches):
        x, y = myData.getBatch(32)
        for i, item in enumerate(x):
            print("Generated batches: {}" .format(counter))
            new = cv2.cvtColor(item[0], cv2.COLOR_GRAY2BGR)
            if y[i] == 0:
                cv2.imwrite("./images_png/{}_F.png" .format(counter), new)
            if y[i] == 1:
                cv2.imwrite("./images_png/{}_T.png" .format(counter), new)
            counter += 1


# -*- coding: utf-8 -*-
"""Home dashboard layout."""

import locale

from apps import utils_dash

import dash_bootstrap_components as dbc

import dash_core_components as dcc

import pandas as pd

import plotly.express as px
import plotly.graph_objects as go

from portfolio.fii import FiiPortfolio
from portfolio.funds import FundsPortfolio
from portfolio.stocks import StocksPortfolio

print("locale: ", locale.setlocale(locale.LC_ALL, utils_dash.my_locale))

stocksportfolio = StocksPortfolio()
fiiportfolio = FiiPortfolio()
fundsportfolio = FundsPortfolio()

#############################################################################
# Figures
#############################################################################

fig_class_distribution_labels = ["Stocks", "FIIs", "Funds"]
fig_class_distribution_values = []
fig_class_distribution_values.append(stocksportfolio.total_invest()[1])
fig_class_distribution_values.append(fiiportfolio.total_invest[1])
fig_class_distribution_values.append(fundsportfolio.total_invest[1])

fig_class_distribution = go.Figure(
    data=[
        go.Pie(
            labels=fig_class_distribution_labels, values=fig_class_distribution_values
        )
    ]
)
fig_class_distribution.update_layout(title="Porfolio Class Distribution", title_x=0.5)

df_fii_money_monthly = fiiportfolio.fiitransactions.money_invested_monthly()
df_fii_money_monthly["Class"] = "FII"
df_fun_money_monthly = fundsportfolio.money_invested_monthly()
df_fun_money_monthly.rename(columns={"Value": "Operation Cost"}, inplace=True)
df_fun_money_monthly["Class"] = "Funds"
df_sto_money_monthly = stocksportfolio.stockstransactions.money_invested_monthly()
df_sto_money_monthly["Class"] = "Stocks"
fig_money_inv_monthly = px.bar(
    pd.concat([df_fii_money_monthly, df_fun_money_monthly, df_sto_money_monthly]),
    x="Date",
    y="Operation Cost",
    labels={"Operation Cost": "Amount Invested", "Date": "Month"},
    color="Class",
    title="Money Invested Monthly",
)
fig_money_inv_monthly.update_layout(
    title_x=0.5, yaxis={"tickprefix": utils_dash.graph_money_prefix}
)
fig_money_inv_monthly.update_xaxes(rangeslider_visible=True)
fig_money_inv_monthly.update_layout(
    title_x=0.5,
    xaxis={
        "rangeselector": {
            "buttons": [
                {"count": 1, "label": "1m", "step": "month", "stepmode": "backward"},
                {"count": 6, "label": "6m", "step": "month", "stepmode": "backward"},
                {"count": 1, "label": "YTD", "step": "year", "stepmode": "todate"},
                {"count": 1, "label": "1y", "step": "year", "stepmode": "backward"},
                {"count": 2, "label": "2y", "step": "year", "stepmode": "backward"},
                {"count": 5, "label": "5y", "step": "year", "stepmode": "backward"},
                {"step": "all"},
            ],
        },
        "rangeslider": {"visible": False},
        "type": "date",
    },
)

#############################################################################
# layout
#############################################################################

layout = dbc.Container(
    [
        dbc.Row(
            [
                dbc.Col(
                    [
                        dcc.Graph(
                            id="fig_class_distribution_id",
                            figure=fig_class_distribution,
                        )
                    ],
                ),
                dbc.Col(
                    [
                        dcc.Graph(
                            id="fig_money_inv_monthly_id",
                            figure=fig_money_inv_monthly,
                        )
                    ],
                ),
            ]
        ),
    ],
    fluid=True,
)

# vim: ts=4


# かぼちゃライブラリを読み込む
import CaboCha

cabocha = CaboCha.Parser('-n1')

def parse_sentence(sentence_str, sentence_begin):
    # カボチャ関数の呼び出し、係り受け木を取得
    tree = cabocha.parse(sentence_str)

    offset = sentence_begin
    text = sentence_str

    for i in range(tree.chunk_size()):
        # chunk「文節に相当する塊」の事、chanking単語を一番小さい物に変換する
        chunk = tree.chunk(i)
        chunk_begin = None

        print('chunk:')

        #各チャンクの情報を取り出し。
        for j in range(
                chunk.token_pos,
                chunk.token_pos + chunk.token_size):
            # トークン（単語）とチャンクを切り分け表示する。
            token = tree.token(j)
            features = token.feature.split(',')
            # トークンの開始位置を指定
            token_begin = text.find(token.surface) + offset
            token_end = token_begin + len(token.surface)
            if chunk_begin is None:
                # チャンクの開始位置
                chunk_begin = token_begin

            print('    token_begin:', token_begin)
            print('    token_end:',   token_end)
            print('    features:',    features)
            print('    lemma:',       features[-3])
            print('    POS:',         features[0])
            print('    POS2:',        features[1])
            print('    NE:',          token.ne)
            print()

            text = text[token_end-offset:]
            # トークンの終了位置を指定
            offset = token_end
        # チャンクの終了位置
        chunk_end = token_end

        print('  chunk_link:',    chunk.link)
        print('  chunk_begin:',   chunk_begin)
        print('  chunk_end:',     chunk_end)
        print()

if __name__ == '__main__':
    parse_sentence('プログラムを作って、動かしながら自然言語処理を学ぶ事で、どんどん身につく', 0)


'''Find minimum number of merge operations to make an array palindrome

Link for this problem is given below:

https://www.geeksforgeeks.org/find-minimum-number-of-merge-operations-to-make-an-array-palindrome/

'''
def mergeOperations(arr):
    n = len(arr)
    for i in range(n):
        if arr[i] != arr[n-(i+1)]:
            return (n-(i*2))-1;
print(mergeOperations([11, 14, 15, 99]))

GeeksForGeeks/minimum number of merge operations.py


import os
from torchvision.transforms import CenterCrop
from PIL import Image

completed_processing = set()

source_dir = "C:/Users/jessi/Documents/Master_Courses/MIE1517_IDL/Project/dataset/dataset/"
output_dir = "C:/Users/jessi/Documents/Master_Courses/MIE1517_IDL/Project/dataset/processed_dataset/"

i = 0
nb_excluded = 0

with os.scandir(source_dir) as it:
    for item in it:
        i += 1
        if item.name.endswith('.jpg'):
            if item.name in completed_processing:
                continue

            completed_processing.add(item.name)

            with Image.open(source_dir + item.name) as image:

                width, height = image.size
                if width < 128 or height < 128:
                    nb_excluded += 1
                    continue

                crop_img = CenterCrop(128)(image)
                crop_img.save(output_dir + item.name)

        if i % 50 == 0:
            print(i)


# Imports
import bs4
import requests
from bs4 import BeautifulSoup

# URL Base
url_base = 'https://www.who.int'
def scrapingCovid():
    # URL com os dados do COVID-19 (coloque essa URL no seu navegador e compreenda os dados disponíveis)
    url = 'https://www.who.int/emergencies/diseases/novel-coronavirus-2019/situation-reports/'

    # Obtém os dados da URL
    response = requests.get(url)

    # Leitura do conteúdo
    soup = BeautifulSoup(response.content, 'lxml')

    # Lista temporária
    tempList = []

    # Loop pelos elementos da página para buscar as tags "a" que indicam URLs
    # Estamos buscando os links dos relatórios chamados 'situation-reports'
    for element in soup.find_all('a', href = True):
        if 'situation-reports' in str(element):
            tempList.append(str(element))

    # Visualiza alguns registros da lista
    # print(tempList[1:5])

    # Lista para as URLs dos arquivos em pdf
    pdfurls = []

    # Variável que indica se é o último relatório
    lastreport = None

    # Loop para limpar cada registro da lista temporária, extraindo somente a URL dos arquivos em pdf
    for url in tempList:

        # Replace da tag de link html
        x = url.replace('<a href="', '')

        # Busca pelo sinal de interrogação para usar como índice de final do endereço de cada arquivo pdf
        index = x.find('?')

        # Extrai tudo de 0 até o índice
        extraction = x[0:index]

        # Nas posições de 68:70 do endereço do arquivo pdf estão os dígitos do número do relatório
        reportnumber = extraction[68:70]

        # Verifica se o relatório é o último
        # Se não for, adicionamos a url_base ao nome do arquivo e gravamos na lista de URLs
        if reportnumber != lastreport:
            pdfurls.append(url_base + extraction)

        # Atualiza a variável com o número do último relatório (posições de 68:70)
        lastreport = extraction[68:70]

    # Total de arquivos pdf
    print(len(pdfurls))

    # Visualiza amostra da lista de URLs dos arquivos pdf
    return pdfurls