From 2f5764938095113e5a840c8a8c366afaa5e99142 Mon Sep 17 00:00:00 2001 From: Jonathan Barnoud Date: Thu, 12 Jan 2017 22:14:39 +0100 Subject: [PATCH] Some fix for python 3 --- package/MDAnalysis/analysis/density.py | 2 +- package/MDAnalysis/analysis/encore/utils.py | 9 +++++---- package/MDAnalysis/core/topologyattrs.py | 6 +++--- package/MDAnalysis/core/topologyobjects.py | 2 +- package/MDAnalysis/lib/distances.py | 2 +- package/MDAnalysis/lib/util.py | 4 ++-- package/MDAnalysis/topology/PSFParser.py | 8 ++++---- package/MDAnalysis/topology/TOPParser.py | 8 ++++---- package/MDAnalysis/topology/base.py | 2 ++ package/MDAnalysis/topology/guessers.py | 6 ++++-- testsuite/MDAnalysisTests/analysis/test_base.py | 4 ++++ .../MDAnalysisTests/analysis/test_contacts.py | 4 ++++ testsuite/MDAnalysisTests/analysis/test_hbonds.py | 3 ++- testsuite/MDAnalysisTests/analysis/test_rms.py | 2 ++ .../MDAnalysisTests/coordinates/test_memory.py | 5 ++++- testsuite/MDAnalysisTests/test_atomgroup.py | 4 ++++ testsuite/MDAnalysisTests/util.py | 14 +++++++++++--- 17 files changed, 58 insertions(+), 27 deletions(-) diff --git a/package/MDAnalysis/analysis/density.py b/package/MDAnalysis/analysis/density.py index d60a92c2bce..85b4f8ccc0e 100644 --- a/package/MDAnalysis/analysis/density.py +++ b/package/MDAnalysis/analysis/density.py @@ -492,7 +492,7 @@ def current_coordinates(): if tuple(angles) != (90., 90., 90.): msg = "Non-orthorhombic unit-cell --- make sure that it has been remapped properly!" warnings.warn(msg) - logger.warn(msg) + logger.warning(msg) # Make the box bigger to avoid as much as possible 'outlier'. This # is important if the sites are defined at a high density: in this diff --git a/package/MDAnalysis/analysis/encore/utils.py b/package/MDAnalysis/analysis/encore/utils.py index 9f571977fbf..4b3539843a6 100644 --- a/package/MDAnalysis/analysis/encore/utils.py +++ b/package/MDAnalysis/analysis/encore/utils.py @@ -19,6 +19,7 @@ # MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations. # J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787 # +from six.moves import range from multiprocessing.sharedctypes import SynchronizedArray from multiprocessing import Process, Manager import numpy as np @@ -438,8 +439,8 @@ def trm_indices_nodiag(n): Matrix size """ - for i in xrange(1, n): - for j in xrange(i): + for i in range(1, n): + for j in range(i): yield (i, j) @@ -454,8 +455,8 @@ def trm_indices_diag(n): Matrix size """ - for i in xrange(0, n): - for j in xrange(i+1): + for i in range(0, n): + for j in range(i + 1): yield (i, j) diff --git a/package/MDAnalysis/core/topologyattrs.py b/package/MDAnalysis/core/topologyattrs.py index de6d2413abe..9f1fd08d512 100644 --- a/package/MDAnalysis/core/topologyattrs.py +++ b/package/MDAnalysis/core/topologyattrs.py @@ -31,7 +31,7 @@ These are usually read by the TopologyParser. """ -from six.moves import zip +from six.moves import zip, range from collections import defaultdict import itertools import numpy as np @@ -672,7 +672,7 @@ def shape_parameter(group, **kwargs): recenteredpos = atomgroup.positions - com tensor = np.zeros((3, 3)) - for x in xrange(recenteredpos.shape[0]): + for x in range(recenteredpos.shape[0]): tensor += masses[x] * np.outer(recenteredpos[x, :], recenteredpos[x, :]) tensor /= atomgroup.total_mass() @@ -721,7 +721,7 @@ def asphericity(group, pbc=None): atomgroup.center_of_mass(pbc=False)) tensor = np.zeros((3, 3)) - for x in xrange(recenteredpos.shape[0]): + for x in range(recenteredpos.shape[0]): tensor += masses[x] * np.outer(recenteredpos[x], recenteredpos[x]) diff --git a/package/MDAnalysis/core/topologyobjects.py b/package/MDAnalysis/core/topologyobjects.py index fd92a3a24f5..94fe9a34eca 100644 --- a/package/MDAnalysis/core/topologyobjects.py +++ b/package/MDAnalysis/core/topologyobjects.py @@ -560,7 +560,7 @@ def __init__(self, bondidx, universe, btype=None, type=None, guessed=None, # Create vertical AtomGroups self._ags = [universe.atoms[self._bix[:, i]] - for i in xrange(self._bix.shape[1])] + for i in range(self._bix.shape[1])] else: # Empty TopologyGroup self._bix = np.array([]) diff --git a/package/MDAnalysis/lib/distances.py b/package/MDAnalysis/lib/distances.py index eb9d6234be8..816c67a40f8 100644 --- a/package/MDAnalysis/lib/distances.py +++ b/package/MDAnalysis/lib/distances.py @@ -118,7 +118,7 @@ def _run(funcname, args=None, kwargs=None, backend="serial"): ortho_pbc, triclinic_pbc) -from c_distances_openmp import OPENMP_ENABLED as USED_OPENMP +from .c_distances_openmp import OPENMP_ENABLED as USED_OPENMP def _box_check(box): diff --git a/package/MDAnalysis/lib/util.py b/package/MDAnalysis/lib/util.py index 2e711f9122d..3596be4518f 100644 --- a/package/MDAnalysis/lib/util.py +++ b/package/MDAnalysis/lib/util.py @@ -1258,12 +1258,12 @@ def unique_rows(arr, return_index=False): if return_index: u, r_idx = np.unique(arr.view(dtype=np.dtype([(str(i), arr.dtype) - for i in xrange(m)])), + for i in range(m)])), return_index=True) return u.view(arr.dtype).reshape(-1, m), r_idx else: u = np.unique(arr.view( - dtype=np.dtype([(str(i), arr.dtype) for i in xrange(m)]) + dtype=np.dtype([(str(i), arr.dtype) for i in range(m)]) )) return u.view(arr.dtype).reshape(-1, m) diff --git a/package/MDAnalysis/topology/PSFParser.py b/package/MDAnalysis/topology/PSFParser.py index 453562b72e2..f9d946a610c 100644 --- a/package/MDAnalysis/topology/PSFParser.py +++ b/package/MDAnalysis/topology/PSFParser.py @@ -43,8 +43,8 @@ """ from __future__ import absolute_import - from six.moves import range + import logging import functools from math import ceil @@ -103,7 +103,7 @@ def parse(self): """ # Open and check psf validity with openany(self.filename, 'r') as psffile: - header = psffile.next() + header = next(psffile) if not header.startswith("PSF"): err = ("{0} is not valid PSF file (header = {1})" "".format(self.filename, header)) @@ -146,7 +146,7 @@ def parse(self): try: for attr, info in sections: - psffile.next() + next(psffile) top.add_TopologyAttr( attr(self._parse_sec(psffile, info))) except StopIteration: @@ -272,7 +272,7 @@ def _parseatoms(self, lines, atoms_per, numlines): charges = np.zeros(numlines, dtype=np.float32) masses = np.zeros(numlines, dtype=np.float64) - for i in xrange(numlines): + for i in range(numlines): try: line = lines() except StopIteration: diff --git a/package/MDAnalysis/topology/TOPParser.py b/package/MDAnalysis/topology/TOPParser.py index e471d202950..ddb13061068 100644 --- a/package/MDAnalysis/topology/TOPParser.py +++ b/package/MDAnalysis/topology/TOPParser.py @@ -167,7 +167,7 @@ def parse(self): header = self.topfile.next() self.topfile.next() - topremarks = [self.topfile.next().strip() for i in xrange(4)] + topremarks = [self.topfile.next().strip() for i in range(4)] sys_info = [int(k) for i in topremarks for k in i.split()] header = self.topfile.next() @@ -284,7 +284,7 @@ def parse_residx(self, atoms_per, numlines): def parsebond(self, atoms_per, numlines): y = self.topfile.next().strip("%FORMAT(") section = [] - for i in xrange(numlines): + for i in range(numlines): l = self.topfile.next() # Subtract 1 from each number to ensure zero-indexing for the atoms fields = map(lambda x: int(x) - 1, l.split()) @@ -297,9 +297,9 @@ def parsesection_mapper(self, atoms_per, numlines, mapper): y = self.topfile.next().strip("%FORMAT(") y.strip(")") x = FORTRANReader(y) - for i in xrange(numlines): + for i in range(numlines): l = self.topfile.next() - for j in xrange(len(x.entries)): + for j in range(len(x.entries)): val = l[x.entries[j].start:x.entries[j].stop].strip() if val: section.append(mapper(val)) diff --git a/package/MDAnalysis/topology/base.py b/package/MDAnalysis/topology/base.py index 8bb7e9a712e..d6846073415 100644 --- a/package/MDAnalysis/topology/base.py +++ b/package/MDAnalysis/topology/base.py @@ -37,6 +37,8 @@ """ import six from six.moves import zip +# While reduce is a built-in in python 2, it is not in python 3 +from functools import reduce import itertools import numpy as np diff --git a/package/MDAnalysis/topology/guessers.py b/package/MDAnalysis/topology/guessers.py index 5d384cf2571..db2353bc064 100644 --- a/package/MDAnalysis/topology/guessers.py +++ b/package/MDAnalysis/topology/guessers.py @@ -27,6 +27,8 @@ while `guess_Xs` will work on an array of many atoms. """ +from six.moves import map + import numpy as np import warnings @@ -46,7 +48,7 @@ def guess_masses(atom_types): ------- atom_masses : np.ndarray dtype float64 """ - masses = np.array(map(get_atom_mass, atom_types), dtype=np.float64) + masses = np.array(list(map(get_atom_mass, atom_types)), dtype=np.float64) if np.any(masses == 0.0): # figure out where the misses were and report misses = np.unique(np.asarray(atom_types)[np.where(masses == 0.0)]) @@ -67,7 +69,7 @@ def guess_types(atom_names): ------- atom_types : np.ndarray dtype object """ - return np.array(map(guess_atom_element, atom_names), + return np.array(list(map(guess_atom_element, atom_names)), dtype=object) diff --git a/testsuite/MDAnalysisTests/analysis/test_base.py b/testsuite/MDAnalysisTests/analysis/test_base.py index 318d0dfc11d..767e94d18b7 100644 --- a/testsuite/MDAnalysisTests/analysis/test_base.py +++ b/testsuite/MDAnalysisTests/analysis/test_base.py @@ -131,6 +131,8 @@ def simple_function(mobile): return mobile.center_of_geometry() +@dec.skipif(parser_not_found('DCD'), + 'DCD parser not available. Are you using python 3?') def test_AnalysisFromFunction(): u = mda.Universe(PSF, DCD) step = 2 @@ -150,6 +152,8 @@ def test_AnalysisFromFunction(): assert_array_equal(results, ana.results) +@dec.skipif(parser_not_found('DCD'), + 'DCD parser not available. Are you using python 3?') def test_analysis_class(): ana_class = base.analysis_class(simple_function) assert_(issubclass(ana_class, base.AnalysisBase)) diff --git a/testsuite/MDAnalysisTests/analysis/test_contacts.py b/testsuite/MDAnalysisTests/analysis/test_contacts.py index 19db93be35f..44346d0a20c 100644 --- a/testsuite/MDAnalysisTests/analysis/test_contacts.py +++ b/testsuite/MDAnalysisTests/analysis/test_contacts.py @@ -112,6 +112,8 @@ def test_contact_matrix(): assert_array_equal(out, [True, True, True, False, False]) +@dec.skipif(parser_not_found('DCD'), + 'DCD parser not available. Are you using python 3?') def test_new_selection(): u = mda.Universe(PSF, DCD) selections = ('all', ) @@ -293,6 +295,8 @@ def test_save(self): assert_array_almost_equal(ca.timeseries, saved) +@dec.skipif(parser_not_found('DCD'), + 'DCD parser not available. Are you using python 3?') def test_q1q2(): u = mda.Universe(PSF, DCD) q1q2 = contacts.q1q2(u, 'name CA', radius=8) diff --git a/testsuite/MDAnalysisTests/analysis/test_hbonds.py b/testsuite/MDAnalysisTests/analysis/test_hbonds.py index 74f15b4dad7..2f2052595ca 100644 --- a/testsuite/MDAnalysisTests/analysis/test_hbonds.py +++ b/testsuite/MDAnalysisTests/analysis/test_hbonds.py @@ -20,6 +20,7 @@ # J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787 # from __future__ import print_function +from six.moves import map import MDAnalysis import MDAnalysis.analysis.hbonds @@ -39,7 +40,7 @@ def guess_types(names): """GRO doesn't supply types, this returns an Attr""" - return Atomtypes(np.array(map(guess_atom_type, names), dtype=object)) + return Atomtypes(np.array(list(map(guess_atom_type, names)), dtype=object)) class TestHydrogenBondAnalysis(object): def setUp(self): diff --git a/testsuite/MDAnalysisTests/analysis/test_rms.py b/testsuite/MDAnalysisTests/analysis/test_rms.py index ce1d4c36454..d3dfb3da092 100644 --- a/testsuite/MDAnalysisTests/analysis/test_rms.py +++ b/testsuite/MDAnalysisTests/analysis/test_rms.py @@ -42,6 +42,8 @@ class Testrmsd(object): + @dec.skipif(parser_not_found('DCD'), + 'DCD parser not available. Are you using python 3?') def __init__(self): shape = (5, 3) # vectors with length one diff --git a/testsuite/MDAnalysisTests/coordinates/test_memory.py b/testsuite/MDAnalysisTests/coordinates/test_memory.py index 3e431296816..9d0229848c0 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_memory.py +++ b/testsuite/MDAnalysisTests/coordinates/test_memory.py @@ -5,10 +5,13 @@ from MDAnalysisTests.coordinates.base import (BaseReference, BaseReaderTest) from MDAnalysis.coordinates.memory import Timestep -from numpy.testing import assert_equal +from numpy.testing import assert_equal, dec +from MDAnalysisTests import parser_not_found class MemoryReference(BaseReference): + @dec.skipif(parser_not_found('DCD'), + 'DCD parser not available. Are you using python 3?') def __init__(self): super(MemoryReference, self).__init__() diff --git a/testsuite/MDAnalysisTests/test_atomgroup.py b/testsuite/MDAnalysisTests/test_atomgroup.py index 9e8efdbf736..6c72026ec24 100644 --- a/testsuite/MDAnalysisTests/test_atomgroup.py +++ b/testsuite/MDAnalysisTests/test_atomgroup.py @@ -1440,6 +1440,8 @@ def setUp(self): self.sB = self.universe.segments[1] # VALID but temporary + @dec.skipif(parser_not_found('DCD'), + 'DCD parser not available. Are you using python 3?') def setUp(self): self.universe = MDAnalysis.Universe(PSF, DCD) @@ -2385,6 +2387,8 @@ def test_adding_empty_ags(self): class TestDihedralSelections(object): + @dec.skipif(parser_not_found('DCD'), + 'DCD parser not available. Are you using python 3?') def setUp(self): self.universe = MDAnalysis.Universe(PSF, DCD) self.dih_prec = 2 diff --git a/testsuite/MDAnalysisTests/util.py b/testsuite/MDAnalysisTests/util.py index 6a66ef67e53..992b427107b 100644 --- a/testsuite/MDAnalysisTests/util.py +++ b/testsuite/MDAnalysisTests/util.py @@ -24,7 +24,15 @@ """ -import __builtin__ +try: + import __builtin__ + builtins_name = '__builtin__' + importer = __builtin__.__import__ +except ImportError: + import builtins + builtins_name = 'builtins' + importer = builtins.__import__ + from functools import wraps import mock @@ -44,8 +52,8 @@ def try_and_do_something(): def blocker_wrapper(func): @wraps(func) def func_wrapper(*args, **kwargs): - with mock.patch('__builtin__.__import__', - wraps=__builtin__.__import__) as mbi: + with mock.patch('{}.__import__'.format(builtins_name), + wraps=importer) as mbi: def blocker(*args, **kwargs): if package in args: raise ImportError("Blocked by block_import")