def _test_match(values): values = np.array(values, dtype=float) ix = list(o.find_nearest_matches(values, values)) assert [values[i] for i in ix] == list(values) values2 = values + 0.1 ix2 = list(o.find_nearest_matches(values2, values)) assert [values2[i] for i in ix] == [values2[i] for i in ix2] values2 = values - 0.1 ix2 = list(o.find_nearest_matches(values2, values)) assert [values2[i] for i in ix] == [values2[i] for i in ix2]
def _test_match(values): values = array32(values) ix = list(o.find_nearest_matches(values, values)) assert [values[i] for i in ix] == list(values) values2 = values + 0.1 ix2 = list(o.find_nearest_matches(values2, values)) assert [values2[i] for i in ix] == [values2[i] for i in ix2] values2 = values - 0.1 ix2 = list(o.find_nearest_matches(values2, values)) assert [values2[i] for i in ix] == [values2[i] for i in ix2]
def test_find_neared_matches(): ix = o.find_nearest_matches(np.arange(4.0), np.arange(2.0)) assert list(ix) == [0, 1] ix = o.find_nearest_matches(np.arange(2.0), np.arange(4.0)) assert list(ix) == [0, 1, 1, 1] # unsorted with duplicates _test_match([2, 7, 0, 5, 3, 7, 2, 1, 3, 9]) # unsorted without duplicates _test_match([7, 0, 5, 2, 1, 3, 9]) # sorted _test_match([1, 2, 5, 6, 9, 11]) # reverse sorted _test_match([11, 9, 6, 5, 2, 1, 0]) # one element basis _test_match([2]) # two element basis const _test_match([2, 2]) # two element basis asc _test_match([2, 3]) # two element basis desc _test_match([3, 2]) # constant sequence _test_match([2, 2, 2]) # nearly constant sequence 1 _test_match([3, 2, 2, 2]) # nearly constant sequence 2 _test_match([3, 3, 3, 2]) # nearly constant sequence 3 _test_match([1, 2, 2, 2]) # nearly constant sequence 4 _test_match([1, 1, 1, 2])
def test_find_neared_matches(): ix = o.find_nearest_matches(arange32(4.0), arange32(2.0)) assert list(ix) == [0, 1] ix = o.find_nearest_matches(arange32(2.0), arange32(4.0)) assert list(ix) == [0, 1, 1, 1] # unsorted with duplicates _test_match([2, 7, 0, 5, 3, 7, 2, 1, 3, 9]) # unsorted without duplicates _test_match([7, 0, 5, 2, 1, 3, 9]) # sorted _test_match([1, 2, 5, 6, 9, 11]) # reverse sorted _test_match([11, 9, 6, 5, 2, 1, 0]) # one element basis _test_match([2]) # two element basis const _test_match([2, 2]) # two element basis asc _test_match([2, 3]) # two element basis desc _test_match([3, 2]) # constant sequence _test_match([2, 2, 2]) # nearly constant sequence 1 _test_match([3, 2, 2, 2]) # nearly constant sequence 2 _test_match([3, 3, 3, 2]) # nearly constant sequence 3 _test_match([1, 2, 2, 2]) # nearly constant sequence 4 _test_match([1, 1, 1, 2])
def _test_find_neared_matches_fuzzy(): for l in range(1, 100): for i in range(100): basis = np.random.random((l, )) basis.sort() search = np.random.random((l, )) tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) basis = basis[::-1] tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) search = basis tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) basis = basis[::-1] tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) # introduct dupplicates basis = np.hstack((basis, basis)) basis.sort() search = np.random.random((l, )) search = basis tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) basis = basis[::-1] tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) # introduct more dupplicates basis = np.hstack((basis, basis)) basis.sort() search = np.random.random((l, )) search = basis tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) basis = basis[::-1] tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim)
def _test_find_neared_matches_fuzzy(): for l in range(1, 100): for i in range(100): basis = np.random.random((l,)) basis.sort() search = np.random.random((l,)) tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) basis = basis[::-1] tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) search = basis tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) basis = basis[::-1] tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) # introduct dupplicates basis = np.hstack((basis, basis)) basis.sort() search = np.random.random((l,)) search = basis tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) basis = basis[::-1] tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) # introduct more dupplicates basis = np.hstack((basis, basis)) basis.sort() search = np.random.random((l,)) search = basis tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim) basis = basis[::-1] tobe = o.find_nearest_matches(basis, search, 0) optim = o.find_nearest_matches(basis, search) assert np.all(tobe == optim)