Skip to content

Estimating Ellipse Sometimes fails due to np.linalg.eig Casting to complex #7013

Open

Description

@CSSFrancis

Description:

It seems like in the EllipseModel.fit function np.linalg.eig is called which returns either a complex matrix or casts to a real matrix if the matrix has no complex component. See here

The relevant code is found here:

eig_vals, eig_vecs = np.linalg.eig(M)

We have been running into a problem where occasionally the np.linalg.eig() function will return a very small complex component some times. Most likely this is a result of some very small floating point error, maybe from the casting to 32 bit floats?

The code fails when it tries to find the remainder of a complex and float dtype:

phi %= np.pi

If you want to see the failing CI check here

The solution here is to change it to

eig_vals, eig_vecs = np.linalg.eig(M).real

Way to reproduce:

It seems to fail about every 6-7 times if you want to test it.

import numpy as np
from skimage.measure.fit import EllipseModel

e = EllipseModel()

for i in range(100):
    rand = np.random.rand(4,2)
    print(rand)
    e.estimate(rand)

This rarely would come up except if you are using something like np.random as most people don't have floats that extend to the full bit depth.

Version information:

3.8.10 (default, May 26 2023, 14:05:08) 
[GCC 9.4.0]
Linux-5.15.0-73-generic-x86_64-with-glibc2.29
scikit-image version: 0.21.0
numpy version: 1.21.6

Activity

  1. stefanv commented on Jun 8, 2023

    @stefanv
    Member

    Thanks @CSSFrancis! Are you able to come up with a reproducible test case, and add the fix along with a test as a PR?

  2. CSSFrancis commented on Jun 8, 2023

    @CSSFrancis
    Author

    @stefanv I don't exactly know about a reproducible test case. I've played around with it and most of the failing cases require floats with lots of precision. It might be hardware/operating system dependent as well but I'm not sure.

    I can make a PR with the fix and maybe we can think about how to best test this.

  3. stefanv commented on Jun 8, 2023

    @stefanv
    Member

    Thanks @CSSFrancis, that works.

  4. github-actions commented on Mar 17, 2024

    @github-actions

    Hello scikit-image core devs! There hasn't been any activity on this issue for more than 180 days. I have marked it as "dormant" to make it easy to find.
    To our contributors, thank you for your contribution and apologies if this issue fell through the cracks! Hopefully this ping will help bring some fresh attention to the issue. If you need help, you can always reach out on our forum
    If you think that this issue is no longer relevant, you may close it, or we may do it at some point (either way, it will be done manually).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 Bug😴 Dormant

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions