Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CodeEntropy/config/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 11 additions & 5 deletions CodeEntropy/entropy/water.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WaterEntropyInput:
end: int
step: int
temperature: float
parallel: bool
group_id: int | None = None


Expand All @@ -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:
Expand All @@ -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.
Expand All @@ -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)

Expand All @@ -119,18 +122,20 @@ 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,
inputs.start,
inputs.end,
inputs.step,
inputs.temperature,
parallel=True,
inputs.parallel,
)

def _log_orientational_entropy(
Expand All @@ -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
)
Expand Down
7 changes: 6 additions & 1 deletion CodeEntropy/entropy/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -394,6 +398,7 @@ def _compute_water_entropy(
start=start,
end=end,
step=step,
parallel=self._args.water_entropy_parallel,
group_id=group_id,
)

Expand Down
6 changes: 5 additions & 1 deletion docs/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 11 additions & 5 deletions tests/unit/CodeEntropy/entropy/test_water_entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand All @@ -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()

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)

Expand Down
30 changes: 26 additions & 4 deletions tests/unit/CodeEntropy/entropy/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
)
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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",
)
Expand All @@ -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"
Expand Down
Loading