Skip to content

TypeError: '<' not supported between instances of 'Cell' and 'Cell' #5

Closed

Description

@adnanobot

Hi,

when I provide input like the following,
coordinates = [[[100, 0], [101, 0], [110, 10], [100, 1], [100, 0]]]

Then it's ok and I am getting the output, Centroid: [101.25, 1.25]

But if I just change the second coordinate,
coordinates = [[[100, 0], [105, 0], [110, 10], [100, 1], [100, 0]]]

then I get the following error:
Traceback (most recent call last): File "curve2poly.py", line 75, in <module> centroid = polylabel(x) File "C:\Users\mahmad\vision\lib\site-packages\polylabel\__init__.py", line 138, in polylabel _, __, cell = cell_queue.get() File "C:\Program Files\Python37\lib\queue.py", line 180, in get item = self._get() File "C:\Program Files\Python37\lib\queue.py", line 236, in _get return heappop(self.queue) TypeError: '<' not supported between instances of 'Cell' and 'Cell'

I am not getting what kind of input polylabel() takes? It should work with any shape and size of polygons, right?

Update: It is working fine in ubuntu but not in windows.

Activity

  1. changed the title [-]Input coordinate type[/-] [+]TypeError: '<' not supported between instances of 'Cell' and 'Cell'[/+] on Jul 25, 2019
  2. Twista commented on Jul 25, 2019

    @Twista
    Owner

    That is interesting. I just tried to run that code in python3.7 (as it seems your machine is using it) and everything works great.

    I have made test case for it, but I have no idea where to look, as I have no access to windows computer.

    Would you mind to take a closer look?

  3. added a commit that references this issue on Jul 25, 2019
  4. adnanobot commented on Jul 25, 2019

    @adnanobot
    Author

    Yes, I have found the solution by modifying the Cell class in __init__.py.
    I followed the suggestion from here: https://github.com/laurentluce/python-algorithms/issues/6.

    So I have also modified the Cell class:

    class Cell(object):
        def __init__(self, x, y, h, polygon):
            self.h = h
            self.y = y
            self.x = x
            self.d = _point_to_polygon_distance(x, y, polygon)
            self.max = self.d + self.h * sqrt(2)
    
        def __lt__(self, other):
            return self.max < other.max

    Now it is working but I did not understand his explanation. Can you please explain the reason?

  5. Twista commented on Jul 26, 2019

    @Twista
    Owner

    It seems that in some cases (where values are same) queue is trying to sort elements. and to perform sorting in needs to compare two elements which has higher/lower elements. Therefore it performs < operator on to the Cell class - which doesn't support it.

    by declaring __lt__ you tell python how < (less-than) should behave.

    Thanks a lot for checking it and providing a solution. I will add it to the code-base and create new package version soon :)

  6. added a commit that references this issue on Jul 26, 2019
    5e42f7b
  7. Twista commented on Jul 26, 2019

    @Twista
    Owner

    version 0.6 released. feel free to update :))

  8. adnanobot commented on Jul 26, 2019

    @adnanobot
    Author

    ok...got it! thanks for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions