def plot_test_prediction(target, predictions, model_name, data_desc,
                         error_func):
    save_plot = options.save_plot
    if save_plot:
        pp_path = get_path(options, "prediction_%s" % data_desc, "pdf")
        pp = PdfPages(pp_path)
    error = concat_and_calc_error(predictions, target, error_func)
    if np.any(target == 0):
        mapestr = "no MAPE due to 0's in observations"
    else:
        mapestr = 'MAPE: %3.2f' % concat_and_calc_error(
            predictions, target, sg.utils.mape)
    title = 'Test data prediction, %s, %s, %i days, model error: ' \
      '%3.2f, RMSE: %3.2f, %s' % \
      (model_name, data_desc, len(predictions), error,
       concat_and_calc_error(predictions, target, Oger.utils.rmse), mapestr)
    print "%s." % title
    plt.figure()
    plt.title(title)
    plot_target_predictions(target, predictions)
    if save_plot:
        pp.savefig()
        pp.close()
    if not options.no_show_plot:
        plt.ioff()  # In case --live-plot is chosen, since that sets ion().
        plt.show()
def plot_test_prediction(target, predictions, 
                         model_name, data_desc, error_func):
    save_plot = options.save_plot
    if save_plot:
        pp_path = get_path(options, "prediction_%s" % data_desc, "pdf")
        pp = PdfPages(pp_path)
    error = concat_and_calc_error(predictions, target, error_func)
    if np.any(target == 0):
        mapestr = "no MAPE due to 0's in observations"
    else:
        mapestr = 'MAPE: %3.2f' % concat_and_calc_error(predictions, target, 
                                              sg.utils.mape)
    title = 'Test data prediction, %s, %s, %i days, model error: ' \
      '%3.2f, RMSE: %3.2f, %s' % \
      (model_name, data_desc, len(predictions), error, 
       concat_and_calc_error(predictions, target, Oger.utils.rmse), mapestr)
    print "%s." % title
    plt.figure()
    plt.title(title)
    plot_target_predictions(target, predictions)
    if save_plot:
        pp.savefig()
        pp.close()
    if not options.no_show_plot:
        plt.ioff() # In case --live-plot is chosen, since that sets ion().
        plt.show()
def _run_models(models, dataset):
    data_desc = dataset.desc
    for model in models:
        if not is_mpi_slave(options):
            print "Optimizing parameters for model", model.name, "."
        run_GA(model, options)
        if is_mpi_slave(options):
            continue
        raw_genes = pu.raw_genes(model.genome, strip=True)
        print "Best genes found during evolution: ", raw_genes
        target, predictions = test_genome(raw_genes, model)
        _save_test_prediction(target, predictions, data_desc)
        error = concat_and_calc_error(predictions, target, model.error_func)
        print "Error on test phase for best genome found, " \
          "%s, %i days: %5.4f" % (data_desc, len(predictions), error)
        if not options.no_plot:
            plot_test_prediction(target, predictions, model.name, data_desc,
                                 model.error_func)
def _run_models(models, dataset):
    data_desc = dataset.desc
    for model in models:
        if not is_mpi_slave(options):
            print "Optimizing parameters for model", model.name, "."
        run_GA(model, options)
        if is_mpi_slave(options):
            continue
        raw_genes = pu.raw_genes(model.genome, strip=True)
        print "Best genes found during evolution: ", raw_genes
        target, predictions = test_genome(raw_genes, model)
        _save_test_prediction(target, predictions, data_desc)
        error = concat_and_calc_error(predictions, target, model.error_func)
        print "Error on test phase for best genome found, " \
          "%s, %i days: %5.4f" % (data_desc, len(predictions), error)
        if not options.no_plot:
            plot_test_prediction(target, predictions, 
                                 model.name, data_desc, model.error_func)