def test_push_pop(self): # 1) Push 256 random numbers and pop them off, verifying all's OK. heap = [] data = [] self.check_invariant(heap) for i in range(256): item = random.random() data.append(item) heappush_max(heap, item) self.check_invariant(heap) results = [] while heap: item = heappop_max(heap) self.check_invariant(heap) results.append(item) data_sorted = data[:] data_sorted.sort(reverse=True) self.assertEqual(data_sorted, results) # 2) Check that the invariant holds for a sorted array self.check_invariant(results) self.assertRaises(TypeError, heappush_max, []) try: self.assertRaises(TypeError, heappush_max, None, None) self.assertRaises(TypeError, heappop_max, None) except AttributeError: pass
def updateNeighbors(self, currId, currNode, trainDocs, k=8): """ Takes the trained docs and updates the nearest neighbors within the train() method. k is the number of neighbors you want to consider for the classification. """ #this is part of bottleneck currNode_neighbors = [] for docId, docNode in trainDocs.items(): dist = classifierTraining.euclideanDist( currNode.doc_vec, docNode.doc_vec) #this is what makes this n^2 if len(currNode_neighbors) < k: heapq_max.heappush_max(currNode_neighbors, (dist, docId)) else: if currNode_neighbors[0][0] > dist: heapq_max.heapreplace_max(currNode_neighbors, (dist, docId)) if len(docNode.k_neighbors) < k: heapq_max.heappush_max(docNode.k_neighbors, (dist, currId)) else: if docNode.k_neighbors[0][0] > dist: heapq_max.heapreplace_max(docNode.k_neighbors, (dist, currId)) return currNode_neighbors
def writefile(number, trie, tag_dict): detail_info = {} name_list = {} # eklenen kelimeleri ismini tut for i in tag_dict: name_list[i] = [] # name_list[i].append(i) detail_info[i] = [] min_list = {} # temp min list for j in tag_dict: min_list[j] = [] for x in trie: for y in tag_dict: result = spatial.distance.cosine(trie[x], tag_dict[y]) heapq_max.heappush_max(min_list[y], (result, x)) if len(min_list[y]) > number: heapq_max.heappop_max(min_list[y]) for name in min_list: length = len(min_list[name]) for i in range(length): out = heapq_max.heappop_max(min_list[name]) if name != out[1]: detail_info[name].append(out) name_list[name].append(out[1]) detail_info[name].append((0.0, name)) name_list[name].append(name) f1 = open( "/home/asus/Desktop/Classification/lastversion/2-ChooseWordsFromGlove/Data/datanew" + str(number), "w") for y in name_list: name_list[y].reverse() # print(name_list[y]) f1.write(str(name_list[y])) f1.write("\n") f2 = open( "/home/asus/Desktop/Classification/lastversion/2-ChooseWordsFromGlove/Data/datadnew" + str(number), "w") for y in detail_info: detail_info[y].reverse() # print(detail_info[y]) f2.write(str(detail_info[y])) f2.write("\n") f3 = open( "/home/asus/Desktop/Classification/lastversion/2-ChooseWordsFromGlove/Data/datapnew" + str(number), "w") for y in name_list: # name_list[y].reverse() # print(name_list[y]) num1 = 0 for j in name_list[y]: # num1=num1+1 # if num1>1900 and num1 <2500: f3.write(str(j)) if j != name_list[y][-1]: f3.write("$#$") f3.write("\n")
def addNum(self, num: int) -> None: heapq_max.heappush_max(self.left, num) a = heapq_max.heappop_max(self.left) heapq.heappush(self.right, a) if not self.count % 2: b = heapq.heappop(self.right) heapq_max.heappush_max(self.left, b) self.count += 1
def top(iterable, key=toolz.identity): """Generates elements from max to min.""" h = [] for i, value in enumerate(iterable): # Use the index as a tie breaker. heapq_max.heappush_max(h, (key(value), i, value)) while h: yield toolz.nth(2, heapq_max.heappop_max(h))
def test_order_of_occupied_bins_correct_for_worst_fit(self): for alg in self.algorithm_executions: last_cpu_capacity = 999999 temp_heap = [] while alg.occupied_bins: bin_ = heapq_max.heappop_max(alg.occupied_bins) cpu = bin_.capacity.get("cpu") self.assertLessEqual(cpu, last_cpu_capacity) last_cpu_capacity = cpu heapq_max.heappush_max(temp_heap, bin_) alg.occupied_bins = temp_heap
def push(self, value): #print "****" ,value if len(self.__heap) < self.__max_item_count: heapq_max.heappush_max(self.__heap, value) else: if self.__heap[0][0] <= value[ 0]: #a bigger value is trying to come inside, dont let it pass else: heapq_max.heappop_max(self.__heap) #throw away heapq_max.heappush_max(self.__heap, value) #add new value
def push(self, priority, item): ''' Push an item with priority into the heap if size is fixed, push new item and pop item with lowest priority ''' if not self.size_fixed: heapq_max.heappush_max(self.h, (priority, item)) else: if len(self.h) < self.size: heapq_max.heappush_max(self.h, (priority, item)) else: _ = heapq_max.heappushpop_max(self.h, (priority, item))
def _try_to_allocate_vm(self, vm): temp_heap = [] while self.occupied_bins: # noinspection PyBroadException try: bin_ = heapq_max.heappop_max(self.occupied_bins) heapq_max.heappush_max(temp_heap, bin_) bin_.add(vm) heapq_max.heappush_max(self.occupied_bins, bin_) temp_heap.remove(bin_) while temp_heap: temp_bin = heapq_max.heappop_max(temp_heap) heapq_max.heappush_max(self.occupied_bins, temp_bin) self.vm_bins[vm.uuid] = bin_ return True except: continue else: self.occupied_bins = temp_heap if not self.bins: self._reject(vm) return False else: bin_ = self.bins.pop() bin_.add(vm) heapq_max.heappush_max(self.occupied_bins, bin_) self.vm_bins[vm.uuid] = bin_ return True
def top(iterable: Iterable, key=identity): """Generates elements from max to min. >>> tuple(top((1, 3, 2))) (3, 2, 1) >>> tuple(top(('a', 'aa', 'aaa'), len)) ('aaa', 'aa', 'a') """ h: List = [] for i, value in enumerate(iterable): # Use the index as a tie breaker. heapq_max.heappush_max(h, (key(value), i, value)) while h: yield nth(2)(heapq_max.heappop_max(h))
def get_shortest_path(ref, qry, ref_coords, species, ce, genome_size, verbose=False): if verbose: print('current species: (might be a dead end)') shortest_path = {} orange = [] heapq_max.heappush_max(orange, (1, ref, ref_coords)) shortest_path[ref] = (1.0, '', ref_coords, (), ()) while len(orange) > 0: (current_score, current_species, current_coords) = heapq_max.heappop_max(orange) if shortest_path.get(current_species, (0, ))[0] > current_score: continue # the current species was already reached by a faster path, ignore this path and go to the next species if verbose: print( current_species ) # remember: this is not necessarily going to the shortest path as it might be a dead end that doesn't lead to the qry. not all printed species are part of the shortest path! if current_species == qry: break # qry species reached, stop for nxt_species in species[species != current_species]: nxt_best_score = shortest_path.get(nxt_species, (0, ))[ 0] # current score entry for nxt_species in shortest_path if current_score < nxt_best_score: continue # if the score to current_species was lower than any previous path to nxt_species, nxt_species won't be reached faster through current_species. ignore and move on to the next species else: nxt_score, nxt_coords, current_anchors, nxt_anchors = project_genomic_location( current_species, nxt_species, current_coords, current_score, ce, genome_size) if nxt_score < nxt_best_score: continue # only save the current path to nxt_species if it was indeed faster than any previous path to it else: shortest_path[nxt_species] = (nxt_score, current_species, nxt_coords, current_anchors, nxt_anchors) heapq_max.heappush_max(orange, (nxt_score, nxt_species, nxt_coords)) shortest_path_to_qry = get_shortest_path_to_qry(qry, shortest_path) return shortest_path_to_qry, shortest_path, orange
def search(self, t, k, Q, depth=None): if distancia(t, self.loc) - self.radius >= distancia( t, Q[0][2]) or depth == 0: return Q elif self.child1 is None and self.child2 is None: c_i = 0 for p in (self.data).tolist(): if distancia(t, p) < distancia(t, Q[0][2]): point = [distancia(t, p), self.image[c_i], p] heappush_max(Q, point) if len(Q) > k: heappop_max(Q) c_i = c_i + 1 else: if self.child1 is not None: if depth is None: self.child1.search(t, k, Q) self.child2.search(t, k, Q) elif depth > 0: self.child1.search(t, k, Q, depth - 1) self.child2.search(t, k, Q, depth - 1)
def minRadius(trainDocs, docNode, j=4): #O(n) #finds the jth closest distance if j = 0 then return 0 #assume j < k -@AM rankList = [] count = 0 for docId in docNode.k_neighbors: dist = classifierTraining.euclideanDist( trainDocs[docId].doc_vec, docNode.doc_vec ) / 2.0 # should try to remove this and replace with O(1) if count < j: heapq_max.heappush_max(rankList, dist) else: if rankList[0] > dist: heapq_max.heapreplace_max( rankList, dist ) # replaces biggest dist in list with smaller one, so if k neighbors, then k-j biggest outside list so j smallest count += 1 if count is not 0: return max(rankList) else: return 0
def nearestNeighbors(trainDocs, testId, test_vec, n=1): #muse n = 1 for the refined case, use n=k for kNN approach #O(n^2) #find closest vector to new test node #returns the nearest neighbor id #optimize (should be ideally linear) neighbors = [] for currId, currNode in trainDocs.items(): if currId is not testId: # in case we use a train doc dist = classifierTraining.euclideanDist( currNode.doc_vec, test_vec) if len(neighbors) < n: heapq_max.heappush_max(neighbors, (dist, currId)) else: if neighbors[0][0] > dist: heapq_max.heapreplace_max(neighbors, (dist, currId)) neighbors = [i for d, i in neighbors] if len(neighbors) is 1: return neighbors[0] else: return neighbors
from heapq_max import * MyList = [] with open("Median_HW3.txt", "r") as f: for line in f: x = int(line.rstrip()) MyList.append(x) Heap_low = [] heapq_max.heapify_max(Heap_low) Heap_high = [] heapq.heapify(Heap_high) heapq.heappush(Heap_high, MyList[0]) heapq_max.heappush_max(Heap_low, MyList[1]) M = 6331 + 2793 for x in MyList[2:]: if x >= Heap_high[0]: heapq.heappush(Heap_high, x) else: heapq_max.heappush_max(Heap_low, x) combined_len = len(Heap_low) + len(Heap_high) if combined_len % 2 == 0: if len(Heap_high) > len(Heap_low): popped_h = heapq.heappop(Heap_high) heapq_max.heappush_max(Heap_low, popped_h) if len(Heap_high) < len(Heap_low): popped_l = heapq_max.heappop_max(Heap_low)
tiempo_1 = time.time() - t1 tiempo1.append(tiempo_1) otherpoint = np.array(query) otherpoint = otherpoint.tolist()[0] print quer mostrar_que(quer) Q = [] for op1 in range(5, 10): Q = [] for i in range(op1): point = [ distancia(otherpoint, (X.tolist())[i]), image[i], (X.tolist())[i] ] heappush_max(Q, point) t2 = time.time() BT.search(otherpoint, op1, Q) tiempo_2 = time.time() - t2 tiempo2.append(tiempo_2) cantidad.append(op1) #mostrar_res(Q) #pause() ''' formato1='' formato1=formato1+'balltree'+'_build.csv' with open(formato1, 'a') as f: writer = csv.writer(f, delimiter='\t') writer.writerows(zip(tiempo1,str(quer))) '''
def push(self, x): heappush_max(self._heap, x)
'654896547') shops['Telepizza'] = ('plaza', 'italian', 'plaza number 56', 2, 14, '698547123') shops['Dominos pizza'] = ('plaza', 'italian', 'plaza number 57', 2, 14, '659874532') shops['Damario'] = ('plaza', 'italian', 'plaza number 8', 3, 14, '698547123') shops_heap = [] #Create List so after we can convert it to heap. for keys, condiciones in shops.items( ): #Here we do a loop that will go through the dictionary. And will get the different intersections values. condicion = set(condiciones) priority = len(preferencia & condicion) #We save the lenght of the intersection. shop = (priority, keys) heapq_max.heappush_max(shops_heap, shop) #Check the restaurants that bests fits your preferences print('Restaurant info: ', shops[shops_heap[0][1]]) print(heapq_max.heappop_max(shops_heap)[1]) input('Press enter if you want to see the next restaurant') #Check the next one print('Restaurant info: ', shops[shops_heap[0][1]]) print(heapq_max.heappop_max(shops_heap)[1]) input('Press enter if you want to see the next restaurant') #Check the next one print('Restaurant info: ', shops[shops_heap[0][1]]) print(heapq_max.heappop_max(shops_heap)[1]) input('Press enter if you want to see the next restaurant')
def Push(self, value): heappush_max(self.heap, value)
import heapq import heapq_max if __name__ == '__main__': f = open('Median.txt') #f = open('Test.txt') medians = [] Hhi = [] Hlo = [] for x in f: i = int(x) if len(Hhi) == 0: heapq_max.heappush_max(Hlo, i) elif i > Hlo[0]: heapq.heappush(Hhi, i) else: heapq_max.heappush_max(Hlo, i) if len(Hhi) > len(Hlo) + 1: heapq_max.heappush_max(Hlo, heapq.heappop(Hhi)) elif len(Hlo) > len(Hhi) + 1: heapq.heappush(Hhi, heapq_max.heappop_max(Hlo)) if len(Hlo) < len(Hhi): medians.append(Hhi[0]) else: medians.append(Hlo[0]) print(medians) print(sum(medians) % 10000)
import heapq_max arr = [4, 3, 33, 90, 321, 23, 567] heapq_max.heapify_max(arr) print(arr) print(heapq_max.heappop_max(arr)) heapq_max.heappush_max(arr, 999) print(arr) heapq_max.heapreplace_max(arr, 100) print(arr)
while run == 1: startt = time.time() # for j in tag_dict: # min_list[j] = [] for x in trie: count += 1 if found_name == "": for i in tag_dict: if x not in name_list[i]: # try: result = spatial.distance.cosine(trie[x], mid_points_list[i]) # except : # print(closest_points_list[i]) heapq_max.heappush_max(min_list[i], (result, trie[x], x, i)) # pushes a new item on the heap if len(min_list[i]) > 1: heapq_max.heappop_max(min_list[i]) heapq_max.heappush_max(heap_list[i], (result, x)) if len(heap_list[i]) > 40: heapq_max.heappop_max(heap_list[i]) # if result < min_list[i]: # min_list[i]=result else: if x not in name_list[found_name]: # try: result = spatial.distance.cosine(trie[x], mid_points_list[found_name])
def writefile(number,trie,tag_dict): detail_info={} name_list = {} # eklenen kelimeleri ismini tut for i in tag_dict: name_list[i] = [] # name_list[i].append(i) detail_info[i]=[] min_list = {} # temp min list for j in tag_dict: min_list[j] = [] print("[INFO] -> 1st and 2nd loops end.") for x in trie: for y in tag_dict: result=spatial.distance.cosine(trie[x],tag_dict[y]) heapq_max.heappush_max(min_list[y],(result,x)) if len(min_list[y])>number: heapq_max.heappop_max(min_list[y]) ''' for x in trie: for y in tag_dict.keys(): vector = tag_dict.get(y) if type(vector) is list: try: print("list") result1 = spatial.distance.cosine(trie[x],vector[0]) result2 = spatial.distance.cosine(trie[x],vector[1]) minResult = min(result1, result2) heapq_max.heappush_max(min_list[y],(minResult,x)) if len(min_list[y]) > number: heapq_max.heappop_max(min_list[y]) except KeyError: print("[ERROR] -> Key error occured in 'if' ") except ValueError: print("[ERROR] -> Value error occured in 'if' ") else: print("not list") print(vector) try: result=spatial.distance.cosine(trie[x],vector) heapq_max.heappush_max(min_list[y],(result,x)) if len(min_list[y])>number: heapq_max.heappop_max(min_list[y]) except KeyError: print("[ERROR] -> Key error occured in 'else' ") except ValueError: print("[ERROR] -> Value error occured in 'else' ") ''' print("[INFO] -> 3rd loop end.") for name in min_list: length=len(min_list[name]) for i in range(length): out=heapq_max.heappop_max(min_list[name]) if name!=out[1]: detail_info[name].append(out) name_list[name].append(out[1]) detail_info[name].append((0.0,name)) name_list[name].append(name) print("[INFO] -> 4th loop end.") f1=open("./AllCatWords/words"+str(number),"w") print("[INFO] -> Writing 1st file.") for y in name_list: name_list[y].reverse() # print(name_list[y]) f1.write(str(name_list[y])) f1.write("\n") f2=open("./AllCatWords/wordsD"+str(number),"w") print("[INFO] -> Writing 2nd file.") for y in detail_info: detail_info[y].reverse() # print(detail_info[y]) f2.write(str(detail_info[y])) f2.write("\n") f3=open("./AllCatWords/wordsP"+str(number),"w") print("[INFO] -> Writing 3rd file.") for y in name_list: # name_list[y].reverse() # print(name_list[y]) num1=0 for j in name_list[y]: # num1=num1+1 # if num1>1900 and num1 <2500: f3.write(str(j)) if j!= name_list[y][-1]: f3.write("$#$") f3.write("\n")