Exemplo n.º 1
0
    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)
Exemplo n.º 2
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())
Exemplo n.º 3
0
 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)
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
 def test_has_model_data_empty_elements(self):
     f = FlannNearestNeighborsIndex('', '', '')
     self.assertFalse(f._has_model_data())
Exemplo n.º 6
0
 def test_has_model_data_no_uris(self):
     f = FlannNearestNeighborsIndex()
     self.assertFalse(f._has_model_data())
Exemplo n.º 7
0
 def _make_inst(self, dist_method):
     """
     Make an instance of FlannNearestNeighborsIndex
     """
     return FlannNearestNeighborsIndex(distance_method=dist_method,
                                       random_seed=self.RAND_SEED)
Exemplo n.º 8
0
 def test_build_index_no_descriptors(self):
     f = FlannNearestNeighborsIndex()
     self.assertRaises(ValueError, f.build_index, [])