Skip to content

Commit 985b8e3

Browse files
committed
Refactor HydrogenBondAutoCorrel to inherit from AnalysisBase
1 parent 1e7644f commit 985b8e3

1 file changed

Lines changed: 52 additions & 52 deletions

File tree

package/MDAnalysis/analysis/hydrogenbonds/hbond_autocorrel.py

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
from MDAnalysis.lib.log import ProgressBar
213213
from MDAnalysis.lib.distances import capped_distance, calc_angles, calc_bonds
214214
from MDAnalysis.core.groups import requires
215+
from MDAnalysis.analysis.base import AnalysisBase
215216

216217
from MDAnalysis.due import due, Doi
217218

@@ -243,7 +244,7 @@ def find_hydrogen_donors(hydrogens):
243244
return sum(h.bonded_atoms[0] for h in hydrogens)
244245

245246

246-
class HydrogenBondAutoCorrel(object):
247+
class HydrogenBondAutoCorrel(AnalysisBase):
247248
"""Perform a time autocorrelation of the hydrogen bonds in the system.
248249
249250
Parameters
@@ -305,17 +306,9 @@ def __init__(
305306
nsamples=50, # number of different points to sample in a run
306307
pbc=True,
307308
):
308-
309-
# warnings.warn("This class is deprecated, use analysis.hbonds.HydrogenBondAnalysis "
310-
# "which has .autocorrelation function",
311-
# category=DeprecationWarning)
312-
309+
310+
super(HydrogenBondAutoCorrel, self).__init__(universe.trajectory)
313311
self.u = universe
314-
# check that slicing is possible
315-
try:
316-
self.u.trajectory[0]
317-
except Exception:
318-
raise ValueError("Trajectory must support slicing") from None
319312

320313
self.h = hydrogens
321314
self.a = acceptors
@@ -400,50 +393,57 @@ def _slice_traj(self, sample_time):
400393
)
401394
self._skip = 1
402395

403-
def run(self, force=False):
404-
"""Run all the required passes
405-
406-
Parameters
407-
----------
408-
force : bool, optional
409-
Will overwrite previous results if they exist
410-
"""
411-
# if results exist, don't waste any time
412-
if self.solution["results"] is not None and not force:
413-
return
396+
def run(self, start=None, stop=None, step=None, verbose=None, **kwargs):
397+
"""Run all the required passes
398+
399+
Parameters
400+
----------
401+
start : int, optional
402+
start frame of trajectory (ignored, uses nruns logic)
403+
stop : int, optional
404+
end frame of trajectory (ignored, uses nruns logic)
405+
step : int, optional
406+
step size (ignored, uses nruns logic)
407+
verbose : bool, optional
408+
Show the progress bar
409+
"""
410+
self._slice_traj(self.sample_time)
414411

415-
main_results = np.zeros_like(
416-
np.arange(self._starts[0], self._stops[0], self._skip),
417-
dtype=np.float32,
418-
)
419-
# for normalising later
420-
counter = np.zeros_like(main_results, dtype=np.float32)
421-
422-
for i, (start, stop) in ProgressBar(
423-
enumerate(zip(self._starts, self._stops)),
424-
total=self.nruns,
425-
desc="Performing run",
426-
):
427-
428-
# needed else trj seek thinks a np.int64 isn't an int?
429-
results = self._single_run(int(start), int(stop))
430-
431-
nresults = len(results)
432-
if nresults == len(main_results):
433-
main_results += results
434-
counter += 1.0
435-
else:
436-
main_results[:nresults] += results
437-
counter[:nresults] += 1.0
412+
main_results = np.zeros_like(
413+
np.arange(self._starts[0], self._stops[0], self._skip),
414+
dtype=np.float32,
415+
)
416+
# for normalising later
417+
counter = np.zeros_like(main_results, dtype=np.float32)
418+
419+
for i, (start, stop) in ProgressBar(
420+
enumerate(zip(self._starts, self._stops)),
421+
total=self.nruns,
422+
desc="Performing run",
423+
verbose=verbose,
424+
):
425+
426+
# needed else trj seek thinks a np.int64 isn't an int?
427+
results = self._single_run(int(start), int(stop))
428+
429+
nresults = len(results)
430+
if nresults == len(main_results):
431+
main_results += results
432+
counter += 1.0
433+
else:
434+
main_results[:nresults] += results
435+
counter[:nresults] += 1.0
438436

439-
main_results /= counter
437+
main_results /= counter
440438

441-
self.solution["time"] = (
442-
np.arange(len(main_results), dtype=np.float32)
443-
* self.u.trajectory.dt
444-
* self._skip
445-
)
446-
self.solution["results"] = main_results
439+
self.solution["time"] = (
440+
np.arange(len(main_results), dtype=np.float32)
441+
* self.u.trajectory.dt
442+
* self._skip
443+
)
444+
self.solution["results"] = main_results
445+
446+
return self
447447

448448
def _single_run(self, start, stop):
449449
"""Perform a single pass of the trajectory"""

0 commit comments

Comments
 (0)