<commit_before>import falconn
import numpy as np

def test_lsh_index_positive():
  n = 1000
  d = 128
  p = falconn.get_default_parameters(n, d)
  t = falconn.LSHIndex(p)
  dataset = np.random.randn(n, d).astype(np.float32)
  t.fit(dataset)
  u = np.random.randn(d).astype(np.float32)
  t.find_k_nearest_neighbors(u, 10)
  t.find_near_neighbors(u, 10.0)
  t.find_nearest_neighbor(u)
  t.get_candidates_with_duplicates(u)
  t.get_max_num_candidates()
  t.get_num_probes()
  t.get_query_statistics()
  t.get_unique_candidates(u)
  t.get_unique_sorted_candidates(u)
  t.reset_query_statistics()
  t.set_max_num_candidates(100)
  t.set_num_probes(10)

def test_lsh_index_negative():
  n = 1000
  d = 128
  p = falconn.get_default_parameters(n, d)
  t = falconn.LSHIndex(p)
  try:
    t.find_nearest_neighbor(np.random.randn(d))
    assert False
  except RuntimeError:
    pass
  try:
    dataset = [[1.0, 2.0], [3.0, 4.0]]
    t.fit(dataset)
    assert False
  except TypeError:
    pass
  try:
    dataset = np.random.randn(n, d).astype(np.int32)
    t.fit(dataset)
    assert False
  except ValueError:
    pass
  try:
    dataset = np.random.randn(10, 10, 10)
    t.fit(dataset)
    assert False
  except ValueError:
    pass
  dataset = np.random.randn(n, d).astype(np.float32)
  t.fit(dataset)
  dataset = np.random.randn(n, d).astype(np.float64)
  t.fit(dataset)
  u = np.random.randn(d).astype(np.float64)
  
  try:
    t.find_k_nearest_neighbors(u, 0.5)
    assert False
  except TypeError:
    pass

  try:
    t.find_k_nearest_neighbors(u, -1)
    assert False
  except ValueError:
    pass
  
  t.find_near_neighbors(u, -1)
  
  try:
    t.set_max_num_candidates(0.5)
    assert False
  except TypeError:
    pass
  try:
    t.set_max_num_candidates(-10)
    assert False
  except ValueError:
    pass
  t.set_num_probes(t._params.l)
  try:
    t.set_num_probes(t._params.l - 1)
    assert False
  except ValueError:
    pass
  try:
    t.set_num_probes(1000.1)
    assert False
  except TypeError:
    pass

  def check_check_query(f):
    try:
      f(u.astype(np.float32))
      assert False
    except ValueError:
      pass
    try:
      f([0.0] * d)
      assert False
    except TypeError:
      pass
    try:
      f(u[:d-1])
      assert False
    except ValueError:
      pass
    try:
      f(np.random.randn(d, d))
      assert False
    except ValueError:
      pass

  check_check_query(lambda u: t.find_k_nearest_neighbors(u, 10))
  check_check_query(lambda u: t.find_near_neighbors(u, 0.5))
  check_check_query(lambda u: t.find_nearest_neighbor(u))
  check_check_query(lambda u: t.get_candidates_with_duplicates(u))
  check_check_query(lambda u: t.get_unique_candidates(u))
  check_check_query(lambda u: t.get_unique_sorted_candidates(u))
  t.find_near_neighbors(u, 0.0)<commit_msg>updated Python module test<commit_after>import falconn
import numpy as np

def test_lsh_index_positive():
  n = 1000
  d = 128
  p = falconn.get_default_parameters(n, d)
  t = falconn.LSHIndex(p)
  dataset = np.random.randn(n, d).astype(np.float32)
  t.fit(dataset)
  u = np.random.randn(d).astype(np.float32)
  t.find_k_nearest_neighbors(u, 10)
  t.find_near_neighbors(u, 10.0)
  t.find_nearest_neighbor(u)
  t.get_candidates_with_duplicates(u)
  t.get_max_num_candidates()
  t.get_num_probes()
  t.get_query_statistics()
  t.get_unique_candidates(u)
  #t.get_unique_sorted_candidates(u)
  t.reset_query_statistics()
  t.set_max_num_candidates(100)
  t.set_num_probes(10)

def test_lsh_index_negative():
  n = 1000
  d = 128
  p = falconn.get_default_parameters(n, d)
  t = falconn.LSHIndex(p)
  try:
    t.find_nearest_neighbor(np.random.randn(d))
    assert False
  except RuntimeError:
    pass
  try:
    dataset = [[1.0, 2.0], [3.0, 4.0]]
    t.fit(dataset)
    assert False
  except TypeError:
    pass
  try:
    dataset = np.random.randn(n, d).astype(np.int32)
    t.fit(dataset)
    assert False
  except ValueError:
    pass
  try:
    dataset = np.random.randn(10, 10, 10)
    t.fit(dataset)
    assert False
  except ValueError:
    pass
  dataset = np.random.randn(n, d).astype(np.float32)
  t.fit(dataset)
  dataset = np.random.randn(n, d).astype(np.float64)
  t.fit(dataset)
  u = np.random.randn(d).astype(np.float64)
  
  try:
    t.find_k_nearest_neighbors(u, 0.5)
    assert False
  except TypeError:
    pass

  try:
    t.find_k_nearest_neighbors(u, -1)
    assert False
  except ValueError:
    pass
  
  t.find_near_neighbors(u, -1)
  
  try:
    t.set_max_num_candidates(0.5)
    assert False
  except TypeError:
    pass
  try:
    t.set_max_num_candidates(-10)
    assert False
  except ValueError:
    pass
  t.set_num_probes(t._params.l)
  try:
    t.set_num_probes(t._params.l - 1)
    assert False
  except ValueError:
    pass
  try:
    t.set_num_probes(1000.1)
    assert False
  except TypeError:
    pass

  def check_check_query(f):
    try:
      f(u.astype(np.float32))
      assert False
    except ValueError:
      pass
    try:
      f([0.0] * d)
      assert False
    except TypeError:
      pass
    try:
      f(u[:d-1])
      assert False
    except ValueError:
      pass
    try:
      f(np.random.randn(d, d))
      assert False
    except ValueError:
      pass

  check_check_query(lambda u: t.find_k_nearest_neighbors(u, 10))
  check_check_query(lambda u: t.find_near_neighbors(u, 0.5))
  check_check_query(lambda u: t.find_nearest_neighbor(u))
  check_check_query(lambda u: t.get_candidates_with_duplicates(u))
  check_check_query(lambda u: t.get_unique_candidates(u))
  #check_check_query(lambda u: t.get_unique_sorted_candidates(u))
  t.find_near_neighbors(u, 0.0)
{'fraction_non_alphanumeric': 0.11124933357028612, 'fraction_numerical': 0.02665718855518038, 'mean_word_length': 3.168888888888889, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4914514', 'n_tokens_mistral': 2428, 'n_tokens_neox': 2300, 'n_words': 402}