Skip to content

Residue.mass and Residue.charge use deprecated numpy indexing #2990

@jbarnoud

Description

@jbarnoud

Running the tests throws a warning about MDAnalysis using a behaviour of numpy that is subject to change:

MDAnalysisTests/core/test_accumulate.py::TestTotals::test_total_charge_duplicates[residues]
  /home/jon/dev/mdanalysis/package/MDAnalysis/core/topologyattrs.py:1540: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
    charges = self.values[resatoms].sum()

MDAnalysisTests/core/test_accumulate.py::TestTotals::test_total_mass_duplicates[residues]
  /home/jon/dev/mdanalysis/package/MDAnalysis/core/topologyattrs.py:1119: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
    masses = self.values[resatoms].sum()

The warnings are thrown for core/test_accumulate.py::TestTotals::test_total_charge (and its mass equivalent):

ref = group.total_charge() + group[0].charge

The problematic call is a call to Residue.charge or Residue.mass (note the singular group name). This calls topologyattrs.Charges.get_residues which makes a problematic array indexing on line

charges = self.values[resatoms].sum()
or topologyattrs.Masses.get_residues on line
masses = self.values[resatoms].sum()

Here, an array is indexed with a list of arrays, hence the warning.

The get_segments counterpart are fixed and convert the list of arrays into a tuple of arrays, not triggering the warning.

Code to reproduce the behavior

import MDAnalysis as mda
from MDAnalysis.tests.datafiles import XTC, TRR

u = mda.Universe(TRR, XTC)
u.residues[0].mass  # or u.residues[0].charge

or

pytest core/test_accumulate.py::TestTotals

Current version of MDAnalysis

  • Which version are you using? (run python -c "import MDAnalysis as mda; print(mda.__version__)") 2.0.0-dev0 64a7c05
  • Which version of Python (python -V)? Python 3.8.5
  • Which operating system? Ubuntu 20.04 on WSL

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions