Skip to content

Commit 84e63d2

Browse files
fix hbond iterator
1 parent a029dcd commit 84e63d2

4 files changed

Lines changed: 34 additions & 3 deletions

File tree

package/AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ Chronological list of authors
266266
- Pranay Pelapkar
267267
- Shreejan Dolai
268268
- Tanisha Dubey
269+
- Brady Johnston
269270

270271
External code
271272
-------------

package/CHANGELOG

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ The rules for this file:
1515

1616
-------------------------------------------------------------------------------
1717
??/??/?? IAlibay, orbeckst, marinegor, tylerjereddy, ljwoods2, marinegor,
18-
spyke7, talagayev, tanii1125
18+
spyke7, talagayev
1919

2020
* 2.11.0
2121

2222
Fixes
23+
* HydrogenBondAnalysis: Fixed `count_by_time()` when using `run(FrameIterator)` that
24+
results in `self.start` and `self.end` being None (Issue #5200, PR #****)
2325
* DSSP now explicitly checks for a minimum of 6 residues and raises a clear
2426
error message, unlike the previous behavior where it would fail with an
2527
incomprehensible broadcasting error at execution time (Issue #5046, PR #5163)

package/MDAnalysis/analysis/hydrogenbonds/hbond_analysis.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,8 +912,16 @@ def count_by_time(self):
912912
the number of hydrogen bonds over time.
913913
"""
914914

915-
indices, tmp_counts = np.unique(self.results.hbonds[:, 0], axis=0,
916-
return_counts=True)
915+
indices, tmp_counts = np.unique(
916+
self.results.hbonds[:, 0], axis=0, return_counts=True
917+
)
918+
919+
# if we pass a specific subset of frames there won't be a start / end frame
920+
# so we do a manual lookup and return instead. some frames might be zero
921+
# so they would be missing from the np.unique return
922+
if self.start is None:
923+
count_lookup = {idx: count for idx, count in zip(indices, tmp_counts)}
924+
return np.array([count_lookup.get(i, 0) for i in range(len(self.frames))])
917925

918926
indices -= self.start
919927
indices /= self.step

testsuite/MDAnalysisTests/analysis/test_hydrogenbonds_analysis.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,3 +760,23 @@ def test_hbond_analysis(self, universe, client_HydrogenBondAnalysis):
760760
assert h.hydrogens_sel == ""
761761
assert h.acceptors_sel == ""
762762
assert h.results.hbonds.size == 0
763+
764+
765+
class TestHydrogenBondAnalysisFrameIterator:
766+
@staticmethod
767+
@pytest.fixture(scope="class")
768+
def universe():
769+
return MDAnalysis.Universe(waterPSF, waterDCD)
770+
771+
def test_frame_iterator(self, universe):
772+
frames = np.array([0, 1, 2, 5, 6, 7, 8])
773+
hbonds = HydrogenBondAnalysis(
774+
universe=universe,
775+
hydrogens_sel="name H1 H2",
776+
acceptors_sel="name OH2",
777+
update_selections=False,
778+
)
779+
hbonds.run(frames=frames)
780+
assert np.array_equal(
781+
hbonds.count_by_time(), np.array([2, 1, 4, 0, 0, 3, 3])
782+
)

0 commit comments

Comments
 (0)