Skip to content

Commit

Permalink
Remove py2 things from coordinates & topology (#2759)
Browse files Browse the repository at this point in the history
* Removes py2 things from coordinates

* Removes py2 things from coordinate tests

* Removes py2 things from topology
  • Loading branch information
IAlibay authored Jun 16, 2020
1 parent 34288cc commit 0d951a2
Show file tree
Hide file tree
Showing 104 changed files with 65 additions and 330 deletions.
5 changes: 0 additions & 5 deletions package/MDAnalysis/coordinates/DLPoly.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@
.. _Poly: http://www.stfc.ac.uk/SCD/research/app/ccg/software/DL_POLY/44516.aspx
"""
from __future__ import (absolute_import, division,
print_function, unicode_literals)

from six.moves import range

import numpy as np

from . import base
Expand Down
2 changes: 0 additions & 2 deletions package/MDAnalysis/coordinates/DMS.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
.. _Desmond: http://www.deshawresearch.com/resources_desmond.html
.. _DMS: http://www.deshawresearch.com/Desmond_Users_Guide-0.7.pdf
"""
from __future__ import absolute_import

import numpy as np
import sqlite3

Expand Down
2 changes: 0 additions & 2 deletions package/MDAnalysis/coordinates/GMS.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
:members:
"""
from __future__ import absolute_import

import os
import errno
import re
Expand Down
5 changes: 1 addition & 4 deletions package/MDAnalysis/coordinates/GSD.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
:inherited-members:
"""
from __future__ import absolute_import, division
from six import raise_from

import numpy as np
import os
import gsd.hoomd
Expand Down Expand Up @@ -103,7 +100,7 @@ def _read_frame(self, frame):
try :
myframe = self._file[frame]
except IndexError:
raise_from(IOError, None)
raise IOError from None

# set frame number
self._frame = frame
Expand Down
3 changes: 0 additions & 3 deletions package/MDAnalysis/coordinates/INPCRD.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
:members:
"""
from __future__ import absolute_import, division, print_function, unicode_literals

from six.moves import range

from . import base

Expand Down
24 changes: 8 additions & 16 deletions package/MDAnalysis/coordinates/LAMMPS.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@
:inherited-members:
"""
from __future__ import absolute_import

from six.moves import zip, range, map
from six import raise_from
import os
import numpy as np

Expand Down Expand Up @@ -161,9 +157,8 @@ def __init__(self, *args, **kwargs):
if units.unit_types[unit] != unit_type:
raise TypeError("LAMMPS DCDWriter: wrong unit {0!r} for unit type {1!r}".format(unit, unit_type))
except KeyError:
raise_from(
ValueError("LAMMPS DCDWriter: unknown unit {0!r}".format(unit)),
None)
errmsg = f"LAMMPS DCDWriter: unknown unit {unit}"
raise ValueError(errmsg) from None
super(DCDWriter, self).__init__(*args, **kwargs)


Expand Down Expand Up @@ -343,10 +338,9 @@ def _write_bonds(self, bonds):
self.f.write('{:d} {:d} '.format(i, int(bond.type))+\
' '.join((bond.atoms.indices + 1).astype(str))+'\n')
except TypeError:
raise_from(TypeError('LAMMPS DATAWriter: Trying to write bond, '
'but bond type {} is not '
'numerical.'.format(bond.type)),
None)
errmsg = (f"LAMMPS DATAWriter: Trying to write bond, but bond "
f"type {bond.type} is not numerical.")
raise TypeError(errmsg) from None

def _write_dimensions(self, dimensions):
"""Convert dimensions to triclinic vectors, convert lengths to native
Expand Down Expand Up @@ -405,11 +399,9 @@ def write(self, selection, frame=None):
try:
atoms.types.astype(np.int32)
except ValueError:
raise_from(
ValueError(
'LAMMPS.DATAWriter: atom types must be '
'convertible to integers'),
None)
errmsg = ("LAMMPS.DATAWriter: atom types must be convertible to "
"integers")
raise ValueError(errmsg) from None

try:
velocities = atoms.velocities
Expand Down
2 changes: 0 additions & 2 deletions package/MDAnalysis/coordinates/MMTF.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
.. _MMTF: https://mmtf.rcsb.org/
"""
from __future__ import absolute_import

import mmtf

from . import base
Expand Down
14 changes: 5 additions & 9 deletions package/MDAnalysis/coordinates/MOL2.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@
1 BENZENE 1 PERM 0 **** **** 0 ROOT
"""
from __future__ import absolute_import
from six import raise_from

import numpy as np

from . import base
Expand Down Expand Up @@ -219,9 +216,9 @@ def _read_frame(self, frame):
try:
block = self.frames[frame]
except IndexError:
raise_from(IOError("Invalid frame {0} for trajectory with length {1}"
"".format(frame, len(self))),
None)
errmsg = (f"Invalid frame {frame} for trajectory with length "
f"{len(self)}")
raise IOError(errmsg) from None

sections, coords = self.parse_block(block)

Expand Down Expand Up @@ -319,9 +316,8 @@ def encode_block(self, obj):
try:
molecule = ts.data['molecule']
except KeyError:
raise_from(NotImplementedError(
"MOL2Writer cannot currently write non MOL2 data"),
None)
errmsg = "MOL2Writer cannot currently write non MOL2 data"
raise NotImplementedError(errmsg) from None

# Need to remap atom indices to 1 based in this selection
mapping = {a: i for i, a in enumerate(obj.atoms, start=1)}
Expand Down
17 changes: 5 additions & 12 deletions package/MDAnalysis/coordinates/PDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@
http://www.wwpdb.org/documentation/file-format-content/format32/v3.2.html
"""
from __future__ import absolute_import

from six.moves import range, zip
from six import raise_from, StringIO, BytesIO

from io import StringIO, BytesIO
import os
import errno
import itertools
Expand Down Expand Up @@ -394,7 +390,7 @@ def _read_frame(self, frame):
start = self._start_offsets[frame]
stop = self._stop_offsets[frame]
except IndexError: # out of range of known frames
raise_from(IOError, None)
raise IOError from None

pos = 0
occupancy = np.ones(self.n_atoms)
Expand Down Expand Up @@ -940,12 +936,9 @@ def _write_next_frame(self, ts=None, **kwargs):
try:
ts = self.ts
except AttributeError:
raise_from(
NoDataError(
"PBDWriter: no coordinate data to write to "
"trajectory file"
),
None)
errmsg = ("PBDWriter: no coordinate data to write to "
"trajectory file")
raise NoDataError(errmsg) from None
self._check_pdb_coordinates()
self._write_timestep(ts, **kwargs)

Expand Down
1 change: 0 additions & 1 deletion package/MDAnalysis/coordinates/PDBQT.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
http://autodock.scripps.edu/
"""

from __future__ import absolute_import
import os
import errno
import itertools
Expand Down
1 change: 0 additions & 1 deletion package/MDAnalysis/coordinates/ParmEd.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"""
from __future__ import absolute_import
import functools

from . import base
Expand Down
6 changes: 1 addition & 5 deletions package/MDAnalysis/coordinates/TXYZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@
:inherited-members:
"""
from __future__ import absolute_import, division
from six.moves import range
from six import raise_from

import numpy as np
import os
import errno
Expand Down Expand Up @@ -155,7 +151,7 @@ def _read_next_timestep(self, ts=None):
ts.frame += 1
return ts
except (ValueError, IndexError) as err:
raise_from(EOFError(err), None)
raise EOFError(err) from None

def _reopen(self):
self.close()
Expand Down
4 changes: 1 addition & 3 deletions package/MDAnalysis/coordinates/XDR.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
MDAnalysis.coordinates.TRR: Read and write GROMACS TRR trajectory files.
MDAnalysis.lib.formats.libmdaxdr: Low level xdr format reader
"""
from __future__ import absolute_import
import six

import errno
import numpy as np
Expand Down Expand Up @@ -83,7 +81,7 @@ def read_numpy_offsets(filename):
"""
try:
return {k: v for k, v in six.iteritems(np.load(filename))}
return {k: v for k, v in np.load(filename).items()}
except IOError:
warnings.warn("Failed to load offsets file {}\n".format(filename))
return False
Expand Down
3 changes: 0 additions & 3 deletions package/MDAnalysis/coordinates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,8 @@ class can choose an appropriate reader automatically.
raw :class:`~MDAnalysis.coordinates.base.Timestep` objects.
"""
from __future__ import absolute_import
__all__ = ['reader', 'writer']

import six

from . import base
from .core import reader, writer
from . import chain
Expand Down
2 changes: 0 additions & 2 deletions package/MDAnalysis/coordinates/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
.. automethod:: _chained_iterator
"""
from __future__ import absolute_import

import warnings

import os.path
Expand Down
13 changes: 2 additions & 11 deletions package/MDAnalysis/coordinates/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
.. autofunction:: get_writer_for
"""
from __future__ import absolute_import

import six

from ..lib import util
from ..lib.mdamath import triclinic_box, triclinic_vectors, box_volume
Expand Down Expand Up @@ -84,14 +81,8 @@ def reader(filename, format=None, **kwargs):
try:
return Reader(filename, **kwargs)
except ValueError:
six.raise_from(
TypeError(
'Unable to read {fn} with {r}.'.format(
fn=filename,
r=Reader
)
),
None)
errmsg = f'Unable to read {filename} with {Reader}.'
raise TypeError(errmsg) from None


def writer(filename, n_atoms=None, **kwargs):
Expand Down
27 changes: 11 additions & 16 deletions package/MDAnalysis/coordinates/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@
:inherited-members:
"""
from __future__ import absolute_import
from six import raise_from
import logging
import errno
import numpy as np
Expand Down Expand Up @@ -319,10 +317,9 @@ def __init__(self, coordinate_array, order='fac',
if coordinate_array.ndim == 2 and coordinate_array.shape[1] == 3:
coordinate_array = coordinate_array[np.newaxis, :, :]
except AttributeError:
raise_from(TypeError("The input has to be a numpy.ndarray that "
"corresponds to the layout specified by the "
"'order' keyword."),
None)
errmsg = ("The input has to be a numpy.ndarray that corresponds "
"to the layout specified by the 'order' keyword.")
raise TypeError(errmsg) from None

self.set_array(coordinate_array, order)
self.n_frames = \
Expand All @@ -334,10 +331,9 @@ def __init__(self, coordinate_array, order='fac',
try:
velocities = np.asarray(velocities, dtype=np.float32)
except ValueError:
raise_from(
TypeError("'velocities' must be array-like got {}"
"".format(type(velocities))),
None)
errmsg = (f"'velocities' must be array-like got "
f"{type(velocities)}")
raise TypeError(errmsg) from None
# if single frame, make into array of 1 frame
if velocities.ndim == 2:
velocities = velocities[np.newaxis, :, :]
Expand All @@ -354,9 +350,8 @@ def __init__(self, coordinate_array, order='fac',
try:
forces = np.asarray(forces, dtype=np.float32)
except ValueError:
raise_from(TypeError("'forces' must be array like got {}"
"".format(type(forces))),
None)
errmsg = f"'forces' must be array like got {type(forces)}"
raise TypeError(errmsg) from None
if forces.ndim == 2:
forces = forces[np.newaxis, :, :]
if not forces.shape == self.coordinate_array.shape:
Expand Down Expand Up @@ -386,9 +381,9 @@ def __init__(self, coordinate_array, order='fac',
try:
dimensions = np.asarray(dimensions, dtype=np.float32)
except ValueError:
raise_from(TypeError("'dimensions' must be array-like got {}"
"".format(type(dimensions))),
None)
errmsg = (f"'dimensions' must be array-like got "
f"{type(dimensions)}")
raise TypeError(errmsg) from None
if dimensions.shape == (6,):
# single box, tile this to trajectory length
# allows modifying the box of some frames
Expand Down
2 changes: 0 additions & 2 deletions package/MDAnalysis/coordinates/null.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
:members:
"""
from __future__ import absolute_import

from . import base


Expand Down
9 changes: 3 additions & 6 deletions package/MDAnalysis/topology/CRDParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
:inherited-members:
"""
from __future__ import absolute_import
from six import raise_from

import numpy as np

from ..lib.util import openany, FORTRANReader
Expand Down Expand Up @@ -123,9 +120,9 @@ def parse(self, **kwargs):
(serial, resnum, resName, name,
x, y, z, segid, resid, tempFactor) = r.read(line)
except Exception:
raise_from(ValueError("Check CRD format at line {0}: {1}"
"".format(linenum + 1, line.rstrip())),
None)
errmsg = (f"Check CRD format at line {linenum + 1}: "
f"{line.rstrip()}")
raise ValueError(errmsg) from None

atomids.append(serial)
atomnames.append(name)
Expand Down
3 changes: 0 additions & 3 deletions package/MDAnalysis/topology/DLPolyParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
.. _Poly: http://www.stfc.ac.uk/SCD/research/app/ccg/software/DL_POLY/44516.aspx
"""
from __future__ import (absolute_import, division,
print_function, unicode_literals)

import numpy as np

from . import guessers
Expand Down
Loading

0 comments on commit 0d951a2

Please sign in to comment.