class_docstr: Generates and stores PyTorch DataLoader objects for the train, dev and test datasets.
  Relies upon functionality in the processor to do the conversion of the data. Will also
  calculate and display some statistics.
   
class_docstr_tok: ["Generates","and","stores","PyTorch","DataLoader","objects","for","the","train",",","dev","and","test","datasets",".","Relies","upon","functionality","in","the","processor","to","do","the","conversion","of","the","data",".","Will","also","calculate","and","display","some","statistics","."]
docstr: For imbalanced datasets, we can calculate class weights that can be used later in the
  loss function of the prediction head to upweight the loss of minorities.
  
  :param task_name: name of the task as used in the processor
  :type task_name: str
docstr_tok: ["For","imbalanced","datasets",",","we","can","calculate","class","weights","that","can","be","used","later","in","the","loss","function","of","the","prediction","head","to","upweight","the","loss","of","minorities",".",":","param","task_name",":","name","of","the","task","as","used","in","the","processor",":","type","task_name",":","str"]
code: def calculate_class_weights(self, task_name, source="train"):
      """For imbalanced datasets, we can calculate class weights that can be used later in the
      loss function of the prediction head to upweight the loss of minorities.
  
      :param task_name: name of the task as used in the processor
      :type task_name: str
      """
  
      tensor_name = self.processor.tasks[task_name]["label_tensor_name"]
      label_list = self.processor.tasks[task_name]["label_list"]
      tensor_idx = list(self.tensor_names).index(tensor_name)
      # we need at least ONE observation for each label to avoid division by zero in compute_class_weights.
      observed_labels = copy.deepcopy(label_list)
      if source == "all":
          datasets = self.data.values()
      elif source == "train":
          datasets = [self.data["train"]]
      else:
          raise Exception(
              'source argument expects one of ["train", "all"]'
          )
      for dataset in datasets:
          if (
              "multilabel"
              in self.processor.tasks[task_name]["task_type"]
          ):
              for x in dataset:
                  observed_labels += [
                      label_list[label_id]
                      for label_id in (x[tensor_idx] == 1).nonzero()
                  ]
          else:
              observed_labels += [
                  label_list[x[tensor_idx].item()] for x in dataset
              ]
  
      # TODO scale e.g. via logarithm to avoid crazy spikes for rare classes
      class_weights = compute_class_weight(
          "balanced", classes=np.asarray(label_list), y=observed_labels
      )
  
      # conversion necessary to have class weights of same type as model weights
      class_weights = class_weights.astype(np.float32)
      return class_weights
  
imports: import copy
  import logging
  import torch.multiprocessing
  from contextlib import ExitStack
  from functools import partial
  import random
  from pathlib import Path
  from itertools import chain, groupby
  import numpy
  from sklearn.utils.class_weight import compute_class_weight
  from torch.utils.data import Dataset, Subset, IterableDataset
  from torch.utils.data.distributed import DistributedSampler
  from torch.utils.data.sampler import RandomSampler, SequentialSampler
  import torch
  from sklearn.model_selection import StratifiedKFold, KFold, ShuffleSplit, StratifiedShuffleSplit
  from tqdm import tqdm
  from farm.data_handler.dataloader import NamedDataLoader
  from farm.data_handler.processor import Processor, BertStyleLMProcessor
  from farm.data_handler.utils import grouper
  from farm.data_handler.dataset import ConcatTensorDataset
  from farm.modeling.tokenization import EmbeddingTokenizer
  from farm.utils import MLFlowLogger
  from farm.utils import log_ascii_workers, calc_chunksize
  from farm.utils import get_dict_checksum
  from farm.visual.ascii.images import TRACTOR_SMALL
label_desc: Use image node_id 14 for calling the DataSilo obj's underlying member method code with example usage: obj.calculate_class_weights(task_name, source) and returns: class_weights