Skip to content

Commit

Permalink
Merge pull request #658 from jbarnoud/issue-260-python-3
Browse files Browse the repository at this point in the history
Issue #260 Partial python 3 compatibility
  • Loading branch information
richardjgowers committed Jan 26, 2016
2 parents 5330a7c + 187272c commit ebc2f59
Show file tree
Hide file tree
Showing 59 changed files with 472 additions and 252 deletions.
34 changes: 20 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
env:
global:
- secure: "HIj3p+p2PV8DBVg/KGUx6n83KwB0ASE5FwOn0SMB9zxnzAqe8sapwdBQdMdq0sXB7xT1spJqRxuxOMVEVn35BNLu7bxMLfa4287C8YXcomnvmv9xruxAsjsIewnNQ80vtPVbQddBPxa4jKbqgPby5QhhAP8KANAqYe44pIV70fY="
- GH_DOC_BRANCH: develop
- GH_REPOSITORY: github.com/MDAnalysis/mdanalysis.git
- GIT_CI_USER: TravisCI
- GIT_CI_EMAIL: [email protected]
- MDA_DOCDIR: package/doc/html
matrix:
- SETUP=full CYTHONIZE=true
- SETUP=minimal CYTHONIZE=false
language: python
python:
- "2.7"
env:
global:
- secure: "HIj3p+p2PV8DBVg/KGUx6n83KwB0ASE5FwOn0SMB9zxnzAqe8sapwdBQdMdq0sXB7xT1spJqRxuxOMVEVn35BNLu7bxMLfa4287C8YXcomnvmv9xruxAsjsIewnNQ80vtPVbQddBPxa4jKbqgPby5QhhAP8KANAqYe44pIV70fY="
- GH_DOC_BRANCH: develop
- GH_REPOSITORY: github.com/MDAnalysis/mdanalysis.git
- GIT_CI_USER: TravisCI
- GIT_CI_EMAIL: [email protected]
- MDA_DOCDIR: package/doc/html
matrix:
allow_failures:
- python: "3.3"
env: SETUP=full CYTHONIZE=true
include:
- python: "2.7"
env: SETUP=minimal CYTHONIZE=false
- python: "2.7"
env: SETUP=full CYTHONIZE=true
- python: "3.3"
env: SETUP=full CYTHONIZE=true
# command to install dependencies
addons:
apt:
Expand Down Expand Up @@ -41,7 +47,7 @@ install:
- chmod +x testsuite/MDAnalysisTests/mda_nosetests
# command to run tests
script:
- ./testsuite/MDAnalysisTests/mda_nosetests --with-coverage --cover-package MDAnalysis --processes=2 --process-timeout=300 --with-memleak
- ./testsuite/MDAnalysisTests/mda_nosetests --with-coverage --cover-package MDAnalysis --processes=2 --process-timeout=300 $( if [[ $TRAVIS_PYTHON_VERSION == 2.7 ]]; then echo '--with-memleak'; fi )
- |
test ${TRAVIS_PULL_REQUEST} == "false" && \
test ${TRAVIS_BRANCH} == ${GH_DOC_BRANCH} && \
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ lists of supported `trajectory formats`_ and `topology formats`_.)
# Iterate through trajectories
for ts in u.trajectory:
print ag.center_of_mass()
print(ag.center_of_mass())
Source code
===========
Expand Down
9 changes: 9 additions & 0 deletions package/MDAnalysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,12 @@
del weakref

from .migration.ten2eleven import ten2eleven

