Skip to content

Commit 45719ea

Browse files
committed
unit tests for edge cases
1 parent 8fd9a48 commit 45719ea

1 file changed

Lines changed: 148 additions & 16 deletions

File tree

‎tests/unit/CodeEntropy/levels/test_axes.py‎

Lines changed: 148 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,139 @@ def _sel(q):
16411641
ax.get_UA_axes(u, index=0, res_position=None)
16421642

16431643

1644-
def get_residue_bonded_axes_terminal_2_points(monkeypatch):
1644+
def test_get_ua_axes_bonded_terminal_2_points(monkeypatch):
1645+
ax = AxesCalculator()
1646+
residue_group = MagicMock()
1647+
residue_group.__len__ = 2
1648+
residue = residue_group.residues[1]
1649+
heavy_atoms = _FakeAtomGroup(
1650+
[
1651+
_atom(index=0, mass=12.0, pos=(1, 0, 0)),
1652+
_atom(index=1, mass=12.0, pos=(0, 1, 0)),
1653+
],
1654+
)
1655+
edge_atom_set = [heavy_atoms[0]]
1656+
bonded_atoms = [heavy_atoms[1]]
1657+
1658+
def _select_atoms(q):
1659+
if q == "mass 2 to 999":
1660+
# return heavy atoms group
1661+
return heavy_atoms
1662+
if q.startswith("index"):
1663+
return [heavy_atoms[0]]
1664+
if q.startswith("resindex "):
1665+
return edge_atom_set
1666+
if q.startswith("(mass 2 to 999) and bonded index "):
1667+
return bonded_atoms
1668+
1669+
residue_group.select_atoms.side_effect = _select_atoms
1670+
residue.atoms.select_atoms.side_effect = _select_atoms
1671+
1672+
monkeypatch.setattr(
1673+
ax,
1674+
"get_bonded_axes",
1675+
lambda system, atom, dimensions: (np.eye(3), np.array([1.0, 1.0, 1.0])),
1676+
)
1677+
1678+
monkeypatch.setattr(ax, "get_custom_axes", lambda a, b, c: 2 * np.eye(3))
1679+
1680+
trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes(
1681+
data_container=residue_group, index=0, res_position=1
1682+
)
1683+
1684+
assert np.allclose(trans_axes, 2 * np.eye(3))
1685+
assert np.allclose(rot_axes, np.eye(3))
1686+
assert np.allclose(rot_center, [1, 0, 0])
1687+
assert np.allclose(moi, np.array([1, 1, 1]))
1688+
1689+
1690+
def test_get_ua_axes_non_terminal_2_atoms(monkeypatch):
1691+
ax = AxesCalculator()
1692+
residue_group = MagicMock()
1693+
residue_group.__len__ = 3
1694+
residue = residue_group.residues[1]
1695+
heavy_atoms = _FakeAtomGroup(
1696+
[
1697+
_atom(index=0, mass=12.0, pos=(1, 1, 1)),
1698+
_atom(index=1, mass=12.0, pos=(3, 3, 3)),
1699+
],
1700+
)
1701+
1702+
def _select_atoms(q):
1703+
if q == "mass 2 to 999":
1704+
# return heavy atoms group
1705+
return heavy_atoms
1706+
if q.startswith("index"):
1707+
return [heavy_atoms[0]]
1708+
if q.startswith("resindex "):
1709+
return heavy_atoms
1710+
1711+
residue_group.select_atoms.side_effect = _select_atoms
1712+
residue.atoms.select_atoms.side_effect = _select_atoms
1713+
monkeypatch.setattr(
1714+
ax,
1715+
"get_bonded_axes",
1716+
lambda system, atom, dimensions: (np.eye(3), 3 * np.eye(3)),
1717+
)
1718+
monkeypatch.setattr(ax, "get_custom_axes", lambda a, b, c: 2 * np.eye(3))
1719+
monkeypatch.setattr(ax, "get_chain", lambda residue, first, last: [])
1720+
trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes(
1721+
data_container=residue_group, index=0, res_position=0
1722+
)
1723+
1724+
assert np.allclose(trans_axes, 2 * np.eye(3))
1725+
assert np.allclose(rot_axes, np.eye(3))
1726+
assert np.allclose(rot_center, [1, 1, 1])
1727+
assert np.allclose(moi, 3 * np.eye(3))
1728+
1729+
1730+
def test_get_residue_axes_non_terminal_2_atoms(monkeypatch):
1731+
ax = AxesCalculator()
1732+
u = MagicMock()
1733+
u.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90])
1734+
monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None)
1735+
residue = u.select_atoms("resindex 5")
1736+
residue.__len__.return_value = 2
1737+
print(f"The residue should be: {residue}")
1738+
u.atoms.principal_axes.return_value = np.eye(3)
1739+
uas = _FakeAtomGroup(
1740+
[
1741+
_atom(index=0, mass=12.0, pos=(1, 1, 1)),
1742+
_atom(index=1, mass=12.0, pos=(3, 3, 3)),
1743+
],
1744+
)
1745+
1746+
def _select_atoms(q):
1747+
if q == "mass 2 to 999":
1748+
return uas
1749+
if q.startswith("resindex 5 and (bonded resindex"):
1750+
return uas
1751+
1752+
u.atoms.select_atoms.side_effect = _select_atoms
1753+
residue.select_atoms.side_effect = residue
1754+
monkeypatch.setattr(ax, "get_chain", lambda residue, first, last: [])
1755+
monkeypatch.setattr(ax, "get_custom_axes", lambda a, b, c: 2 * np.eye(3))
1756+
monkeypatch.setattr(
1757+
ax,
1758+
"get_custom_residue_moment_of_inertia",
1759+
lambda center_of_mass, positions, masses, custom_rot_axes, dimensions: np.array(
1760+
[1, 1, 1]
1761+
),
1762+
)
1763+
1764+
trans_axes, rot_axes, rot_center, moi = ax.get_residue_axes(
1765+
data_container=u,
1766+
index=5,
1767+
relative_index=0,
1768+
)
1769+
1770+
assert np.allclose(trans_axes, np.eye(3))
1771+
assert np.allclose(rot_axes, 2 * np.eye(3))
1772+
assert np.allclose(rot_center, [2, 2, 2])
1773+
assert np.allclose(moi, [1, 1, 1])
1774+
1775+
1776+
def test_get_residue_axes_terminal_2_atoms(monkeypatch):
16451777
ax = AxesCalculator()
16461778
u = MagicMock()
16471779
u.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90])
@@ -1650,38 +1782,38 @@ def get_residue_bonded_axes_terminal_2_points(monkeypatch):
16501782
residue.__len__.return_value = 3
16511783
uas = _FakeAtomGroup(
16521784
[
1653-
_atom(index=0, mass=12.0, pos=[1, 0, 0]),
1654-
_atom(index=1, mass=12.0, pos=[0, 1, 0]),
1655-
]
1785+
_atom(index=0, mass=12.0, pos=(1, 0, 0)),
1786+
_atom(index=1, mass=12.0, pos=(0, 1, 0)),
1787+
_atom(index=2, mass=12.0, pos=(0, 0, 1)),
1788+
],
16561789
)
1790+
u.atoms.principal_axes.return_value = np.eye(3)
16571791

