Skip to content

Commit 4142f1f

Browse files
committed
tidy tests within CodeEntropy/tests/unit/entropy
1 parent 9481193 commit 4142f1f

11 files changed

Lines changed: 643 additions & 670 deletions

tests/unit/CodeEntropy/entropy/test_aggregate_node.py renamed to tests/unit/CodeEntropy/entropy/nodes/test_aggregate_node.py

File renamed without changes.

tests/unit/CodeEntropy/entropy/test_configurational_node_missing_branches.py renamed to tests/unit/CodeEntropy/entropy/nodes/test_configurational_node.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
from unittest.mock import MagicMock
22

33
import numpy as np
4+
import pytest
45

56
from CodeEntropy.entropy.nodes.configurational import ConfigurationalEntropyNode
67

78

9+
def test_config_node_raises_if_frame_count_missing():
10+
node = ConfigurationalEntropyNode()
11+
with pytest.raises(KeyError):
12+
node._get_n_frames({})
13+
14+
15+
def test_config_node_run_writes_results(shared_data):
16+
node = ConfigurationalEntropyNode()
17+
18+
shared_data["conformational_states"] = {
19+
"ua": {(0, 0): [0, 0, 1, 1]},
20+
"res": {0: [0, 1, 1, 1]},
21+
}
22+
23+
shared_data["levels"] = {0: ["united_atom", "residue"]}
24+
shared_data["groups"] = {0: [0]}
25+
26+
out = node.run(shared_data)
27+
28+
assert "configurational_entropy" in out
29+
assert "configurational_entropy" in shared_data
30+
assert 0 in shared_data["configurational_entropy"]
31+
32+
833
def test_run_skips_empty_mol_ids_group():
934
node = ConfigurationalEntropyNode()
1035

tests/unit/CodeEntropy/entropy/test_vibrational_node_branches.py renamed to tests/unit/CodeEntropy/entropy/nodes/test_vibrational_node.py

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ def shared_data_base():
2727
}
2828

2929

30+
@pytest.fixture()
31+
def shared_groups():
32+
frag = MagicMock()
33+
frag.residues = [MagicMock(resname="RES")]
34+
ru = MagicMock()
35+
ru.atoms.fragments = [frag]
36+
37+
return {
38+
"run_manager": MagicMock(),
39+
"args": SimpleNamespace(temperature=298.0, combined_forcetorque=False),
40+
"groups": {5: [0]},
41+
"levels": {0: ["united_atom"]},
42+
"reduced_universe": ru,
43+
"force_covariances": {"ua": {}, "res": [], "poly": []},
44+
"torque_covariances": {"ua": {}, "res": [], "poly": []},
45+
"n_frames": 5,
46+
"reporter": MagicMock(),
47+
}
48+
49+
3050
def test_united_atom_branch_logs_and_stores(shared_data_base, monkeypatch):
3151
node = VibrationalEntropyNode()
3252

