diff --git a/CodeEntropy/config/argparse.py b/CodeEntropy/config/argparse.py index 6b5161b9..a3492620 100644 --- a/CodeEntropy/config/argparse.py +++ b/CodeEntropy/config/argparse.py @@ -129,9 +129,17 @@ class ArgSpec: help="Force partitioning", ), "water_entropy": ArgSpec( + type=bool, + default=False, + help="If set to True, enables the calculation of water entropy", + ), + "water_entropy_parallel": ArgSpec( type=bool, default=True, - help="If set to False, disables the calculation of water entropy", + help=( + "If set to False, disables the parallel calculation of " + "water entropy and switches to serial" + ), ), "grouping": ArgSpec( type=str, diff --git a/CodeEntropy/entropy/water.py b/CodeEntropy/entropy/water.py index 23ab2202..24988472 100644 --- a/CodeEntropy/entropy/water.py +++ b/CodeEntropy/entropy/water.py @@ -35,6 +35,7 @@ class WaterEntropyInput: end: int step: int temperature: float + parallel: bool group_id: int | None = None @@ -53,7 +54,7 @@ def __init__( self, args: Any, reporter: Any, - solver: Callable[..., tuple[dict, Any, Any, Any, Any]] = ( + solver: Callable[..., tuple[dict, Any, Any, Any, Any, Any]] = ( GetSolvent.get_interfacial_water_orient_entropy ), ) -> None: @@ -77,6 +78,7 @@ def calculate_and_log( start: int, end: int, step: int, + parallel: bool, group_id: int | None = None, ) -> None: """Compute water entropy and write results to the data logger. @@ -95,6 +97,7 @@ def calculate_and_log( step=step, temperature=float(self._args.temperature), group_id=group_id, + parallel=parallel, ) self._calculate_and_log_from_inputs(inputs) @@ -119,10 +122,12 @@ def _run_solver(self, inputs: WaterEntropyInput): Tuple of solver outputs. """ logger.info( - "[WaterEntropy] Computing water entropy (start=%s, end=%s, step=%s)", + "[WaterEntropy] Computing water entropy (start=%s, end=%s, step=%s, " + "parallel=%s)", inputs.start, inputs.end, inputs.step, + inputs.parallel, ) return self._solver( inputs.universe, @@ -130,7 +135,7 @@ def _run_solver(self, inputs: WaterEntropyInput): inputs.end, inputs.step, inputs.temperature, - parallel=True, + inputs.parallel, ) def _log_orientational_entropy( @@ -144,8 +149,9 @@ def _log_orientational_entropy( """ for _resid, resname_dict in Sorient_dict.items(): for resname, values in resname_dict.items(): - if isinstance(values, list) and len(values) == 2: - entropy, count = values + if isinstance(values, dict): + entropy = values["Sorient_Nw_ave"] + count = values["tot_count"] self._reporter.add_residue_data( group_id, resname, "Water", "Orientational", count, entropy ) diff --git a/CodeEntropy/entropy/workflow.py b/CodeEntropy/entropy/workflow.py index 9d5298e2..0b1c9817 100644 --- a/CodeEntropy/entropy/workflow.py +++ b/CodeEntropy/entropy/workflow.py @@ -105,7 +105,11 @@ def execute(self) -> None: reduced_universe, groups ) - if self._args.water_entropy and water_groups and nonwater_groups: + if self._args.water_entropy and water_groups: + if not nonwater_groups: + logger.warning( + "You are running waterEntropy on bulk water, this will be slow!" + ) self._compute_water_entropy(frame_selection, water_groups) else: nonwater_groups.update(water_groups) @@ -394,6 +398,7 @@ def _compute_water_entropy( start=start, end=end, step=step, + parallel=self._args.water_entropy_parallel, group_id=group_id, ) diff --git a/docs/source/getting_started.rst b/docs/source/getting_started.rst index 4dbc4541..609bae07 100644 --- a/docs/source/getting_started.rst +++ b/docs/source/getting_started.rst @@ -196,9 +196,13 @@ The ``top_traj_file`` argument is required; other arguments have default values. - ``0.5`` - ``float`` * - ``--water_entropy`` - - Use Jas Kalayan's waterEntropy code to calculate the water conformational entropy. + - Use Jas Kalayan's waterEntropy code to calculate the water orientational entropy. - ``False`` - ``bool`` + * - ``--water_entropy_parallel`` + - If water_entropy is True, run waterEntropy in parallel to calculate the water orientational entropy. + - ``True`` + - ``bool`` * - ``--grouping`` - How to group molecules for averaging. - ``molecules`` diff --git a/pyproject.toml b/pyproject.toml index 18d772ab..51c2f7f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,7 +45,7 @@ dependencies = [ "rich>=13.0.0,<16.0", "art>=5.0,<7.0", "networkx>=2.8,<4.0", - "waterEntropy>=2,<3.0", + "waterEntropy>=2.3.0,<3.0", "requests>=2.32.2,<3.0", "rdkit>=2025.3.4,<2027", "numba>=0.63.0,<0.70", diff --git a/tests/unit/CodeEntropy/entropy/test_water_entropy.py b/tests/unit/CodeEntropy/entropy/test_water_entropy.py index 67de69d0..9de59479 100644 --- a/tests/unit/CodeEntropy/entropy/test_water_entropy.py +++ b/tests/unit/CodeEntropy/entropy/test_water_entropy.py @@ -24,7 +24,7 @@ def test_water_entropy_calls_solver_and_logs_components(): args = SimpleNamespace(temperature=298.0) reporter = MagicMock() - Sorient_dict = {1: {"WAT": [3.0, 5]}} + Sorient_dict = {1: {"WAT": {"Sorient_Nw_ave": 3.0, "tot_count": 5}}} covariances = SimpleNamespace(counts={(7, "WAT"): 2}) @@ -41,7 +41,9 @@ def test_water_entropy_calls_solver_and_logs_components(): universe = _make_fake_universe_with_water() - we.calculate_and_log(universe=universe, start=0, end=10, step=1, group_id=9) + we.calculate_and_log( + universe=universe, start=0, end=10, step=1, parallel=True, group_id=9 + ) solver.assert_called_once() @@ -79,7 +81,9 @@ def test_water_entropy_handles_empty_solver_results_gracefully(): universe = _make_fake_universe_with_water() - we.calculate_and_log(universe=universe, start=0, end=10, step=1, group_id=1) + we.calculate_and_log( + universe=universe, start=0, end=10, step=1, parallel=True, group_id=1 + ) reporter.add_residue_data.assert_not_called() reporter.add_group_label.assert_called_once() @@ -113,7 +117,9 @@ def test_water_group_label_handles_multiple_water_resnames(): universe.select_atoms.return_value = water_selection - we.calculate_and_log(universe=universe, start=0, end=10, step=1, group_id=1) + we.calculate_and_log( + universe=universe, start=0, end=10, step=1, parallel=True, group_id=1 + ) reporter.add_group_label.assert_called_once() @@ -144,7 +150,7 @@ def test_log_group_label_defaults_to_WAT_when_no_residue_names_match(): water_selection.atoms = [1, 2, 3] universe.select_atoms.return_value = water_selection - Sorient_dict = {1: {"TIP3": [1.0, 1]}} + Sorient_dict = {1: {"TIP3": {"Sorient_Nw_ave": 1.0, "tot_count": 1}}} we._log_group_label(universe, Sorient_dict, group_id=7) diff --git a/tests/unit/CodeEntropy/entropy/test_workflow.py b/tests/unit/CodeEntropy/entropy/test_workflow.py index 04b9c900..a493cf5a 100644 --- a/tests/unit/CodeEntropy/entropy/test_workflow.py +++ b/tests/unit/CodeEntropy/entropy/test_workflow.py @@ -2,7 +2,7 @@ import sys import types from types import SimpleNamespace -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, call, patch import pytest @@ -73,13 +73,23 @@ def test_execute_calls_level_dag_and_entropy_graph_and_logs_tables(): wf._reporter.log_tables.assert_called_once() -def test_execute_water_entropy_branch_calls_water_entropy_solver(): +@pytest.mark.parametrize( + ("nonwater_groups", "expect_warning"), + [ + ({0: [0]}, False), + ({}, True), + ], +) +def test_execute_water_entropy_branch_calls_water_entropy_solver( + nonwater_groups, expect_warning +): args = SimpleNamespace( start=0, end=-1, step=1, grouping="molecules", water_entropy=True, + water_entropy_parallel=True, selection_string="all", output_file="out.json", ) @@ -105,15 +115,19 @@ def test_execute_water_entropy_branch_calls_water_entropy_solver(): wf._build_reduced_universe = MagicMock(return_value=MagicMock()) wf._detect_levels = MagicMock(return_value={0: ["united_atom"]}) - wf._split_water_groups = MagicMock(return_value=({0: [0]}, {9: [1, 2]})) + wf._split_water_groups = MagicMock(return_value=(nonwater_groups, {9: [1, 2]})) wf._finalize_molecule_results = MagicMock() - wf._group_molecules.grouping_molecules.return_value = {0: [0], 9: [1, 2]} + wf._group_molecules.grouping_molecules.return_value = { + **nonwater_groups, + 9: [1, 2], + } with ( patch("CodeEntropy.entropy.workflow.WaterEntropy") as WaterCls, patch("CodeEntropy.entropy.workflow.LevelDAG") as LevelDAGCls, patch("CodeEntropy.entropy.workflow.EntropyGraph") as GraphCls, + patch("CodeEntropy.entropy.workflow.logger") as logger, ): water_instance = WaterCls.return_value water_instance.calculate_and_log = MagicMock() @@ -125,6 +139,8 @@ def test_execute_water_entropy_branch_calls_water_entropy_solver(): water_instance.calculate_and_log.assert_called_once() _, kwargs = water_instance.calculate_and_log.call_args + warning = call("You are running waterEntropy on bulk water, this will be slow!") + assert (warning in logger.warning.call_args_list) is expect_warning assert kwargs["universe"] is universe assert kwargs["start"] == 0 assert kwargs["end"] == 5 @@ -291,6 +307,7 @@ def test_compute_water_entropy_updates_selection_string_and_calls_internal_metho args = SimpleNamespace( selection_string="all", water_entropy=True, + water_entropy_parallel=True, temperature=298.0, ) wf = EntropyWorkflow( @@ -317,6 +334,7 @@ def test_compute_water_entropy_updates_selection_string_and_calls_internal_metho start=0, end=5, step=1, + parallel=True, group_id=9, ) assert wf._args.selection_string == "not water" @@ -428,6 +446,7 @@ def test_compute_water_entropy_instantiates_waterentropy_and_updates_selection_s args = SimpleNamespace( selection_string="all", water_entropy=True, + water_entropy_parallel=True, temperature=298.0, ) universe = MagicMock() @@ -458,6 +477,7 @@ def test_compute_water_entropy_instantiates_waterentropy_and_updates_selection_s start=0, end=5, step=1, + parallel=True, group_id=9, ) assert args.selection_string == "not water" @@ -771,6 +791,7 @@ def test_compute_water_entropy_appends_not_water_to_existing_selection(): args = SimpleNamespace( selection_string="protein", water_entropy=True, + water_entropy_parallel=True, temperature=298.0, output_file="out.json", ) @@ -789,6 +810,7 @@ def test_compute_water_entropy_appends_not_water_to_existing_selection(): start=0, end=5, step=1, + parallel=True, group_id=9, ) assert args.selection_string == "protein and not water"