Python heapq.heappop() Examples

The following are 30 code examples of heapq.heappop(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module heapq , or try the search function .
Example #1
Source File: crossover.py    From gaps with MIT License 6 votes vote down vote up
def run(self):
        self._initialize_kernel()

        while len(self._candidate_pieces) > 0:
            _, (position, piece_id), relative_piece = heapq.heappop(self._candidate_pieces)

            if position in self._taken_positions:
                continue

            # If piece is already placed, find new piece candidate and put it back to
            # priority queue
            if piece_id in self._kernel:
                self.add_piece_candidate(relative_piece[0], relative_piece[1], position)
                continue

            self._put_piece_to_kernel(piece_id, position) 
Example #2
Source File: river_network.py    From terrain-erosion-3-ways with MIT License 6 votes vote down vote up
def compute_height(points, neighbors, deltas, get_delta_fn=None):
  if get_delta_fn is None:
    get_delta_fn = lambda src, dst: deltas[dst]

  dim = len(points)
  result = [None] * dim
  seed_idx = min_index([sum(p) for p in points])
  q = [(0.0, seed_idx)]

  while len(q) > 0:
    (height, idx) = heapq.heappop(q)
    if result[idx] is not None: continue
    result[idx] = height
    for n in neighbors[idx]:
      if result[n] is not None: continue
      heapq.heappush(q, (get_delta_fn(idx, n) + height, n))
  return util.normalize(np.array(result))


# Same as above, but computes height taking into account river downcutting.
# `max_delta` determines the maximum difference in neighboring points (to
# give the effect of talus slippage). `river_downcutting_constant` affects how
# deeply rivers cut into terrain (higher means more downcutting). 
Example #3
Source File: ida_utils.py    From idasec with GNU Lesser General Public License v2.1 6 votes vote down vote up
def dijkstra(graph):
        seen = set()            # élément traités
        d = {0: 0}           # distance map
        p = {}               # path map
        worklist = [(0, 0)]  # worklist d'éléments à traiter (distance, id)

        while worklist:  # tant qu'il reste des éléments dans la worklist à traiter

            dx, x_id = heappop(worklist)  # distance, and id
            if x_id in seen:                 # si l'élément est déjà traité on continue
                continue

            seen.add(x_id)                  # l'ajoute aux éléments traités

            for w, y in graph[x_id]:     # itère successeurs du noeud traité
                if y in seen:               # si le succ à déjà été traité continue
                    continue
                dy = dx + w                      # pondération du succ
                if y not in d or d[y] > dy:      # si succ n'est pas encore referencé ou new distance < alors update
                    d[y] = dy                    # met à jour la distance pour succ dans la distance map
                    heappush(worklist, (dy, y))  # met le succ dans la worklist (avec sa pondération)
                    p[y] = x_id                  # met à jour le prédecesseur le plus court pour succ
        # TODO: Do something with orphan BB
        return p 
Example #4
Source File: sqlite_udf.py    From Quiver-alfred with MIT License 6 votes vote down vote up
def finalize(self):
        if self.ct < 1:
            return
        elif self.ct == 1:
            return 0

        total = ct = 0
        dtp = None
        while self.heap:
            if total == 0:
                if dtp is None:
                    dtp = heapq.heappop(self.heap)
                    continue

            dt = heapq.heappop(self.heap)
            diff = dt - dtp
            ct += 1
            total += total_seconds(diff)
            dtp = dt

        return float(total) / ct 
Example #5
Source File: manager.py    From Paradrop with Apache License 2.0 6 votes vote down vote up
def unload(self, execute=True):
        commands = CommandList()

        undoWork = ConfigObject.prioritizeConfigs(self.currentConfig.values())
        heapq.heapify(undoWork)

        while undoWork:
            prio, config = heapq.heappop(undoWork)
            commands.extend(config.revert(self.currentConfig))

        # Finally, execute the commands.
        if execute and self.execCommands:
            self.execute(commands)

        self.previousCommands = commands
        self.currentConfig = dict()
        return True 
Example #6
Source File: sqlite_udf.py    From Quiver-alfred with MIT License 6 votes vote down vote up
def finalize(self):
        if self.ct == 0:
            return
        elif self.ct == 1:
            return 0

        prev = min_diff = None

        while self.heap:
            if min_diff is None:
                if prev is None:
                    prev = heapq.heappop(self.heap)
                    continue
            curr = heapq.heappop(self.heap)
            diff = curr - prev
            if min_diff is None or min_diff > diff:
                min_diff = diff
            prev = curr
        return min_diff 
Example #7
Source File: sqlite_udf.py    From Quiver-alfred with MIT License 6 votes vote down vote up
def finalize(self):
        if self.ct == 0:
            return
        elif self.ct == 1:
            return 0

        total = ct = 0
        prev = None
        while self.heap:
            if total == 0:
                if prev is None:
                    prev = heapq.heappop(self.heap)
                    continue

            curr = heapq.heappop(self.heap)
            diff = curr - prev
            ct += 1
            total += diff
            prev = curr

        return float(total) / ct 
Example #8
Source File: best_first_seach.py    From pynpuzzle with MIT License 6 votes vote down vote up
def search(state, goal_state, fn):
    """Best-first search"""
    queue = []
    entrance = 0
    node = Node(state)
    while not node.is_goal(goal_state):
        node.expand()
        for child in node.children:
            queue_item = (fn(child), entrance, child)
            heapq.heappush(queue, queue_item)
            entrance += 1
        node = heapq.heappop(queue)[2]

    output = []
    output.append(node.state)
    for parent in node.parents():
        output.append(parent.state)
    output.reverse()

    return output 
Example #9
Source File: Learning.py    From pneumothorax-segmentation with MIT License 6 votes vote down vote up
def post_processing(self, score, epoch, model):
        if self.freeze_model:
            return

        checkpoints_history_path = Path(
            self.checkpoints_history_folder, 
            '{}_epoch{}.pth'.format(self.calculation_name, epoch)
        )

        torch.save(self.get_state_dict(model), checkpoints_history_path)
        heapq.heappush(self.score_heap, (score, checkpoints_history_path))
        if len(self.score_heap) > self.checkpoints_topk:
            _, removing_checkpoint_path = heapq.heappop(self.score_heap)
            removing_checkpoint_path.unlink()
            self.logger.info('Removed checkpoint is {}'.format(removing_checkpoint_path))
        if score > self.best_score:
            self.best_score = score
            self.best_epoch = epoch
            torch.save(self.get_state_dict(model), self.best_checkpoint_path)
            self.logger.info('best model: {} epoch - {:.5}'.format(epoch, score))

        if self.scheduler.__class__.__name__ == 'ReduceLROnPlateau':
            self.scheduler.step(score)
        else:
            self.scheduler.step() 
Example #10
Source File: scheduled_executor.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def _loop_and_run_scheduled_events(self):
    with self._work_ready_condition:
      while not self._quit_event.is_set():
        now = time.time()
        while self._queue and self._queue[0].eta <= now:
          event = heapq.heappop(self._queue)
          if not event.cancelled:
            # Only remove uncancelled events because when an Event is cancelled,
            # its entry in _key_to_events is replaced with the replacement
            # Event.
            if event.key:
              del self._key_to_events[event.key]
            self._work_ready_condition.release()
            self._thread_pool.submit(event.run)
            self._work_ready_condition.acquire()
          now = time.time()
        if self._queue:
          self._work_ready_condition.wait(self._queue[0].eta - now)
        else:
          self._work_ready_condition.wait() 
Example #11
Source File: celebA_estimators.py    From csgm with MIT License 6 votes vote down vote up
def k_sparse_reconstr(x, k):
    coefs_list = get_wavelet(x)
    heap = get_heap(coefs_list)

    y = 0*x
    coefs_list_sparse = get_wavelet(y)
    for i in range(k):
        _, idxs_val = heapq.heappop(heap)
        if len(idxs_val) == 5:
            t, i, j, m, val = idxs_val
            coefs_list_sparse[t][i][j][m] = val
        else:
            t, i, j, m, n, val = idxs_val
            coefs_list_sparse[t][i][j][m][n] = val
    x_sparse = get_image(coefs_list_sparse)
    return x_sparse 
Example #12
Source File: Dijkstra.py    From Competitive_Programming with MIT License 6 votes vote down vote up
def calculate_distances(graph, starting_vertex):
    distances = {vertex: float('inf') for vertex in graph}
    distances[starting_vertex] = 0

    pq = [(0, starting_vertex)]
    visit = [False for vertex in graph]
    visit[starting_vertex] = True
    while len(pq) > 0:
        # print (pq)
        current_distance, current_vertex = heapq.heappop(pq)
        visit[current_vertex] = True
        for neighbor, weight in graph[current_vertex].items():
            distance = current_distance + weight
            # Only consider this new path if it's better than any path we've already found.
            if not visit[neighbor] and distance < distances[neighbor]:
                distances[neighbor] = distance
                heapq.heappush(pq, (distance, neighbor))
    return sorted(distances.items()) 
Example #13
Source File: plangraph.py    From Paradrop with Apache License 2.0 6 votes vote down vote up
def getNextTodo(self):
        """
        Like an iterator function, it returns each element in the list of plans in order.

        Returns:
            (function, args) : Each todo is returned just how the user first added it
            None             : None is returned when there are no more todo's
        """
        if len(self.workingPlans) == 0:
            return None
        else:
            prio, todo, abt = heapq.heappop(self.workingPlans)
            self.maxPriorityReturned = prio

            # After popping the next plan, check if this is a skipped function.
            # If so, then get the next plan.
            if todo.func in self.skipFunctions:
                return self.getNextTodo()
            else:
                self.abortPlans.append(abt)
                return (todo.func, todo.args) 
Example #14
Source File: cluster.py    From lingvo with Apache License 2.0 6 votes vote down vote up
def _AssignVar(self, var_op):
    size = var_op.get_attr('dtype').size
    shape = tf.TensorShape(var_op.get_attr('shape'))
    assert self._var_space_pq, ('No ps devices to use.')
    allocated, device = heapq.heappop(self._var_space_pq)
    if shape.num_elements() is None:
      assert var_op.name.endswith(
          'wb/var'), 'Unexpected name pattern: %s' % var_op.name
      # CuDNN RNN vars shape aren't known statically, decide to make a constant
      # estimate to avoid introducing more complexities.
      allocated += 10 * 1024**2 * size
    else:
      allocated += shape.num_elements() * size
    heapq.heappush(self._var_space_pq, (allocated, device))
    tf.logging.info('Place variable %s on %s %d', var_op.name, device,
                         allocated)
    return device 
Example #15
Source File: launchpad.py    From concurrency2017 with MIT License 6 votes vote down vote up
def run_until_complete(self):
        # Start all the coroutines.
        for coro in self._new:
            wait_for = coro.send(None)
            heapq.heappush(self._waiting, Task(wait_for, coro))
        # Keep running until there is no more work to do.
        while self._waiting:
            now = datetime.datetime.now()
            # Get the coroutine with the soonest resumption time.
            task = heapq.heappop(self._waiting)
            if now < task.waiting_until:
                # We're ahead of schedule; wait until it's time to resume.
                delta = task.waiting_until - now
                time.sleep(delta.total_seconds())
                now = datetime.datetime.now()
            try:
                # It's time to resume the coroutine.
                wait_until = task.coro.send(now)
                heapq.heappush(self._waiting, Task(wait_until, task.coro))
            except StopIteration:
                # The coroutine is done.
                pass 
Example #16
Source File: scheduler.py    From classification-of-encrypted-traffic with MIT License 6 votes vote down vote up
def run(self):
        self.__running = True
        while self.__running:
            self.__signal.acquire()
            if not self.__jobs:
                self.__signal.wait()

            else:
                now = datetime.datetime.now()
                while (self.__jobs[0] < now):
                    job = heapq.heappop(self.__jobs)

                    action = job()
                    if action is not False:
                        self.__runner(action)

                    heapq.heappush(self.__jobs, job)

                logging.getLogger("scheduler").debug("Sleeping %s seconds", (self.__jobs[0] - now))
                self.__signal.wait((self.__jobs[0] - now).total_seconds())

            self.__signal.release() 
Example #17
Source File: skyline.py    From FunUtils with MIT License 6 votes vote down vote up
def get_skyline(lrh):
    """
    Wortst Time Complexity: O(NlogN)
    :type buildings: List[List[int]]
    :rtype: List[List[int]]
    """
    skyline, live = [], []
    i, n = 0, len(lrh)
    while i < n or live:
        if not live or i < n and lrh[i][0] <= -live[0][1]:
            x = lrh[i][0]
            while i < n and lrh[i][0] == x:
                heapq.heappush(live, (-lrh[i][2], -lrh[i][1]))
                i += 1
        else:
            x = -live[0][1]
            while live and -live[0][1] <= x:
                heapq.heappop(live)
        height = len(live) and -live[0][0]
        if not skyline or height != skyline[-1][1]:
            skyline += [x, height],
    return skyline 
Example #18
Source File: mesh_pool.py    From MeshCNN with MIT License 6 votes vote down vote up
def __pool_main(self, mesh_index):
        mesh = self.__meshes[mesh_index]
        queue = self.__build_queue(self.__fe[mesh_index, :, :mesh.edges_count], mesh.edges_count)
        # recycle = []
        # last_queue_len = len(queue)
        last_count = mesh.edges_count + 1
        mask = np.ones(mesh.edges_count, dtype=np.bool)
        edge_groups = MeshUnion(mesh.edges_count, self.__fe.device)
        while mesh.edges_count > self.__out_target:
            value, edge_id = heappop(queue)
            edge_id = int(edge_id)
            if mask[edge_id]:
                self.__pool_edge(mesh, edge_id, mask, edge_groups)
        mesh.clean(mask, edge_groups)
        fe = edge_groups.rebuild_features(self.__fe[mesh_index], mask, self.__out_target)
        self.__updated_fe[mesh_index] = fe 
Example #19
Source File: grower.py    From pygbm with MIT License 6 votes vote down vote up
def __lt__(self, other_node):
        """Comparison for priority queue.

        Nodes with high gain are higher priority than nodes with low gain.

        heapq.heappush only need the '<' operator.
        heapq.heappop take the smallest item first (smaller is higher
        priority).

        Parameters
        -----------
        other_node : TreeNode
            The node to compare with.
        """
        if self.split_info is None or other_node.split_info is None:
            raise ValueError("Cannot compare nodes with split_info")
        return self.split_info.gain > other_node.split_info.gain 
Example #20
Source File: NetworkDelayTime_Optimized.py    From Competitive_Programming with MIT License 6 votes vote down vote up
def calculate_distances(graph, starting_vertex):
    distances = {vertex: float('inf') for vertex in graph}
    distances[starting_vertex] = 0
    pq = [(0, starting_vertex)]
    visited = 0
    visit = [False] * len(graph)
    while len(pq) > 0:
        if visited == len(graph):
            break
        # print (pq)
        current_distance, current_vertex = heapq.heappop(pq)
        if not visit[current_vertex]:
            visit[current_vertex] = True
            visited += 1
            for neighbor, weight in graph[current_vertex].items():
                distance = current_distance + weight
                if not visit[neighbor] and distance < distances[neighbor]:
                    distances[neighbor] = distance
                    heapq.heappush(pq, (distance, neighbor))
    return distances 
Example #21
Source File: Dijkstra_Optimized.py    From Competitive_Programming with MIT License 6 votes vote down vote up
def calculate_distances(graph, starting_vertex):
    distances = {vertex: float('inf') for vertex in graph}
    distances[starting_vertex] = 0
    pq = [(0, starting_vertex)]
    visited = 0
    visit = [False] * len(graph)
    while len(pq) > 0:
        if visited == len(graph):
            break
        # print (pq)
        current_distance, current_vertex = heapq.heappop(pq)
        if not visit[current_vertex]:
            visit[current_vertex] = True
            visited += 1
            for neighbor, weight in graph[current_vertex].items():
                distance = current_distance + weight
                if not visit[neighbor] and distance < distances[neighbor]:
                    distances[neighbor] = distance
                    heapq.heappush(pq, (distance, neighbor))
    return distances 
Example #22
Source File: merge_k_Sorted_lists.py    From Competitive_Programming with MIT License 6 votes vote down vote up
def merge(lists):
    merged_list = []

    heap = [(lst[0], i, 0) for i, lst in enumerate(lists) if lst]
    
    heapq.heapify(heap)

    while heap:
        #print(heap)
        val, list_ind, element_ind = heapq.heappop(heap)

        merged_list.append(val)

        if element_ind + 1 < len(lists[list_ind]):
            next_tuple = (lists[list_ind][element_ind + 1],
                          list_ind,
                          element_ind + 1)
            heapq.heappush(heap, next_tuple)
    return merged_list

#Test Cases 
Example #23
Source File: sched.py    From meddle with MIT License 5 votes vote down vote up
def queue(self):
        """An ordered list of upcoming events.

        Events are named tuples with fields for:
            time, priority, action, arguments

        """
        # Use heapq to sort the queue rather than using 'sorted(self._queue)'.
        # With heapq, two events scheduled at the same time will show in
        # the actual order they would be retrieved.
        events = self._queue[:]
        return map(heapq.heappop, [events]*len(events)) 
Example #24
Source File: sched.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def queue(self):
        """An ordered list of upcoming events.

        Events are named tuples with fields for:
            time, priority, action, arguments

        """
        # Use heapq to sort the queue rather than using 'sorted(self._queue)'.
        # With heapq, two events scheduled at the same time will show in
        # the actual order they would be retrieved.
        events = self._queue[:]
        return map(heapq.heappop, [events]*len(events)) 
Example #25
Source File: sched.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def run(self):
        """Execute events until the queue is empty.

        When there is a positive delay until the first event, the
        delay function is called and the event is left in the queue;
        otherwise, the event is removed from the queue and executed
        (its action function is called, passing it the argument).  If
        the delay function returns prematurely, it is simply
        restarted.

        It is legal for both the delay function and the action
        function to modify the queue or to raise an exception;
        exceptions are not caught but the scheduler's state remains
        well-defined so run() may be called again.

        A questionable hack is added to allow other threads to run:
        just after an event is executed, a delay of 0 is executed, to
        avoid monopolizing the CPU when other threads are also
        runnable.

        """
        # localize variable access to minimize overhead
        # and to improve thread safety
        q = self._queue
        delayfunc = self.delayfunc
        timefunc = self.timefunc
        pop = heapq.heappop
        while q:
            time, priority, action, argument = checked_event = q[0]
            now = timefunc()
            if now < time:
                delayfunc(time - now)
            else:
                event = pop(q)
                # Verify that the event was not removed or altered
                # by another thread after we last looked at q[0].
                if event is checked_event:
                    action(*argument)
                    delayfunc(0)   # Let other threads run
                else:
                    heapq.heappush(q, event) 
Example #26
Source File: Queue.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def _get(self, heappop=heapq.heappop):
        return heappop(self.queue) 
Example #27
Source File: average_precision_calculator.py    From youtube-8m with Apache License 2.0 5 votes vote down vote up
def accumulate(self, predictions, actuals, num_positives=None):
    """Accumulate the predictions and their ground truth labels.

    After the function call, we may call peek_ap_at_n to actually calculate
    the average precision.
    Note predictions and actuals must have the same shape.

    Args:
      predictions: a list storing the prediction scores.
      actuals: a list storing the ground truth labels. Any value
      larger than 0 will be treated as positives, otherwise as negatives.
      num_positives = If the 'predictions' and 'actuals' inputs aren't complete,
      then it's possible some true positives were missed in them. In that case,
      you can provide 'num_positives' in order to accurately track recall.

    Raises:
      ValueError: An error occurred when the format of the input is not the
      numpy 1-D array or the shape of predictions and actuals does not match.
    """
    if len(predictions) != len(actuals):
      raise ValueError("the shape of predictions and actuals does not match.")

    if not num_positives is None:
      if not isinstance(num_positives, numbers.Number) or num_positives < 0:
        raise ValueError("'num_positives' was provided but it wan't a nonzero number.")

    if not num_positives is None:
      self._total_positives += num_positives
    else:
      self._total_positives += numpy.size(numpy.where(actuals > 0))
    topk = self._top_n
    heap = self._heap

    for i in range(numpy.size(predictions)):
      if topk is None or len(heap) < topk:
        heapq.heappush(heap, (predictions[i], actuals[i]))
      else:
        if predictions[i] > heap[0][0]:  # heap[0] is the smallest
          heapq.heappop(heap)
          heapq.heappush(heap, (predictions[i], actuals[i])) 
Example #28
Source File: ctci-find-the-running-median.py    From hackerrank with MIT License 5 votes vote down vote up
def pop(self):
        return heappop(self.heap) 
Example #29
Source File: bag2tf.py    From udacity-driving-reader with Apache License 2.0 5 votes vote down vote up
def dequeue_samples_until(queue, timestamp):
    samples = []
    while queue and queue[0][0] < timestamp:
        samples.append(heapq.heappop(queue))
    return samples 
Example #30
Source File: quads.py    From xy with MIT License 5 votes vote down vote up
def pop(self):
        return heapq.heappop(self.heap)[-1]