code: def plot_f1_vs_epoch(
      results, stats_graph_folder, metric, parameters, from_json=False
  ):
      """
      Takes results dictionary and saves the f1 vs epoch plot in stats_graph_folder.
      from_json indicates if the results dictionary was loaded from results.json file.
      In this case, dictionary indexes are mapped from string to int.
  
      metric can be f1_score or accuracy
      """
  
      assert metric in ["f1_score", "accuracy_score", "f1_conll"]
  
      if not from_json:
          epoch_idxs = sorted(results["epoch"].keys())
      else:
          epoch_idxs = sorted(
              map(int, results["epoch"].keys())
          )  # when loading json file
  
      dataset_types = []
      for dataset_type in ["train", "valid", "test"]:
          if dataset_type in results["epoch"][epoch_idxs[0]][-1]:
              dataset_types.append(dataset_type)
      if len(dataset_type) < 2:
          return
  
      f1_dict_all = {}
      for dataset_type in dataset_types:
          f1_dict_all[dataset_type] = []
      for eidx in epoch_idxs:
          if not from_json:
              result_epoch = results["epoch"][eidx][-1]
          else:
              result_epoch = results["epoch"][str(eidx)][
                  -1
              ]  # when loading json file
          for dataset_type in dataset_types:
              f1_dict_all[dataset_type].append(
                  result_epoch[dataset_type][metric]
              )
  
      # Plot micro f1 vs epoch for all classes
      plt.figure()
      plot_handles = []
      f1_all = {}
      for dataset_type in dataset_types:
          if dataset_type not in results:
              results[dataset_type] = {}
          if metric in ["f1_score", "f1_conll"]:
              f1 = [
                  f1_dict["micro"]
                  for f1_dict in f1_dict_all[dataset_type]
              ]
          else:
              f1 = [
                  score_value
                  for score_value in f1_dict_all[dataset_type]
              ]
          results[dataset_type]["best_{0}".format(metric)] = max(f1)
          results[dataset_type][
              "epoch_for_best_{0}".format(metric)
          ] = int(np.asarray(f1).argmax())
          f1_all[dataset_type] = f1
          plot_handles.extend(
              plt.plot(
                  epoch_idxs,
                  f1,
                  "-",
                  label=dataset_type
                  + " (max: {0:.4f})".format(
                      results[dataset_type]["best_{0}".format(metric)]
                  ),
              )
          )
      # Record the best values according to the best epoch for valid
      best_epoch = results["valid"]["epoch_for_best_{0}".format(metric)]
      plt.axvline(
          x=best_epoch, color="k", linestyle=":"
      )  # Add a vertical line at best epoch for valid
      for dataset_type in dataset_types:
          best_score_based_on_valid = f1_all[dataset_type][best_epoch]
          results[dataset_type][
              "best_{0}_based_on_valid".format(metric)
          ] = best_score_based_on_valid
          if dataset_type == "test":
              plot_handles.append(
                  plt.axhline(
                      y=best_score_based_on_valid,
                      label=dataset_type
                      + " (best: {0:.4f})".format(
                          best_score_based_on_valid
                      ),
                      color="k",
                      linestyle=":",
                  )
              )
          else:
              plt.axhline(
                  y=best_score_based_on_valid,
                  label="{0:.4f}".format(best_score_based_on_valid),
                  color="k",
                  linestyle=":",
              )
      title = "{0} vs epoch number for all classes\n".format(metric)
      xlabel = "epoch number"
      ylabel = metric
      plt.title(title)
      plt.xlabel(xlabel)
      plt.ylabel(ylabel)
      plt.legend(handles=plot_handles, loc=0)
      plt.savefig(
          os.path.join(
              stats_graph_folder,
              "{0}_vs_epoch_for_all_classes.{1}".format(
                  metric, parameters["plot_format"]
              ),
          )
      )
      plt.close()
  
file_path: NeuroNER/neuroner/evaluate.py
imports: import json
  import os
  import pkg_resources
  import time
  import numpy
  import matplotlib.pyplot
  import sklearn.metrics
  from neuroner import utils_plots
  from neuroner import utils_nlp
label_desc: Use image node_id 3 for calling a global function with example usage: plot_f1_vs_epoch(results, stats_graph_folder, metric, parameters, from_json) without return types