# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.6.0
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import torch
import torch.utils.data
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd

from modules.dataset import SoundDataset

class VCNN(nn.Module):
    def __init__(self,input_shape,z_shape):
        super(VCNN, self).__init__()

        self.input_shape = input_shape
        self.z_shape = z_shape
        # encoder
        self.encoder = nn.Sequential()
        self.encoder.add_module('enc_conv1', nn.Conv1d(in_channels=3, out_channels=9, kernel_size=16, stride=10, padding=6, padding_mode='zeros'))
        self.encoder.add_module('enc_relu1', nn.ReLU(True))
        self.encoder.add_module('enc_conv2', nn.Conv1d(in_channels=9, out_channels=9, kernel_size=16, stride=10, padding=6, padding_mode='zeros'))
        self.encoder.add_module('enc_relu2', nn.ReLU(True))
        self.encoder.add_module('enc_conv3', nn.Conv1d(in_channels=9, out_channels=9, kernel_size=16, stride=10, padding=6, padding_mode='zeros'))
        self.encoder.add_module('enc_relu3', nn.ReLU(True))
        # z to mean
        self.encmean_fc11 = nn.Linear(int(input_shape/10/10/10*9), z_shape)
        # z to var
        self.encvar_fc12 = nn.Linear(int(input_shape/10/10/10*9), z_shape)
        # estimator
        self.estimator = nn.Sequential()
        self.estimator.add_module('cla_fc1', nn.Linear(z_shape, 10))
        self.estimator.add_module('cla_relu1', nn.ReLU(True))
        self.estimator.add_module('cla_fc2', nn.Linear(10, 2))

    def encode(self, x):
        x = x.view(x.size()[0],3,-1)
        x = self.encoder(x)
        x = x.view(x.size()[0], -1)
        return self.encmean_fc11(x), self.encvar_fc12(x)

    def reparameterize(self, mu, logvar):
        std = torch.exp(0.5*logvar)
        eps = torch.randn_like(std)
        return mu + eps*std

    def forward(self, x):
        # encode
        mu, logvar = self.encode(x.view(-1, 3, self.input_shape).float())
        # reparameterize
        z = self.reparameterize(mu, logvar)
        # estimate
        y = self.estimator(z)
        y = y.view(-1,2)

        return y, mu, logvar

    def valid(self, x):
        mu, logvar = self.encode(x.view(-1, 3, self.input_shape).float())
        y = self.estimator(mu)
        y = y.view(-1,2)

        return y, mu, logvar

    def loss_function_vae(self, mu, logvar, beta=0.001):
        KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
        return KLD * beta

    def loss_function_estimator(self, pre, ans):
        return F.mse_loss(pre, ans.float(), reduction = 'sum')

class VCNN_controller():
    def __init__(self, dim_z = 10, data_path = '../data/sound', device="cuda"):
        # prepare cuda device
        #self.device = torch.device(device if torch.cuda.is_available() else "cpu")
        self.device = torch.device("cpu")
        # prepare dataset
        self.dataset = SoundDataset(data_path, transform=transforms.ToTensor())
        # define model
        self.model = VCNN(self.dataset.data_size, dim_z).to(self.device)
        # define optimizer
        self.optimizer = optim.Adam(self.model.parameters(), lr=1e-3)
        self.dim_z = dim_z

    #def __del__(self):
    #    self.save_weight()

    def train(self, epoch, max_epoch):
        # train mode
        self.model.train()
        train_loss = 0
        train_loss_vae = 0
        train_loss_estimator = 0
        for batch_idx, (data, ans) in enumerate(self.train_loader):
            data, ans = data.to(self.device), ans.to(self.device)
            # zero the parameter gradients
            self.optimizer.zero_grad()

            # forward
            pre, mu, logvar = self.model(data)

            loss_vae = self.model.loss_function_vae(mu, logvar)
            loss_estimator = self.model.loss_function_estimator(pre, ans)
            loss = loss_vae + loss_estimator
            # backward
            loss.backward()
            train_loss += loss.item()
            train_loss_vae += loss_vae.item()
            train_loss_estimator += loss_estimator.item()
            # update the parameter
            self.optimizer.step()
            if batch_idx % 1 == 0:
                print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                    epoch, batch_idx * len(data), len(self.train_loader.dataset),
                    100. * batch_idx / len(self.train_loader),
                    loss.item() / len(data)))

        train_loss /= len(self.train_loader.dataset)
        train_loss_vae /= len(self.train_loader.dataset)
        train_loss_estimator /= len(self.train_loader.dataset)
        print('====> Epoch: {} Average loss: {:.4f}'.format(epoch, train_loss))

        return train_loss, train_loss_vae, train_loss_estimator

    def valid(self, epoch):
        # test mode
        self.model.eval()
        valid_loss = 0
        valid_loss_vae = 0
        valid_loss_estimator = 0
        # test mode
        with torch.no_grad():
            for i, (data, ans) in enumerate(self.valid_loader):
                data, ans = data.to(self.device), ans.to(self.device)
                pre, mu, logvar = self.model.valid(data)
                loss_vae = self.model.loss_function_vae(mu, logvar)
                loss_estimator = self.model.loss_function_estimator(pre, ans)
                loss = loss_vae + loss_estimator
                valid_loss += loss.item()
                valid_loss_vae += loss_vae.item()
                valid_loss_estimator += loss_estimator.item()

        valid_loss /= len(self.valid_loader.dataset)
        valid_loss_vae /= len(self.valid_loader.dataset)
        valid_loss_estimator /= len(self.valid_loader.dataset)
        print('====> Validation set loss: {:.4f}'.format(valid_loss))

        return valid_loss, valid_loss_vae, valid_loss_estimator

    def auto_train(self, max_epoch, save_path = '../result/VCNN/model'):
        train_set, valid_set = torch.utils.data.random_split(self.dataset, [int(len(self.dataset)*0.8), len(self.dataset) - int(len(self.dataset)*0.8)])
        self.train_loader = torch.utils.data.DataLoader(train_set,batch_size=10,shuffle=True)
        self.valid_loader = torch.utils.data.DataLoader(valid_set,batch_size=10,shuffle=True)

        train_loss = []
        train_loss_vae = []
        train_loss_estimator = []
        valid_loss = []
        valid_loss_vae = []
        valid_loss_estimator = []
        for epoch in range(1,max_epoch):
            t_loss, t_loss_vae, t_loss_estimator = self.train(epoch, max_epoch)
            v_loss, v_loss_vae, v_loss_estimator = self.valid(epoch)
            train_loss.append(t_loss)
            train_loss_vae.append(t_loss_vae)
            train_loss_estimator.append(t_loss_estimator)
            valid_loss.append(v_loss)
            valid_loss_vae.append(v_loss_vae)
            valid_loss_estimator.append(v_loss_estimator)
        # plot result
        ## loss
        plt.figure()
        plt.plot(range(1,max_epoch),train_loss,label="train")
        plt.plot(range(1,max_epoch),valid_loss,label="validation")
        plt.title('VCNN Loss')
        plt.xlabel('Epochs')
        plt.ylabel('Loss')
        plt.legend()
        if save_path is not None:
            plt.savefig(save_path+'/model_vcnn_loss.png')
        plt.close()
        ## loss vae
        plt.figure()
        plt.plot(range(1,max_epoch),train_loss_vae,label="train")
        plt.plot(range(1,max_epoch),valid_loss_vae,label="validation")
        plt.title('VAE Loss')
        plt.xlabel('Epochs')
        plt.ylabel('Loss')
        plt.legend()
        if save_path is not None:
            plt.savefig(save_path+'/model_vcnn_loss_vae.png')
        plt.close()
        ## acc
        plt.figure()
        plt.plot(range(1,max_epoch),train_loss_estimator,label="train")
        plt.plot(range(1,max_epoch),valid_loss_estimator,label="validation")
        plt.title('Estimator Loss')
        plt.xlabel('Epochs')
        plt.ylabel('Loss')
        plt.legend()
        if save_path is not None:
            plt.savefig(save_path+'/model_vcnn_loss_estimator.png')

        self.plot_result()

    def plot_result(self, save_path='../result/VCNN/model/result.png'):
        train_pre = []
        train_ans = []
        valid_pre = []
        valid_ans = []

        self.model.eval()

        with torch.no_grad():
            for i, (data, ans) in enumerate(self.train_loader):
                data = data.to(self.device)
                pre, mu, logvar = self.model.valid(data)
                train_pre = np.append(train_pre, pre.to('cpu').clone().numpy())
                train_ans = np.append(train_ans, ans.to('cpu').clone().numpy())

        with torch.no_grad():
            for i, (data, ans) in enumerate(self.valid_loader):
                data = data.to(self.device)
                pre, mu, logvar = self.model.valid(data)
                valid_pre = np.append(valid_pre, pre.to('cpu').clone().numpy())
                valid_ans = np.append(valid_ans, ans.to('cpu').clone().numpy())

        train_pre = np.array(train_pre).reshape(-1, 2)
        train_ans = np.array(train_ans).reshape(-1, 2)
        valid_pre = np.array(valid_pre).reshape(-1, 2)
        valid_ans = np.array(valid_ans).reshape(-1, 2)

        #plot_pre_ans(save_path, self.dataset.ans_max, train_pre, train_ans, valid_pre, valid_ans)
        plot_pre_ans2(save_path, self.dataset.ans_max, valid_pre, valid_ans)

    def plot(self, save_path='../result/VCNN/model/plot.png'):

        loader = torch.utils.data.DataLoader(self.dataset,batch_size=len(self.dataset),shuffle=False)

        pre_pts = []
        ans_pts = []

        self.model.eval()

        with torch.no_grad():
            for i, (data, ans) in enumerate(loader):
                data = data.to(self.device)
                pre, mu, logvar = self.model.valid(data)
                pre_pts = pre.to('cpu').clone().numpy()
                ans_pts = ans.to('cpu').clone().numpy()

        pre_pts = np.array(pre_pts).reshape(-1, 2)
        ans_pts = np.array(ans_pts).reshape(-1, 2)

        plot_pre_ans(save_path, self.dataset.ans_max, pre_pts, ans_pts)

    def save_weight(self, save_path = '../result/VCNN/model/model_vcnn'):
        torch.save(self.model.state_dict(), save_path)

    def load_weight(self, load_path = '../result/VCNN/model/model_vcnn'):
        self.model.load_state_dict(torch.load(load_path))

    def plot_map(self, save_path = '../result/VCNN/model/map.png'):
        train_pre = []
        train_ans = []
        valid_pre = []
        valid_ans = []

        self.model.eval()

        with torch.no_grad():
            for i, (data, ans) in enumerate(self.train_loader):
                data = data.to(self.device)
                pre, mu, logvar = self.model.valid(data)
                train_pre = np.append(train_pre, pre.to('cpu').clone().numpy())
                train_ans = np.append(train_ans, ans.to('cpu').clone().numpy())

        with torch.no_grad():
            for i, (data, ans) in enumerate(self.valid_loader):
                data = data.to(self.device)
                pre, mu, logvar = self.model.valid(data)
                valid_pre = np.append(valid_pre, pre.to('cpu').clone().numpy())
                valid_ans = np.append(valid_ans, ans.to('cpu').clone().numpy())

        train_pre = np.array(train_pre).reshape(-1, 2)
        train_ans = np.array(train_ans).reshape(-1, 2)
        valid_pre = np.array(valid_pre).reshape(-1, 2)
        valid_ans = np.array(valid_ans).reshape(-1, 2)

        k = self.dataset.ans_max
        train_map = average_distance(train_pre * k, train_ans * k)
        valid_map = average_distance(valid_pre * k, valid_ans * k)

        print('====> Train average error: {:.4f}'.format(np.average(train_map)))
        print('====> Validation average error: {:.4f}'.format(np.average(valid_map)))
        #fig = plt.figure()
        #ax1 = fig.add_subplot(2,1,1)
        #ax1.set_title('Training')
        #ax1.set_xlabel('X[cm]')
        #ax1.set_ylabel('Y[cm]')
        #ax1.imshow(train_map, aspect='auto', origin='lower', cmap='jet',
        #           extent=[-7, 7, -7, 7])
        #ax2 = fig.add_subplot(2,1,2)
        #ax2.set_title('Validation')
        #ax2.set_xlabel('X[cm]')
        #ax2.set_ylabel('Y[cm]')
        #ax2.imshow(valid_map, aspect='auto', origin='lower', cmap='jet',
        #           extent=[-7, 7, -7, 7])
        #fig.tight_layout()

        fig = plt.figure()
        ax = fig.add_subplot(1,1,1)
        df = pd.DataFrame(data=train_map, index=['-6','-4','-2','0','2','4','6'], columns=['-6','-4','-2','0','2','4','6'])
        ax = sns.heatmap(df.T, annot=True, fmt='.2f', square=True)
        ax.invert_yaxis()
        plt.savefig(save_path.split('.png')[0]+'_train.png')
        plt.close()

        fig = plt.figure()
        ax = fig.add_subplot(1,1,1)
        df = pd.DataFrame(data=valid_map, index=['-6','-4','-2','0','2','4','6'], columns=['-6','-4','-2','0','2','4','6'])
        ax = sns.heatmap(df.T, annot=True, fmt='.2f', square=True)
        ax.invert_yaxis()
        plt.savefig(save_path.split('.png')[0]+'_valid.png')
        plt.close()

def plot_pre_ans(save_path, k, train_pre, train_ans, valid_pre = None, valid_ans = None, plot_pos = [35, 12, 52]):

    fig, ax = plt.subplots(figsize=(8, 8))
    ax.set_xlim(-2,14)
    ax.set_ylim(-2,14)
    ax.grid()
    ax.set_xticks(np.linspace(-2,14,9))
    ax.set_yticks(np.linspace(-2,14,9))

    train = np.concatenate([train_pre, train_ans], axis = 1) * k
    train_pts = []
    ans_pts = []

    for i in plot_pos:
        ans = [int(i/10)*2, int(i%10)*2]
        ans_pts.append(ans)

        for j in range(train.shape[0]):
            if train[j][2] == ans[0] and train[j][3] == ans[1]:
                train_pts.append([train[j][0], train[j][1]])

    if len(train_pts) is not 0:
        train_pts = np.array(train_pts).transpose()
        ax.scatter(train_pts[0], train_pts[1], label='train', s=30, alpha = 0.4)

    if valid_pre is not None:
        valid = np.concatenate([valid_pre, valid_ans], axis = 1) * k
        valid_pts = []

        for i in plot_pos:
            ans = [int(i/10)*2, int(i%10)*2]

            for j in range(valid.shape[0]):
                if valid[j][2] == ans[0] and valid[j][3] == ans[1]:
                    valid_pts.append([valid[j][0], valid[j][1]])

        if len(valid_pts) is not 0:
            valid_pts = np.array(valid_pts).transpose()
            ax.scatter(valid_pts[0], valid_pts[1], label='validation', s=30, alpha = 0.4)

    ans_pts = np.array(ans_pts).transpose()
    ax.scatter(ans_pts[0], ans_pts[1], label='answer', s=50, marker="*")

    ax.legend()
    plt.savefig(save_path)
    plt.close()

def plot_pre_ans2(save_path, k, valid_pre, valid_ans, plot_pos = [35, 12, 52]):

    fig, ax = plt.subplots(figsize=(8, 8))
    ax.set_xlim(-2,14)
    ax.set_ylim(-2,14)
    ax.grid()
    ax.set_xticks(np.linspace(-2,14,9))
    ax.set_yticks(np.linspace(-2,14,9))

    ans_pts = []

    valid = np.concatenate([valid_pre, valid_ans], axis = 1) * k
    valid_pts = []

    for i in plot_pos:
        ans = [int(i/10)*2, int(i%10)*2]
        ans_pts.append(ans)

        for j in range(valid.shape[0]):
            if valid[j][2] == ans[0] and valid[j][3] == ans[1]:
                valid_pts.append([valid[j][0], valid[j][1]])

    if len(valid_pts) is not 0:
        valid_pts = np.array(valid_pts).transpose()
        ax.scatter(valid_pts[0], valid_pts[1], label='estimated', s=30, alpha = 0.4)

    ans_pts = np.array(ans_pts).transpose()
    ax.scatter(ans_pts[0], ans_pts[1], label='truth', s=50, marker="*")

    ax.legend()
    plt.savefig(save_path)
    plt.close()

def average_distance(pre, ans):
    print(np.max(ans, axis=0))
    size = np.max(ans, axis=0)
    ret = (pre - ans)*(pre - ans)
    ave = np.zeros((int(size[0]/2+1), int(size[1]/2+1)), dtype=np.float64)
    cnt = np.zeros((int(size[0]/2+1), int(size[1]/2+1)), dtype=np.float64)
    for i in range(len(ans)):
        ave[int(ans[i][0]/2), int(ans[i][1]/2)] += np.sqrt((ret[i][0] + ret[i][1]))
        cnt[int(ans[i][0]/2), int(ans[i][1]/2)] += 1.
    for i in range(len(cnt)):
        for j in range(len(cnt[0])):
            if cnt[i][j] != 0.:
                ave[i][j] /= cnt[i][j]
    return np.nan_to_num(ave)

def mse(pre_in, ans_in):
    pre = pre_in.clone().detach().numpy()
    ans = ans_in.clone().detach().numpy()
    sum = 0
    for i in range(len(pre)):
        temp = 0
        for j in range(len(pre[i])):
            temp += (pre[i][j] - ans[i][j])**2
        temp /= len(pre[i])
        sum += temp
    print(sum)

vcnn = VCNN_controller(data_path="../data/sound/")
vcnn.auto_train(1000)
vcnn.plot_map()
vcnn.save_weight()
#vcnn = VCNN_controller(data_path="../data/sound_small/")
#vcnn.load_weight()
#vcnn.plot()


from common.libs.harri_db import db_session
from app_name.models.models import Schedule, Station, Reservation
from sqlalchemy.orm import load_only, contains_eager, aliased, joinedload
from datetime import datetime
from sqlalchemy.orm import Load

class ReservationRepository(object):
    @staticmethod
    def get_reservation_by_id(schedule_id, load_only_tuple=None):
        if load_only_tuple == None:
            query = db_session.query(Schedule)
        else:
            query = db_session.query(Schedule).options(load_only(*(load_only_tuple)))
        reservation_request = query.filter(Schedule.id == schedule_id).one()
        return reservation_request

    @staticmethod
    def get_all_reservations():
        request = db_session.query(Schedule).all()
        return request

    @staticmethod
    def get_all_reservations_per_user(customer_id):
        station1 = aliased(Station)
        station2 = aliased(Station)
        query = db_session.query(Reservation)\
            .join(Schedule, Reservation.schedule_id == Schedule.id) \
            .join(station1, station1.id == Schedule.departure_station_id) \
            .join(station2, station2.id == Schedule.arrival_station_id) \
            .options(
            Load(Reservation).load_only("id").
                contains_eager(Schedule).load_only("id", "arrival_time", "departure_time", "price"),
            contains_eager(Schedule.departure_station, alias=station1).load_only("id", "name"),
            contains_eager(Schedule.arrival_station, alias=station2).load_only("id", "name")
                ).filter(Reservation.customer_id >= customer_id)

        request = query.all()
        return request

    @staticmethod
    def create_reservation(customer_id, schedule_id):
        try: # need to validate input
            reservation = Reservation()
            reservation.customer_id = customer_id
            reservation.schedule_id = schedule_id
            db_session.add(reservation)
            db_session.commit()
        except Exception as e:
            db_session.rollback()
            return False
        return True


# -*- encoding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.core.urlresolvers import reverse

from .models import City

import logging
logger = logging.getLogger(__name__)

def change_city_1(request, slug):
    try:
        city = City.objects.get(slug = slug)
    except City.DoesNotExist:
        raise Http404
    html = 'changed'
    return HttpResponse('Holla! Your choice is %s' % city)

def change_city_2(request, slug):
    from django.shortcuts import get_object_or_404
    city = get_object_or_404(City, slug = slug)

    from django.template.loader import get_template
    template = get_template('city/holla.html')
    html = template.render({'city':city})

    return HttpResponse(html)

def change_city_3(request, slug):
    from django.shortcuts import get_object_or_404, render
    city = get_object_or_404(City, slug=slug)

    city_pk = request.session.get('city_pk', None)

    context = {
        'city': city,
        'city_pk':city_pk,
    }

    request.session['city_pk'] = city.pk

    return render(request, 'city/ses.html', context)

def change_city_4(request, slug):
    from django.shortcuts import get_object_or_404, render
    city = get_object_or_404(City, slug=slug)

    if not city.show:
        raise Http404

    request.session['city_pk'] = city.pk
    logger.debug('Holla!!!')

    return HttpResponseRedirect(reverse('mainpage'))


'''
Created on Nov 2, 2012

@author: neeraj
'''
import numpy as np
from scipy.optimize import fmin_l_bfgs_b as bfgs
import scipy.sparse as sp
#from scipy.optimize.minpack import check_gradient

def matdot(U, V, rows, cols):
    return (U[rows]*V[cols]).sum(axis=1)

def matvec(V, E, rows, cols, M, N):
    Matrix   = sp.coo_matrix((E, np.vstack((rows, cols))), shape=(M, N), dtype=float).tocsr()
    return np.array(Matrix.dot(V), copy=False)

def f_and_g(w, reg, rows, cols, Y, M, N, rank,Su,Sv,Su_root,Sv_root):
    '''Matrix factorization with sq loss'''
    #######################################
    # re-set sizes
    #######################################
    W = w.reshape((M+N, rank))
    U = W[:M]
    V = W[M:]

    # the following avoids unnecessary memory copy for MF.
    Yp = matdot(U, V, rows, cols) # U, V -> Yp
    E = Yp - Y

    U_hat = (Su_root*U).ravel()
    V_hat = (Sv_root*V).ravel()
    cost  = 0.5*( np.dot(E,E) + reg*( np.dot(U_hat, U_hat) + np.dot(V_hat, V_hat) ) )

    # grad of cost = KEV, GE'U
    EV = matvec(V, E, rows, cols, M, N)
    EU = matvec(U, E, cols, rows, N, M)

    grad     = np.empty(W.shape)
    grad[:M] = EV + reg*Su*U
    grad[M:] = EU + reg*Sv*V
    #print cost/len(Y)
    return cost, grad.ravel()

def mf_solver(rows, cols, vals, rank, reg, M, N, Su,Sv,Su_root,Sv_root):
    w0 = np.random.randn((M+N)*rank)
    Su_root = sp.coo_matrix((Su_root,(range(M),range(M))),shape=(M,M))
    Sv_root = sp.coo_matrix((Sv_root,(range(N),range(N))),shape=(N,N))
    Su = sp.coo_matrix((Su,(range(M),range(M))),shape=(M,M))
    Sv = sp.coo_matrix((Sv,(range(N),range(N))),shape=(N,N))

    w = bfgs(func=f_and_g, x0=w0, args=(reg, rows, cols, vals, M, N, rank ,Su,Sv,Su_root,Sv_root),maxfun=500)[0]
    W = w.reshape((M+N, rank))
    U = W[:M]
    V = W[M:]

    return U, V


import sys
from os.path import basename
from PyQt5.QtCore import QPoint, Qt, QRect
from PyQt5.QtWidgets import QAction, QMainWindow, QApplication, QPushButton, QMenu, QFileDialog
from PyQt5.QtGui import QPixmap, QImage, QPainter, QPen

