class_docstr: A Batch sampler to group mentions in batches with close number of pairs to be padded together
      
class_docstr_tok: ["A","Batch","sampler","to","group","mentions","in","batches","with","close","number","of","pairs","to","be","padded","together"]
docstr: Create and feed batches of mentions having close number of antecedents
      The batch are padded and collated by the padder_collate function
  
  # Arguments:
      mentions_pairs_length array of shape (N, 1): list/array of the number of pairs for each mention
      batchsize: Number of pairs of each batch will be capped at this
docstr_tok: ["Create","and","feed","batches","of","mentions","having","close","number","of","antecedents","The","batch","are","padded","and","collated","by","the","padder_collate","function","#","Arguments",":","mentions_pairs_length","array","of","shape","(","N",",","1",")",":","list\/array","of","the","number","of","pairs","for","each","mention","batchsize",":","Number","of","pairs","of","each","batch","will","be","capped","at","this"]
code: def __init__(
      self,
      mentions_pairs_length,
      batchsize=600,
      shuffle=False,
      debug=False,
  ):
      """Create and feed batches of mentions having close number of antecedents
          The batch are padded and collated by the padder_collate function
  
      # Arguments:
          mentions_pairs_length array of shape (N, 1): list/array of the number of pairs for each mention
          batchsize: Number of pairs of each batch will be capped at this
      """
      self.shuffle = shuffle
      num_mentions = len(mentions_pairs_length)
      mentions_lengths = np.concatenate(
          [
              mentions_pairs_length,
              np.arange(0, num_mentions, 1, dtype=int)[:, np.newaxis],
          ],
          axis=1,
      )
      sorted_lengths = mentions_lengths[
          mentions_lengths[:, 0].argsort()
      ]
      print("Preparing batches 📚")
  
      self.batches = []
      self.batches_pairs = []
      self.batches_size = []
      batch = []
      n_pairs = []
      num = 0
      for length, mention_idx in sorted_lengths:
          if num > batchsize or (
              num == len(batch) and length != 0
          ):  # We keep the no_pairs batches pure
              if debug:
                  print(
                      "Added batch number",
                      len(self.batches),
                      "with",
                      len(batch),
                      "mentions and",
                      num,
                      "pairs",
                  )
              self.batches.append(batch)
              self.batches_size.append(
                  num
              )  # We don't count the max 7 additional mentions that are repeated
              self.batches_pairs.append(n_pairs)
  
              # Start a new batch
              batch = [mention_idx]
              n_pairs = [length]
              num = (
                  length + 1
              )  # +1 since we also have the single mention to add to the number of pairs
          else:
              num += length + 1
              batch.append(mention_idx)
              n_pairs.append(length)
  
      # Complete and store the last batch
      if debug:
          print(
              "Added batch number",
              len(self.batches),
              "with",
              len(batch),
              "mentions and",
              num,
              "pairs",
          )
      self.batches.append(batch)
      self.batches_size.append(num)
      self.batches_pairs.append(n_pairs)
      self.n_pairs = sum(sum(p) for p in self.batches_pairs)
      self.n_mentions = sum(len(b) for b in self.batches)
      self.n_batches = len(self.batches)
      self.pairs_per_batch = float(self.n_pairs) / self.n_batches
      self.mentions_per_batch = float(self.n_mentions) / self.n_batches
      print(
          "Dataset has:",
          self.n_batches,
          "batches,",
          self.n_mentions,
          "mentions,",
          self.n_pairs,
          "pairs",
      )
  
imports: import os
  import io
  import numpy
  import torch
  import torch.utils.data
  from torch.utils.data.sampler import Sampler
  from torch.utils.data import Dataset
  from neuralcoref.train.utils import encode_distance, BATCH_SIZE_PATH, SIZE_FP, SIZE_FP_COMPRESSED, SIZE_FS, SIZE_FS_COMPRESSED, SIZE_GENRE, SIZE_PAIR_IN, SIZE_SINGLE_IN
  from neuralcoref.train.conllparser import FEATURES_NAMES
label_desc: Use image node_id 1 to create a new NCBatchSampler object from inherited base classes: Sampler with example: obj = NCBatchSampler(mentions_pairs_length, batchsize, shuffle, debug)