Skip to content

Commit

Permalink
heapq operates on tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
MoritzWillig committed Jan 21, 2021
1 parent cd8ad5e commit 6555f5b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
25 changes: 6 additions & 19 deletions pysnic/algorithms/snic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,23 @@
from ..metric.snic import create_augmented_snic_distance


class QueueElement(object):
__sub_index = 0

def __init__(self, key, value):
self._key = key
self.value = value

def __lt__(self, other):
"""
:type other: QueueElement
"""
return self._key < other._key


class Queue(object):
def __init__(self, _buffer_size=0):
# TODO we perform a lot of single insertions
# it would be more efficient to use a cache class that already contains a buffer of the maximum heap size
# But since heapq is implemented in C, a custom python implementation will probably be slower
self.heap = []
self._sub_idx = 0

def add(self, priority, value):
heapq.heappush(self.heap, QueueElement(priority, value))
heapq.heappush(self.heap, (priority, self._sub_idx, value))
self._sub_idx += 1

def is_empty(self):
return len(self.heap) == 0

def pop_value(self):
item = heapq.heappop(self.heap)
return item.value
return heapq.heappop(self.heap)[2]

def pop(self):
return heapq.heappop(self.heap)
Expand Down Expand Up @@ -149,8 +136,8 @@ def snic(
while True:
# get pixel that has the currently smallest distance to a centroid
item = q_pop()
candidate_distance = item._key
candidate = item.value
candidate_distance = item[0]
candidate = item[2]
candidate_pos = candidate[0]

# test if pixel is not already labeled
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def readme():

setuptools.setup(
name='pysnic',
version='1.0.1',
version='1.0.2',
description='SNIC superpixels algorithm',
long_description='''
Python-only implementation of the SNIC superpixels algorithm (https://www.epfl.ch/labs/ivrl/research/snic-superpixels/).
Expand Down

0 comments on commit 6555f5b

Please sign in to comment.