import SnippingTool

class Menu(QMainWindow):

    default_title = "Snipping Tool"

    def __init__(self):
        super().__init__()

        self.title = Menu.default_title

        # New snip
        new_snip_action = QAction('New', self)
        new_snip_action.setShortcut('Ctrl+N')
        new_snip_action.setStatusTip('Snip!')
        new_snip_action.triggered.connect(self.new_image_window)

        # Exit
        exit_window = QAction('Exit', self)
        exit_window.setShortcut('Ctrl+Q')
        exit_window.setStatusTip('Exit application')
        exit_window.triggered.connect(self.close)

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(new_snip_action)
        self.toolbar.addAction(exit_window)

        self.snippingTool = SnippingTool.SnippingWidget()
        self.show()

    # snippingTool.start() will open a new window.
    def new_image_window(self):

        self.snippingTool.start()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainMenu = Menu()
    sys.exit(app.exec_())


# Import the os module to allow us to create file
# paths across operating systems and Module
# for reading CSV files
import os
import csv
from collections import Counter

# Path to collect data from the csv file in the Resources folder
csvpath = os.path.join('Resources', 'election_data.csv')

# open the csv file
with open(csvpath, newline='') as csvfile:

    # CSV reader specifies delimiter and variable
    # that hold its contents
    csvreader = csv.reader(csvfile, delimiter=',')

    # print title to the screen
    print(f"Election Results")
    print(f"--------------------")

    # Read the header row first (skip this step if there
    # is no header)
    csv_header = next(csvreader)

    # Calulate the total number of votes cast and print it
    # to the screen
    value = len(list(csvreader))

    print(f"Total number of votes cast: " + str(value))
    print(f"--------------------")

# Calulate the total number of votes
# A complete list of candidates who received votes
# The percentage of votes each candidate won
# The total number of votes each candidate won
# The winner of the election based on popular vote.

from pprint import pprint
# Path to collect data from the csv file in the Resources folder
csvpath = os.path.join('Resources', 'election_data.csv')
#open the csv file
with open(csvpath, newline='') as csvfile:

    # CSV reader specifies delimiter and variable that holds contents
    csvreader = csv.reader(csvfile, delimiter=',')
    csv_header = next(csvreader)
    #print(csv_header)

    #Calulates the total number of votes
    #totalC = len(list(csvreader))
    #print(totalC)
    #A complete list of candidates who received votes
    result = {}
    candTotal = 0
    avg = 0
    for cand in csvreader:
        candTotal += 1
        #print(cand)
        if cand[2] not in result:
            result[cand[2]] = 1
            #print(result)
        else:
            result[cand[2]] = result[cand[2]] + 1

    output_file = os.path.join("output", "PollReport.txt")
    with open(output_file, "w", newline="") as pollrep:
        for k, v in result.items():

            avg = '{0:.4%}'.format(v/candTotal)
            #Print to screen
            print(k, avg, '(',v,')')
            polldata = f'{k} {avg} "("{v},")"'

            #Print to file
            pollrep.writelines(polldata)
        winner = max(result.values())
        winnername = max(result, key=result.get)
        print('--------------------')
        print(f'Winner: {winnername} {winner}')
        print('--------------------')


"""File containing the Graphical User Interface of the project"""
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import messagebox
from filemanager import getfile
from criteria import correctcfile
from criteria import correcthfile
from process import processdata

class HagresGUI(object):
    """Class for main window managing"""
    def __init__(self):
        self.cfile = ""
        self.hfile = ""
        self.texts = {'text01':('Para continuar, seleccione la ruta para los ficheros'
                                ' de complejo simplicial y homología generados por '
                                'la herramienta Voxelo.'),
                      'text02': 'Complejo simplicial: ',
                      'text03': 'Homología: ',
                      'text04': 'Seleccione la ruta del fichero.',
                      'text05': 'Buscar...',
                      'text06': 'Procesar',
                      'text07': 'Acerca de',
                      'text08': 'Proyecto HAGRES',
                      'text09': ('TFG por Juan Jesús Pérez Luna para la titulación de '
                                 'Ingeniería del Software.\nDepartamento Matemática Aplicada 1'
                                 ' (MA1) https://github.com/juanjepl/hagres'),
                      'text10': 'Aceptar',
                      'text11': 'Acerca de',
                      'text12': 'Por favor, seleccione ambos ficheros antes de continuar.',
                      'text13': ('Alguno de los ficheros de entrada no parece '
                                 'ser correcto, por favor revíselos.'),
                      'text14': 'Procesado en curso:',
                      'text15': 'Leyendo fichero de complejo simplicial...',
                      'text16': 'Hecho.',
                      'text17': 'Leyendo fichero de homología...',
                      'text18': 'Creando estructuras de vóxeles...',
                      'text19': 'Creando estructuras intermedias de homología...',
                      'text20': 'Formando árbol sin redundancias...',
                      'text21': 'Guardando fichero de salida...',
                      'text22': 'Proceso finalizado con éxito.',
                      'text23': ''}

    def rungui(self):
        """Method for running the GUI"""
        hagreswindow = Tk()
        hagreswindow.geometry("600x300+300+200")
        hagreswindow.wm_iconbitmap("img/favicon.ico")
        hagreswindow.title(self.texts['text08'])
        hagreswindow.config(bg="white")
        hagreswindow.resizable(0, 0)
        imagebanner = PhotoImage(file="img/appbanner.gif")
        appbanner = Label(hagreswindow, image=imagebanner, border=0)
        appbanner.place(x=0, y=0)

        exptext = Message(hagreswindow, text=self.texts['text01'],
                          bg="white", fg="black", padx="10", pady="10",
                          width="580", font=("arial", 10))
        exptext.place(x=0, y=100)

        clabel = Label(hagreswindow, text=self.texts['text02'],
                       bg="white", padx="10", pady="10", font=("arial", 10))
        clabel.place(x=10, y=159)

        hlabel = Label(hagreswindow, text=self.texts['text03'],
                       bg="white", padx="10", pady="10", font=("arial", 10))
        hlabel.place(x=60, y=199)

        cpath = StringVar()
        hpath = StringVar()

        self.cbox = Entry(hagreswindow, textvariable=cpath,
                          bg="white", fg="grey", width=58,
                          state="readonly")
        self.cbox.place(x=150, y=170)
        self.hbox = Entry(hagreswindow, textvariable=hpath,
                          bg="white", fg="grey", width=58,
                          state="readonly")
        self.hbox.place(x=150, y=210)

        cpath.set(self.texts['text04'])
        hpath.set(self.texts['text04'])

        cpathbtn = Button(hagreswindow, text=self.texts['text05'],
                          padx="10", pady="0", font=("arial", 9),
                          command=self.getsimplicialcomplexfileuri)
        cpathbtn.place(x=510, y=167)

        hpathbtn = Button(hagreswindow, text=self.texts['text05'],
                          padx="10", pady="0", font=("arial", 9),
                          command=self.gethomologyfileuri)
        hpathbtn.place(x=510, y=207)

        gobtn = Button(hagreswindow, text=self.texts['text06'],
                       padx="10", pady="5", font=("arial", 10),
                       command=self.startprocess)
        gobtn.place(x=260, y=250)

        aboutbtn = Button(hagreswindow, text=self.texts['text07'],
                          padx='5', pady='3', font=('arial', 8), bg='white',
                          command=self.showauthor)
        aboutbtn.place(x=520, y=20)

        self.mainwindow = hagreswindow

        hagreswindow.mainloop()

    def getsimplicialcomplexfileuri(self):
        """Method who takes the contents of simplicial complex file"""
        fileuri = askopenfilename(filetypes=(("Ficheros de texto", "*.txt"),
                                             ("Todos los ficheros", "*.*")))
        self.cfile = fileuri
        if fileuri != "":
            updateduri = StringVar()
            updateduri.set(fileuri)
            self.cbox.config(textvariable=updateduri)

    def gethomologyfileuri(self):
        """Method who takes the contents of homology file"""
        fileuri = askopenfilename(filetypes=(("Ficheros de texto", "*.txt"),
                                             ("Todos los ficheros", "*.*")))
        self.hfile = fileuri
        if fileuri != "":
            updateduri = StringVar()
            updateduri.set(fileuri)
            self.hbox.config(textvariable=updateduri)

    def showauthor(self):
        """Method for managing about window"""
        aboutwindow = Toplevel(self.mainwindow)
        relx = self.mainwindow.winfo_x()+180
        if relx < 0:
            relx = str(relx)
        else:
            relx = "+"+str(relx)

        if self.mainwindow.winfo_y() <= 50:
            rely = self.mainwindow.winfo_y()
        else:
            rely = self.mainwindow.winfo_y()-50

        if rely < 0:
            rely = str(rely)
        else:
            rely = "+"+str(rely)

        aboutwindow.geometry('240x400'+relx+rely)
        aboutwindow.wm_iconbitmap("img/favicon.ico")
        aboutwindow.title(self.texts['text11'])
        aboutwindow.config(bg='white')
        aboutwindow.minsize(240, 400)
        aboutwindow.maxsize(240, 400)

        imgabout = PhotoImage(file="img/scomplex.gif")
        imgaboutlabel = Label(aboutwindow, image=imgabout, border=0)
        imgaboutlabel.place(x=20, y=20)

        exptitle = Label(aboutwindow, text=self.texts['text08'],
                         bg="white", fg="black", padx="10", pady="10", font=("arial", 12))
        exptitle.place(x=40, y=240)

        exptext = Message(aboutwindow, text=self.texts['text09'],
                          justify="center", bg="white", fg="black",
                          padx="10", pady="0", width="200", font=("arial", 8))
        exptext.place(x=10, y=280)

        def stopaboutwindow():
            """To stop the about window"""
            aboutwindow.destroy()

        okbutton = Button(aboutwindow, text=self.texts['text10'],
                          width="18", pady=5, padx=5, font=("arial", 10),
                          justify='center', command=stopaboutwindow)
        okbutton.place(x=40, y=350)

        aboutwindow.focus_set()
        aboutwindow.transient(self.mainwindow)
        aboutwindow.grab_set()
        self.mainwindow.wait_window(aboutwindow)

        aboutwindow.mainloop()

    def startprocess(self):
        """Method who calls the entire process"""

        if self.cfile == "" or self.hfile == "":
            messagebox.showinfo('Aviso', message=self.texts['text12'])
        else:
            complexfile = getfile(self.cfile)
            homologyfile = getfile(self.hfile)

            if not(correctcfile(complexfile) and correcthfile(homologyfile)):
                messagebox.showinfo('Aviso', message=self.texts['text13'])
            else:
                processdata(complexfile, homologyfile)
                self.showinfowindow()

    def showinfowindow(self):
        """method for info window"""
        self.mainwindow.destroy()

        self.mainwindow = Tk()
        self.mainwindow.geometry("600x300+300+200")
        self.mainwindow.wm_iconbitmap("img/favicon.ico")
        self.mainwindow.title(self.texts['text08'])
        self.mainwindow.config(bg="white")
        self.mainwindow.resizable(0, 0)
        imagebanner = PhotoImage(file="img/appbanner.gif")
        appbanner = Label(self.mainwindow, image=imagebanner, border=0)
        appbanner.place(x=0, y=0)
        aboutbtn = Button(self.mainwindow, text=self.texts['text07'],
                          padx='5', pady='3', font=('arial', 8), bg='white',
                          command=self.showauthor)
        aboutbtn.place(x=520, y=20)

        self.progressframe = Frame(self.mainwindow, bg="white", width=600, height=200)
        self.progressframe.place(x=0, y=100)

        self.showlabel_startingprocess()

        self.showlabel_readingsimplexfile()
        self.showlabel_readingsimplexfile_done()

        self.showlabel_readinghomologyfile()
        self.showlabel_readinghomologyfile_done()

        self.showlabel_creatingvoxelstructures()
        self.showlabel_creatingvoxelstructures_done()

        self.showlabel_creatingmiddlestructures()
        self.showlabel_creatingmiddlestructures_done()

        self.showlabel_doingtree()
        self.showlabel_doingtree_done()

        self.showlabel_savefile()
        self.showlabel_savefile_done()

        self.showlabel_process_complete()

        self.mainwindow.mainloop()

    def showlabel_startingprocess(self):
        startprogress = Label(self.progressframe, text=self.texts['text14'],
                              bg='white', fg='black', padx='0', pady='0', font=('arial', 10))
        startprogress.place(x=10, y=10)

    def showlabel_readingsimplexfile(self):
        readsimplexfile = Label(self.progressframe, text=self.texts['text15'],
                                bg='white', fg='black', padx='0', pady='0', font=('arial', 10))
        readsimplexfile.place(x=10, y=40)

    def showlabel_readingsimplexfile_done(self):
        readsimplexfiledone = Label(self.progressframe, text=self.texts['text16'],
                                    bg='white', fg='green', padx='0', pady='0', font=('arial', 10))
        readsimplexfiledone.place(x=250, y=40)

    def showlabel_readinghomologyfile(self):
        readhomologyfile = Label(self.progressframe, text=self.texts['text17'],
                                 bg='white', fg='black', padx='0', pady='0', font=('arial', 10))
        readhomologyfile.place(x=10, y=60)

    def showlabel_readinghomologyfile_done(self):
        readhomologyfiledone = Label(self.progressframe, text=self.texts['text16'],
                                     bg='white', fg='green', padx='0', pady='0',
                                     font=('arial', 10))
        readhomologyfiledone.place(x=200, y=60)

    def showlabel_creatingvoxelstructures(self):
        creatingvoxelstructures = Label(self.progressframe, text=self.texts['text18'],
                                        bg='white', fg='black', padx='0', pady='0',
                                        font=('arial', 10))
        creatingvoxelstructures.place(x=10, y=80)

    def showlabel_creatingvoxelstructures_done(self):
        creatingvoxelstructuresdone = Label(self.progressframe, text=self.texts['text16'],
                                            bg='white', fg='green', padx='0', pady='0',
                                            font=('arial', 10))
        creatingvoxelstructuresdone.place(x=210, y=80)

    def showlabel_creatingmiddlestructures(self):
        creatingmiddlestructures = Label(self.progressframe,
                                         text=self.texts['text19'],
                                         bg='white', fg='black', padx='0', pady='0',
                                         font=('arial', 10))
        creatingmiddlestructures.place(x=10, y=100)

    def showlabel_creatingmiddlestructures_done(self):
        creatingmiddlestructuresdone = Label(self.progressframe, text=self.texts['text16'],
                                             bg='white', fg='green', padx='0', pady='0',
                                             font=('arial', 10))
        creatingmiddlestructuresdone.place(x=300, y=100)

    def showlabel_doingtree(self):
        doingtree = Label(self.progressframe, text=self.texts['text20'],
                          bg='white', fg='black', padx='0', pady='0',
                          font=('arial', 10))
        doingtree.place(x=10, y=120)

    def showlabel_doingtree_done(self):
        doingtreedone = Label(self.progressframe, text=self.texts['text16'],
                              bg='white', fg='green', padx='0', pady='0',
                              font=('arial', 10))
        doingtreedone.place(x=220, y=120)

    def showlabel_savefile(self):
        savefile = Label(self.progressframe, text=self.texts['text21'],
                         bg='white', fg='black', padx='0', pady='0',
                         font=('arial', 10))
        savefile.place(x=10, y=140)

    def showlabel_savefile_done(self):
        savefiledone = Label(self.progressframe, text=self.texts['text16'],
                             bg='white', fg='green', padx='0', pady='0',
                             font=('arial', 10))
        savefiledone.place(x=190, y=140)

    def showlabel_process_complete(self):
        processdone = Label(self.progressframe, text=self.texts['text22'],
                            bg='white', fg='green', padx='0', pady='0',
                            font=('arial', 10))
        processdone.place(x=10, y=165)


