code: def test_space_main(self, space, dim):
      # Generating sample data
      data = np.float32(np.random.random((self.num_elements, dim)))
      test_data = np.float32(
          np.random.random((self.num_test_elements, dim))
      )
  
      # Declaring index
      p = hnswlib.Index(
          space=space, dim=dim
      )  # possible options are l2, cosine or ip
      print(f"Running pickle tests for {p}")
  
      p.num_threads = (
          self.num_threads
      )  # by default using all available cores
  
      p0 = pickle.loads(pickle.dumps(p))  # pickle un-initialized Index
      p.init_index(
          max_elements=self.num_elements,
          ef_construction=self.ef_construction,
          M=self.M,
      )
      p0.init_index(
          max_elements=self.num_elements,
          ef_construction=self.ef_construction,
          M=self.M,
      )
  
      p.ef = self.ef
      p0.ef = self.ef
  
      p1 = pickle.loads(
          pickle.dumps(p)
      )  # pickle Index before adding items
  
      # add items to ann index p,p0,p1
      p.add_items(data)
      p1.add_items(data)
      p0.add_items(data)
  
      p2 = pickle.loads(
          pickle.dumps(p)
      )  # pickle Index before adding items
  
      self.assertTrue(
          np.allclose(p.get_items(), p0.get_items()),
          "items for p and p0 must be same",
      )
      self.assertTrue(
          np.allclose(p0.get_items(), p1.get_items()),
          "items for p0 and p1 must be same",
      )
      self.assertTrue(
          np.allclose(p1.get_items(), p2.get_items()),
          "items for p1 and p2 must be same",
      )
  
      # Test if returned distances are same
      l, d = p.knn_query(test_data, k=self.k)
      l0, d0 = p0.knn_query(test_data, k=self.k)
      l1, d1 = p1.knn_query(test_data, k=self.k)
      l2, d2 = p2.knn_query(test_data, k=self.k)
  
      self.assertLessEqual(
          np.sum(((d - d0) ** 2.0) > 1e-3),
          self.dists_err_thresh,
          msg=f"knn distances returned by p and p0 must match",
      )
      self.assertLessEqual(
          np.sum(((d0 - d1) ** 2.0) > 1e-3),
          self.dists_err_thresh,
          msg=f"knn distances returned by p0 and p1 must match",
      )
      self.assertLessEqual(
          np.sum(((d1 - d2) ** 2.0) > 1e-3),
          self.dists_err_thresh,
          msg=f"knn distances returned by p1 and p2 must match",
      )
  
      # check if ann results match brute-force search
      #   allow for 2 labels to be missing from ann results
      check_ann_results(
          self,
          space,
          data,
          test_data,
          self.k,
          l,
          d,
          err_thresh=self.label_err_thresh,
          total_thresh=self.item_err_thresh,
          dists_thresh=self.dists_err_thresh,
      )
  
      check_ann_results(
          self,
          space,
          data,
          test_data,
          self.k,
          l2,
          d2,
          err_thresh=self.label_err_thresh,
          total_thresh=self.item_err_thresh,
          dists_thresh=self.dists_err_thresh,
      )
  
      # Check ef parameter value
      self.assertEqual(p.ef, self.ef, "incorrect value of p.ef")
      self.assertEqual(p0.ef, self.ef, "incorrect value of p0.ef")
      self.assertEqual(p2.ef, self.ef, "incorrect value of p2.ef")
      self.assertEqual(p1.ef, self.ef, "incorrect value of p1.ef")
  
      # Check M parameter value
      self.assertEqual(p.M, self.M, "incorrect value of p.M")
      self.assertEqual(p0.M, self.M, "incorrect value of p0.M")
      self.assertEqual(p1.M, self.M, "incorrect value of p1.M")
      self.assertEqual(p2.M, self.M, "incorrect value of p2.M")
  
      # Check ef_construction parameter value
      self.assertEqual(
          p.ef_construction,
          self.ef_construction,
          "incorrect value of p.ef_construction",
      )
      self.assertEqual(
          p0.ef_construction,
          self.ef_construction,
          "incorrect value of p0.ef_construction",
      )
      self.assertEqual(
          p1.ef_construction,
          self.ef_construction,
          "incorrect value of p1.ef_construction",
      )
      self.assertEqual(
          p2.ef_construction,
          self.ef_construction,
          "incorrect value of p2.ef_construction",
      )
  
file_path: hnswlib/tests/python/bindings_test_pickle.py
imports: import pickle
  import unittest
  import numpy
  import hnswlib
label_desc: Use image node_id 4 for calling a global function with example usage: test_space_main(space, dim) without return types