Skip to content

Commit

Permalink
Merge pull request #683 from jbarnoud/issue-260-more-fix
Browse files Browse the repository at this point in the history
Issue #260 More python 3 compatibility fix
  • Loading branch information
richardjgowers committed Feb 1, 2016
2 parents 4e4f0fa + 2cdcb2a commit 68b587a
Show file tree
Hide file tree
Showing 22 changed files with 580 additions and 524 deletions.
2 changes: 1 addition & 1 deletion package/MDAnalysis/analysis/density.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def make_density(self):
warnings.warn(msg)
return

dedges = map(np.diff, self.edges)
dedges = [np.diff(edge) for edge in self.edges]
D = len(self.edges)
for i in range(D):
shape = np.ones(D, int)
Expand Down
12 changes: 6 additions & 6 deletions package/MDAnalysis/analysis/hbonds/hbond_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,8 @@ def generate_table(self):
# build empty output table
dtype = [
("time", float), ("donor_idx", int), ("acceptor_idx", int),
("donor_resnm", "|S4"), ("donor_resid", int), ("donor_atom", "|S4"),
("acceptor_resnm", "|S4"), ("acceptor_resid", int), ("acceptor_atom", "|S4"),
("donor_resnm", "|U4"), ("donor_resid", int), ("donor_atom", "|U4"),
("acceptor_resnm", "|U4"), ("acceptor_resid", int), ("acceptor_atom", "|U4"),
("distance", float), ("angle", float)]
# according to Lukas' notes below, using a recarray at this stage is ineffective
# and speedups of ~x10 can be achieved by filling a standard array, like this:
Expand Down Expand Up @@ -1040,8 +1040,8 @@ def count_by_type(self):
# build empty output table
dtype = [
('donor_idx', int), ('acceptor_idx', int),
('donor_resnm', 'S4'), ('donor_resid', int), ('donor_heavy_atom', 'S4'), ('donor_atom', 'S4'),
('acceptor_resnm', 'S4'), ('acceptor_resid', int), ('acceptor_atom', 'S4'),
('donor_resnm', 'U4'), ('donor_resid', int), ('donor_heavy_atom', 'U4'), ('donor_atom', 'U4'),
('acceptor_resnm', 'U4'), ('acceptor_resid', int), ('acceptor_atom', 'U4'),
('frequency', float)
]
out = np.empty((len(hbonds),), dtype=dtype)
Expand Down Expand Up @@ -1101,8 +1101,8 @@ def timesteps_by_type(self):
# build empty output table
dtype = [
('donor_idx', int), ('acceptor_idx', int),
('donor_resnm', 'S4'), ('donor_resid', int), ('donor_heavy_atom', 'S4'), ('donor_atom', 'S4'),
('acceptor_resnm', 'S4'), ('acceptor_resid', int), ('acceptor_atom', 'S4'),
('donor_resnm', 'U4'), ('donor_resid', int), ('donor_heavy_atom', 'U4'), ('donor_atom', 'U4'),
('acceptor_resnm', 'U4'), ('acceptor_resid', int), ('acceptor_atom', 'U4'),
('time', float)]
out = np.empty((out_nrows,), dtype=dtype)

Expand Down
7 changes: 4 additions & 3 deletions package/MDAnalysis/analysis/leaflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@

from six.moves import range

import warnings

import numpy as np
import MDAnalysis
import networkx as NX
import distances
import warnings
import MDAnalysis
from . import distances


class LeafletFinder(object):
Expand Down
4 changes: 2 additions & 2 deletions package/MDAnalysis/coordinates/CRD.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def _read_first_frame(self):
# process coordinates
try:
if extended:
coords_list.append(np.array(map(float, line[45:100].split()[0:3])))
coords_list.append(np.array(line[45:100].split()[0:3], dtype=float))
else:
coords_list.append(np.array(map(float, line[20:50].split()[0:3])))
coords_list.append(np.array(line[20:50].split()[0:3], dtype=float))
except:
raise ValueError("Check CRD format at line {0}: {1}"
"".format(linenum, line.rstrip()))
Expand Down
4 changes: 2 additions & 2 deletions package/MDAnalysis/coordinates/GMS.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(self, outfilename, **kwargs):
super(GMSReader, self).__init__(outfilename, **kwargs)

# the filename has been parsed to be either b(g)zipped or not
self.outfile = util.anyopen(self.filename, 'r')
self.outfile = util.anyopen(self.filename, 'rt')

# note that, like for xtc and trr files, _n_atoms and _n_frames are used quasi-private variables
# to prevent the properties being recalculated
Expand Down Expand Up @@ -242,7 +242,7 @@ def open_trajectory(self):
# must check; otherwise might segmentation fault
raise IOError(errno.ENOENT, 'GMS file not found', self.filename)

self.outfile = util.anyopen(self.filename, 'r')
self.outfile = util.anyopen(self.filename, 'rt')

# reset ts
ts = self.ts
Expand Down
4 changes: 2 additions & 2 deletions package/MDAnalysis/coordinates/GRO.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class GROReader(base.SingleFrameReader):
_Timestep = Timestep

