code: def anchor_beam(
      sample_fn,
      delta=0.05,
      epsilon=0.1,
      batch_size=10,
      min_shared_samples=0,
      desired_confidence=1,
      beam_size=1,
      verbose=False,
      epsilon_stop=0.05,
      min_samples_start=0,
      max_anchor_size=None,
      verbose_every=1,
      stop_on_first=False,
      coverage_samples=10000,
  ):
      anchor = {
          "feature": [],
          "mean": [],
          "precision": [],
          "coverage": [],
          "examples": [],
          "all_precision": 0,
      }
      _, coverage_data, _ = sample_fn(
          [], coverage_samples, compute_labels=False
      )
      raw_data, data, labels = sample_fn([], max(1, min_samples_start))
      mean = labels.mean()
      beta = np.log(1.0 / delta)
      lb = AnchorBaseBeam.dlow_bernoulli(mean, beta / data.shape[0])
      while (
          mean > desired_confidence
          and lb < desired_confidence - epsilon
      ):
          nraw_data, ndata, nlabels = sample_fn([], batch_size)
          data = np.vstack((data, ndata))
          raw_data = np.vstack((raw_data, nraw_data))
          labels = np.hstack((labels, nlabels))
          mean = labels.mean()
          lb = AnchorBaseBeam.dlow_bernoulli(mean, beta / data.shape[0])
      if lb > desired_confidence:
          anchor["num_preds"] = data.shape[0]
          anchor["all_precision"] = mean
          return anchor
      prealloc_size = batch_size * 10000
      current_idx = data.shape[0]
      data = np.vstack(
          (data, np.zeros((prealloc_size, data.shape[1]), data.dtype))
      )
      raw_data = np.vstack(
          (
              raw_data,
              np.zeros(
                  (prealloc_size, raw_data.shape[1]), raw_data.dtype
              ),
          )
      )
      labels = np.hstack(
          (labels, np.zeros(prealloc_size, labels.dtype))
      )
      n_features = data.shape[1]
      state = {
          "t_idx": collections.defaultdict(lambda: set()),
          "t_nsamples": collections.defaultdict(lambda: 0.0),
          "t_positives": collections.defaultdict(lambda: 0.0),
          "data": data,
          "prealloc_size": prealloc_size,
          "raw_data": raw_data,
          "labels": labels,
          "current_idx": current_idx,
          "n_features": n_features,
          "t_coverage_idx": collections.defaultdict(lambda: set()),
          "t_coverage": collections.defaultdict(lambda: 0.0),
          "coverage_data": coverage_data,
          "t_order": collections.defaultdict(lambda: list()),
      }
      current_size = 1
      best_of_size = {0: []}
      best_coverage = -1
      best_tuple = ()
      t = 1
      if max_anchor_size is None:
          max_anchor_size = n_features
      while current_size <= max_anchor_size:
          tuples = AnchorBaseBeam.make_tuples(
              best_of_size[current_size - 1], state
          )
          tuples = [
              x
              for x in tuples
              if state["t_coverage"][x] > best_coverage
          ]
          if len(tuples) == 0:
              break
          sample_fns = AnchorBaseBeam.get_sample_fns(
              sample_fn, tuples, state
          )
          initial_stats = AnchorBaseBeam.get_initial_statistics(
              tuples, state
          )
          # print tuples, beam_size
          chosen_tuples = AnchorBaseBeam.lucb(
              sample_fns,
              initial_stats,
              epsilon,
              delta,
              batch_size,
              min(beam_size, len(tuples)),
              verbose=verbose,
              verbose_every=verbose_every,
          )
          best_of_size[current_size] = [
              tuples[x] for x in chosen_tuples
          ]
          if verbose:
              print("Best of size ", current_size, ":")
          # print state['data'].shape[0]
          stop_this = False
          for i, t in zip(chosen_tuples, best_of_size[current_size]):
              # I can choose at most (beam_size - 1) tuples at each step,
              # and there are at most n_feature steps
              beta = np.log(
                  1.0 / (delta / (1 + (beam_size - 1) * n_features))
              )
              # beta = np.log(1. / delta)
              # if state['t_nsamples'][t] == 0:
              #     mean = 1
              # else:
              mean = state["t_positives"][t] / state["t_nsamples"][t]
              lb = AnchorBaseBeam.dlow_bernoulli(
                  mean, beta / state["t_nsamples"][t]
              )
              ub = AnchorBaseBeam.dup_bernoulli(
                  mean, beta / state["t_nsamples"][t]
              )
              coverage = state["t_coverage"][t]
              if verbose:
                  print(i, mean, lb, ub)
              while (
                  mean >= desired_confidence
                  and lb < desired_confidence - epsilon_stop
              ) or (
                  mean < desired_confidence
                  and ub >= desired_confidence + epsilon_stop
              ):
                  # print mean, lb, state['t_nsamples'][t]
                  sample_fns[i](batch_size)
                  mean = (
                      state["t_positives"][t] / state["t_nsamples"][t]
                  )
                  lb = AnchorBaseBeam.dlow_bernoulli(
                      mean, beta / state["t_nsamples"][t]
                  )
                  ub = AnchorBaseBeam.dup_bernoulli(
                      mean, beta / state["t_nsamples"][t]
                  )
              if verbose:
                  print(
                      "%s mean = %.2f lb = %.2f ub = %.2f coverage: %.2f n: %d"
                      % (
                          t,
                          mean,
                          lb,
                          ub,
                          coverage,
                          state["t_nsamples"][t],
                      )
                  )
              if (
                  mean >= desired_confidence
                  and lb > desired_confidence - epsilon_stop
              ):
                  if verbose:
                      print(
                          "Found eligible anchor ",
                          t,
                          "Coverage:",
                          coverage,
                          "Is best?",
                          coverage > best_coverage,
                      )
                  if coverage > best_coverage:
                      best_coverage = coverage
                      best_tuple = t
                      if best_coverage == 1 or stop_on_first:
                          stop_this = True
          if stop_this:
              break
          current_size += 1
      if best_tuple == ():
          # Could not find an anchor, will now choose the highest precision
          # amongst the top K from every round
          if verbose:
              print(
                  "Could not find an anchor, now doing best of each size"
              )
          tuples = []
          for i in range(0, current_size):
              tuples.extend(best_of_size[i])
          # tuples = best_of_size[current_size - 1]
          sample_fns = AnchorBaseBeam.get_sample_fns(
              sample_fn, tuples, state
          )
          initial_stats = AnchorBaseBeam.get_initial_statistics(
              tuples, state
          )
          # print tuples, beam_size
          chosen_tuples = AnchorBaseBeam.lucb(
              sample_fns,
              initial_stats,
              epsilon,
              delta,
              batch_size,
              1,
              verbose=verbose,
          )
          best_tuple = tuples[chosen_tuples[0]]
      # return best_tuple, state
      return AnchorBaseBeam.get_anchor_from_tuple(best_tuple, state)
  
file_path: anchor/anchor/anchor_base.py
imports: from __future__ import print_function
  import numpy
  import operator
  import copy
  import sklearn
  import collections
label_desc: Use image node_id 11 for calling the AnchorBaseBeam obj's underlying member method code with example usage: obj.anchor_beam(sample_fn, delta, epsilon, batch_size, min_shared_samples, desired_confidence, beam_size, verbose, epsilon_stop, min_samples_start, max_anchor_size, verbose_every, stop_on_first, coverage_samples) and returns: AnchorBaseBeam, anchor