class_docstr: Token Classification Model with BERT, applicable for tasks such as Named Entity Recognition
class_docstr_tok: ["Token","Classification","Model","with","BERT",",","applicable","for","tasks","such","as","Named","Entity","Recognition"]
docstr: Run inference on data from a file, plot confusion matrix and calculate classification report.
  Use this method for final evaluation.
  
  Args:
      output_dir: path to output directory to store model predictions, confusion matrix plot (if set to True)
      text_file: path to file with text. Each line of the text.txt file contains text sequences, where words
          are separated with spaces: [WORD] [SPACE] [WORD] [SPACE] [WORD]
      labels_file (Optional): path to file with labels. Each line of the labels_file should contain
          labels corresponding to each word in the text_file, the labels are separated with spaces:
          [LABEL] [SPACE] [LABEL] [SPACE] [LABEL] (for labels.txt).'
      add_confusion_matrix: whether to generate confusion matrix
      normalize_confusion_matrix: whether to normalize confusion matrix
      batch_size: batch size to use during inference.
docstr_tok: ["Run","inference","on","data","from","a","file",",","plot","confusion","matrix","and","calculate","classification","report",".","Use","this","method","for","final","evaluation",".","Args",":","output_dir",":","path","to","output","directory","to","store","model","predictions",",","confusion","matrix","plot","(","if","set","to","True",")","text_file",":","path","to","file","with","text",".","Each","line","of","the","text.txt","file","contains","text","sequences",",","where","words","are","separated","with","spaces",":","[","WORD","]","[","SPACE","]","[","WORD","]","[","SPACE","]","[","WORD","]","labels_file","(","Optional",")",":","path","to","file","with","labels",".","Each","line","of","the","labels_file","should","contain","labels","corresponding","to","each","word","in","the","text_file",",","the","labels","are","separated","with","spaces",":","[","LABEL","]","[","SPACE","]","[","LABEL","]","[","SPACE","]","[","LABEL","]","(","for","labels.txt",")",".","'","add_confusion_matrix",":","whether","to","generate","confusion","matrix","normalize_confusion_matrix",":","whether","to","normalize","confusion","matrix","batch_size",":","batch","size","to","use","during","inference","."]
code: def evaluate_from_file(
      self,
      output_dir: str,
      text_file: str,
      labels_file: Optional[str] = None,
      add_confusion_matrix: Optional[bool] = False,
      normalize_confusion_matrix: Optional[bool] = True,
      batch_size: int = 1,
  ) -> None:
      """
      Run inference on data from a file, plot confusion matrix and calculate classification report.
      Use this method for final evaluation.
  
      Args:
          output_dir: path to output directory to store model predictions, confusion matrix plot (if set to True)
          text_file: path to file with text. Each line of the text.txt file contains text sequences, where words
              are separated with spaces: [WORD] [SPACE] [WORD] [SPACE] [WORD]
          labels_file (Optional): path to file with labels. Each line of the labels_file should contain
              labels corresponding to each word in the text_file, the labels are separated with spaces:
              [LABEL] [SPACE] [LABEL] [SPACE] [LABEL] (for labels.txt).'
          add_confusion_matrix: whether to generate confusion matrix
          normalize_confusion_matrix: whether to normalize confusion matrix
          batch_size: batch size to use during inference.
      """
      output_dir = os.path.abspath(output_dir)
  
      with open(text_file, "r") as f:
          queries = f.readlines()
  
      all_preds = self._infer(queries, batch_size)
      with_labels = labels_file is not None
      if with_labels:
          with open(labels_file, "r") as f:
              all_labels_str = f.readlines()
              all_labels_str = " ".join(
                  [labels.strip() for labels in all_labels_str]
              )
  
      # writing labels and predictions to a file in output_dir is specified in the config
      os.makedirs(output_dir, exist_ok=True)
      filename = os.path.join(
          output_dir, "infer_" + os.path.basename(text_file)
      )
      try:
          with open(filename, "w") as f:
              if with_labels:
                  f.write("labels\t" + all_labels_str + "\n")
                  logging.info(f"Labels save to {filename}")
  
              # convert labels from string label to ids
              ids_to_labels = {
                  v: k for k, v in self._cfg.label_ids.items()
              }
              all_preds_str = [
                  ids_to_labels[pred] for pred in all_preds
              ]
              f.write("preds\t" + " ".join(all_preds_str) + "\n")
              logging.info(f"Predictions saved to {filename}")
  
          if with_labels and add_confusion_matrix:
              all_labels = all_labels_str.split()
              # convert labels from string label to ids
              label_ids = self._cfg.label_ids
              all_labels = [label_ids[label] for label in all_labels]
  
              plot_confusion_matrix(
                  all_labels,
                  all_preds,
                  output_dir,
                  label_ids=label_ids,
                  normalize=normalize_confusion_matrix,
              )
              logging.info(
                  get_classification_report(
                      all_labels, all_preds, label_ids
                  )
              )
      except Exception:
          logging.error(
              f"When providing a file with labels, check that all labels in {labels_file} were"
              f"seen during training."
          )
          raise
  
imports: import os
  from typing import List, Optional, Union
  import torch
  from omegaconf import DictConfig, OmegaConf
  from pytorch_lightning import Trainer
  from torch.utils.data import DataLoader
  from nemo.collections.common.losses import CrossEntropyLoss
  from nemo.collections.nlp.data.data_utils.data_preprocessing import get_labels_to_labels_id_mapping
  from nemo.collections.nlp.data.token_classification.token_classification_dataset import BertTokenClassificationDataset, BertTokenClassificationInferDataset
  from nemo.collections.nlp.data.token_classification.token_classification_utils import get_label_ids
  from nemo.collections.nlp.metrics.classification_report import ClassificationReport
  from nemo.collections.nlp.models.nlp_model import NLPModel
  from nemo.collections.nlp.modules.common import TokenClassifier
  from nemo.collections.nlp.parts.utils_funcs import get_classification_report, plot_confusion_matrix, tensor2list
  from nemo.core.classes.common import PretrainedModelInfo, typecheck
  from nemo.utils import logging
label_desc: Use image node_id 17 for calling the TokenClassificationModel obj's underlying member method code with example usage: obj.evaluate_from_file(output_dir, text_file, labels_file, add_confusion_matrix, normalize_confusion_matrix, batch_size) without return types