Skip to content

Commit 10aa300

Browse files
committed
Merge remote-tracking branch 'origin/main' into 426-axis-argument
2 parents b76b687 + 585bea4 commit 10aa300

7 files changed

Lines changed: 69 additions & 18 deletions

File tree

‎CodeEntropy/config/argparse.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,17 @@ class ArgSpec:
129129
help="Force partitioning",
130130
),
131131
"water_entropy": ArgSpec(
132+
type=bool,
133+
default=False,
134+
help="If set to True, enables the calculation of water entropy",
135+
),
136+
"water_entropy_parallel": ArgSpec(
132137
type=bool,
133138
default=True,
134-
help="If set to False, disables the calculation of water entropy",
139+
help=(
140+
"If set to False, disables the parallel calculation of "
141+
"water entropy and switches to serial"
142+
),
135143
),
136144
"grouping": ArgSpec(
137145
type=str,

‎CodeEntropy/entropy/water.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class WaterEntropyInput:
3535
end: int
3636
step: int
3737
temperature: float
38+
parallel: bool
3839
group_id: int | None = None
3940

4041

@@ -53,7 +54,7 @@ def __init__(
5354
self,
5455
args: Any,
5556
reporter: Any,
56-
solver: Callable[..., tuple[dict, Any, Any, Any, Any]] = (
57+
solver: Callable[..., tuple[dict, Any, Any, Any, Any, Any]] = (
5758
GetSolvent.get_interfacial_water_orient_entropy
5859
),
5960
) -> None:
@@ -77,6 +78,7 @@ def calculate_and_log(
7778
start: int,
7879
end: int,
7980
step: int,
81+
parallel: bool,
8082
group_id: int | None = None,
8183
) -> None:
8284
"""Compute water entropy and write results to the data logger.
@@ -95,6 +97,7 @@ def calculate_and_log(
9597
step=step,
9698
temperature=float(self._args.temperature),
9799
group_id=group_id,
100+
parallel=parallel,
98101
)
99102
self._calculate_and_log_from_inputs(inputs)
100103

@@ -119,18 +122,20 @@ def _run_solver(self, inputs: WaterEntropyInput):
119122
Tuple of solver outputs.
120123
"""
121124
logger.info(
122-
"[WaterEntropy] Computing water entropy (start=%s, end=%s, step=%s)",
125+
"[WaterEntropy] Computing water entropy (start=%s, end=%s, step=%s, "
126+
"parallel=%s)",
123127
inputs.start,
124128
inputs.end,
125129
inputs.step,
130+
inputs.parallel,
126131
)
127132
return self._solver(
128133
inputs.universe,
129134
inputs.start,
130135
inputs.end,
131136
inputs.step,
132137
inputs.temperature,
133-
parallel=True,
138+
inputs.parallel,
134139
)
135140

136141
def _log_orientational_entropy(
@@ -144,8 +149,9 @@ def _log_orientational_entropy(
144149
"""
145150
for _resid, resname_dict in Sorient_dict.items():
146151
for resname, values in resname_dict.items():
147-
if isinstance(values, list) and len(values) == 2:
148-
entropy, count = values
152+
if isinstance(values, dict):
153+
entropy = values["Sorient_Nw_ave"]
154+
count = values["tot_count"]
149155
self._reporter.add_residue_data(
150156
group_id, resname, "Water", "Orientational", count, entropy
151157
)

‎CodeEntropy/entropy/workflow.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ def execute(self) -> None:
105105
reduced_universe, groups
106106
)
107107

108-
if self._args.water_entropy and water_groups and nonwater_groups:
108+
if self._args.water_entropy and water_groups:
109+
if not nonwater_groups:
110+
logger.warning(
111+
"You are running waterEntropy on bulk water, this will be slow!"
112+
)
109113
self._compute_water_entropy(frame_selection, water_groups)
110114
else:
111115
nonwater_groups.update(water_groups)
@@ -394,6 +398,7 @@ def _compute_water_entropy(
394398
start=start,
395399
end=end,
396400
step=step,
401+
parallel=self._args.water_entropy_parallel,
397402
group_id=group_id,
398403
)
399404

‎docs/source/getting_started.rst‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,13 @@ The ``top_traj_file`` argument is required; other arguments have default values.
196196
- ``0.5``
197197
- ``float``
198198
* - ``--water_entropy``
199-
- Use Jas Kalayan's waterEntropy code to calculate the water conformational entropy.
199+
- Use Jas Kalayan's waterEntropy code to calculate the water orientational entropy.
200200
- ``False``
201201
- ``bool``
202+
* - ``--water_entropy_parallel``
203+
- If water_entropy is True, run waterEntropy in parallel to calculate the water orientational entropy.
204+
- ``True``
205+
- ``bool``
202206
* - ``--grouping``
203207
- How to group molecules for averaging.
204208
- ``molecules``

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ dependencies = [
4545
"rich>=13.0.0,<16.0",
4646
"art>=5.0,<7.0",
4747
"networkx>=2.8,<4.0",
48-
"waterEntropy>=2,<3.0",
48+
"waterEntropy>=2.3.0,<3.0",
4949
"requests>=2.32.2,<3.0",
5050
"rdkit>=2025.3.4,<2027",
5151
"numba>=0.63.0,<0.70",

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_water_entropy_calls_solver_and_logs_components():
2424
args = SimpleNamespace(temperature=298.0)
2525
reporter = MagicMock()
2626

27-
Sorient_dict = {1: {"WAT": [3.0, 5]}}
27+
Sorient_dict = {1: {"WAT": {"Sorient_Nw_ave": 3.0, "tot_count": 5}}}
2828

2929
covariances = SimpleNamespace(counts={(7, "WAT"): 2})
3030

@@ -41,7 +41,9 @@ def test_water_entropy_calls_solver_and_logs_components():
4141

4242
universe = _make_fake_universe_with_water()
4343

44-
we.calculate_and_log(universe=universe, start=0, end=10, step=1, group_id=9)
44+
we.calculate_and_log(
45+
universe=universe, start=0, end=10, step=1, parallel=True, group_id=9
46+
)
4547

4648
solver.assert_called_once()
4749

@@ -79,7 +81,9 @@ def test_water_entropy_handles_empty_solver_results_gracefully():
7981

8082
universe = _make_fake_universe_with_water()
8183

82-
we.calculate_and_log(universe=universe, start=0, end=10, step=1, group_id=1)
84+
we.calculate_and_log(
85+
universe=universe, start=0, end=10, step=1, parallel=True, group_id=1
86+
)
8387

8488
reporter.add_residue_data.assert_not_called()
8589
reporter.add_group_label.assert_called_once()
@@ -113,7 +117,9 @@ def test_water_group_label_handles_multiple_water_resnames():
113117

114118
universe.select_atoms.return_value = water_selection
115119

116-
we.calculate_and_log(universe=universe, start=0, end=10, step=1, group_id=1)
120+
we.calculate_and_log(
121+
universe=universe, start=0, end=10, step=1, parallel=True, group_id=1
122+
)
117123

118124
reporter.add_group_label.assert_called_once()
119125

@@ -144,7 +150,7 @@ def test_log_group_label_defaults_to_WAT_when_no_residue_names_match():
144150
water_selection.atoms = [1, 2, 3]
145151
universe.select_atoms.return_value = water_selection
146152

147-
Sorient_dict = {1: {"TIP3": [1.0, 1]}}
153+
Sorient_dict = {1: {"TIP3": {"Sorient_Nw_ave": 1.0, "tot_count": 1}}}
148154

149155
we._log_group_label(universe, Sorient_dict, group_id=7)
150156

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
import types
44
from types import SimpleNamespace
5-
from unittest.mock import MagicMock, patch
5+
from unittest.mock import MagicMock, call, patch
66

77
import pytest
88

@@ -73,13 +73,23 @@ def test_execute_calls_level_dag_and_entropy_graph_and_logs_tables():
7373
wf._reporter.log_tables.assert_called_once()
7474

7575

76-
def test_execute_water_entropy_branch_calls_water_entropy_solver():
76+
@pytest.mark.parametrize(
77+
("nonwater_groups", "expect_warning"),
78+
[
79+
({0: [0]}, False),
80+
({}, True),
81+
],
82+
)
83+
def test_execute_water_entropy_branch_calls_water_entropy_solver(
84+
nonwater_groups, expect_warning
85+
):
7786
args = SimpleNamespace(
7887
start=0,
7988
end=-1,
8089
step=1,
8190
grouping="molecules",
8291
water_entropy=True,
92+
water_entropy_parallel=True,
8393
selection_string="all",
8494
output_file="out.json",
8595
)
@@ -105,15 +115,19 @@ def test_execute_water_entropy_branch_calls_water_entropy_solver():
105115
wf._build_reduced_universe = MagicMock(return_value=MagicMock())
106116
wf._detect_levels = MagicMock(return_value={0: ["united_atom"]})
107117

108-
wf._split_water_groups = MagicMock(return_value=({0: [0]}, {9: [1, 2]}))
118+
wf._split_water_groups = MagicMock(return_value=(nonwater_groups, {9: [1, 2]}))
109119
wf._finalize_molecule_results = MagicMock()
110120

111-
wf._group_molecules.grouping_molecules.return_value = {0: [0], 9: [1, 2]}
121+
wf._group_molecules.grouping_molecules.return_value = {
122+
**nonwater_groups,
123+
9: [1, 2],
124+
}
112125

113126
with (
114127
patch("CodeEntropy.entropy.workflow.WaterEntropy") as WaterCls,
115128
patch("CodeEntropy.entropy.workflow.LevelDAG") as LevelDAGCls,
116129
patch("CodeEntropy.entropy.workflow.EntropyGraph") as GraphCls,
130+
patch("CodeEntropy.entropy.workflow.logger") as logger,
117131
):
118132
water_instance = WaterCls.return_value
119133
water_instance.calculate_and_log = MagicMock()
@@ -125,6 +139,8 @@ def test_execute_water_entropy_branch_calls_water_entropy_solver():
125139

126140
water_instance.calculate_and_log.assert_called_once()
127141
_, kwargs = water_instance.calculate_and_log.call_args
142+
warning = call("You are running waterEntropy on bulk water, this will be slow!")
143+
assert (warning in logger.warning.call_args_list) is expect_warning
128144
assert kwargs["universe"] is universe
129145
assert kwargs["start"] == 0
130146
assert kwargs["end"] == 5
@@ -291,6 +307,7 @@ def test_compute_water_entropy_updates_selection_string_and_calls_internal_metho
291307
args = SimpleNamespace(
292308
selection_string="all",
293309
water_entropy=True,
310+
water_entropy_parallel=True,
294311
temperature=298.0,
295312
)
296313
wf = EntropyWorkflow(
@@ -317,6 +334,7 @@ def test_compute_water_entropy_updates_selection_string_and_calls_internal_metho
317334
start=0,
318335
end=5,
319336
step=1,
337+
parallel=True,
320338
group_id=9,
321339
)
322340
assert wf._args.selection_string == "not water"
@@ -428,6 +446,7 @@ def test_compute_water_entropy_instantiates_waterentropy_and_updates_selection_s
428446
args = SimpleNamespace(
429447
selection_string="all",
430448
water_entropy=True,
449+
water_entropy_parallel=True,
431450
temperature=298.0,
432451
)
433452
universe = MagicMock()
@@ -458,6 +477,7 @@ def test_compute_water_entropy_instantiates_waterentropy_and_updates_selection_s
458477
start=0,
459478
end=5,
460479
step=1,
480+
parallel=True,
461481
group_id=9,
462482
)
463483
assert args.selection_string == "not water"
@@ -771,6 +791,7 @@ def test_compute_water_entropy_appends_not_water_to_existing_selection():
771791
args = SimpleNamespace(
772792
selection_string="protein",
773793
water_entropy=True,
794+
water_entropy_parallel=True,
774795
temperature=298.0,
775796
output_file="out.json",
776797
)
@@ -789,6 +810,7 @@ def test_compute_water_entropy_appends_not_water_to_existing_selection():
789810
start=0,
790811
end=5,
791812
step=1,
813+
parallel=True,
792814
group_id=9,
793815
)
794816
assert args.selection_string == "protein and not water"

0 commit comments

Comments
 (0)