Unverified Commit 79202805 authored by Timothy Palpant's avatar Timothy Palpant Committed by GitHub
Browse files

Fix import of netcdf_file for compatibility with scipy 1.14 (#4602)



* Fix import of netcdf_file for scipy 1.14

* Fix indentation

---------
Co-authored-by: default avatarTimothy Palpant <tim@atommapper.com>
parent ab7ad049
...@@ -1440,14 +1440,9 @@ class AmberNetcdfRestart(object): ...@@ -1440,14 +1440,9 @@ class AmberNetcdfRestart(object):
""" """
def __init__(self, filename, asNumpy=False): def __init__(self, filename, asNumpy=False):
try: try:
from scipy.io import NetCDFFile from scipy.io import netcdf_file
except ImportError: except ImportError:
# scipy < 1.8.0 raise ImportError('scipy is necessary to parse NetCDF restarts')
try:
from scipy.io.netcdf import NetCDFFile
except ImportError:
raise ImportError('scipy is necessary to parse NetCDF '
'restarts')
self.filename = filename self.filename = filename
self.velocities = self.boxVectors = self.time = None self.velocities = self.boxVectors = self.time = None
...@@ -1457,10 +1452,10 @@ class AmberNetcdfRestart(object): ...@@ -1457,10 +1452,10 @@ class AmberNetcdfRestart(object):
# to valid memory while the file handle is open. Since the context # to valid memory while the file handle is open. Since the context
# manager GCs the ncfile handle, the memory for the original variables # manager GCs the ncfile handle, the memory for the original variables
# is no longer valid. So copy those arrays while the handle is still # is no longer valid. So copy those arrays while the handle is still
# open. This is unnecessary in scipy v.0.12 and lower because NetCDFFile # open. This is unnecessary in scipy v.0.12 and lower because netcdf_file
# accidentally leaks the file handle, but that was 'fixed' in 0.13. This # accidentally leaks the file handle, but that was 'fixed' in 0.13. This
# fix taken from MDTraj # fix taken from MDTraj
ncfile = NetCDFFile(filename, 'r') ncfile = netcdf_file(filename, 'r')
try: try:
self.natom = ncfile.dimensions['atom'] self.natom = ncfile.dimensions['atom']
self.coordinates = np.array(ncfile.variables['coordinates'][:]) self.coordinates = np.array(ncfile.variables['coordinates'][:])
......
...@@ -5,14 +5,9 @@ from openmm import * ...@@ -5,14 +5,9 @@ from openmm import *
from openmm.unit import * from openmm.unit import *
import openmm.app.element as elem import openmm.app.element as elem
try: try:
from scipy.io import NetCDFFile from scipy.io import netcdf_file
SCIPY_IMPORT_FAILED = False SCIPY_IMPORT_FAILED = False
except ImportError: except ImportError:
try:
# scipy < 1.8.0
from scipy.io import netcdf
SCIPY_IMPORT_FAILED = False
except:
SCIPY_IMPORT_FAILED = True SCIPY_IMPORT_FAILED = True
def compareByElement(array1, array2, cmp): def compareByElement(array1, array2, cmp):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment