Patching Dec 9, 2021 6-7a CST- All GitLab services may be unavailable for 5-10 minutes

Commit 2442b89e authored by Wei's avatar Wei
Browse files

romspline converted to python3

parent 0ac59b9e
...@@ -69,7 +69,7 @@ class _ImportStates(object): ...@@ -69,7 +69,7 @@ class _ImportStates(object):
import numpy as np import numpy as np
except: except:
# Raise an exception if numpy can't be imported # Raise an exception if numpy can't be imported
raise Exception, "Error: Cannot import `NumPy` module." raise Exception("Error: Cannot import `NumPy` module.")
# Try importing scipy.interpolate.UnivariateSpline class # Try importing scipy.interpolate.UnivariateSpline class
try: try:
...@@ -77,14 +77,14 @@ class _ImportStates(object): ...@@ -77,14 +77,14 @@ class _ImportStates(object):
except: except:
# Raise an exception if UnivariateSpline can't be imported # Raise an exception if UnivariateSpline can't be imported
# This class is crucial to RomSpline # This class is crucial to RomSpline
raise Exception, "Error: Cannot import `scipy.interpolate.UnivariateSpline` class." raise Exception("Error: Cannot import `scipy.interpolate.UnivariateSpline` class.")
# Try importing h5py module # Try importing h5py module
try: try:
import h5py import h5py
self._H5PY = True self._H5PY = True
except: except:
print "Warning: Cannot import `h5py` module. File I/O features will be limited to text formats." print("Warning: Cannot import `h5py` module. File I/O features will be limited to text formats.")
self._H5PY = False self._H5PY = False
# Try importing matplotlib module # Try importing matplotlib module
...@@ -92,7 +92,7 @@ class _ImportStates(object): ...@@ -92,7 +92,7 @@ class _ImportStates(object):
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
self._MATPLOTLIB = True self._MATPLOTLIB = True
except: except:
print "Warning: Cannot import `matplotlib.pyplot` module." print("Warning: Cannot import `matplotlib.pyplot` module.")
self._MATPLOTLIB = False self._MATPLOTLIB = False
# Try importing futures module # Try importing futures module
...@@ -100,7 +100,7 @@ class _ImportStates(object): ...@@ -100,7 +100,7 @@ class _ImportStates(object):
from concurrent.futures import ProcessPoolExecutor, wait, as_completed from concurrent.futures import ProcessPoolExecutor, wait, as_completed
self._FUTURES = True self._FUTURES = True
except: except:
print "Warning: Cannot import `futures` module. Try running `pip install futures` to install." print("Warning: Cannot import `futures` module. Try running `pip install futures` to install.")
self._FUTURES = False self._FUTURES = False
# Try importing multiprocessing module # Try importing multiprocessing module
...@@ -108,7 +108,7 @@ class _ImportStates(object): ...@@ -108,7 +108,7 @@ class _ImportStates(object):
from multiprocessing import cpu_count from multiprocessing import cpu_count
self._MP = True self._MP = True
except: except:
print "Warning: Cannot import `multiprocessing` module." print("Warning: Cannot import `multiprocessing` module.")
self._MP = False self._MP = False
# If can import both futures and multiprocessing modules # If can import both futures and multiprocessing modules
...@@ -118,7 +118,7 @@ class _ImportStates(object): ...@@ -118,7 +118,7 @@ class _ImportStates(object):
if self._FUTURES and self._MP: if self._FUTURES and self._MP:
self._PARALLEL = True self._PARALLEL = True
else: else:
print "Warning: Parallel computation options will be unavailable." print("Warning: Parallel computation options will be unavailable.")
self._PARALLEL = False self._PARALLEL = False
state = _ImportStates() state = _ImportStates()
...@@ -128,12 +128,12 @@ state = _ImportStates() ...@@ -128,12 +128,12 @@ state = _ImportStates()
# Import submodules # # Import submodules #
##################### #####################
from greedy import * # For building reduced-order splines from .greedy import * # For building reduced-order splines
from convergence import * # For studying convergence from .convergence import * # For studying convergence
from random_seeds import * # For studying the effect of seed points on reduced data sizes from .random_seeds import * # For studying the effect of seed points on reduced data sizes
from cross_validation import * # For estimating (global) interpolation errors from .cross_validation import * # For estimating (global) interpolation errors
from build_spline import * # Convenience module for bulding reduced-order spline from .build_spline import * # Convenience module for bulding reduced-order spline
# with a global interpolation error estimate from cross-validation # with a global interpolation error estimate from cross-validation
from example import * # Built-in function for testing and demonstration purposes from .example import * # Built-in function for testing and demonstration purposes
from regression import * # Regression testing from .regression import * # Regression testing
from __init__ import state from .__init__ import state
if state._MATPLOTLIB: if state._MATPLOTLIB:
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
import greedy, random_seeds, cross_validation from . import greedy, random_seeds, cross_validation
...@@ -77,7 +77,7 @@ def build_spline(x, y, tol=1e-6, deg=None, rel=False, seeds=None, small=True, cv ...@@ -77,7 +77,7 @@ def build_spline(x, y, tol=1e-6, deg=None, rel=False, seeds=None, small=True, cv
# Build a reduced-order spline with given specifications (deg, tol, etc.) # Build a reduced-order spline with given specifications (deg, tol, etc.)
if small is False: if small is False:
if verbose: if verbose:
print "\nBuilding the reduced-order spline...", print("\nBuilding the reduced-order spline...", end=' ')
if deg is None: if deg is None:
deg = 5 deg = 5
spline = greedy.ReducedOrderSpline(x, y, tol=tol, deg=deg, rel=rel, seeds=seeds, verbose=verbose) spline = greedy.ReducedOrderSpline(x, y, tol=tol, deg=deg, rel=rel, seeds=seeds, verbose=verbose)
...@@ -85,7 +85,7 @@ def build_spline(x, y, tol=1e-6, deg=None, rel=False, seeds=None, small=True, cv ...@@ -85,7 +85,7 @@ def build_spline(x, y, tol=1e-6, deg=None, rel=False, seeds=None, small=True, cv
# Otherwise, sample the seed values to find a small reduced-order spline # Otherwise, sample the seed values to find a small reduced-order spline
else: else:
print "\nFinding a smallest reduced-order data set..." print("\nFinding a smallest reduced-order data set...")
if small is True: # Default number of seed sets to sample is 10 if small is True: # Default number of seed sets to sample is 10
num = 10 num = 10
else: # Otherwise, `small` is the number of sets of seed points to sample else: # Otherwise, `small` is the number of sets of seed points to sample
...@@ -96,7 +96,7 @@ def build_spline(x, y, tol=1e-6, deg=None, rel=False, seeds=None, small=True, cv ...@@ -96,7 +96,7 @@ def build_spline(x, y, tol=1e-6, deg=None, rel=False, seeds=None, small=True, cv
if cv is not False: if cv is not False:
print "\nPerforming Monte Carlo K-fold cross-validation..." print("\nPerforming Monte Carlo K-fold cross-validation...")
if cv is True: # Default number of Monte Carlo K-fold cross-validation studies to perform if cv is True: # Default number of Monte Carlo K-fold cross-validation studies to perform
num = 10 num = 10
else: else:
......
from __init__ import state from .__init__ import state
if state._MATPLOTLIB: if state._MATPLOTLIB:
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
import greedy from . import greedy
from helpers import Linfty from .helpers import Linfty
...@@ -203,7 +203,7 @@ class Convergence(object): ...@@ -203,7 +203,7 @@ class Convergence(object):
return ax return ax
else: else:
print "No data to plot. Run `make` method." print("No data to plot. Run `make` method.")
def _plot_Linfty_errors(self, ax): def _plot_Linfty_errors(self, ax):
...@@ -234,7 +234,7 @@ class Convergence(object): ...@@ -234,7 +234,7 @@ class Convergence(object):
return ax return ax
else: else:
print "No data to plot. Run `make` method." print("No data to plot. Run `make` method.")
def _plot_sizes(self, ax, axes=None): def _plot_sizes(self, ax, axes=None):
...@@ -308,6 +308,6 @@ class Convergence(object): ...@@ -308,6 +308,6 @@ class Convergence(object):
return ax return ax
else: else:
print "No data to plot. Run `make` method." print("No data to plot. Run `make` method.")
from __init__ import state from .__init__ import state
if state._MATPLOTLIB: if state._MATPLOTLIB:
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
if state._PARALLEL: if state._PARALLEL:
from __init__ import ProcessPoolExecutor, wait, as_completed, cpu_count from .__init__ import ProcessPoolExecutor, wait, as_completed, cpu_count
import numpy as np import numpy as np
import greedy from . import greedy
from helpers import * from .helpers import *
...@@ -226,7 +226,7 @@ class CrossValidation(object): ...@@ -226,7 +226,7 @@ class CrossValidation(object):
for nn in range(n): for nn in range(n):
if verbose and not (nn+1)%10: if verbose and not (nn+1)%10:
print "Trials completed:", nn+1 print("Trials completed:", nn+1)
self.Kfold(x, y, K=K, parallel=parallel, random=random) self.Kfold(x, y, K=K, parallel=parallel, random=random)
self.all_args[nn] = self.args self.all_args[nn] = self.args
...@@ -327,7 +327,7 @@ class CrossValidation(object): ...@@ -327,7 +327,7 @@ class CrossValidation(object):
if ax is None: if ax is None:
fig, ax = plt.subplots(nrows=1, ncols=1) fig, ax = plt.subplots(nrows=1, ncols=1)
x_plot = range(len(self._partitions)) x_plot = list(range(len(self._partitions)))
ax.plot(x_plot, self.errors, color=color, marker=marker, linestyle=linestyle) ax.plot(x_plot, self.errors, color=color, marker=marker, linestyle=linestyle)
ax.set_xlabel('Partition') ax.set_xlabel('Partition')
...@@ -343,7 +343,7 @@ class CrossValidation(object): ...@@ -343,7 +343,7 @@ class CrossValidation(object):
return ax return ax
else: else:
print "No data attributes to plot." print("No data attributes to plot.")
def plot_monte_carlo_errors(self, x=None, n=20, axes='plot', ax=None, show=True, color='k', marker='.'): def plot_monte_carlo_errors(self, x=None, n=20, axes='plot', ax=None, show=True, color='k', marker='.'):
...@@ -409,6 +409,6 @@ class CrossValidation(object): ...@@ -409,6 +409,6 @@ class CrossValidation(object):
return ax return ax
else: else:
print "No data attributes to plot." print("No data attributes to plot.")
import numpy as np
################################
# Class for generating some #
# test data for showing how #
# to use the code in the #
# Jupyter/IPython notebooks #
################################
class TestData(object):
"""Generate the test data used as in example IPython notebooks
for demonstrating the construction, properties, and errors
of a reduced-order spline interpolant.
"""
def __init__(self, num=4001, noise=0., uv=0.):
"""Create a TestData object.
Input
-----
num -- number of samples to evaluate the function
in domain [-1,1]
noise -- amplitude of stochastic fluctuations added
to smooth function values
(default is 0.)
uv -- amplitude of high-frequency (i.e., ultra-violet)
features added to smooth function values
(default is 0.)
Attributes
----------
x -- samples
y -- values of sampled function
"""
# Generate test data
self.x = np.linspace(-1, 1, num)
self.y = self.f(self.x, noise=noise, uv=uv)
def f(self, x, noise=0., uv=0.):
"""Function to sample for reduced-order spline examples
Inputs
------
x -- values to sample the (smooth) function
noise -- amplitude of stochastic fluctuations added
to smooth function values
(default is 0.)
uv -- amplitude of high-frequency (i.e., ultra-violet)
features added to smooth function values
(default is 0.)
Output
------
sampled function values
Comments
--------
The function being evaluated is
f(x) = 100.*( (1.+x) * sin(5.*(x-0.2)**2)
+ exp(-(x-0.5)**2/2./0.01) * sin(100*x)
)
"""
# Validate inputs
x = np.asarray(x)
# Return smooth function values
ans = 100.*( (x+1.)*np.sin(5.*(x-0.2)**2) + np.exp(-(x-0.5)**2/2./0.01)*np.sin(100*x) )
# Return smooth function values with high-frequency (UV) features
if uv != 0.:
assert type(uv) in [float, int], "Expecting integer or float type."
ans += float(uv)*self.uv(x)
# Return smooth function values with stochastic noise
if noise != 0.:
assert type(noise) in [float, int], "Expecting integer or float type."
ans += float(noise)*np.random.randn(len(x))
return ans
def dfdx(self, x):
"""Analytic derivative of f
Inputs
------
x -- values to sample the derivative of
the function f(x) (see self.f method)
Outputs
-------
ans -- values of analytically calculated
derivative of the function
"""
x = np.asarray(x)
a = 10.*(-0.2+x)*(1.+x)*np.cos(5.*(-0.2 + x)**2)
b = 100.*np.exp(-50.*(-0.5+x)**2)*np.cos(100.*x)
c = np.sin(5.*(-0.2+x)**2)
d = -100.*np.exp(-50.*(-0.5+x)**2)*(-0.5+x)*np.sin(100.*x)
ans = 100.*(a+b+c+d)
return ans
def uv(self, x, width=20):
"""Generate high-frequency oscillations
Inputs
------
x -- values to sample the high-frequency
oscillations
width -- number of samples corresponding to
the period of the high-frequency
oscillations
(default is 20)
Outputs
-------
array of high-frequency oscillating values
"""
X = x[width] - x[0]
return np.sin(len(x)/X * x)
from __init__ import _ImportStates from .__init__ import _ImportStates
state = _ImportStates() state = _ImportStates()
if state._MATPLOTLIB: if state._MATPLOTLIB:
...@@ -189,10 +189,10 @@ class ReducedOrderSpline(object): ...@@ -189,10 +189,10 @@ class ReducedOrderSpline(object):
if abs: if abs:
errors = np.abs(errors) errors = np.abs(errors)
else: else:
raise Exception, "No spline interpolant to compare against. Run the greedy method." raise Exception("No spline interpolant to compare against. Run the greedy method.")
if verbose: if verbose:
print "Requested tolerance of {} met: ".format(self._tol), np.all(np.abs(errors) <= self.tol) print("Requested tolerance of {} met: ".format(self._tol), np.all(np.abs(errors) <= self.tol))
return errors return errors
...@@ -251,10 +251,10 @@ class ReducedOrderSpline(object): ...@@ -251,10 +251,10 @@ class ReducedOrderSpline(object):
return ax return ax
else: else:
print "No data to plot. Run `greedy` method." print("No data to plot. Run `greedy` method.")
else: else:
print "Error: matplotlib.pyplot module not imported." print("Error: matplotlib.pyplot module not imported.")
def write(self, file, slim=False): def write(self, file, slim=False):
...@@ -289,7 +289,7 @@ class ReducedOrderSpline(object): ...@@ -289,7 +289,7 @@ class ReducedOrderSpline(object):
""" """
if not state._H5PY: if not state._H5PY:
print "h5py module not imported. Try writing data to text (.txt) format." print("h5py module not imported. Try writing data to text (.txt) format.")
return return
# If file is an HDF5 file or group descriptor... # If file is an HDF5 file or group descriptor...
...@@ -310,12 +310,12 @@ class ReducedOrderSpline(object): ...@@ -310,12 +310,12 @@ class ReducedOrderSpline(object):
fp = h5py.File(file, 'w') fp = h5py.File(file, 'w')
isopen = True isopen = True
except: except:
raise Exception, "Could not open file for writing." raise Exception("Could not open file for writing.")
if isopen: if isopen:
self._write(fp, slim=slim) self._write(fp, slim=slim)
fp.close() fp.close()
else: else:
print "Error: h5py module is not imported. Try writing data to text (.txt) format." print("Error: h5py module is not imported. Try writing data to text (.txt) format.")
return return
# Text format # Text format
...@@ -364,7 +364,7 @@ class ReducedOrderSpline(object): ...@@ -364,7 +364,7 @@ class ReducedOrderSpline(object):
if not slim: if not slim:
descriptor.create_dataset('errors', data=self.errors, dtype='double', compression='gzip', shuffle=True) descriptor.create_dataset('errors', data=self.errors, dtype='double', compression='gzip', shuffle=True)
else: else:
raise Exception, "Descriptor not recognized." raise Exception("Descriptor not recognized.")
def read(self, file, group=None): def read(self, file, group=None):
...@@ -418,21 +418,21 @@ def readSpline(file, group=None): ...@@ -418,21 +418,21 @@ def readSpline(file, group=None):
fp = h5py.File(file, 'r') fp = h5py.File(file, 'r')
isopen = True isopen = True
except: except:
raise Exception, "Could not open file for reading." raise Exception("Could not open file for reading.")
if isopen: if isopen:
gp = fp[group] if group else fp gp = fp[group] if group else fp
deg = gp['deg'][()] deg = gp['deg'][()]
tol = gp['tol'][()] tol = gp['tol'][()]
X = gp['X'][:] X = gp['X'][:]
Y = gp['Y'][:] Y = gp['Y'][:]
if hasattr(gp, 'errors') or 'errors' in gp.keys(): if hasattr(gp, 'errors') or 'errors' in list(gp.keys()):
errors = gp['errors'][:] errors = gp['errors'][:]
else: else:
errors = [] errors = []
fp.close() fp.close()
_made = True _made = True
else: else:
print "Error: h5py module is not imported." print("Error: h5py module is not imported.")
return return
# Text format # Text format
...@@ -449,7 +449,7 @@ def readSpline(file, group=None): ...@@ -449,7 +449,7 @@ def readSpline(file, group=None):
errs_isopen = False errs_isopen = False
isopen = True isopen = True
except: except:
raise IOError, "Could not open file(s) for reading." raise IOError("Could not open file(s) for reading.")
if isopen: if isopen:
deg = int(fp_deg.read()) deg = int(fp_deg.read())
...@@ -492,7 +492,7 @@ def readSpline(file, group=None): ...@@ -492,7 +492,7 @@ def readSpline(file, group=None):
spline._made = _made spline._made = _made
return spline return spline
else: else:
raise Exception, "Reduced-order spline interpolant could not be constructed from file." raise Exception("Reduced-order spline interpolant could not be constructed from file.")
...@@ -537,8 +537,8 @@ def _seed(x, deg=5, seeds=None): ...@@ -537,8 +537,8 @@ def _seed(x, deg=5, seeds=None):
def _greedy(x, y, tol=1e-6, rel=False, deg=5, verbose=False, seeds=None): def _greedy(x, y, tol=1e-6, rel=False, deg=5, verbose=False, seeds=None):
"""Greedy algorithm for building a reduced-order spline""" """Greedy algorithm for building a reduced-order spline"""
if verbose: if verbose:
print "\nSize", "\t", "Error" print("\nSize", "\t", "Error")
print "="*13 print("="*13)
if rel: if rel:
ymax = np.max(np.abs(y)) ymax = np.max(np.abs(y))
...@@ -570,7 +570,7 @@ def _greedy(x, y, tol=1e-6, rel=False, deg=5, verbose=False, seeds=None): ...@@ -570,7 +570,7 @@ def _greedy(x, y, tol=1e-6, rel=False, deg=5, verbose=False, seeds=None):
# Print to screen, if requested # Print to screen, if requested
if verbose: if verbose:
print ctr, "\t", errors[-1] print(ctr, "\t", errors[-1])
# Check if greedy error is below tolerance and exit if so # Check if greedy error is below tolerance and exit if so
if errors[-1] < _tol: <