Skip to content

moved self-test from Vector.py - #1127

Merged
peterjc merged 13 commits into
biopython:masterfrom
Gasta88:Gasta88-patch-3
Apr 6, 2017
Merged

peterjc merged 13 commits into
biopython:masterfrom
Gasta88:Gasta88-patch-3

Conversation

@Gasta88

@Gasta88 Gasta88 commented Mar 30, 2017

Copy link
Copy Markdown
Contributor

In reference to issue #820, I've moved the self test part of Bio/PDB/Vector.py into Tests/test_PDB.py.

Gasta88 added 3 commits March 10, 2017 11:03
deleted self test and placed in `Tests/test_PDB.py`
moved `Bio/PDB/Vector.py` test inside method test_Vector
Comment thread Bio/PDB/Vector.py Outdated

class Vector(object):
"""3D vector."""
"3D vector"

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.

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.

This is why the style check is failing under TravisCI, lots of entries like this from the pydocstyle tool;

D300: Use """triple double quotes""" (found '-quotes)

You can install this code checking tool with pip install pydocstyle

Comment thread Bio/PDB/Vector.py
import numpy


def m2rotaxis(m):

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.

Why are you removing this function (and the others after it) from Bio.Vector? They are part of the module's public API and should stay here.

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.

This explains why many of the tests are failing on TravisCI, e.g.

======================================================================
ERROR: test_DSSP_tool
----------------------------------------------------------------------
Traceback (most recent call last):
  File "run_tests.py", line 436, in runTest
    suite = loader.loadTestsFromName(name)
  File "/opt/python/2.7.9/lib/python2.7/unittest/loader.py", line 91, in loadTestsFromName
    module = __import__('.'.join(parts_copy))
  File "/home/travis/build/biopython/biopython/Tests/test_DSSP_tool.py", line 17, in <module>
    from Bio.PDB import PDBParser
  File "/home/travis/build/biopython/biopython/build/lib.linux-x86_64-2.7/Bio/PDB/__init__.py", line 27, in <module>
    from .Polypeptide import PPBuilder, CaPPBuilder, is_aa, standard_aa_names
  File "/home/travis/build/biopython/biopython/build/lib.linux-x86_64-2.7/Bio/PDB/Polypeptide.py", line 60, in <module>
    from Bio.PDB.Vector import calc_dihedral, calc_angle
ImportError: cannot import name calc_dihedral

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Apologies, I thought they were testing methods since they were outside the Class definition.

I'll sort them out this weekend.

Gasta88 added 2 commits April 1, 2017 20:17
re-instated methods outside Vector class and removed the self-test
added `test_Vector()` method with more tests
@Gasta88

Gasta88 commented Apr 1, 2017

Copy link
Copy Markdown
Contributor Author

One side note, when I test for the division operand I get this error:

>>> v1 / 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'Vector' and 'int'

where v1 is a Vector object. I'm using Python 3.6

Gasta88 added 2 commits April 1, 2017 22:07
corrected the docstring
moved `from numpy.random import random`
@peterjc

peterjc commented Apr 1, 2017

Copy link
Copy Markdown
Member

If division works under Python 2, that's likely a bug we should fix under Python 3.

@peterjc

peterjc commented Apr 1, 2017

Copy link
Copy Markdown
Member

There are still some minor style issue being picked up by TravisCI running flake8,

$ flake8 Bio/
Bio/PDB/Vector.py:56:1: W293 blank line contains whitespace
Bio/PDB/Vector.py:75:1: W293 blank line contains whitespace
Bio/PDB/Vector.py:184:1: W293 blank line contains whitespace

You should be able to install flake8 locally using pip install flake8.

Comment thread Bio/PDB/Vector.py Outdated

def rotaxis2m(theta, vector):
"""Calculate a left multiplying rotation matrix that rotates
"""Rotaxi2m method.

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.

How about Calculate left multiplying rotation matrix as the docstring one-line summary?

Comment thread Bio/PDB/Vector.py Outdated

def refmat(p, q):
"""Return a (left multiplying) matrix that mirrors p onto q.
"""

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.

Comment thread Tests/test_PDB.py
finally:
os.remove(filename)

def test_deepcopy_of_structure_with_disorder(self):

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.

You seem to have accidentally removed this PDB test for deepcopy?

Comment thread Tests/test_PDB.py Outdated
self.assertEqual(calc_dihedral(v1, v2, v3, v4), 1.5707963267948966)
ref = refmat(v1, v3)
rot = rotmat(v1, v3)
self.assertEqual(ref[0].all(), numpy.array([1.0, 0.0, 0.0]).all())

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.

I think the .all() bits should be removed, note numpy.array([1.0, 0.0, 0.0]).all() u
is just False.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a behavior that I don't understand.
When I launch the unittest of this script from my local machine, the entire test compiles successfully without a warning.

Is it just this one line or all the others that make a comparison with a numpy.array?

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.

I think you should remove every .all() in your tests, it just reduces an array to a single boolean value (usually True but it would be False for a zero vector).

$ python3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> a = numpy.random.random(3)
>>> b = numpy.random.random(3)
>>> a == b#
array([False, False, False], dtype=bool)
>>> a.all() == b.all()
True
>>> a.all()
True
>>> b.all()
True

Comment thread Tests/test_PDB.py
self.assertEqual(v1.normsq(), 1.0)
v1[2] = 10
self.assertEqual(v1.__getitem__(2), 10)
angle = random() * numpy.pi

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.

Can this onwards be a separate test method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you mean the part from line 1184 or 1187?

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.

Lines 1198 to 1193 looked self contained:

   def test_Vector(self):
        ...
        angle = random() * numpy.pi
        axis = Vector(random(3) - random(3))
        axis.normalize()
        m = rotaxis(angle, axis)
        cangle, caxis = m2rotaxis(m)
        self.assertAlmostEqual((angle - cangle), 0.0)
        self.assertEqual((axis - caxis).get_array().all(), numpy.array([0, 0, 0]).all())

Could you do:

    def test_Vector(self):
        ...

    def test_angles(self):
        angle = random() * numpy.pi
        axis = Vector(random(3) - random(3))
        axis.normalize()
        m = rotaxis(angle, axis)
        cangle, caxis = m2rotaxis(m)
        self.assertAlmostEqual((angle - cangle), 0.0)
        self.assertEqual((axis - caxis).get_array().all(), numpy.array([0, 0, 0]).all())

@peterjc

peterjc commented Apr 1, 2017

Copy link
Copy Markdown
Member

TravisCI is failing currently on Python 3.6 with:

======================================================================
FAIL: test_Vector (test_PDB.TransformTests)
Test Vector object
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/build/biopython/biopython/Tests/test_PDB.py", line 1193, in test_Vector
    self.assertEqual((axis - caxis).get_array().all(), numpy.array([0, 0, 0]).all())
AssertionError: True != False
----------------------------------------------------------------------

As noted earlier, removing the .all() bits in these tests will likely help here.

Comment thread Tests/test_PDB.py Outdated
from Bio.PDB import make_dssp_dict
from Bio.PDB import DSSP
from Bio.PDB.NACCESS import process_asa_data, process_rsa_data
from numpy.random import random

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.

Please move this next to the other numpy imports, e.g. after line 26:

from numpy.linalg import svd, det

@peterjc

peterjc commented Apr 4, 2017

Copy link
Copy Markdown
Member

The style failure looks easy to fix,

$ flake8 Bio/
Bio/PDB/Vector.py:56:1: W293 blank line contains whitespace
Bio/PDB/Vector.py:75:1: W293 blank line contains whitespace
Bio/PDB/Vector.py:184:1: W293 blank line contains whitespace

You should be able to install this code checking tool locally with pep install flake8.

@peterjc

peterjc commented Apr 4, 2017

Copy link
Copy Markdown
Member

The line from copy import deepcopy is still missing from test_PDB.py which explains this failure:

======================================================================
ERROR: test_deepcopy_of_structure_with_disorder (test_PDB.ParseTest)
Test deepcopy of a structure with disordered atoms.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/build/biopython/biopython/Tests/test_PDB.py", line 533, in test_deepcopy_of_structure_with_disorder
    _ = deepcopy(self.structure)
NameError: global name 'deepcopy' is not defined

@peterjc

peterjc commented Apr 4, 2017

Copy link
Copy Markdown
Member

I understand now why you were trying to use .any() earlier, I should have spotted this earlier - one option is to use numpy.array_equal here:

>>> import numpy
>>> a = numpy.array([1.0, 2.0, 3.0])
>>> b = numpy.array([1.0, 2.0, 3.0])
>>> a == b
array([ True,  True,  True], dtype=bool)
>>> numpy.array_equal(a, b)
True

More generally, using self.assertAlmostEqual(a, b, places=3) should allow for some floating point number variation between platforms.

@Gasta88

Gasta88 commented Apr 4, 2017

Copy link
Copy Markdown
Contributor Author

Apologies for the error in Vector.py, but I realized I've committed only one file change while I left this one unmodified.

The matter with deepcopy must have been one clumsy error. Hopefully will be resolved with the next change (famous last words...)

At the moment I'm trying both options to test numpy.array objects and see how my version of Python react.

@Gasta88

Gasta88 commented Apr 4, 2017

Copy link
Copy Markdown
Contributor Author

Before updating this PR, there is one problem. If I run the following commands in the Python console I get the displayed result:

>>> import numpy
>>> from Bio.PDB import Vector
>>> from numpy.random import random
>>> from Bio.PDB import rotaxis,m2rotaxis
>>> angle = random() * numpy.pi
>>> axis = Vector(random(3) - random(3))
>>> axis.normalize()
>>> m = rotaxis(angle, axis)
>>> cangle, caxis = m2rotaxis(m)
>>> axis
<Vector 0.08, -0.10, -0.99>
>>> caxis
<Vector 0.08, -0.10, -0.99>
>>> numpy.array_equal((axis-caxis).get_array(),numpy.array([0,0,0]))
True

On the other hand, I get the following if run in Powershell:

PS H:\Documents\biopython\Tests> python .\test_PDB.py TransformTests.test_Vector_angles
test_Vector_angles (__main__.TransformTests) ... FAIL

======================================================================
FAIL: test_Vector_angles (__main__.TransformTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".\test_PDB.py", line 1201, in test_Vector_angles
    self.assertTrue(numpy.array_equal((axis - caxis).get_array(), numpy.array([0, 0, 0])))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 1 test in 0.107s

FAILED (failures=1)

Do you think it depends on the environment?

test_Vector() doesn't display any error.

@peterjc

peterjc commented Apr 4, 2017

Copy link
Copy Markdown
Member

You can give a second argument like self.assertTrue(value, msg)

Try using something like this:

    self.assertTrue(numpy.array_equal((axis - caxis).get_array(), numpy.array([0, 0, 0])),
                    "Expected zero vector, got %r" % (axis - caxis).get_array())

I've not checked this but isn't this just trying to compare axis and xaxis, so would this make sense instead:

    self.assertAlmostEqual(axis, caxis, places=3)

@peterjc

peterjc commented Apr 4, 2017

Copy link
Copy Markdown
Member

Probably what is happening is that axis and caxis are very similar but not always equal, and that could depend on the environment (which version of Python and how it was compiled etc).

@Gasta88

Gasta88 commented Apr 5, 2017

Copy link
Copy Markdown
Contributor Author

I started with the second suggestion since it seems logical, but there is this problem now:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'Vector'

It seems that abs() doesn't relate well with the Vector object, but it's successful with Vector.get_array() since it generates a numpy.array.

I changed the test to:

self.assertTrue(numpy.array_equal(axis.get_array(), caxis.get_array()),
                    "Expected zero vector, got %r" % (axis - caxis).get_array())

but each time the arrays have similar values but not equal. In this case the test will fail more frequently that it should because of a matter of precision.

Does having the msg parameter set up helps to maintain testing quality?

@peterjc

peterjc commented Apr 5, 2017

Copy link
Copy Markdown
Member

If self.assertTrue(value) fails all you are told is that value was not True. In this case you don't see what the values given to numpy.array_equal where, so an explicit failure message is useful.

With the self.assertEqual and assertAlmostEqual the default message is very good.

How about something like this (again, untested)?

self.assertAlmostEqual(axis.get_array(), caxis.get_array(), places=3)

@Gasta88

Gasta88 commented Apr 5, 2017

Copy link
Copy Markdown
Contributor Author
self.assertAlmostEqual(axis.get_array(), caxis.get_array(), places=3)

doesn't work either because two numpy.array objects are compared and it doesn't like it unless a .all() or .any() is specified in the test.

I'll stick with the message and commit the new files.

@peterjc

peterjc commented Apr 5, 2017

Copy link
Copy Markdown
Member

How about:

self.assertTrue(numpy.allclose(axis.get_array(), caxis.get_array(),
               "Want %r and %r to be almost equal" % (axis, caxis))

The numpy.allclose() function has optional arguments for the strictness of the check.

@codecov

codecov Bot commented Apr 6, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #1127 into master will decrease coverage by 1.16%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1127      +/-   ##
==========================================
- Coverage   80.81%   79.64%   -1.17%     
==========================================
  Files         322      322              
  Lines       49446    51001    +1555     
==========================================
+ Hits        39961    40622     +661     
- Misses       9485    10379     +894
Impacted Files Coverage Δ
Bio/PDB/Vector.py 78.39% <ø> (+36.36%) ⬆️
Bio/NeuralNetwork/BackPropagation/Network.py 38.98% <0%> (-61.02%) ⬇️
Bio/Pathway/Rep/Graph.py 37.79% <0%> (-36.89%) ⬇️
Bio/Entrez/Parser.py 39.93% <0%> (-28.47%) ⬇️
Bio/Pathway/Rep/MultiGraph.py 27.55% <0%> (-28.42%) ⬇️
Bio/SearchIO/_model/query.py 75.56% <0%> (-16.11%) ⬇️
Bio/motifs/matrix.py 75% <0%> (-1.32%) ⬇️
Bio/DocSQL.py 38.62% <0%> (-0.16%) ⬇️
Bio/UniProt/GOA.py 26.77% <0%> (-0.11%) ⬇️
Bio/SeqIO/AbiIO.py 95.94% <0%> (-0.06%) ⬇️
... and 105 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2fa15d3...30c7714. Read the comment docs.

@peterjc

peterjc commented Apr 6, 2017

Copy link
Copy Markdown
Member

Don't worry about the CodeCov numbers - it seems to be a little unreliable at the moment because I made some setting changes on the master branch.

Comment thread Tests/run_tests.py Outdated
"Bio.Blast.Applications",
"Bio.Emboss.Applications",
"Bio.GenBank",
"Bio.Graphics.GenomeDiagram._Colors",

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.

Please remove this change as it not relevant to the PDB Vector test work.

@peterjc

peterjc commented Apr 6, 2017

Copy link
Copy Markdown
Member

Other than the extra change to run_tests.py, this looks ready to merge - good job 👍

@Gasta88

Gasta88 commented Apr 6, 2017

Copy link
Copy Markdown
Contributor Author

run_tests.py has been updated. Thanks for keeping up with this one Peter :)

@peterjc
peterjc merged commit b46309d into biopython:master Apr 6, 2017
@peterjc

peterjc commented Apr 6, 2017

Copy link
Copy Markdown
Member

Thank you :)

MarkusPiotrowski pushed a commit to MarkusPiotrowski/biopython that referenced this pull request Oct 31, 2017
Squashed commit of pull request biopython#1127
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.

2 participants