Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcello-Sega committed Dec 1, 2024
1 parent 7d4f52f commit 8e563b4
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 51 deletions.
8 changes: 5 additions & 3 deletions pytim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
from . import observables, utilities, datafiles
from .version import __version__
import warnings
from .patches import patchMDTRAJ_ReplacementTables

patchMDTRAJ_ReplacementTables()
try: # waiting for numpy2 support in mdtraj>=1.10.2
from .patches import patchMDTRAJ_ReplacementTables
patchMDTRAJ_ReplacementTables()
except:
pass

warnings.filterwarnings(
"ignore",
Expand Down
18 changes: 10 additions & 8 deletions pytim/examples/example_mdtraj.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
loaded with MDTraj (http://mdtraj.org/)
(see also the openmm interoperability)
"""
try:
import mdtraj
import pytim
from pytim.datafiles import WATER_GRO, WATER_XTC

import mdtraj
import pytim
from pytim.datafiles import WATER_GRO, WATER_XTC

t = mdtraj.load_xtc(WATER_XTC, top=WATER_GRO)
inter = pytim.ITIM(t)
for step in t[:]:
print("surface atoms: "+repr(inter.atoms.indices))
t = mdtraj.load_xtc(WATER_XTC, top=WATER_GRO)
inter = pytim.ITIM(t)
for step in t[:]:
print("surface atoms: "+repr(inter.atoms.indices))
except ModuleNotFoundError:
print('Mdtraj not installed')
3 changes: 3 additions & 0 deletions pytim/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,10 @@ def _():
>>> # mdtraj
>>> try:
... from packaging.version import Version
... import mdtraj
... if Version(mdtraj.__version__) < Version('1.10.2'): # numpy2 support
... pass
... try:
... import numpy as np
... import MDAnalysis as mda
Expand Down
13 changes: 8 additions & 5 deletions pytim/itim.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,14 @@ class ITIM(Interface):
>>> # pytim can be used also on top of mdtraj (MDAnalysis must be present,though)
>>> import mdtraj
>>> import pytim
>>> from pytim.datafiles import WATER_GRO, WATER_XTC
>>> t = mdtraj.load_xtc(WATER_XTC,top=WATER_GRO)
>>> inter = pytim.ITIM(t)
>>> try:
... import mdtraj
... import pytim
... from pytim.datafiles import WATER_GRO, WATER_XTC
... t = mdtraj.load_xtc(WATER_XTC,top=WATER_GRO)
... inter = pytim.ITIM(t)
... except ModuleNotFoundError:
... pass
.. _MDAnalysis: http://www.mdanalysis.org/
Expand Down
4 changes: 2 additions & 2 deletions pytim/observables/basic_observables.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ class Distance(Observable):
>>> u = mda.Universe(pytim.datafiles.WATER_GRO)
>>> d1 = pytim.observables.Distance().compute(u.atoms[:9],u.atoms[:9])
>>> d2 = pytim.observables.RelativePosition(spherical=True).compute(u.atoms[:9],u.atoms[:9])[:,0]
>>> np.all(np.isclose(d1,d2))
>>> all(np.isclose(d1,d2))
True
>>> d1 = pytim.observables.Distance('xy').compute(u.atoms[:9],u.atoms[:9])
>>> d2 = pytim.observables.RelativePosition('xy',spherical=True).compute(u.atoms[:9],u.atoms[:9])[:,0]
>>> np.all(np.isclose(d1,d2))
>>> all(np.isclose(d1,d2))
True
"""
Expand Down
10 changes: 5 additions & 5 deletions pytim/observables/contactangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ class ContactAngle(object):
>>> for ts in u.trajectory[::]:
... CA.sample()
>>> # Instantaneous contact angle (last frame) by fitting a circle...
>>> np.round(CA.contact_angle,2)
>>> print(np.round(CA.contact_angle,2))
90.58
>>>
>>> # ... and using an elliptical fit:
>>> left, right = CA.contact_angles
>>> # left angle
>>> np.round(np.abs(left),2)
>>> print(np.round(np.abs(left),2))
79.95
>>> # right angle
>>> np.round(right,2)
>>> print(np.round(right,2))
83.84
>>> # Contact angles from the averaged binned statistics of
>>> # surface atoms' radial distance as a function of the azimuthal angle
>>> list(np.round(CA.mean_contact_angles,2))
[96.2, 100.68]
>>> np.round(CA.mean_contact_angles,2).tolist()
[96.23, 100.74]
"""

Expand Down
10 changes: 5 additions & 5 deletions pytim/observables/correlator.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def sample(self, group):
RuntimeError(
'Cannot compute survival probability without a reference')
sampled = self.observable.compute(group)
self.timeseries.append(list(sampled.flatten()))
self.timeseries.append(sampled.flatten().tolist())

if self.shape is None:
self.shape = sampled.shape
Expand All @@ -149,13 +149,13 @@ def _sample_intermittent(self, group):
# the residence function (1 if in the reference group, 0 otherwise)
mask = np.isin(self.reference, group)
# append the residence function to its timeseries
self.maskseries.append(list(mask))
self.maskseries.append(mask.tolist())
if self.observable is not None:
# this copies a vector of zeros with the correct shape
sampled = self.reference_obs.copy()
obs = self.observable.compute(group)
sampled[np.where(mask)] = obs
self.timeseries.append(list(sampled.flatten()))
self.timeseries.append(sampled.flatten().tolist())
else:
self.timeseries = self.maskseries
if self.shape is None:
Expand Down Expand Up @@ -249,7 +249,7 @@ def correlation(self, normalized=True, continuous=True):
>>> print (np.allclose(corr, [ c0, c1, c2, c3]))
True
>>> # check normalization
>>> np.all(vv.correlation(continuous=False) == corr/corr[0])
>>> print(np.all(vv.correlation(continuous=False) == corr/corr[0]))
True
>>> # not normalizd, continuous
>>> corr = vv.correlation(normalized=False,continuous=True)
Expand All @@ -258,7 +258,7 @@ def correlation(self, normalized=True, continuous=True):
>>> print (np.allclose(corr, [ c0, c1, c2, c3]))
True
>>> # check normalization
>>> np.all(vv.correlation(continuous=True) == corr/corr[0])
>>> print(np.all(vv.correlation(continuous=True) == corr/corr[0]))
True
"""
Expand Down
2 changes: 1 addition & 1 deletion pytim/observables/distributionfunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def sample(self, g1=None, g2=None, kargs1=None, kargs2=None):
self.count += count

box = self.universe.dimensions
self.volume += np.product(box[:3])
self.volume += np.prod(box[:3])
if self.g2 is None or len(self.g2) == 0:
self.n_normalize += len(self.g1)
else:
Expand Down
4 changes: 2 additions & 2 deletions pytim/observables/free_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class FreeVolume(object):
>>> FV = pytim.observables.FreeVolume(u,npoints = nsamples)
>>> np.random.seed(1) # ensure reproducibility of test
>>> free, err = FV.compute()
>>> np.isclose(free,1.0-0.6802,rtol=1e-3)
>>> print(np.isclose(free,1.0-0.6802,rtol=1e-3))
True
>>> np.random.seed(1) # ensure reproducibility of test
>>> lst, _ = FV._compute()
>>> np.isclose(free,1.0-len(lst)*1.0/nsamples, rtol=1e-6)
>>> print(np.isclose(free,1.0-len(lst)*1.0/nsamples, rtol=1e-6))
True
"""
Expand Down
2 changes: 1 addition & 1 deletion pytim/observables/orientation.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def compute(self, inp, **kargs):
>>> condition = np.logical_and(u.atoms.sides==0,u.atoms.layers==1)
>>> group = u.atoms[condition]
>>> costheta, phi = biv.compute(group)
>>> np.all(np.isclose([costheta[0],phi[0]], [0.6533759236335754, 0.10778185716460659]))
>>> print(all(np.isclose([costheta[0],phi[0]], [0.6533759236335754, 0.10778185716460659])))
True
"""

Expand Down
21 changes: 11 additions & 10 deletions pytim/observables/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,18 +311,19 @@ def _():
>>> inter = pytim.ITIM(u,cluster_cut=3.5,alpha=2.5)
>>> print(inter.normal)
2
>>> np.set_printoptions(precision=8)
>>> np.random.seed(1) # for the MC normalization
>>> stdprof = pytim.observables.Profile()
>>> stdprof.sample(u.atoms)
>>> print(stdprof.get_values(binwidth=0.5)[2][:6])
[0.09229169 0.10959639 0.08075523 0.10959639 0.09805993 0.09805993]
>>> vals = stdprof.get_values(binwidth=0.5)[2]
>>> print(np.around(vals[:6],decimals=3))
[0.092 0.11 0.081 0.11 0.098 0.098]
>>> prof = pytim.observables.Profile(interface=inter)
>>> prof.sample(u.atoms)
>>> vals = prof.get_values(binwidth=0.5)[2]
>>> print(vals[len(vals)//2-3:len(vals)//2+3])
[0.07344066 0.04300743 0.02803522 inf 0. 0. ]
>>> print(np.around(vals[len(vals)//2-3:len(vals)//2+3],decimals=6))
[0.073441 0.043007 0.028035 inf 0. 0. ]
Expand Down Expand Up @@ -353,15 +354,15 @@ def _():
>>> np.random.seed(1) # for the MC normalization
>>> stdprof = pytim.observables.Profile()
>>> stdprof.sample(u.atoms)
>>> print(stdprof.get_values(binwidth=0.5)[2][:6])
[0.09229169 0.10959639 0.08075523 0.10959639 0.09805993 0.09805993]
>>> vals = stdprof.get_values(binwidth=0.5)[2]
>>> print(np.around(vals[:6],decimals=6))
[0.092292 0.109596 0.080755 0.109596 0.09806 0.09806 ]
>>> prof = pytim.observables.Profile(interface=inter)
>>> prof.sample(u.atoms)
>>> vals = prof.get_values(binwidth=1.0)[2]
>>> print(vals[len(vals)//2-4:len(vals)//2+2])
[0.09554818 0.09796541 0.05555127 0. inf 0. ]
>>> print(np.around(vals[len(vals)//2-4:len(vals)//2+2],decimals=6))
[0.095548 0.097965 0.055551 0. inf 0. ]
"""

Expand Down
15 changes: 10 additions & 5 deletions pytim/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def patchTrajectory(trajectory, interface):
this patch makes the layer assignement being automatically
called whenever a new frame is loaded.
"""
from importlib.metadata import version
if int(version('numpy').split('.')[0])<2 : return
try:
trajectory.interface
trajectory.interface = interface
Expand Down Expand Up @@ -97,11 +99,14 @@ def patchMDTRAJ(trajectory, universe):
Example:
>>> import mdtraj
>>> import pytim
>>> from pytim.datafiles import WATER_GRO, WATER_XTC
>>> t = mdtraj.load_xtc(WATER_XTC,top=WATER_GRO)
>>> inter = pytim.ITIM(t)
>>> try:
... import mdtraj
... import pytim
... from pytim.datafiles import WATER_GRO, WATER_XTC
... t = mdtraj.load_xtc(WATER_XTC,top=WATER_GRO)
... inter = pytim.ITIM(t)
... except:
... pass
"""
Expand Down
2 changes: 1 addition & 1 deletion pytim/utilities_dbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _():
>>> print (np.sort(c2)[-2:])
[ 0 9335]
>>> print ((np.all(c1==c2), np.all(l1==l2)))
>>> print ((all(c1==c2), all(l1==l2)))
(True, True)
"""
Expand Down
4 changes: 2 additions & 2 deletions pytim/utilities_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ def polygonalArea(points):
>>> s1 = np.sin(2*np.pi/5.) ; s2 = np.sin(4*np.pi/5.)
>>> pentagon = np.array([[1,0,0],[c1,s1,0],[-c2,s2,0],[-c2,-s2,0],[c1,-s1,0]])
>>> A = 0.25 * np.sqrt(25+10*np.sqrt(5)) * 100./ (50+10*np.sqrt(5))
>>> np.isclose(pytim.utilities.polygonalArea(pentagon),A)
>>> print(np.isclose(pytim.utilities.polygonalArea(pentagon),A))
True
>>> # now let's rotate it:
>>> rotated = np.dot(EulerRotation(0,np.pi/2.,0),pentagon.T).T
>>> np.isclose(pytim.utilities.polygonalArea(rotated),A)
>>> print(np.isclose(pytim.utilities.polygonalArea(rotated),A))
True
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements.testing.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mdtraj
pytest==8.1.1
codecov==2.1.13
pytest-cov==5.0.0
packaging

0 comments on commit 8e563b4

Please sign in to comment.