Skip to content

Commit 3744a63

Browse files
committed
Apply black formatting
1 parent 985b8e3 commit 3744a63

3 files changed

Lines changed: 104 additions & 78 deletions

File tree

package/MDAnalysis/analysis/hydrogenbonds/hbond_autocorrel.py

Lines changed: 64 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def __init__(
306306
nsamples=50, # number of different points to sample in a run
307307
pbc=True,
308308
):
309-
309+
310310
super(HydrogenBondAutoCorrel, self).__init__(universe.trajectory)
311311
self.u = universe
312312

@@ -321,20 +321,16 @@ def __init__(
321321

322322
if exclusions is not None:
323323
if len(exclusions[0]) != len(exclusions[1]):
324-
raise ValueError(
325-
"'exclusion' must be two arrays of identical length"
326-
)
327-
self.exclusions = np.column_stack(
328-
(exclusions[0], exclusions[1])
329-
).astype(np.intp)
324+
raise ValueError("'exclusion' must be two arrays of identical length")
325+
self.exclusions = np.column_stack((exclusions[0], exclusions[1])).astype(
326+
np.intp
327+
)
330328
else:
331329
self.exclusions = None
332330

333331
self.bond_type = bond_type
334332
if self.bond_type not in ["continuous", "intermittent"]:
335-
raise ValueError(
336-
"bond_type must be either 'continuous' or 'intermittent'"
337-
)
333+
raise ValueError("bond_type must be either 'continuous' or 'intermittent'")
338334

339335
self.a_crit = np.deg2rad(angle_crit)
340336
self.d_crit = dist_crit
@@ -354,6 +350,12 @@ def __init__(
354350
}
355351

356352
def _slice_traj(self, sample_time):
353+
354+
try:
355+
self.u.trajectory[0]
356+
except TypeError:
357+
raise ValueError("Trajectory is unable to be sliced")
358+
357359
"""Set up start and end points in the trajectory for the
358360
different passes
359361
"""
@@ -364,9 +366,7 @@ def _slice_traj(self, sample_time):
364366
if req_frames > n_frames:
365367
warnings.warn(
366368
"Number of required frames ({}) greater than the"
367-
" number of frames in trajectory ({})".format(
368-
req_frames, n_frames
369-
),
369+
" number of frames in trajectory ({})".format(req_frames, n_frames),
370370
RuntimeWarning,
371371
)
372372

@@ -394,56 +394,56 @@ def _slice_traj(self, sample_time):
394394
self._skip = 1
395395

396396
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)
397+
"""Run all the required passes
411398
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
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)
436411

437-
main_results /= counter
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-
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
437+
main_results /= counter
438+
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"""
@@ -477,9 +477,7 @@ def _single_run(self, start, stop):
477477
aidx = aidx[idx2]
478478

479479
nbonds = len(hidx) # number of hbonds at t=0
480-
results = np.zeros_like(
481-
np.arange(start, stop, self._skip), dtype=np.float32
482-
)
480+
results = np.zeros_like(np.arange(start, stop, self._skip), dtype=np.float32)
483481

484482
if self.time_cut:
485483
# counter for time criteria
@@ -488,9 +486,7 @@ def _single_run(self, start, stop):
488486
for i, ts in enumerate(self.u.trajectory[start : stop : self._skip]):
489487
box = self.u.dimensions if self.pbc else None
490488

491-
d = calc_bonds(
492-
self.h.positions[hidx], self.a.positions[aidx], box=box
493-
)
489+
d = calc_bonds(self.h.positions[hidx], self.a.positions[aidx], box=box)
494490
a = calc_angles(
495491
self.d.positions[hidx],
496492
self.h.positions[hidx],
@@ -615,9 +611,7 @@ def triple(x, A1, A2, tau1, tau2, tau3):
615611
"""Sum of three exponential functions"""
616612
A3 = 1 - (A1 + A2)
617613
return (
618-
A1 * np.exp(-x / tau1)
619-
+ A2 * np.exp(-x / tau2)
620-
+ A3 * np.exp(-x / tau3)
614+
A1 * np.exp(-x / tau1) + A2 * np.exp(-x / tau2) + A3 * np.exp(-x / tau3)
621615
)
622616

623617
if self.bond_type == "continuous":
@@ -658,9 +652,7 @@ def triple(x, A1, A2, tau1, tau2, tau3):
658652
self.solution["ier"] = ier
659653

660654
if ier in [1, 2, 3, 4]: # solution found if ier is one of these values
661-
self.solution["estimate"] = self._my_solve(
662-
self.solution["time"], *p
663-
)
655+
self.solution["estimate"] = self._my_solve(self.solution["time"], *p)
664656
else:
665657
warnings.warn("Solution to results not found", RuntimeWarning)
666658

testsuite/MDAnalysisTests/analysis/conftest.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
from MDAnalysis.analysis.hydrogenbonds.hbond_analysis import (
1616
HydrogenBondAnalysis,
1717
)
18+
from MDAnalysis.analysis.hydrogenbonds.hbond_autocorrel import (
19+
HydrogenBondAutoCorrel,
20+
)
1821
from MDAnalysis.analysis.nucleicacids import NucPairDist
1922
from MDAnalysis.analysis.contacts import Contacts
2023
from MDAnalysis.analysis.density import DensityAnalysis
@@ -217,3 +220,34 @@ def client_InterRDF_s(request):
217220
@pytest.fixture(scope="module", params=params_for_cls(DistanceMatrix))
218221
def client_DistanceMatrix(request):
219222
return request.param
223+
224+
225+
# MDAnalysis.analysis.hydrogenbonds.hbond_autocorrel
226+
227+
228+
@pytest.fixture(
229+
scope="module",
230+
params=params_for_cls(HydrogenBondAutoCorrel, exclude=["multiprocessing"]),
231+
)
232+
def client_HydrogenBondAutoCorrel(request):
233+
return request.param
234+
235+
236+
@pytest.fixture(scope="module")
237+
def hbond_autocorrel(trajectory):
238+
"""Fixture for HydrogenBondAutoCorrel analysis"""
239+
u = trajectory
240+
h = u.select_atoms("name H1")
241+
n = u.select_atoms("name N")
242+
o = u.select_atoms("name O")
243+
244+
return HydrogenBondAutoCorrel(
245+
u,
246+
hydrogens=h,
247+
donors=n,
248+
acceptors=o,
249+
bond_type="continuous",
250+
sample_time=10.0,
251+
nruns=2,
252+
nsamples=10,
253+
)

testsuite/MDAnalysisTests/analysis/test_hydrogenbondautocorrel.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def nitrogens(self, u):
5858
def oxygens(self, u):
5959
return u.atoms.select_atoms("name O")
6060

61+
@pytest.fixture(autouse=True)
62+
def client(self, client_HydrogenBondAutoCorrel):
63+
self.client = client_HydrogenBondAutoCorrel
64+
6165
# regression tests for different conditions
6266
def test_continuous(self, u, hydrogens, oxygens, nitrogens):
6367
hbond = HBAC(
@@ -245,9 +249,7 @@ def actual_function_int(t):
245249
tau2 = 1
246250
tau3 = 0.1
247251
return (
248-
A1 * np.exp(-t / tau1)
249-
+ A2 * np.exp(-t / tau2)
250-
+ A3 * np.exp(-t / tau3)
252+
A1 * np.exp(-t / tau1) + A2 * np.exp(-t / tau2) + A3 * np.exp(-t / tau3)
251253
)
252254

253255
hbond.solution["time"] = time = np.arange(0, 6.0, 0.01)
@@ -321,9 +323,7 @@ def test_solve_before_run_VE(self, u, hydrogens, oxygens, nitrogens):
321323
hbond.solve()
322324

323325
@mock.patch("MDAnalysis.coordinates.TRZ.TRZReader._read_frame")
324-
def test_unslicable_traj_VE(
325-
self, mock_read, u, hydrogens, oxygens, nitrogens
326-
):
326+
def test_unslicable_traj_VE(self, mock_read, u, hydrogens, oxygens, nitrogens):
327327
mock_read.side_effect = TypeError
328328

329329
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)