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

ENH: support pathlib in ReaderBase #3935

Merged
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
6 changes: 5 additions & 1 deletion package/MDAnalysis/coordinates/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
from ..auxiliary.core import get_auxreader_for
from ..auxiliary import _AUXREADERS
from ..lib.util import asiterable, Namespace, store_init_arguments
from ..lib.util import NamedStream


class FrameIteratorBase(object):
Expand Down Expand Up @@ -1458,7 +1459,10 @@ class ReaderBase(ProtoReader):
def __init__(self, filename, convert_units=True, **kwargs):
super(ReaderBase, self).__init__()

self.filename = filename
if isinstance(filename, NamedStream):
self.filename = filename
else:
self.filename = str(filename)
self.convert_units = convert_units

ts_kwargs = {}
Expand Down
12 changes: 12 additions & 0 deletions testsuite/MDAnalysisTests/coordinates/test_dcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations.
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
from pathlib import Path
import numpy as np

import MDAnalysis as mda
Expand Down Expand Up @@ -424,3 +425,14 @@ def test_ts_time(universe):
for ts in u.trajectory]
times = [ts.time for ts in u.trajectory]
assert_almost_equal(times, ref_times, decimal=5)


def test_pathlib():
# regression test for DCD path of
# gh-2497
top = Path(PSF)
traj = Path(DCD)
u = mda.Universe(top, traj)
# we really only care that pathlib
# object handling worked
assert u.atoms.n_atoms == 3341
12 changes: 12 additions & 0 deletions testsuite/MDAnalysisTests/coordinates/test_xdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from os.path import split
import shutil
import subprocess
from pathlib import Path

from numpy.testing import (assert_equal,
assert_almost_equal,
Expand Down Expand Up @@ -904,3 +905,14 @@ class TestTRRReader_offsets(_GromacsReader_offsets):
9155712, 10300176
])
_reader = mda.coordinates.TRR.TRRReader


def test_pathlib():
# regression test for XDR path of
# gh-2497
top = Path(GRO)
traj = Path(XTC)
u = mda.Universe(top, traj)
# we really only care that pathlib
# object handling worked
assert u.atoms.n_atoms == 47681