Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with Kriging values #89

Closed
donhalmina opened this issue May 4, 2020 · 5 comments · Fixed by #97
Closed

Issues with Kriging values #89

donhalmina opened this issue May 4, 2020 · 5 comments · Fixed by #97
Assignees
Labels
bug Something isn't working enhancement New feature or request question Further information is requested
Milestone

Comments

@donhalmina
Copy link

I am performing an ordinary kriging and the results I get after performing the kriging are totally wrong. For simplicity, my grid points are also my data points. The working code is:

"Import libraries and modules"
import numpy as np
import gstools as gs
import tkinter as tk
from tkinter import filedialog
import pandas as pd
from scipy.spatial.distance import pdist, squareform

"Import data"
root = tk.Tk()
root.withdraw()
filepath = filedialog.askopenfilenames(parent=root, title='Choose a file')
data = pd.DataFrame()
for file in filepath:
    df = pd.read_excel(file)
    data = data.append(df)
data = data.iloc[:, :3]
data = data[~np.isnan(data).any(axis=1)]
data = np.array(data, dtype=np.float)

"Calculate semivariance"
sill = np.var(data[:, 2])
pwdist = squareform(pdist(data[:, :2]))
max_lag = np.int(np.ceil(np.max(pwdist)))
npoint = np.int(np.floor(max_lag/40)) 
firstlag = max_lag / 100
lags = np.linspace(firstlag, max_lag, npoint)
bin_center, gamma = gs.vario_estimate_unstructured((data[:,0], data[:,1]), data[:,2], lags)

"Fit theoretical variogram to data"
fit_model = gs.Stable(dim=2)
fit_model.set_arg_bounds(var=[0, sill])
fit_model.set_arg_bounds(len_scale=[0, 1000000])
fit_model.fit_variogram(bin_center, gamma, nugget=False)
ax = fit_model.plot(x_max=max_lag)
ax.plot(bin_center, gamma)
print(fit_model)

"Perform kriging"
OK = gs.krige.Ordinary(fit_model, [data[:, 0], data[:, 1]], data[:, 2])
OK.unstructured([data[:, 0], data[:, 1]])
ax = OK.plot()
ax.set_aspect("equal")

The semivariogram looks good (as attached below).
GSTool_Stable Variogram

The value of my kriging should range between 0 and 400 as data[:, 2] ranges between this value. However, i am getting values between -3000 and 8000.
GSTool_Unstructured_OK

Find attached the data to reproduce the problem

CNDA-ESP_SIMRAD_MDS_DGPS_2000_2001.xlsx

@MuellerSeb
Copy link
Member

What helped in my case, was to change the estimator to cressie:

bin_center, gamma = gs.vario_estimate_unstructured(
    (data[:, 0], data[:, 1]), data[:, 2], lags, estimator="cressie"
)

better_field

But the only difference is a different estimated len-scale. So I think the problem is in the kriging routines. An educated guess is, that the resulting kriging matrix is singular and produces an invalid inverse matrix. Maybe this will be resolved by this suggestion: #79 (comment)

I will keep track of that!

@donhalmina
Copy link
Author

I changed the estimator to Cressie, to try to reproduce your results but still get erreoneous result. I am using gstools version 1.2.1 and don't know if it is the same version you are using as I am expecting to obtain the same results you obtianed.
Figure 2020-05-04 222816

I have my Geostat tool which i programmed and use and for determining the weight parameter, I use np.linalg.lstsq as it is more robust and stable than np.linalg.inv.

Also, how can I obtain the values of the kriging estimate or export it (.csv, .txt, etc)?

@MuellerSeb MuellerSeb self-assigned this May 5, 2020
@MuellerSeb MuellerSeb added bug Something isn't working enhancement New feature or request question Further information is requested labels May 5, 2020
@MuellerSeb MuellerSeb added this to the 1.3 milestone May 5, 2020
@MuellerSeb
Copy link
Member

MuellerSeb commented May 26, 2020

We will use the pseudo-inverse in the future, which should hold equal results as using least-square.

Exporting can be done to a "vtk" file as demonstrated here:
https://geostat-framework.readthedocs.io/projects/gstools/en/stable/examples/00_misc/01_export.html#sphx-glr-examples-00-misc-01-export-py
and here:
https://geostat-framework.readthedocs.io/projects/gstools/en/stable/index.html#vtk-pyvista-export

@MuellerSeb MuellerSeb linked a pull request Jun 3, 2020 that will close this issue
@MuellerSeb
Copy link
Member

This problem will be solved with #95

@MuellerSeb MuellerSeb mentioned this issue Jul 15, 2020
10 tasks
@MuellerSeb
Copy link
Member

MuellerSeb commented Jul 30, 2020

This should be solved with #97

Feel free to re-open this issue, if you have further questions.

@MuellerSeb MuellerSeb removed a link to a pull request Jan 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants