Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fmt] Format after PR#4800 #4890

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 44 additions & 27 deletions package/MDAnalysis/coordinates/XDR.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from ..lib.util import store_init_arguments


def offsets_filename(filename, ending='npz'):
def offsets_filename(filename, ending="npz"):
"""Return offset or its lock filename for XDR files.
For this the filename is appended
with `_offsets.{ending}`.
Expand All @@ -63,7 +63,7 @@ def offsets_filename(filename, ending='npz'):

"""
head, tail = split(filename)
return join(head, f'.{tail}_offsets.{ending}')
return join(head, f".{tail}_offsets.{ending}")


def read_numpy_offsets(filename):
Expand All @@ -90,6 +90,7 @@ def read_numpy_offsets(filename):
warnings.warn(f"Failed to load offsets file {filename}\n")
return False


class XDRBaseReader(base.ReaderBase):
"""Base class for libmdaxdr file formats xtc and trr

Expand Down Expand Up @@ -124,9 +125,16 @@ class XDRBaseReader(base.ReaderBase):
.. versionchanged:: 2.9.0
Changed fasteners.InterProcessLock() to filelock.FileLock
"""

@store_init_arguments
def __init__(self, filename, convert_units=True, sub=None,
refresh_offsets=False, **kwargs):
def __init__(
self,
filename,
convert_units=True,
sub=None,
refresh_offsets=False,
**kwargs,
):
"""
Parameters
----------
Expand All @@ -144,9 +152,9 @@ def __init__(self, filename, convert_units=True, sub=None,
General reader arguments.

"""
super(XDRBaseReader, self).__init__(filename,
convert_units=convert_units,
**kwargs)
super(XDRBaseReader, self).__init__(
filename, convert_units=convert_units, **kwargs
)
self._xdr = self._file(self.filename)

