Skip to content

Convert libmdaxdr test#1442

Merged
richardjgowers merged 3 commits intodevelopfrom
convert-libmdaxdr-test
Jul 11, 2017
Merged

Convert libmdaxdr test#1442
richardjgowers merged 3 commits intodevelopfrom
convert-libmdaxdr-test

Conversation

@kain88-de
Copy link
Member

@kain88-de kain88-de commented Jun 27, 2017

Changes made in this Pull Request:

  • libmdaxdr is ported to pytest fully
  • new tests based on libdcd
  • changed behavior of libmdaxdr to accept a broader range of inputs

PR Checklist

  • Tests?
  • Docs?
  • CHANGELOG updated?
    - [x] Issue raised/referenced?

@kain88-de kain88-de force-pushed the convert-libmdaxdr-test branch 2 times, most recently from 8b95a78 to 4a81a97 Compare June 28, 2017 05:58
assert frame.lmbda == 0.5


class XDRFormatBaseTest(object):
Copy link
Member

Choose a reason for hiding this comment

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

You could have kept the class and parametrised it - do you prefer the standalone functions instead?

Copy link
Member Author

@kain88-de kain88-de Jun 28, 2017

Choose a reason for hiding this comment

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

I prefer the stand alone funtions. A neat thing about them is that I see all variables effecting a test at once. No need to scroll and it is immediately clear which files are going to be tested.

assert_equal(f_out.n_atoms, 0)
for frame in f_in:
@pytest.mark.parametrize('dtype', (np.int32, np.int64, np.float32, np.float64, int, float))
def test_write_trr_dtype(tmpdir, dtype):
Copy link
Member

Choose a reason for hiding this comment

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

Something I have in another project is a in_tmpdir fixture, which is something like:

def in_tmpdir(tmpdir):
    with tmpdir.as_cwd():
        yield

But it then saves you another level of indentation as the fixture is dealing with teardown. Worth putting in?

Copy link
Member Author

Choose a reason for hiding this comment

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

then we have def test_a(in_tmpdir): which runs even more magically in a temporary directory and we don't use the in_tmpdir variable. Not using a variable isn't so nice since I would like to enable pylint to warn about unused variables soon.

So good idea. But maybe to much magic

@kain88-de kain88-de force-pushed the convert-libmdaxdr-test branch from 4a81a97 to 13e50d3 Compare June 28, 2017 07:49
@richardjgowers
Copy link
Member

So does this conflict with anything that @utkbansal is currently working on?

@kain88-de
Copy link
Member Author

It shouldn't he already ported formats to pytest a while ago. I just wanted to add new useful tests that @tylerjereddy wrote for libdcd.

@kain88-de
Copy link
Member Author

I admit I might have been a bit over enthusiastic implementing pytest idoms in the tests.



def test_xtc():
f = XTCFile(XTC_single_frame)
Copy link
Member

Choose a reason for hiding this comment

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

won't this open a file handle that isn't otherwise cleaned up?

Copy link
Member Author

Choose a reason for hiding this comment

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

I fixed that. I didn't see any before because the function should clean up nicely and call dealloc. But lets be on the safe side


@pytest.mark.parametrize('dtype', (np.int32, np.int64, np.float32, np.float64, int, float))
def test_write_xtc_dtype(tmpdir, dtype):
with tmpdir.as_cwd():
Copy link
Member

Choose a reason for hiding this comment

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

So maybe having a unused fixture isn't any better, but I prefer the decorator approach to tmpdir. We're only using the inside of the context manager, so it's cleaner to not have the indentation.

@richardjgowers
Copy link
Member

Yeah, I think PRs like this will probably set up which idioms etc we'll use globally.

@kain88-de kain88-de force-pushed the convert-libmdaxdr-test branch from 13e50d3 to b10e5e5 Compare June 28, 2017 20:16
@kain88-de
Copy link
Member Author

Yeah, I think PRs like this will probably set up which idioms etc we'll use globally.

I opened an issue #1444 to talk about this a bit.

# xtc only saves with 3 decimal places precision
assert_array_almost_equal(xyz.flat, np.arange(30), decimal=3)
assert_array_almost_equal(box, np.eye(3) * 20, decimal=3)
assert step == 0
Copy link
Member

Choose a reason for hiding this comment

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

I don't like having multiple asserts in one test function, it makes it harder to debug. Eg, if xyz.flat stopped working, I'd also want to know if box was working too, to help me try and locate the problem. This test wouldn't give me the breakdown of what is broken.

I know you're not a fan of classes, but ..

class TestXTC(object):
    @staticmethod
    @pytest.fixture()
    def XTC():
        with XTCFile(XTC_single_frame) as f:
            yield f

    def test_n_atoms(self, XTC):
        assert XTC.n_atoms == 10

    def test_xyc(self, XTC):
        etc

This is a lot nicer imo

Copy link
Member Author

Choose a reason for hiding this comment

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

I like the XTC fixture. I think I will add that one.

Copy link
Member

@orbeckst orbeckst left a comment

Choose a reason for hiding this comment

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

You do like pytest...


