question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Refactoring] N-dimenstional Kriging

See original GitHub issue

Just a few thoughts about possible code refactoring in PyKrige.

Currently PyKrige has separate code that implements 2D and 3D kriging (e.g. in ok.py and ok3d.py), this result in code duplication and makes code maintenance and adding new features more difficult (they need to be added to every single file). Besides the 1D Kriging is not implemented, and it might have been nice to have some 1D examples as they are easier to visualize.

In addition, PR #24 adds a scikit-learn API to PyKrige on top of the existing UniversalKrigging and OrdinaryKrigging methods.

A possible solution to remove the current code duplication, would be to refactor the UniversalKrigging and OrdinaryKrigging to work in N-dimensions. The simplest way of doing it would be to use something along the lines of the scikit-learn API which will also remove the need for an additional wrapper for that. The general API could be something like,

class OrdinaryKrigging(pykrige.compat.BaseEstimator):
    def __init__(self, <kriging options>):
         [..]
    def fit(X, y):
        """ Where X is an array [n_samples, n_dimensions] and y is an array [n_samples]"""
        [...]
    def predict(X):
        """ Where X is an array [n_samples, n_dimensions]
        The equivalent of the current execute(style="points", ...)
        """
        [...]

The case of execute(style="masked", ...) could be done by supporting masked arrays for predict(X), while the case execute(style="grid", ...) can be done with a helper function,

def points2grid(**points):
       """ points: a list of arrays e.g [zpts, ypts, xpts] """
       grids = np.meshgrid(*points, indexing='ij')
       return np.concatenate([grid.flatten()[:, None] for grid in grids])

which is mostly what is done internally by execute at present.

This would break backward compatibility though, so a major version change would be needed (PyKrige v2).

Update: the refactoring could follow the steps below,

  • in core.py merge adjust_for_anisotropy and adjust_for_anisotropy_3d into a single private function _adjust_for_anisotropy(X, center, scaling, angle) where X is a [n_samples, n_dim] array, and all the other are list of floats and, it returns the X_modified array. (PR #33 )
  • in core.py merge the initialize_variogram_model and initialize_variogram_model_3d into a single private function (PR #47 )
    _initialize_variogram_model(X, y,  variogram_model, variogram_model_parameters, variogram_function, nlags, weight)
    
  • in core.py merge the krige and krige_3d into a single private function _krige(X, y, coords, variogram_function, variogram_model_parameters) (PR #51 )
  • in core.py similarly merge find_statistics* (PR #51)
  • create a new ND kriging class for ordinary Kriging that follows the scikit-learn compatible api. We need to decide on a name OrdinaryKriging would be great but it’s already taken). Maybe OrdinaryNDKriging ?
  • rewrite the internals of OrdinaryKriging and OrdinaryKriging3D to use OrdinaryNDKriging internally.
  • rewrite specific tests for OrdinaryNDKriging and add deprecation warnings on OrdinaryKriging , OrdinaryKriging3D
  • do the same thing for other kriging methods
  • after a few versions remove the old style kriging interface.

What do you think?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
basakscommented, Jan 14, 2017

@rth Your list looks good to me. I also reviewed your most recent PR and cross checked both the functions that you have manged to pull together into one.

1reaction
basakscommented, Jan 9, 2017

@rth Thank you for putting this list together. I will go through your list in detail tonight. A bit under the pump today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Refactoring] N-dimenstional Kriging · Issue #31
The first big question I'd have about this is how to generalize the array input and handling to an arbitrary number of dimensions....
Read more >
Confusion related to difference of kriging and gaussian ...
Ultimately kriging/GPR is an interpolation technique and most (not all) of the difference among the variants of it lays on the assumption ...
Read more >
Improving kriging surrogates of high-dimensional design ...
The KPLS methods is used for many academic and industrial verifications, and promising results have been obtained for problems with up to 100...
Read more >
Dimension Reduction for the Design Optimization of Large Scale ...
Model refactoring is emerging as a desirable means to improve design model by restructuring it while preserving the behavior properties. It applies the...
Read more >
New abstract types: RadialBasis and Kriging.
Kriging has the nice property of knowing how much error there is at a single evaluation. All the code below follows the mathematical...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found