Skip to content

Commit 89f1b81

Browse files
committed
fix and update unit tests to reflect changes made within this refactor
1 parent 164178e commit 89f1b81

3 files changed

Lines changed: 85 additions & 14 deletions

File tree

tests/test_CodeEntropy/test_entropy.py

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ def test_frequency_calculation_0(self):
805805
806806
Ensures that the method returns 0 when the input eigenvalue (lambda) is zero.
807807
"""
808-
lambdas = 0
808+
lambdas = [0]
809809
temp = 298
810810

811811
run_manager = RunManager("mock_folder")
@@ -815,7 +815,7 @@ def test_frequency_calculation_0(self):
815815
)
816816
frequencies = ve.frequency_calculation(lambdas, temp)
817817

818-
assert frequencies == 0
818+
assert np.allclose(frequencies, [0.0])
819819

820820
def test_frequency_calculation_positive(self):
821821
"""
@@ -842,30 +842,75 @@ def test_frequency_calculation_positive(self):
842842
[1899594266400.4016, 2013894687315.6213, 2195940987139.7097]
843843
)
844844

845-
def test_frequency_calculation_negative(self):
845+
def test_frequency_calculation_filters_invalid(self):
846846
"""
847-
Test `frequency_calculation` with a negative eigenvalue.
847+
Test `frequency_calculation` filters out invalid eigenvalues.
848848
849-
Ensures that the method raises a `ValueError` when any eigenvalue is negative,
850-
as this is physically invalid for frequency calculations.
849+
Ensures that negative, complex, and near-zero eigenvalues are excluded,
850+
and frequencies are calculated only for valid ones.
851851
"""
852-
lambdas = np.array([585495.0917897299, -658074.5130064893, 782425.305888707])
852+
lambdas = np.array(
853+
[585495.0917897299, -658074.5130064893, 0.0, 782425.305888707]
854+
)
853855
temp = 298
854856

855857
# Create a mock RunManager and set return value for get_KT2J
856-
run_manager = RunManager("temp_folder")
857-
run_manager.get_KT2J
858+
run_manager = MagicMock()
859+
run_manager.get_KT2J.return_value = 2.479e-21 # example value in Joules
858860

859861
# Instantiate VibrationalEntropy with mocks
860862
ve = VibrationalEntropy(
861863
run_manager, MagicMock(), MagicMock(), MagicMock(), MagicMock(), MagicMock()
862864
)
863865

864-
# Assert that ValueError is raised due to negative eigenvalue
865-
with self.assertRaises(ValueError) as context:
866-
ve.frequency_calculation(lambdas, temp)
866+
# Call the method
867+
frequencies = ve.frequency_calculation(lambdas, temp)
868+
869+
# Expected: only two valid eigenvalues used
870+
expected_lambdas = np.array([585495.0917897299, 782425.305888707])
871+
expected_frequencies = (
872+
1
873+
/ (2 * np.pi)
874+
* np.sqrt(expected_lambdas / run_manager.get_KT2J.return_value)
875+
)
876+
877+
# Assert frequencies match expected
878+
np.testing.assert_allclose(frequencies, expected_frequencies, rtol=1e-5)
879+
880+
def test_frequency_calculation_filters_invalid_with_warning(self):
881+
"""
882+
Test `frequency_calculation` filters out invalid eigenvalues and logs a warning.
867883
868-
self.assertIn("Negative eigenvalues", str(context.exception))
884+
Ensures that negative, complex, and near-zero eigenvalues are excluded,
885+
and a warning is logged about the exclusions.
886+
"""
887+
lambdas = np.array(
888+
[585495.0917897299, -658074.5130064893, 0.0, 782425.305888707]
889+
)
890+
temp = 298
891+
892+
run_manager = MagicMock()
893+
run_manager.get_KT2J.return_value = 2.479e-21 # example value
894+
895+
ve = VibrationalEntropy(
896+
run_manager, MagicMock(), MagicMock(), MagicMock(), MagicMock(), MagicMock()
897+
)
898+
899+
with self.assertLogs("CodeEntropy.entropy", level="WARNING") as cm:
900+
frequencies = ve.frequency_calculation(lambdas, temp)
901+
902+
# Check that warning was logged
903+
warning_messages = "\n".join(cm.output)
904+
self.assertIn("invalid eigenvalues excluded", warning_messages)
905+
906+
# Check that only valid frequencies are returned
907+
expected_lambdas = np.array([585495.0917897299, 782425.305888707])
908+
expected_frequencies = (
909+
1
910+
/ (2 * np.pi)
911+
* np.sqrt(expected_lambdas / run_manager.get_KT2J.return_value)
912+
)
913+
np.testing.assert_allclose(frequencies, expected_frequencies, rtol=1e-5)
869914

870915
def test_vibrational_entropy_calculation_force_not_highest(self):
871916
"""

tests/test_CodeEntropy/test_levels.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,32 @@ def test_get_dihedrals_no_residue(self):
257257
# Should result in no resdies
258258
self.assertEqual(result, [])
259259

260+
def test_compute_dihedral_conformations_no_dihedrals(self):
261+
"""
262+
Test `compute_dihedral_conformations` when no dihedrals are found.
263+
Ensures it returns an empty list of states.
264+
"""
265+
level_manager = LevelManager()
266+
267+
# Mock get_dihedrals to return an empty list
268+
level_manager.get_dihedrals = MagicMock(return_value=[])
269+
270+
selector = MagicMock()
271+
272+
result = level_manager.compute_dihedral_conformations(
273+
selector=selector,
274+
level="united_atom",
275+
number_frames=10,
276+
bin_width=10.0,
277+
start=0,
278+
end=10,
279+
step=1,
280+
ce=MagicMock(),
281+
)
282+
283+
# Match current behavior: returns only states (empty list)
284+
self.assertEqual(result, [])
285+
260286
def test_get_beads_polymer_level(self):
261287
"""
262288
Test `get_beads` for 'polymer' level.

tests/test_CodeEntropy/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_main_entry_point_runs(self):
105105

106106
config_path = os.path.join(self.test_dir, "config.yaml")
107107
with open(config_path, "w") as f:
108-
f.write("run1:\n" " selection_string: resid 1\n")
108+
f.write("run1:\n" " end: 60\n" " selection_string: resid 1\n")
109109

110110
result = subprocess.run(
111111
[

0 commit comments

Comments
 (0)