def test_build_index_with_cache(self): # 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 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)
def test_build_index(self): # 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 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 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)
def test_has_model_data_valid_uris(self, _m_flann_lfm): # 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())
def test_load_flann_model_empty_data_elements(self): # 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)
def test_configuration(self): index_filepath = '/index_filepath' para_filepath = '/param_fp' descr_cache_fp = '/descrcachefp' # Make configuration based on default c = FlannNearestNeighborsIndex.get_default_config() c['index_filepath'] = index_filepath c['parameters_filepath'] = para_filepath c['descriptor_cache_filepath'] = descr_cache_fp c['distance_method'] = 'hik' c['random_seed'] = 42 # Build based on configuration index = FlannNearestNeighborsIndex.from_config(c) ntools.assert_equal(index._index_filepath, index_filepath) ntools.assert_equal(index._index_param_filepath, para_filepath) ntools.assert_equal(index._descr_cache_filepath, descr_cache_fp) c2 = index.get_config() ntools.assert_equal(c, c2)
def test_configuration(self): self._make_cache_files() self._clean_cache_files() # Make configuration based on default c = FlannNearestNeighborsIndex.get_default_config() c['index_filepath'] = self.FLANN_INDEX_CACHE c['parameters_filepath'] = self.FLANN_PARAMETER_CACHE c['descriptor_cache_filepath'] = self.FLANN_DESCR_CACHE c['distance_method'] = 'hik' c['random_seed'] = 42 # Build based on configuration index = FlannNearestNeighborsIndex.from_config(c) ntools.assert_equal(index._index_filepath, self.FLANN_INDEX_CACHE) ntools.assert_equal(index._index_param_filepath, self.FLANN_PARAMETER_CACHE) ntools.assert_equal(index._descr_cache_filepath, self.FLANN_DESCR_CACHE) c2 = index.get_config() ntools.assert_equal(c, c2)
def test_configuration(self): 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
def test_build_index_no_descriptors(self): f = FlannNearestNeighborsIndex() self.assertRaises(ValueError, f.build_index, [])
import random import unittest import nose.tools as ntools import numpy from smqtk.representation.descriptor_element.local_elements import \ DescriptorMemoryElement from smqtk.algorithms.nn_index.flann import FlannNearestNeighborsIndex from smqtk.utils.file_utils import make_tempfile __author__ = "*****@*****.**" # Don't bother running tests of the class is not usable if FlannNearestNeighborsIndex.is_usable(): class TestFlannIndex (unittest.TestCase): FLANN_INDEX_CACHE = None FLANN_PARAMETER_CACHE = None FLANN_DESCR_CACHE = None RAND_SEED = 42 @classmethod def _make_cache_files(cls): cls.FLANN_INDEX_CACHE = make_tempfile('.flann') cls.FLANN_PARAMETER_CACHE = make_tempfile('.pickle') cls.FLANN_DESCR_CACHE = make_tempfile('.pickle')
def test_has_model_data_empty_elements(self): f = FlannNearestNeighborsIndex('', '', '') self.assertFalse(f._has_model_data())
import os import random import unittest import nose.tools as ntools import numpy from smqtk.representation.descriptor_element.local_elements import \ DescriptorMemoryElement from smqtk.algorithms.nn_index.flann import FlannNearestNeighborsIndex __author__ = "*****@*****.**" # Don't bother running tests of the class is not usable if FlannNearestNeighborsIndex.is_usable(): class TestFlannIndex(unittest.TestCase): RAND_SEED = 42 def _make_inst(self, dist_method): """ Make an instance of FlannNearestNeighborsIndex """ return FlannNearestNeighborsIndex(distance_method=dist_method, random_seed=self.RAND_SEED) def test_known_descriptors_euclidean_unit(self): dim = 5 ###
import unittest.mock as mock import random import unittest import numpy import pytest from smqtk.algorithms import NearestNeighborsIndex from smqtk.algorithms.nn_index.flann import FlannNearestNeighborsIndex from smqtk.representation.descriptor_element.local_elements import \ DescriptorMemoryElement from smqtk.utils.configuration import configuration_test_helper # 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): """ Make an instance of FlannNearestNeighborsIndex """ return FlannNearestNeighborsIndex(distance_method=dist_method, random_seed=self.RAND_SEED) def test_impl_findable(self): # Already here because the implementation is reporting itself as
def test_has_model_data_no_uris(self): f = FlannNearestNeighborsIndex() self.assertFalse(f._has_model_data())
def _make_inst(self, dist_method): """ Make an instance of FlannNearestNeighborsIndex """ return FlannNearestNeighborsIndex(distance_method=dist_method, random_seed=self.RAND_SEED)