Repository navigation
Bug fixes in Bio/PDB/Vectors.py - #494
bertrand-caron wants to merge 2 commits into
Conversation
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]
|
@bertrand-caron: Can you add tests for these changes in @etal @lennax @JoaoRodrigues (etc): Can any of you review this? The change to the |
|
@pjcock : I will have a look at this a.s.a.p. Seems like a sensible fix, |
|
@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: The output on my machine is the following: The angle of the rotaxis object is fine (180 degrees), but the vector is not ! I have just discovered the bug and have not yet looked into solving it, but I thought you should know. |
|
I was looking at this for a bit but could not find a quick solution.. I'll |
|
@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
left a comment
There was a problem hiding this comment.
Is it better to cache self.norm() here rather than computing it twice?
|
@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 .. which is what we asked for. The problem seems to be that 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 |
|
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. |
|
@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 |
|
@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. |
|
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 @JoaoRodrigues It looks like a manual merge is needed, do you want to do that or should I? |
|
@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). |
See preceeding commits from pull request #494.
|
Applied at the command line (which is why GitHub doesn't see this as merged), thank you all! |
See preceeding commits from pull request biopython#494.
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) biopython#1412 (comment) - Bertrand Caron (@bertrand-caron) biopython#494 (comment) - Brian Osborne (@bosborne) biopython#898 (comment) - Christian Brueffer (@cbrueffer) biopython#898 (comment) - Brad Chapman (@chapmanb) http://mailman.open-bio.org/pipermail/biopython-dev/2013-July/019903.html - Connor McCoy (@cmccoy) biopython#898 (comment) - Eric Talevich (@etal) biopython#898 (comment) - Francesco Gastaldello (@Gasta88) biopython#1063 - Hom (@platinhom) biopython#1920 - Joe Greener (@jgreener64) biopython#1393 - Michiel de Hoon (@mdehoon) biopython#898 (comment) - @morrme biopython#1172 (comment) - Peter Cock (@peterjc) biopython#898 (comment) - Ralf Stephan (@rwst) biopython#1900 - Rasmus Fonseca (@fonseca.rasmus) biopython#1171 (comment) - Sergei Lebedev (@superbobry) biopython#898 (comment) - Sergio Valqui (@svalqui) biopython#1749 - Thomas Hamelryck (CVS username nisse) https://mailman.open-bio.org/pipermail/biopython/2018-September/016546.html
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
Hi,
I came across two bugs in Bio/PDB/Vectors.py that I think you would be interested in getting fixed.
Regards, Bertrand