16581792
def _select_atoms(q):
16591793
if q == "mass 2 to 999":
16601794
return uas
1661-
if q.startswith("(mass 2 to 999) and bonded"):
1662-
# the bonded atom
1663-
return [uas[1]]
16641795
if q.startswith("resindex 0 and (bonded resindex"):
1665-
# edge atom
1796+
# the edge atom
16661797
return [uas[2]]
1798+
if q.startswith("(mass 2 to 999) and bonded index "):
1799+
return uas[0:2]
16671800

1668-
u.atoms.principal_axes.return_value = np.eye(3)
16691801
u.atoms.select_atoms.side_effect = _select_atoms
16701802
residue.select_atoms.side_effect = _select_atoms
1671-
1803+
monkeypatch.setattr(ax, "get_custom_axes", lambda a, b, c: 2 * np.eye(3))
16721804
monkeypatch.setattr(
16731805
ax,
16741806
"get_custom_residue_moment_of_inertia",
16751807
lambda center_of_mass, positions, masses, custom_rot_axes, dimensions: np.array(
16761808
[1, 1, 1]
16771809
),
16781810
)
1679-
16801811
trans_axes, rot_axes, rot_center, moi = ax.get_residue_axes(
1681-
u, index=0, relative_index=0
1812+
data_container=u,
1813+
index=0,
1814+
relative_index=0,
16821815
)
1683-
16841816
assert np.allclose(trans_axes, np.eye(3))
1685-
assert rot_axes.shape == (3, 3)
1686-
assert np.allclose(rot_center, [1, 1, 0])
1687-
assert np.allclose(moi, np.array([1, 1, 1]))
1817+
assert np.allclose(rot_axes, 2 * np.eye(3))
1818+
assert np.allclose(rot_center, [0, 0, 1])
1819+
assert np.allclose(moi, [1, 1, 1])

0 commit comments

Comments
 (0)