def calculate_image_similarity(self): print(self.location_id) print(self.model) print(self.image_id) query_vector_index = int( self.cache.get(self.location_id + '_' + self.model + "_" + self.image_id)) query_vector = self.reduced_data[self.location_id][query_vector_index] similar_vectors = [] for locations, images in self.reduced_data.items(): image_index = 0 for image in images: s = {} s['query_id'] = self.image_id s['compare_id'] = self.cache.get(self.location_id + '_' + self.model + "_" + str(image_index)) s['value'] = self.math.manhattan_distance(query_vector, image) similar_vectors = helpers.sort(similar_vectors, s, self.limit, 'value', 1) image_index = image_index + 1 return similar_vectors
def location_similarity(self): query_matrix = self.reduced_data[self.location_id] similar_vectors = [] for k, v in self.reduced_data.items(): s = {} s['query_id'] = self.location_id s['compare_id'] = k s['value'] = self.min_pair_similarity(query_matrix, v) similar_vectors = helpers.sort(similar_vectors, s, self.limit, 'value', 1) return similar_vectors
def self_similarity(self, query_id, limit, vector_space): ids = self.cache.hgetall("dm_"+vector_space) indices = self.cache.hgetall("idm_"+vector_space) if vector_space == "location": query_id = self.cache.hgetall("inverse_location_map")[query_id.encode('utf-8')].decode('utf-8') print(query_id) query = self.data[vector_space][int(ids[query_id.encode('utf-8')].decode('utf-8'))] similar_vectors = [] for i in range(len(self.data[vector_space])): similarity = {} similarity['value'] = self.math.cosine_similarity(self.data[vector_space][i], query) similarity['index'] = str(i) similarity['id'] = indices[str(i).encode('utf-8')] similar_vectors = helpers.sort(similar_vectors, similarity, limit, 'value', 0) return similar_vectors
def find_closest(self, key, v1, v2, k): minDist = [] average = np.zeros((len(v1[0],))) for i in range(0, len(v1)): average = average + v1[i] average = average / len(v1) for j in range(0, len(v2)): val = self.math.cosine_similarity(average, np.ravel(v2[j])) minDist.append({"distance": val, "term1":i, "term2":j}) minDist = helpers.sort(minDist, {"distance": val, "term1":i, "term2":j}, k, 'distance', 0) count = 0 l = [] while (count < k): if minDist[count]["term2"] not in l: temp = {} temp['distance'] = minDist[count]['distance'] temp['compare_id'] = self.cache.hgetall("idm_"+key)[str(minDist[count]['term2']).encode('utf-8')] l.append(temp) count = count + 1 return l