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

Using pathlib Path object #2497

Open
mimischi opened this issue Feb 3, 2020 · 5 comments
Open

Using pathlib Path object #2497

mimischi opened this issue Feb 3, 2020 · 5 comments

Comments

@mimischi
Copy link
Contributor

mimischi commented Feb 3, 2020

Expected behavior

Providing a PosixPath from pathlib to mda.Universe should create the Universe. Instead, we currently have to wrap the PosixPath object in a str() call.

Actual behavior

Code throws an AttributeError:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-154-4c04e03d6c7d> in <module>
      2 gro = base_path / "ref.gro"
      3 xtc = base_path / "trjcat_p1p10.mc.xtc"
----> 4 u = mda.Universe(gro, xtc)

~/.conda/envs/mda020/lib/python3.7/site-packages/MDAnalysis/core/universe.py in __init__(self, *args, **kwargs)
    335                 coordinatefile = None
    336 
--> 337             self.load_new(coordinatefile, **kwargs)
    338             # parse transformations
    339             trans_arg = kwargs.pop('transformations', None)

~/.conda/envs/mda020/lib/python3.7/site-packages/MDAnalysis/core/universe.py in load_new(self, filename, format, in_memory, **kwargs)
    602         kwargs['n_atoms'] = self.atoms.n_atoms
    603 
--> 604         self.trajectory = reader(filename, **kwargs)
    605         if self.trajectory.n_atoms != len(self.atoms):
    606             raise ValueError("The topology and {form} trajectory files don't"

~/.conda/envs/mda020/lib/python3.7/site-packages/MDAnalysis/coordinates/XDR.py in __init__(self, filename, convert_units, sub, refresh_offsets, **kwargs)
    134                                             convert_units=convert_units,
    135                                             **kwargs)
--> 136         self._xdr = self._file(self.filename)
    137 
    138         self._sub = sub

MDAnalysis/lib/formats/libmdaxdr.pyx in MDAnalysis.lib.formats.libmdaxdr._XDRFile.__cinit__()

AttributeError: 'PosixPath' object has no attribute 'encode'

Code to reproduce the behavior

from pathlib import Path 
import MDAnalysis as mda

base_path = Path(".")
gro = base_path / "example.gro"

u = mda.Universe(gro)

Currently version of MDAnalysis

  • Which version are you using? 0.20.1
  • Which version of Python (python -V)? 3.7.3
  • Which operating system? Scientific Linux
@orbeckst
Copy link
Member

orbeckst commented Feb 3, 2020

Is pathlib in the standard library or do we install it as part of our other dependencies?

@mattwthompson
Copy link
Contributor

It's in the standard library 3.4+

In principle it means os.path should never happen, although that's easier said than done

@orbeckst
Copy link
Member

orbeckst commented Feb 3, 2020

And in Python 2?

This version of MDA still supports 2.

@mattwthompson
Copy link
Contributor

It's not in the standard library there, but it can still be installed. I just tested that it can be installed via pip and conda in a fresh 2.7 environment and it installed and imported fine. It can also be installed as a separate packing with Python 3 (although that seems a little silly). I don't know how you all deal with these 2/3 conflicts, but I would guess an "if Python < 3: conda/pip install pathlib" approach could work

@richardjgowers
Copy link
Member

richardjgowers commented Feb 3, 2020 via email

tylerjereddy added a commit to tylerjereddy/mdanalysis that referenced this issue Nov 22, 2022
* the creation of a `Universe` object involving
GROMACS formats should now accept `pathlib`
objects as requested in MDAnalysisgh-2497; resolving that
issue entirely is a separate matter, this branch
focuses only on the GROMACS format and only if
you proceed via the public `Universe` creation
API

* probably could make it work in Cython directly,
though I'm not convinced we need to do that for
this part of the fix at least, and we probably
should still support strings as well of course
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants