Skip to content

Commit

Permalink
removed TopologyGroup reshaping and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyminium committed Nov 6, 2019
1 parent bc941a4 commit 6e06367
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 24 deletions.
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Fixes
* Chainreader and continuous option work correctly when readers work for more
than one format (Issue #2353)
* PDBQTParser now gives icode TopologyAttrs (Issue #2361)
* TopologyGroup no longer reshapes the type, guessed, and order properties
(Issue #2392)

Enhancements
* Uniforms exception handling between Python 2.7 and Python 3: raised exceptions
Expand Down
13 changes: 7 additions & 6 deletions package/MDAnalysis/core/topologyobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,18 +539,19 @@ def __init__(self, bondidx, universe, btype=None, type=None, guessed=None,
raise ValueError("Unsupported btype, use one of '{}'"
"".format(', '.join(_BTYPE_TO_SHAPE)))

bondidx = np.asarray(bondidx)
nbonds = len(bondidx)
# remove duplicate bonds
if type is None:
type = np.repeat(None, nbonds).reshape(nbonds, 1)
type = np.repeat(None, nbonds)
if guessed is None:
guessed = np.repeat(True, nbonds).reshape(nbonds, 1)
guessed = np.repeat(True, nbonds)
elif guessed is True or guessed is False:
guessed = np.repeat(guessed, nbonds).reshape(nbonds, 1)
guessed = np.repeat(guessed, nbonds)
else:
guessed = np.asarray(guessed, dtype=np.bool).reshape(nbonds, 1)
guessed = np.asarray(guessed, dtype=np.bool)
if order is None:
order = np.repeat(None, nbonds).reshape(nbonds, 1)
order = np.repeat(None, nbonds)

if nbonds > 0:
uniq, uniq_idx = util.unique_rows(bondidx, return_index=True)
Expand Down Expand Up @@ -736,7 +737,7 @@ def __add__(self, other):
type=np.concatenate([self._bondtypes,
np.array([other._bondtype])]),
guessed=np.concatenate([self._guessed,
np.array([[other.is_guessed]])]),
np.array([other.is_guessed])]),
order=np.concatenate([self._order,
np.array([other.order])]),
)
Expand Down
9 changes: 6 additions & 3 deletions package/MDAnalysis/core/universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,17 +1013,20 @@ def _add_TopologyObjects(self, object_type, values, types=None, guessed=False,
order : int (optional)
Bond order
"""
values = [x.indices if isinstance(x, (AtomGroup, TopologyObject))
else x for x in values]
if all(isinstance(x, TopologyObject) for x in values):
first = values[0]
try:
types = value.type
types = first.type
except AttributeError:
types = None
guessed = first.is_guessed
order = first.order

print(types, guessed, order)

values = [x.indices if isinstance(x, (AtomGroup, TopologyObject))
else x for x in values]

try:
attr = getattr(self._topology, object_type)
except AttributeError:
Expand Down
6 changes: 2 additions & 4 deletions testsuite/MDAnalysisTests/core/test_topologyobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,12 @@ def test_dihedral_tg_creation_notype(self, PSFDCD):
def test_create_guessed_tg(self, PSFDCD):
vals = np.array([[0, 10], [5, 15]])
tg = TopologyGroup(vals, PSFDCD, guessed=True)

assert_equal(tg._guessed, np.array([[True], [True]]))
assert_equal(tg._guessed, np.array([True, True]))

def test_create_guessed_tg_2(self, PSFDCD):
vals = np.array([[0, 10], [5, 15]])
tg = TopologyGroup(vals, PSFDCD, guessed=False)

assert_equal(tg._guessed, np.array([[False], [False]]))
assert_equal(tg._guessed, np.array([False, False]))

def test_TG_equality(self, PSFDCD):
"""Make two identical TGs,
Expand Down
54 changes: 43 additions & 11 deletions testsuite/MDAnalysisTests/core/test_universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from MDAnalysis.topology.base import TopologyReaderBase
from MDAnalysis.transformations import translate
from MDAnalysisTests import assert_nowarns
from MDAnalysis.exceptions import NoDataError


class IOErrorParser(TopologyReaderBase):
Expand Down Expand Up @@ -683,22 +684,46 @@ def test_add_dihedrals(self, universe):
assert not hasattr(universe, 'dihedrals')
universe.add_Dihedrals(dih)
assert len(universe.dihedrals) == len(idx)

def test_add_atomgroups(self, universe):
idx = [[1, 2, 3, 1], [3, 1, 5, 2], [3, 1, 5, 2]]
imp = [universe.atoms[i] for i in idx]

assert not hasattr(universe, 'impropers')
universe.add_Impropers(imp)
assert len(universe.impropers) == 2

def test_add_topologygroups(self, universe):
arr = np.array([[1, 0, 2], [1, 2, 3]])
tg = mda.core.topologyobjects.TopologyGroup(arr, universe)

assert not hasattr(universe, 'angles')
assert len(tg) == 2
universe.add_Angles(tg)
assert len(universe.angles) == 2

ua = universe.angles[-1]
tga = tg[0]
assert ua.is_guessed == tga.is_guessed
assert ua.order == tga.order

assert list(ua.indices) == list(arr[-1])

def test_add_topologygroup_error(self, universe):
arr = np.array([(1, 0), (1, 2)])
tg = mda.core.topologyobjects.TopologyGroup(arr, universe)

with pytest.raises(ValueError):
universe.add_Angles(tg)

def add_angles_wrong_number_of_atoms_error(self, universe):
def test_add_angles_wrong_number_of_atoms_error(self, universe):
indices = [(0, 1, 2), (2, 3, 0), (4, 1)]
with pytest.raises(ValueError):
universe.add_Angles(indices)

def add_bonds_refresh_fragments(self, universe):
assert not hasattr(universe.atoms, 'fragments')
def test_add_bonds_refresh_fragments(self, universe):
with pytest.raises(NoDataError):
assert not hasattr(universe.atoms, 'fragments')

universe.add_Bonds([universe.atoms[:2]])
assert len(universe.atoms.fragments) == len(universe.atoms)-1
Expand Down Expand Up @@ -739,27 +764,34 @@ def test_delete_atomgroups(self, universe):
universe.delete_Dihedrals([ag])
assert len(universe.dihedrals) == 1

def delete_mixed_topologyobjects_type(self, universe):
mixed = [universe.atoms[[0, 1, 2]].angle, (3, 4, 5)]
universe.delete_Angles(mixed)
assert len(universe.angles) == 1
def test_delete_mixed_topologyobjects_type(self, universe):
mixed = [universe.atoms[[1, 3, 5, 2]].improper, (5, 3, 4, 2)]
universe.delete_Impropers(mixed)
assert len(universe.impropers) == 1

def delete_angles_wrong_number_of_atoms_error(self, universe):
def test_delete_angles_wrong_number_of_atoms_error(self, universe):
indices = [(0, 1, 2), (2, 3, 0), (4, 1)]
with pytest.raises(ValueError):
universe.delete_Angles(indices)

def ignore_topologyobjects_not_in_universe(self, universe):
def test_ignore_topologyobjects_not_in_universe(self, universe):
n_bonds = len(universe.bonds)
bonds = [(0, 1), (99, 100), (22, 2)]
universe.delete_Bonds(bonds)
assert len(universe.bonds) == n_bonds-1

def delete_bonds_refresh_fragments(self, universe):
def test_delete_bonds_refresh_fragments(self, universe):
n_fragments = len(universe.atoms.fragments)
universe.delete_Bonds([universe.atoms[[2, 3]]])
assert len(universe.atoms.fragments) == n_fragments + 1

def test_delete_bonds_empty_universe(self):
u = make_Universe()
assert not hasattr(u, 'bonds')
bonds = [(0, 1), (99, 100), (22, 2)]
with pytest.raises(ValueError):
u.delete_Bonds(bonds)

class TestAllCoordinatesKwarg(object):
@pytest.fixture(scope='class')
def u_GRO_TRR(self):
Expand Down

0 comments on commit 6e06367

Please sign in to comment.