verde.SplineCV

class verde.SplineCV(mindists=(1000.0, 10000.0, 100000.0), dampings=(1e-10, 1e-05, 0.1), force_coords=None, engine='auto', cv=None, client=None)[source]

Cross-validated biharmonic spline interpolation.

Similar to verde.Spline but automatically chooses the best damping and mindist parameters using cross-validation. Tests all combinations of the given dampings and mindists and selects the maximum (or minimum) mean cross-validation score (i.e., a grid search).

The grid search can optionally run in parallel using dask. To do this, pass in a dask.distributed.Client as the client argument. The client can manage a process or thread pool in your local computer or a remote cluster.

Other cross-validation generators from sklearn.model_selection can be used by passing them through the cv argument.

Parameters:
mindists : iterable or 1d array

List (or other iterable) of mindist parameter values to try. Can be considered a minimum distance between the point forces and data points. Needed because the Green’s functions are singular when forces and data points coincide. Acts as a fudge factor.

dampings : iterable or 1d array

List (or other iterable) of damping parameter values to try. Is the positive damping regularization parameter. Controls how much smoothness is imposed on the estimated forces. If None, no regularization is used.

force_coords : None or tuple of arrays

The easting and northing coordinates of the point forces. If None (default), then will be set to the data coordinates the first time fit is called.

engine : str

Computation engine for the Jacobian matrix and prediction. Can be 'auto', 'numba', or 'numpy'. If 'auto', will use numba if it is installed or numpy otherwise. The numba version is multi-threaded and usually faster, which makes fitting and predicting faster.

cv : None or cross-validation generator

Any scikit-learn cross-validation generator. If not given, will use the default set by verde.cross_val_score.

client : None or dask.distributed.Client

If None, then computations are run serially. Otherwise, should be a dask Client object. It will be used to dispatch computations to the dask cluster.

See also

Spline
The bi-harmonic spline
cross_val_score
Score an estimator/gridder using cross-validation
Attributes:
force_ : array

The estimated forces that fit the data.

force_coords_ : tuple of arrays

The optimal force locations

region_ : tuple

The bounding region of the data used to fit the spline

scores_ : array

The mean cross-validation score for each parameter combination.

mindist_ : float

The optimal mindist parameter

damping_ : float

The optimal damping parameter

Methods

filter(self, coordinates, data[, weights]) Filter the data through the gridder and produce residuals.
fit(self, coordinates, data[, weights]) Fit the biharmonic spline to the given data and automatically tune parameters.
get_params(self[, deep]) Get parameters for this estimator.
grid(self[, region, shape, spacing, dims, …]) Interpolate the data onto a regular grid.
predict(self, coordinates) Evaluate the best estimated spline on the given set of points.
profile(self, point1, point2, size[, dims, …]) Interpolate data along a profile between two points.
scatter(self[, region, size, random_state, …]) Interpolate values onto a random scatter of points.
score(self, coordinates, data[, weights]) Score the gridder predictions against the given data.
set_params(self, \*\*params) Set the parameters of this estimator.

Examples using verde.SplineCV

SplineCV.filter(self, coordinates, data, weights=None)

Filter the data through the gridder and produce residuals.

Calls fit on the data, evaluates the residuals (data - predicted data), and returns the coordinates, residuals, and weights.

No very useful by itself but this interface makes gridders compatible with other processing operations and is used by verde.Chain to join them together (for example, so you can fit a spline on the residuals of a trend).

Parameters:
coordinates : tuple of arrays

Arrays with the coordinates of each data point. Should be in the following order: (easting, northing, vertical, …).

data : array or tuple of arrays

The data values of each data point. If the data has more than one component, data must be a tuple of arrays (one for each component).

weights : None or array or tuple of arrays

If not None, then the weights assigned to each data point. If more than one data component is provided, you must provide a weights array for each data component (if not None).

Returns:
coordinates, residuals, weights

The coordinates and weights are same as the input. Residuals are the input data minus the predicted data.

SplineCV.fit(self, coordinates, data, weights=None)[source]

Fit the biharmonic spline to the given data and automatically tune parameters.

For each combination of the parameters given, computes the mean cross validation score using verde.cross_val_score and the given CV splitting class (the cv parameter of this class). The configuration with the best score is then chosen and used to fit the entire dataset.

The data region is captured and used as default for the grid and scatter methods.

All input arrays must have the same shape.

Parameters:
coordinates : tuple of arrays

Arrays with the coordinates of each data point. Should be in the following order: (easting, northing, vertical, …). Only easting and northing will be used, all subsequent coordinates will be ignored.

data : array

The data values of each data point.

weights : None or array

If not None, then the weights assigned to each data point. Typically, this should be 1 over the data uncertainty squared.

Returns:
self

Returns this estimator instance for chaining operations.

SplineCV.get_params(self, deep=True)

Get parameters for this estimator.

Parameters:
deep : boolean, optional

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
params : mapping of string to any

Parameter names mapped to their values.

SplineCV.grid(self, region=None, shape=None, spacing=None, dims=None, data_names=None, projection=None, **kwargs)

Interpolate the data onto a regular grid.

The grid can be specified by either the number of points in each dimension (the shape) or by the grid node spacing. See verde.grid_coordinates for details. Other arguments for verde.grid_coordinates can be passed as extra keyword arguments (kwargs) to this method.

If the interpolator collected the input data region, then it will be used if region=None. Otherwise, you must specify the grid region.

Use the dims and data_names arguments to set custom names for the dimensions and the data field(s) in the output xarray.Dataset. Default names will be provided if none are given.

Parameters:
region : list = [W, E, S, N]

The boundaries of a given region in Cartesian or geographic coordinates.

