1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
|
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8
#
# MDAnalysis --- https://www.mdanalysis.org
# Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors
# (see the file AUTHORS for the full list of names)
#
# Released under the Lesser GNU Public Licence, v2.1 or any higher version
#
# Please cite your use of MDAnalysis in published work:
#
# R. J. Gowers, M. Linke, J. Barnoud, T. J. E. Reddy, M. N. Melo, S. L. Seyler,
# D. L. Dotson, J. Domanski, S. Buchoux, I. M. Kenney, and O. Beckstein.
# MDAnalysis: A Python package for the rapid analysis of molecular dynamics
# simulations. In S. Benthall and S. Rostrup editors, Proceedings of the 15th
# Python in Science Conference, pages 102-109, Austin, TX, 2016. SciPy.
# doi: 10.25080/majora-629e541a-00e
#
# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein.
# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations.
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
import pickle
from collections import UserDict
import MDAnalysis as mda
import numpy as np
import pytest
from MDAnalysis.analysis import backends, base
from numpy.testing import assert_allclose, assert_equal
from MDAnalysisTests.datafiles import DCD, PSF, TPR, XTC
from MDAnalysisTests.util import no_deprecated_call
class FrameAnalysis(base.AnalysisBase):
"""Just grabs frame numbers of frames it goes over"""
@classmethod
def get_supported_backends(cls):
return ("serial", "dask", "multiprocessing")
_analysis_algorithm_is_parallelizable = True
def __init__(self, reader, **kwargs):
super(FrameAnalysis, self).__init__(reader, **kwargs)
self.traj = reader
def _prepare(self):
self.results.found_frames = []
def _single_frame(self):
self.results.found_frames.append(self._ts.frame)
def _conclude(self):
self.found_frames = list(self.results.found_frames)
def _get_aggregator(self):
return base.ResultsGroup(
{"found_frames": base.ResultsGroup.ndarray_hstack}
)
class IncompleteAnalysis(base.AnalysisBase):
def __init__(self, reader, **kwargs):
super(IncompleteAnalysis, self).__init__(reader, **kwargs)
class OldAPIAnalysis(base.AnalysisBase):
"""for version 0.15.0"""
def __init__(self, reader, **kwargs):
self._setup_frames(reader, **kwargs)
def _single_frame(self):
pass
def _prepare(self):
self.results = base.Results()
@pytest.fixture(scope="module")
def u():
return mda.Universe(PSF, DCD)
@pytest.fixture(scope="module")
def u_xtc():
return mda.Universe(TPR, XTC) # dt = 100
FRAMES_ERR = "AnalysisBase.frames is incorrect"
TIMES_ERR = "AnalysisBase.times is incorrect"
class Parallelizable(base.AnalysisBase):
_analysis_algorithm_is_parallelizable = True
@classmethod
def get_supported_backends(cls):
return ("multiprocessing", "dask")
def _single_frame(self):
pass
class SerialOnly(base.AnalysisBase):
def _single_frame(self):
pass
class ParallelizableWithDaskOnly(base.AnalysisBase):
_analysis_algorithm_is_parallelizable = True
@classmethod
def get_supported_backends(cls):
return ("dask",)
def _single_frame(self):
pass
class CustomSerialBackend(backends.BackendBase):
def apply(self, func, computations):
return [func(task) for task in computations]
class ManyWorkersBackend(backends.BackendBase):
def apply(self, func, computations):
return [func(task) for task in computations]
def test_incompatible_n_workers(u):
backend = ManyWorkersBackend(n_workers=2)
with pytest.raises(ValueError):
FrameAnalysis(u).run(backend=backend, n_workers=3)
def test_frame_values_incompatability(u):
start, stop, step = 0, 4, 1
frames = [1, 2, 3, 4]
with pytest.raises(
ValueError, match="start/stop/step cannot be combined with frames"
):
FrameAnalysis(u.trajectory).run(
frames=frames, start=start, stop=stop, step=step
)
def test_n_workers_conflict_raises_value_error(u):
backend_instance = ManyWorkersBackend(n_workers=4)
with pytest.raises(ValueError, match="n_workers specified twice"):
FrameAnalysis(u.trajectory).run(
backend=backend_instance, n_workers=1, unsupported_backend=True
)
@pytest.mark.parametrize(
"run_class,backend,n_workers",
[
(Parallelizable, "not-existing-backend", 2),
(Parallelizable, "not-existing-backend", None),
(SerialOnly, "not-existing-backend", 2),
(SerialOnly, "not-existing-backend", None),
(SerialOnly, "multiprocessing", 2),
(SerialOnly, "dask", None),
(ParallelizableWithDaskOnly, "multiprocessing", None),
(ParallelizableWithDaskOnly, "multiprocessing", 2),
],
)
def test_backend_configuration_fails(u, run_class, backend, n_workers):
u = mda.Universe(TPR, XTC) # dt = 100
with pytest.raises(ValueError):
_ = run_class(u.trajectory).run(
backend=backend, n_workers=n_workers, stop=0
)
@pytest.mark.parametrize(
"run_class,backend,n_workers",
[
(Parallelizable, CustomSerialBackend, 2),
(ParallelizableWithDaskOnly, CustomSerialBackend, 2),
],
)
def test_backend_configuration_works_when_unsupported_backend(
u, run_class, backend, n_workers
):
u = mda.Universe(TPR, XTC) # dt = 100
backend_instance = backend(n_workers=n_workers)
_ = run_class(u.trajectory).run(
backend=backend_instance,
n_workers=n_workers,
stop=0,
unsupported_backend=True,
)
@pytest.mark.parametrize(
"run_class,backend,n_workers",
[
(Parallelizable, CustomSerialBackend, 1),
(ParallelizableWithDaskOnly, CustomSerialBackend, 1),
],
)
def test_custom_backend_works(u, run_class, backend, n_workers):
backend_instance = backend(n_workers=n_workers)
u = mda.Universe(TPR, XTC) # dt = 100
_ = run_class(u.trajectory).run(
backend=backend_instance, n_workers=n_workers, unsupported_backend=True
)
@pytest.mark.parametrize(
"run_class,backend_instance,n_workers",
[
(Parallelizable, map, 1),
(SerialOnly, list, 1),
(ParallelizableWithDaskOnly, object, 1),
],
)
def test_fails_incorrect_custom_backend(
u, run_class, backend_instance, n_workers
):
u = mda.Universe(TPR, XTC) # dt = 100
with pytest.raises(ValueError):
_ = run_class(u.trajectory).run(
backend=backend_instance,
n_workers=n_workers,
unsupported_backend=True,
)
with pytest.raises(ValueError):
_ = run_class(u.trajectory).run(
backend=backend_instance, n_workers=n_workers
)
@pytest.mark.parametrize(
"run_class,backend,n_workers",
[
(SerialOnly, CustomSerialBackend, 1),
(SerialOnly, "multiprocessing", 1),
(SerialOnly, "dask", 1),
],
)
def test_fails_for_unparallelizable(u, run_class, backend, n_workers):
u = mda.Universe(TPR, XTC) # dt = 100
with pytest.raises(ValueError):
if not isinstance(backend, str):
backend_instance = backend(n_workers=n_workers)
_ = run_class(u.trajectory).run(
backend=backend_instance,
n_workers=n_workers,
unsupported_backend=True,
)
else:
_ = run_class(u.trajectory).run(
backend=backend, n_workers=n_workers, unsupported_backend=True
)
@pytest.mark.parametrize(
"run_kwargs,frames",
[
({}, np.arange(98)),
({"start": 20}, np.arange(20, 98)),
({"stop": 30}, np.arange(30)),
({"step": 10}, np.arange(0, 98, 10)),
],
)
def test_start_stop_step_parallel(u, run_kwargs, frames, client_FrameAnalysis):
# client_FrameAnalysis is defined [here](testsuite/MDAnalysisTests/analysis/conftest.py),
# and determines a set of parameters ('backend', 'n_workers'), taking only backends
# that are implemented for a given subclass, to run the test against.
an = FrameAnalysis(u.trajectory).run(**run_kwargs, **client_FrameAnalysis)
assert an.n_frames == len(frames)
assert_equal(an.found_frames, frames)
assert_equal(an.frames, frames, err_msg=FRAMES_ERR)
assert_allclose(an.times, frames + 1, rtol=0, atol=1e-4, err_msg=TIMES_ERR)
def test_reset_n_parts_to_n_frames(u):
"""
Issue #4685
https://github.com/MDAnalysis/mdanalysis/issues/4685
"""
a = FrameAnalysis(u.trajectory)
with pytest.warns(UserWarning, match="Set `n_parts` to"):
a.run(
backend="multiprocessing", start=0, stop=1, n_workers=2, n_parts=2
)
@pytest.mark.parametrize(
"run_kwargs,frames",
[
({}, np.arange(98)),
({"start": 20}, np.arange(20, 98)),
({"stop": 30}, np.arange(30)),
({"step": 10}, np.arange(0, 98, 10)),
],
)
def test_start_stop_step(u, run_kwargs, frames):
an = FrameAnalysis(u.trajectory).run(**run_kwargs)
assert an.n_frames == len(frames)
assert_equal(an.found_frames, frames)
assert_equal(an.frames, frames, err_msg=FRAMES_ERR)
assert_allclose(
an.times, frames + 1, rtol=0, atol=1.5e-4, err_msg=TIMES_ERR
)
@pytest.mark.parametrize(
"run_kwargs, frames",
[
({"frames": [4, 5, 6, 7, 8, 9]}, np.arange(4, 10)),
({"frames": [0, 2, 4, 6, 8]}, np.arange(0, 10, 2)),
({"frames": [4, 6, 8]}, np.arange(4, 10, 2)),
({"frames": [0, 3, 4, 3, 5]}, [0, 3, 4, 3, 5]),
(
{
"frames": [
True,
True,
False,
True,
False,
True,
True,
False,
True,
False,
]
},
(0, 1, 3, 5, 6, 8),
),
],
)
def test_frame_slice(u_xtc, run_kwargs, frames):
an = FrameAnalysis(u_xtc.trajectory).run(**run_kwargs)
assert an.n_frames == len(frames)
assert_equal(an.found_frames, frames)
assert_equal(an.frames, frames, err_msg=FRAMES_ERR)
@pytest.mark.parametrize(
"run_kwargs, frames",
[
({"frames": [4, 5, 6, 7, 8, 9]}, np.arange(4, 10)),
({"frames": [0, 2, 4, 6, 8]}, np.arange(0, 10, 2)),
({"frames": [4, 6, 8]}, np.arange(4, 10, 2)),
({"frames": [0, 3, 4, 3, 5]}, [0, 3, 4, 3, 5]),
(
{
"frames": [
True,
True,
False,
True,
False,
True,
True,
False,
True,
False,
]
},
(0, 1, 3, 5, 6, 8),
),
],
)
def test_frame_slice_parallel(run_kwargs, frames, client_FrameAnalysis):
u = mda.Universe(TPR, XTC) # dt = 100
an = FrameAnalysis(u.trajectory).run(**run_kwargs, **client_FrameAnalysis)
assert an.n_frames == len(frames)
assert_equal(an.found_frames, frames)
assert_equal(an.frames, frames, err_msg=FRAMES_ERR)
@pytest.mark.parametrize(
"run_kwargs",
[
({"start": 4, "frames": [4, 5, 6, 7, 8, 9]}),
({"stop": 6, "frames": [0, 1, 2, 3, 4, 5]}),
({"step": 2, "frames": [0, 2, 4, 6, 8]}),
({"start": 4, "stop": 7, "frames": [4, 5, 6]}),
({"stop": 6, "step": 2, "frames": [0, 2, 4, 6]}),
({"start": 4, "step": 2, "frames": [4, 6, 8]}),
({"start": 0, "stop": 0, "step": 0, "frames": [4, 6, 8]}),
],
)
def test_frame_fail(u, run_kwargs, client_FrameAnalysis):
an = FrameAnalysis(u.trajectory)
msg = "start/stop/step cannot be combined with frames"
with pytest.raises(ValueError, match=msg):
an.run(**client_FrameAnalysis, **run_kwargs)
def test_parallelizable_transformations():
# pick any transformation that would allow
# for parallelizable attribute
from MDAnalysis.transformations import NoJump
u = mda.Universe(XTC, to_guess=())
u.trajectory.add_transformations(NoJump())
# test that serial works
FrameAnalysis(u.trajectory).run()
# test that parallel fails
with pytest.raises(ValueError):
FrameAnalysis(u.trajectory).run(backend="multiprocessing")
def test_instance_serial_backend(u):
# test that isinstance is checked and the correct ValueError raise appears
msg = "Can not display progressbar with non-serial backend"
with pytest.raises(ValueError, match=msg):
FrameAnalysis(u.trajectory).run(
backend=backends.BackendMultiprocessing(n_workers=2),
verbose=True,
progressbar_kwargs={"leave": True},
unsupported_backend=True,
)
def test_frame_bool_fail(client_FrameAnalysis):
u = mda.Universe(TPR, XTC) # dt = 100
an = FrameAnalysis(u.trajectory)
frames = [True, True, False]
msg = "boolean index did not match indexed array along (axis|dimension) 0"
with pytest.raises(IndexError, match=msg):
an.run(**client_FrameAnalysis, frames=frames)
def test_rewind(client_FrameAnalysis):
u = mda.Universe(TPR, XTC) # dt = 100
an = FrameAnalysis(u.trajectory).run(
**client_FrameAnalysis, frames=[0, 2, 3, 5, 9]
)
assert_equal(u.trajectory.ts.frame, 0)
def test_frames_times(client_FrameAnalysis):
u = mda.Universe(TPR, XTC) # dt = 100
an = FrameAnalysis(u.trajectory).run(
start=1, stop=8, step=2, **client_FrameAnalysis
)
frames = np.array([1, 3, 5, 7])
assert an.n_frames == len(frames)
assert_equal(an.found_frames, frames)
assert_equal(an.frames, frames, err_msg=FRAMES_ERR)
assert_allclose(
an.times, frames * 100, rtol=0, atol=1.5e-4, err_msg=TIMES_ERR
)
def test_verbose(u):
a = FrameAnalysis(u.trajectory, verbose=True)
assert a._verbose
def test_warn_nparts_nworkers(u):
a = FrameAnalysis(u.trajectory)
with pytest.warns(UserWarning):
a.run(backend="multiprocessing", n_workers=3, n_parts=2)
@pytest.mark.parametrize(
"classname,is_parallelizable",
[
(base.AnalysisBase, False),
(base.AnalysisFromFunction, True),
(FrameAnalysis, True),
],
)
def test_not_parallelizable(u, classname, is_parallelizable):
assert classname._analysis_algorithm_is_parallelizable == is_parallelizable
def test_verbose_progressbar(u, capsys):
FrameAnalysis(u.trajectory).run()
_, err = capsys.readouterr()
expected = ""
actual = err.strip().split("\r")[-1]
assert actual == expected
def test_verbose_progressbar_run(u, capsys):
FrameAnalysis(u.trajectory).run(verbose=True)
_, err = capsys.readouterr()
expected = "100%|██████████"
actual = err.strip().split("\r")[-1]
assert actual[:15] == expected
def test_verbose_progressbar_run_with_kwargs(u, capsys):
FrameAnalysis(u.trajectory).run(
verbose=True, progressbar_kwargs={"desc": "custom"}
)
_, err = capsys.readouterr()
expected = "custom: 100%|██████████"
actual = err.strip().split("\r")[-1]
assert actual[:23] == expected
def test_progressbar_multiprocessing(u):
with pytest.raises(ValueError):
FrameAnalysis(u.trajectory).run(
backend="multiprocessing", verbose=True
)
def test_incomplete_defined_analysis(u):
with pytest.raises(NotImplementedError):
IncompleteAnalysis(u.trajectory).run()
def test_old_api(u):
OldAPIAnalysis(u.trajectory).run()
def test_filter_baseanalysis_kwargs_VE():
def bad_f(mobile, verbose=2):
pass
kwargs = {"step": 3, "foo": None}
with pytest.raises(ValueError):
base._filter_baseanalysis_kwargs(bad_f, kwargs)
def test_filter_baseanalysis_kwargs():
def good_f(mobile, ref):
pass
kwargs = {"step": 3, "foo": None}
base_kwargs, kwargs = base._filter_baseanalysis_kwargs(good_f, kwargs)
assert 2 == len(kwargs)
assert kwargs["foo"] == None
assert len(base_kwargs) == 1
assert base_kwargs["verbose"] is False
def simple_function(mobile):
return mobile.center_of_geometry()
def test_results_type(u):
an = FrameAnalysis(u.trajectory)
assert type(an.results) == base.Results
@pytest.mark.parametrize(
"start, stop, step, nframes",
[
(None, None, 2, 49),
(None, 50, 2, 25),
(20, 50, 2, 15),
(20, 50, None, 30),
],
)
def test_AnalysisFromFunction(
u, start, stop, step, nframes, client_AnalysisFromFunction
):
# client_AnalysisFromFunction is defined [here](testsuite/MDAnalysisTests/analysis/conftest.py),
# and determines a set of parameters ('backend', 'n_workers'), taking only backends
# that are implemented for a given subclass, to run the test against.
ana1 = base.AnalysisFromFunction(simple_function, mobile=u.atoms)
ana1.run(start=start, stop=stop, step=step, **client_AnalysisFromFunction)
ana2 = base.AnalysisFromFunction(simple_function, u.atoms)
ana2.run(start=start, stop=stop, step=step, **client_AnalysisFromFunction)
ana3 = base.AnalysisFromFunction(simple_function, u.trajectory, u.atoms)
ana3.run(start=start, stop=stop, step=step, **client_AnalysisFromFunction)
frames = []
times = []
timeseries = []
for ts in u.trajectory[start:stop:step]:
frames.append(ts.frame)
times.append(ts.time)
timeseries.append(simple_function(u.atoms))
frames = np.asarray(frames)
times = np.asarray(times)
timeseries = np.asarray(timeseries)
assert np.size(timeseries, 0) == nframes
for ana in (ana1, ana2, ana3):
assert_equal(frames, ana.results.frames)
assert_equal(times, ana.results.times)
assert_equal(timeseries, ana.results.timeseries)
def mass_xyz(atomgroup1, atomgroup2, masses):
return atomgroup1.positions * masses
def test_AnalysisFromFunction_args_content(u, client_AnalysisFromFunction):
protein = u.select_atoms("protein")
masses = protein.masses.reshape(-1, 1)
another = mda.Universe(TPR, XTC).select_atoms("protein")
ans = base.AnalysisFromFunction(mass_xyz, protein, another, masses)
assert len(ans.args) == 3
result = np.sum(ans.run(**client_AnalysisFromFunction).results.timeseries)
assert_allclose(result, -317054.67757345125, rtol=0, atol=1.5e-6)
assert_allclose(result, -317054.67757345125, rtol=0, atol=1e-6)
assert (ans.args[0] is protein) and (ans.args[1] is another)
assert ans._trajectory is protein.universe.trajectory
def test_analysis_class(client_AnalysisFromFunctionAnalysisClass):
ana_class = base.analysis_class(simple_function)
assert issubclass(ana_class, base.AnalysisBase)
assert issubclass(ana_class, base.AnalysisFromFunction)
u = mda.Universe(PSF, DCD)
step = 2
ana = ana_class(u.atoms).run(
step=step, **client_AnalysisFromFunctionAnalysisClass
)
results = []
for ts in u.trajectory[::step]:
results.append(simple_function(u.atoms))
results = np.asarray(results)
assert_equal(results, ana.results.timeseries)
with pytest.raises(ValueError):
ana_class(2)
def test_analysis_class_decorator():
# Issue #1511
# analysis_class should not raise
# a DeprecationWarning
u = mda.Universe(PSF, DCD)
def distance(a, b):
return np.linalg.norm((a.centroid() - b.centroid()))
Distances = base.analysis_class(distance)
with no_deprecated_call():
d = Distances(u.atoms[:10], u.atoms[10:20]).run()
|