import six
if six.PY3:
warnings.warn('''\
#####
MDAnalysis on python 3 is highly experimental!
It is mostly non functional and dramatically untested.
Use at your own risks!!!
''')
2 changes: 0 additions & 2 deletions package/MDAnalysis/analysis/density.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,13 @@
"""
from __future__ import print_function

from six.moves import range

import numpy as np
import sys
import os
import os.path
import errno
import cPickle
import warnings

try:
Expand Down
9 changes: 6 additions & 3 deletions package/MDAnalysis/analysis/gnm.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@

# import copy #unused

from __future__ import print_function
from six.moves import range

import numpy as np
Expand Down Expand Up @@ -113,7 +114,7 @@ def backup_file(filename):
failure = False
break
if failure:
print "Too many backups. Clean up and try again"
print("Too many backups. Clean up and try again")
exit()


Expand Down Expand Up @@ -195,7 +196,8 @@ def generate_output(self, w, v, outputobject, time, matrix, nmodes=2, ReportVect
if ReportVector:
with open(ReportVector, "a") as oup:
for item in enumerate(v[list_map[1]]):
print >> oup, "", counter, time, item[0] + 1, w[list_map[1]], item[1]
print("", counter, time, item[0] + 1,
w[list_map[1]], item[1], file=oup)
outputobject.append((time, w[list_map[1]], v[list_map[1]]))
#outputobject.append((time, [ w[list_map[i]] for i in range(nmodes) ], [ v[list_map[i]] for i in range(
# nmodes) ] ))
Expand Down Expand Up @@ -288,7 +290,8 @@ def _get_timestep():
try:
[u, w, v] = linalg.svd(matrix)
except linalg.LinAlgError:
print "\nFrame skip at", timestep, "(SVD failed to converge). Cutoff", self.cutoff
print("\nFrame skip at", timestep,
"(SVD failed to converge). Cutoff", self.cutoff)
continue
#Save the results somewhere useful in some useful format. Usefully.
self.generate_output(w, v, self.results, timestep, matrix, ReportVector=self.ReportVector, counter=counter)
Expand Down
12 changes: 5 additions & 7 deletions package/MDAnalysis/analysis/hbonds/hbond_analysis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
#
# MDAnalysis --- http://www.MDAnalysis.org
# Copyright (c) 2006-2015 Naveen Michaud-Agrawal, Elizabeth J. Denning, Oliver Beckstein
Expand Down Expand Up @@ -309,10 +309,10 @@ class HydrogenBondAnalysis_OtherFF(HydrogenBondAnalysis):
.. automethod:: _get_bonded_hydrogens_list
"""

import six
from six.moves import range, zip, map, cPickle

from collections import defaultdict
from six.moves import range, zip, map
import numpy as np
import warnings
import logging
Expand Down Expand Up @@ -984,8 +984,6 @@ def save_table(self, filename="hbond_table.pickle"):
.. SeeAlso:: :mod:`cPickle` module and :class:`numpy.recarray`
"""
import cPickle

if self.table is None:
self.generate_table()
cPickle.dump(self.table, open(filename, 'wb'), protocol=cPickle.HIGHEST_PROTOCOL)
Expand Down Expand Up @@ -1050,7 +1048,7 @@ def count_by_type(self):

# float because of division later
tsteps = float(len(self.timesteps))
for cursor, (key, count) in enumerate(hbonds.iteritems()):
for cursor, (key, count) in enumerate(six.iteritems(hbonds)):
out[cursor] = key + (count / tsteps,)

# return array as recarray
Expand Down Expand Up @@ -1109,7 +1107,7 @@ def timesteps_by_type(self):
out = np.empty((out_nrows,), dtype=dtype)

out_row = 0
for (key, times) in hbonds.iteritems():
for (key, times) in six.iteritems(hbonds):
for tstep in times:
out[out_row] = key + (tstep,)
out_row += 1
Expand Down
77 changes: 40 additions & 37 deletions package/MDAnalysis/analysis/helanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
.. autofunction:: helanal_main
"""

from __future__ import print_function
from six.moves import range

