Skip to content

Commit

Permalink
Merge pull request #1842 from palnabarun/1685-load-new-memory-reader
Browse files Browse the repository at this point in the history
Adds a check to MDAnalysis.coordinates.memory.MemoryReader to check f…
  • Loading branch information
orbeckst authored Apr 19, 2018
2 parents 092d5a5 + 558d845 commit 8c331d7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ The rules for this file:
* release numbers follow "Semantic Versioning" http://semver.org

------------------------------------------------------------------------------
??/??/18 richardjgowers
??/??/18 richardjgowers, palnabarun

* 0.18.1

Enhancements

Fixes
* Fixed order of indices in Angle/Dihedral/Improper repr
* coordinates.memory.MemoryReader now takes np.ndarray only (Issue #1685)

Changes

Expand Down
23 changes: 20 additions & 3 deletions package/MDAnalysis/coordinates/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ def __init__(self, coordinate_array, order='fac',
Parameters
----------
coordinate_array : numpy.ndarray
The underlying array of coordinates
The underlying array of coordinates. The MemoryReader now
necessarily requires a np.ndarray
order : {"afc", "acf", "caf", "fac", "fca", "cfa"} (optional)
the order/shape of the return data array, corresponding
to (a)tom, (f)rame, (c)oordinates all six combinations
Expand All @@ -252,19 +253,35 @@ def __init__(self, coordinate_array, order='fac',
The name of the file from which this instance is created. Set to ``None``
when created from an array
Raises
------
TypeError if the coordinate array passed is not a np.ndarray
Note
----
At the moment, only a fixed `dimension` is supported, i.e., the same
unit cell for all frames in `coordinate_array`. See issue `#1041`_.
.. _`#1041`: https://github.com/MDAnalysis/mdanalysis/issues/1041
.. versionchanged:: 0.18.1
The input to the MemoryReader now must be a np.ndarray
"""

super(MemoryReader, self).__init__()
self.filename = filename
self.stored_order = order
self.set_array(np.asarray(coordinate_array), order)

# See Issue #1685. The block below checks if the coordinate array passed is of shape (N, 3) and if it is, the coordiante array is reshaped to (1, N, 3)
try:
if len(coordinate_array.shape) == 2 and coordinate_array.shape[1] == 3:
coordinate_array = coordinate_array[np.newaxis, :, :]
except AttributeError as e:
raise TypeError("The input has to be a numpy.ndarray that corresponds to the layout specified by the 'order' keyword."
)

self.set_array(coordinate_array, order)
self.n_frames = \
self.coordinate_array.shape[self.stored_order.find('f')]
self.n_atoms = \
Expand Down Expand Up @@ -323,7 +340,7 @@ def copy(self):
for auxname, auxread in self._auxs.items():
new.add_auxiliary(auxname, auxread.copy())

return new
return new

def set_array(self, coordinate_array, order='fac'):
"""
Expand Down
16 changes: 16 additions & 0 deletions testsuite/MDAnalysisTests/core/test_universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,22 @@ def test_load_new_VE(self):
with pytest.raises(TypeError):
u.load_new('thisfile', format = 'soup')

def test_load_new_memory_reader_success(self):
u = mda.Universe(GRO)
prot = u.select_atoms('protein')
u2 = mda.Merge(prot)
assert u2.load_new( [ prot.positions ], format=mda.coordinates.memory.MemoryReader) is u2

def test_load_new_memory_reader_fails(self):
def load():
u = mda.Universe(GRO)
prot = u.select_atoms('protein')
u2 = mda.Merge(prot)
u2.load_new( [[ prot.positions ]], format=mda.coordinates.memory.MemoryReader)

with pytest.raises(TypeError):
load()

def test_universe_kwargs(self):
u = mda.Universe(PSF, PDB_small, fake_kwarg=True)
assert_equal(len(u.atoms), 3341, "Loading universe failed somehow")
Expand Down

0 comments on commit 8c331d7

Please sign in to comment.