test_scheduler.py 1.7 KB
Newer Older
X
xj.lin 已提交
1 2
from ..scheduler import *

X
xj.lin 已提交
3 4 5 6 7 8 9 10 11
import unittest
import faiss
import numpy as np


class TestScheduler(unittest.TestCase):
    def test_schedule(self):
        d = 64
        nb = 10000
X
xj.lin 已提交
12
        nq = 2
X
xj.lin 已提交
13 14
        nt = 5000
        xt, xb, xq = get_dataset(d, nb, nt, nq)
X
xj.lin 已提交
15
        file_name = "/tmp/tempfile_1"
X
xj.lin 已提交
16 17 18 19 20 21 22 23 24

        index = faiss.IndexFlatL2(d)
        print(index.is_trained)
        index.add(xb)
        faiss.write_index(index, file_name)
        Dref, Iref = index.search(xq, 5)

        index2 = faiss.read_index(file_name)

J
jinhai 已提交
25
        scheduler_instance = Scheduler()
X
xj.lin 已提交
26 27 28 29

        # query args 1
        query_index = dict()
        query_index['index'] = [file_name]
J
jinhai 已提交
30
        vectors = scheduler_instance.Search(query_index, vectors=xq, k=5)
X
xj.lin 已提交
31 32 33 34 35
        assert np.all(vectors == Iref)

        # query args 2
        query_index = dict()
        query_index['raw'] = xt
J
jinhai 已提交
36 37
        # Xiaojun TODO: 'raw_id' part
        # query_index['raw_id'] = 
X
xj.lin 已提交
38 39
        query_index['dimension'] = d
        query_index['index'] = [file_name]
X
xj.lin 已提交
40

J
jinhai 已提交
41 42
        # Xiaojun TODO: once 'raw_id' part added, open below
        # vectors = scheduler_instance.Search(query_index, vectors=xq, k=5)
X
xj.lin 已提交
43

X
xj.lin 已提交
44 45 46 47 48 49 50
        # print("success")


def get_dataset(d, nb, nt, nq):
    """A dataset that is not completely random but still challenging to
    index
    """
X
xj.lin 已提交
51
    d1 = 10  # intrinsic dimension (more or less)
X
xj.lin 已提交
52 53 54 55 56 57 58 59 60 61 62
    n = nb + nt + nq
    rs = np.random.RandomState(1338)
    x = rs.normal(size=(n, d1))
    x = np.dot(x, rs.rand(d1, d))
    # now we have a d1-dim ellipsoid in d-dimensional space
    # higher factor (>4) -> higher frequency -> less linear
    x = x * (rs.rand(d) * 4 + 0.1)
    x = np.sin(x)
    x = x.astype('float32')
    return x[:nt], x[nt:-nq], x[-nq:]

X
xj.lin 已提交
63

X
xj.lin 已提交
64
if __name__ == "__main__":
X
xj.lin 已提交
65
    unittest.main()