Commit 647fe406 authored by inhuszar's avatar inhuszar
Browse files

Added fill_value option for extrapolation.

parent f2ed0660
......@@ -18,6 +18,7 @@
# DEPENDENCIES
import warnings
import numpy as np
......@@ -42,6 +43,13 @@ class ScipyInterpolator(Interpolator):
kw = {k: v for k, v in self.kwargs.get("ipkwargs", dict()).items()}
# Override the ones that were specified for the call
kw.update(ipkwargs)
mode = kw.get("mode", None)
# Set default fill value unless the mode parameter was given
if mode is None:
mode = "constant"
cval = self.kwargs.get("fill_value")
kw.update(mode=mode, cval=cval)
if isinstance(coordinates, (tuple, list)):
coordinates = np.asarray(coordinates).reshape((1, -1))
......@@ -57,7 +65,8 @@ class ScipyInterpolator(Interpolator):
class ScipyRegularGridInterpolator(Interpolator):
def __init__(self, source, tensor_axes=(), radius=1, hold=False,
threads=-1, verbose=False, ipkwargs=None, **kwargs):
threads=-1, verbose=False, ipkwargs=None, fill_value=None,
**kwargs):
"""
Initialisation of ScipyRegularGridInterpolator
......@@ -65,7 +74,7 @@ class ScipyRegularGridInterpolator(Interpolator):
# Set default arguments for the RegularGridInterpolator.
# These parameters will repress out-of-bounds errors when values need
# to be interpolated outside the domain of known values.
default_ipkwargs = dict(bounds_error=False, fill_value=ts.FILL_VALUE)
default_ipkwargs = dict(bounds_error=False, fill_value=fill_value)
if isinstance(ipkwargs, dict):
default_ipkwargs.update(ipkwargs)
ipkwargs = default_ipkwargs
......@@ -73,8 +82,11 @@ class ScipyRegularGridInterpolator(Interpolator):
# Call parent-class initialisation
super(ScipyRegularGridInterpolator, self).__init__(
source, tensor_axes=tensor_axes, radius=radius, hold=hold,
threads=threads, verbose=verbose, ipkwargs=ipkwargs, **kwargs)
threads=threads, verbose=verbose, ipkwargs=ipkwargs,
fill_value=fill_value, **kwargs)
if self.ipkwargs.get("fill_value", None) is None:
self.ipkwargs.update(fill_value=ts.FILL_VALUE)
def interpolate(self, coordinates, input_array=None, **ipkwargs):
"""
......@@ -104,9 +116,10 @@ class ScipyRegularGridInterpolator(Interpolator):
class ScipyNearestNeighbours(Interpolator):
def __init__(self, source, tensor_axes=(), radius=1, hold=False,
threads=-1, verbose=False, ipkwargs=None, **kwargs):
threads=-1, verbose=False, ipkwargs=None, fill_value=None,
**kwargs):
"""
Initialisation of ScipyRegularGridInterpolator
Initialisation of ScipyNearestNeighboursr
"""
# Set default arguments for the RegularGridInterpolator.
......@@ -117,11 +130,18 @@ class ScipyNearestNeighbours(Interpolator):
default_ipkwargs.update(ipkwargs)
ipkwargs = default_ipkwargs
# Note: the fill_value parameter lands in the kwargs, as it is not used
# by this interpolator.
if fill_value is not None:
warnings.warn(f"The fill_value parameter was ignored by "
f"{self.__class__.__name__}")
# Call parent-class initialisation
super(ScipyNearestNeighbours, self).__init__(
source, tensor_axes=tensor_axes, radius=radius, hold=hold,
threads=threads, verbose=verbose, ipkwargs=ipkwargs, **kwargs)
def interpolate(self, coordinates, input_array=None, **ipkwargs):
from scipy.interpolate import NearestNDInterpolator
......
Supports Markdown
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