import os
Expand Down Expand Up @@ -316,31 +316,32 @@ def helanal_trajectory(universe, selection="name CA", start=None, end=None, begi

#print out rotations across the helix to a file
with open(twist_filename, "a") as twist_output:
print >> twist_output, frame,
print(frame, end='', file=twist_output)
for loc_twist in twist:
print >> twist_output, loc_twist,
print >> twist_output, ""
print(loc_twist, end='', file=twist_output)
print("", file=twist_output)

with open(bend_filename, "a") as bend_output:
print >> bend_output, frame,
print(frame, end='', file=bend_output)
for loc_bend in bending_angles:
print >> bend_output, loc_bend,
print >> bend_output, ""
print(loc_bend, end='', file=bend_output)
print("", file=bend_output)

with open(screw_filename, "a") as rot_output:
print >> rot_output, frame,
print(frame, end='', file=rot_output)
for rotation in local_screw_angles:
print >> rot_output, rotation,
print >> rot_output, ""
print(rotation, end='', file=rot_output)
print("", file=rot_output)

with open(tilt_filename, "a") as tilt_output:
print >> tilt_output, frame,
print(frame, end='', file=tilt_output)
for tilt in local_helix_axes:
print >> tilt_output, np.rad2deg(mdamath.angle(tilt, ref_axis)),
print >> tilt_output, ""
print(np.rad2deg(mdamath.angle(tilt, ref_axis)),
end='', file=tilt_output)
print("", file=tilt_output)

with open(fitted_tilt_filename, "a") as tilt_output:
print >> tilt_output, frame, np.rad2deg(fit_tilt)
print(frame, np.rad2deg(fit_tilt), file=tilt_output)

if len(global_bending) == 0:
global_bending = [[] for item in bending_angles]
Expand All @@ -360,26 +361,26 @@ def helanal_trajectory(universe, selection="name CA", start=None, end=None, begi

bending_statistics_matrix = [[stats(col) for col in row] for row in global_bending_matrix]
with open(matrix_filename, 'w') as mat_output:
print >> mat_output, "Mean"
print("Mean", file=mat_output)
for row in bending_statistics_matrix:
for col in row:
formatted_angle = "{0:6.1f}".format(col[0])
print >> mat_output, formatted_angle,
print >> mat_output, ''
print(formatted_angle, end='', file=mat_output)
print('', file=mat_output)

print >> mat_output, "\nSD"
print('\nSD', file=mat_output)
for row in bending_statistics_matrix:
for col in row:
formatted_angle = "{0:6.1f}".format(col[1])
print >> mat_output, formatted_angle,
print >> mat_output, ''
print(formatted_angle, end='', file=mat_output)
print('', file=mat_output)

print >> mat_output, "\nABDEV"
print("\nABDEV", file=mat_output)
for row in bending_statistics_matrix:
for col in row:
formatted_angle = "{0:6.1f}".format(col[2])
print >> mat_output, formatted_angle,
print >> mat_output, ''
print(formatted_angle, end='', file=mat_output)
print('', file=mat_output)

logger.info("Height: %g SD: %g ABDEV: %g (Angstroem)", height_mean, height_sd, height_abdev)
logger.info("Twist: %g SD: %g ABDEV: %g", twist_mean, twist_sd, twist_abdev)
Expand All @@ -401,29 +402,31 @@ def helanal_trajectory(universe, selection="name CA", start=None, end=None, begi
logger.info(output)

with open(summary_filename, 'w') as summary_output:
print >> summary_output, "Height:", height_mean, "SD", height_sd, "ABDEV", height_abdev, '(nm)'
print >> summary_output, "Twist:", twist_mean, "SD", twist_sd, "ABDEV", twist_abdev
print >> summary_output, "Residues/turn:", rnou_mean, "SD", rnou_sd, "ABDEV", rnou_abdev
print >> summary_output, "Local bending angles:"
residue_statistics = zip(*bending_statistics)
print("Height:", height_mean, "SD", height_sd, "ABDEV", height_abdev, '(nm)', file=summary_output)
print("Twist:", twist_mean, "SD", twist_sd, "ABDEV", twist_abdev,
file=summary_output)
print("Residues/turn:", rnou_mean, "SD", rnou_sd, "ABDEV", rnou_abdev,
file=summary_output)
print("Local bending angles:", file=summary_output)
residue_statistics = list(zip(*bending_statistics))
measure_names = ["Mean ", "SD ", "ABDEV"]
print >> summary_output, "ResID",
print("ResID", end='', file=summary_output)
if start is None:
for item in range(4, len(residue_statistics[0]) + 4):
output = "{0:8d}".format(item)
print >> summary_output, output,
print(output, end='', file=summary_output)
else:
for item in range(start + 3, len(residue_statistics[0]) + start + 3):
output = "{0:8d}".format(item)
print >> summary_output, output,
print >> summary_output, ""
print(output, end='', file=summary_output)
print('', file=summary_output)

for measure, name in zip(residue_statistics, measure_names):
print >> summary_output, name,
print(name, end='', file=summary_output)
for residue in measure:
output = "{0:8.1f}".format(residue)
print >> summary_output, output,
print >> summary_output, ''
print(output, end='', file=summary_output)
print('', file=summary_output)


def tilt_correct(number):
Expand Down Expand Up @@ -596,9 +599,9 @@ def origin_pdb(origins, pdbfile):
i = 1
for xyz in origins:
tmp = "ATOM {0:3d} CA ALA {1:3d} {2:8.3f}{3:8.3f}{4:8.3f} 1.00 0.00".format(i, i, xyz[0], xyz[1], xyz[2])
print >> output, tmp
print(tmp, file=output)
i += 1
print >> output, "TER\nENDMDL"
print("TER\nENDMDL", file=output)


def main_loop(positions, ref_axis=None):
Expand Down
4 changes: 2 additions & 2 deletions package/MDAnalysis/analysis/hole.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@
iterate over the sorted profiles (see :meth:`HOLEtraj.sorted_profiles_iter`) ::
for q, profile in H:
print "orderparameter = %g" % q
print "min(R) = %g" % profile.radius.min()
print("orderparameter = %g" % q)
print("min(R) = %g" % profile.radius.min())
Data structures
Expand Down
19 changes: 9 additions & 10 deletions package/MDAnalysis/analysis/psa.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
#
# MDAnalysis --- http://www.MDAnalysis.org
# Copyright (c) 2006-2015 Naveen Michaud-Agrawal, Elizabeth J. Denning, Oliver Beckstein
Expand Down Expand Up @@ -217,16 +217,15 @@
.. |Np| replace:: :math:`N_p`
"""

from six.moves import range
import six
from six.moves import range, cPickle

import numpy as np

import MDAnalysis
import MDAnalysis.analysis.align
from MDAnalysis import NoDataError

import cPickle as pickle
import os

import logging
Expand Down Expand Up @@ -1144,7 +1143,7 @@ def __init__(self, universes, reference=None, ref_select='name CA',
'paths' : '/paths',
'distance_matrices' : '/distance_matrices',
'plots' : '/plots'}
for dir_name, directory in self.datadirs.iteritems():
for dir_name, directory in six.iteritems(self.datadirs):
try:
full_dir_name = os.path.join(self.targetdir, dir_name)
os.makedirs(full_dir_name)
Expand Down Expand Up @@ -1173,11 +1172,11 @@ def __init__(self, universes, reference=None, ref_select='name CA',
self._labels_pkl = os.path.join(self.targetdir, "psa_labels.pkl")
# Pickle topology and trajectory filenames for this analysis to curdir
with open(self._top_pkl, 'wb') as output:
pickle.dump(self.top_name, output)
cPickle.dump(self.top_name, output)
with open(self._trjs_pkl, 'wb') as output:
pickle.dump(self.trj_names, output)
cPickle.dump(self.trj_names, output)
with open(self._labels_pkl, 'wb') as output:
pickle.dump(self.labels, output)
cPickle.dump(self.labels, output)

self.natoms = None
self.npaths = None
Expand Down Expand Up @@ -1259,7 +1258,7 @@ def generate_paths(self, **kwargs):
self.fit_trj_names = fit_trj_names
if save:
with open(self._fit_trjs_pkl, 'wb') as output:
pickle.dump(self.fit_trj_names, output)
cPickle.dump(self.fit_trj_names, output)
if store:
filename = kwargs.pop('filename', None)
self.save_paths(filename=filename)
Expand Down Expand Up @@ -1407,7 +1406,7 @@ def save_paths(self, filename=None):
logger.info("Wrote path to file %r", current_outfile)
self.path_names = path_names
with open(self._paths_pkl, 'wb') as output:
pickle.dump(self.path_names, output)
cPickle.dump(self.path_names, output)
return filename


Expand Down
Loading

0 comments on commit ebc2f59

Please sign in to comment.