Example #1
0
    def test_build_index_with_cache(self) -> None:
        # Empty memory data elements for storage
        empty_data = 'base64://'
        f = FlannNearestNeighborsIndex(empty_data, empty_data, empty_data)
        # Internal elements should initialize have zero-length byte values
        assert f._index_elem is not None
        assert f._index_param_elem is not None
        assert f._descr_cache_elem is not None
        self.assertEqual(len(f._index_elem.get_bytes()), 0)
        self.assertEqual(len(f._index_param_elem.get_bytes()), 0)
        self.assertEqual(len(f._descr_cache_elem.get_bytes()), 0)

        # Make unit vectors, one for each feature dimension.
        dim = 8
        test_descriptors = []
        for i in range(dim):
            v = numpy.zeros(dim, float)
            v[i] = 1.
            d = DescriptorMemoryElement('unit', i)
            d.set_vector(v)
            test_descriptors.append(d)

        f.build_index(test_descriptors)

        # Internal elements should not have non-zero byte values.
        self.assertGreater(len(f._index_elem.get_bytes()), 0)
        self.assertGreater(len(f._index_param_elem.get_bytes()), 0)
        self.assertGreater(len(f._descr_cache_elem.get_bytes()), 0)
Example #2
0
 def test_has_model_data_valid_uris(self, _m_flann_lfm: mock.MagicMock) -> None:
     # Mocking flann data loading that occurs in constructor when given
     # non-empty URI targets
     f = FlannNearestNeighborsIndex(
         'base64://bW9kZWxEYXRh',  # 'modelData'
         'base64://cGFyYW1EYXRh',  # 'paramData'
         'base64://ZGVzY3JEYXRh',  # 'descrData'
     )
     self.assertTrue(f._has_model_data())
Example #3
0
 def test_load_flann_model_empty_data_elements(self) -> None:
     # Construct index with valid, but empty, data URIs instances
     empty_data = 'base64://'
     f = FlannNearestNeighborsIndex(empty_data, empty_data, empty_data)
     # Load method should do nothing but set PID since given data was
     # empty.
     f._load_flann_model()
     self.assertIsNone(f._descr_cache)
     self.assertIsNone(f._flann)
     self.assertIsNone(f._flann_build_params)
     self.assertIsNotNone(f._pid)
Example #4
0
    def test_configuration(self) -> None:
        index_filepath = '/index_filepath'
        para_filepath = '/param_fp'
        descr_cache_fp = '/descrcachefp'

        c = FlannNearestNeighborsIndex(
            index_uri=index_filepath,
            parameters_uri=para_filepath,
            descriptor_cache_uri=descr_cache_fp,
            distance_method='hik', random_seed=42,
        )
        for inst in configuration_test_helper(c):  # type: FlannNearestNeighborsIndex
            assert inst._index_uri == index_filepath
            assert inst._index_param_uri == para_filepath
            assert inst._descr_cache_uri == descr_cache_fp
            assert inst._distance_method == 'hik'
            assert inst._rand_seed == 42
Example #5
0
 def test_has_model_data_empty_elements(self) -> None:
     f = FlannNearestNeighborsIndex('', '', '')
     self.assertFalse(f._has_model_data())
Example #6
0
 def test_has_model_data_no_uris(self) -> None:
     f = FlannNearestNeighborsIndex()
     self.assertFalse(f._has_model_data())
Example #7
0
 def test_build_index_no_descriptors(self) -> None:
     f = FlannNearestNeighborsIndex()
     self.assertRaises(
         ValueError,
         f.build_index, []
     )
Example #8
0
 def _make_inst(self, dist_method: str) -> FlannNearestNeighborsIndex:
     """
     Make an instance of FlannNearestNeighborsIndex
     """
     return FlannNearestNeighborsIndex(distance_method=dist_method,
                                       random_seed=self.RAND_SEED)
Example #9
0
import random
import unittest.mock as mock
import unittest

import numpy
import pytest

from smqtk_core.configuration import configuration_test_helper
from smqtk_descriptors.impls.descriptor_element.memory import DescriptorMemoryElement
from smqtk_indexing import NearestNeighborsIndex
from smqtk_indexing.impls.nn_index.flann import FlannNearestNeighborsIndex


# Don't bother running tests of the class is not usable
@pytest.mark.skipif(not FlannNearestNeighborsIndex.is_usable(),
                    reason="FlannNearestNeighborsIndex does not report as "
                           "usable.")
class TestFlannIndex (unittest.TestCase):

    RAND_SEED = 42

    def _make_inst(self, dist_method: str) -> FlannNearestNeighborsIndex:
        """
        Make an instance of FlannNearestNeighborsIndex
        """
        return FlannNearestNeighborsIndex(distance_method=dist_method,
                                          random_seed=self.RAND_SEED)

    def test_impl_findable(self) -> None:
        # Already here because the implementation is reporting itself as
        # usable.