From 52adce9a231b76864d206a5fdc4828ca3b5c7faa Mon Sep 17 00:00:00 2001 From: Lily Wang Date: Sun, 20 Oct 2024 21:14:14 +1100 Subject: [PATCH 1/3] switch to nodataerror --- package/MDAnalysis/guesser/default_guesser.py | 13 +++++++------ .../MDAnalysisTests/guesser/test_default_guesser.py | 9 +++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/package/MDAnalysis/guesser/default_guesser.py b/package/MDAnalysis/guesser/default_guesser.py index f49e75b24c6..c2fe3166e16 100644 --- a/package/MDAnalysis/guesser/default_guesser.py +++ b/package/MDAnalysis/guesser/default_guesser.py @@ -109,6 +109,7 @@ import re +from ..exceptions import NoDataError from ..lib import distances from . import tables @@ -218,15 +219,15 @@ 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") @@ -291,8 +292,8 @@ 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") diff --git a/testsuite/MDAnalysisTests/guesser/test_default_guesser.py b/testsuite/MDAnalysisTests/guesser/test_default_guesser.py index 2aacb28d4f1..3177ecd44e7 100644 --- a/testsuite/MDAnalysisTests/guesser/test_default_guesser.py +++ b/testsuite/MDAnalysisTests/guesser/test_default_guesser.py @@ -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 @@ -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']) @@ -119,7 +120,7 @@ def test_guess_elements_from_no_data(self): top = Topology(5) msg = "there is no reference attributes in this universe" "to guess types from" - with pytest.raises(ValueError, match=(msg)): + with pytest.raises(NoDataError, match=(msg)): mda.Universe(top, to_guess=['types']) @pytest.mark.parametrize('name, element', ( @@ -148,7 +149,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']) From 301f17209cc3a7bb6974c8a660a743e4cda756e8 Mon Sep 17 00:00:00 2001 From: Lily Wang <31115101+lilyminium@users.noreply.github.com> Date: Sat, 26 Oct 2024 15:36:46 +1100 Subject: [PATCH 2/3] Update package/MDAnalysis/guesser/default_guesser.py Co-authored-by: Yuxuan Zhuang --- package/MDAnalysis/guesser/default_guesser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/MDAnalysis/guesser/default_guesser.py b/package/MDAnalysis/guesser/default_guesser.py index c2fe3166e16..7414c9ae2e9 100644 --- a/package/MDAnalysis/guesser/default_guesser.py +++ b/package/MDAnalysis/guesser/default_guesser.py @@ -230,7 +230,7 @@ def guess_masses(self, atom_types=None, indices_to_guess=None): 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] From 4955dc0aa9aee785af2fbd0344f183a618f472b5 Mon Sep 17 00:00:00 2001 From: Lily Wang <31115101+lilyminium@users.noreply.github.com> Date: Sat, 26 Oct 2024 15:36:53 +1100 Subject: [PATCH 3/3] Update package/MDAnalysis/guesser/default_guesser.py Co-authored-by: Yuxuan Zhuang --- package/MDAnalysis/guesser/default_guesser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/MDAnalysis/guesser/default_guesser.py b/package/MDAnalysis/guesser/default_guesser.py index 7414c9ae2e9..9d1ead16366 100644 --- a/package/MDAnalysis/guesser/default_guesser.py +++ b/package/MDAnalysis/guesser/default_guesser.py @@ -295,7 +295,7 @@ def guess_types(self, atom_types=None, indices_to_guess=None): 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]