from pyramid.authentication import SessionAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy

from pyramid.config import Configurator

from pyramid.renderers import JSON

from pyramid.session import SignedCookieSessionFactory

from pyramid.security import (
    ALL_PERMISSIONS,
    Everyone,
    Authenticated,
    Allow
)

from sqlalchemy import engine_from_config

from .models import (
    DB_SESSION,
    BASE,
)

from argux_net_monitor import dao

from argux_net_monitor.monitors.ICMPMonitor import ICMPMonitor
from argux_net_monitor.monitors.ServerMonitor import ServerMonitor

from argux_net_monitor.monitors import (
    register_monitor,
    start_monitors
)

# MAP GROUPS TO PERMISSIONS
class RootFactory(object):

    """ RootFactory:

    __acl__: base ACL
    """

    __acl__ = [
        (Allow, 'g:admin', ALL_PERMISSIONS),
        (Allow, Authenticated, 'view'),
        (Allow, Everyone, 'koffie')
    ]

    def __init__(self, request):
        """Initialize RootFactory."""
        self.request = request

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DB_SESSION.configure(bind=engine)
    BASE.metadata.bind = engine
    settings['dao'] = dao.DAO(DB_SESSION)

    if settings['session.secure_cookie'] == 'true':
        secure_cookie=True
    else:
        secure_cookie=False

    factory = SignedCookieSessionFactory(
        'SEECREET',
        cookie_name='argux_net_monitor',
        secure=secure_cookie,
        httponly=True)
    authentication_policy = SessionAuthenticationPolicy()
    authorization_policy = ACLAuthorizationPolicy()

    config = Configurator(
        settings=settings,
        root_factory=RootFactory
    )
    config.set_session_factory(factory)
    config.set_authentication_policy(authentication_policy)
    config.set_authorization_policy(authorization_policy)

    config.include('pyramid_chameleon')
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('login', '/login')
    config.add_route('logout', '/logout')
    config.add_route('home', '/')
    config.add_route('status', '/status')
    config.add_route('monitor_icmp', '/monitor/icmp')

    config.add_route('rest_login_1',
                     '/rest/1.0/login')
    config.add_route('rest_logout_1',
                     '/rest/1.0/logout')
    config.add_route('rest_host_addresses_1',
                     '/rest/1.0/host/{host}/addr')
    config.add_route('rest_host_address_1',
                     '/rest/1.0/host/{host}/addr/{address}')
    config.add_route('rest_monitor_1',
                     '/rest/1.0/monitor/{type}/{host}/{address}')

    config.scan('.views')
    config.scan('.rest.views')

    monitor = ICMPMonitor()
    register_monitor('icmp', monitor, settings['dao'])

    start_monitors()

    # Only start the server monitor if the server-url
    # is specified. The Server-Monitor is a special kind
    # of monitor that does not need registration.
    if settings['argux-server.url']:
        monitor = ServerMonitor(
            settings['argux-server.url'],
            settings['argux-server.user'],
            settings['argux-server.pass'])
        monitor.start()
    else:
        # TODO: Use proper logger.
        print("argux-server.url=='', not forwarding")

    return config.make_wsgi_app()


def parser(iterable):  # функция для парсинга итерируемых объектов
    sum = 0
    for item in iterable:
        if isinstance(item, dict):
            sum += dict_parser(item)
        elif isinstance(item, list):
            sum += parser(item)
        elif isinstance(item, int):
            sum += item
    return sum

def dict_parser(object):  # отдельная функция для парсинга словарей, чтобы не делать первую функцию слишком большой
    sum = 0
    if 'red' in object.keys() or 'red' in object.values():  # убираем словари, в которых есть 'red'
        return 0
    for value in object.values():
        if isinstance(value, dict):
            sum += dict_parser(value)
        elif isinstance(value, list):
            sum += parser(value)
        elif isinstance(value, int):
            sum += value
    return sum

puzzle = open('D:\PyCharm projects\Advent_of_code_2015\Day12\puzzle_input', encoding='utf-8')
puzzle_list = eval(puzzle.read())
print(parser(puzzle_list))


'''

Description:

Given a linked list, remove the n-th node from the end of list and return its head.

Example:

Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:

Given n will always be valid.

Follow up:

Could you do this in one pass?

Hint:
Maintain two pointers and update one with a delay of n steps.

'''

# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:

        # use dummy head will make the removal of head node easier
        dummy_head = ListNode(-1)
        dummy_head.next = head

        # cur keeps iteration till the end
        # prev_of_removal traverses to the previous node of the one of being removed
        cur, prev_of_removal = dummy_head, dummy_head

        while cur.next != None:

            # n-step delay for prev_of_removal
            if n <= 0:
                prev_of_removal = prev_of_removal.next

            cur = cur.next

            n -=1

        # Remove the N-th node from end of list
        n_th_node = prev_of_removal.next
        prev_of_removal.next = n_th_node.next

        del n_th_node

        return dummy_head.next

# n : the length of linked list

## Time Complexity: O( n )
#
# The overhead in time is the cost of linear scan of linked list, which is of O( n ).

## Space Complexity: O( 1 )
#
# The overhead in space is the storage for two-pointers, which is of O( 1 ).

def linked_list_factory( elements ):

    last_node = None
    for element in reversed( elements ):
        cur_node = ListNode( element )
        cur_node.next = last_node
        last_node = cur_node

    return last_node

def linked_list_print( head: ListNode ):

    cur = head

    while cur:

        print( cur.val, end = '->' )

        cur = cur.next

    print('None\n')

def test_bench():

    # expected output:
    '''
    1->2->3->4->None
    '''
    head_of_test_1 = linked_list_factory( [1,2,3,4,5] )
    head_of_test_1 = Solution().removeNthFromEnd( head_of_test_1, 1 )
    linked_list_print( head_of_test_1 )

    # expected output:
    '''
    1->2->3->5->None
    '''
    head_of_test_2 = linked_list_factory( [1,2,3,4,5] )
    head_of_test_2 = Solution().removeNthFromEnd( head_of_test_2, 2 )
    linked_list_print( head_of_test_2 )

    # expected output:
    '''
    1->2->4->5->None
    '''
    head_of_test_3 = linked_list_factory( [1,2,3,4,5] )
    head_of_test_3 = Solution().removeNthFromEnd( head_of_test_3, 3 )
    linked_list_print( head_of_test_3 )

    # expected output:
    '''
    1->3->4->5->None
    '''
    head_of_test_4 = linked_list_factory( [1,2,3,4,5] )
    head_of_test_4 = Solution().removeNthFromEnd( head_of_test_4, 4 )
    linked_list_print( head_of_test_4 )

    # expected output:
    '''
    2->3->4->5->None
    '''
    head_of_test_5 = linked_list_factory( [1,2,3,4,5] )
    head_of_test_5 = Solution().removeNthFromEnd( head_of_test_5, 5 )
    linked_list_print( head_of_test_5 )

if __name__ == '__main__':

    test_bench()

No_0019_Remove Nth Node From End of List/by_two_pointers_and_n_step_delay.py


import numpy as np

from wildcat.network.local_endpoint import LocalEndpoint
from wildcat.solver.base_solver import BaseSolver
from wildcat.util.matrix import check_symmetric, random_symmetric_matrix

def test_qubo_solver_initialization():
    solver = BaseSolver()
    assert not (solver is None)

def test_qubo_solver_initialization_with_qubo():
    solver = BaseSolver()
    solver.qubo = np.ones([100, 100])
    assert np.array_equal(solver.qubo, np.ones([100, 100]))

def test_qubo_solver_ising_interactions_from_qubo_is_symmetric():
    solver = BaseSolver()
    qubo = random_symmetric_matrix()
    solver.qubo = qubo.copy()
    solver.build_ising_interactions()
    ising = solver.ising_interactions.copy()
    assert check_symmetric(ising)

def test_qubo_solver_qubo_from_ising_interactions_is_symmetric():
    solver = BaseSolver()
    ising = random_symmetric_matrix()
    solver.ising_interactions = ising.copy()
    solver.build_qubo()
    qubo = solver.qubo.copy()
    assert check_symmetric(qubo)

def test_qubo_solver_ising_qubo_ising_reconstructs_original():
    solver = BaseSolver()
    ising = random_symmetric_matrix()
    solver.ising_interactions = ising.copy()
    solver.build_qubo()
    solver.build_ising_interactions()
    reconstructed_ising = solver.ising_interactions
    assert np.allclose(ising, reconstructed_ising)

def test_qubo_solver():
    solver = BaseSolver()
    solver.qubo = np.ones([100, 100])
    solver.build_ising_interactions()
    assert solver.ising_interactions.shape == (100, 100)

def test_base_solver_build_ising_interactions_with_qubo():
    solver = BaseSolver()
    solver.qubo = random_symmetric_matrix()
    callback = lambda result: None
    solver.solve(callback, endpoint=LocalEndpoint())
    assert solver.ising_interactions.shape[0] > 0

def test_base_solver_build_qubo_with_ising_interactions():
    solver = BaseSolver()
    solver.ising_interactions = random_symmetric_matrix()
    callback = lambda result: None
    solver.solve(callback, endpoint=LocalEndpoint())
    assert solver.qubo.shape[0] > 0


import numpy as np
import tensorflow as tf
import tensorflow.contrib.eager as tfe

tfe.enable_eager_execution()

def smooth(y, box_pts):
    y_smooth = np.zeros_like(y)
    box = np.ones(box_pts)/box_pts
    if y.shape[1] > 1:
        for i in range(y.shape[1]):
            y_smooth[:, i] = np.convolve(y[:, i], box, mode='same')

    return y_smooth

S_t = tf.constant(0.0)

def assign(x, y):
    x = y

    return x

def exp_moving_average(i, X):
    alpha = 0.95
    c = tf.equal(i, tf.constant(0))
    t_f = lambda S_t: assign(S_t, X[0])
    f_f = lambda S_t: assign(S_t, alpha*X[i] + (1-alpha)*S_t); assign(X[i], S_t)

    tf.cond(c, t_f, f_f)

    return X

def smooth_loss(output, target):
    batch_size, max_time, output_size = output.shape
    i = tf.constant(0)
    c = tf.less(i, max_time)
    b = lambda i, output: exp_moving_average(i, output)

    exp_mvg_o = tf.while_loop(c, b, [i])

    return tf.reduce_mean(loss_function(exp_mvg_o, target))

def conv_loss(output, target):
    batch_size, max_time, output_size = output.shape
    i = tf.constant(0)
    c = tf.less(i, max_time)
    b = lambda i, output: exp_moving_average(i, output)

    exp_mvg_o = tf.while_loop(c, b, [i])

    return tf.reduce_mean(loss_function(exp_mvg_o, target))

def unreg_loss(pred, alpha):
    # pred is RNN prediciton, alpha is coeficient balance this loss with other losses.
    out = pred[:, 1:] - pred[:, 0:-1]
    mean, var = tf.nn.moments(out, axes=1)

    return alpha*(var[0]+var[1])

output = tf.constant(np.array([1, 2, 3, 4]))
target = tf.constant(np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]]))

#batch_size, max_time, output_size = output.shape
max_time = output.shape
i = tf.constant(0)
c = tf.less(i, max_time)
#b = lambda i, output: exp_moving_average(i, output)
m, v = unreg_loss(target, 0.2)
#exp_mvg_o = tf.while_loop(c, b, [i])
exp_mvg_o = np.zeros(4)
with tf.Session() as sess:
    tf.global_variables_initializer()

    #for i in range(4):
        #exp_mvg_o[i] = exp_moving_average(i, output)
    #print(exp_mvg_o)


import re
text_value="Today is Monday while it was sunday yesterday"

value=re.search("day",text_value)
if value:
    print("Yes there is a match")
else:
    print("no match")

value=re.findall('day',text_value)
print(len(value))
print(value)
#to check the code


import logging
import pandas as pd
import requests
import os
from bs4 import BeautifulSoup
from datetime import datetime

logger = logging.getLogger('nodes.data_gathering')

def update(client, params):
	response = requests.get(params.url)
	soup = BeautifulSoup(response.content)
	movie_names = [movie.find('a')['title'].replace('Filme ','') for movie in soup.find_all('div', attrs={'class':'col-sm-6 col-md-3'})]
	movie_links = ['https://www.cinemark.com.br/' + movie.find('a')['href'] for movie in soup.find_all('div', attrs={'class':'col-sm-6 col-md-3'})]

	classificacao = []
	trailers = []
	duracao = []

	# run through the movies and get some info from it.
	df = pd.DataFrame()
	for link in movie_links:
		response = requests.get(link)
		soup = BeautifulSoup(response.content, 'xlml')

		try:
			classificacao.append(soup.find('div', attrs={'class':'movie-details'}).find_all('span')[1].text)
			trailers.append(soup.find('div', attrs={'class':'movie-details'}).find_all('li')[1].find('a')['href'].replace('//','https://'))
		except:
			classificacao.append('unknown')
			trailers.append('unknown')

	if len(classificacao) != len(movie_names):
		classificacao = classificacao[0:len(movie_names)]

	df = pd.concat([df, pd.DataFrame({'name': movie_names, 'trailers': trailers,})])
	df.to_csv(params.raw_data,index=True)

def done(client, params):
	return os.path.isfile(params.raw_data)


from PIL import ImageFilter
from torchvision import transforms
import random

imageSize = 224
normalize = transforms.Normalize(
    mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
)

class GaussianBlur(object):
    def __init__(self, sigma=[0.1, 2.0]):
        self.sigma = sigma

    def __call__(self, x):
        sigma = random.uniform(self.sigma[0], self.sigma[1])
        x = x.filter(ImageFilter.GaussianBlur(radius=sigma))
        return x

def get_all_in_aug(img_size=imageSize):
    all_in_transform = transforms.Compose(
        [
            transforms.Resize((300, 300)),
            transforms.RandomHorizontalFlip(p=0.5),
            transforms.ColorJitter(brightness=[0.5, 1.5]),
            transforms.RandomRotation(degrees=15),
            transforms.CenterCrop((img_size, img_size)),
            transforms.RandomApply([GaussianBlur([0.1, 2.0])], p=0.5),
            transforms.ToTensor(),
            normalize,
        ]
    )
    return all_in_transform

def get_aug_trnsform(img_size=imageSize):
    transform = transforms.Compose(
        [
            transforms.RandomResizedCrop(img_size, scale=(0.2, 1.0)),
            transforms.RandomApply(
                [
                    transforms.ColorJitter(
                        0.4, 0.4, 0.4, 0.1
                    )  # not strengthened
                ],
                p=0.8,
            ),
            transforms.RandomGrayscale(p=0.2),
            transforms.RandomApply([GaussianBlur([0.1, 2.0])], p=0.5),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ]
    )
    return transform

def get_eval_trnsform(img_size=imageSize):
    transform = transforms.Compose(
        [
            transforms.Resize((img_size, img_size)),
            transforms.ToTensor(),
            normalize,
        ]
    )
    return transform


"""empty message

Revision ID: 64281b6893a1
Revises: 6b643097f85e
Create Date: 2020-11-27 13:27:08.331242

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '64281b6893a1'
down_revision = '6b643097f85e'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('users', sa.Column('email', sa.String(), nullable=True))
    op.drop_column('users', 'token')
    op.drop_column('users', 'uid')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('users', sa.Column('uid', sa.INTEGER(), autoincrement=False, nullable=True))
    op.add_column('users', sa.Column('token', sa.VARCHAR(), autoincrement=False, nullable=True))
    op.drop_column('users', 'email')
    # ### end Alembic commands ###


# -*- coding: utf-8 -*-
"""
Created on Thu Sep 14 16:59:02 2017

