code: def get_weighted_indices(
      n: int,
      weights: Optional[np.ndarray],
      ensure_all_occur: bool = False,
  ) -> np.ndarray:
      indices = np.arange(n)
      if weights is not None:
          numbers = np.random.multinomial(n, weights)
          if ensure_all_occur:
              numbers += 1
          indices = indices.repeat(numbers)
      return indices
  
imports: import torch
  import numpy
  from abc import abstractmethod
  from abc import ABC
  from typing import Any
  from typing import Dict
  from typing import List
  from typing import Tuple
  from typing import Union
  from typing import Optional
  from cftool.misc import shallow_copy_dict
  from cftool.array import to_device
  from cftool.types import arr_type
  from cftool.types import np_dict_type
  from cftool.types import tensor_dict_type
  from ..schema import IDataset
  from ..schema import IDLModel
  from ..schema import DataArgs
  from ..schema import DataBundle
  from ..schema import DataConfig
  from ..schema import IDataLoader
  from ..schema import DataProcessor
  from ..constants import BATCH_INDICES_KEY
  from ..misc.toolkit import eval_context
  from ..misc.toolkit import get_device
  from ..misc.toolkit import np_batch_to_tensor
  from ..misc.toolkit import tensor_batch_to_np
label_desc: Use image node_id 1 for calling a global function with example usage: get_weighted_indices(n, weights, ensure_all_occur) and returns: indices