From 6cb2049e745b3fd75a552c6dbfc34b05062d68a3 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Tue, 8 Sep 2026 16:21:39 +0100 Subject: [PATCH 01/30] added axes_topology as None --- CodeEntropy/levels/nodes/covariance.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CodeEntropy/levels/nodes/covariance.py b/CodeEntropy/levels/nodes/covariance.py index 57a7a922..8bc9c7ed 100644 --- a/CodeEntropy/levels/nodes/covariance.py +++ b/CodeEntropy/levels/nodes/covariance.py @@ -483,6 +483,7 @@ def _build_ua_vectors( if customised_axes: ua_topology = None + axes_topology = None if axes_topology is not None: ua_topology = axes_topology.ua.get((mol_id, local_res_i, ua_i)) if ua_topology is not None: @@ -636,6 +637,7 @@ def _get_residue_axes( if customised_axes: res = mol.residues[local_res_i] residue_topology = None + axes_topology = None if axes_topology is not None: residue_topology = axes_topology.residue.get((mol_id, local_res_i)) From fdfb2a9284841f90f0dd80c26537f4e469900594 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Tue, 15 Sep 2026 11:25:38 +0100 Subject: [PATCH 02/30] disabled axes topology --- CodeEntropy/levels/nodes/covariance.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/CodeEntropy/levels/nodes/covariance.py b/CodeEntropy/levels/nodes/covariance.py index 8bc9c7ed..57a7a922 100644 --- a/CodeEntropy/levels/nodes/covariance.py +++ b/CodeEntropy/levels/nodes/covariance.py @@ -483,7 +483,6 @@ def _build_ua_vectors( if customised_axes: ua_topology = None - axes_topology = None if axes_topology is not None: ua_topology = axes_topology.ua.get((mol_id, local_res_i, ua_i)) if ua_topology is not None: @@ -637,7 +636,6 @@ def _get_residue_axes( if customised_axes: res = mol.residues[local_res_i] residue_topology = None - axes_topology = None if axes_topology is not None: residue_topology = axes_topology.residue.get((mol_id, local_res_i)) From 8be9be4609dbdf52d107497f73311419e2057f65 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Tue, 10 Nov 2026 16:49:02 +0000 Subject: [PATCH 03/30] residue group formation and identification of edge atoms is now independent of residue index --- CodeEntropy/levels/axes.py | 114 ++++++++++++------------- CodeEntropy/levels/nodes/covariance.py | 59 ++++--------- 2 files changed, 70 insertions(+), 103 deletions(-) diff --git a/CodeEntropy/levels/axes.py b/CodeEntropy/levels/axes.py index 87d8832a..eb859d2c 100644 --- a/CodeEntropy/levels/axes.py +++ b/CodeEntropy/levels/axes.py @@ -114,10 +114,6 @@ def get_residue_axes( ValueError: If the residue selection is empty. """ - # TODO refine selection so that it will work for branched polymers - - index_prev = index + relative_index - 1 - index_next = index + relative_index + 1 if residue is None: residue = data_container.select_atoms(f"resindex {index + relative_index}") @@ -126,12 +122,17 @@ def get_residue_axes( raise ValueError( f"Empty residue selection for resindex={index + relative_index}" ) - - edge_atom_set = data_container.atoms.select_atoms( - f"resindex {index + relative_index} and " - f"(bonded resindex {index_prev} or " - f"resindex {index_next})" + neighbours = data_container.select_atoms( + f"(not resindex {index + relative_index}) and " + f"(bonded resindex {index + relative_index})" ) + edge_atom_set = [] + for neighbour in neighbours: + edge = data_container.select_atoms( + f"(bonded resindex {neighbour.resindex}) and " + f"(resindex {index + relative_index})" + ) + edge_atom_set.append(edge[0]) if len(edge_atom_set) == 0: rot_axes, moment_of_inertia = self.get_molecule_axes(residue) @@ -203,7 +204,7 @@ def get_residue_axes_from_topology( return trans_axes, rot_axes, center, moment_of_inertia - def get_UA_axes(self, data_container, index: int, res_position): + def get_UA_axes(self, data_container, index: int, resindex: int): """Compute united-atom-level translational and rotational axes. The translational and rotational axes at the united-atom level. @@ -231,8 +232,8 @@ def get_UA_axes(self, data_container, index: int, res_position): Molecule and trajectory data. index (int): Bead index (ordinal among heavy atoms). - res_position: where the residue of interest is - in data_container + res_index (int): + Index of the residue the UA is in. Returns: Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: - trans_axes: Translational axes (3, 3). @@ -248,34 +249,26 @@ def get_UA_axes(self, data_container, index: int, res_position): """ index = int(index) # UA bead index - heavy_atoms = data_container.select_atoms("mass 2 to 999") + heavy_atoms = data_container.atoms.select_atoms("mass 2 to 999") # use the same trans axes as the residue level if len(heavy_atoms) > 1: - if len(data_container.residues) == 1: + if len(data_container.atoms.residues) == 1: # only the one residue => use principal axes residue = data_container trans_center = data_container.atoms.center_of_mass(unwrap=True) trans_axes = self.get_principal_axes_from_group(data_container.atoms) else: # residue of interest has at least one neighbour - if res_position == -1 or res_position == 1: - # look at a terminal residue - if res_position == -1: - # first residue - residue = data_container.residues[0] - resindex = residue.resindex - resindex_next = resindex + 1 - edge_atom_set = data_container.select_atoms( - f"resindex {resindex} and bonded resindex {resindex_next}" - ) - else: - # last residue - residue = data_container.residues[1] - resindex = residue.resindex - resindex_prev = resindex - 1 - edge_atom_set = data_container.select_atoms( - f"resindex {resindex} and bonded resindex {resindex_prev}" - ) + residue = data_container.select_atoms(f"resindex {resindex}") + neighbours = data_container.select_atoms(f"not resindex {resindex}") + edge_atom_set = [] + for neighbour in neighbours: + edge = data_container.select_atoms( + f"(bonded resindex {neighbour.resindex}) " + f"and (resindex {resindex})" + ) + edge_atom_set.append(edge[0]) + if len(edge_atom_set) == 1: edge_atom = edge_atom_set[0] trans_center, trans_axes = self.get_terminal_axes( residue=residue, @@ -284,15 +277,6 @@ def get_UA_axes(self, data_container, index: int, res_position): ) else: # between 2 residues - residue = data_container.residues[1] - resindex = residue.resindex - resindex_next = resindex + 1 - resindex_prev = resindex - 1 - edge_atom_set = data_container.select_atoms( - f"resindex {resindex} and " - f"(bonded resindex {resindex_prev} or " - f"resindex {resindex_next})" - ) trans_center, trans_axes = self.get_non_terminal_axes( residue=residue, edges=edge_atom_set, @@ -608,11 +592,14 @@ def get_terminal_axes(self, residue, edge, dimensions): def get_non_terminal_axes(self, residue, edges, dimensions): """ Compute rotation axes at the residue level/ translation axes at - the UA level for the non-terminal residues in a linear polymer, given the - edge atoms (i.e. heavy atoms bonded to neighbour residues) and - residue of interest. Find the shortest chain between edge atoms: the backbone. - Edges + backbone average position determine the residue rotational axes. - (see get_residue_custom_axes). If the two edge heavy atoms + the UA level for the non-terminal residues, given the edge + atoms (i.e. heavy atoms bonded to neighbour residues) and + residue of interest. If there are two edge atoms, find the shortest chain + between them: the backbone. Edges + backbone average position determine the + residue rotational axes. If there are at least three edge atoms, first two + edge atoms act the same as the edges in the linear case and the average + position of the other edges atoms acts as the backbone centre in the linear + case. (see get_residue_custom_axes). If the two edge heavy atoms are bonded to each other (i.e. there is no backbone), x-axis is set along the vector between the edge atom and average position of bonded atoms, y-axis is arbitrary and z-axis is paralel to the two. This is the @@ -626,22 +613,27 @@ def get_non_terminal_axes(self, residue, edges, dimensions): rot_center: (3,) rotation centre, rot_axes: (3,3) rotation axes of residue """ - backbone = self.get_chain(residue, edges[0], edges[1]) - backbone_center = np.zeros(3) - if len(backbone) > 0: - for heavy_atom in backbone: - backbone_center += heavy_atom.position - backbone_center /= len(backbone) - rot_center, rot_axes = self.get_residue_custom_axes( - edges.positions, backbone_center - ) + if len(edges) == 2: + backbone = self.get_chain(residue, edges[0], edges[1]) + backbone_center = np.zeros(3) + if len(backbone) > 0: + for heavy_atom in backbone: + backbone_center += heavy_atom.position + backbone_center /= len(backbone) + rot_center, rot_axes = self.get_residue_custom_axes( + [edges[0].position, edges[1].position], backbone_center + ) + else: + rot_center = (edges[0].position + edges[1].position) / 2 + rot_axes = self.get_custom_axes( + a=rot_center, + b_list=[edges[0].position], + c=np.zeros(3), + dimensions=dimensions, + ) else: - rot_center = (edges[0].position + edges[1].position) / 2 - rot_axes = self.get_bonded_vector_axes( - a=rot_center, - b_list=[edges[0].position], - c=np.zeros(3), - dimensions=dimensions, + rot_center, rot_axes = self.get_residue_custom_axes( + [edges[0].position, edges[1].position], edges[2].position ) return rot_center, rot_axes diff --git a/CodeEntropy/levels/nodes/covariance.py b/CodeEntropy/levels/nodes/covariance.py index 57a7a922..9551a8b9 100644 --- a/CodeEntropy/levels/nodes/covariance.py +++ b/CodeEntropy/levels/nodes/covariance.py @@ -185,15 +185,10 @@ def _process_united_atom( molcount: dict[tuple[int, int], int], ) -> None: """Compute united-atom second moments for one molecule. - If there are multiple residues in the molecule, build - residue group and attribute res_position: - - First residue: residue_group contains first two - residues. res_position is -1. - - Last residue: residue_group contains last two - residues. res_position is 1. - - Non-terminal residue: residue_group contains - residue of interest + two neighbours. res_position - is 0. + If there are multiple residues in the molecule, + residue group and retain residue_id for residue of + interest. Each residue group contains the residue of + interest + bonded heavy atoms in neighbouring residues. Args: u: Universe-like object used to resolve bead atom indices. @@ -211,32 +206,13 @@ def _process_united_atom( out_torque: Frame-local torque second-moment accumulator, mutated in place. molcount: Per-residue group sample counters, mutated in place. """ + relative_res_i = mol.residues[0].resindex for local_res_i, res in enumerate(mol.residues): if len(mol.residues) > 1: - if local_res_i == 0: - relative_id = res.resindex - # index relative to first in mda universe - res_position = -1 - residue_group = mol.select_atoms( - f"resindex {local_res_i + relative_id} " - f"or resindex {local_res_i + 1 + relative_id}" - ) - elif local_res_i == len(mol.residues) - 1: - res_position = 1 - residue_group = mol.select_atoms( - f"resindex {local_res_i - 1 + relative_id} " - f"or resindex {local_res_i + relative_id}" - ) - else: - res_position = 0 - residue_group = mol.select_atoms( - f"resindex {local_res_i - 1 + relative_id} " - f"or resindex {local_res_i + relative_id} " - f"or resindex {local_res_i + 1 + relative_id}" - ) + resindex = local_res_i + relative_res_i + residue_group = mol.select_atoms(f"(bonded resindex {resindex})") else: - res_position = None residue_group = res bead_key = (mol_id, "united_atom", local_res_i) bead_idx_list = beads.get(bead_key, []) @@ -251,6 +227,7 @@ def _process_united_atom( u=u, mol_id=mol_id, local_res_i=local_res_i, + relative_res_i=relative_res_i, residue_group=residue_group, bead_groups=bead_groups, axes_manager=axes_manager, @@ -259,7 +236,6 @@ def _process_united_atom( force_partitioning=force_partitioning, customised_axes=customised_axes, is_highest=is_highest, - res_position=res_position, ) F, T = self._ft.compute_frame_covariance(force_vecs, torque_vecs) @@ -441,6 +417,7 @@ def _build_ua_vectors( u: Any, mol_id: int, local_res_i: int, + relative_res_i: int, bead_groups: list[Any], residue_group: Any, axes_manager: Any, @@ -449,7 +426,6 @@ def _build_ua_vectors( force_partitioning: float, customised_axes: bool, is_highest: bool, - res_position: int, ) -> tuple[list[np.ndarray], list[np.ndarray]]: """Build force and torque vectors for united-atom beads. @@ -457,6 +433,8 @@ def _build_ua_vectors( u: Universe-like object used to resolve cached atom indices. mol_id: Molecule index used in axes-topology lookup keys. local_res_i: Local residue index used in axes-topology lookup keys. + relative_res_i: Residue index of first residue within ``mol``. This + might differ from residue index of first residue in topology. bead_groups: Atom groups representing UA beads in a residue. residue_group: Atom group for the parent residue group. axes_manager: Axes helper used to select axes, centres, and moments. @@ -472,17 +450,13 @@ def _build_ua_vectors( """ force_vecs: list[np.ndarray] = [] torque_vecs: list[np.ndarray] = [] + resindex = local_res_i + relative_res_i + residue = residue_group.atoms.select_atoms(f"resindex {resindex}") for ua_i, bead in enumerate(bead_groups): - if res_position == -1: - residue = residue_group.residues[0] - elif res_position == 0 or res_position == 1: - residue = residue_group.residues[1] - else: - residue = residue_group - if customised_axes: ua_topology = None + axes_topology = None if axes_topology is not None: ua_topology = axes_topology.ua.get((mol_id, local_res_i, ua_i)) if ua_topology is not None: @@ -496,10 +470,10 @@ def _build_ua_vectors( ) else: # we don't use the topology - make_whole(residue_group) + make_whole(residue_group.atoms) make_whole(bead) trans_axes, rot_axes, center, moi = axes_manager.get_UA_axes( - residue_group, ua_i, res_position + residue_group, ua_i, resindex ) else: @@ -635,6 +609,7 @@ def _get_residue_axes( """ if customised_axes: res = mol.residues[local_res_i] + axes_topology = None residue_topology = None if axes_topology is not None: residue_topology = axes_topology.residue.get((mol_id, local_res_i)) From a9edb4358f7e61084c7a11688e103029283ae0d0 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Wed, 11 Nov 2026 12:17:48 +0000 Subject: [PATCH 04/30] topology axes are not disabled anymore --- CodeEntropy/levels/nodes/covariance.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/CodeEntropy/levels/nodes/covariance.py b/CodeEntropy/levels/nodes/covariance.py index 9551a8b9..95718794 100644 --- a/CodeEntropy/levels/nodes/covariance.py +++ b/CodeEntropy/levels/nodes/covariance.py @@ -456,7 +456,6 @@ def _build_ua_vectors( for ua_i, bead in enumerate(bead_groups): if customised_axes: ua_topology = None - axes_topology = None if axes_topology is not None: ua_topology = axes_topology.ua.get((mol_id, local_res_i, ua_i)) if ua_topology is not None: @@ -609,7 +608,6 @@ def _get_residue_axes( """ if customised_axes: res = mol.residues[local_res_i] - axes_topology = None residue_topology = None if axes_topology is not None: residue_topology = axes_topology.residue.get((mol_id, local_res_i)) From 1be9bb6da9142f229830e3136ea810c4b43b14b6 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Wed, 11 Nov 2026 14:41:50 +0000 Subject: [PATCH 05/30] changed calling get_custom_axes (old function name) to get_bonded_vector_axes --- CodeEntropy/levels/axes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CodeEntropy/levels/axes.py b/CodeEntropy/levels/axes.py index eb859d2c..95dedf4c 100644 --- a/CodeEntropy/levels/axes.py +++ b/CodeEntropy/levels/axes.py @@ -625,7 +625,7 @@ def get_non_terminal_axes(self, residue, edges, dimensions): ) else: rot_center = (edges[0].position + edges[1].position) / 2 - rot_axes = self.get_custom_axes( + rot_axes = self.get_bonded_vector_axes( a=rot_center, b_list=[edges[0].position], c=np.zeros(3), From 089125e1e22d721b142bdaf5c338c38dd529e6b2 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Wed, 11 Nov 2026 14:44:09 +0000 Subject: [PATCH 06/30] updated unit tests for new residue group (residue of interest + neighbour atom), removed res_position, removed redundant test for 2 point terminal residue case, was the same as test_get_ua_axes_bonded_terminal_2_points --- .../levels/nodes/test_covariance_node.py | 16 ++-- tests/unit/CodeEntropy/levels/test_axes.py | 74 +++---------------- 2 files changed, 21 insertions(+), 69 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py b/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py index ff02facd..2e973f78 100644 --- a/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py +++ b/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py @@ -12,11 +12,12 @@ class FakeAtomGroup: """Small AtomGroup-like object for covariance-node tests.""" - def __init__(self, name="ag", *, length=1): + def __init__(self, name="ag", *, length=1, select_map=None): self.name = name self._length = length self.indices = np.arange(length) self.atoms = self + self._select_map = dict(select_map or {}) def __len__(self): return self._length @@ -27,6 +28,9 @@ def principal_axes(self): def center_of_mass(self, unwrap=False): return np.array([1.0, 2.0, 3.0]) + def select_atoms(self, query: str): + return self._select_map.get(query, FakeAtomGroup([])) + class FakeResidue: """Small residue-like object.""" @@ -434,6 +438,7 @@ def test_build_ua_vectors_uses_customised_axes(): u=FakeUniverse([]), mol_id=0, local_res_i=0, + relative_res_i=0, bead_groups=[FakeAtomGroup("ua")], residue_group=FakeAtomGroup("res"), axes_manager=axes_manager, @@ -442,7 +447,6 @@ def test_build_ua_vectors_uses_customised_axes(): force_partitioning=0.5, customised_axes=True, is_highest=True, - res_position=None, ) assert make_whole.call_count == 2 @@ -471,6 +475,7 @@ def test_build_ua_vectors_uses_cached_axes_topology_when_available(): u=u, mol_id=3, local_res_i=4, + relative_res_i=0, bead_groups=[FakeAtomGroup("ua")], residue_group=FakeAtomGroup("res"), axes_manager=axes_manager, @@ -479,7 +484,6 @@ def test_build_ua_vectors_uses_cached_axes_topology_when_available(): force_partitioning=0.5, customised_axes=True, is_highest=True, - res_position=None, ) assert len(force_vecs) == 1 @@ -510,6 +514,7 @@ def test_build_ua_vectors_uses_molecule_axes_when_not_customised(): u=FakeUniverse([]), mol_id=0, local_res_i=0, + relative_res_i=0, bead_groups=[FakeAtomGroup("ua")], residue_group=FakeAtomGroup("res"), axes_manager=axes_manager, @@ -518,7 +523,6 @@ def test_build_ua_vectors_uses_molecule_axes_when_not_customised(): force_partitioning=0.5, customised_axes=False, is_highest=False, - res_position=None, ) assert make_whole.call_count == 2 @@ -778,6 +782,7 @@ def test_build_ua_vectors_multiple_residues_first_residue(): u=FakeUniverse([]), mol_id=0, local_res_i=0, + relative_res_i=0, bead_groups=[FakeAtomGroup("ua")], residue_group=FakeMolecule(n_residues=2), axes_manager=axes_manager, @@ -786,7 +791,6 @@ def test_build_ua_vectors_multiple_residues_first_residue(): force_partitioning=0.5, customised_axes=True, is_highest=True, - res_position=-1, ) assert make_whole.call_count == 2 @@ -810,6 +814,7 @@ def test_build_ua_vectors_multiple_residues_middle_residue(): u=FakeUniverse([]), mol_id=0, local_res_i=0, + relative_res_i=1, bead_groups=[FakeAtomGroup("ua")], residue_group=FakeMolecule(n_residues=3), axes_manager=axes_manager, @@ -818,7 +823,6 @@ def test_build_ua_vectors_multiple_residues_middle_residue(): force_partitioning=0.5, customised_axes=True, is_highest=True, - res_position=0, ) assert make_whole.call_count == 2 diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index a58b1d49..06e80bbe 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -182,7 +182,7 @@ def _sel(q): lambda system, atom, dimensions: (np.eye(3), np.array([1.0, 2.0, 3.0])), ) - trans, rot, center, moi = ax.get_UA_axes(u, index=0, res_position=None) + trans, rot, center, moi = ax.get_UA_axes(u, index=0, resindex=3) assert np.allclose(trans, np.eye(3)) assert np.allclose(rot, np.eye(3)) @@ -216,7 +216,7 @@ def _sel(q): monkeypatch.setattr(ax, "get_bonded_axes", lambda **kwargs: (None, None)) with pytest.raises(ValueError): - ax.get_UA_axes(u, index=5, res_position=None) + ax.get_UA_axes(u, index=5, resindex=0) def test_get_bonded_vector_axes_degenerate_axis1_raises(): @@ -749,7 +749,6 @@ def test_get_UA_axes_multiple_heavy_atoms_uses_custom_principal_axes(monkeypatch ], positions=np.array([[0, 0, 0], [1, 0, 0]], dtype=float), ) - system_atom = _atom(index=0, mass=12.0, pos=[0, 0, 0]) heavy_atom_selection = _FakeAtomGroup( [system_atom], positions=np.array([[0, 0, 0]], dtype=float) @@ -770,7 +769,7 @@ def select_atoms(self, q): data_container = MagicMock() data_container.atoms = _Atoms() - data_container.residues.__len__.return_value = 1 + data_container.atoms.residues = [heavy_atoms] data_container.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90], dtype=float) _FakeAtomGroup.atoms = heavy_atom_selection @@ -790,7 +789,7 @@ def _select_atoms(q): ) trans_axes, rot_axes, center, moi = ax.get_UA_axes( - data_container, index=0, res_position=None + data_container, index=0, resindex=0 ) assert trans_axes.shape == (3, 3) @@ -1534,7 +1533,7 @@ def _select_atoms(q): ) trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=1, res_position=0 + data_container=residue_group, index=1, resindex=1 ) assert trans_axes.shape == (3, 3) @@ -1588,7 +1587,7 @@ def _select_atoms(q): ) trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=1, res_position=0 + data_container=residue_group, index=1, resindex=2 ) assert trans_axes.shape == (3, 3) @@ -1635,7 +1634,7 @@ def _select_atoms(q): lambda system, atom, dimensions: (np.eye(3), np.array([1.0, 1.0, 1.0])), ) trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=0, res_position=-1 + data_container=residue_group, index=0, resindex=0 ) assert trans_axes.shape == (3, 3) @@ -1682,7 +1681,7 @@ def _select_atoms(q): lambda system, atom, dimensions: (np.eye(3), np.array([1.0, 1.0, 1.0])), ) trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=0, res_position=1 + data_container=residue_group, index=0, resindex=2 ) assert trans_axes.shape == (3, 3) @@ -1749,7 +1748,7 @@ def _sel(q): monkeypatch.setattr(ax, "get_bonded_axes", lambda **kwargs: (None, None)) with pytest.raises(ValueError): - ax.get_UA_axes(u, index=0, res_position=None) + ax.get_UA_axes(u, index=0, resindex=0) def test_get_ua_axes_bonded_terminal_2_points(monkeypatch): @@ -1791,7 +1790,7 @@ def _select_atoms(q): ) trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=0, res_position=1 + data_container=residue_group, index=0, resindex=2 ) assert np.allclose(trans_axes, 2 * np.eye(3)) @@ -1833,7 +1832,7 @@ def _select_atoms(q): ) monkeypatch.setattr(ax, "get_chain", lambda residue, first, last: []) trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=0, res_position=0 + data_container=residue_group, index=0, resindex=1 ) assert np.allclose(trans_axes, 2 * np.eye(3)) @@ -1988,57 +1987,6 @@ def _select_atoms(q): assert np.allclose(moi, [1, 1, 1]) -def test_get_UA_axes_terminal_1_atom(monkeypatch): - ax = AxesCalculator() - residue_group = MagicMock() - residue_group.__len__ = 2 - residue = residue_group.residues[1] - heavy_atoms = _FakeAtomGroup( - [ - _atom(index=0, mass=12.0, pos=(1, 1, 1)), - _atom(index=1, mass=12.0, pos=(3, 3, 3)), - ], - ) - - def _select_atoms_data_container(q): - if q == "mass 2 to 999": - # return heavy atoms group - return heavy_atoms - if q.startswith("resindex "): - return [heavy_atoms[0]] - - def _select_atoms_residue(q): - if q == "mass 2 to 999": - # return heavy atoms group - return [heavy_atoms[0]] - if q.startswith("index"): - return [heavy_atoms[0]] - if q.startswith("(mass 2 to 999) and bonded "): - return [] - - residue_group.select_atoms.side_effect = _select_atoms_data_container - residue_group.atoms.select_atoms.side_effect = _select_atoms_data_container - residue.select_atoms.side_effect = _select_atoms_residue - residue.atoms.select_atoms.side_effect = _select_atoms_residue - monkeypatch.setattr( - ax, - "get_bonded_axes", - lambda system, atom, dimensions: (np.eye(3), 3 * np.eye(3)), - ) - monkeypatch.setattr( - ax, "get_principal_axes_from_group", lambda *_a, **_k: 2 * np.eye(3) - ) - - trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=0, res_position=-1 - ) - - assert np.allclose(trans_axes, 2 * np.eye(3)) - assert np.allclose(rot_axes, np.eye(3)) - assert np.allclose(rot_center, [1, 1, 1]) - assert np.allclose(moi, 3 * np.eye(3)) - - def test_get_terminal_axes_1point(monkeypatch): ax = AxesCalculator() residue = MagicMock() From 233229836874e1fbf40711c314ea3e95b1c5eb3a Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Thu, 12 Nov 2026 10:21:16 +0000 Subject: [PATCH 07/30] updated unit tests --- tests/unit/CodeEntropy/levels/test_axes.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 06e80bbe..6dec75db 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -1754,16 +1754,19 @@ def _sel(q): def test_get_ua_axes_bonded_terminal_2_points(monkeypatch): ax = AxesCalculator() residue_group = MagicMock() - residue_group.__len__ = 2 - residue = residue_group.residues[1] + residue = residue_group.atoms.residues[1] + print(type(residue_group.atoms.residues)) + residue_group.atoms.residues.__len__ = 2 heavy_atoms = _FakeAtomGroup( [ _atom(index=0, mass=12.0, pos=(1, 0, 0)), _atom(index=1, mass=12.0, pos=(0, 1, 0)), + _atom(index=2, mass=14.0, pos=(2, 2, 2)), ], ) edge_atom_set = [heavy_atoms[0]] bonded_atoms = [heavy_atoms[1]] + # neighbour = [heavy_atoms[2]] def _select_atoms(q): if q == "mass 2 to 999": @@ -1777,6 +1780,7 @@ def _select_atoms(q): return bonded_atoms residue_group.select_atoms.side_effect = _select_atoms + residue_group.atoms.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms monkeypatch.setattr( @@ -1803,6 +1807,7 @@ def test_get_ua_axes_non_terminal_2_atoms(monkeypatch): ax = AxesCalculator() residue_group = MagicMock() residue_group.__len__ = 3 + residue_group.atoms.residues.__len__ = 3 residue = residue_group.residues[1] heavy_atoms = _FakeAtomGroup( [ @@ -1820,6 +1825,7 @@ def _select_atoms(q): if q.startswith("resindex "): return heavy_atoms + residue_group.atoms.select_atoms.side_effect = _select_atoms residue_group.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms monkeypatch.setattr( From 16921ae82ad1595b59716448183940fb0b5e000b Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Tue, 17 Nov 2026 12:07:52 +0000 Subject: [PATCH 08/30] updated test_get_residue_bonded_axes_1_heavy_atom_backbone_2neighbours --- tests/unit/CodeEntropy/levels/test_axes.py | 33 ++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 6dec75db..c7fe5dfa 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -1359,13 +1359,30 @@ def test_get_residue_bonded_axes_1_heavy_atom_backbone_2neighbours(monkeypatch): ], positions=np.array([[1.0, 0.0, 0.0], [0.0, 0.0, 0.0]], dtype=float), ) + backbone_atom = _atom(index=9, mass=12.0, pos=[0, 1, 0]) + neighbours = _FakeAtomGroup( + [ + _atom(index=7, mass=14.0, pos=[1, 1, 1]), + _atom(index=11, mass=12.0, pos=[2, 2, 2]), + ], + positions=np.array([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]], dtype=float), + ) + neighbours[0].resindex = 0 + neighbours[1].resindex = 2 def _select_atoms(q): - if q.endswith("(bonded resindex 0 or resindex 2)"): - return edge_atom_set + if q == "resindex 1": + return residue + if q.startswith("(not resindex 1"): + return neighbours + if q == "(bonded resindex 0) and (resindex 1)": + return [edge_atom_set[0]] + if q == "(bonded resindex 2) and (resindex 1)": + return [edge_atom_set[1]] u.atoms.select_atoms.side_effect = _select_atoms + u.select_atoms.side_effect = _select_atoms monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) @@ -1496,21 +1513,16 @@ def test_get_ua_axes_bonded_axes_2neighbours_1_heavy_atom_backbone(monkeypatch): residue = residue_group.residues[1] heavy_atoms = _FakeAtomGroup( - [ + [ _atoms(index=2, mass=12.0, pos=(1,1,1)), _atom(index=3, mass=12.0, pos=(1, 0, 0)), _atom(index=5, mass=12.0, pos=(0, 1, 0)), _atom(index=7, mass=12.0, pos=(0, 0, 1)), + _atom(index=8,mass=12.0, pos=(2, 2, 2)) ], positions=np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float), ) - edge_atoms = _FakeAtomGroup( - [ - _atom(index=3, mass=12.0, pos=(1, 0, 0)), - _atom(index=7, mass=12.0, pos=(0, 0, 1)), - ], - positions=np.array([[1, 0, 0], [0, 0, 1]], dtype=float), - ) + edge_atoms = [heavy_atoms[1],heavy_atoms[3]] def _select_atoms(q): if q == "mass 2 to 999": @@ -1755,7 +1767,6 @@ def test_get_ua_axes_bonded_terminal_2_points(monkeypatch): ax = AxesCalculator() residue_group = MagicMock() residue = residue_group.atoms.residues[1] - print(type(residue_group.atoms.residues)) residue_group.atoms.residues.__len__ = 2 heavy_atoms = _FakeAtomGroup( [ From 54d53b8f57b9d524d16bbd5a5d5047e437233053 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Tue, 17 Nov 2026 15:28:06 +0000 Subject: [PATCH 09/30] updated test_get_residue_bonded_axes_1_heavy_atom_backbone_2neighbours --- tests/unit/CodeEntropy/levels/test_axes.py | 41 ++++++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index c7fe5dfa..e2b5ad6a 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -1425,12 +1425,29 @@ def test_get_residue_bonded_axes_multiple_heavy_atoms_backbone_2neighbours(monke _atom(index=9, mass=12.0, pos=[0, 1, 0]), _atom(index=10, mass=14.0, pos=[0, 2, 0]), ] + neighbours = _FakeAtomGroup( + [ + _atom(index=7, mass=14.0, pos=[1, 1, 1]), + _atom(index=13, mass=12.0, pos=[2, 2, 2]), + ], + positions=np.array([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]], dtype=float), + ) + neighbours[0].resindex = 0 + neighbours[1].resindex = 2 def _select_atoms(q): - if q.endswith("(bonded resindex 0 or resindex 2)"): - return edge_atom_set + if q == "resindex 1": + return residue + if q.startswith("(not resindex 1"): + return neighbours + if q == "(bonded resindex 0) and (resindex 1)": + return [edge_atom_set[0]] + if q == "(bonded resindex 2) and (resindex 1)": + return [edge_atom_set[1]] + u.atoms.select_atoms.side_effect = _select_atoms + u.select_atoms.side_effect = _select_atoms monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) @@ -1465,6 +1482,7 @@ def test_get_residue_bonded_axes_terminal_resid(monkeypatch): monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) residue = u.select_atoms("resindex 0") residue.__len__.return_value = 3 + uas = _FakeAtomGroup( [ _atom(index=0, mass=12.0, pos=[1, -1, 0]), @@ -1473,14 +1491,23 @@ def test_get_residue_bonded_axes_terminal_resid(monkeypatch): ] ) + neighbour = _FakeAtomGroup( + [ + _atom(index=3, mass=12.0, pos=[-1, -1, 0]), + + ] + + ) + def _select_atoms(q): - if q == "mass 2 to 999": - return uas - if q.startswith("(mass 2 to 999) and bonded"): - return [uas[1]] - if q.startswith("resindex 0 and (bonded resindex"): + if q == "resindex 0": + return residue + if q.startswith("(not resindex 0"): + return neighbour + if q == "(bonded resindex 1) and (resindex 0)": return [uas[2]] + monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) From 5b0cf1f50a9f888820275f0df7eefbd4569d9e5f Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Wed, 18 Nov 2026 16:05:12 +0000 Subject: [PATCH 10/30] fixed terminal residue unit test --- tests/unit/CodeEntropy/levels/test_axes.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index e2b5ad6a..910d91fc 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -1490,7 +1490,6 @@ def test_get_residue_bonded_axes_terminal_resid(monkeypatch): _atom(index=2, mass=12.0, pos=[0, 0, 0]), ] ) - neighbour = _FakeAtomGroup( [ _atom(index=3, mass=12.0, pos=[-1, -1, 0]), @@ -1498,19 +1497,31 @@ def test_get_residue_bonded_axes_terminal_resid(monkeypatch): ] ) + edge = uas[2] + neighbour[0].resindex= 1 + def _select_atoms(q): + if q == "mass 2 to 999": + return uas if q == "resindex 0": return residue if q.startswith("(not resindex 0"): return neighbour if q == "(bonded resindex 1) and (resindex 0)": - return [uas[2]] + return [edge] + if q.startswith("(mass 2 to 999) and bonded"): + return [uas[1]] + + print(f"The neighbour should be: {neighbour[0]}") + print(f"The edge we found should be: {edge}") + monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) + u.select_atoms.side_effect = _select_atoms u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms From 86d92b97ef68e14b23ced559deb8e50555b8c129 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Wed, 18 Nov 2026 16:06:52 +0000 Subject: [PATCH 11/30] fixed terminal residue unit test --- tests/unit/CodeEntropy/levels/test_axes.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 910d91fc..930bf5fe 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -1359,7 +1359,7 @@ def test_get_residue_bonded_axes_1_heavy_atom_backbone_2neighbours(monkeypatch): ], positions=np.array([[1.0, 0.0, 0.0], [0.0, 0.0, 0.0]], dtype=float), ) - + backbone_atom = _atom(index=9, mass=12.0, pos=[0, 1, 0]) neighbours = _FakeAtomGroup( [ @@ -1445,7 +1445,6 @@ def _select_atoms(q): if q == "(bonded resindex 2) and (resindex 1)": return [edge_atom_set[1]] - u.atoms.select_atoms.side_effect = _select_atoms u.select_atoms.side_effect = _select_atoms monkeypatch.setattr( @@ -1493,13 +1492,10 @@ def test_get_residue_bonded_axes_terminal_resid(monkeypatch): neighbour = _FakeAtomGroup( [ _atom(index=3, mass=12.0, pos=[-1, -1, 0]), - ] - ) edge = uas[2] - neighbour[0].resindex= 1 - + neighbour[0].resindex = 1 def _select_atoms(q): if q == "mass 2 to 999": @@ -1512,11 +1508,6 @@ def _select_atoms(q): return [edge] if q.startswith("(mass 2 to 999) and bonded"): return [uas[1]] - - print(f"The neighbour should be: {neighbour[0]}") - print(f"The edge we found should be: {edge}") - - monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) @@ -1551,16 +1542,17 @@ def test_get_ua_axes_bonded_axes_2neighbours_1_heavy_atom_backbone(monkeypatch): residue = residue_group.residues[1] heavy_atoms = _FakeAtomGroup( - [ _atoms(index=2, mass=12.0, pos=(1,1,1)), + [ + _atom(index=2, mass=12.0, pos=(1, 1, 1)), _atom(index=3, mass=12.0, pos=(1, 0, 0)), _atom(index=5, mass=12.0, pos=(0, 1, 0)), _atom(index=7, mass=12.0, pos=(0, 0, 1)), - _atom(index=8,mass=12.0, pos=(2, 2, 2)) + _atom(index=8, mass=12.0, pos=(2, 2, 2)), ], positions=np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float), ) - edge_atoms = [heavy_atoms[1],heavy_atoms[3]] + edge_atoms = [heavy_atoms[1], heavy_atoms[3]] def _select_atoms(q): if q == "mass 2 to 999": From 3140aa63860855c7cd5d084533475736db5d349e Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Thu, 19 Nov 2026 16:32:29 +0000 Subject: [PATCH 12/30] fixed terminal residue unit test --- tests/unit/CodeEntropy/levels/test_axes.py | 126 ++++++++++++++++----- 1 file changed, 95 insertions(+), 31 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 930bf5fe..f45cad77 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -1539,7 +1539,8 @@ def test_get_ua_axes_bonded_axes_2neighbours_1_heavy_atom_backbone(monkeypatch): ax = AxesCalculator() residue_group = MagicMock() residue_group.__len__ = 3 - residue = residue_group.residues[1] + residue = residue_group.select_atoms("resindex 1") + residue.__len__.return_value = 5 heavy_atoms = _FakeAtomGroup( [ @@ -1551,21 +1552,35 @@ def test_get_ua_axes_bonded_axes_2neighbours_1_heavy_atom_backbone(monkeypatch): ], positions=np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float), ) - - edge_atoms = [heavy_atoms[1], heavy_atoms[3]] + neighbours = _FakeAtomGroup( + [ + _atom(index=1, mass=12.0, pos=(0, -1, 0)), + _atom(index=9, mass=12.0, pos=(2, 3, 2)), + ], + positions=np.array([[0, -1, 0], [2, 3, 2]], dtype=float), + ) def _select_atoms(q): + if q == "resindex 1": + return residue if q == "mass 2 to 999": # return heavy atoms group return heavy_atoms - if q.startswith("resindex "): - return edge_atoms if q.startswith("index "): return [heavy_atoms[1]] + if q == ("not resindex 1"): + return neighbours + if q == ("(bonded resindex 0) and (resindex 1)"): + return [heavy_atoms[0]] + if q == ("(bonded resindex 2) and (resindex 1)"): + return [heavy_atoms[3]] residue_group.select_atoms.side_effect = _select_atoms + residue_group.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms + neighbours[0].resindex = 0 + neighbours[1].resindex = 2 monkeypatch.setattr(ax, "get_chain", lambda residue, first, last: [heavy_atoms[1]]) monkeypatch.setattr( @@ -1588,7 +1603,7 @@ def test_get_ua_axes_bonded_axes_2neighbours_multiple_heavy_atoms_backbone(monke ax = AxesCalculator() residue_group = MagicMock() residue_group.__len__ = 3 - residue = residue_group.residues[1] + residue = residue_group.select_atoms("resindex 2") heavy_atoms = _FakeAtomGroup( [ @@ -1608,16 +1623,34 @@ def test_get_ua_axes_bonded_axes_2neighbours_multiple_heavy_atoms_backbone(monke positions=np.array([[1, 0, 0], [0, 1, 1]], dtype=float), ) + neighbours = _FakeAtomGroup( + [ + _atom(index=1, mass=12.0, pos=(0, -1, 0)), + _atom(index=10, mass=12.0, pos=(2, 3, 2)), + ], + positions=np.array([[0, -1, 0], [2, 3, 2]], dtype=float), + ) + def _select_atoms(q): if q == "mass 2 to 999": # return heavy atoms group return heavy_atoms - if q.startswith("resindex "): - return edge_atoms - if q.startswith("index "): + if q == "resindex 2": + return residue + if q == ("not resindex 2"): + return neighbours + if q == ("(bonded resindex 1) and (resindex 2)"): + return [edge_atoms[0]] + if q == ("(bonded resindex 3) and (resindex 2)"): + return [edge_atoms[1]] + if q == ("index 5"): return [heavy_atoms[1]] + neighbours[0].resindex = 1 + neighbours[1].resindex = 3 + residue_group.select_atoms.side_effect = _select_atoms + residue_group.atoms.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms monkeypatch.setattr( ax, "get_chain", lambda residue, first, last: [heavy_atoms[1], heavy_atoms[2]] @@ -1651,23 +1684,34 @@ def test_get_ua_axes_bonded_axes_first_resid(monkeypatch): ], ) + neighbour = _FakeAtomGroup( + [ + _atom(index=3, mass=12.0, pos=(1, 1, 1)), + ], + ) + neighbour[0].resindex = 1 + residue.atoms[0] = heavy_atoms[0] residue.atoms[0].position = heavy_atoms[0].position - edge_atom_set = [heavy_atoms[2]] bonded_atoms = [heavy_atoms[1]] def _select_atoms(q): + if q == "resindex 0": + return residue if q == "mass 2 to 999": # return heavy atoms group return heavy_atoms - if q.startswith("index"): + if q == ("index 0"): return [heavy_atoms[0]] - if q.startswith("resindex "): - return edge_atom_set if q.startswith("(mass 2 to 999) and bonded index "): return bonded_atoms + if q == "not resindex 0": + return neighbour + if q == "(bonded resindex 1) and (resindex 0)": + return [heavy_atoms[2]] residue_group.select_atoms.side_effect = _select_atoms + residue_group.atoms.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms monkeypatch.setattr( @@ -1689,32 +1733,43 @@ def test_get_ua_axes_bonded_axes_last_resid(monkeypatch): ax = AxesCalculator() residue_group = MagicMock() residue_group.__len__ = 2 - residue = residue_group.residues[0] + residue = residue_group.residues[1] heavy_atoms = _FakeAtomGroup( [ - _atom(index=0, mass=12.0, pos=(1, 0, 0)), - _atom(index=1, mass=12.0, pos=(0, 1, 0)), - _atom(index=2, mass=12.0, pos=(0, 0, 1)), + _atom(index=1, mass=12.0, pos=(1, 0, 0)), + _atom(index=2, mass=12.0, pos=(0, 1, 0)), + _atom(index=3, mass=12.0, pos=(0, 0, 1)), ], ) + neighbour = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + ], + ) + neighbour[0].resindex = 1 + residue.atoms[0] = heavy_atoms[0] residue.atoms[0].position = heavy_atoms[0].position - edge_atom_set = [heavy_atoms[0]] bonded_atoms = [heavy_atoms[1]] def _select_atoms(q): + if q == "resindex 2": + return residue if q == "mass 2 to 999": # return heavy atoms group return heavy_atoms - if q.startswith("index"): + if q == ("index 1"): return [heavy_atoms[0]] - if q.startswith("resindex "): - return edge_atom_set if q.startswith("(mass 2 to 999) and bonded index "): return bonded_atoms + if q == "not resindex 2": + return neighbour + if q == "(bonded resindex 1) and (resindex 2)": + return [heavy_atoms[0]] residue_group.select_atoms.side_effect = _select_atoms + residue_group.atoms.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms monkeypatch.setattr( @@ -1775,7 +1830,7 @@ def test_get_UA_axes_raises_when_only_rot_axes_fail(monkeypatch): _atom(index=0, mass=12.0, pos=(1, 0, 0)), _atom(index=1, mass=12.0, pos=(0, 1, 0)), ] - u.residues = [heavy_atoms] + u.atoms.residues = [heavy_atoms] def _sel(q): if q == "mass 2 to 999": @@ -1797,26 +1852,35 @@ def test_get_ua_axes_bonded_terminal_2_points(monkeypatch): ax = AxesCalculator() residue_group = MagicMock() residue = residue_group.atoms.residues[1] - residue_group.atoms.residues.__len__ = 2 + residue_group.atoms.residues.__len__.return_value = 2 heavy_atoms = _FakeAtomGroup( [ - _atom(index=0, mass=12.0, pos=(1, 0, 0)), - _atom(index=1, mass=12.0, pos=(0, 1, 0)), - _atom(index=2, mass=14.0, pos=(2, 2, 2)), + _atom(index=1, mass=12.0, pos=(1, 0, 0)), + _atom(index=2, mass=12.0, pos=(0, 1, 0)), + _atom(index=3, mass=14.0, pos=(2, 2, 2)), ], ) - edge_atom_set = [heavy_atoms[0]] + bonded_atoms = [heavy_atoms[1]] - # neighbour = [heavy_atoms[2]] + neighbour = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + ], + ) + neighbour[0].resindex = 1 def _select_atoms(q): if q == "mass 2 to 999": # return heavy atoms group return heavy_atoms - if q.startswith("index"): + if q == "index 1": + return [heavy_atoms[0]] + if q == "resindex 2": + return residue + if q == "not resindex 2": + return neighbour + if q == "(bonded resindex 1) and (resindex 2)": return [heavy_atoms[0]] - if q.startswith("resindex "): - return edge_atom_set if q.startswith("(mass 2 to 999) and bonded index "): return bonded_atoms From 464f3ff782365addb4e60d4fe68ded3fa60c6b85 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Thu, 19 Nov 2026 17:52:51 +0000 Subject: [PATCH 13/30] updated covariance node unit tests --- tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py b/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py index e19e1474..cfe5629c 100644 --- a/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py +++ b/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py @@ -846,6 +846,7 @@ def test_build_ua_vectors_topology_falls_back_to_local_when_key_missing(): u=FakeUniverse([]), mol_id=0, local_res_i=0, + relative_res_i=0, bead_groups=[FakeAtomGroup("ua")], residue_group=FakeAtomGroup("res"), axes_manager=axes_manager, @@ -854,7 +855,6 @@ def test_build_ua_vectors_topology_falls_back_to_local_when_key_missing(): force_partitioning=0.5, axes="topology", is_highest=True, - res_position=None, ) axes_manager.get_UA_axes.assert_called_once() @@ -869,6 +869,7 @@ def test_build_ua_vectors_raises_for_unknown_axes(): u=FakeUniverse([]), mol_id=0, local_res_i=0, + relative_res_i=0, bead_groups=[FakeAtomGroup("ua")], residue_group=FakeAtomGroup("res"), axes_manager=MagicMock(), @@ -877,7 +878,6 @@ def test_build_ua_vectors_raises_for_unknown_axes(): force_partitioning=0.5, axes="bogus", is_highest=True, - res_position=None, ) From 4c189410b4d721618e12fde04e771a2849bea015 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 16:03:04 +0000 Subject: [PATCH 14/30] updated axes unit tests --- tests/unit/CodeEntropy/levels/test_axes.py | 124 ++++++++++++++++----- 1 file changed, 97 insertions(+), 27 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index f45cad77..9ee455a2 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -1857,8 +1857,7 @@ def test_get_ua_axes_bonded_terminal_2_points(monkeypatch): [ _atom(index=1, mass=12.0, pos=(1, 0, 0)), _atom(index=2, mass=12.0, pos=(0, 1, 0)), - _atom(index=3, mass=14.0, pos=(2, 2, 2)), - ], + ] ) bonded_atoms = [heavy_atoms[1]] @@ -1887,6 +1886,7 @@ def _select_atoms(q): residue_group.select_atoms.side_effect = _select_atoms residue_group.atoms.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms + residue.select_atoms.side_effect = _select_atoms monkeypatch.setattr( ax, @@ -1911,24 +1911,37 @@ def _select_atoms(q): def test_get_ua_axes_non_terminal_2_atoms(monkeypatch): ax = AxesCalculator() residue_group = MagicMock() - residue_group.__len__ = 3 - residue_group.atoms.residues.__len__ = 3 + residue_group.atoms.residues.__len__.return_value = 3 residue = residue_group.residues[1] heavy_atoms = _FakeAtomGroup( [ - _atom(index=0, mass=12.0, pos=(1, 1, 1)), - _atom(index=1, mass=12.0, pos=(3, 3, 3)), + _atom(index=1, mass=12.0, pos=(1, 1, 1)), + _atom(index=2, mass=12.0, pos=(3, 3, 3)), + ], + ) + neighbours = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=3, mass=12.0, pos=(3, 3, 4)), ], ) + neighbours[0].resindex = 0 + neighbours[1].resindex = 2 def _select_atoms(q): if q == "mass 2 to 999": # return heavy atoms group return heavy_atoms - if q.startswith("index"): + if q == "resindex 1": + return residue + if q == "index 1": return [heavy_atoms[0]] - if q.startswith("resindex "): - return heavy_atoms + if q == "not resindex 1": + return neighbours + if q == ("(bonded resindex 0) and (resindex 1)"): + return [heavy_atoms[0]] + if q == ("(bonded resindex 2) and (resindex 1)"): + return [heavy_atoms[1]] residue_group.atoms.select_atoms.side_effect = _select_atoms residue_group.select_atoms.side_effect = _select_atoms @@ -1959,22 +1972,41 @@ def test_get_residue_axes_non_terminal_2_atoms(monkeypatch): monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) residue = u.select_atoms("resindex 5") residue.__len__.return_value = 2 + u.atoms.residues.__len__.return_value = 3 monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) + uas = _FakeAtomGroup( [ - _atom(index=0, mass=12.0, pos=(1, 1, 1)), - _atom(index=1, mass=12.0, pos=(3, 3, 3)), + _atom(index=1, mass=12.0, pos=(1, 1, 1)), + _atom(index=2, mass=12.0, pos=(3, 3, 3)), + ], + ) + + neighbours = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=3, mass=12.0, pos=(3, 3, 4)), ], ) + neighbours[0].resindex = 4 + neighbours[1].resindex = 6 def _select_atoms(q): if q == "mass 2 to 999": + # return heavy atoms group return uas - if q.startswith("resindex 5 and (bonded resindex"): - return uas + if q == "resindex 5": + return residue + if q.startswith("(not resindex 5)"): + return neighbours + if q == ("(bonded resindex 4) and (resindex 5)"): + return [uas[0]] + if q == ("(bonded resindex 6) and (resindex 5)"): + return [uas[1]] + u.select_atoms.side_effect = _select_atoms u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = residue monkeypatch.setattr(ax, "get_chain", lambda residue, first, last: []) @@ -2012,22 +2044,34 @@ def test_get_residue_axes_terminal_2_atoms(monkeypatch): [ _atom(index=0, mass=12.0, pos=(1, 0, 0)), _atom(index=1, mass=12.0, pos=(0, 1, 0)), - _atom(index=2, mass=12.0, pos=(0, 0, 1)), ], ) monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) + neighbour = _FakeAtomGroup( + [ + _atom(index=2, mass=12.0, pos=(2, 1, 1)), + ], + ) + neighbour[0].resindex = 1 + bonded_atoms = [uas[0]] + def _select_atoms(q): if q == "mass 2 to 999": + # return heavy atoms group return uas - if q.startswith("resindex 0 and (bonded resindex"): - # the edge atom - return [uas[2]] + if q == "resindex 0": + return residue + if q.startswith("(not resindex 0)"): + return neighbour + if q == "(bonded resindex 1) and (resindex 0)": + return [uas[1]] if q.startswith("(mass 2 to 999) and bonded index "): - return uas[0:2] + return bonded_atoms + u.select_atoms.side_effect = _select_atoms u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms @@ -2048,7 +2092,7 @@ def _select_atoms(q): ) assert np.allclose(trans_axes, np.eye(3)) assert np.allclose(rot_axes, 2 * np.eye(3)) - assert np.allclose(rot_center, [0, 0, 1]) + assert np.allclose(rot_center, [0, 1, 0]) assert np.allclose(moi, [1, 1, 1]) @@ -2064,6 +2108,12 @@ def test_get_res_axes_terminal_1_atom(monkeypatch): _atom(index=0, mass=12.0, pos=(1, 0, 0)), ], ) + neighbour = _FakeAtomGroup( + [ + _atom(index=1, mass=12.0, pos=(2, 1, 1)), + ], + ) + neighbour[0].resindex = 1 monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) @@ -2071,11 +2121,16 @@ def test_get_res_axes_terminal_1_atom(monkeypatch): def _select_atoms(q): if q == "mass 2 to 999": return uas + if q == "resindex 0": + return residue if q.startswith("(mass 2 to 999) and bonded index "): return [] - if q.startswith("resindex "): + if q.startswith("(not resindex 0)"): + return neighbour + if q == "(bonded resindex 1) and (resindex 0)": return [uas[0]] + u.select_atoms.side_effect = _select_atoms u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms @@ -2247,22 +2302,37 @@ def test_get_residue_axes_all_atom_MOI(monkeypatch): monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) atoms = _FakeAtomGroup( [ - _atom(index=0, mass=12.0, pos=(1, 0, 0)), - _atom(index=1, mass=12.0, pos=(0, 1, 0)), - _atom(index=2, mass=12.0, pos=(0, 0, 1)), - _atom(index=3, mass=1.0, pos=(0, 0, 0)), - _atom(index=4, mass=1.0, pos=(1, 1, 1)), + _atom(index=1, mass=12.0, pos=(1, 0, 0)), + _atom(index=2, mass=12.0, pos=(0, 1, 0)), + _atom(index=3, mass=12.0, pos=(0, 0, 1)), + _atom(index=4, mass=1.0, pos=(0, 0, 0)), + _atom(index=5, mass=1.0, pos=(1, 1, 1)), ], positions=np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0], [1, 1, 1]]), ) + neighbours = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(1, 2, 1)), + _atom(index=6, mass=12.0, pos=(3, 3, 4)), + ], + ) residue.atoms.positions = atoms.positions residue.atoms.masses = atoms.masses + neighbours[0].resindex = 0 + neighbours[1].resindex = 2 def _select_atoms(q): - if q.startswith("resindex"): - return atoms[0:2] + if q == ("resindex 1"): + return residue + if q.startswith("(not resindex 1)"): + return neighbours + if q == "(bonded resindex 0) and (resindex 1)": + return [atoms[0]] + if q == "(bonded resindex 2) and (resindex 1)": + return [atoms[2]] u.atoms.select_atoms.side_effect = _select_atoms + u.select_atoms.side_effect = _select_atoms monkeypatch.setattr(ax, "get_principal_axes_from_group", lambda _ag: 2 * np.eye(3)) monkeypatch.setattr( ax, From e4c0eda957672a3d934a4420b3318039c4dfaaa3 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 16:05:31 +0000 Subject: [PATCH 15/30] merged with updates from main --- config.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 config.yaml diff --git a/config.yaml b/config.yaml new file mode 100644 index 00000000..83d26a3d --- /dev/null +++ b/config.yaml @@ -0,0 +1,12 @@ +--- + +run1: + top_traj_file: + - "../thioredoxin.prmtop" + - "../thioredoxin_1_imaged.nc" + selection_string: "resid 29-34" + start: 0 + end: 100 + step: 1 + combined_forcetorque: False + axes: "local" From 3dd12d3f3eb832f72ccd8f2ecb2eaacc9c16978a Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 17:33:00 +0000 Subject: [PATCH 16/30] updated covariance node tests --- tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py b/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py index cfe5629c..2197c805 100644 --- a/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py +++ b/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py @@ -171,7 +171,7 @@ def test_process_united_atom_updates_outputs_and_molcount(): out_force = {"ua": {}, "res": {}, "poly": {}} out_torque = {"ua": {}, "res": {}, "poly": {}} molcount = {} - + mol.residues[0].resindex = 0 node._process_united_atom( u=universe, mol=mol, @@ -199,7 +199,7 @@ def test_process_united_atom_returns_when_no_beads_or_empty_atom_groups(): mol = FakeMolecule(n_residues=1) out_force = {"ua": {}, "res": {}, "poly": {}} out_torque = {"ua": {}, "res": {}, "poly": {}} - + mol.residues[0].resindex = 7 node._process_united_atom( u=FakeUniverse([mol]), mol=mol, From d17cb50d47b8d4527bf5bedd0cac1927c169f9c7 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 18:34:29 +0000 Subject: [PATCH 17/30] covariance node tests --- .../levels/nodes/test_covariance_node.py | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py b/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py index 2197c805..ac18473c 100644 --- a/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py +++ b/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py @@ -461,6 +461,15 @@ def test_build_ua_vectors_uses_cached_axes_topology_when_available(): u = FakeUniverse([]) ua_topology = object() axes_topology = SimpleNamespace(ua={(3, 4, 0): ua_topology}) + residue_group = FakeAtomGroup("res") + residue = FakeResidue() + residue.atoms.name = residue_group.name + + def _select_atoms(q): + if q == "resindex 7": + return residue + + residue_group.select_atoms = _select_atoms axes_manager.get_UA_axes_from_topology.return_value = ( np.eye(3), @@ -475,9 +484,9 @@ def test_build_ua_vectors_uses_cached_axes_topology_when_available(): u=u, mol_id=3, local_res_i=4, - relative_res_i=0, + relative_res_i=3, bead_groups=[FakeAtomGroup("ua")], - residue_group=FakeAtomGroup("res"), + residue_group=residue_group, axes_manager=axes_manager, axes_topology=axes_topology, box=None, @@ -776,7 +785,14 @@ def test_build_ua_vectors_multiple_residues_first_residue(): ) node._ft.get_weighted_forces = MagicMock(return_value=np.array([1.0, 0.0, 0.0])) node._ft.get_weighted_torques = MagicMock(return_value=np.array([0.0, 1.0, 0.0])) + residue = FakeResidue() + def _select_atoms(q): + if q == "resindex 0": + return residue + + residue_group = FakeMolecule(n_residues=2) + residue_group.select_atoms = _select_atoms with patch("CodeEntropy.levels.nodes.covariance.make_whole") as make_whole: force_vecs, torque_vecs = node._build_ua_vectors( u=FakeUniverse([]), @@ -784,7 +800,7 @@ def test_build_ua_vectors_multiple_residues_first_residue(): local_res_i=0, relative_res_i=0, bead_groups=[FakeAtomGroup("ua")], - residue_group=FakeMolecule(n_residues=2), + residue_group=residue_group, axes_manager=axes_manager, axes_topology=None, box=None, @@ -808,6 +824,14 @@ def test_build_ua_vectors_multiple_residues_middle_residue(): ) node._ft.get_weighted_forces = MagicMock(return_value=np.array([1.0, 0.0, 0.0])) node._ft.get_weighted_torques = MagicMock(return_value=np.array([0.0, 1.0, 0.0])) + residue = FakeResidue() + + def _select_atoms(q): + if q == "resindex 1": + return residue + + residue_group = FakeMolecule(n_residues=3) + residue_group.select_atoms = _select_atoms with patch("CodeEntropy.levels.nodes.covariance.make_whole") as make_whole: force_vecs, torque_vecs = node._build_ua_vectors( @@ -816,7 +840,7 @@ def test_build_ua_vectors_multiple_residues_middle_residue(): local_res_i=0, relative_res_i=1, bead_groups=[FakeAtomGroup("ua")], - residue_group=FakeMolecule(n_residues=3), + residue_group=residue_group, axes_manager=axes_manager, axes_topology=None, box=None, From d2980e510f08dcd11f878765e26ed91b6b62aa13 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 20:26:10 +0000 Subject: [PATCH 18/30] changed documentation to introduce branched residues match labels used in paper --- docs/source/science.rst | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/source/science.rst b/docs/source/science.rst index b42af0ac..7a786967 100644 --- a/docs/source/science.rst +++ b/docs/source/science.rst @@ -70,13 +70,14 @@ The axes for this transformation are calculated for each bead in each time step. For the polymer level, the translational and rotational axes are defined as the principal axes of the molecule. -For the residue level, there are three situations. -When the residue is not bonded to any other residues, the translational and rotational axes are the principal axes of the molecule. -When the residue is part of a larger polymer and is not a terminus of that polymer, the translational axes are the principal axes of the polymer, and the rotational axes are defined from the two heavy atoms bonded to neighbour residues (E1,E2) and the average position of all other backbone atoms in the residue (C). The backbone of a residue is defined as the shortest path between the two edge atoms of the residue, i.e.the two heavy atoms bonded to neighbour residues.The centre of rotation (O) is located at the point where the perpendicular from C meets the E1-E2 vector. -When the residue is part of a larger polymer and is a terminus of that polymer, the translational axes are the principal axes of the polymer, and the rotational axes are defined from the heavy atom bonded to a -neighbour residue (E1), the average position of all heavy atoms bonded to E1 (C) and the average position of all other heavy atoms in the residue (E2). The centre of rotation (O) is defined the same as above forthe non-terminal residue case. - -For the united atom level, the translational axes are defined as the residue rotational axes and the rotational axes are defined from the average position of the bonds to neighbouring heavy atoms. +For the residue level, translational axes correspond to the principal axes of the molecule. For rotational axes, there are four situations. +1. When the residue is not bonded to any other residues, rotational axes are the same as translational axes. +2. When the residue is part of a polymer and is a terminus of that polymer, rotational axes are defined from the heavy atom bonded to a +neighbour residue (A1), the average position of all heavy atoms bonded to A1 (A3) and the average position of all other heavy atoms in the residue (A2). The centre of rotation (O) is located at the point where the perpendicular from A3 meets the A1-A2 vector. The x axis is then defined along the O-A1 vector, the y axis is defined along the O-A3 vector, and the z axis is defined as perpendicular to the x and y axes. +3. When the residue is part of a larger polymer and is bonded to two other residues within that polymer, rotational axes are defined from the two heavy atoms bonded to neighbour residues (A1,A2) and the average position of all other backbone atoms in the residue (A3). The backbone of a residue is defined as the shortest path between A1 and A2. The axes and centre of rotation are then defined in the same manner as in the terminal residue case. +4. When the residue is part of a larger polymer and is bonded to three or more residues within that polymer, rotational axes are defined from three of the heavy atom bonded to neighbour residues (E1,E2,E3). The axes and centre of rotation are then defined in the same manner as in the cases above. + +For the united atom level, the translational axes correspond to the residue rotational axes and the rotational axes are defined based on bonds to neighbouring heavy atoms. If there are no bonds to other heavy atoms, the principal axes of the molecule are used. Conformational Entropy From a51587f2a9da6de14dedadbc45ad1ad20f6077bd Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 21:27:41 +0000 Subject: [PATCH 19/30] new tests for branched residues --- tests/unit/CodeEntropy/levels/test_axes.py | 170 +++++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 9ee455a2..8e8a13a6 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -2347,3 +2347,173 @@ def _select_atoms(q): assert np.allclose(rot_axes, np.eye(3)) assert np.allclose(rot_center, [1, 0, 0]) assert np.allclose(moi, [26, 38, 38]) + +def get_residue_axes_branched(monkeypatch): + ax = AxesCalculator() + u = MagicMock() + u.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90]) + monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) + residue = u.select_atoms("resindex 5") + residue.__len__.return_value = 3 + u.atoms.residues.__len__.return_value = 4 + monkeypatch.setattr( + ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) + ) + + uas = _FakeAtomGroup( + [ + _atom(index=1, mass=12.0, pos=(1, 1, 1)), + _atom(index=2, mass=12.0, pos=(3, 3, 3)), + _atom(index=3, mass = 16.0, pos = (4,4,4)) + ], + ) + + neighbours = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=4, mass=12.0, pos=(3, 3, 4)), + _atom(index = 6, mass = 16.0, pos=(4,5,4)) + ], + ) + neighbours[0].resindex = 4 + neighbours[1].resindex = 6 + neighbours[2].resindex = 8 + + def _select_atoms(q): + if q == "mass 2 to 999": + # return heavy atoms group + return uas + if q == "resindex 5": + return residue + if q.startswith("(not resindex 5)"): + return neighbours + if q == ("(bonded resindex 4) and (resindex 5)"): + return [uas[0]] + if q == ("(bonded resindex 6) and (resindex 5)"): + return [uas[1]] + if q == ("(bonded resindex 8) and (resindex 5)"): + return [uas[2]] + + u.select_atoms.side_effect = _select_atoms + u.atoms.select_atoms.side_effect = _select_atoms + residue.select_atoms.side_effect = residue + monkeypatch.setattr( + ax, "get_residue_custom_axes", lambda edges, center: 2 * np.eye(3), [2,2,2] + ) + monkeypatch.setattr( + ax, + "get_custom_residue_moment_of_inertia", + lambda center_of_mass, positions, masses, custom_rot_axes, dimensions: np.array( + [1, 1, 1] + ), + ) + + trans_axes, rot_axes, rot_center, moi = ax.get_residue_axes( + data_container=u, + index=5, + relative_index=0, + ) + + assert np.allclose(trans_axes, np.eye(3)) + assert np.allclose(rot_axes, 2 * np.eye(3)) + assert np.allclose(rot_center, [2, 2, 2]) + assert np.allclose(moi, [1, 1, 1]) + + + +def get_ua_axes_branched(monkeypatch): + ax = AxesCalculator() + u = MagicMock() + u.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90]) + monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) + residue = u.select_atoms("resindex 5") + residue.__len__.return_value = 3 + u.atoms.residues.__len__.return_value = 4 + + + heavy_atoms = _FakeAtomGroup( + [ + _atom(index=1, mass=12.0, pos=(1, 1, 1)), + _atom(index=2, mass=12.0, pos=(3, 3, 3)), + _atom(index=3, mass = 16.0, pos = (4,4,4)) + ], + ) + + neighbours = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=4, mass=12.0, pos=(3, 3, 4)), + _atom(index = 6, mass = 16.0, pos=(4,5,4)) + ], + ) + neighbours[0].resindex = 4 + neighbours[1].resindex = 6 + neighbours[2].resindex = 8 + + def _select_atoms(q): + if q == "mass 2 to 999": + # return heavy atoms group + return uas + if q == "resindex 5": + return residue + if q == "index 3": + return [heavy_atoms[2]] + if q == "not resindex 5": + return neighbours + if q == ("(bonded resindex 4) and (resindex 5)"): + return [uas[0]] + if q == ("(bonded resindex 6) and (resindex 5)"): + return [uas[1]] + if q == ("(bonded resindex 8) and (resindex 5)"): + return [uas[2]] + + u.select_atoms.side_effect = _select_atoms + u.atoms.select_atoms.side_effect = _select_atoms + residue.select_atoms.side_effect = residue + monkeypatch.setattr( + ax, "get_residue_custom_axes", lambda edges, center: (2 * np.eye(3), [2,2,2]) + ) + monkeypatch.setattr( + ax, "get_bonded_axes", lambda system, atom, dimensions: (3* np.eye(3), [12,12,12]) + ) + + + trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( + data_container=u, + index=2, + resindex=5, + ) + + assert np.allclose(trans_axes, 2 * np.eye(3)) + assert np.allclose(rot_axes, 3 * np.eye(3)) + assert np.allclose(rot_center, [4, 4, 4]) + assert np.allclose(moi, [12, 12, 12]) + +def get_non_terminal_axes_branched(monkeypatch): + ax = AxesCalculator() + residue = MagicMock() + residue.__len__ = 4 + heavy_atoms = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=1, mass=12.0, pos=(1, 0, 0)), + _atom(index=2, mass=14.0, pos=(3, 0, 0)), + _atom(index=3, mass=12.0, pos=(1, 1, 0)), + ], + ) + edges = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=2, mass=14.0, pos=(3, 0, 0)), + _atom(index=3, mass=12.0, pos=(1, 1, 0)), + ], + ) + monkeypatch.setattr( + ax, "get_residue_custom_axes", lambda edges, center: ([1, 0, 0], 3 * np.eye(3)) + ) + centre, axes = ax.get_non_terminal_axes( + residue=residue, edges=edges, dimensions=np.array([1, 1, 1]) + ) + + assert np.allclose(centre, [1, 0, 0]) + assert np.allclose(axes, 3 * np.eye(3)) \ No newline at end of file From ff946bd5ab1d0a9a4409ba3a46984131890d1a1b Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 21:31:29 +0000 Subject: [PATCH 20/30] new tests for branched residues --- tests/unit/CodeEntropy/levels/test_axes.py | 39 +++++++++------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 8e8a13a6..82a6351d 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -2348,6 +2348,7 @@ def _select_atoms(q): assert np.allclose(rot_center, [1, 0, 0]) assert np.allclose(moi, [26, 38, 38]) + def get_residue_axes_branched(monkeypatch): ax = AxesCalculator() u = MagicMock() @@ -2364,7 +2365,7 @@ def get_residue_axes_branched(monkeypatch): [ _atom(index=1, mass=12.0, pos=(1, 1, 1)), _atom(index=2, mass=12.0, pos=(3, 3, 3)), - _atom(index=3, mass = 16.0, pos = (4,4,4)) + _atom(index=3, mass=16.0, pos=(4, 4, 4)), ], ) @@ -2372,7 +2373,7 @@ def get_residue_axes_branched(monkeypatch): [ _atom(index=0, mass=12.0, pos=(0, 0, 0)), _atom(index=4, mass=12.0, pos=(3, 3, 4)), - _atom(index = 6, mass = 16.0, pos=(4,5,4)) + _atom(index=6, mass=16.0, pos=(4, 5, 4)), ], ) neighbours[0].resindex = 4 @@ -2398,7 +2399,7 @@ def _select_atoms(q): u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = residue monkeypatch.setattr( - ax, "get_residue_custom_axes", lambda edges, center: 2 * np.eye(3), [2,2,2] + ax, "get_residue_custom_axes", lambda edges, center: 2 * np.eye(3), [2, 2, 2] ) monkeypatch.setattr( ax, @@ -2420,7 +2421,6 @@ def _select_atoms(q): assert np.allclose(moi, [1, 1, 1]) - def get_ua_axes_branched(monkeypatch): ax = AxesCalculator() u = MagicMock() @@ -2430,12 +2430,11 @@ def get_ua_axes_branched(monkeypatch): residue.__len__.return_value = 3 u.atoms.residues.__len__.return_value = 4 - heavy_atoms = _FakeAtomGroup( [ _atom(index=1, mass=12.0, pos=(1, 1, 1)), _atom(index=2, mass=12.0, pos=(3, 3, 3)), - _atom(index=3, mass = 16.0, pos = (4,4,4)) + _atom(index=3, mass=16.0, pos=(4, 4, 4)), ], ) @@ -2443,7 +2442,7 @@ def get_ua_axes_branched(monkeypatch): [ _atom(index=0, mass=12.0, pos=(0, 0, 0)), _atom(index=4, mass=12.0, pos=(3, 3, 4)), - _atom(index = 6, mass = 16.0, pos=(4,5,4)) + _atom(index=6, mass=16.0, pos=(4, 5, 4)), ], ) neighbours[0].resindex = 4 @@ -2453,7 +2452,7 @@ def get_ua_axes_branched(monkeypatch): def _select_atoms(q): if q == "mass 2 to 999": # return heavy atoms group - return uas + return heavy_atoms if q == "resindex 5": return residue if q == "index 3": @@ -2461,23 +2460,24 @@ def _select_atoms(q): if q == "not resindex 5": return neighbours if q == ("(bonded resindex 4) and (resindex 5)"): - return [uas[0]] + return [heavy_atoms[0]] if q == ("(bonded resindex 6) and (resindex 5)"): - return [uas[1]] + return [heavy_atoms[1]] if q == ("(bonded resindex 8) and (resindex 5)"): - return [uas[2]] + return [heavy_atoms[2]] u.select_atoms.side_effect = _select_atoms u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = residue monkeypatch.setattr( - ax, "get_residue_custom_axes", lambda edges, center: (2 * np.eye(3), [2,2,2]) + ax, "get_residue_custom_axes", lambda edges, center: (2 * np.eye(3), [2, 2, 2]) ) monkeypatch.setattr( - ax, "get_bonded_axes", lambda system, atom, dimensions: (3* np.eye(3), [12,12,12]) + ax, + "get_bonded_axes", + lambda system, atom, dimensions: (3 * np.eye(3), [12, 12, 12]), ) - trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( data_container=u, index=2, @@ -2489,18 +2489,11 @@ def _select_atoms(q): assert np.allclose(rot_center, [4, 4, 4]) assert np.allclose(moi, [12, 12, 12]) + def get_non_terminal_axes_branched(monkeypatch): ax = AxesCalculator() residue = MagicMock() residue.__len__ = 4 - heavy_atoms = _FakeAtomGroup( - [ - _atom(index=0, mass=12.0, pos=(0, 0, 0)), - _atom(index=1, mass=12.0, pos=(1, 0, 0)), - _atom(index=2, mass=14.0, pos=(3, 0, 0)), - _atom(index=3, mass=12.0, pos=(1, 1, 0)), - ], - ) edges = _FakeAtomGroup( [ _atom(index=0, mass=12.0, pos=(0, 0, 0)), @@ -2516,4 +2509,4 @@ def get_non_terminal_axes_branched(monkeypatch): ) assert np.allclose(centre, [1, 0, 0]) - assert np.allclose(axes, 3 * np.eye(3)) \ No newline at end of file + assert np.allclose(axes, 3 * np.eye(3)) From 5b9d88e5f79c46df43b5613ced15ca396da0aafe Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 22:10:13 +0000 Subject: [PATCH 21/30] unit tests --- tests/unit/CodeEntropy/levels/test_axes.py | 163 --------------------- 1 file changed, 163 deletions(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 82a6351d..9ee455a2 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -2347,166 +2347,3 @@ def _select_atoms(q): assert np.allclose(rot_axes, np.eye(3)) assert np.allclose(rot_center, [1, 0, 0]) assert np.allclose(moi, [26, 38, 38]) - - -def get_residue_axes_branched(monkeypatch): - ax = AxesCalculator() - u = MagicMock() - u.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90]) - monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) - residue = u.select_atoms("resindex 5") - residue.__len__.return_value = 3 - u.atoms.residues.__len__.return_value = 4 - monkeypatch.setattr( - ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) - ) - - uas = _FakeAtomGroup( - [ - _atom(index=1, mass=12.0, pos=(1, 1, 1)), - _atom(index=2, mass=12.0, pos=(3, 3, 3)), - _atom(index=3, mass=16.0, pos=(4, 4, 4)), - ], - ) - - neighbours = _FakeAtomGroup( - [ - _atom(index=0, mass=12.0, pos=(0, 0, 0)), - _atom(index=4, mass=12.0, pos=(3, 3, 4)), - _atom(index=6, mass=16.0, pos=(4, 5, 4)), - ], - ) - neighbours[0].resindex = 4 - neighbours[1].resindex = 6 - neighbours[2].resindex = 8 - - def _select_atoms(q): - if q == "mass 2 to 999": - # return heavy atoms group - return uas - if q == "resindex 5": - return residue - if q.startswith("(not resindex 5)"): - return neighbours - if q == ("(bonded resindex 4) and (resindex 5)"): - return [uas[0]] - if q == ("(bonded resindex 6) and (resindex 5)"): - return [uas[1]] - if q == ("(bonded resindex 8) and (resindex 5)"): - return [uas[2]] - - u.select_atoms.side_effect = _select_atoms - u.atoms.select_atoms.side_effect = _select_atoms - residue.select_atoms.side_effect = residue - monkeypatch.setattr( - ax, "get_residue_custom_axes", lambda edges, center: 2 * np.eye(3), [2, 2, 2] - ) - monkeypatch.setattr( - ax, - "get_custom_residue_moment_of_inertia", - lambda center_of_mass, positions, masses, custom_rot_axes, dimensions: np.array( - [1, 1, 1] - ), - ) - - trans_axes, rot_axes, rot_center, moi = ax.get_residue_axes( - data_container=u, - index=5, - relative_index=0, - ) - - assert np.allclose(trans_axes, np.eye(3)) - assert np.allclose(rot_axes, 2 * np.eye(3)) - assert np.allclose(rot_center, [2, 2, 2]) - assert np.allclose(moi, [1, 1, 1]) - - -def get_ua_axes_branched(monkeypatch): - ax = AxesCalculator() - u = MagicMock() - u.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90]) - monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) - residue = u.select_atoms("resindex 5") - residue.__len__.return_value = 3 - u.atoms.residues.__len__.return_value = 4 - - heavy_atoms = _FakeAtomGroup( - [ - _atom(index=1, mass=12.0, pos=(1, 1, 1)), - _atom(index=2, mass=12.0, pos=(3, 3, 3)), - _atom(index=3, mass=16.0, pos=(4, 4, 4)), - ], - ) - - neighbours = _FakeAtomGroup( - [ - _atom(index=0, mass=12.0, pos=(0, 0, 0)), - _atom(index=4, mass=12.0, pos=(3, 3, 4)), - _atom(index=6, mass=16.0, pos=(4, 5, 4)), - ], - ) - neighbours[0].resindex = 4 - neighbours[1].resindex = 6 - neighbours[2].resindex = 8 - - def _select_atoms(q): - if q == "mass 2 to 999": - # return heavy atoms group - return heavy_atoms - if q == "resindex 5": - return residue - if q == "index 3": - return [heavy_atoms[2]] - if q == "not resindex 5": - return neighbours - if q == ("(bonded resindex 4) and (resindex 5)"): - return [heavy_atoms[0]] - if q == ("(bonded resindex 6) and (resindex 5)"): - return [heavy_atoms[1]] - if q == ("(bonded resindex 8) and (resindex 5)"): - return [heavy_atoms[2]] - - u.select_atoms.side_effect = _select_atoms - u.atoms.select_atoms.side_effect = _select_atoms - residue.select_atoms.side_effect = residue - monkeypatch.setattr( - ax, "get_residue_custom_axes", lambda edges, center: (2 * np.eye(3), [2, 2, 2]) - ) - monkeypatch.setattr( - ax, - "get_bonded_axes", - lambda system, atom, dimensions: (3 * np.eye(3), [12, 12, 12]), - ) - - trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=u, - index=2, - resindex=5, - ) - - assert np.allclose(trans_axes, 2 * np.eye(3)) - assert np.allclose(rot_axes, 3 * np.eye(3)) - assert np.allclose(rot_center, [4, 4, 4]) - assert np.allclose(moi, [12, 12, 12]) - - -def get_non_terminal_axes_branched(monkeypatch): - ax = AxesCalculator() - residue = MagicMock() - residue.__len__ = 4 - edges = _FakeAtomGroup( - [ - _atom(index=0, mass=12.0, pos=(0, 0, 0)), - _atom(index=2, mass=14.0, pos=(3, 0, 0)), - _atom(index=3, mass=12.0, pos=(1, 1, 0)), - ], - ) - monkeypatch.setattr( - ax, "get_residue_custom_axes", lambda edges, center: ([1, 0, 0], 3 * np.eye(3)) - ) - centre, axes = ax.get_non_terminal_axes( - residue=residue, edges=edges, dimensions=np.array([1, 1, 1]) - ) - - assert np.allclose(centre, [1, 0, 0]) - assert np.allclose(axes, 3 * np.eye(3)) From 237865422bf5bf23850795c63ad995b8dbf84d24 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 22:11:11 +0000 Subject: [PATCH 22/30] unit tests --- tests/unit/CodeEntropy/levels/test_axes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 9ee455a2..da119533 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -2346,4 +2346,4 @@ def _select_atoms(q): assert np.allclose(trans_axes, 2 * np.eye(3)) assert np.allclose(rot_axes, np.eye(3)) assert np.allclose(rot_center, [1, 0, 0]) - assert np.allclose(moi, [26, 38, 38]) + assert np.allclose(moi, [26, 38, 39]) From 0ed64d3655864c563ea7952a0b9a83ae8709c143 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 22:13:31 +0000 Subject: [PATCH 23/30] get_residue_axes for branched residues unit test --- tests/unit/CodeEntropy/levels/test_axes.py | 74 +++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index da119533..a6b32358 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -2346,4 +2346,76 @@ def _select_atoms(q): assert np.allclose(trans_axes, 2 * np.eye(3)) assert np.allclose(rot_axes, np.eye(3)) assert np.allclose(rot_center, [1, 0, 0]) - assert np.allclose(moi, [26, 38, 39]) + assert np.allclose(moi, [26, 38, 38]) + + +def get_residue_axes_branched(monkeypatch): + ax = AxesCalculator() + u = MagicMock() + u.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90]) + monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) + residue = u.select_atoms("resindex 5") + residue.__len__.return_value = 3 + u.atoms.residues.__len__.return_value = 4 + monkeypatch.setattr( + ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) + ) + + uas = _FakeAtomGroup( + [ + _atom(index=1, mass=12.0, pos=(1, 1, 1)), + _atom(index=2, mass=12.0, pos=(3, 3, 3)), + _atom(index=3, mass=16.0, pos=(4, 4, 4)), + ], + ) + + neighbours = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=4, mass=12.0, pos=(3, 3, 4)), + _atom(index=6, mass=16.0, pos=(4, 5, 4)), + ], + ) + neighbours[0].resindex = 4 + neighbours[1].resindex = 6 + neighbours[2].resindex = 8 + + def _select_atoms(q): + if q == "mass 2 to 999": + # return heavy atoms group + return uas + if q == "resindex 5": + return residue + if q.startswith("(not resindex 5)"): + return neighbours + if q == ("(bonded resindex 4) and (resindex 5)"): + return [uas[0]] + if q == ("(bonded resindex 6) and (resindex 5)"): + return [uas[1]] + if q == ("(bonded resindex 8) and (resindex 5)"): + return [uas[2]] + + u.select_atoms.side_effect = _select_atoms + u.atoms.select_atoms.side_effect = _select_atoms + residue.select_atoms.side_effect = residue + monkeypatch.setattr( + ax, "get_residue_custom_axes", lambda edges, center: 2 * np.eye(3), [2, 2, 2] + ) + monkeypatch.setattr( + ax, + "get_custom_residue_moment_of_inertia", + lambda center_of_mass, positions, masses, custom_rot_axes, dimensions: np.array( + [1, 1, 1] + ), + ) + + trans_axes, rot_axes, rot_center, moi = ax.get_residue_axes( + data_container=u, + index=5, + relative_index=0, + ) + + assert np.allclose(trans_axes, np.eye(3)) + assert np.allclose(rot_axes, 2 * np.eye(3)) + assert np.allclose(rot_center, [2, 2, 2]) + assert np.allclose(moi, [1, 1, 1]) From 385c4622d285a2b3b79c8856706af62cc9b41aef Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 22:14:52 +0000 Subject: [PATCH 24/30] get_residue_axes for branched residues unit test with correct name --- tests/unit/CodeEntropy/levels/test_axes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index a6b32358..b6f1b566 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -2349,7 +2349,7 @@ def _select_atoms(q): assert np.allclose(moi, [26, 38, 38]) -def get_residue_axes_branched(monkeypatch): +def test_get_residue_axes_branched(monkeypatch): ax = AxesCalculator() u = MagicMock() u.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90]) From 3dd380a920622fa38c536ccb1bbf00282aac6623 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 22:20:10 +0000 Subject: [PATCH 25/30] get_residue_axes for branched residues unit test with correct name --- tests/unit/CodeEntropy/levels/test_axes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index b6f1b566..87075b66 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -2399,7 +2399,7 @@ def _select_atoms(q): u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = residue monkeypatch.setattr( - ax, "get_residue_custom_axes", lambda edges, center: 2 * np.eye(3), [2, 2, 2] + ax, "get_residue_custom_axes", lambda edges, center: (2 * np.eye(3), [2, 2, 2]) ) monkeypatch.setattr( ax, From 33d69de2f5da0dd33aa653f230bd681439f98bbf Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 22:28:11 +0000 Subject: [PATCH 26/30] get_residue_axes for branched residues fixed get_residue_custom_axes return order --- tests/unit/CodeEntropy/levels/test_axes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 87075b66..6f0e7592 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -2399,7 +2399,7 @@ def _select_atoms(q): u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = residue monkeypatch.setattr( - ax, "get_residue_custom_axes", lambda edges, center: (2 * np.eye(3), [2, 2, 2]) + ax, "get_residue_custom_axes", lambda edges, center: ([2, 2, 2], 2 * np.eye(2)) ) monkeypatch.setattr( ax, From 8cdf1ffe2b1b1e3a58145df3b5a95cef821b3763 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 22:29:29 +0000 Subject: [PATCH 27/30] get_residue_axes for branched residues fixed get_residue_custom_axes return order --- tests/unit/CodeEntropy/levels/test_axes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 6f0e7592..6aca17ee 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -2399,7 +2399,7 @@ def _select_atoms(q): u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = residue monkeypatch.setattr( - ax, "get_residue_custom_axes", lambda edges, center: ([2, 2, 2], 2 * np.eye(2)) + ax, "get_residue_custom_axes", lambda edges, center: ([2, 2, 2], 2 * np.eye(3)) ) monkeypatch.setattr( ax, From d1bf09c3588bc3f8b0482c22d09b917815d1a3e0 Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 22:32:05 +0000 Subject: [PATCH 28/30] test get_ua_axes for branched residues fixed --- tests/unit/CodeEntropy/levels/test_axes.py | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 6aca17ee..6c2ed32c 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -2419,3 +2419,72 @@ def _select_atoms(q): assert np.allclose(rot_axes, 2 * np.eye(3)) assert np.allclose(rot_center, [2, 2, 2]) assert np.allclose(moi, [1, 1, 1]) + + +def test_get_ua_axes_branched(monkeypatch): + ax = AxesCalculator() + u = MagicMock() + u.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90]) + monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) + residue = u.select_atoms("resindex 5") + residue.__len__.return_value = 3 + u.atoms.residues.__len__.return_value = 4 + + heavy_atoms = _FakeAtomGroup( + [ + _atom(index=1, mass=12.0, pos=(1, 1, 1)), + _atom(index=2, mass=12.0, pos=(3, 3, 3)), + _atom(index=3, mass=16.0, pos=(4, 4, 4)), + ], + ) + + neighbours = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=4, mass=12.0, pos=(3, 3, 4)), + _atom(index=6, mass=16.0, pos=(4, 5, 4)), + ], + ) + neighbours[0].resindex = 4 + neighbours[1].resindex = 6 + neighbours[2].resindex = 8 + + def _select_atoms(q): + if q == "mass 2 to 999": + # return heavy atoms group + return heavy_atoms + if q == "resindex 5": + return residue + if q == "index 3": + return [heavy_atoms[2]] + if q == "not resindex 5": + return neighbours + if q == ("(bonded resindex 4) and (resindex 5)"): + return [heavy_atoms[0]] + if q == ("(bonded resindex 6) and (resindex 5)"): + return [heavy_atoms[1]] + if q == ("(bonded resindex 8) and (resindex 5)"): + return [heavy_atoms[2]] + + u.select_atoms.side_effect = _select_atoms + u.atoms.select_atoms.side_effect = _select_atoms + residue.select_atoms.side_effect = residue + monkeypatch.setattr( + ax, "get_residue_custom_axes", lambda edges, center: ([2, 2, 2], 2 * np.eye(3)) + ) + monkeypatch.setattr( + ax, + "get_bonded_axes", + lambda system, atom, dimensions: (3 * np.eye(3), [12, 12, 12]), + ) + + trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( + data_container=u, + index=2, + resindex=5, + ) + + assert np.allclose(trans_axes, 2 * np.eye(3)) + assert np.allclose(rot_axes, 3 * np.eye(3)) + assert np.allclose(rot_center, [4, 4, 4]) + assert np.allclose(moi, [12, 12, 12]) From 04a9b35b4c257b6809cbf01a50a6a33f2d00dd0c Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 22:35:40 +0000 Subject: [PATCH 29/30] test get_ua_axes for branched residues fixed --- tests/unit/CodeEntropy/levels/test_axes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 6c2ed32c..41c986df 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -2468,7 +2468,8 @@ def _select_atoms(q): u.select_atoms.side_effect = _select_atoms u.atoms.select_atoms.side_effect = _select_atoms - residue.select_atoms.side_effect = residue + residue.select_atoms.side_effect = _select_atoms + residue.atoms.select_atoms.side_effect = _select_atoms monkeypatch.setattr( ax, "get_residue_custom_axes", lambda edges, center: ([2, 2, 2], 2 * np.eye(3)) ) From 20d87ca72fcf8ffa7af58d9eb426fd01ac28843f Mon Sep 17 00:00:00 2001 From: Ioana Papa Date: Fri, 27 Nov 2026 22:38:38 +0000 Subject: [PATCH 30/30] test get_non_terminal_axes for residue with 3 edges --- tests/unit/CodeEntropy/levels/test_axes.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index 41c986df..b0acf066 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -2489,3 +2489,25 @@ def _select_atoms(q): assert np.allclose(rot_axes, 3 * np.eye(3)) assert np.allclose(rot_center, [4, 4, 4]) assert np.allclose(moi, [12, 12, 12]) + + +def test_get_non_terminal_axes_branched(monkeypatch): + ax = AxesCalculator() + residue = MagicMock() + residue.__len__ = 4 + edges = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=2, mass=14.0, pos=(3, 0, 0)), + _atom(index=3, mass=12.0, pos=(1, 1, 0)), + ], + ) + monkeypatch.setattr( + ax, "get_residue_custom_axes", lambda edges, center: ([1, 0, 0], 3 * np.eye(3)) + ) + centre, axes = ax.get_non_terminal_axes( + residue=residue, edges=edges, dimensions=np.array([1, 1, 1]) + ) + + assert np.allclose(centre, [1, 0, 0]) + assert np.allclose(axes, 3 * np.eye(3))