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

Universe() should either work as Universe.empty() or raise an exception #2527

Closed
Luthaf opened this issue Feb 12, 2020 · 4 comments · Fixed by #2322
Closed

Universe() should either work as Universe.empty() or raise an exception #2527

Luthaf opened this issue Feb 12, 2020 · 4 comments · Fixed by #2322

Comments

@Luthaf
Copy link
Contributor

Luthaf commented Feb 12, 2020

Expected behavior

import MDAnalysis as mda
u = mda.Universe()
print(u)

Prints something sensible.

Actual behavior

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-f7a893c68ed4> in <module>
----> 1 print(u)

~/code/mdanalysis/package/MDAnalysis/core/universe.py in __repr__(self)
    775
    776         return "<Universe with {n_atoms} atoms>".format(
--> 777             n_atoms=len(self.atoms))
    778
    779     def __getstate__(self):

TypeError: object of type 'NoneType' has no len()

Currently version of MDAnalysis

  • Which version are you using? (run python -c "import MDAnalysis as mda; print(mda.__version__)"): 0.20.2-dev0
  • Which version of Python (python -V)? Python 3.7.6
  • Which operating system? macOS 10.14.5
@Luthaf
Copy link
Contributor Author

Luthaf commented Feb 12, 2020

After looking a bit more at the docs, it looks like I should use mda.Universe.empty() instead to create a Universe. Then maybe calling mda.Universe() with no argument should fail?

@orbeckst
Copy link
Member

Honestly, since we have Universe.empty() it is not clear to me why Universe() cannot (semantically) do the same thing — I concede that this would be pythonic behavior.

(There's a good chance that this is simply due to history and the fact that for the longest time empty universes weren't possible. I am also not sure if there are insurmountable technical difficulties.)

@orbeckst orbeckst changed the title Printing an empty universe raises an Exception Universe() should either work as Universe.empty() or raise an exception Feb 13, 2020
@richardjgowers
Copy link
Member

@Luthaf thanks for the report. Because there's C allocation going on, and resizing is a pain, even an empty Universe needs to know its size.

It might actually be that you want to pass in the Topology object, which is the thing that a Parser (our reader of everything but coordinates/velocities/forces/dimensions) spits out?

@Luthaf
Copy link
Contributor Author

Luthaf commented Feb 13, 2020

It might actually be that you want to pass in the Topology object, which is the thing that a Parser (our reader of everything but coordinates/velocities/forces/dimensions) spits out?

I ended up using Universe.empty() and working with that without issue.

What I wanted to do initially was to create an Universe/a Topology without reading a file, instead adding atoms/coordinates directly from Python. So I created an universe with Universe.empty(n_atoms); and then set the different properties I wanted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants