Example #1
0
        def test_configuration(self):
            # Make configuration based on default
            c = FaissNearestNeighborsIndex.get_default_config()

            self.assertIn('MemoryDescriptorIndex', c['descriptor_set'])
            c['descriptor_set']['type'] = 'MemoryDescriptorIndex'

            self.assertIn('MemoryKeyValueStore', c['idx2uid_kvs'])
            c['idx2uid_kvs']['type'] = 'MemoryKeyValueStore'

            self.assertIn('MemoryKeyValueStore', c['uid2idx_kvs'])
            c['uid2idx_kvs']['type'] = 'MemoryKeyValueStore'

            self.assertIn('DataMemoryElement', c['index_element'])
            c['index_element']['type'] = 'DataMemoryElement'

            self.assertIn('DataMemoryElement', c['index_param_element'])
            c['index_param_element']['type'] = 'DataMemoryElement'

            # # Build based on configuration
            index = FaissNearestNeighborsIndex.from_config(c)
            self.assertEqual(index.factory_string, 'IVF1,Flat')
            self.assertIsInstance(index.factory_string, six.string_types)

            # Test that constructing a new instance from ``index``'s config
            # yields an index with the same configuration (idempotent).
            index2 = FaissNearestNeighborsIndex.from_config(
                index.get_config())
            self.assertEqual(index.get_config(), index2.get_config())
Example #2
0
        def test_configuration(self):
            # Make configuration based on default
            c = FaissNearestNeighborsIndex.get_default_config()

            self.assertIn('MemoryDescriptorIndex', c['descriptor_set'])
            c['descriptor_set']['type'] = 'MemoryDescriptorIndex'

            self.assertIn('MemoryKeyValueStore', c['idx2uid_kvs'])
            c['idx2uid_kvs']['type'] = 'MemoryKeyValueStore'

            self.assertIn('MemoryKeyValueStore', c['uid2idx_kvs'])
            c['uid2idx_kvs']['type'] = 'MemoryKeyValueStore'

            self.assertIn('DataMemoryElement', c['index_element'])
            c['index_element']['type'] = 'DataMemoryElement'

            self.assertIn('DataMemoryElement', c['index_param_element'])
            c['index_param_element']['type'] = 'DataMemoryElement'

            # # Build based on configuration
            index = FaissNearestNeighborsIndex.from_config(c)
            self.assertEqual(index.factory_string, 'IVF1,Flat')
            self.assertIsInstance(index.factory_string, six.string_types)

            # Test that constructing a new instance from ``index``'s config
            # yields an index with the same configuration (idempotent).
            index2 = FaissNearestNeighborsIndex.from_config(index.get_config())
            self.assertEqual(index.get_config(), index2.get_config())
Example #3
0
 def test_init_valid_metric_type_integer(self):
     """
     Test passing an integer that matches one of the FAISS metric constant
     values.
     """
     # Mock positional parameter values.
     m = mock.Mock()
     FaissNearestNeighborsIndex(m, m, m, metric_type=0)
     FaissNearestNeighborsIndex(m, m, m, metric_type=1)
     FaissNearestNeighborsIndex(m, m, m, metric_type=20)
Example #4
0
 def test_init_valid_metric_type_label(self):
     """
     Test passing a non-default, valid metric type string label
     """
     # Mock positional parameter values.
     m = mock.Mock()
     FaissNearestNeighborsIndex(m, m, m, metric_type="l1")
     # Case should not matter
     FaissNearestNeighborsIndex(m, m, m, metric_type="InNeR_pRoDuCt")
     FaissNearestNeighborsIndex(m, m, m, metric_type="CaNbErRa")
Example #5
0
    def test_configuration(self):
        ex_descr_set = MemoryDescriptorSet()
        ex_i2u_kvs = MemoryKeyValueStore()
        ex_u2i_kvs = MemoryKeyValueStore()
        ex_index_elem = DataMemoryElement()
        ex_index_param_elem = DataMemoryElement()

        i = FaissNearestNeighborsIndex(
            descriptor_set=ex_descr_set,
            idx2uid_kvs=ex_i2u_kvs,
            uid2idx_kvs=ex_u2i_kvs,
            index_element=ex_index_elem,
            index_param_element=ex_index_param_elem,
            read_only=True,
            factory_string=u'some fact str',
            ivf_nprobe=88,
            use_gpu=False,
            gpu_id=99,
            random_seed=8,
        )
        for inst in configuration_test_helper(i):
            assert isinstance(inst._descriptor_set, MemoryDescriptorSet)
            assert isinstance(inst._idx2uid_kvs, MemoryKeyValueStore)
            assert isinstance(inst._uid2idx_kvs, MemoryKeyValueStore)
            assert isinstance(inst._index_element, DataMemoryElement)
            assert isinstance(inst._index_param_element, DataMemoryElement)
            assert inst.read_only is True
            assert isinstance(inst.factory_string, six.string_types)
            assert inst.factory_string == 'some fact str'
            assert inst._ivf_nprobe == 88
            assert inst._use_gpu is False
            assert inst._gpu_id == 99
            assert inst.random_seed == 8