shape : tuple = (n_north, n_east) or None

The number of points in the South-North and West-East directions, respectively.

spacing : tuple = (s_north, s_east) or None

The grid spacing in the South-North and West-East directions, respectively.

dims : list or None

The names of the northing and easting data dimensions, respectively, in the output grid. Defaults to ['northing', 'easting']. NOTE: This is an exception to the “easting” then “northing” pattern but is required for compatibility with xarray.

data_names : list of None

The name(s) of the data variables in the output grid. Defaults to ['scalars'] for scalar data, ['east_component', 'north_component'] for 2D vector data, and ['east_component', 'north_component', 'vertical_component'] for 3D vector data.

projection : callable or None

If not None, then should be a callable object projection(easting, northing) -> (proj_easting, proj_northing) that takes in easting and northing coordinate arrays and returns projected northing and easting coordinate arrays. This function will be used to project the generated grid coordinates before passing them into predict. For example, you can use this to generate a geographic grid from a Cartesian gridder.

Returns:
grid : xarray.Dataset

The interpolated grid. Metadata about the interpolator is written to the attrs attribute.

See also

verde.grid_coordinates
Generate the coordinate values for the grid.
SplineCV.predict(self, coordinates)[source]

Evaluate the best estimated spline on the given set of points.

Requires a fitted estimator (see fit).

Parameters:
coordinates : tuple of arrays

Arrays with the coordinates of each data point. Should be in the following order: (easting, northing, vertical, …). Only easting and northing will be used, all subsequent coordinates will be ignored.

Returns:
data : array

The data values evaluated on the given points.

SplineCV.profile(self, point1, point2, size, dims=None, data_names=None, projection=None, **kwargs)

Interpolate data along a profile between two points.

Generates the profile along a straight line assuming Cartesian distances. Point coordinates are generated by verde.profile_coordinates. Other arguments for this function can be passed as extra keyword arguments (kwargs) to this method.

Use the dims and data_names arguments to set custom names for the dimensions and the data field(s) in the output pandas.DataFrame. Default names are provided.

Includes the calculated Cartesian distance from point1 for each data point in the profile.

Parameters:
point1 : tuple

The easting and northing coordinates, respectively, of the first point.

point2 : tuple

The easting and northing coordinates, respectively, of the second point.

size : int

The number of points to generate.

dims : list or None

The names of the northing and easting data dimensions, respectively, in the output dataframe. Defaults to ['northing', 'easting']. NOTE: This is an exception to the “easting” then “northing” pattern but is required for compatibility with xarray.

data_names : list of None

The name(s) of the data variables in the output dataframe. Defaults to ['scalars'] for scalar data, ['east_component', 'north_component'] for 2D vector data, and ['east_component', 'north_component', 'vertical_component'] for 3D vector data.

projection : callable or None

If not None, then should be a callable object projection(easting, northing) -> (proj_easting, proj_northing) that takes in easting and northing coordinate arrays and returns projected northing and easting coordinate arrays. This function will be used to project the generated profile coordinates before passing them into predict. For example, you can use this to generate a geographic profile from a Cartesian gridder.

Returns:
table : pandas.DataFrame

The interpolated values along the profile.

SplineCV.scatter(self, region=None, size=300, random_state=0, dims=None, data_names=None, projection=None, **kwargs)

Interpolate values onto a random scatter of points.

Point coordinates are generated by verde.scatter_points. Other arguments for this function can be passed as extra keyword arguments (kwargs) to this method.

If the interpolator collected the input data region, then it will be used if region=None. Otherwise, you must specify the grid region.

Use the dims and data_names arguments to set custom names for the dimensions and the data field(s) in the output pandas.DataFrame. Default names are provided.

Parameters:
region : list = [W, E, S, N]

The boundaries of a given region in Cartesian or geographic coordinates.

size : int

The number of points to generate.

random_state : numpy.random.RandomState or an int seed

A random number generator used to define the state of the random permutations. Use a fixed seed to make sure computations are reproducible. Use None to choose a seed automatically (resulting in different numbers with each run).

dims : list or None

The names of the northing and easting data dimensions, respectively, in the output dataframe. Defaults to ['northing', 'easting']. NOTE: This is an exception to the “easting” then “northing” pattern but is required for compatibility with xarray.

data_names : list of None

The name(s) of the data variables in the output dataframe. Defaults to ['scalars'] for scalar data, ['east_component', 'north_component'] for 2D vector data, and ['east_component', 'north_component', 'vertical_component'] for 3D vector data.

projection : callable or None

If not None, then should be a callable object projection(easting, northing) -> (proj_easting, proj_northing) that takes in easting and northing coordinate arrays and returns projected northing and easting coordinate arrays. This function will be used to project the generated scatter coordinates before passing them into predict. For example, you can use this to generate a geographic scatter from a Cartesian gridder.

Returns:
table : pandas.DataFrame

The interpolated values on a random set of points.

SplineCV.score(self, coordinates, data, weights=None)

Score the gridder predictions against the given data.

Calculates the R^2 coefficient of determination of between the predicted values and the given data values. A maximum score of 1 means a perfect fit. The score can be negative.

If the data has more than 1 component, the scores of each component will be averaged.

Parameters:
coordinates : tuple of arrays

Arrays with the coordinates of each data point. Should be in the following order: (easting, northing, vertical, …).

data : array or tuple of arrays

The data values of each data point. If the data has more than one component, data must be a tuple of arrays (one for each component).

weights : None or array or tuple of arrays

If not None, then the weights assigned to each data point. If more than one data component is provided, you must provide a weights array for each data component (if not None).

Returns:
score : float

The R^2 score

SplineCV.set_params(self, **params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Returns:
self