class_docstr: Test the IDSelector filtering for as many (index class, id selector class)
  combinations as possible.
class_docstr_tok: ["Test","the","IDSelector","filtering","for","as","many","(","index","class",",","id","selector","class",")","combinations","as","possible","."]
docstr: Verify that the id selector returns the subset of results that are
  members according to the IDSelector.
  Supports id_selector_type="batch", "bitmap", "range", "range_sorted", "and", "or", "xor"
code: def do_test_id_selector(
      self, index_key, id_selector_type="batch", mt=faiss.METRIC_L2
  ):
      """Verify that the id selector returns the subset of results that are
      members according to the IDSelector.
      Supports id_selector_type="batch", "bitmap", "range", "range_sorted", "and", "or", "xor"
      """
      ds = datasets.SyntheticDataset(32, 1000, 100, 20)
      index = faiss.index_factory(ds.d, index_key, mt)
      index.train(ds.get_train())
      k = 10
  
      # reference result
      if "range" in id_selector_type:
          subset = np.arange(30, 80).astype("int64")
      elif id_selector_type == "or":
          lhs_rs = np.random.RandomState(123)
          lhs_subset = lhs_rs.choice(ds.nb, 50, replace=False).astype(
              "int64"
          )
          rhs_rs = np.random.RandomState(456)
          rhs_subset = rhs_rs.choice(ds.nb, 20, replace=False).astype(
              "int64"
          )
          subset = np.union1d(lhs_subset, rhs_subset)
      elif id_selector_type == "and":
          lhs_rs = np.random.RandomState(123)
          lhs_subset = lhs_rs.choice(ds.nb, 50, replace=False).astype(
              "int64"
          )
          rhs_rs = np.random.RandomState(456)
          rhs_subset = rhs_rs.choice(ds.nb, 10, replace=False).astype(
              "int64"
          )
          subset = np.intersect1d(lhs_subset, rhs_subset)
      elif id_selector_type == "xor":
          lhs_rs = np.random.RandomState(123)
          lhs_subset = lhs_rs.choice(ds.nb, 50, replace=False).astype(
              "int64"
          )
          rhs_rs = np.random.RandomState(456)
          rhs_subset = rhs_rs.choice(ds.nb, 40, replace=False).astype(
              "int64"
          )
          subset = np.setxor1d(lhs_subset, rhs_subset)
      else:
          rs = np.random.RandomState(123)
          subset = rs.choice(ds.nb, 50, replace=False).astype("int64")
      # add_with_ids not supported for all index types
      # index.add_with_ids(ds.get_database()[subset], subset)
      index.add(ds.get_database()[subset])
      if "IVF" in index_key and id_selector_type == "range_sorted":
          self.assertTrue(index.check_ids_sorted())
      Dref, Iref0 = index.search(ds.get_queries(), k)
      Iref = subset[Iref0]
      Iref[Iref0 < 0] = -1
  
      radius = float(Dref[Iref > 0].max()) * 1.01
      try:
          Rlims_ref, RDref, RIref = index.range_search(
              ds.get_queries(), radius
          )
      except RuntimeError as e:
          if "not implemented" in str(e):
              have_range_search = False
          else:
              raise
      else:
          RIref = subset[RIref]
          # normalize the range search results
          RDref, RIref = sort_range_res_2(Rlims_ref, RDref, RIref)
          have_range_search = True
  
      # result with selector: fill full database and search with selector
      index.reset()
      index.add(ds.get_database())
      if id_selector_type == "range":
          sel = faiss.IDSelectorRange(30, 80)
      elif id_selector_type == "range_sorted":
          sel = faiss.IDSelectorRange(30, 80, True)
      elif id_selector_type == "array":
          sel = faiss.IDSelectorArray(subset)
      elif id_selector_type == "bitmap":
          bitmap = np.zeros(ds.nb, dtype=bool)
          bitmap[subset] = True
          bitmap = np.packbits(bitmap, bitorder="little")
          sel = faiss.IDSelectorBitmap(bitmap)
      elif id_selector_type == "not":
          ssubset = set(subset)
          inverse_subset = np.array(
              [i for i in range(ds.nb) if i not in ssubset]
          ).astype("int64")
          sel = faiss.IDSelectorNot(
              faiss.IDSelectorBatch(inverse_subset)
          )
      elif id_selector_type == "or":
          sel = faiss.IDSelectorOr(
              faiss.IDSelectorBatch(lhs_subset),
              faiss.IDSelectorBatch(rhs_subset),
          )
      elif id_selector_type == "and":
          sel = faiss.IDSelectorAnd(
              faiss.IDSelectorBatch(lhs_subset),
              faiss.IDSelectorBatch(rhs_subset),
          )
      elif id_selector_type == "xor":
          sel = faiss.IDSelectorXOr(
              faiss.IDSelectorBatch(lhs_subset),
              faiss.IDSelectorBatch(rhs_subset),
          )
      else:
          sel = faiss.IDSelectorBatch(subset)
  
      params = (
          faiss.SearchParametersIVF(sel=sel)
          if "IVF" in index_key
          else faiss.SearchParametersPQ(sel=sel)
          if "PQ" in index_key
          else faiss.SearchParameters(sel=sel)
      )
      Dnew, Inew = index.search(ds.get_queries(), k, params=params)
      np.testing.assert_array_equal(Iref, Inew)
      np.testing.assert_almost_equal(Dref, Dnew, decimal=5)
  
      if have_range_search:
          Rlims_new, RDnew, RInew = index.range_search(
              ds.get_queries(), radius, params=params
          )
          np.testing.assert_array_equal(Rlims_ref, Rlims_new)
          RDref, RIref = sort_range_res_2(Rlims_ref, RDref, RIref)
          np.testing.assert_array_equal(RIref, RInew)
          np.testing.assert_almost_equal(RDref, RDnew, decimal=5)
  
imports: import numpy
  import faiss
  import unittest
  import sys
  import gc
  from faiss.contrib import datasets
  from faiss.contrib.evaluation import sort_range_res_2, check_ref_range_results
label_desc: Use image node_id 1 for calling the TestSelector obj's underlying member method code with example usage: obj.do_test_id_selector(index_key, id_selector_type, mt) without return types