本文整理汇总了Python中smqtk.representation.descriptor_index.memory.MemoryDescriptorIndex类的典型用法代码示例。如果您正苦于以下问题:Python MemoryDescriptorIndex类的具体用法?Python MemoryDescriptorIndex怎么用?Python MemoryDescriptorIndex使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MemoryDescriptorIndex类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_build_index_fresh_build
def test_build_index_fresh_build(self):
descr_index = MemoryDescriptorIndex()
hash_kvs = MemoryKeyValueStore()
index = LSHNearestNeighborIndex(DummyHashFunctor(),
descr_index, hash_kvs)
descriptors = [
DescriptorMemoryElement('t', 0),
DescriptorMemoryElement('t', 1),
DescriptorMemoryElement('t', 2),
DescriptorMemoryElement('t', 3),
DescriptorMemoryElement('t', 4),
]
# Vectors of length 1 for easy dummy hashing prediction.
for i, d in enumerate(descriptors):
d.set_vector(np.ones(1, float) * i)
index.build_index(descriptors)
# Make sure descriptors are now in attached index and in key-value-store
self.assertEqual(descr_index.count(), 5)
for d in descriptors:
self.assertIn(d, descr_index)
# Dummy hash function bins sum of descriptor vectors.
self.assertEqual(hash_kvs.count(), 5)
for i in range(5):
self.assertSetEqual(hash_kvs.get(i), {i})
示例2: test_has
def test_has(self):
i = MemoryDescriptorIndex()
descrs = [random_descriptor() for _ in xrange(10)]
i.add_many_descriptors(descrs)
ntools.assert_true(i.has_descriptor(descrs[4].uuid()))
ntools.assert_false(i.has_descriptor('not_an_int'))
示例3: test_update_index_no_existing_index
def test_update_index_no_existing_index(self):
# Test that calling update_index with no existing index acts like
# building the index fresh. This test is basically the same as
# test_build_index_fresh_build but using update_index instead.
descr_index = MemoryDescriptorIndex()
hash_kvs = MemoryKeyValueStore()
index = LSHNearestNeighborIndex(DummyHashFunctor(),
descr_index, hash_kvs)
descriptors = [
DescriptorMemoryElement('t', 0),
DescriptorMemoryElement('t', 1),
DescriptorMemoryElement('t', 2),
DescriptorMemoryElement('t', 3),
DescriptorMemoryElement('t', 4),
]
# Vectors of length 1 for easy dummy hashing prediction.
for d in descriptors:
d.set_vector(np.ones(1, float) * d.uuid())
index.update_index(descriptors)
# Make sure descriptors are now in attached index and in key-value-store
self.assertEqual(descr_index.count(), 5)
for d in descriptors:
self.assertIn(d, descr_index)
# Dummy hash function bins sum of descriptor vectors.
self.assertEqual(hash_kvs.count(), 5)
for i in range(5):
self.assertSetEqual(hash_kvs.get(i), {i})
示例4: test_get_config
def test_get_config(self):
self.assertEqual(
MemoryDescriptorIndex().get_config(),
MemoryDescriptorIndex.get_default_config()
)
self.assertEqual(
MemoryDescriptorIndex(None).get_config(),
MemoryDescriptorIndex.get_default_config()
)
empty_elem = DataMemoryElement()
self.assertEqual(
MemoryDescriptorIndex(empty_elem).get_config(),
merge_dict(MemoryDescriptorIndex.get_default_config(), {
'cache_element': {'type': 'DataMemoryElement'}
})
)
dict_pickle_bytes = pickle.dumps({1: 1, 2: 2, 3: 3}, -1)
cache_elem = DataMemoryElement(bytes=dict_pickle_bytes)
self.assertEqual(
MemoryDescriptorIndex(cache_elem).get_config(),
merge_dict(MemoryDescriptorIndex.get_default_config(), {
'cache_element': {
'DataMemoryElement': {
'bytes': dict_pickle_bytes
},
'type': 'DataMemoryElement'
}
})
)
示例5: test_count_empty_hash2uid
def test_count_empty_hash2uid(self):
"""
Test that an empty hash-to-uid mapping results in a 0 return regardless
of descriptor-set state.
"""
descr_set = MemoryDescriptorIndex()
hash_kvs = MemoryKeyValueStore()
self.assertEqual(descr_set.count(), 0)
self.assertEqual(hash_kvs.count(), 0)
lsh = LSHNearestNeighborIndex(DummyHashFunctor(), descr_set, hash_kvs)
self.assertEqual(lsh.count(), 0)
# Additions to the descriptor-set should not impact LSH index "size"
lsh.descriptor_index.add_descriptor(DescriptorMemoryElement('t', 0))
self.assertEqual(lsh.descriptor_index.count(), 1)
self.assertEqual(lsh.hash2uuids_kvstore.count(), 0)
self.assertEqual(lsh.count(), 0)
lsh.descriptor_index.add_descriptor(DescriptorMemoryElement('t', 1))
self.assertEqual(lsh.descriptor_index.count(), 2)
self.assertEqual(lsh.hash2uuids_kvstore.count(), 0)
self.assertEqual(lsh.count(), 0)
lsh.hash2uuids_kvstore.add(0, {0})
self.assertEqual(lsh.descriptor_index.count(), 2)
self.assertEqual(lsh.count(), 1)
lsh.hash2uuids_kvstore.add(0, {0, 1})
self.assertEqual(lsh.descriptor_index.count(), 2)
self.assertEqual(lsh.count(), 2)
lsh.hash2uuids_kvstore.add(0, {0, 1, 2})
self.assertEqual(lsh.descriptor_index.count(), 2)
self.assertEqual(lsh.count(), 3)
示例6: test_from_config
def test_from_config(self):
inst = MemoryDescriptorIndex.from_config({'file_cache': None})
ntools.assert_is_none(inst.file_cache)
fp = '/doesnt/exist/yet'
inst = MemoryDescriptorIndex.from_config({'file_cache': fp})
ntools.assert_equal(inst.file_cache, fp)
示例7: test_cache_table_empty_table
def test_cache_table_empty_table(self):
inst = MemoryDescriptorIndex(DataMemoryElement(), -1)
inst._table = {}
expected_table_pickle_bytes = pickle.dumps(inst._table, -1)
inst.cache_table()
self.assertIsNotNone(inst.cache_element)
self.assertEqual(inst.cache_element.get_bytes(),
expected_table_pickle_bytes)
示例8: test_clear
def test_clear(self):
i = MemoryDescriptorIndex()
n = 10
descrs = [random_descriptor() for _ in xrange(n)]
i.add_many_descriptors(descrs)
ntools.assert_equal(len(i), n)
i.clear()
ntools.assert_equal(len(i), 0)
ntools.assert_equal(i._table, {})
示例9: test_add_descriptor
def test_add_descriptor(self):
index = MemoryDescriptorIndex()
d1 = random_descriptor()
index.add_descriptor(d1)
ntools.assert_equal(index._table[d1.uuid()], d1)
d2 = random_descriptor()
index.add_descriptor(d2)
ntools.assert_equal(index._table[d2.uuid()], d2)
示例10: test_remove_from_index_shared_hashes_partial
def test_remove_from_index_shared_hashes_partial(self):
"""
Test that only some hashes are removed from the hash index, but not
others when those hashes still refer to other descriptors.
"""
# Simulate initial state with some descriptor hashed to one value and
# other descriptors hashed to another.
# Vectors of length 1 for easy dummy hashing prediction.
descriptors = [
DescriptorMemoryElement('t', 0).set_vector([0]),
DescriptorMemoryElement('t', 1).set_vector([1]),
DescriptorMemoryElement('t', 2).set_vector([2]),
DescriptorMemoryElement('t', 3).set_vector([3]),
DescriptorMemoryElement('t', 4).set_vector([4]),
]
# Dummy hash function to do the simulated thing
hash_func = DummyHashFunctor()
hash_func.get_hash = mock.Mock(
# Vectors of even sum hash to 0, odd to 1.
side_effect=lambda vec: [vec.sum() % 2]
)
d_set = MemoryDescriptorIndex()
d_set._table = {
0: descriptors[0],
1: descriptors[1],
2: descriptors[2],
3: descriptors[3],
4: descriptors[4],
}
hash2uid_kvs = MemoryKeyValueStore()
hash2uid_kvs._table = {
0: {0, 2, 4},
1: {1, 3},
}
idx = LSHNearestNeighborIndex(hash_func, d_set, hash2uid_kvs)
idx.hash_index = mock.Mock(spec=HashIndex)
idx.remove_from_index([1, 2, 3])
# Check that only one hash vector was passed to hash_index's removal
# method (deque of hash-code vectors).
idx.hash_index.remove_from_index.assert_called_once_with(
collections.deque([
[1],
])
)
self.assertDictEqual(d_set._table, {
0: descriptors[0],
4: descriptors[4],
})
self.assertDictEqual(hash2uid_kvs._table, {0: {0, 4}})
示例11: test_from_config_null_cache_elem
def test_from_config_null_cache_elem(self):
inst = MemoryDescriptorIndex.from_config({'cache_element': None})
self.assertIsNone(inst.cache_element)
self.assertEqual(inst._table, {})
inst = MemoryDescriptorIndex.from_config({
'cache_element': {
'type': None
}
})
self.assertIsNone(inst.cache_element)
self.assertEqual(inst._table, {})
示例12: test_update_index_existing_descriptors_frozenset
def test_update_index_existing_descriptors_frozenset(self):
"""
Same as ``test_update_index_similar_descriptors`` but testing that
we can update the index when seeded with structures with existing
values.
"""
# Similar Descriptors to build and update on (different instances)
descriptors1 = [
DescriptorMemoryElement('t', 0).set_vector([0]),
DescriptorMemoryElement('t', 1).set_vector([1]),
DescriptorMemoryElement('t', 2).set_vector([2]),
DescriptorMemoryElement('t', 3).set_vector([3]),
DescriptorMemoryElement('t', 4).set_vector([4]),
]
descriptors2 = [
DescriptorMemoryElement('t', 5).set_vector([0]),
DescriptorMemoryElement('t', 6).set_vector([1]),
DescriptorMemoryElement('t', 7).set_vector([2]),
DescriptorMemoryElement('t', 8).set_vector([3]),
DescriptorMemoryElement('t', 9).set_vector([4]),
]
descr_index = MemoryDescriptorIndex()
descr_index.add_many_descriptors(descriptors1)
hash_kvs = MemoryKeyValueStore()
hash_kvs.add(0, frozenset({0}))
hash_kvs.add(1, frozenset({1}))
hash_kvs.add(2, frozenset({2}))
hash_kvs.add(3, frozenset({3}))
hash_kvs.add(4, frozenset({4}))
index = LSHNearestNeighborIndex(DummyHashFunctor(),
descr_index, hash_kvs)
index.update_index(descriptors2)
assert descr_index.count() == 10
# Above descriptors should be considered "in" the descriptor set now.
for d in descriptors1:
assert d in descr_index
for d in descriptors2:
assert d in descr_index
# Known hashes of the above descriptors should be in the KVS
assert set(hash_kvs.keys()) == {0, 1, 2, 3, 4}
assert hash_kvs.get(0) == {0, 5}
assert hash_kvs.get(1) == {1, 6}
assert hash_kvs.get(2) == {2, 7}
assert hash_kvs.get(3) == {3, 8}
assert hash_kvs.get(4) == {4, 9}
示例13: test_clustering_equal_descriptors
def test_clustering_equal_descriptors(self):
# Test that clusters of descriptor of size n-features are correctly
# clustered together.
print("Creating dummy descriptors")
n_features = 8
n_descriptors = 20
index = MemoryDescriptorIndex()
c = 0
for i in range(n_features):
v = numpy.ndarray((8,))
v[...] = 0
v[i] = 1
for j in range(n_descriptors):
d = DescriptorMemoryElement('test', c)
d.set_vector(v)
index.add_descriptor(d)
c += 1
print("Creating test MBKM")
mbkm = MiniBatchKMeans(n_features, batch_size=12, verbose=True,
compute_labels=False, random_state=0)
# Initial fit with half of index
d_classes = mb_kmeans_build_apply(index, mbkm, n_descriptors)
# There should be 20 descriptors per class
for c in d_classes:
self.assertEqual(
len(d_classes[c]),
n_descriptors,
"Cluster %s did not have expected number of descriptors "
"(%d != %d)"
% (c, n_descriptors, len(d_classes[c]))
)
# Each descriptor in each cluster should be equal to the other
# descriptors in that cluster
uuids = list(d_classes[c])
v = index[uuids[0]].vector()
for uuid in uuids[1:]:
v2 = index[uuid].vector()
numpy.testing.assert_array_equal(v, v2,
"vector in cluster %d did not "
"match other vectors "
"(%s != %s)"
% (c, v, v2))
示例14: test_added_descriptor_table_caching
def test_added_descriptor_table_caching(self):
cache_elem = DataMemoryElement(readonly=False)
descrs = [random_descriptor() for _ in range(3)]
expected_table = dict((r.uuid(), r) for r in descrs)
i = MemoryDescriptorIndex(cache_elem)
self.assertTrue(cache_elem.is_empty())
# Should add descriptors to table, caching to writable element.
i.add_many_descriptors(descrs)
self.assertFalse(cache_elem.is_empty())
self.assertEqual(pickle.loads(i.cache_element.get_bytes()),
expected_table)
# Changing the internal table (remove, add) it should reflect in
# cache
new_d = random_descriptor()
expected_table[new_d.uuid()] = new_d
i.add_descriptor(new_d)
self.assertEqual(pickle.loads(i.cache_element.get_bytes()),
expected_table)
rm_d = list(expected_table.values())[0]
del expected_table[rm_d.uuid()]
i.remove_descriptor(rm_d.uuid())
self.assertEqual(pickle.loads(i.cache_element.get_bytes()),
expected_table)
示例15: test_count
def test_count(self):
index = MemoryDescriptorIndex()
ntools.assert_equal(index.count(), 0)
d1 = random_descriptor()
index.add_descriptor(d1)
ntools.assert_equal(index.count(), 1)
d2 = random_descriptor()
index.add_descriptor(d2)
ntools.assert_equal(index.count(), 2)