Skip to content

Commit

Permalink
Switch guessers to catching and raising NoDataErrors (#4755)
Browse files Browse the repository at this point in the history
* Switch guessers to catching and raising NoDataErrors

---------

Co-authored-by: Yuxuan Zhuang <[email protected]>
  • Loading branch information
lilyminium and yuxuanzhuang authored Oct 26, 2024
1 parent 52ff77b commit 961cbd5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
17 changes: 9 additions & 8 deletions package/MDAnalysis/guesser/default_guesser.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@

import re

from ..exceptions import NoDataError
from ..lib import distances
from . import tables

Expand Down Expand Up @@ -218,18 +219,18 @@ def guess_masses(self, atom_types=None, indices_to_guess=None):
if atom_types is None:
try:
atom_types = self._universe.atoms.elements
except AttributeError:
except NoDataError:
try:
atom_types = self._universe.atoms.types
except AttributeError:
except NoDataError:
try:
atom_types = self.guess_types(
atom_types=self._universe.atoms.names)
except ValueError:
raise ValueError(
except NoDataError:
raise NoDataError(
"there is no reference attributes"
" (elements, types, or names)"
" in this universe to guess mass from")
" in this universe to guess mass from") from None

if indices_to_guess is not None:
atom_types = atom_types[indices_to_guess]
Expand Down Expand Up @@ -291,10 +292,10 @@ def guess_types(self, atom_types=None, indices_to_guess=None):
if atom_types is None:
try:
atom_types = self._universe.atoms.names
except AttributeError:
raise ValueError(
except NoDataError:
raise NoDataError(
"there is no reference attributes in this universe "
"to guess types from")
"to guess types from") from None

if indices_to_guess is not None:
atom_types = atom_types[indices_to_guess]
Expand Down
7 changes: 4 additions & 3 deletions testsuite/MDAnalysisTests/guesser/test_default_guesser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

from numpy.testing import assert_equal, assert_allclose
import numpy as np
from MDAnalysis.core.topologyattrs import Angles, Atomtypes, Atomnames, Masses
from MDAnalysis.core.topologyattrs import Angles, Atomtypes, Atomnames
from MDAnalysis.exceptions import NoDataError
from MDAnalysis.guesser.default_guesser import DefaultGuesser
from MDAnalysis.core.topology import Topology
from MDAnalysisTests import make_Universe
Expand Down Expand Up @@ -82,7 +83,7 @@ def test_guess_atom_mass(self, default_guesser):

def test_guess_masses_with_no_reference_elements(self):
u = mda.Universe.empty(3)
with pytest.raises(ValueError,
with pytest.raises(NoDataError,
match=('there is no reference attributes ')):
u.guess_TopologyAttrs('default', ['masses'])

Expand Down Expand Up @@ -150,7 +151,7 @@ def test_guess_charge(default_guesser):
def test_guess_bonds_Error():
u = make_Universe(trajectory=True)
msg = "This Universe does not contain name information"
with pytest.raises(ValueError, match=msg):
with pytest.raises(NoDataError, match=msg):
u.guess_TopologyAttrs(to_guess=['bonds'])


Expand Down

0 comments on commit 961cbd5

Please sign in to comment.