0

while running groopm program from https://github.com/Ecogenomics/GroopM, I encountered the following error :

Unexpected error: <type 'exceptions.FloatingPointError'>
Traceback (most recent call last):
  File "/home/mathed/virtualenv/groopm_env/bin/groopm", line 381, in <module>
    GM_parser.parseOptions(args)
  File "/home/mathed/virtualenv/groopm_env/local/lib/python2.7/site-packages/groopm/groopm.py", line 138, in parseOptions
    gf=gf)
  File "/home/mathed/virtualenv/groopm_env/local/lib/python2.7/site-packages/groopm/cluster.py", line 217, in makeCores
    self.RE.refineBins(self.timer, auto=True, saveBins=False, plotFinal=prfx, gf=gf)
  File "/home/mathed/virtualenv/groopm_env/local/lib/python2.7/site-packages/groopm/refine.py", line 158, in refineBins
    graph = self.autoRefineBins(timer, makeGraph=gf!="")
  File "/home/mathed/virtualenv/groopm_env/local/lib/python2.7/site-packages/groopm/refine.py", line 474, in autoRefineBins
    self.mergeSimilarBins(graph=graph, verbose=False)
  File "/home/mathed/virtualenv/groopm_env/local/lib/python2.7/site-packages/groopm/refine.py", line 571, in mergeSimilarBins
    mergers = self.findMergeGroups(kCutMedian, kCutStd, cCutMedian, cCutStd, verbose=verbose)
  File "/home/mathed/virtualenv/groopm_env/local/lib/python2.7/site-packages/groopm/refine.py", line 631, in findMergeGroups
    retA=True)
  File "/home/mathed/virtualenv/groopm_env/local/lib/python2.7/site-packages/groopm/bin.py", line 352, in getBoundingKEllipseArea
    (A, center, radii, _rotation) = ET.getMinVolEllipse(KPCAs, retA=True)
  File "/home/mathed/virtualenv/groopm_env/local/lib/python2.7/site-packages/groopm/ellipsoid.py", line 112, in getMinVolEllipse
    (A, center, radii, rotation) = self.getMinVolEllipse(PP, retA=True)
  File "/home/mathed/virtualenv/groopm_env/local/lib/python2.7/site-packages/groopm/ellipsoid.py", line 112, in getMinVolEllipse
    (A, center, radii, rotation) = self.getMinVolEllipse(PP, retA=True)
  File "/home/mathed/virtualenv/groopm_env/local/lib/python2.7/site-packages/groopm/ellipsoid.py", line 153, in getMinVolEllipse
    radii = 1.0/np.sqrt(s)
FloatingPointError: divide by zero encountered in divide

at ellipsoid.py, from line 151, I have :

        try:
            U, s, rotation = linalg.svd(A)
            radii = 1.0/np.sqrt(s)
        except np.linalg.linalg.LinAlgError:
            # hack -> better than crashing...
            rotation = np.eye(3)
            radii = np.ones(3)

The ellipsoidtool whole code looks like the following :

class EllipsoidTool:
    """Some stuff for playing with ellipsoids"""
    def __init__(self): pass

    def getMinVolEllipse(self, P, tolerance=0.01, retA=False):
        """ Find the minimum volume ellipsoid which holds all the points

        Based on work by Nima Moshtagh
        http://www.mathworks.com/matlabcentral/fileexchange/9542
        and also by looking at:
        http://cctbx.sourceforge.net/current/python/scitbx.math.minimum_covering_ellipsoid.html
        Which is based on the first reference anyway!

        Here, P is a numpy array of 3D points like this:
        P = [[x,y,z],
             [x,y,z],
             [x,y,z]]

        Returns:
        (center, radii, rotation)

        """
        (N, d) = np.shape(P)

        # Q will be out working array
        Q = np.copy(P.T)
        Q = np.vstack([Q, np.ones(N)])
        QT = Q.T

        # initializations
        err = 1 + tolerance
        u = np.array([1.0 / N for i in range(N)]) # first iteration

        # Khachiyan Algorithm
        singular = False
        while err > tolerance:
            V = np.dot(Q, np.dot(np.diag(u), QT))
            try:
                M = np.diag(np.dot(QT , np.dot(linalg.inv(V), Q)))    # M the diagonal vector of an NxN matrix
            except linalg.linalg.LinAlgError:
                # most likely a singular matrix
                # permute the values a little and then we'll try again
                from random import random, randint
                PP = np.copy(P)
                for i in range(N):
                    if randint(0,3) == 0:
                        j = randint(0,2)
                        if randint(0,1) != 0:
                            PP[i,j] += random()
                        else:
                            PP[i,j] -= random()
                (A, center, radii, rotation) = self.getMinVolEllipse(PP, retA=True)
                singular = True
                break

            j = np.argmax(M)
            maximum = M[j]
            step_size = (maximum - d - 1.0) / ((d + 1.0) * (maximum - 1.0))
            new_u = (1.0 - step_size) * u
            new_u[j] += step_size
            err = np.linalg.norm(new_u - u)
            u = new_u

        if not singular:
            # center of the ellipse
            center = np.dot(P.T, u)

            # the A matrix for the ellipse
            try:
                A = linalg.inv(
                               np.dot(P.T, np.dot(np.diag(u), P)) -
                               np.array([[a * b for b in center] for a in center])
                               ) / d
            except linalg.linalg.LinAlgError:
                # the matrix is singular so we need to return a degenerate ellipse
                #print '[Notice] Degenerate ellipse constructed indicating a bin with extremely small coverage divergence.'
                center = np.mean(P, axis=0)
                radii = np.max(P,axis=0) - np.min(P, axis=0)

                if len(P[0]) == 3:
                    rotation = [[0,0,0],[0,0,0],[0,0,0]]
                else:
                    rotation = [[0,0],[0,0]]

                if retA:
                    return (None, center, radii, rotation)
                else:
                    return (center, radii, rotation)

            # Get the values we'd like to return
            try:
                U, s, rotation = linalg.svd(A)
                radii = 1.0/np.sqrt(s)
            except np.linalg.linalg.LinAlgError:
                # hack -> better than crashing...
                rotation = np.eye(3)
                radii = np.ones(3)
        else:
            # hack -> better than crashing...
            rotation = np.eye(3)
            radii = np.ones(3)
        if retA:
            return (A, center, radii, rotation)
        else:
            return (center, radii, rotation)

I am not sure why it has given me an error even though the code is in try block. Is there a way to fix this?

Thank you.

5
  • 1
    your error is not handled in your except block, they are different kinds of errors Commented Dec 6, 2018 at 23:58
  • 2
    np.linalg.linalg.LinAlgError: isn't the error you're seeing. Commented Dec 7, 2018 at 0:00
  • I see, then do I have to remove np.linalg.linalg.LinAlgError from except np.linalg.linalg.LinAlgError:? Thank you. @roganjosh Commented Dec 7, 2018 at 0:07
  • 1
    You need to catch FloatingPointError. This seems to be coming from groopm so you'll perhaps want to look into that. I don't believe I've ever encountered that error, I think it's specific to that library. Commented Dec 7, 2018 at 0:10
  • Ok, I see! Thank you very much! @roganjosh Commented Dec 7, 2018 at 0:13

1 Answer 1

1

Your except block does not handle the error being raised. It is set to handle a np.linalg.linalg.LinAlgError, but the error being raised is a FloatingPointError.

Change your code to catch either exception:

try:
    U, s, rotation = linalg.svd(A)
    radii = 1.0/np.sqrt(s)
except (np.linalg.linalg.LinAlgError, FloatingPointError):
    # hack -> better than crashing...
    rotation = np.eye(3)
    radii = np.ones(3)

The except block will now run if there is either a np.linalg.linalg.LinAlgError or a FloatingPointError.

Sign up to request clarification or add additional context in comments.

1 Comment

This has fixed my problem! Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.