def entrenar():
    cur = conn.cursor()
    cur.execute("SELECT * FROM songs")
    rv = cur.fetchall()

    inputsTrain = np.empty([len(rv), 36])
    outputsTrain = np.empty([len(rv), 4])

    def eval_fitness(genomes):
        for g in genomes:
            net = nn.create_feed_forward_phenotype(g)
            sum_square_error = 0.0
            for inputs, expected in zip(inputsTrain, outputsTrain):
                output = net.serial_activate(inputs)
                sum_square_error += np.mean((output - expected) ** 2)
            g.fitness = -sum_square_error

    for i in range(0, len(rv)):
        dict = common.loadDict(os.path.join(common.load("data_dir"), rv[i]["data"]))
        arr = common.featureDictToArray(dict)
        inputsTrain[i, :] = arr
        temp = np.zeros(4)
        temp[int(rv[i]["genre"]) - 1] = 1
        outputsTrain[i, :] = temp

    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, "neuron.config")
    pop = population.Population(config_path)
    reporte = NEATReporter()
    pop.add_reporter(reporte)
    pop.run(eval_fitness, 500)

    stats = pop.statistics
    winner = pop.statistics.best_genome()
    pickle.dump(winner, open(os.path.join(common.load("data_dir"), "redNeuronal.p"), "w"))
    visualize.plot_stats(stats, filename="app/static/results/evolucion.svg")
    visualize.plot_species(stats)
    visualize.draw_net(winner, filename="app/static/results/redNeuronal.svg")

    return winner
from app import app
from app import ALLOWED_EXTENSIONS
from app.dbconnect import conn
from app.audio.audioClass import Audio
from app.audio import feature
from app import common
from app.train import NEAT
from flask import render_template, request, redirect
from flask import url_for, send_from_directory
from werkzeug.utils import secure_filename
import os
import threading


cur = conn.cursor()
ANALYSIS_WINDOW = 512       # Ventana de analisis de 512 muestras
HOPSIZE = 256
TEXTURE_WINDOW = 86         # Nro de ventanas de analisis
NRO_TEXTURE_WINDOWS = 2584  # 30 segundos aprox.


def daemon():
    NEAT.entrenar()

d = threading.Thread(target=daemon, name='Daemon')


@app.route('/verResultados', methods=['GET'])
def verRed():
    return render_template('estadistica.html')