fhirschmann / rdp

Python/Numpy implementation of the Ramer-Douglas-Peucker algorithm
https://pypi.python.org/pypi/rdp
MIT License
244 stars 117 forks source link

Bizarre np.linalg.norm error #6

Closed hardmaru closed 7 years ago

hardmaru commented 7 years ago

Hi fhirschmann,

Thanks for writing this library, it's immensely useful for me. I'm trying to use it in some neural network applications where I'm simplifying the data I am analyzing - mainly vector images.

Occasionally I get a weird error thrown from the rdp code from the np.linalg.norm calls: "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()". It works on 95% of the time though. I wonder if you have also encountered this issue before. If not, I'll try to do more digging to see what is up with this.

Cheers.

fhirschmann commented 7 years ago

Hi hardmaru,

I have seen that error before, but I was unable to pinpoint the root cause. Do you know what the input data in that case looked like? Is it possible that it was an empty array?

You can also try if it works with an older version: https://pypi.python.org/packages/6c/4e/7ab95aad0a39c5d938014344cc321be6012b4156226d04ab3803b759a35c/rdp-0.6.tar.gz#md5=0e5de170f1322ef2d9e390a4502b5d0e

If that works it may be a regression bug.

I really like your work by the way! :)

hardmaru commented 7 years ago

Thanks, I'll try to look to find the root cause.

On Mon, Nov 28, 2016 at 9:01 PM Fabian Hirschmann notifications@github.com wrote:

Hi hardmaru,

I have seen that error before, but I was unable to pinpoint the root cause. Do you know what the input data in that case looked like? Is it possible that it was an empty array?

You can also try if it works with an older version: https://pypi.python.org/packages/6c/4e/7ab95aad0a39c5d938014344cc321be6012b4156226d04ab3803b759a35c/rdp-0.6.tar.gz#md5=0e5de170f1322ef2d9e390a4502b5d0e

If that works it may be a regression bug.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fhirschmann/rdp/issues/6#issuecomment-263392826, or mute the thread https://github.com/notifications/unsubscribe-auth/AGBoHsBREbJFOv-o-g6d6-mjwxMrsfzGks5rC0EegaJpZM4K8zWG .

hardmaru commented 7 years ago

Hi fhirschmann,

It seems that is the case. An example of a line where rdp was breaking is:

line = [[53, 90],[52, 89],[51, 89],[50, 89],[50, 90],[50, 91],[52, 91],[56, 91],[56, 90],[55, 90],[54, 90],[53, 90]]

If I use the pl_dist in the older version, and rename it pldist_old

def pldist_old(x0, x1, x2):
    """
    Calculates the distance from the point ``x0`` to the line given
    by the points ``x1`` and ``x2``.

    :param x0: a point
    :type x0: a 2x1 numpy array
    :param x1: a point of the line
    :type x1: 2x1 numpy array
    :param x2: another point of the line
    :type x2: 2x1 numpy array
    """
    if x1[0] == x2[0]:
        return np.abs(x0[0] - x1[0])

    return np.divide(np.linalg.norm(np.linalg.det([x2 - x1, x1 - x0])),
                     np.linalg.norm(x2 - x1))

After I call rdp(line, epsilon=0.99, dist=pldist_old), we can now get reduced line:

[[53, 90], [50, 89], [50, 91], [56, 91], [53, 90]]

But if we call rdp(line, epsilon=0.99, dist=pldist), the error pops up:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I'm not sure the best way to get this back into your code, but it's very helpful to have resolved this!

Cheers.

fhirschmann commented 7 years ago

Hi,

Thanks for providing a failing example. I'll be at NIPS next week and will, hence, look into this the week after next week.

On Dec 2, 2016 11:56 AM, "hardmaru" notifications@github.com wrote:

Hi fhirschmann,

It seems that is the case. An example of a line where rdp was breaking is:

