Skip to content

Commit

Permalink
Fix doctest for MDAnalysis.analysis.psa (MDAnalysis#4035)
Browse files Browse the repository at this point in the history
* Updated imports
* Moved to Python 3 compatible code
* Updated reported numerical results
  • Loading branch information
ooprathamm authored Mar 3, 2023
1 parent 79e0c86 commit 2cad39e
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions package/MDAnalysis/analysis/psa.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,20 +398,23 @@ def hausdorff(P, Q):
-------
Calculate the Hausdorff distance between two halves of a trajectory:
>>> import MDAnalysis as mda
>>> import numpy
>>> from MDAnalysis.tests.datafiles import PSF, DCD
>>> u = Universe(PSF,DCD)
>>> mid = len(u.trajectory)/2
>>> from MDAnalysis.analysis import psa
>>> u = mda.Universe(PSF,DCD)
>>> mid = int(len(u.trajectory)/2)
>>> ca = u.select_atoms('name CA')
>>> P = numpy.array([
... ca.positions for _ in u.trajectory[:mid:]
... ]) # first half of trajectory
>>> Q = numpy.array([
... ca.positions for _ in u.trajectory[mid::]
... ]) # second half of trajectory
>>> hausdorff(P,Q)
4.7786639840135905
>>> hausdorff(P,Q[::-1]) # hausdorff distance w/ reversed 2nd trajectory
4.7786639840135905
>>> psa.hausdorff(P,Q)
4.778663899862152
>>> psa.hausdorff(P,Q[::-1]) # hausdorff distance w/ reversed 2nd trajectory
4.778663899862152
Notes
Expand Down Expand Up @@ -470,20 +473,22 @@ def hausdorff_wavg(P, Q):
Example
-------
>>> import MDAnalysis as mda
>>> from MDAnalysis import Universe
>>> from MDAnalysis.tests.datafiles import PSF, DCD
>>> u = Universe(PSF,DCD)
>>> mid = len(u.trajectory)/2
>>> from MDAnalysis.analysis import psa
>>> u = mda.Universe(PSF,DCD)
>>> mid = int(len(u.trajectory)/2)
>>> ca = u.select_atoms('name CA')
>>> P = numpy.array([
... ca.positions for _ in u.trajectory[:mid:]
... ]) # first half of trajectory
>>> Q = numpy.array([
... ca.positions for _ in u.trajectory[mid::]
... ]) # second half of trajectory
>>> hausdorff_wavg(P,Q)
>>> psa.hausdorff_wavg(P,Q)
2.5669644353703447
>>> hausdorff_wavg(P,Q[::-1]) # weighted avg hausdorff dist w/ Q reversed
>>> psa.hausdorff_wavg(P,Q[::-1]) # weighted avg hausdorff dist w/ Q reversed
2.5669644353703447
Notes
Expand Down Expand Up @@ -527,19 +532,21 @@ def hausdorff_avg(P, Q):
Example
-------
>>> import MDAnalysis as mda
>>> from MDAnalysis.tests.datafiles import PSF, DCD
>>> u = Universe(PSF,DCD)
>>> mid = len(u.trajectory)/2
>>> from MDAnalysis.analysis import psa
>>> u = mda.Universe(PSF,DCD)
>>> mid = int(len(u.trajectory)/2)
>>> ca = u.select_atoms('name CA')
>>> P = numpy.array([
... ca.positions for _ in u.trajectory[:mid:]
... ]) # first half of trajectory
>>> Q = numpy.array([
... ca.positions for _ in u.trajectory[mid::]
... ]) # second half of trajectory
>>> hausdorff_avg(P,Q)
>>> psa.hausdorff_avg(P,Q)
2.5669646575869005
>>> hausdorff_avg(P,Q[::-1]) # hausdorff distance w/ reversed 2nd trajectory
>>> psa.hausdorff_avg(P,Q[::-1]) # hausdorff distance w/ reversed 2nd trajectory
2.5669646575869005
Expand Down Expand Up @@ -623,19 +630,23 @@ def discrete_frechet(P, Q):
Calculate the discrete Fréchet distance between two halves of a
trajectory.
>>> u = Universe(PSF,DCD)
>>> mid = len(u.trajectory)/2
>>> import MDAnalysis as mda
>>> import numpy as np
>>> from MDAnalysis.tests.datafiles import PSF, DCD
>>> from MDAnalysis.analysis import psa
>>> u = mda.Universe(PSF,DCD)
>>> mid = int(len(u.trajectory)/2)
>>> ca = u.select_atoms('name CA')
>>> P = np.array([
... ca.positions for _ in u.trajectory[:mid:]
... ]) # first half of trajectory
>>> Q = np.array([
... ca.positions for _ in u.trajectory[mid::]
... ]) # second half of trajectory
>>> discrete_frechet(P,Q)
4.7786639840135905
>>> discrete_frechet(P,Q[::-1]) # frechet distance w/ 2nd trj reversed 2nd
6.8429011177113832
>>> psa.discrete_frechet(P,Q)
4.778663984013591
>>> psa.discrete_frechet(P,Q[::-1]) # frechet distance w/ 2nd trj reversed 2nd
6.842901117711383
Note that reversing the direction increased the Fréchet distance:
it is sensitive to the direction of the path.
Expand Down

0 comments on commit 2cad39e

Please sign in to comment.