Example #6
0
    def test_configuration_null_persistence(self):
        # Make configuration based on default
        c = FaissNearestNeighborsIndex.get_default_config()
        c['descriptor_set']['type'] = 'MemoryDescriptorSet'
        c['idx2uid_kvs']['type'] = 'MemoryKeyValueStore'
        c['uid2idx_kvs']['type'] = 'MemoryKeyValueStore'

        # # Build based on configuration
        index = FaissNearestNeighborsIndex.from_config(c)
        self.assertEqual(index.factory_string, 'IDMap,Flat')
        self.assertIsInstance(index.factory_string, six.string_types)

        # Test that constructing a new instance from ``index``'s config
        # yields an index with the same configuration (idempotent).
        index2 = FaissNearestNeighborsIndex.from_config(index.get_config())
        self.assertEqual(index.get_config(), index2.get_config())
Example #7
0
 def test_init_invalid_metric_type_label(self):
     """
     Test that a ValueError is thrown when the metric type given is not a
     valid string key.
     """
     # Mock positional parameter values.
     m = mock.Mock()
     with pytest.raises(ValueError):
         FaissNearestNeighborsIndex(m, m, m, metric_type="NotAValidLabel")
Example #8
0
 def test_init_invlaid_metric_type_int(self):
     """
     Test that a ValueError is thrown when the metric type given is an
     integer but not one present in the autogenerated map.
     """
     # Mock positional parameter values.
     m = mock.Mock()
     with pytest.raises(ValueError):
         # Probably not a valid constant value...
         FaissNearestNeighborsIndex(m, m, m, metric_type=123456789)
Example #9
0
 def _make_inst(self,
                descriptor_set=None,
                idx2uid_kvs=None,
                uid2idx_kvs=None,
                **kwargs):
     """
     Make an instance of FaissNearestNeighborsIndex
     """
     if 'random_seed' not in kwargs:
         kwargs.update(random_seed=self.RAND_SEED)
     if descriptor_set is None:
         descriptor_set = MemoryDescriptorSet()
     if idx2uid_kvs is None:
         idx2uid_kvs = MemoryKeyValueStore()
     if uid2idx_kvs is None:
         uid2idx_kvs = MemoryKeyValueStore()
     return FaissNearestNeighborsIndex(descriptor_set, idx2uid_kvs,
                                       uid2idx_kvs, **kwargs)
Example #10
0
import six
from six.moves import range, zip

from smqtk.algorithms import NearestNeighborsIndex
from smqtk.algorithms.nn_index.faiss import FaissNearestNeighborsIndex
from smqtk.exceptions import ReadOnlyError
from smqtk.representation.data_element.memory_element import (
    DataMemoryElement, )
from smqtk.representation.descriptor_element.local_elements import (
    DescriptorMemoryElement, )
from smqtk.representation.descriptor_set.memory import MemoryDescriptorSet
from smqtk.representation.key_value.memory import MemoryKeyValueStore
from smqtk.utils.configuration import configuration_test_helper


@pytest.mark.skipif(not FaissNearestNeighborsIndex.is_usable(),
                    reason="FaissNearestNeighborsIndex does not report as "
                    "usable.")
class TestFAISSIndex(unittest.TestCase):

    RAND_SEED = 42

    def _make_inst(self,
                   descriptor_set=None,
                   idx2uid_kvs=None,
                   uid2idx_kvs=None,
                   **kwargs):
        """
        Make an instance of FaissNearestNeighborsIndex
        """
        if 'random_seed' not in kwargs:
Example #11
0
import six
from six.moves import range, zip

from smqtk.algorithms import get_nn_index_impls
from smqtk.algorithms.nn_index.faiss import FaissNearestNeighborsIndex
from smqtk.exceptions import ReadOnlyError
from smqtk.representation.data_element.memory_element import (
    DataMemoryElement,
)
from smqtk.representation.descriptor_element.local_elements import (
    DescriptorMemoryElement,
)
from smqtk.representation.descriptor_index.memory import MemoryDescriptorIndex
from smqtk.representation.key_value.memory import MemoryKeyValueStore

if FaissNearestNeighborsIndex.is_usable():

    class TestFAISSIndex (unittest.TestCase):

        RAND_SEED = 42

        def _make_inst(self, descriptor_set=None, idx2uid_kvs=None,
                       uid2idx_kvs=None, **kwargs):
            """
            Make an instance of FaissNearestNeighborsIndex
            """
            if 'random_seed' not in kwargs:
                kwargs.update(random_seed=self.RAND_SEED)
            if descriptor_set is None:
                descriptor_set = MemoryDescriptorIndex()
            if idx2uid_kvs is None:
Example #12
0
import numpy as np
import six
from six.moves import range, zip

from smqtk.algorithms import get_nn_index_impls
from smqtk.algorithms.nn_index.faiss import FaissNearestNeighborsIndex
from smqtk.exceptions import ReadOnlyError
from smqtk.representation.data_element.memory_element import (
    DataMemoryElement, )
from smqtk.representation.descriptor_element.local_elements import (
    DescriptorMemoryElement, )
from smqtk.representation.descriptor_index.memory import MemoryDescriptorIndex
from smqtk.representation.key_value.memory import MemoryKeyValueStore

if FaissNearestNeighborsIndex.is_usable():

    class TestFAISSIndex(unittest.TestCase):

        RAND_SEED = 42

        def _make_inst(self,
                       descriptor_set=None,
                       idx2uid_kvs=None,
                       uid2idx_kvs=None,
                       **kwargs):
            """
            Make an instance of FaissNearestNeighborsIndex
            """
            if 'random_seed' not in kwargs:
                kwargs.update(random_seed=self.RAND_SEED)