def _read_first_frame(self):
with util.openany(self.filename, 'r') as grofile:
with util.openany(self.filename, 'rt') as grofile:
# Read first two lines to get number of atoms
grofile.readline()
self.n_atoms = n_atoms = int(grofile.readline())
Expand Down Expand Up @@ -281,7 +281,7 @@ def write(self, selection, frame=None):
"".format(self.gro_coor_limits["min"],
self.gro_coor_limits["max"]))

with util.openany(self.filename, 'w') as output_gro:
with util.openany(self.filename, 'wt') as output_gro:
# Header
output_gro.write('Written by MDAnalysis\n')
output_gro.write(self.fmt['n_atoms'].format(len(atoms)))
Expand Down
6 changes: 3 additions & 3 deletions package/MDAnalysis/coordinates/PDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def __init__(self, filename, **kwargs):

pos = 0 # atom position for filling coordinates array
occupancy = np.ones(self._n_atoms)
with util.openany(filename, 'r') as pdbfile:
with util.openany(filename, 'rt') as pdbfile:
for i, line in enumerate(pdbfile):
line = line.strip() # Remove extra spaces
if len(line) == 0: # Skip line if empty
Expand Down Expand Up @@ -594,7 +594,7 @@ def _read_frame(self, frame):
# forth; should improve performance substantially
pos = 0
occupancy = np.ones(self._n_atoms)
with util.openany(self.filename, 'r') as f:
with util.openany(self.filename, 'rt') as f:
for i in range(line):
next(f) # forward to frame
for line in f:
Expand Down Expand Up @@ -776,7 +776,7 @@ def __init__(self, filename, bonds="conect", n_atoms=None, start=0, step=1,
self.step = step
self.remarks = remarks

self.pdbfile = util.anyopen(self.filename, 'w') # open file on init
self.pdbfile = util.anyopen(self.filename, 'wt') # open file on init
self.has_END = False

def close(self):
Expand Down
12 changes: 8 additions & 4 deletions package/MDAnalysis/coordinates/XDR.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _load_offsets(self):
self._read_offsets(store=True)
return

with open(fname) as f:
with open(fname, 'rb') as f:
data = {k: v for k, v in six.iteritems(np.load(f))}

ctime_ok = getctime(self.filename) == data['ctime']
Expand All @@ -116,8 +116,12 @@ def _read_offsets(self, store=False):
np.savez(offsets_filename(self.filename),
offsets=offsets, size=size, ctime=ctime)
except Exception as e:
warnings.warn("Couldn't save offsets because: {}".format(
e.message))
try:
warnings.warn("Couldn't save offsets because: {}".format(
e.message))
except AttributeError:
warnings.warn("Couldn't save offsets because: {}".format(e))


def rewind(self):
"""Read the first frame again"""
Expand All @@ -133,7 +137,7 @@ def _reopen(self):
self.ts.frame = 0
self._frame = -1
self._xdr.close()
self._xdr.open(self.filename, 'r')
self._xdr.open(self.filename.encode('utf-8'), 'r')

def _read_frame(self, i):
"""read frame i"""
Expand Down
2 changes: 1 addition & 1 deletion package/MDAnalysis/coordinates/XYZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def __init__(self, filename, n_atoms=None, atoms='X', convert_units=None,
self.__class__.__name__, __version__)
self.remark = default_remark if remark == 'default' else remark
# can also be gz, bz2
self._xyz = util.anyopen(self.filename, 'w')
self._xyz = util.anyopen(self.filename, 'wt')

def _get_atomnames(self, atoms):
"""Return a list of atom names"""
Expand Down
2 changes: 1 addition & 1 deletion package/MDAnalysis/coordinates/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ def apply_limits(frame):
elif isinstance(frame, (list, np.ndarray)):
def listiter(frames):
for f in frames:
if not isinstance(f, int):
if not isinstance(f, (int, np.integer)):
raise TypeError("Frames indices must be integers")
yield self._read_frame(apply_limits(f))
return listiter(frame)
Expand Down
10 changes: 6 additions & 4 deletions package/MDAnalysis/core/Selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@
This is all invisible to the user through ag.select_atoms
"""
import six
from six.moves import zip

import collections
import re
import functools
import warnings

import numpy as np
from numpy.lib.utils import deprecate
from Bio.KDTree import KDTree
import warnings
import six
from six.moves import zip

from MDAnalysis.core import flags
from ..lib import distances
Expand Down Expand Up @@ -909,7 +911,7 @@ def apply(self, group):
if self.prop == 'fragment':
# Combine all fragments together, then check where group
# indices are same as fragment(s) indices
allfrags = reduce(lambda x, y: x + y, res.fragments)
allfrags = functools.reduce(lambda x, y: x + y, res.fragments)

mask = np.in1d(group.indices, allfrags.indices)
return unique(group[mask])
Expand Down
Loading

0 comments on commit 68b587a

Please sign in to comment.