def sumidxtab(self, D, ivfidx): """ Compute distance to database items based on distances to centroids. D: nsubq x ksub """ ids = self.storage[ivfidx].get_keys() if ids.shape[0] == 0: dis = np.ndarray(0) else: dis = cext.sumidxtab_core(D, self.storage[ivfidx].get_codes()) return np.array(ids), np.array(dis) """ Deprecated code """ num_candidates = self.storage[ivfidx].get_num_items() dis = np.zeros(num_candidates) ids = np.arange(0) start_id = 0 for keys, blk in self.storage[ivfidx]: cur_num = blk.shape[0] # dis[start_id:start_id+cur_num] = self.sumidxtab_core(D, blk) dis[start_id : start_id + cur_num] = cext.sumidxtab_core(D, blk) start_id += cur_num ids = np.hstack((ids, keys)) return ids, dis
def sumidxtab(self, D, ivfidx): """ Compute distance to database items based on distances to centroids. D: nsubq x ksub """ ids = self.storage[ivfidx].get_keys() if ids.shape[0] == 0: dis = np.ndarray(0) else: dis = cext.sumidxtab_core(D, self.storage[ivfidx].get_codes()) return np.array(ids), np.array(dis) """ Deprecated code """ num_candidates = self.storage[ivfidx].get_num_items() dis = np.zeros(num_candidates) ids = np.arange(0) start_id = 0 for keys, blk in self.storage[ivfidx]: cur_num = blk.shape[0] # dis[start_id:start_id+cur_num] = self.sumidxtab_core(D, blk) dis[start_id:start_id + cur_num] = cext.sumidxtab_core(D, blk) start_id += cur_num ids = np.hstack((ids, keys)) return ids, dis
def sumidxtab(self, D): """ Compute distance to database items based on distances to centroids. D: nsubq x ksub """ ids = self.storage.get_keys() dis = cext.sumidxtab_core(D, self.storage.get_codes()) return np.array(ids), np.array(dis) """
def sumidxtab(self, D): """ Compute distance to database items based on distances to centroids. D: nsubq x ksub """ ids = self.storage.get_keys() dis = cext.sumidxtab_core(D, self.storage.get_codes()) return np.array(ids), np.array(dis) """
def test_sumidxtab_core(self): nsq = 8 ksub = 256 cur_num = 10 for iround in range(10): raw_D = np.random.random((nsq, ksub)) raw_blk = np.random.random_integers(0, ksub-1, (cur_num, nsq)) D = np.require(raw_D, np.float32, "C") blk = np.require(raw_blk, np.uint8, "C") self.assertLessEqual(np.abs(raw_D - D).sum(), 1e-4) self.assertEqual(np.abs(raw_blk - blk).sum(), 0) py_res = self.sumidxtab_core(D, blk) c_res = cext.sumidxtab_core(D, blk) self.assertLessEqual(np.abs(py_res - c_res).sum(), 1e-4)