line = [[53, 90],[52, 89],[51, 89],[50, 89],[50, 90],[50, 91],[52, 91],[56, 91],[56, 90],[55, 90],[54, 90],[53, 90]]

If I use the pl_dist in the older version, and rename it pldist_old

def pldist_old(x0, x1, x2): """ Calculates the distance from the point x0 to the line given by the points x1 and x2.

:param x0: a point
:type x0: a 2x1 numpy array
:param x1: a point of the line
:type x1: 2x1 numpy array
:param x2: another point of the line
:type x2: 2x1 numpy array
"""
if x1[0] == x2[0]:
    return np.abs(x0[0] - x1[0])

return np.divide(np.linalg.norm(np.linalg.det([x2 - x1, x1 - x0])),
                 np.linalg.norm(x2 - x1))

After I call rdp(line, epsilon=0.99, dist=pldist_old), we can now get reduced line:

[[53, 90], [50, 89], [50, 91], [56, 91], [53, 90]]

But if we call rdp(line, epsilon=0.99, dist=pldist), the error pops up:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I'm not sure the best way to get this back into your code, but it's very helpful to have resolved this!

Cheers.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/fhirschmann/rdp/issues/6#issuecomment-264428528, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAvqI_ruZpgjdBhcP5y2S3-6rek0Cqaks5rD_l6gaJpZM4K8zWG .

hardmaru commented 7 years ago

me too, will be at nips

On Fri, Dec 2, 2016 at 7:24 PM Fabian Hirschmann notifications@github.com wrote:

Hi,

Thanks for providing a failing example. I'll be at NIPS next week and will,

hence, look into this the week after next week.

On Dec 2, 2016 11:56 AM, "hardmaru" notifications@github.com wrote:

Hi fhirschmann,

It seems that is the case. An example of a line where rdp was breaking is:

line = [[53, 90],[52, 89],[51, 89],[50, 89],[50, 90],[50, 91],[52, 91],[56, 91],[56, 90],[55, 90],[54, 90],[53, 90]]

If I use the pl_dist in the older version, and rename it pldist_old

def pldist_old(x0, x1, x2):

"""

Calculates the distance from the point x0 to the line given

by the points x1 and x2.

:param x0: a point

:type x0: a 2x1 numpy array

:param x1: a point of the line

:type x1: 2x1 numpy array

:param x2: another point of the line

:type x2: 2x1 numpy array

"""

if x1[0] == x2[0]:

return np.abs(x0[0] - x1[0])

return np.divide(np.linalg.norm(np.linalg.det([x2 - x1, x1 - x0])),

np.linalg.norm(x2 - x1))

After I call rdp(line, epsilon=0.99, dist=pldist_old), we can now get

reduced line:

[[53, 90], [50, 89], [50, 91], [56, 91], [53, 90]]

But if we call rdp(line, epsilon=0.99, dist=pldist), the error pops up:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I'm not sure the best way to get this back into your code, but it's very

helpful to have resolved this!

Cheers.

You are receiving this because you commented.

Reply to this email directly, view it on GitHub

https://github.com/fhirschmann/rdp/issues/6#issuecomment-264428528, or mute

the thread

< https://github.com/notifications/unsubscribe-auth/AAAvqI_ruZpgjdBhcP5y2S3-6rek0Cqaks5rD_l6gaJpZM4K8zWG

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fhirschmann/rdp/issues/6#issuecomment-264525186, or mute the thread https://github.com/notifications/unsubscribe-auth/AGBoHiUWE6Z3P2IMEw4-4RtI6lgfBXd3ks5rEGJ6gaJpZM4K8zWG .

robertrlindner commented 7 years ago

Hi all, I'm using rdp version (0.7) and experiencing the same error. I believe it's coming from line 33:

return np.linalg.norm(point, start)

Based on what pldist is trying to compute, and the inputs for linalg.norm, I think this should simply be:

return np.linalg.norm(point - start).

Let me know what you think, Thanks, Bob