Enhancements
* libmdaxdr classes now accept more argument types for a write (PR #1442)
* libmdaxdr classes now raise EOFError when accessing another frame after
Copy link
Member

Choose a reason for hiding this comment

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

I think this counts as a "Change", not an enhancement. State what it used to do, IOError,


from numpy.testing import assert_almost_equal
from numpy.testing import assert_array_almost_equal
from numpy.testing import assert_array_equal
Copy link
Member

Choose a reason for hiding this comment

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

Just out of curiosity: why not

from numpy.testing import assert_array_almost_equal, assert_array_equal

?

box_compare = np.eye(3) * 20
for i, frame in enumerate(f):
xyz, box, step, time, prec = frame
assert_array_almost_equal(xyz, ones * i, decimal=3)
Copy link
Member

Choose a reason for hiding this comment

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

Split into two tests? See @richardjgowers 's comment above.


@pytest.mark.parametrize("xdrfile, fname", ((XTCFile, XTC_multi_frame),
(TRRFile, TRR_multi_frame)))
def test_over_seek(xdrfile, fname):
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm unable in this test to use the xtc and trr fixtures above with parametrize.

@pytest.mark.parametrize(
'dtype', (np.int32, np.int64, np.float32, np.float64, int, float))
def test_write_xtc_dtype(tmpdir, dtype, xtc):
outname = str(tmpdir.join('foo.xtc'))
Copy link
Member Author

Choose a reason for hiding this comment

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

@richardjgowers This is another option to use tmpdir. It avoids the extra indentation.

@kain88-de
Copy link
Member Author

@utkbansal input from you would be appreciated here and in #1444 as well since you will also spend a lot of time implementing the guides we are setting now.

@kain88-de kain88-de force-pushed the convert-libmdaxdr-test branch from caa971f to db5172a Compare June 30, 2017 07:40
assert f._bytes_tell() == big_offset


def test_read_multi_frame_xtc(xtc):
Copy link
Member Author

Choose a reason for hiding this comment

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

I know I still need to untangle these tests



@pytest.mark.parametrize('array_like', (np.array, list))
def test_write_xtc_array_like(tmpdir, array_like, xtc):
Copy link
Member Author

Choose a reason for hiding this comment

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

I know these tests should be untangled as well. I also think I know how to do that best giving the granularity in the tests that @richardjgowers likes.

@richardjgowers richardjgowers self-assigned this Jun 30, 2017

@pytest.mark.parametrize('fname, xdr', ((XTC_multi_frame, XTCFile),
(TRR_multi_frame, TRRFile)))
class TestCommonAPI:
Copy link
Member Author

Choose a reason for hiding this comment

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

@richardjgowers is that what you have in common with using classes to parametrize common tests?

Copy link
Member Author

Choose a reason for hiding this comment

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

I do admit that this removes a lot of boiler plate for these tests


def test_seek_overflow(self, reader, offsets):
with pytest.raises(OverflowError):
reader._bytes_seek(2**65)
Copy link
Member Author

Choose a reason for hiding this comment

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

there is a bad side effect though. We need to always use all parametrized values to a function even if they are not used.

def test_box_xtc(xtc):
box = np.eye(3) * 20
for frame in xtc:
assert_array_almost_equal(frame.box, box, decimal=3)
Copy link
Member Author

Choose a reason for hiding this comment

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

So here I decided to go with special functions since the API of XTC and TRR are different.

for frame in xtc:
f.write(*frame)
with XTCFile(fname) as f:
yield f
Copy link
Member Author

Choose a reason for hiding this comment

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

these fixtures with yield are really nice. They do remove a hell of a lot of boilerplate code

@kain88-de kain88-de force-pushed the convert-libmdaxdr-test branch from dcee674 to ee5f872 Compare July 1, 2017 21:27
@kain88-de kain88-de mentioned this pull request Jul 1, 2017
@richardjgowers
Copy link
Member

@kain88-de I was playing around with pytest...

@pytest.fixture(params=(
    (XTC_multi_frame, XTCFile),
    (TRR_multi_frame, TRRFile),
))
def reader(request):
    fname, cls = request.param
    with cls(fname) as f:
        yield f


class TestCommonAPI(object):
    def test_natoms(self, reader):
        assert reader.n_atoms == 10

    def test_over_seek(self, reader):
        with pytest.raises(EOFError):
            reader.seek(100)

So you can define a fixture with n params, and then all tests that use this fixture get called n times with each of the versions of the fixture.

@kain88-de
Copy link
Member Author

@richardjgowers that is pretty cool. But then we really don't need the class anymore.

@kain88-de
Copy link
Member Author

This pattern would yield parametrize over fixtures and single functions instead of parametrize over classes.

XTC_sub_sol = resource_filename(__name__, 'data/cobrotoxin.xtc')
PDB_sub_sol = resource_filename(__name__, 'data/cobrotoxin.pdb')
PDB_xlserial = resource_filename(__name__, 'data/xl_serial.pdb')
XTC_single_frame = resource_filename(
Copy link
Member

Choose a reason for hiding this comment

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

Did we remove the XTC and TRR single frame files?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes I noticed they weren't needed in the first place. They test nothing that isn't tested in the other files

@kain88-de
Copy link
Member Author

@richardjgowers anything left to be done on this?

@kain88-de kain88-de force-pushed the convert-libmdaxdr-test branch from ee5f872 to 909097b Compare July 11, 2017 09:34
@richardjgowers richardjgowers merged commit 94faa8c into develop Jul 11, 2017
@richardjgowers richardjgowers deleted the convert-libmdaxdr-test branch July 11, 2017 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants