Skip to content

Bug fixes in Bio/PDB/Vectors.py - #494

Closed
bertrand-caron wants to merge 2 commits into
biopython:masterfrom
bertrand-caron:master
Closed

bertrand-caron wants to merge 2 commits into
biopython:masterfrom
bertrand-caron:master

Conversation

@bertrand-caron

Copy link
Copy Markdown
Contributor

Hi,
I came across two bugs in Bio/PDB/Vectors.py that I think you would be interested in getting fixed.

Regards, Bertrand

Calling rotmat([0,0,0],[0,1,0]), which is a valid rotation would
call normalize that will set p to [Nan, Nan, Nan] as it norm is null.
To my understanding, that is a bug and not a feature.
Instead of normalizing a copy of the p and q vectors, refmat would
normalize p and q themselves!
As a conseauence, calling rotmat on two vectors would normalize them,
which is a huge side-effect that I am pretty sure is a bug and not a
feature.

NB: This bug was discovered while fixing the previous one, as I was
curious to know while calling rotmat([0,0,0],[0,1,0]) would
**set** the first vector to [Nan, Nan, Nan]
@peterjc

peterjc commented Mar 26, 2015

Copy link
Copy Markdown
Member

@bertrand-caron: Can you add tests for these changes in Tests/test_PDB.py? (There is chapter on the test framework in the main Biopython tutorial).

@etal @lennax @JoaoRodrigues (etc): Can any of you review this? The change to the refmat method to remove the side effect looks sensible but is it likely anyone is relying on the undocumented behaviour?

@JoaoRodrigues

Copy link
Copy Markdown
Member

@pjcock : I will have a look at this a.s.a.p. Seems like a sensible fix,
I'll quickly check if there are any changes in some of my scripts where I
use this.

@bertrand-caron

Copy link
Copy Markdown
Contributor Author

@peterjc I'd be glad to do so when I find the time to (not in the next 4 days for sure, I apologise for that ...).

While playing with this Vector module, I actually came upon a much more painful bug that is reproducible with the following snippet:

from Vector import Vector, rotmat, m2rotaxis
from numpy import array, dot
#Matrix rotating <Vector 1.00, 0.80, 0.00> onto <Vector -1.00, -0.80, 0.00>:
m = rotmat(Vector([1.00, 0.80, 0.00]), Vector([-1.00, -0.80, 0.00]))
print m2rotaxis(m)
print m

The output on my machine is the following:

(1.570796326794897, <Vector 0.00, 0.00, 0.00>)
[[-0.2195122  -0.97560976  0.        ]
 [-0.97560976  0.2195122   0.        ]
 [ 0.          0.          1.        ]]

The angle of the rotaxis object is fine (180 degrees), but the vector is not !
Furthermore, the rotation matrix is completely wrong, it should be [[-1,0,0],[0,-1,0],[0,0,1]].

I have just discovered the bug and have not yet looked into solving it, but I thought you should know.
More to come in a couple days as I am off tonight until Monday (Australian time).

@JoaoRodrigues

Copy link
Copy Markdown
Member

I was looking at this for a bit but could not find a quick solution.. I'll
have a better look next week.

@peterjc

peterjc commented Apr 11, 2017

Copy link
Copy Markdown
Member

@bertrand-caron @JoaoRodrigues did you ever did deeper into the rotation issue?

Does it make sense to apply the proposed pull request as is?

@peterjc peterjc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to cache self.norm() here rather than computing it twice?

@RasmusFonseca

RasmusFonseca commented Apr 18, 2017 •

Copy link
Copy Markdown
Contributor

@bertrand-caron nothing seems to be wrong with the rotation matrix. If I type

from Bio.PDB.Vector import Vector, rotmat, m2rotaxis
from numpy import array, dot
m = rotmat(Vector([1.00, 0.80, 0.00]), Vector([-1.00, -0.80, 0.00]))
print(Vector([1,0.8,0]).left_multiply(m))

I get

<Vector -1.00, -0.80, 0.00>

.. which is what we asked for. The problem seems to be that m2rotaxis doesn't properly check for singularities (i.e. 0 deg and 180 deg rotation). Note that the returned angle (1.570796326794897) is not 180 degs but 90 degs. @JoaoRodrigues Based on this walkthrough I added a couple of if-statements to get the angle right:

def m2rotaxis(m):
    """
    Return angles, axis pair that corresponds to rotation matrix m.
    """
    eps = 1e-5

    #Check for singularities a la http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/
    if ( abs(m[0,1]-m[1,0]) < eps and 
         abs(m[0,2]-m[2,0]) < eps and 
         abs(m[1,2]-m[2,1]) < eps 
    ):
        #Singularity encountered. Check if its 0 or 180 deg
        if ( abs(m[0,1]+m[1,0]) < eps and 
             abs(m[0,2]+m[2,0]) < eps and 
             abs(m[1,2]+m[2,1]) < eps and
             abs(m[0,0]+m[1,1]+m[2,2]-3) < eps
        ):
            angle = 0
        else:
            angle = numpy.pi
    else:
        # Angle always between 0 and pi
        # Sense of rotation is defined by axis orientation
        t = 0.5 * (numpy.trace(m) - 1)
        t = max(-1, t)
        t = min(1, t)
        angle = numpy.arccos(t)

    if angle < 1e-15:
        # Angle is 0
        return 0.0, Vector(1, 0, 0)
    elif angle < numpy.pi:
        # Angle is smaller than pi
        x = m[2, 1] - m[1, 2]
        y = m[0, 2] - m[2, 0]
        z = m[1, 0] - m[0, 1]
        axis = Vector(x, y, z)
        axis.normalize()
        return angle, axis
    else:
        # Angle is pi - special case!
        m00 = m[0, 0]
        m11 = m[1, 1]
        m22 = m[2, 2]
        if m00 > m11 and m00 > m22:
            x = numpy.sqrt(m00 - m11 - m22 + 0.5)
            y = m[0, 1] / (2 * x)
            z = m[0, 2] / (2 * x)
        elif m11 > m00 and m11 > m22:
            y = numpy.sqrt(m11 - m00 - m22 + 0.5)
            x = m[0, 1] / (2 * y)
            z = m[1, 2] / (2 * y)
        else:
            z = numpy.sqrt(m22 - m00 - m11 + 0.5)
            x = m[0, 2] / (2 * z)
            y = m[1, 2] / (2 * z)
        axis = Vector(x, y, z)
        axis.normalize()
        return numpy.pi, axis

@peterjc

peterjc commented Apr 18, 2017

Copy link
Copy Markdown
Member

Ah, matrix singularities are a pain - I know that much from following the NumPy mailing list, but haven't had to deal with that kind of thing for a long time. I have no advice to offer here.

@JoaoRodrigues

Copy link
Copy Markdown
Member

@peterjc I can't offer any either, so I asked @RasmusFonseca (lab mate) for his advice :)

@RasmusFonseca if you are sure this fixes (or improves) the module, then go ahead and make a pull request. You also mentioned yesterday (offline) that there were some issues with the code silently changing vectors in some operations, so maybe adding a few in_place function options would be good. Thanks!!

@RasmusFonseca

Copy link
Copy Markdown
Contributor

@JoaoRodrigues Ill test it properly and create a pull req within the next couple of days.

The problem with changing state of the vectors when normalizing is solved by one of @bertrand-caron's original commits. All his suggestions look completely sensible to me.

@peterjc

peterjc commented Apr 18, 2017

Copy link
Copy Markdown
Member

Thanks for reviewing this @RasmusFonseca

@bertrand-caron Are you happy to dual licence your contributions under both the Biopython License Agreement AND the BSD 3-Clause License? See #898.

Also, are you happy to be thanked by name in the NEWS.rst and CONTRIB.rst files?

@JoaoRodrigues It looks like a manual merge is needed, do you want to do that or should I?

@JoaoRodrigues

Copy link
Copy Markdown
Member

@peterjc I'll handle that. I'll talk to Rasmus about this too and we can do it together (PR and then merge if it passes tests).

@bertrand-caron

Copy link
Copy Markdown
Contributor Author

@ALL Sorry for leaving this one hanging for so long.

@peterjc I am fine with the licensing, and I'd be thrilled to be thanked for such a trivial fix ;)

@peterjc peterjc mentioned this pull request Apr 19, 2017
peterjc added a commit that referenced this pull request Apr 19, 2017
See preceeding commits from pull request #494.
@peterjc

peterjc commented Apr 19, 2017

Copy link
Copy Markdown
Member

Applied at the command line (which is why GitHub doesn't see this as merged), thank you all!

@peterjc peterjc closed this Apr 19, 2017
MarkusPiotrowski pushed a commit to MarkusPiotrowski/biopython that referenced this pull request Oct 31, 2017
See preceeding commits from pull request biopython#494.
peterjc added a commit to peterjc/biopython that referenced this pull request Aug 14, 2019
peterjc added a commit that referenced this pull request Aug 29, 2019
Original author Thomas Hamelryck (when the file was called
Bio/PDB/Vector.py) and all the contributors since as tracked
with version control have agreed:

 - Adhemar Zerlotini (@azneto)
   #1412 (comment)
 - Bertrand Caron (@bertrand-caron)
   #494 (comment)
 - Brian Osborne (@bosborne)
   #898 (comment)
 - Christian Brueffer (@cbrueffer)
   #898 (comment)
 - Brad Chapman (@chapmanb)
   http://mailman.open-bio.org/pipermail/biopython-dev/2013-July/019903.html
 - Connor McCoy (@cmccoy)
   #898 (comment)
 - Eric Talevich (@etal)
   #898 (comment)
 - Francesco Gastaldello (@Gasta88)
   #1063
 - Hom (@platinhom)
   #1920
 - Joe Greener (@jgreener64)
   #1393
 - Michiel de Hoon (@mdehoon)
   #898 (comment)
 - @morrme
   #1172 (comment)
 - Peter Cock (@peterjc)
   #898 (comment)
 - Ralf Stephan (@rwst)
   #1900
 - Rasmus Fonseca (@fonseca.rasmus)
   #1171 (comment)
 - Sergei Lebedev (@superbobry)
   #898 (comment)
 - Sergio Valqui (@svalqui)
   #1749
 - Thomas Hamelryck (CVS username nisse)
   https://mailman.open-bio.org/pipermail/biopython/2018-September/016546.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants