-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from GeoStat-Framework/binning
Automatic Binning
- Loading branch information
Showing
15 changed files
with
320 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,26 +177,25 @@ y = np.random.RandomState(20011012).rand(1000) * 100. | |
model = gs.Exponential(dim=2, var=2, len_scale=8) | ||
srf = gs.SRF(model, mean=0, seed=19970221) | ||
field = srf((x, y)) | ||
# estimate the variogram of the field with 40 bins | ||
bins = np.arange(40) | ||
bin_center, gamma = gs.vario_estimate((x, y), field, bins) | ||
# estimate the variogram of the field | ||
bin_center, gamma = gs.vario_estimate((x, y), field) | ||
# fit the variogram with a stable model. (no nugget fitted) | ||
fit_model = gs.Stable(dim=2) | ||
fit_model.fit_variogram(bin_center, gamma, nugget=False) | ||
# output | ||
ax = fit_model.plot(x_max=40) | ||
ax.plot(bin_center, gamma) | ||
ax = fit_model.plot(x_max=bin_center[-1]) | ||
ax.scatter(bin_center, gamma) | ||
print(fit_model) | ||
``` | ||
|
||
Which gives: | ||
|
||
```python | ||
Stable(dim=2, var=1.92, len_scale=8.15, nugget=0.0, anis=[1.], angles=[0.], alpha=1.05) | ||
Stable(dim=2, var=1.85, len_scale=7.42, nugget=0.0, anis=[1.0], angles=[0.0], alpha=1.09) | ||
``` | ||
|
||
<p align="center"> | ||
<img src="https://raw.githubusercontent.com/GeoStat-Framework/GSTools/master/docs/source/pics/exp_vario_fit.png" alt="Variogram" width="600px"/> | ||
<img src="https://github.com/GeoStat-Framework/GeoStat-Framework.github.io/raw/master/img/GS_vario_est.png" alt="Variogram" width="600px"/> | ||
</p> | ||
|
||
|
||
|
@@ -325,6 +324,7 @@ in memory for immediate 3D plotting in Python. | |
- [hankel >= 1.0.2](https://github.com/steven-murray/hankel) | ||
- [emcee >= 3.0.0](https://github.com/dfm/emcee) | ||
- [pyevtk >= 1.1.1](https://github.com/pyscience-projects/pyevtk) | ||
- [meshio>=4.0.3, <5.0](https://github.com/nschloe/meshio) | ||
|
||
### Optional | ||
|
||
|
@@ -339,7 +339,7 @@ You can contact us via <[email protected]>. | |
|
||
## License | ||
|
||
[LGPLv3][license_link] © 2018-2020 | ||
[LGPLv3][license_link] © 2018-2021 | ||
|
||
[pip_link]: https://pypi.org/project/gstools | ||
[conda_link]: https://docs.conda.io/en/latest/miniconda.html | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
Fit Variogram with automatic binning | ||
------------------------------------ | ||
""" | ||
import numpy as np | ||
import gstools as gs | ||
|
||
############################################################################### | ||
# Generate a synthetic field with an exponential model. | ||
|
||
x = np.random.RandomState(19970221).rand(1000) * 100.0 | ||
y = np.random.RandomState(20011012).rand(1000) * 100.0 | ||
model = gs.Exponential(dim=2, var=2, len_scale=8) | ||
srf = gs.SRF(model, mean=0, seed=19970221) | ||
field = srf((x, y)) | ||
print(field.var()) | ||
############################################################################### | ||
# Estimate the variogram of the field with automatic binning. | ||
|
||
bin_center, gamma = gs.vario_estimate((x, y), field) | ||
print("estimated bin number:", len(bin_center)) | ||
print("maximal bin distance:", bin_center[-1]) | ||
|
||
############################################################################### | ||
# Fit the variogram with a stable model (no nugget fitted). | ||
|
||
fit_model = gs.Stable(dim=2) | ||
fit_model.fit_variogram(bin_center, gamma, nugget=False) | ||
print(fit_model) | ||
|
||
############################################################################### | ||
# Plot the fitting result. | ||
|
||
ax = fit_model.plot(x_max=bin_center[-1]) | ||
ax.scatter(bin_center, gamma) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
""" | ||
Automatic binning with lat-lon data | ||
----------------------------------- | ||
In this example we demonstrate automatic binning for a tiny data set | ||
containing temperature records from Germany | ||
(See the detailed DWD example for more information on the data). | ||
We use a data set from 20 meteo-stations choosen randomly. | ||
""" | ||
import numpy as np | ||
import gstools as gs | ||
|
||
# lat, lon, temperature | ||
data = np.array( | ||
[ | ||
[52.9336, 8.237, 15.7], | ||
[48.6159, 13.0506, 13.9], | ||
[52.4853, 7.9126, 15.1], | ||
[50.7446, 9.345, 17.0], | ||
[52.9437, 12.8518, 21.9], | ||
[53.8633, 8.1275, 11.9], | ||
[47.8342, 10.8667, 11.4], | ||
[51.0881, 12.9326, 17.2], | ||
[48.406, 11.3117, 12.9], | ||
[49.7273, 8.1164, 17.2], | ||
[49.4691, 11.8546, 13.4], | ||
[48.0197, 12.2925, 13.9], | ||
[50.4237, 7.4202, 18.1], | ||
[53.0316, 13.9908, 21.3], | ||
[53.8412, 13.6846, 21.3], | ||
[54.6792, 13.4343, 17.4], | ||
[49.9694, 9.9114, 18.6], | ||
[51.3745, 11.292, 20.2], | ||
[47.8774, 11.3643, 12.7], | ||
[50.5908, 12.7139, 15.8], | ||
] | ||
) | ||
pos = data.T[:2] # lat, lon | ||
field = data.T[2] # temperature | ||
|
||
############################################################################### | ||
# Since the overall range of these meteo-stations is too low, we can use the | ||
# data-variance as additional information during the fit of the variogram. | ||
|
||
emp_v = gs.vario_estimate(pos, field, latlon=True) | ||
sph = gs.Spherical(latlon=True, rescale=gs.EARTH_RADIUS) | ||
sph.fit_variogram(*emp_v, sill=np.var(field)) | ||
ax = sph.plot(x_max=2 * np.max(emp_v[0])) | ||
ax.scatter(*emp_v, label="Empirical variogram") | ||
ax.legend() | ||
print(sph) | ||
|
||
############################################################################### | ||
# As we can see, the variogram fitting was successful and providing the data | ||
# variance helped finding the right length-scale. | ||
# | ||
# Now, we'll use this covariance model to interpolate the given data with | ||
# ordinary kriging. | ||
|
||
# enclosing box for data points | ||
grid_lat = np.linspace(np.min(pos[0]), np.max(pos[0])) | ||
grid_lon = np.linspace(np.min(pos[1]), np.max(pos[1])) | ||
# ordinary kriging | ||
krige = gs.krige.Ordinary(sph, pos, field) | ||
krige((grid_lat, grid_lon), mesh_type="structured") | ||
ax = krige.plot() | ||
# plotting lat on y-axis and lon on x-axis | ||
ax.scatter(pos[1], pos[0], 50, c=field, edgecolors="k", label="input") | ||
ax.legend() | ||
|
||
############################################################################### | ||
# Looks good, doesn't it? | ||
# | ||
# This example shows, that setting up variogram estimation and kriging routines | ||
# is straight forward with GSTools. ;-) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.