Skip to content

Commit

Permalink
Merge pull request #1158 from jbarnoud/py3
Browse files Browse the repository at this point in the history
Some fix for python 3
  • Loading branch information
kain88-de authored Jan 12, 2017
2 parents 59af18f + 2f57649 commit 39c29e5
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package/MDAnalysis/analysis/density.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions package/MDAnalysis/analysis/encore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand All @@ -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)


Expand Down
6 changes: 3 additions & 3 deletions package/MDAnalysis/core/topologyattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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])

Expand Down
2 changes: 1 addition & 1 deletion package/MDAnalysis/core/topologyobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand Down
2 changes: 1 addition & 1 deletion package/MDAnalysis/lib/distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions package/MDAnalysis/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions package/MDAnalysis/topology/PSFParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"""
from __future__ import absolute_import

from six.moves import range

import logging
import functools
from math import ceil
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions package/MDAnalysis/topology/TOPParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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())
Expand All @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions package/MDAnalysis/topology/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions package/MDAnalysis/topology/guessers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)])
Expand All @@ -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)


Expand Down
4 changes: 4 additions & 0 deletions testsuite/MDAnalysisTests/analysis/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions testsuite/MDAnalysisTests/analysis/test_contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', )
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion testsuite/MDAnalysisTests/analysis/test_hbonds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions testsuite/MDAnalysisTests/analysis/test_rms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion testsuite/MDAnalysisTests/coordinates/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()

Expand Down
4 changes: 4 additions & 0 deletions testsuite/MDAnalysisTests/test_atomgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions testsuite/MDAnalysisTests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Expand Down

0 comments on commit 39c29e5

Please sign in to comment.