code: def calc_class_weights(file_path: str, num_classes: int):
      """
      iterates over a data file and calculate the weights of each class to be used for class_balancing
      Args:
          file_path: path to the data file
          num_classes: number of classes in the dataset
      """
  
      if not os.path.exists(file_path):
          raise FileNotFoundError(
              f"Could not find data file {file_path} to calculate the class weights!"
          )
  
      with open(file_path, "r") as f:
          input_lines = f.readlines()
  
      labels = []
      for input_line in input_lines:
          parts = input_line.strip().split()
          try:
              label = int(parts[-1])
          except ValueError:
              raise ValueError(
                  f"No numerical labels found for {file_path}. Labels should be integers and separated by [TAB] at the end of each line."
              )
          labels.append(label)
  
      logging.info(f"Calculating stats of {file_path}...")
      total_sents, sent_label_freq, max_id = get_label_stats(
          labels, f"{file_path}_sentence_stats.tsv", verbose=False
      )
      if max_id >= num_classes:
          raise ValueError(
              f"Found an invalid label in {file_path}! Labels should be from [0, num_classes-1]."
          )
  
      class_weights_dict = get_freq_weights(sent_label_freq)
  
      logging.info(f"Total Sentence: {total_sents}")
      logging.info(f"Sentence class frequencies: {sent_label_freq}")
  
      logging.info(f"Class Weights: {class_weights_dict}")
      class_weights = fill_class_weights(
          weights=class_weights_dict, max_id=num_classes - 1
      )
  
      return class_weights
  
imports: import os
  import pickle
  import random
  from typing import Dict, List, Optional
  import numpy
  import torch
  from nemo.collections.common.tokenizers.tokenizer_spec import TokenizerSpec
  from nemo.collections.nlp.data.data_utils.data_preprocessing import fill_class_weights, get_freq_weights, get_label_stats, get_stats
  from nemo.collections.nlp.parts.utils_funcs import list2str
  from nemo.core.classes import Dataset
  from nemo.core.neural_types import ChannelType, LabelsType, MaskType, NeuralType
  from nemo.utils import logging
  from nemo.utils.env_var_parsing import get_envint
label_desc: Use image node_id 1 for calling a global function with example usage: calc_class_weights(file_path, num_classes) and returns: class_weights