From f260c4a8d5aa79df7746c0db5f32ad539afbb452 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Wed, 22 Jan 2025 11:49:07 -0700 Subject: [PATCH 1/2] [CI] switch off autosyncing of labels (#4895) close #4840 --- .github/workflows/labels.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/labels.yaml b/.github/workflows/labels.yaml index 503be1ee03b..094bf567ff2 100644 --- a/.github/workflows/labels.yaml +++ b/.github/workflows/labels.yaml @@ -14,4 +14,4 @@ jobs: with: repo-token: "${{ secrets.GITHUB_TOKEN }}" configuration-path: ".github/pr_path_labeler.yml" - sync-labels: true # remove labels if file not modified anymore + sync-labels: false # remove labels if file not modified anymore From d2227208396789feac364c2f058d67f3fccfc5bb Mon Sep 17 00:00:00 2001 From: Rocco Meli Date: Sun, 26 Jan 2025 22:07:08 +0100 Subject: [PATCH 2/2] [fmt] format after PR#4800 (#4890) --- package/MDAnalysis/coordinates/XDR.py | 71 ++++++++++++------- package/pyproject.toml | 1 - .../MDAnalysisTests/coordinates/test_xdr.py | 12 ++-- testsuite/pyproject.toml | 1 - 4 files changed, 50 insertions(+), 35 deletions(-) diff --git a/package/MDAnalysis/coordinates/XDR.py b/package/MDAnalysis/coordinates/XDR.py index bf036e34e51..fab953bbeca 100644 --- a/package/MDAnalysis/coordinates/XDR.py +++ b/package/MDAnalysis/coordinates/XDR.py @@ -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}`. @@ -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): @@ -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 @@ -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 ---------- @@ -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 @@ -192,8 +200,7 @@ 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: @@ -201,8 +208,10 @@ def _load_offsets(self): 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: @@ -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""" @@ -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}") @@ -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) @@ -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) @@ -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""" diff --git a/package/pyproject.toml b/package/pyproject.toml index 238c09d0739..8d0e6a86bcd 100644 --- a/package/pyproject.toml +++ b/package/pyproject.toml @@ -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 diff --git a/testsuite/MDAnalysisTests/coordinates/test_xdr.py b/testsuite/MDAnalysisTests/coordinates/test_xdr.py index 2ca76580fcc..c2c7a0b2179 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_xdr.py +++ b/testsuite/MDAnalysisTests/coordinates/test_xdr.py @@ -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 @@ -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): diff --git a/testsuite/pyproject.toml b/testsuite/pyproject.toml index c3529a7bd63..b5bbda77af2 100644 --- a/testsuite/pyproject.toml +++ b/testsuite/pyproject.toml @@ -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