Skip to content

Commit 9fb4198

Browse files
committed
tests
1 parent 92a65cf commit 9fb4198

7 files changed

Lines changed: 43 additions & 23 deletions

File tree

CodeEntropy/entropy/nodes/vibrational.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def run(self, shared_data: MutableMapping[str, Any], **_: Any) -> Dict[str, Any]
109109
residues=rep_mol.residues,
110110
force_ua=force_cov["ua"],
111111
torque_ua=torque_cov["ua"],
112-
flexible=flexible["ua"],
112+
flexible_ua=flexible["ua"],
113113
ua_frame_counts=ua_frame_counts,
114114
reporter=reporter,
115115
n_frames_default=shared_data.get("n_frames", 0),
@@ -224,7 +224,7 @@ def _compute_united_atom_entropy(
224224
residues: Any,
225225
force_ua: Mapping[CovKey, Any],
226226
torque_ua: Mapping[CovKey, Any],
227-
flexible: Any,
227+
flexible_ua: Any,
228228
ua_frame_counts: Mapping[CovKey, int],
229229
reporter: Optional[Any],
230230
n_frames_default: int,
@@ -259,7 +259,7 @@ def _compute_united_atom_entropy(
259259
key = (group_id, res_id)
260260
fmat = force_ua.get(key)
261261
tmat = torque_ua.get(key)
262-
flexible = flexible[key]
262+
flexible = flexible_ua.get(key)
263263

264264
pair = self._compute_force_torque_entropy(
265265
ve=ve,

CodeEntropy/entropy/vibrational.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,12 @@ def _entropy_components(
123123
Array of entropy components (J/mol/K) for each valid mode.
124124
"""
125125
lambdas = self._matrix_eigenvalues(matrix)
126+
logger.debug("lambdas: %s", lambdas)
126127
lambdas = self._convert_lambda_units(lambdas)
128+
logger.debug("lambdas converted units: %s", lambdas)
127129
if matrix_type == "force" and flexible > 0:
128130
lambdas = self._flexible_dihedral(lambdas, flexible)
131+
logger.debug("lambdas flexible halved: %s", lambdas)
129132

130133
freqs = self._frequencies_from_lambdas(lambdas, temp)
131134
if freqs.size == 0:

CodeEntropy/levels/dihedrals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def build_conformational_states(
8282
states_ua: Dict[UAKey, List[str]] = {}
8383
states_res: List[List[str]] = [None] * number_groups
8484
flexible_ua: Dict[UAKey, int] = {}
85-
flexible_res: List[int] = [None] * number_groups
85+
flexible_res: List[int] = [0] * number_groups
8686

8787
task = None
8888
if progress is not None:

tests/unit/CodeEntropy/entropy/nodes/test_vibrational_node.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def shared_data_base():
2222
"reduced_universe": reduced_universe,
2323
"force_covariances": {"ua": {}, "res": [], "poly": []},
2424
"torque_covariances": {"ua": {}, "res": [], "poly": []},
25+
"flexible_dihedrals": {"ua": 0, "res": [0]},
2526
"n_frames": 5,
2627
"reporter": MagicMock(),
2728
}
@@ -42,6 +43,7 @@ def shared_groups():
4243
"reduced_universe": ru,
4344
"force_covariances": {"ua": {}, "res": [], "poly": []},
4445
"torque_covariances": {"ua": {}, "res": [], "poly": []},
46+
"flexible_dihedrals": {"ua": [], "res": []},
4547
"n_frames": 5,
4648
"reporter": MagicMock(),
4749
}
@@ -131,6 +133,7 @@ def test_run_raises_on_unknown_level(shared_data, monkeypatch):
131133

132134
shared_data["force_covariances"] = {"ua": {}, "res": [], "poly": []}
133135
shared_data["torque_covariances"] = {"ua": {}, "res": [], "poly": []}
136+
shared_data["flexible_dihedrals"] = {"ua": [], "res": []}
134137

135138
with pytest.raises(ValueError):
136139
node.run(shared_data)
@@ -143,6 +146,7 @@ def test_run_united_atom_branch_stores_results(shared_data, monkeypatch):
143146
shared_data["groups"] = {0: [0]}
144147
shared_data["force_covariances"] = {"ua": {}, "res": [], "poly": []}
145148
shared_data["torque_covariances"] = {"ua": {}, "res": [], "poly": []}
149+
shared_data["flexible_dihedrals"] = {"ua": [], "res": []}
146150

147151
fake_pair = MagicMock(trans=1.0, rot=2.0)
148152
monkeypatch.setattr(
@@ -164,6 +168,7 @@ def test_unknown_level_raises(shared_data):
164168
shared_data["groups"] = {0: [0]}
165169
shared_data["force_covariances"] = {"ua": {}, "res": [], "poly": []}
166170
shared_data["torque_covariances"] = {"ua": {}, "res": [], "poly": []}
171+
shared_data["flexible_dihedrals"] = {"ua": [], "res": []}
167172

168173
with pytest.raises(ValueError):
169174
node.run(shared_data)
@@ -177,6 +182,7 @@ def test_polymer_branch_executes(shared_data, monkeypatch):
177182

178183
shared_data["force_covariances"] = {"ua": {}, "res": [], "poly": [MagicMock()]}
179184
shared_data["torque_covariances"] = {"ua": {}, "res": [], "poly": [MagicMock()]}
185+
shared_data["flexible_dihedrals"] = {"ua": [], "res": []}
180186

181187
shared_data["reduced_universe"].atoms.fragments = [MagicMock(residues=[])]
182188

@@ -205,6 +211,7 @@ def test_run_skips_empty_mol_ids_group():
205211
"reduced_universe": MagicMock(atoms=MagicMock(fragments=[])),
206212
"force_covariances": {"ua": {}, "res": [], "poly": []},
207213
"torque_covariances": {"ua": {}, "res": [], "poly": []},
214+
"flexible_dihedrals": {"ua": [], "res": []},
208215
"n_frames": 5,
209216
"reporter": None,
210217
}
@@ -235,6 +242,7 @@ def test_compute_united_atom_entropy_logs_residue_data_when_reporter_present():
235242
residues=residues,
236243
force_ua={},
237244
torque_ua={},
245+
flexible_ua={(7, 0): 0, (7, 1): 0},
238246
ua_frame_counts={(7, 0): 3, (7, 1): 4},
239247
reporter=reporter,
240248
n_frames_default=10,
@@ -255,6 +263,7 @@ def test_compute_force_torque_entropy_success_calls_vibrational_engine():
255263
temp=298.0,
256264
fmat=np.eye(3),
257265
tmat=np.eye(3),
266+
flexible=0,
258267
highest=False,
259268
)
260269

@@ -267,7 +276,7 @@ def test_compute_ft_entropy_success_calls_vibrational_engine_for_trans_and_rot()
267276
ve = MagicMock()
268277
ve.vibrational_entropy_calculation.side_effect = [1.5, 2.5]
269278

270-
out = node._compute_ft_entropy(ve=ve, temp=298.0, ftmat=np.eye(6))
279+
out = node._compute_ft_entropy(ve=ve, temp=298.0, ftmat=np.eye(6), flexible=0)
271280

272281
assert out == EntropyPair(trans=1.5, rot=2.5)
273282
assert ve.vibrational_entropy_calculation.call_count == 2
@@ -312,7 +321,7 @@ def test_compute_force_torque_entropy_returns_zero_when_missing_matrix(shared_gr
312321
node = VibrationalEntropyNode()
313322
ve = MagicMock()
314323
pair = node._compute_force_torque_entropy(
315-
ve=ve, temp=298.0, fmat=None, tmat=np.eye(3), highest=True
324+
ve=ve, temp=298.0, fmat=None, tmat=np.eye(3), flexible=0, highest=True
316325
)
317326
assert pair == EntropyPair(trans=0.0, rot=0.0)
318327

@@ -326,17 +335,17 @@ def test_compute_force_torque_entropy_returns_zero_when_filter_removes_all(monke
326335
)
327336

328337
pair = node._compute_force_torque_entropy(
329-
ve=ve, temp=298.0, fmat=np.eye(3), tmat=np.eye(3), highest=True
338+
ve=ve, temp=298.0, fmat=np.eye(3), tmat=np.eye(3), flexible=0, highest=True
330339
)
331340
assert pair == EntropyPair(trans=0.0, rot=0.0)
332341

333342

334343
def test_compute_ft_entropy_returns_zero_when_none():
335344
node = VibrationalEntropyNode()
336345
ve = MagicMock()
337-
assert node._compute_ft_entropy(ve=ve, temp=298.0, ftmat=None) == EntropyPair(
338-
trans=0.0, rot=0.0
339-
)
346+
assert node._compute_ft_entropy(
347+
ve=ve, temp=298.0, ftmat=None, flexible=0
348+
) == EntropyPair(trans=0.0, rot=0.0)
340349

341350

342351
def test_log_molecule_level_results_ft_labels_branch():
@@ -376,7 +385,7 @@ def test_compute_ft_entropy_returns_zeros_when_filtered_ft_matrix_is_empty(monke
376385
lambda _arr, atol: np.empty((0, 0), dtype=float),
377386
)
378387

379-
out = node._compute_ft_entropy(ve=ve, temp=298.0, ftmat=np.eye(6))
388+
out = node._compute_ft_entropy(ve=ve, temp=298.0, ftmat=np.eye(6), flexible=0)
380389

381390
assert out == EntropyPair(trans=0.0, rot=0.0)
382391
ve.vibrational_entropy_calculation.assert_not_called()

tests/unit/CodeEntropy/entropy/test_vibrational_entropy_math.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_entropy_components_returns_empty_when_all_invalid(run_manager):
4747
ve = VibrationalEntropy(run_manager=run_manager)
4848

4949
ve._matrix_eigenvalues = lambda m: np.array([-1.0, 0.0, 0.0])
50-
comps = ve._entropy_components(np.eye(3), temp=298.0)
50+
comps = ve._entropy_components(np.eye(3), temp=298.0, matrix_type="", flexible=0)
5151

5252
assert comps.size == 0
5353

@@ -154,7 +154,7 @@ def test_vibrational_entropy_calculation_end_to_end_returns_float(
154154
)
155155

156156
out = ve.vibrational_entropy_calculation(
157-
np.eye(3), matrix_type="torque", temp=298.0, highest_level=False
157+
np.eye(3), matrix_type="torque", temp=298.0, highest_level=False, flexible=0
158158
)
159159

160160
assert isinstance(out, float)

tests/unit/CodeEntropy/levels/nodes/test_conformations_node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def test_compute_conformational_states_node_runs_and_writes_shared_data():
99
node = ComputeConformationalStatesNode(universe_operations=uops)
1010

1111
node._dihedral_analysis.build_conformational_states = MagicMock(
12-
return_value=({"ua_key": ["0", "1"]}, [["00", "01"]])
12+
return_value=({"ua_key": ["0", "1"]}, [["00", "01"]], {"ua_key": [0]}, [0])
1313
)
1414

1515
shared = {

tests/unit/CodeEntropy/levels/test_dihedrals.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def run(self):
209209
return SimpleNamespace(results=SimpleNamespace(angles=angles))
210210

211211
with patch("CodeEntropy.levels.dihedrals.Dihedral", _FakeDihedral):
212-
states = dt._assign_states(
212+
states, num_flexible = dt._assign_states(
213213
data_container=MagicMock(),
214214
molecules=[0, 1],
215215
dihedrals=["D0"],
@@ -227,8 +227,10 @@ def test_assign_states_for_group_sets_empty_lists_and_delegates_for_nonempty():
227227

228228
states_ua = {}
229229
states_res = [None, None]
230+
flexible_ua = {}
231+
flexible_res = [None, None]
230232

231-
with patch.object(dt, "_assign_states", return_value=["x"]) as assign_spy:
233+
with patch.object(dt, "_assign_states", return_value=[["x"], 0]) as assign_spy:
232234
dt._assign_states_for_group(
233235
data_container=MagicMock(),
234236
group_id=1,
@@ -243,6 +245,8 @@ def test_assign_states_for_group_sets_empty_lists_and_delegates_for_nonempty():
243245
level_list=["united_atom", "residue"],
244246
states_ua=states_ua,
245247
states_res=states_res,
248+
flexible_ua=flexible_ua,
249+
flexible_res=flexible_res,
246250
)
247251

248252
assert states_ua[(1, 0)] == []
@@ -264,7 +268,7 @@ def test_build_conformational_states_runs_group_and_skips_empty_group(monkeypatc
264268
monkeypatch.setattr(dt, "_collect_peaks_for_group", lambda **kw: ([], []))
265269
monkeypatch.setattr(dt, "_assign_states_for_group", lambda **kw: None)
266270

267-
states_ua, states_res = dt.build_conformational_states(
271+
states_ua, states_res, flex_ua, flex_res = dt.build_conformational_states(
268272
data_container=MagicMock(),
269273
levels=levels,
270274
groups=groups,
@@ -337,7 +341,7 @@ def run(self):
337341
return SimpleNamespace(results=SimpleNamespace(angles=[]))
338342

339343
with patch("CodeEntropy.levels.dihedrals.Dihedral", _FakeDihedral):
340-
out = dt._assign_states(
344+
out_state, out_flex = dt._assign_states(
341345
data_container=MagicMock(),
342346
molecules=[0],
343347
dihedrals=[],
@@ -347,7 +351,7 @@ def run(self):
347351
step=1,
348352
)
349353

350-
assert out == []
354+
assert out_state == []
351355

352356

353357
def test_identify_peaks_multiple_molecules_real_histogram():
@@ -420,8 +424,10 @@ def test_assign_states_for_group_residue_nonempty_calls_assign_states():
420424

421425
states_ua = {}
422426
states_res = [None, None]
427+
flexible_ua = {}
428+
flexible_res = [None, None]
423429

424-
with patch.object(dt, "_assign_states", return_value=["A"]) as spy:
430+
with patch.object(dt, "_assign_states", return_value=[["A"], 0]) as spy:
425431
dt._assign_states_for_group(
426432
data_container=MagicMock(),
427433
group_id=1,
@@ -436,6 +442,8 @@ def test_assign_states_for_group_residue_nonempty_calls_assign_states():
436442
level_list=["residue"],
437443
states_ua=states_ua,
438444
states_res=states_res,
445+
flexible_ua=flexible_ua,
446+
flexible_res=flexible_res,
439447
)
440448

441449
assert states_res[1] == ["A"]
@@ -463,7 +471,7 @@ def run(self):
463471
return SimpleNamespace(results=SimpleNamespace(angles=angles))
464472

465473
with patch("CodeEntropy.levels.dihedrals.Dihedral", _FakeDihedral):
466-
states = dt._assign_states(
474+
states, num_flex = dt._assign_states(
467475
data_container=MagicMock(),
468476
molecules=[0, 1],
469477
dihedrals=["D0"],
@@ -523,7 +531,7 @@ def run(self):
523531
return SimpleNamespace(results=SimpleNamespace(angles=angles))
524532

525533
with patch("CodeEntropy.levels.dihedrals.Dihedral", _FakeDihedral):
526-
states = dt._assign_states(
534+
states, num_flex = dt._assign_states(
527535
data_container=MagicMock(),
528536
molecules=[0],
529537
dihedrals=["D0"],
@@ -571,7 +579,7 @@ def test_build_conformational_states_with_progress_skips_empty_molecule_group():
571579
groups = {0: []}
572580
levels = {}
573581

574-
states_ua, states_res = dt.build_conformational_states(
582+
states_ua, states_res, flex_ua, flex_res = dt.build_conformational_states(
575583
data_container=MagicMock(),
576584
levels=levels,
577585
groups=groups,

0 commit comments

Comments
 (0)