@author: ThinkCentre
"""
#测试数据生成
#之后作为数据预处理程序，构建适合于本网络的数据输入格式
import tensorflow as tf
import constant

xs = []
for i in range(constant.INPUT_PART_NUMBER):
    str1 = 'xs' + str(i) + '=tf.get_variable(\'xs'+str(i)+\
    '\',[1,constant.INPUT_PART_NODE],dtype=tf.float32,initializer = tf.truncated_normal_initializer(10,0.1),trainable=False)'
    exec(str1)
    str2 = 'xs.append(xs'+str(i)+')'
    exec(str2)
ys = tf.get_variable('ys',[1,1],dtype=tf.float32,initializer = tf.constant_initializer(1),trainable = False)
        #xs为一个输入数据列表，ys为所有包含数据的共同标签
#with tf.Session() as sess:
#    init_op = tf.global_variables_initializer()
#    sess.run(init_op)
#    #print(xs)
#   print(sess.run(xs))


import os
import ConfigParser

from windwalker.common.constants import WINDWALKER_ROOT
from windwalker.common.constants import WINDWALKER_CONF_DIR
from windwalker.common.constants import WINDWALKER_CONF_FILE
from windwalker.common.objects import Dictionary

class Config:
    def __init__(self, cfg_file = WINDWALKER_CONF_FILE):
        """@param cfg: configuration file path."""
        cfg = os.path.join(WINDWALKER_ROOT, WINDWALKER_CONF_DIR, cfg_file)
        config = ConfigParser.ConfigParser()
        config.read(cfg)
        self.sections = config.sections()

        for section in config.sections():
            setattr(self, section, Dictionary())
            for name, raw_value in config.items(section):
                try:
                    value = config.getboolean(section, name)
                except ValueError:
                    try:
                        value = config.getint(section, name)
                    except ValueError:
                        value = config.get(section, name)

                setattr(getattr(self, section), name, value)

    def get(self, section):
        return getattr(self, section)


# Name: David Turnbough
# Date: Thursday, December 19, 2019
# Geeks For Geeks: Convert from 12 hour to 24 hour format
# https://practice.geeksforgeeks.org/problems/convert-time-from-12-hour-to-24-hour-format/0

testCase = int(input())

while(testCase > 0):
    testCase -= 1

    TwelveHourFormat = input()

    TwentyFourHourFormat = 0

    hours = TwelveHourFormat[0:2]
    minutes = TwelveHourFormat[3:5]
    seconds = TwelveHourFormat[6:8]
    period = TwelveHourFormat[8:10]

    if(period == 'PM'):
        if(hours == '12'):
            TwentyHourFormat = hours + ':' + minutes + ':' + seconds
        else:
            TwentyHourFormat = str(int(hours) + int(12)) + ':' + minutes + ':' + seconds
    else:
        if(hours == '12'):
            hours = '00'
        TwentyHourFormat = hours + ':' + minutes + ':' + seconds

    print(TwentyHourFormat)

Geeks For Geeks/Convert From 12 Hour To 24 Hour Format.py


from PIL import Image
import random as r
from math import *

#------------Fonction de base---------------

#objet image = Nbligne Nbcolonne tableaudepixel

def ouvrir(nomimage):
    #créer l'objet image a partir du fichier image
    #nomimage='lena.jpg'
    img=Image.open(nomimage).convert('F') #converti en float
    #renvoie l'objet image a partir du fichier image
    return img

def affiche(img):
    img.show()

def imc2img(imc,n): #n=0 Rouge, n=1 vert, n=2 bleu
    #transforme une image couleur en une image de niveau de gris
    img=Image.new("F",imc.size) #cree une image de niv de gris de meme taille que imc
    pixc=imc.load() #recupere le tableau des pixels de imc
    pixg=img.load() #recupere le tableau des pixels de img
    width=imc.size[0] #le nb de coolonne de l'image
    height=imc.size[1] #le nb de ligne de l'image

    #parcours de l'image : lignes/colonnes
    for j in range(height):
        for i in range(width):
            pixg[i,j]=pixc[i,j][n]

    #pour save
    #img.convert("L").save("img.bmp")
    return img

#---------------------EXERCICE 1---------------

def MoyenneImage(ims):
    imd=Image.new("F",ims.size)
    pix1=ims.load()
    pix2=imd.load()
    w=ims.size[0]
    h=ims.size[1]

    for j in range(1,h-1):
            for i in range(1,w-1):
              pix2[i,j]=(pix1[i,j]+pix1[i+1,j]+pix1[i-1,j]+pix1[i,j+1]+pix1[i,j-1])/5.0

    return imd

def MoyenneIterative(ims,n):
    imd=Image.new("F",ims.size)
    pix1=ims.load()
    pix2=imd.load()
    w=ims.size[0]
    h=ims.size[1]

    for k in range(n):
        for j in range(1,h-1):
                for i in range(1,w-1):
                  pix2[i,j]=(pix1[i,j]+pix1[i+1,j]+pix1[i-1,j]+pix1[i,j+1]+pix1[i,j-1])/5.0
        pix1=imd.copy().load()

    return imd

def EquationChaleur(ims,n,tau):
    imd=Image.new("F",ims.size)
    pix1=ims.load()
    pix2=imd.load()
    w=ims.size[0]
    h=ims.size[1]

    for k in range(n):
        for j in range(1,h-1):
                for i in range(1,w-1):
                    (1-4*tau)*pix1[i,j]+tau*(pix1[i+1,j]+pix1[i-1,j]+pix1[i,j+1]+pix1[i,j-1])

#------------------Exercice 2-----------------
def EquationChaleur(ims,n,tau):
    imd=Image.new("F",ims.size)
    pix1=ims.load()
    pix2=imd.load()
    w=ims.size[0]
    h=ims.size[1]
    for k in range(n):
        for j in range(1,h-1):
            for i in range(1,w-1):
              pix2[i,j]=(1-4*tau)*pix1[i,j]+tau*(pix1[i+1,j]+pix1[i-1,j]+pix1[i,j+1]+pix1[i,j-1])

        pix1=imd.copy().load()

    return imd

#le tau doit etre supérieur à 1/4 par le principe de maximum

#------------------Exercice 3-------------------------
def EChaleurDiscrete(ims,n):
    imd=Image.new("F",ims.size)
    pix1=ims.load()
    pix2=imd.load()
    w=ims.size[0]
    h=ims.size[1]
    for k in range(n):
        for j in range(1,h-1):
            for i in range(1,w-1):
                w1=1./(1+abs(pix1[i,j]-pix1[i+1,j]))
                w2=1./(1+abs(pix1[i,j]-pix1[i-1,j]))
                w3=1./(1+abs(pix1[i,j]-pix1[i,j+1]))
                w4=1./(1+abs(pix1[i,j]-pix1[i,j-1]))

                pix2[i,j]=(w1*pix1[i+1,j]+w2*pix1[i-1,j]+w3*pix1[i,j+1]+w4*pix1[i,j-1])/(w1+w2+w3+w4)

        pix1=imd.copy().load()

    return imd


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: WangJin
# Email: wjwcan10482@163.com

import requests
import zipfile
import os
import shutil
print("downloading with requests")
url = r'https://video.mugglecode.com/problem2_files.zip'
r = requests.get(url)
with open("problem2_files.zip", "wb") as code:
    code.write(r.content)

azip = zipfile.ZipFile('problem2_files.zip')
azip.extractall()

path = r"C:\Users\wangjin\PycharmProjects\mugglecode\class02\problem2_files"
SrcImageDir = ['jpg', 'png', 'gif']
SrcDocDir = ['doc', 'docx', 'md', 'ppt']

if not os.path.exists(path+"\image"):
    os.makedirs(path+"\image")

if not os.path.exists(path+"\document"):
    os.makedirs(path+"\document")

for i in SrcImageDir:
    SrcImageUrl = os.path.join(path, i)
    files = os.listdir(SrcImageUrl)
    for f in files:
        fUrl = os.path.join(SrcImageUrl, f)  #join方式可以避免不通平台的转义问题！
        dUrl = os.path.join(path, "image", f)
        if not os.path.exists(dUrl):
            shutil.move(fUrl, dUrl)
    shutil.rmtree(SrcImageUrl)

for j in SrcDocDir:
    SrcDocDirUrl = os.path.join(path, j)
    files = os.listdir(SrcDocDirUrl)
    for f in files:
        fUrl = os.path.join(SrcDocDirUrl, f)
        dUrl = os.path.join(path, "document", f)
        if not os.path.exists(dUrl):
            shutil.move(fUrl, dUrl)
    shutil.rmtree(SrcDocDirUrl)


#!/usr/bin/env python

# Import the system library

import sys

# Import the RIOS rat library
from rios import rat

# Import the python Argument Parser
import argparse

# Import the numpy library
import numpy

# A function to add a color table
def addColourTable(imageFile):
    # Create a colour table (n,5) where
    # n is the number of classes to be
    # coloured. The data type must be
    # of type integer.

    ct = numpy.zeros([5,5], dtype = numpy.int)

    # Set 0 to be Dark Mustard Yellow.

    ct[0][0] = 0      # Pixel Val
    ct[0][1] = 139    # Red
    ct[0][2] = 139    # Green
    ct[0][3] = 0      # Blue
    ct[0][4] = 255    # Opacity

    # Set 1 to be Dark Olive Yellow.

    ct[0][0] = 1      # Pixel Val
    ct[0][1] = 162    # Red
    ct[0][2] = 205    # Green
    ct[0][3] = 90      # Blue
    ct[0][4] = 255    # Opacity

    # Set 2 to be Royal Blue.

    ct[0][0] = 2      # Pixel Val
    ct[0][1] = 72    # Red
    ct[0][2] = 118    # Green
    ct[0][3] = 255      # Blue
    ct[0][4] = 255    # Opacity

    # Set 3 to be Dark Sea Green.

    ct[0][0] = 3      # Pixel Val
    ct[0][1] = 180    # Red
    ct[0][2] = 238    # Green
    ct[0][3] = 180      # Blue
    ct[0][4] = 255    # Opacity

    # Set 4 to be Forest Green.

    ct[0][0] = 4      # Pixel Val
    ct[0][1] = 34    # Red
    ct[0][2] = 139    # Green
    ct[0][3] = 34      # Blue
    ct[0][4] = 255    # Opacity

    rat.setColorTable(imageFile, ct)

# This is the first part of the script to
# be executed.

if __name__ == '__main__':
    # Create the command line options
    # parser.
    parser = argparse.ArgumentParser()
    # Define the argument for specifying the input file.
    parser.add_argument("-i", "--input", type=str,
                        help="Specify the input image file.")

    # Call the parser to parse the arguments.
    args = parser.parse_args()

    # Check that the input parameter has been specified.
    if args.input == None:
        # Print an error message if not and exit
        print("Error: No input image file provided.")
        sys.exit()

    # Run the add colour table function
    addColourTable(args.input)


# Random Forest Classifier

import sys
import numpy as np
from sklearn import model_selection
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
from MNIST_Dataset_Loader.mnist_loader import MNIST
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')

print('Loading MNIST Data...')
# data = MNIST('./python-mnist/data/')

data = MNIST('./MNIST_Dataset_Loader/dataset/')

print('Loading Training Data...')
img_train, labels_train = data.load_training()
train_img = np.array(img_train)
train_labels = np.array(labels_train)

print('Loading Testing Data...')
img_test, labels_test = data.load_testing()
test_img = np.array(img_test)
test_labels = np.array(labels_test)

#Features
X = train_img

#Labels
y = train_labels

print('Preparing Classifier Training and Validation Data...')
X_train, X_test, y_train, y_test = model_selection.train_test_split(X,y,test_size=0.1)

print('Random Forest Classifier with n_estimators = 100, n_jobs = 10')
clf = RandomForestClassifier(n_estimators=100, n_jobs=10)
clf.fit(X_train,y_train)

print('Calculating Accuracy of trained Classifier...')
confidence = clf.score(X_test,y_test)

print('Making Predictions on Validation Data...')
y_pred = clf.predict(X_test)

print('Calculating Accuracy of Predictions...')
accuracy = accuracy_score(y_test, y_pred)

print('Creating Confusion Matrix...')
conf_mat = confusion_matrix(y_test,y_pred)

print('RFC Trained Classifier Confidence: ',confidence)
print('Predicted Values: ',y_pred)
print('Accuracy of Classifier on Validation Image Data: ',accuracy)
print('Confusion Matrix: \n',conf_mat)

# Plot Confusion Matrix Data as a Matrix
plt.matshow(conf_mat)
plt.title('Confusion Matrix for Validation Data')
plt.colorbar()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()

print('Making Predictions on Test Input Images...')
test_labels_pred = clf.predict(test_img)

print('Calculating Accuracy of Trained Classifier on Test Data... ')
acc = accuracy_score(test_labels,test_labels_pred)

print(' Creating Confusion Matrix for Test Data...')
conf_mat_test = confusion_matrix(test_labels,test_labels_pred)

print('Predicted Labels for Test Images: ',test_labels_pred)
print('Accuracy of Classifier on Test Images: ',acc)
print('Confusion Matrix for Test Data: \n',conf_mat_test)

# Plot Confusion Matrix for Test Data
plt.matshow(conf_mat_test)
plt.title('Confusion Matrix for Test Data')
plt.colorbar()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.axis('off')
plt.show()

# Show the Test Images with Original and Predicted Labels
a = np.random.randint(1,30,10)
for i in a:
	two_d = (np.reshape(test_img[i], (28, 28)) * 255).astype(np.uint8)
	plt.title('Original Label: {0}  Predicted Label: {1}'.format(test_labels[i],test_labels_pred[i]))
	plt.imshow(two_d, interpolation='nearest',cmap='gray')
	plt.show()
#------------------------- EOC -----------------------------


from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from schools.models import (
    Student, StudentGroup, StudentStudentGroupRelation
)
from common.models import Status, AcademicYear

class StudentSerializer(serializers.ModelSerializer):
    academic_year = serializers.PrimaryKeyRelatedField(
        queryset=AcademicYear.objects.all(), write_only=True)

    class Meta:
        model = Student
        fields = (
            'id', 'first_name', 'middle_name', 'last_name', 'uid', 'dob',
            'gender', 'mt', 'status', "institution", "academic_year"
        )
        extra_kwargs = {'academic_year': {'write_only': True}}

    def create(self, validated_data):
        studentgroup_id = self.context['view'].kwargs[
            'parent_lookup_studentgroups']
        status = validated_data.get('status', Status.ACTIVE)
        try:
            student_group = StudentGroup.objects.get(id=studentgroup_id)
        except:
            raise ValidationError(studentgroup_id + " not found.")

        academic_year = validated_data.pop('academic_year')
        student = Student.objects.create(**validated_data)
        student.save()

        StudentStudentGroupRelation.objects.get_or_create(
            student=student, student_group=student_group,
            status=status, academic_year=academic_year
        )
        return student

class StudentGroupSerializer(serializers.ModelSerializer):

    class Meta:
        model = StudentGroup
        fields = (
            'id', 'institution', 'name', 'section', 'status', 'group_type'
        )

class StudentStudentGroupSerializer(serializers.ModelSerializer):

    class Meta:
        model = StudentStudentGroupRelation
        fields = (
            'id', 'student', 'student_group', 'academic_year', 'status'
        )


from models.deep.cnn.cnn import CNN
from datamanager.recipemanager import Manager as RecipeManager
from datamanager.textmanager import Manager as TextMangaer
from datamanager.filemanager import Manager as FileManager
from models.lda import LdaModel
from websocketutil import send_message
from log import log_info

class NetWorks:
    def __init__(self, config, account_id, recipe_id, wsock=None):
        self.fma = FileManager(config, account_id)
        self.rma = RecipeManager(file_manager=self.fma)
        self.recipe_id = recipe_id
        self.recipe = self.rma.load_recipe(recipe_id=self.recipe_id)
        self.data_dir = self.fma.data_dir
        self.wsock = wsock

    def save_model_info(self, data_id, model_info, train_config, mapping_list):
        model_id = self.fma.generate_id()

        print("save model info")
        model_info["dataId"] = data_id
        model_info["recipeId"] = self.recipe_id
        model_info["train_config"] = train_config
        model_info["mapping"] = mapping_list

        r = self.fma.put_model_info(model_info, model_id)
        res = {
            "model_id": model_id,
            "model_info": r
        }
        print(res)
        return res

    def train(self, train_config, data_id, model_info):
        sorted_edges, layers_dict = self.rma.sort_layers()
        layer_info = {
            "layers_dict": layers_dict,
            "input": [],
            "deep": [],
            "layer": []
        }
        for layer_id in sorted_edges:
            layer = layers_dict[layer_id]
            layer_info[layer["type"]].append(layer)

        if len(layer_info["deep"]) > 3:
            res = self.deep_learning(train_config, data_id, model_info , layer_info)
        if len(layer_info["layer"]) > 1:
            res = self.learning(train_config, data_id, model_info, layer_info)

        res["action"] = "finishLearning"
        send_message(self.wsock, res)

    def deep_learning(self, train_config, data_id, model_info, layer_info):

        model = CNN(self.fma, self.rma, layer_info=layer_info)
        ratio = float(train_config["data"].get("ratio", 0.1))
        model.ima.load_data(self.fma.get_data_path(data_id), ratio=ratio)
        mapping_list = model.ima.create_mapping_list()
        info = self.save_model_info(data_id, model_info, train_config, mapping_list)
        model_id = info["model_id"]
        res = model.train(model_id, data_id, train_config, self.wsock)
        del model
        return res

    def send_progress(self, iter):
        res = {
            "action": "learning",
            "iter": iter,
            "nIter": self.n_iter,
            "id": self.model_id
        }
        send_message(self.wsock, res)

    def learning(self, train_config, data_id, model_info, layer_info):
        data_path = self.fma.get_data_path(data_id)

        ratio = float(train_config["data"].get("ratio", 0.1))
        self.tma = TextMangaer()
        self.tma.load_data(data_path, ratio=ratio)
        mapping_list = self.tma.create_mapping_list()

        info = self.save_model_info(data_id, model_info, train_config, mapping_list)
        self.model_id = info["model_id"]
        self.n_iter = 100
        self.send_progress(10)

        for layer in layer_info["layer"]:
            layer_name = layer["name"]
            if layer_name == "tokenize":

                self.tma.create_corpus(wsock=self.wsock)
                self.send_progress(20)
                frequencies = self.tma.create_frequencies(num=25)
                self.send_progress(30)

                model_path = self.fma.get_model_path(self.model_id)
                wordcloud_path = str(model_path / "wordcould.png")
                self.tma.word_clould(wordcloud_path)
                res = {
                    "action": "getWordCould",
                    "body": self.fma.encode_base64(open(wordcloud_path, "rb").read())
                }
                send_message(self.wsock, res)
                self.send_progress(40)

                res = {
                    "action": "getFrequencies",
                    "frequencies": frequencies
                }
                send_message(self.wsock, res)
                self.send_progress(50)

            elif layer_name == "LDA":
                num_topics = 20
                model = LdaModel(num_topics, self.tma.corpus, self.tma.dictionary)
                model.learning(passes=1)
                self.send_progress(60)

                topics = model.get_all_topic_words()
                self.send_progress(70)

                res = {
                    "action": "getTopicsDist",
                    "topics": topics
                }
                send_message(self.wsock, res)
                self.send_progress(80)
            elif layer_name == "logistic_regression":
                from sklearn.linear_model import LogisticRegression
                from sklearn.model_selection import train_test_split
                from sklearn.metrics import accuracy_score
                import seaborn as sns
                import matplotlib.pyplot as plt
                labels = [self.tma.labels[t.name] for t in self.tma.texts]
                matrix = model.get_doc_topic_matrix()
                data_train, data_test, label_train, label_test = train_test_split(matrix, labels, test_size=ratio, random_state=1)
                estimator = LogisticRegression(C=10)
                estimator.fit(data_train, label_train)
                label_predict = estimator.predict(data_test)
                accuracy = accuracy_score(label_test, label_predict)
                print(accuracy)
                from sklearn.metrics import confusion_matrix
                cm = confusion_matrix(label_test, label_predict)
                plt.figure(figsize=(9,9))
                sns.heatmap(cm, annot=True, fmt=".3f", linewidths=.5, square = True)#, cmap = 'Blues_r');
                plt.ylabel('Actual label');
                plt.xlabel('Predicted label');
                all_sample_title = 'Accuracy Score: {0}'.format(accuracy)
                plt.title(all_sample_title, size = 15);

                cm_path = str(model_path / "cm.png")
                plt.savefig(cm_path)
                res = {
                    "action": "getConfusionMatrix",
                    "body": self.fma.encode_base64(open(cm_path, "rb").read())
                }
                send_message(self.wsock, res)
                self.send_progress(100)
        return res

if __name__ == "__main__":
    recipe_id = "20180929154341"
    log_info("recipe id: {}".format(recipe_id))
    nw = NetWorks(recipe_id=recipe_id)

    data_id = "20180810185234"
    train_config =  {
        "learning_rate": 0.001,
        "batch_size": 64,
        "epoch": 5,
        "data": {
            "ratio": 0.1
        },
        "saver": {
            "evaluate_every": {
                "train": 10,
                "test": 30
            },
            "num_checkpoints": 5
        }
    }
    model_info = {}
    res = nw.train(train_config=train_config, data_id=data_id, model_info=model_info)
    log_info(res)


from django import template

register = template.Library()

@register.tag
def tolocale(parser, token):
    try:
        tag_name, objtrans, format_string = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError("%r tag requires exactly two arguments" % token.contents.split()[0])
    return CurrentLocaleNode(objtrans, format_string)

class CurrentLocaleNode(template.Node):
    def __init__(self, objtrans, format_string):
        self.format_string = template.Variable(format_string)
        self.objtrans = template.Variable(objtrans)
    def render(self, context):
        objtrans = self.objtrans.resolve(context)
        translang = self.format_string.resolve(context)
        return tolocale_filter(objtrans, translang)

@register.filter(name='tolocale')
def tolocale_filter(objtrans, translang):
    try:
        return objtrans.get_name(lang=translang)
    except AttributeError:
        if isinstance(objtrans, dict):
            return objtrans.get(translang, '')
        else:
            return objtrans


#!/usr/bin/env python3.7
# -*- coding: utf-8 -*-
import json
import logging
import os
from typing import *
from typing import IO #the * above won't load this

from edinmt import CONFIG
from edinmt import retagger
from edinmt.text_processors import TextProcessor

logger = logging.getLogger('edinmt.parse_marian')
logger.setLevel(CONFIG.LOG_LEVEL)
#only translation output should go on stdout so we remove StreamHandlers
handlers = logger.handlers.copy()
logger.handlers = [h for h in handlers if not isinstance(h, logging.StreamHandler)]

def parse(
        process_stdout: IO,
        n_items: Optional[int]=None,
        n_best: Optional[int]=1,
        mtout: Optional[IO]=None,
    ):
    r"""
    Convert Marian format into list of dicts, one dict per translation.
    If n-best sentences are used, each n-best is still its own dict.

    Args:
        process_stdout: an iterator over lines of string translations where
            each translation is a triple pipes delimited 4-tuple such as:
            "id ||| translation ||| F0 ||| score\n"
        n_items: the number of items you want to read from process_stdout
            (default=None reads all items from the process_stdout)
        n_best: each final output item should actually consist of n-best
            translations (default=1 means each final output is 1 translation)
        mtout: stream to write raw marian output to directly (optional)

    Returns:
        outputs: ordered list of dictionaries, one dict per translation item
    """
    batch = []
    count = 0
    n_best_count = 0
    item = {}

    nbest_translations = []
    for line in process_stdout:
        if mtout:
            mtout.write(line)

        try:
            decoded = line.decode('utf-8')
        except AttributeError:
            pass #it's already a string not bytes; no need to decode
        else:
            line = decoded

        n_best_count += 1
        if ' ||| ' in line: #n-best marian
            sent_id, translation, f0, score = line.split(' ||| ')
            nbest_translations.append(translation)
            if n_best_count == n_best:
                item = {
                    'id': count,
                    'translation': nbest_translations
                }
                batch.append(item) #FINISH ITEM
                nbest_translations = []
                count += 1
                n_best_count = 0
        else: #normal marian
            item = {
                'id': count,
                "translation": [line],
            }
            batch.append(item) #FINISH ITEM
            item = {}
            count += 1

        if count == n_items:
            break

    return batch

def parse_nbest_words(
        process_stdout: IO,
        n_items: Optional[int]=None,
        n_best: Optional[int]=1,
        mtout: Optional[IO]=None,
    ):
    r"""
    Convert Marian format into list of dicts, one dict per translation.
    The n-best tokens in each position (decoder from Alham), get added
    to the translation dict as a list of "n_best_words" dicts.

    Args:
        process_stdout: an iterator over lines of string translations where
            each translation is a triple pipes delimited 4-tuple such as
            "id ||| translation ||| F0 ||| score\n"
            and new-line separated n-best tokens underneath, such as
            "tok1 ||| tok1 score tok2 score\n"
            and an empty line afterwards, delimiting the translation
        n_items: the number of items you want to read from process_stdout
            (default=None reads all items from the process_stdout)
        n_best: each final output item should actually consist of n-best
            translations (default=1 means each final output is 1 translation)
        mtout: stream to write raw marian output to directly (optional)

    Returns:
        outputs: ordered list of dictionaries, one dict per translation item

    NOTE: n-best words decoder output available using Alham's decoder:
    https://github.com/afaji/Marian/tree/alt-words
    """
    batch = []
    count = 0
    n_best_count = 0
    parsing = False

    transl = ""
    nbestw = []
    translation = []
    nbest_words = []
    for line in process_stdout:
        if mtout:
            mtout.write(line)

        try:
            decoded = line.decode('utf-8')
        except AttributeError:
            pass #it's already a string not bytes; no need to decode
        else:
            line = decoded
        line = line.strip()

        if line:
            parsing = True
            try:
                sent_id, sent, f0, score = line.split(' ||| ')
            except ValueError:
                token, rest = line.split('|||')
                nbests = rest.split()
                d = {}
                for r in range(0, len(nbests), 2):
                    d[nbests[r]] = nbests[r+1]
                nbestw.append(d)
                if token != '</s> ':
                    transl += token
            else:
                continue

        elif not line and parsing:
            translation.append(transl.strip())
            nbest_words.append(nbestw)
            transl = ""
            nbestw = []
            n_best_count += 1
            parsing = False
            if n_best_count == n_best:
                item = {
                    'id': count,
                    'translation': translation,
                    'nbest_words': nbest_words
                }
                batch.append(item) #FINISH ITEM
                translation = []
                nbest_words = []
                count += 1
                n_best_count = 0

        elif not line and not parsing:
            continue #skip 2nd empty line

        if count == n_items:
            break

    if parsing: #last one can be missed if there's no empty line at eof
        translation.append(transl.strip())
        nbest_words.append(nbestw)
        item = {
            'id': count,
            'translation': translation,
            'nbest_words': nbest_words
        }
        batch.append(item) #FINISH ITEM

    return batch

def unwrap_lines(
        parsed: list,
        true_ids: list,
        text_processor: Optional[TextProcessor]=None,
        empties: Optional[set]=None,
        tagged: Optional[dict]=None,
        n_best: Optional[int]=1,
        expand: Optional[bool]=True
    ):
    """
    Join together parsed items that were created from a single original item
    that got split into multiple lines in preprocessing. Postprocess the
    translations. Items that used to be empty can be blanked out using
    a set of empty ids (original line numbers from the original text).
    Items that had text snippets extracted and replaced with [TAG] tokens can
    have text reinserted in place of the [TAG] token using the tagged dict.

    Warning! Entire parsed output will be held in memory. If this is
    a problem, please provide batches of parsed items instead.

    Args:
        parsed: a batch of items as output by parse or parse_nbest_words
        true_ids: true ids for each item in parsed
        text_processor: TextProcessor to .postprocess() text with
        empties: original ids that should be blanked out
        tagged: line numbers to tuples of ([TAG], text)
        n_best: number of n-best sentences (>1 if marian's --n-best was used)
        expand: expand n-best items into their own item
    """
    final = []

    collated = {}
    true_ids_iter = iter(true_ids)
    last_true_id = 0

    #combine the split parsed item into one item with a list of split outputs
    #e.g. for a sentence with pieceN_topN we have a transformation of:
    #[p0_t1, p0_t2, p1_t1, p1_t1] -> [[p0_t1, p0_t2], [p1_t1, p1_t1]]
    for item in parsed:
        true_id = int(next(true_ids_iter))
        if true_id not in collated:
            collated[true_id] = {'id': true_id, 'translation': []}
            if 'nbest_words' in item:
                collated[true_id]['nbest_words'] = []

        collated[true_id]['translation'].append(item['translation'])
        if 'nbest_words' in item:
            collated[true_id]['nbest_words'].append(item['nbest_words'])
        last_true_id = true_id

    #now combine the n-best sentences into one item, and also debpe, etc.
    #e.g. for a sentence with pieceN_topN we have a transformation of:
    #[[p0_t1, p0_t2], [p1_t1, p1_t1]] -> [p0_t1_p1_t1, p0_t2_p1_t2]
    for idx in collated:
        item = collated[idx]
        if empties and item['id'] in empties: #blank out hallucinations
            item['translation'] = ['']*n_best
            if 'nbest_words' in item:
                item['nbest_words'] = [[]]*n_best
        else:
            transl = list(zip(*item['translation']))
            all_translations = []
            for tup in transl:
                translation = ' '.join([t.strip() for t in tup])
                if text_processor:
                    #debpe, detruecase, etc.
                    translation = text_processor.postprocess(translation).strip()
                    if tagged and item['id'] in tagged:
                        tags = tagged[item['id']]
                        #reinsert urls, emails, etc. that we had passed through
                        translation = retagger.reinsert_tags(translation, tags)
                all_translations.append(translation)
            item['translation'] = all_translations

            if 'nbest_words' in item:
                nbestw = list(zip(*item['nbest_words']))
                all_nbest_words = []
                for tup in nbestw:
                    nbest_words = []
                    for l in tup:
                        for i in l:
                            nbest_words.append(i)
                    all_nbest_words.append(nbest_words)
                item['nbest_words'] = all_nbest_words

        #now separate the n-best sentences into their own separate json-lines
        #so we have json-line per n-best instead of n-bests inside 1 json
        if expand:
            for i in range(len(item['translation'])):
                tmp_item = {
                    'id': item['id'],
                    'translation': item['translation'][i]
                }
                if 'nbest_words' in item:
                    tmp_item['nbest_words'] = item['nbest_words'][i]
                final.append(tmp_item)
        else:
            final.append(item)

    return final

def fmt_item(item, fmt):
    r"""Format the item to the correct output format (choices: CONFIG.FMTS)."""
    if fmt == 'json':
        text = json.dumps(item, ensure_ascii=False, sort_keys=True)
    elif fmt == 'marian':
        text = f"{item['id']} ||| {item['translation'].strip()}"
    elif fmt == 'text':
        text = f"{item['translation'].strip()}"
    return text


## this script creates a token and send it to NEST API. It requires POSTMAN.
# First the GNG of the building has to be created then it can relate the signal received from sensors to the corresponding node of the GNG

def run_main():

    import nest
    import sys
    import matrix_builder
    import xlsxwriter
    import csv

    graph = {'1': ['2', '8'],
                 '2': ['1', '3','0'],
                 '3': ['2','4','10'],
                 '4': ['5','3'],
                 '5': ['4','6','11'],
                 '6': ['5', '7','0'],
                 '7': ['6','8','12'],
                 '8': ['1', '9','7'],
                 '9': ['0','8'],
                 '0': ['2', '6','9'],
                 '10':['3'],
                 '11': ['5'],
                 '12': ['7']}

    client_id = '892e4495-b87a-494e-b2b4-c56b2cf01d57'
    client_secret = 'UzCWRlbKnKFZyRwaClDOKrhek'
    access_token_cache_file = 'nest.json'

    napi = nest.Nest(client_id=client_id, client_secret=client_secret, access_token_cache_file=access_token_cache_file)

    napi = nest.Nest(client_id=client_id, client_secret=client_secret, access_token_cache_file=access_token_cache_file)

    if napi.authorization_required:
        print('Go to ' + napi.authorize_url + ' to authorize, then enter PIN below')
        if sys.version_info[0] < 3:
            pin = raw_input("PIN: ")
        else:
            pin = input("PIN: ")
        napi.request_token(pin)

    for structure in napi.structures:
        print ('Structure %s' % structure.name)
        print ('    Away: %s' % structure.away)
        print ('    Devices:')

    act_sensor=[]
    for device in structure.thermostats:
        print ('        Device: %s' % device.name)
        print ('            Temp: %0.1f' % device.temperature)
        if device.temperature > 70.0:
            act_sensor = device.name

    act_sensor=act_sensor[-7:]
    act_sensor=act_sensor[:6]

    if act_sensor == '332503':
        room_id=3
    if act_sensor == '332595':
        room_id=1
    if act_sensor == '332755':
        room_id=7

    with open('fire_point.csv', 'wb') as csvfile2:
        spamwriter = csv.writer(csvfile2, delimiter=',',
                                quotechar='|', quoting=csv.QUOTE_MINIMAL)
        spamwriter.writerow(room_id)

    with open('startPoint.csv') as csvfile1:
        readCSV = csv.reader(csvfile1, delimiter=',')

        for row in readCSV:
            start_room=row

    print(start_room[0])

    print(room_id)
    all_paths=matrix_builder.find_all_paths(graph, start_room[0], '0', path=[])

    print(all_paths)

    #workbook = xlsxwriter.Workbook('demo.xlsx')
    #worksheet = workbook.add_worksheet()

    final_path=[[] for i in range (20)]
    l1=len(all_paths)

    j=0
    for i in range(0,l1,1):
        if str(room_id) not in all_paths[i] :
            final_path[j]=all_paths[i]

            j=j+1

    final_path=final_path[:j]

    print (final_path)
    final_cord=final_path

    row_point=[[] for i in range (12)]

    with open('spaceCenters.csv') as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')

        p=0
        for row in readCSV:
            row_point[p]=row
            p=p+1

    for k in range (j):
        n=0
        for m in final_path[k]:
            final_cord[k][n]=row_point[int(m)-1]
            n=n+1

    for r in range(len(final_cord)):
        final_cord[r]=final_cord[r][:-1]

    print(final_cord)

    kk=0
    for ii in range(len(final_cord)):
        for jj in range(len(final_cord[ii])):
            final_cord [ii][jj]= map(float, final_cord[ii][jj])
            final_cord[ii][jj]=map(lambda x: x + 0.5*ii, final_cord[ii][jj])
            kk=kk+1
    trace_list = [[] for pp in range (kk)]

    kk = 0
    for ii in range(len(final_cord)):

        for jj in range(len(final_cord[ii]) - 1):

            trace_list[kk]=final_cord[ii][jj] + final_cord[ii][jj + 1]
            kk=kk+1

    print(trace_list)

    with open('trace.csv', 'wb') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',',
                                quotechar='|', quoting=csv.QUOTE_MINIMAL)
        for jj in range (kk):
            spamwriter.writerow(trace_list[jj])

        #spamwriter.writerow(final_cord[0][0])
        #spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
        #spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
    #print(readCSV)

    #for row in readCSV:
        #print(row)
        #print(row[0])
        #print(row[0],row[1],row[2],)

    #l1=len(all_paths)
    #for i in range(0,l1,1):
        #l2=len(all_paths[i])
        #for j in range (0,l2-1,1):
            #worksheet.write(i,j,all_paths[i][j])

    #workbook.close()


# uncompyle6 version 3.7.4
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-x86_64/egg/mercury_agent/procedures/misc.py
# Compiled at: 2018-01-10 00:48:14
# Size of source mod 2**32: 3292 bytes
import logging, subprocess
from mercury_agent.capabilities import capability
from mercury.common.helpers.cli import run
from mercury.common.helpers.util import download_file
log = logging.getLogger(__name__)

@capability('echo', 'Echo something to the console', num_args=1)
def echo(message):
    """
    Echo the dolphin
    :param message: message to Echo
    :return: None
    """
    log.info('Echo: %s' % message)
    print(message)
    return message

@capability('run', 'Run an arbitrary command', num_args=1)
def runner(command, _input=''):
    """
    Run a shell command
    :param command: The shell command to use
    :param _input: Optional data to pass to stdin
    :return:
    """
    log.info('Running: %s' % command)
    r = run(command, ignore_error=True, raise_exception=False, _input=_input)
    return {'stdout':r.stdout,
     'stderr':r.stderr,
     'returncode':r.returncode}

@capability('run_async', 'Run a command in the background', num_args=1)
def runner_async(command, shell=True):
    """

    :param command:
    :param shell:
    :return:
    """
    subprocess.Popen(('{}'.format(command)), shell=shell)

@capability('kexec', 'kexec into kernel at supplied location', kwarg_names=['kernel', 'initrd', 'options'], no_return=True,
  serial=True)
def kexec(kernel='', initrd='', options=None, kernel_type='bzImage'):
    """
    Kexec into a kernel
    """
    options = options or []
    command = 'kexec --type {kernel_type} --initrd={initrd} --append="{options}" {kernel}'.format(kernel_type=kernel_type,
      initrd=initrd,
      options=(' '.join(options)),
      kernel=kernel)
    log.info('Running Kexec: {}'.format(command))
    runner_async('sleep 5;' + command)

@capability('reload', 'kexec into current preboot kernel, re-downloading the root file system', num_args=2)
def reload(kernel_url, initrd_url):
    """
    Reload the environment
    """
    kernel_file = '/tmp/vmlinuz'
    initrd_file = '/tmp/initrd'
    log.info('Downloading: {}'.format(kernel_url))
    download_file(kernel_url, kernel_file)
    log.info('Downloading: {}'.format(initrd_url))
    download_file(initrd_url, initrd_file)
    with open('/proc/cmdline') as (fp):
        options = fp.readline().split()
    kexec(kernel=kernel_file, initrd=initrd_file, options=options)


# Посчитайте, сколько раз символ встречается в строке. Строка и символ вводятся с клавиатуры.DON’T USE METHOD COUNT

x = str(input("Enter the symbol: "))
string = str(input("Enter the String: "))

def count_x(string):
    result = 0
    for i in string:
        if x == i:
            result += 1
    return result

print(count_x(string))