Skip to content

Commit

Permalink
Check the examples in the tests
Browse files Browse the repository at this point in the history
This ensure they don't fall behind the API changes
  • Loading branch information
Luthaf committed Sep 5, 2019
1 parent 8594ccd commit a9354ca
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ chemfiles.egg-info/
.tox/
MANIFEST
.coverage
*.pyc
41 changes: 20 additions & 21 deletions doc/tutorials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,20 @@ Then we open a Trajectory and read the first frame:
:language: python
:lines: 7-8

We can now create a list to store the indices of the atoms with ``x < 5``, and
get the positions of the atoms in the frame with the :py:func:`Frame.positions`
function
Iterating through the atoms in the frame, we store the indices of the atoms with
``x < 5`` in a list. ``len(frame.atoms)`` gives the number of atoms in the
frame, which is also the size of the ``frame.positions`` array. This array is a
numpy array which shape is ``(len(frame), 3)``.

.. literalinclude:: ../examples/indexes.py
:language: python
:lines: 10-11
:lines: 10-13

Iterating through the atoms in the frame, we get the ones matching our
condition. ``len(frame)`` gives the number of atoms in the frame, which is also
the size of the ``positions`` array. This array is a numpy array which shape is
``(len(frame), 3)``.
We can then print our results

.. literalinclude:: ../examples/indexes.py
:language: python
:lines: 13-15

And finally we can print our results

.. literalinclude:: ../examples/indexes.py
:language: python
:lines: 17-19
:lines: 15-17

.. htmlhidden::
:toggle: Click here to see the whole program
Expand All @@ -62,7 +54,7 @@ And finally we can print our results
For more information about reading frame in a trajectory, see the following
functions:

- :py:func:`Trajectory.nsteps` gives the number of frame in a Trajectory.
- :py:attr:`Trajectory.nsteps` is the number of frame in a Trajectory.
- :py:func:`Trajectory.read_step` to directlty read a given step.
- :py:func:`Trajectory.set_cell` and :py:func:`Trajectory.set_topology` to
specify an unit cell or a topology for all frames in a trajectory.
Expand Down Expand Up @@ -98,28 +90,28 @@ We can then set the atomic positions:

.. literalinclude:: ../examples/generate.py
:language: python
:lines: 20-23
:lines: 20-22

Another possibility is to directly add atoms to the frame. Here we define a
second molecule representing carbon dioxyde. :py:func:`Frame.add_atom` takes
two arguments: the atom, and the position of the atom as a 3-element list

.. literalinclude:: ../examples/generate.py
:language: python
:lines: 25-29
:lines: 24-28

Finally, we can set the :py:class:`UnitCell` associated with this frame.

.. literalinclude:: ../examples/generate.py
:language: python
:lines: 31
:lines: 30

Now that our frame is constructed, it is time to write it to a file. For that,
we open a trajectory in write (``'w'``) mode, and write to it.

.. literalinclude:: ../examples/generate.py
:language: python
:lines: 33-34
:lines: 32-33

.. htmlhidden::
:toggle: Click here to see the whole program
Expand Down Expand Up @@ -176,13 +168,20 @@ frame.
:language: python
:lines: 14-15

Finally, we can write the cleaned frame to the output file, and start the next
We can then write the cleaned frame to the output file, and start the next
iteration.

.. literalinclude:: ../examples/select.py
:language: python
:lines: 16

Finally, we close the input and output files to ensure that all the written data
is flushed to the disk.

.. literalinclude:: ../examples/select.py
:language: python
:lines: 18-19

.. htmlhidden::
:toggle: Click here to see the whole program
:before-not-html: The whole program look like this:
Expand Down
21 changes: 10 additions & 11 deletions examples/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,28 @@
from chemfiles import Topology, Frame, Atom, UnitCell, Trajectory

topology = Topology()
topology.add_atom(Atom("H"))
topology.add_atom(Atom("O"))
topology.add_atom(Atom("H"))
topology.atoms.append(Atom("H"))
topology.atoms.append(Atom("O"))
topology.atoms.append(Atom("H"))

topology.add_bond(0, 1)
topology.add_bond(2, 1)

frame = Frame()
frame.resize(3)
frame.set_topology(topology)
frame.topology = topology

positions = frame.positions()
positions[0, :] = np.array([1.0, 0.0, 0.0])
positions[1, :] = np.array([0.0, 0.0, 0.0])
positions[2, :] = np.array([0.0, 1.0, 0.0])
frame.positions[0, :] = np.array([1.0, 0.0, 0.0])
frame.positions[1, :] = np.array([0.0, 0.0, 0.0])
frame.positions[2, :] = np.array([0.0, 1.0, 0.0])

frame.add_atom(Atom("O"), [5.0, 0.0, 0.0])
frame.add_atom(Atom("C"), [6.0, 0.0, 0.0])
frame.add_atom(Atom("O"), [7.0, 0.0, 0.0])
frame.add_bond(3, 4)
frame.add_bond(4, 5)

frame.set_cell(UnitCell(10, 10, 10))
frame.cell = UnitCell(10, 10, 10)

trajectory = Trajectory("water-co2.pdb", 'w')
trajectory.write(frame)
with Trajectory("water-co2.pdb", 'w') as trajectory:
trajectory.write(frame)
10 changes: 4 additions & 6 deletions examples/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
#!/usr/bin/env python
from chemfiles import Trajectory

trajectory = Trajectory("filename.xyz")
frame = trajectory.read()
with Trajectory("filename.xyz") as trajectory:
frame = trajectory.read()

less_than_five = []
positions = frame.positions()

for i in range(len(frame)):
if positions[i, 0] < 5:
for i in range(len(frame.atoms)):
if frame.positions[i, 0] < 5:
less_than_five.append(i)

print("Atoms with x < 5: ")
Expand Down
3 changes: 3 additions & 0 deletions examples/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
for i in reversed(sorted(to_remove)):
frame.remove(i)
output.write(frame)

trajectory.close()
output.close()
57 changes: 57 additions & 0 deletions tests/examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import unittest
import tempfile
import shutil
import os

from chemfiles import Trajectory, Frame, Atom

ROOT = os.path.dirname(__file__)


class TestExamples(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls._cwd = os.getcwd()
cls._tmpdir = tempfile.mkdtemp()
os.chdir(cls._tmpdir)

@classmethod
def tearDownClass(cls):
os.chdir(cls._cwd)
shutil.rmtree(cls._tmpdir)

def test_generate(self):
path = os.path.join(ROOT, "..", "examples", "generate.py")
exec(open(path).read(), globals())

def test_indexes(self):
# Create an input file
frame = Frame()
for i in range(120):
frame.add_atom(Atom('X'), [i % 10, i + 1 % 10, i + 2 % 10])

with Trajectory("filename.xyz", "w") as file:
file.write(frame)

path = os.path.join(ROOT, "..", "examples", "indexes.py")
# disable output
exec(open(path).read(), globals(), {'print': lambda _: None})

def test_select(self):
# Create an input file
frame = Frame()
NAMES = ["N", "Zn", "C", "O"]
for i in range(120):
frame.add_atom(Atom(NAMES[i % 4]), [i % 10, i + 1 % 10, i + 2 % 10])

with Trajectory("input.arc", "w")as file:
file.write(frame)

path = os.path.join(ROOT, "..", "examples", "select.py")
exec(open(path).read(), globals())


if __name__ == "__main__":
unittest.main()

0 comments on commit a9354ca

Please sign in to comment.