@@ -172,3 +192,175 @@ def test_polymer_branch_executes(shared_data, monkeypatch):
172192
assert "vibrational_entropy" in out
173193
assert out["vibrational_entropy"][0]["polymer"]["trans"] == 1.0
174194
assert out["vibrational_entropy"][0]["polymer"]["rot"] == 1.0
195+
196+
197+
def test_run_skips_empty_mol_ids_group():
198+
node = VibrationalEntropyNode()
199+
200+
shared_groups = {
201+
"run_manager": MagicMock(),
202+
"args": SimpleNamespace(temperature=298.0, combined_forcetorque=False),
203+
"groups": {0: []},
204+
"levels": {0: ["united_atom"]},
205+
"reduced_universe": MagicMock(atoms=MagicMock(fragments=[])),
206+
"force_covariances": {"ua": {}, "res": [], "poly": []},
207+
"torque_covariances": {"ua": {}, "res": [], "poly": []},
208+
"n_frames": 5,
209+
"reporter": None,
210+
}
211+
212+
out = node.run(shared_groups)
213+
assert "vibrational_entropy" in out
214+
assert out["vibrational_entropy"][0] == {}
215+
216+
217+
def test_get_ua_frame_counts_falls_back_to_empty_when_shape_wrong():
218+
node = VibrationalEntropyNode()
219+
assert node._get_ua_frame_counts({"frame_counts": "not-a-dict"}) == {}
220+
221+
222+
def test_compute_united_atom_entropy_logs_residue_data_when_reporter_present():
223+
node = VibrationalEntropyNode()
224+
ve = MagicMock()
225+
226+
node._compute_force_torque_entropy = MagicMock(return_value=EntropyPair(1.0, 2.0))
227+
228+
reporter = MagicMock()
229+
residues = [SimpleNamespace(resname="A"), SimpleNamespace(resname="B")]
230+
231+
out = node._compute_united_atom_entropy(
232+
ve=ve,
233+
temp=298.0,
234+
group_id=7,
235+
residues=residues,
236+
force_ua={},
237+
torque_ua={},
238+
ua_frame_counts={(7, 0): 3, (7, 1): 4},
239+
reporter=reporter,
240+
n_frames_default=10,
241+
highest=True,
242+
)
243+
244+
assert out == EntropyPair(trans=2.0, rot=4.0)
245+
assert reporter.add_residue_data.call_count == 4
246+
247+
248+
def test_compute_force_torque_entropy_success_calls_vibrational_engine():
249+
node = VibrationalEntropyNode()
250+
ve = MagicMock()
251+
ve.vibrational_entropy_calculation.side_effect = [10.0, 20.0]
252+
253+
out = node._compute_force_torque_entropy(
254+
ve=ve,
255+
temp=298.0,
256+
fmat=np.eye(3),
257+
tmat=np.eye(3),
258+
highest=False,
259+
)
260+
261+
assert out == EntropyPair(trans=10.0, rot=20.0)
262+
assert ve.vibrational_entropy_calculation.call_count == 2
263+
264+
265+
def test_compute_ft_entropy_success_calls_vibrational_engine_for_trans_and_rot():
266+
node = VibrationalEntropyNode()
267+
ve = MagicMock()
268+
ve.vibrational_entropy_calculation.side_effect = [1.5, 2.5]
269+
270+
out = node._compute_ft_entropy(ve=ve, temp=298.0, ftmat=np.eye(6))
271+
272+
assert out == EntropyPair(trans=1.5, rot=2.5)
273+
assert ve.vibrational_entropy_calculation.call_count == 2
274+
275+
276+
def test_log_molecule_level_results_returns_when_reporter_none():
277+
VibrationalEntropyNode._log_molecule_level_results(
278+
reporter=None,
279+
group_id=1,
280+
level="residue",
281+
pair=EntropyPair(1.0, 2.0),
282+
use_ft_labels=False,
283+
)
284+
285+
286+
def test_log_molecule_level_results_writes_trans_and_rot_labels():
287+
reporter = MagicMock()
288+
VibrationalEntropyNode._log_molecule_level_results(
289+
reporter=reporter,
290+
group_id=1,
291+
level="residue",
292+
pair=EntropyPair(3.0, 4.0),
293+
use_ft_labels=False,
294+
)
295+
296+
reporter.add_results_data.assert_any_call(1, "residue", "Transvibrational", 3.0)
297+
reporter.add_results_data.assert_any_call(1, "residue", "Rovibrational", 4.0)
298+
299+
300+
def test_get_group_id_to_index_builds_from_groups(shared_groups):
301+
node = VibrationalEntropyNode()
302+
gid2i = node._get_group_id_to_index(shared_groups)
303+
assert gid2i == {5: 0}
304+
305+
306+
def test_get_ua_frame_counts_returns_empty_when_missing(shared_groups):
307+
node = VibrationalEntropyNode()
308+
assert node._get_ua_frame_counts(shared_groups) == {}
309+
310+
311+
def test_compute_force_torque_entropy_returns_zero_when_missing_matrix(shared_groups):
312+
node = VibrationalEntropyNode()
313+
ve = MagicMock()
314+
pair = node._compute_force_torque_entropy(
315+
ve=ve, temp=298.0, fmat=None, tmat=np.eye(3), highest=True
316+
)
317+
assert pair == EntropyPair(trans=0.0, rot=0.0)
318+
319+
320+
def test_compute_force_torque_entropy_returns_zero_when_filter_removes_all(monkeypatch):
321+
node = VibrationalEntropyNode()
322+
ve = MagicMock()
323+
324+
monkeypatch.setattr(
325+
node._mat_ops, "filter_zero_rows_columns", lambda a, atol: np.array([])
326+
)
327+
328+
pair = node._compute_force_torque_entropy(
329+
ve=ve, temp=298.0, fmat=np.eye(3), tmat=np.eye(3), highest=True
330+
)
331+
assert pair == EntropyPair(trans=0.0, rot=0.0)
332+
333+
334+
def test_compute_ft_entropy_returns_zero_when_none():
335+
node = VibrationalEntropyNode()
336+
ve = MagicMock()
337+
assert node._compute_ft_entropy(ve=ve, temp=298.0, ftmat=None) == EntropyPair(
338+
trans=0.0, rot=0.0
339+
)
340+
341+
342+
def test_log_molecule_level_results_ft_labels_branch():
343+
node = VibrationalEntropyNode()
344+
reporter = MagicMock()
345+
346+
node._log_molecule_level_results(
347+
reporter, 1, "residue", EntropyPair(1.0, 2.0), use_ft_labels=True
348+
)
349+
350+
reporter.add_results_data.assert_any_call(
351+
1, "residue", "FTmat-Transvibrational", 1.0
352+
)
353+
reporter.add_results_data.assert_any_call(1, "residue", "FTmat-Rovibrational", 2.0)
354+
355+
356+
def test_get_indexed_matrix_out_of_range_returns_none():
357+
node = VibrationalEntropyNode()
358+
assert node._get_indexed_matrix([np.eye(3)], 5) is None
359+
360+
361+
def test_run_unknown_level_raises(shared_groups):
362+
node = VibrationalEntropyNode()
363+
shared_groups["levels"] = {0: ["nope"]}
364+
365+
with pytest.raises(ValueError):
366+
node.run(shared_groups)

‎tests/unit/CodeEntropy/entropy/test_configurational_node.py‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎tests/unit/CodeEntropy/entropy/test_vibrational_node_missing_branches.py‎

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)