def plot_objectives(nnet, xp_path, title_suffix, xlabel, file_prefix):
    """
    plot train, validation, and test objective in a combined plot
    """

    objectives = OrderedDict([("train", nnet.diag['train']['objective'])])
    if nnet.data.n_val > 0:
        objectives["val"] = nnet.diag['val']['objective']
    objectives["test"] = nnet.diag['test']['objective']

    plot_line(objectives, title="Objective " + title_suffix, xlabel=xlabel, ylabel="Objective", log_scale=True,
              export_pdf=(xp_path + "/" + file_prefix + "obj"))
def plot_accuracy(nnet, xp_path, title_suffix, xlabel, file_prefix):
    """
    plot accuracy of train, val, and test set per epoch.
    """

    acc = OrderedDict([("train", nnet.diag['train']['acc'])])
    if nnet.data.n_val > 0:
        acc["val"] = nnet.diag['val']['acc']
    acc["test"] = nnet.diag['test']['acc']

    plot_line(acc, title="Accuracy " + title_suffix, xlabel=xlabel, ylabel="Accuracy (%) ", y_min=-5, y_max=105,
              export_pdf=(xp_path + "/" + file_prefix + "accuracy"))
def plot_center_c_diagnostics(nnet, xp_path, title_suffix, xlabel, file_prefix):
    """
    plot the norm of center c and the means of the outputs on the train, val, and test set per epoch
    """

    norms = OrderedDict([("c", nnet.diag['network']['c_norm']),
                         ("mean_train", nnet.diag['train']['output_mean_norm'])])
    if nnet.data.n_val > 0:
        norms["mean_val"] = nnet.diag['val']['output_mean_norm']
    norms["mean_test"] = nnet.diag['test']['output_mean_norm']

    plot_line(norms, title="Norms in output space " + title_suffix, xlabel=xlabel, ylabel="norm values ",
              log_scale=True, export_pdf=(xp_path + "/" + file_prefix + "c_mean_norms"))

    diffs = OrderedDict([("train", nnet.diag['train']['c_mean_diff'])])
    if nnet.data.n_val > 0:
        diffs["val"] = nnet.diag['val']['c_mean_diff']
    diffs["test"] = nnet.diag['test']['c_mean_diff']

    plot_line(diffs, title="Distance from c to means " + title_suffix, xlabel=xlabel, ylabel="distance ",
              log_scale=True, export_pdf=(xp_path + "/" + file_prefix + "c_mean_diffs"))
def plot_objective_with_parts(nnet, xp_path, title_suffix, xlabel, file_prefix, pretrain=False):
    """
    plot train, validation, and test objective (and their parts)
    """

    for which_set in ['train', 'val', 'test']:

        if (which_set == 'val') & (nnet.data.n_val == 0):
            continue

        # Plot objective (and its parts)
        objective = OrderedDict([("objective", nnet.diag[which_set]['objective']),
                                 ("emp. loss", nnet.diag[which_set]['emp_loss']),
                                 ("l2 penalty", nnet.diag['network']['l2_penalty'])])
        if Cfg.reconstruction_penalty and not pretrain:
            objective["reconstruction penalty"] = nnet.diag[which_set]['reconstruction_penalty']
        if Cfg.svdd_loss and not pretrain:
            objective["R"] = nnet.diag['network']['R']

        title = which_set.title() + " objective " + title_suffix

        plot_line(objective, title=title, xlabel=xlabel, ylabel="Objective", log_scale=True,
                  export_pdf=(xp_path + "/" + file_prefix + "obj_" + which_set))
def plot_parameter_norms(nnet, xp_path, title_suffix, xlabel, file_prefix):
    """
    plot norms of network parameters (and parameter updates)
    """

    # plot norms of parameters for each unit of dense layers
    params = OrderedDict()

    n_layer = 0
    for layer in nnet.trainable_layers:
        if layer.isdense:
            for unit in range(layer.num_units):
                name = "W" + str(n_layer + 1) + str(unit + 1)
                params[name] = nnet.diag['network']['W_norms'][n_layer][unit, :]
                if layer.b is not None:
                    name = "b" + str(n_layer + 1) + str(unit + 1)
                    params[name] = nnet.diag['network']['b_norms'][n_layer][unit, :]
            n_layer += 1

    plot_line(params, title="Norms of network parameters " + title_suffix, xlabel=xlabel, ylabel="Norm", log_scale=True,
              export_pdf=(xp_path + "/" + file_prefix + "param_norms"))

    # plot norms of parameter differences between updates for each layer
    params = OrderedDict()

    n_layer = 0
    for layer in nnet.trainable_layers:
        if layer.isdense | layer.isconv:
            name = "dW" + str(n_layer + 1)
            params[name] = nnet.diag['network']['dW_norms'][n_layer]
            if layer.b is not None:
                name = "db" + str(n_layer + 1)
                params[name] = nnet.diag['network']['db_norms'][n_layer]
            n_layer += 1

    plot_line(params, title="Absolute differences of parameter updates " + title_suffix, xlabel=xlabel, ylabel="Norm",
              log_scale=True, export_pdf=(xp_path + "/" + file_prefix + "param_diff_norms"))
def plot_auc(nnet, xp_path, title_suffix, xlabel, file_prefix):
    """
    plot auc time series of train, val, and test set.
    """

    auc = OrderedDict()

    for which_set in ['train', 'val', 'test']:

        if (which_set == 'val') & (nnet.data.n_val == 0):
            continue

        if which_set == 'train':
            y = nnet.data._y_train
        if which_set == 'val':
            y = nnet.data._y_val
        if which_set == 'test':
            y = nnet.data._y_test

        if sum(y) > 0:
            auc[which_set] = nnet.diag[which_set]['auc']

    plot_line(auc, title="AUC " + title_suffix, xlabel=xlabel, ylabel="AUC", y_min=-0.05, y_max=1.05,
              export_pdf=(xp_path + "/" + file_prefix + "auc"))