From 39ad792b939bdd26e461f9a6e801f4f54ea789fd Mon Sep 17 00:00:00 2001 From: Dominik 'Rathann' Mierzejewski Date: Fri, 9 Jun 2017 11:42:19 +0200 Subject: [PATCH 1/3] use correct int types This fixes ERRORs and FAILs in the testsuite on 32bit: TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe' and assert_(out[0].dtype == np.int64) File "/usr/lib/python2.7/site-packages/numpy/testing/utils.py", line 92, in assert_ raise AssertionError(smsg) AssertionError --- package/MDAnalysis/core/groups.py | 2 +- package/MDAnalysis/core/topology.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package/MDAnalysis/core/groups.py b/package/MDAnalysis/core/groups.py index c386e079ef0..f2459c9f1dd 100644 --- a/package/MDAnalysis/core/groups.py +++ b/package/MDAnalysis/core/groups.py @@ -419,7 +419,7 @@ def __init__(self, *args): ix, u = args # indices for the objects I hold - self._ix = np.asarray(ix, dtype=np.int64) + self._ix = np.asarray(ix, dtype=np.intp) self._u = u self._cache = dict() diff --git a/package/MDAnalysis/core/topology.py b/package/MDAnalysis/core/topology.py index 2c11d2f3632..2653acfb84d 100644 --- a/package/MDAnalysis/core/topology.py +++ b/package/MDAnalysis/core/topology.py @@ -125,12 +125,12 @@ def make_downshift_arrays(upshift, nparents): counter += 1 # If parent is skipped, eg (0, 0, 2, 2, etc) while counter != upshift[order[x:y][0]]: - downshift.append(np.array([], dtype=np.int)) + downshift.append(np.array([], dtype=np.int64)) counter += 1 - downshift.append(np.sort(np.array(order[x:y], copy=True, dtype=np.int))) + downshift.append(np.sort(np.array(order[x:y], copy=True, dtype=np.int64))) # Add entries for childless parents at end of range while counter < (nparents - 1): - downshift.append(np.array([], dtype=np.int)) + downshift.append(np.array([], dtype=np.int64)) counter += 1 # Add None to end of array to force it to be of type Object # Without this, a rectangular array gets squashed into a single array From 8641226001e876f5e0360c4d74d4c239aab3d539 Mon Sep 17 00:00:00 2001 From: Dominik 'Rathann' Mierzejewski Date: Fri, 9 Jun 2017 11:49:09 +0200 Subject: [PATCH 2/3] relax test_symmetry test due to lower precision on 32bit --- testsuite/MDAnalysisTests/analysis/test_psa.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_psa.py b/testsuite/MDAnalysisTests/analysis/test_psa.py index 3d4d097f10b..ddc0b8f4e71 100644 --- a/testsuite/MDAnalysisTests/analysis/test_psa.py +++ b/testsuite/MDAnalysisTests/analysis/test_psa.py @@ -221,7 +221,8 @@ def test_symmetry(self): for a given Hausdorff metric, h.''' forward = self.h(self.path_1, self.path_2) reverse = self.h(self.path_2, self.path_1) - self.assertEqual(forward, reverse) + # lower precision on 32bit + assert_almost_equal(forward, reverse, decimal=15) def test_hausdorff_value(self): '''Test that the undirected Hausdorff From eed776181c90646d960eb00a275bffb3ffb36a20 Mon Sep 17 00:00:00 2001 From: Richard Gowers Date: Mon, 12 Jun 2017 07:58:40 +0100 Subject: [PATCH 3/3] More work on making indexing arrays use intp (#1362) - Added tests for 32 bit index support - added @rathann to AUTHORS --- package/AUTHORS | 2 + package/CHANGELOG | 3 +- package/MDAnalysis/core/groups.py | 4 +- package/MDAnalysis/core/topology.py | 14 +-- .../MDAnalysisTests/core/test_index_dtype.py | 92 +++++++++++++++++++ 5 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 testsuite/MDAnalysisTests/core/test_index_dtype.py diff --git a/package/AUTHORS b/package/AUTHORS index 4267d50f93d..4133aafd72b 100644 --- a/package/AUTHORS +++ b/package/AUTHORS @@ -90,6 +90,8 @@ Chronological list of authors - Sang Young Noh - Andrew William King - Kathleen Clark + - Dominik 'Rathann' Mierzejewski + External code ------------- diff --git a/package/CHANGELOG b/package/CHANGELOG index d3d96542a51..666386fe8ff 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -15,12 +15,13 @@ The rules for this file: ------------------------------------------------------------------------------ mm/dd/17 - * 0.16.2 richardjgowers + * 0.16.2 richardjgowers, rathann Enhancements Fixes * fixed GROWriter truncating long resids from the wrong end (Issue #1395) + * Fixed dtype of numpy arrays to accomodate 32 bit architectures (Issue #1362) Changes diff --git a/package/MDAnalysis/core/groups.py b/package/MDAnalysis/core/groups.py index f2459c9f1dd..963f069d3f2 100644 --- a/package/MDAnalysis/core/groups.py +++ b/package/MDAnalysis/core/groups.py @@ -2464,7 +2464,7 @@ def ix_array(self): -------- ix """ - return np.array([self.ix]) + return np.array([self.ix], dtype=np.intp) class Atom(ComponentBase): @@ -2736,7 +2736,7 @@ def update_selection(self): ix = sum([sel.apply(bg) for sel in sels[1:]], sels[0].apply(bg)).ix else: - ix = np.array([], dtype=np.int) + ix = np.array([], dtype=np.intp) # Run back through AtomGroup init with this information to remake ourselves super(UpdatingAtomGroup, self).__init__(ix, self.universe) self.is_uptodate = True diff --git a/package/MDAnalysis/core/topology.py b/package/MDAnalysis/core/topology.py index 2653acfb84d..3b98daabc91 100644 --- a/package/MDAnalysis/core/topology.py +++ b/package/MDAnalysis/core/topology.py @@ -125,12 +125,12 @@ def make_downshift_arrays(upshift, nparents): counter += 1 # If parent is skipped, eg (0, 0, 2, 2, etc) while counter != upshift[order[x:y][0]]: - downshift.append(np.array([], dtype=np.int64)) + downshift.append(np.array([], dtype=np.intp)) counter += 1 - downshift.append(np.sort(np.array(order[x:y], copy=True, dtype=np.int64))) + downshift.append(np.sort(np.array(order[x:y], copy=True, dtype=np.intp))) # Add entries for childless parents at end of range while counter < (nparents - 1): - downshift.append(np.array([], dtype=np.int64)) + downshift.append(np.array([], dtype=np.intp)) counter += 1 # Add None to end of array to force it to be of type Object # Without this, a rectangular array gets squashed into a single array @@ -210,18 +210,18 @@ def __init__(self, # built atom-to-residue mapping, and vice-versa if atom_resindex is None: - self._AR = np.zeros(n_atoms, dtype=np.int64) + self._AR = np.zeros(n_atoms, dtype=np.intp) else: - self._AR = atom_resindex.copy() + self._AR = np.asarray(atom_resindex, dtype=np.intp).copy() if not len(self._AR) == n_atoms: raise ValueError("atom_resindex must be len n_atoms") self._RA = make_downshift_arrays(self._AR, n_residues) # built residue-to-segment mapping, and vice-versa if residue_segindex is None: - self._RS = np.zeros(n_residues, dtype=np.int64) + self._RS = np.zeros(n_residues, dtype=np.intp) else: - self._RS = residue_segindex.copy() + self._RS = np.asarray(residue_segindex, dtype=np.intp).copy() if not len(self._RS) == n_residues: raise ValueError("residue_segindex must be len n_residues") self._SR = make_downshift_arrays(self._RS, n_segments) diff --git a/testsuite/MDAnalysisTests/core/test_index_dtype.py b/testsuite/MDAnalysisTests/core/test_index_dtype.py new file mode 100644 index 00000000000..43e083a6a74 --- /dev/null +++ b/testsuite/MDAnalysisTests/core/test_index_dtype.py @@ -0,0 +1,92 @@ +# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*- +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8 +# +# MDAnalysis --- http://www.mdanalysis.org +# Copyright (c) 2006-2016 The MDAnalysis Development Team and contributors +# (see the file AUTHORS for the full list of names) +# +# Released under the GNU Public Licence, v2 or any higher version +# +# Please cite your use of MDAnalysis in published work: +# +# R. J. Gowers, M. Linke, J. Barnoud, T. J. E. Reddy, M. N. Melo, S. L. Seyler, +# D. L. Dotson, J. Domanski, S. Buchoux, I. M. Kenney, and O. Beckstein. +# MDAnalysis: A Python package for the rapid analysis of molecular dynamics +# simulations. In S. Benthall and S. Rostrup editors, Proceedings of the 15th +# Python in Science Conference, pages 102-109, Austin, TX, 2016. SciPy. +# +# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein. +# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations. +# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787 +# + +"""32 bit compat tests + +Tests for making sure that integer arrays used for indexing use `np.intp`. +This dtype is important for platform independent indexing of other arrays. + +""" +from __future__ import absolute_import + +import numpy as np +from numpy.testing import ( + assert_, +) +from MDAnalysisTests import make_Universe + + +class TestIndexDtype(object): + def setUp(self): + self.u = make_Universe() + + def tearDown(self): + del self.u + + def test_ag_ix(self): + assert_(self.u.atoms.ix.dtype == np.intp) + + def test_rg_ix(self): + assert_(self.u.residues.ix.dtype == np.intp) + + def test_sg_ix(self): + assert_(self.u.segments.ix.dtype == np.intp) + + def test_atom_ix_array(self): + assert_(self.u.atoms[0].ix_array.dtype == np.intp) + + def test_residue_ix_array(self): + assert_(self.u.residues[0].ix_array.dtype == np.intp) + + def test_segment_ix_array(self): + assert_(self.u.segments[0].ix_array.dtype == np.intp) + + def test_atomgroup_indices(self): + assert_(self.u.atoms.indices.dtype == np.intp) + + def test_atomgroup_residue_upshift(self): + assert_(self.u.atoms.resindices.dtype == np.intp) + + def test_atomgroup_segment_upshift(self): + assert_(self.u.atoms.segindices.dtype == np.intp) + + def test_residuegroup_atom_downshift(self): + # downshift arrays are a list (one for each residue) + assert_(all((arr.dtype == np.intp) + for arr in self.u.residues.indices)) + + def test_residuegroup_resindices(self): + assert_(self.u.residues.resindices.dtype == np.intp) + + def test_residuegroup_segment_upshift(self): + assert_(self.u.residues.segindices.dtype == np.intp) + + def test_segmentgroup_atom_downshift(self): + assert_(all((arr.dtype == np.intp) + for arr in self.u.segments.indices)) + + def test_segmentgroup_residue_downshift(self): + assert_(all((arr.dtype == np.intp) + for arr in self.u.segments.resindices)) + + def test_segmentgroup_segindices(self): + assert_(self.u.segments.segindices.dtype == np.intp)