From eca6a615f602adc49509dce7800a713f01298227 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 16 Jun 2017 18:01:31 -0700 Subject: [PATCH] warnings for ALL instant selectors Thanks to @richardjgowers for insights https://github.com/MDAnalysis/mdanalysis/pull/1403#issuecomment-309136416 --- package/MDAnalysis/core/groups.py | 33 +++++++-------- package/MDAnalysis/core/topologyattrs.py | 40 +++++++++++++++++++ .../source/documentation_pages/selections.rst | 20 +++++++++- 3 files changed, 74 insertions(+), 19 deletions(-) diff --git a/package/MDAnalysis/core/groups.py b/package/MDAnalysis/core/groups.py index 7d31f7122a7..5e225f2860b 100644 --- a/package/MDAnalysis/core/groups.py +++ b/package/MDAnalysis/core/groups.py @@ -1504,9 +1504,7 @@ class AtomGroup(GroupBase): .. deprecated:: 0.16.2 *Instant selectors* of AtomGroup will be removed in the 1.0 release. - See issue `#1377 - `_ for - more details. + See :ref:`Instant selectors`_ for details and alternatives. """ def __getitem__(self, item): @@ -1515,10 +1513,6 @@ def __getitem__(self, item): # # u.atoms['HT1'] access, otherwise default if isinstance(item, string_types): - warnings.warn("Instant selectors AtomGroup[''] " - "will be removed in 1.0. " - "Use `select_atoms('name ')`.", - DeprecationWarning) try: return self._get_named_atom(item) except (AttributeError, selection.SelectionError): @@ -1536,9 +1530,6 @@ def __getattr__(self, attr): # raise NDE(_ATTR_ERRORS[attr]) raise NoDataError("AtomGroup has no fragments; this requires Bonds") elif hasattr(self.universe._topology, 'names'): - warnings.warn("Instant selectors AtomGroup. " - "will be removed in 1.0", - DeprecationWarning) # Ugly hack to make multiple __getattr__s work try: return self._get_named_atom(attr) @@ -2208,6 +2199,11 @@ class ResidueGroup(GroupBase): ResidueGroups can be compared and combined using group operators. See the list of these operators on :class:`GroupBase`. + + .. deprecated:: 0.16.2 + *Instant selectors* of Segments will be removed in the 1.0 release. + See :ref:`Instant selectors`_ for details and alternatives. + """ @property @@ -2322,6 +2318,11 @@ class SegmentGroup(GroupBase): SegmentGroups can be compared and combined using group operators. See the list of these operators on :class:`GroupBase`. + + .. deprecated:: 0.16.2 + *Instant selectors* of Segments will be removed in the 1.0 release. + See :ref:`Instant selectors`_ for details and alternatives. + """ @property @@ -2669,9 +2670,7 @@ class Segment(ComponentBase): .. deprecated:: 0.16.2 *Instant selectors* of Segments will be removed in the 1.0 release. - See issue `#1377 - `_ for - more details. + See :ref:`Instant selectors`_ for details and alternatives. """ def __repr__(self): @@ -2696,16 +2695,14 @@ def __getattr__(self, attr): if attr.startswith('r') and attr[1:].isdigit(): resnum = int(attr[1:]) rg = self.residues[resnum - 1] # convert to 0 based - warnings.warn("Instant selectors Segment.r will be removed in 1.0", + warnings.warn("Instant selectors Segment.r will be removed in 1.0. " + "Use Segment.residues[N-1] instead.", DeprecationWarning) return rg # Resname accesss if hasattr(self.residues, 'resnames'): try: - rg = self.residues._get_named_residue(attr) - warnings.warn("Instant selectors Segment. will be removed in 1.0", - DeprecationWarning) - return rg + return self.residues._get_named_residue(attr) except selection.SelectionError: pass raise AttributeError("{cls} has no attribute {attr}" diff --git a/package/MDAnalysis/core/topologyattrs.py b/package/MDAnalysis/core/topologyattrs.py index 9cdf56cfece..0d3e7ddce67 100644 --- a/package/MDAnalysis/core/topologyattrs.py +++ b/package/MDAnalysis/core/topologyattrs.py @@ -42,6 +42,8 @@ import numbers import numpy as np +from numpy.lib.utils import deprecate + from . import flags from ..lib.util import cached, convert_aa_code, iterable from ..lib import transformations, mdamath @@ -409,6 +411,9 @@ def getattr__(atomgroup, name): raise AttributeError("'{0}' object has no attribute '{1}'".format( atomgroup.__class__.__name__, name)) + @deprecate(message="Instant selector AtomGroup[''] or AtomGroup. " + "is deprecated and will be removed in 1.0. " + "Use AtomGroup.select_atoms('name ') instead.") def _get_named_atom(group, name): """Get all atoms with name *name* in the current AtomGroup. @@ -417,6 +422,14 @@ def _get_named_atom(group, name): no atoms are found, a :exc:`SelectionError` is raised. .. versionadded:: 0.9.2 + + .. deprecated:: 0.16.2 + *Instant selectors* will be removed in the 1.0 release. + Use ``AtomGroup.select_atoms('name ')`` instead. + See issue `#1377 + `_ for + more details. + """ # There can be more than one atom with the same name atomlist = group.atoms.unique[group.atoms.unique.names == name] @@ -1057,6 +1070,13 @@ def getattr__(residuegroup, resname): # This transplant is hardcoded for now to allow for multiple getattr things #transplants[Segment].append(('__getattr__', getattr__)) + + @deprecate(message="Instant selector ResidueGroup. " + "or Segment. " + "is deprecated and will be removed in 1.0. " + "Use ResidueGroup[ResidueGroup.resnames == ''] " + "or Segment.residues[Segment.residues == ''] " + "instead.") def _get_named_residue(group, resname): """Get all residues with name *resname* in the current ResidueGroup or Segment. @@ -1068,6 +1088,15 @@ def _get_named_residue(group, resname): .. versionadded:: 0.9.2 + .. deprecated:: 0.16.2 + *Instant selectors* will be removed in the 1.0 release. + Use ``ResidueGroup[ResidueGroup.resnames == '']`` + or ``Segment.residues[Segment.residues == '']`` + instead. + See issue `#1377 + `_ for + more details. + """ # There can be more than one residue with the same name residues = group.residues.unique[ @@ -1244,6 +1273,10 @@ def getattr__(segmentgroup, segid): transplants[SegmentGroup].append( ('__getattr__', getattr__)) + @deprecate(message="Instant selector SegmentGroup. " + "is deprecated and will be removed in 1.0. " + "Use SegmentGroup[SegmentGroup.segids == ''] " + "instead.") def _get_named_segment(group, segid): """Get all segments with name *segid* in the current SegmentGroup. @@ -1254,6 +1287,13 @@ def _get_named_segment(group, segid): .. versionadded:: 0.9.2 + .. deprecated:: 0.16.2 + *Instant selectors* will be removed in the 1.0 release. + Use ``SegmentGroup[SegmentGroup.segids == '']`` instead. + See issue `#1377 + `_ for + more details. + """ # Undo adding 's' if segid started with digit if segid.startswith('s') and len(segid) >= 2 and segid[1].isdigit(): diff --git a/package/doc/sphinx/source/documentation_pages/selections.rst b/package/doc/sphinx/source/documentation_pages/selections.rst index c4773df5eb1..40caae95b7e 100644 --- a/package/doc/sphinx/source/documentation_pages/selections.rst +++ b/package/doc/sphinx/source/documentation_pages/selections.rst @@ -286,7 +286,6 @@ Instant selectors ================= .. deprecated:: 0.16.2 - *Instant selectors* will be removed in the 1.0 release in order to streamline the MDAnalysis user interface. They do not seem to be widely used anymore, can produce cryptic error messages, and are @@ -309,6 +308,11 @@ other levels of the structural hierarchy, namely for Segment selector ---------------- +.. deprecated:: 0.16.2 + Use ``SegmentGroup[SegmentGroup.segids == '']`` instead. Note that this + *always* returns a :class:`SegmentGroup` and *never* a :class:`Segment` + (unlike the instant selector). + - ``universe.`` or ``universe.s`` (if ** starts with a number) - returns a :class:`~MDAnalysis.core.groups.Segment` @@ -320,6 +324,9 @@ Segment selector Resid selector -------------- +.. deprecated:: 0.16.2 + Use ``Segment.residues[N-1]`` instead. + - ``seg.r`` selects residue with number ```` - returns a :class:`~MDAnalysis.core.groups.Residue` - works for :class:`~MDAnalysis.core.groups.Segment` and :class:`~MDAnalysis.core.groups.SegmentGroup` @@ -330,6 +337,12 @@ Resid selector Residue name selector --------------------- +.. deprecated:: 0.16.2 + Use ``ResidueGroup[ResidueGroup.resnames == '']`` or + ``Segment.residues[Segment.residues == '']`` instead. Note that this + *always* returns a :class:`ResidueGroup` and *never* a :class:`Residue` + (unlike the instant selector). + - ``seg.`` selects residues with residue name ```` - returns a :class:`~MDAnalysis.core.groups.ResidueGroup` - works for :class:`~MDAnalysis.core.groups.Segment` and :class:`~MDAnalysis.core.groups.SegmentGroup` @@ -346,6 +359,11 @@ Residue name selector Atom name selector ------------------ +.. deprecated:: 0.16.2 + Use ``AtomGroup.select_atoms('name ')`` instead. Note that this + *always* returns an :class:`AtomGroup` and *never* an :class:`Atom` (unlike + the instant selector). + - ``g.`` selects a single atom or a group of atoms with name ```` - returns