Opened 84 minutes ago

Closed 83 minutes ago

Last modified 79 minutes ago

#20825 closed defect (fixed)

align command warns about complex values discarding imaginary part

Reported by: Eric Pettersen Owned by: Tom Goddard
Priority: moderate Milestone:
Component: Core Version:
Keywords: Cc:
Blocked By: Blocking:
Notify when closed: Platform: all
Project: ChimeraX

Description

Begin forwarded message:

From: Eric Pettersen <pett@…>
Subject: align_points ComplexWarning
Date: August 5, 2026 at 2:51:57 PM PDT
To: Tom Goddard <goddard@…>

Hi Tom,

Whilst working on the Metal Geometry tool I noted that chimerax.geometry.align_points() sometimes elicits a ComplexWarning, e.g.:

/Users/pett/src/chimerax/ChimeraX.app/Contents/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/site-packages/chimerax/geometry/align.py:68: ComplexWarning: Casting complex values to real discards the imaginary part
tf[:,:3] = R
/Users/pett/src/chimerax/ChimeraX.app/Contents/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/site-packages/chimerax/geometry/align.py:69: ComplexWarning: Casting complex values to real discards the imaginary part
tf[:,3] = ref_center - dot(R,center)
/Users/pett/src/chimerax/ChimeraX.app/Contents/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/site-packages/chimerax/geometry/align.py:76: ComplexWarning: Casting complex values to real discards the imaginary part
rms = sqrt(rms2/len(Si)) if rms2 >= 0 else 0

So I inserted a print statement at the top of align_points() to print out its parameters, and I don't see anything suspicious, and it's only one of a zillion calls to align_points() that elicits the warning. This is my personal build using numpy 2.4.6. I've attached a file of the output (since it's voluminous).

--Eric

Change History (3)

comment:1 by Tom Goddard, 84 minutes ago

Maybe issuing this warning is a new numpy 2 behavior. The numpy linalg.eig() call to get eigenvectors in exact floating point would always return real values (because the matrix is symmetric), but due to finite precision math I see it could return complex values. Maybe there is a numpy call for finding eigenvectors of a symmetric matrix that always returns real values, or I can just add an explicit cast to get the real part to avoid the warning.

comment:2 by Tom Goddard, 83 minutes ago

Resolution: fixed
Status: assignedclosed

Fixed.

Replaced numpy.linalg.eig() with numpy.linalg.eigh() which is specific to symmetric matrices and always returns real values.

comment:3 by Tom Goddard, 79 minutes ago

There is another place in the code in cluster/distmat.py that calls linalg.eig() on a symmetric (distance) matrix that should probably be changed to linalg.eigh() to avoid complex eigenvector resulting from inexact floating point. I'll let Eric handle that so he can test that the change works. The eigh() call has the same arguments and return values so it should be as simple as adding one character "h".

Note: See TracTickets for help on using tickets.