self._sub = sub
Expand Down Expand Up @@ -192,17 +200,18 @@ def _load_offsets(self):
fails. To prevent the competition of generating the same offset file
from multiple processes, an `InterProcessLock` is used."""
fname = offsets_filename(self.filename)
lock_name = offsets_filename(self.filename,
ending='lock')
lock_name = offsets_filename(self.filename, ending="lock")

# check if the location of the lock is writable.
try:
with FileLock(lock_name) as filelock:
pass
except OSError as e:
if isinstance(e, PermissionError) or e.errno == errno.EROFS:
warnings.warn(f"Cannot write lock/offset file in same location as "
f"{self.filename}. Using slow offset calculation.")
warnings.warn(
f"Cannot write lock/offset file in same location as "
f"{self.filename}. Using slow offset calculation."
)
self._read_offsets(store=False)
return
else:
Expand All @@ -221,29 +230,33 @@ def _load_offsets(self):
# refer to Issue #1893
data = read_numpy_offsets(fname)
if not data:
warnings.warn(f"Reading offsets from {fname} failed, "
"reading offsets from trajectory instead.\n"
"Consider setting 'refresh_offsets=True' "
"when loading your Universe.")
warnings.warn(
f"Reading offsets from {fname} failed, "
"reading offsets from trajectory instead.\n"
"Consider setting 'refresh_offsets=True' "
"when loading your Universe."
)
self._read_offsets(store=True)
return

ctime_ok = size_ok = n_atoms_ok = False

try:
ctime_ok = getctime(self.filename) == data['ctime']
size_ok = getsize(self.filename) == data['size']
n_atoms_ok = self._xdr.n_atoms == data['n_atoms']
ctime_ok = getctime(self.filename) == data["ctime"]
size_ok = getsize(self.filename) == data["size"]
n_atoms_ok = self._xdr.n_atoms == data["n_atoms"]
except KeyError:
# we tripped over some old offset formated file
pass

if not (ctime_ok and size_ok and n_atoms_ok):
warnings.warn("Reload offsets from trajectory\n "
"ctime or size or n_atoms did not match")
warnings.warn(
"Reload offsets from trajectory\n "
"ctime or size or n_atoms did not match"
)
self._read_offsets(store=True)
else:
self._xdr.set_offsets(data['offsets'])
self._xdr.set_offsets(data["offsets"])

def _read_offsets(self, store=False):
"""read frame offsets from trajectory"""
Expand All @@ -253,9 +266,13 @@ def _read_offsets(self, store=False):
ctime = getctime(self.filename)
size = getsize(self.filename)
try:
np.savez(fname,
offsets=offsets, size=size, ctime=ctime,
n_atoms=self._xdr.n_atoms)
np.savez(
fname,
offsets=offsets,
size=size,
ctime=ctime,
n_atoms=self._xdr.n_atoms,
)
except Exception as e:
warnings.warn(f"Couldn't save offsets because: {e}")

Expand All @@ -270,7 +287,7 @@ def _reopen(self):
self._frame = -1
offsets = self._xdr.offsets.copy()
self._xdr.close()
self._xdr.open(self.filename.encode('utf-8'), 'r')
self._xdr.open(self.filename.encode("utf-8"), "r")
# only restore in case we actually had offsets
if len(offsets) != 0:
self._xdr.set_offsets(offsets)
Expand All @@ -282,7 +299,7 @@ def _read_frame(self, i):
self._xdr.seek(i)
timestep = self._read_next_timestep()
except IOError:
warnings.warn('seek failed, recalculating offsets and retrying')
warnings.warn("seek failed, recalculating offsets and retrying")
offsets = self._xdr.calc_offsets()
self._xdr.set_offsets(offsets)
self._read_offsets(store=True)
Expand Down Expand Up @@ -316,7 +333,7 @@ def __init__(self, filename, n_atoms, convert_units=True, **kwargs):
self.filename = filename
self._convert_units = convert_units
self.n_atoms = n_atoms
self._xdr = self._file(self.filename, 'w')
self._xdr = self._file(self.filename, "w")

def close(self):
"""close trajectory"""
Expand Down
1 change: 0 additions & 1 deletion package/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ __pycache__
| MDAnalysis/analysis/atomicdistances\.py
| MDAnalysis/topology/CMSParser\.py
| MDAnalysis/topology/__init__\.py
| MDAnalysis/coordinates/XDR\.py
| MDAnalysis/core/selection\.py
| MDAnalysis/analysis/diffusionmap\.py
| MDAnalysis/analysis/align\.py
Expand Down
12 changes: 6 additions & 6 deletions testsuite/MDAnalysisTests/coordinates/test_xdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,10 +984,12 @@ def test_persistent_offsets_readonly(self, tmpdir, trajectory):
shutil.copy(self.filename, str(tmpdir))

filename = str(tmpdir.join(os.path.basename(self.filename)))
print('filename', filename)
print("filename", filename)
ref_offset = trajectory._xdr.offsets
# Mock filelock acquire to raise an error
with patch.object(FileLock, "acquire", side_effect=PermissionError): # Simulate failure
with patch.object(
FileLock, "acquire", side_effect=PermissionError
): # Simulate failure
with pytest.warns(UserWarning, match="Cannot write lock"):
reader = self._reader(filename)
saved_offsets = reader._xdr.offsets
Expand All @@ -1008,12 +1010,10 @@ def test_persistent_offsets_readonly(self, tmpdir, trajectory):

@pytest.mark.skipif(
sys.platform.startswith("win"),
reason="The lock file only exists when it's locked in windows"
reason="The lock file only exists when it's locked in windows",
)
def test_offset_lock_created(self, traj):
assert os.path.exists(
XDR.offsets_filename(traj, ending="lock")
)
assert os.path.exists(XDR.offsets_filename(traj, ending="lock"))


class TestXTCReader_offsets(_GromacsReader_offsets):
Expand Down
1 change: 0 additions & 1 deletion testsuite/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ extend-exclude = '''
__pycache__
| testsuite/MDAnalysisTests/core/test_atomselections\.py
| testsuite/MDAnalysisTests/analysis/test_atomicdistances\.py
| testsuite/MDAnalysisTests/coordinates/test_xdr\.py
| testsuite/MDAnalysisTests/core/test_atomselections\.py
| testsuite/MDAnalysisTests/datafiles\.py
| testsuite/MDAnalysisTests/analysis/conftest\.py
Expand Down
Loading