-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
4,929 additions
and
9 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
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,161 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
module GeoStatsFunctionsMakieExt | ||
|
||
using GeoStatsFunctions | ||
|
||
import Makie | ||
import GeoStatsFunctions: varioplot, varioplot! | ||
|
||
Makie.@recipe(VarioPlot, γ) do scene | ||
Makie.Attributes( | ||
# empirical variogram options | ||
vcolor=:slategray, | ||
psize=12, | ||
ssize=1.5, | ||
tshow=true, | ||
tsize=12, | ||
hshow=true, | ||
hcolor=:slategray, | ||
|
||
# empirical varioplane options | ||
vscheme=:viridis, | ||
rshow=true, | ||
rmodel=SphericalVariogram, | ||
rcolor=:white, | ||
|
||
# theoretical variogram options | ||
maxlag=nothing | ||
) | ||
end | ||
|
||
# ---------- | ||
# EMPIRICAL | ||
# ---------- | ||
|
||
Makie.plottype(::EmpiricalVariogram) = VarioPlot{<:Tuple{EmpiricalVariogram}} | ||
|
||
function Makie.plot!(plot::VarioPlot{<:Tuple{EmpiricalVariogram}}) | ||
# retrieve variogram object | ||
γ = plot[:γ] | ||
|
||
# get the data | ||
xyn = Makie.@lift values($γ) | ||
x = Makie.@lift $xyn[1] | ||
y = Makie.@lift $xyn[2] | ||
n = Makie.@lift $xyn[3] | ||
|
||
# discard empty bins | ||
x = Makie.@lift $x[$n .> 0] | ||
y = Makie.@lift $y[$n .> 0] | ||
n = Makie.@lift $n[$n .> 0] | ||
|
||
# visualize frequencies as bars | ||
if plot[:hshow][] | ||
f = Makie.@lift $n * (maximum($y) / maximum($n)) / 10 | ||
Makie.barplot!(plot, x, f, color=plot[:hcolor], alpha=0.3, gap=0.0) | ||
end | ||
|
||
# visualize variogram | ||
Makie.scatterlines!(plot, x, y, color=plot[:vcolor], markersize=plot[:psize], linewidth=plot[:ssize]) | ||
|
||
# visualize text counts | ||
if plot[:tshow][] | ||
bincounts = Makie.@lift string.($n) | ||
positions = Makie.@lift collect(zip($x, $y)) | ||
Makie.text!(plot, bincounts, position=positions, fontsize=plot[:tsize]) | ||
end | ||
end | ||
|
||
Makie.plottype(::EmpiricalVarioplane) = VarioPlot{<:Tuple{EmpiricalVarioplane}} | ||
|
||
function Makie.plot!(plot::VarioPlot{<:Tuple{EmpiricalVarioplane}}) | ||
# retrieve varioplane object | ||
v = plot[:γ] | ||
|
||
# retrieve range model | ||
rshow = plot[:rshow] | ||
rmodel = plot[:rmodel] | ||
|
||
# underyling variograms | ||
γs = Makie.@lift $v.γs | ||
|
||
# polar angle | ||
θs = Makie.@lift $v.θs | ||
|
||
# polar radius | ||
rs = Makie.@lift values($γs[1])[1] | ||
|
||
# variogram values for all variograms | ||
Z = Makie.@lift let | ||
zs = map($γs) do γ | ||
_, zs, __ = values(γ) | ||
|
||
# handle NaN values (i.e. empty bins) | ||
isnan(zs[1]) && (zs[1] = 0) | ||
for i in 2:length(zs) | ||
isnan(zs[i]) && (zs[i] = zs[i - 1]) | ||
end | ||
|
||
zs | ||
end | ||
reduce(hcat, zs) | ||
end | ||
|
||
# exploit symmetry | ||
θs = Makie.@lift range(0, 2π, length=2 * length($θs)) | ||
Z = Makie.@lift [$Z $Z] | ||
|
||
# hide hole at center | ||
rs = Makie.@lift [0; $rs] | ||
Z = Makie.@lift [$Z[1:1, :]; $Z] | ||
|
||
# transpose for plotting | ||
Z = Makie.@lift transpose($Z) | ||
|
||
Makie.surface!(plot, θs, rs, Z, colormap=plot[:vscheme], shading=Makie.NoShading) | ||
|
||
# show model range | ||
if rshow[] | ||
ls = Makie.@lift [range(GeoStatsFunctions.fit($rmodel, γ)) for γ in $γs] | ||
ls = Makie.@lift [$ls; $ls] | ||
zs = Makie.@lift fill(maximum($Z) + 1, length($ls)) | ||
Makie.lines!(plot, θs, ls, zs, color=plot[:rcolor]) | ||
end | ||
end | ||
|
||
# ------------ | ||
# THEORETICAL | ||
# ------------ | ||
|
||
Makie.plottype(::Variogram) = VarioPlot{<:Tuple{Variogram}} | ||
|
||
function Makie.plot!(plot::VarioPlot{<:Tuple{Variogram}}) | ||
# retrieve variogram object | ||
γ = plot[:γ] | ||
|
||
# retrieve maximum lag | ||
maxlag = plot[:maxlag] | ||
|
||
L = if isnothing(maxlag[]) | ||
Makie.@lift _maxlag($γ) | ||
else | ||
maxlag | ||
end | ||
|
||
# start at 1e-6 instead of 0 to avoid | ||
# nugget artifact in visualization | ||
x = Makie.@lift range(1e-6, stop=$L, length=100) | ||
y = Makie.@lift $γ.($x) | ||
|
||
# visualize variogram | ||
Makie.lines!(plot, x, y, color=plot[:vcolor]) | ||
end | ||
|
||
_maxlag(γ::Variogram) = 3range(γ) | ||
_maxlag(γ::PowerVariogram) = 3.0 | ||
_maxlag(γ::NuggetEffect) = 3.0 | ||
|
||
end |
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 |
---|---|---|
@@ -1,5 +1,90 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
module GeoStatsFunctions | ||
|
||
# Write your package code here. | ||
using Meshes | ||
using GeoTables | ||
|
||
using Optim | ||
using Tables | ||
using Distances | ||
using Bessels: gamma, besselk | ||
using InteractiveUtils: subtypes | ||
using NearestNeighbors: MinkowskiMetric | ||
using Transducers: Map, foldxt | ||
using LinearAlgebra | ||
using Statistics | ||
using Setfield | ||
using Unitful | ||
using Random | ||
using Printf | ||
|
||
import Base: merge, +, * | ||
import Meshes: isisotropic | ||
|
||
include("utils.jl") | ||
include("empirical.jl") | ||
include("variogram.jl") | ||
include("covariance.jl") | ||
include("nesting.jl") | ||
include("fitting.jl") | ||
include("sampling.jl") | ||
include("varioplot.jl") | ||
|
||
# temporary fix for ⋅ with missing values | ||
# https://github.com/JuliaLang/julia/issues/40743 | ||
import LinearAlgebra: ⋅ | ||
⋅(::Missing, ::Missing) = missing | ||
|
||
export | ||
# empirical variograms | ||
EmpiricalVariogram, | ||
EmpiricalVarioplane, | ||
DirectionalVariogram, | ||
PlanarVariogram, | ||
distance, | ||
estimator, | ||
|
||
# theoretical variograms | ||
Variogram, | ||
NuggetEffect, | ||
GaussianVariogram, | ||
ExponentialVariogram, | ||
MaternVariogram, | ||
SphericalVariogram, | ||
CubicVariogram, | ||
PentasphericalVariogram, | ||
PowerVariogram, | ||
SineHoleVariogram, | ||
CircularVariogram, | ||
NestedVariogram, | ||
sill, | ||
nugget, | ||
metricball, | ||
variotype, | ||
isstationary, | ||
isisotropic, | ||
structures, | ||
variosample, | ||
|
||
# theoretical covariance | ||
CircularCovariance, | ||
CubicCovariance, | ||
ExponentialCovariance, | ||
GaussianCovariance, | ||
MaternCovariance, | ||
PentasphericalCovariance, | ||
SineHoleCovariance, | ||
SphericalCovariance, | ||
|
||
# fitting algorithms | ||
VariogramFitAlgo, | ||
WeightedLeastSquares, | ||
|
||
# plotting | ||
varioplot, | ||
varioplot! | ||
|
||
end |
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,25 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
""" | ||
BallSearchAccum(maxlag, nlags, distance) | ||
Accumulate pairs of points in geospatial data with | ||
nearest neighbors inside metric ball. | ||
""" | ||
struct BallSearchAccum{T,D} <: VariogramAccumAlgo | ||
nlags::Int | ||
maxlag::T | ||
distance::D | ||
end | ||
|
||
function neighfun(algo::BallSearchAccum, pset) | ||
ball = MetricBall(algo.maxlag, algo.distance) | ||
searcher = BallSearch(pset, ball) | ||
j -> @inbounds(search(pset[j], searcher)) | ||
end | ||
|
||
skipfun(::BallSearchAccum) = (i, j) -> i ≤ j | ||
|
||
exitfun(::BallSearchAccum) = h -> false |
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,21 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
""" | ||
FullSearchAccum(maxlag, nlags, distance) | ||
Accumulate pairs of points in geospatial data with | ||
exhaustive (or full) search. | ||
""" | ||
struct FullSearchAccum{T,D} <: VariogramAccumAlgo | ||
nlags::Int | ||
maxlag::T | ||
distance::D | ||
end | ||
|
||
neighfun(::FullSearchAccum, pset) = j -> (j + 1):nelements(pset) | ||
|
||
skipfun(::FullSearchAccum) = (i, j) -> false | ||
|
||
exitfun(algo::FullSearchAccum) = h -> h > algo.maxlag |
Oops, something went wrong.