Skip to content

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

@CSSFrancis

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions