Skip to content

Commit

Permalink
Copy files from Variography.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
juliohm committed Jan 24, 2024
1 parent b282601 commit 90dcc78
Show file tree
Hide file tree
Showing 41 changed files with 4,929 additions and 9 deletions.
45 changes: 39 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,44 @@ uuid = "6771c435-bc22-4842-b0c3-41852a255103"
authors = ["Júlio Hoffimann <julio.hoffimann@gmail.com> and contributors"]
version = "0.1.0"

[compat]
julia = "1.9"
[deps]
Bessels = "0e736298-9ec6-45e8-9647-e4fc86a2fe38"
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
GeoTables = "e502b557-6362-48c1-8219-d30d308dcdb0"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa"
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Transducers = "28d57a85-8fef-5791-bfe6-a80928e7c999"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[weakdeps]
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[extensions]
GeoStatsFunctionsMakieExt = "Makie"

[targets]
test = ["Test"]
[compat]
Bessels = "0.2"
Distances = "0.10"
GeoTables = "1.7"
InteractiveUtils = "1.9"
LinearAlgebra = "1.9"
Makie = "0.20"
Meshes = "0.39"
NearestNeighbors = "0.4"
Optim = "1.7"
Printf = "1.9"
Random = "1.9"
Setfield = "1.0"
Statistics = "1.9"
Tables = "1.6"
Transducers = "0.4"
Unitful = "1.11"
julia = "1.9"
161 changes: 161 additions & 0 deletions ext/GeoStatsFunctionsMakieExt.jl
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
87 changes: 86 additions & 1 deletion src/GeoStatsFunctions.jl
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
25 changes: 25 additions & 0 deletions src/algorithms/ballsearch.jl
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
21 changes: 21 additions & 0 deletions src/algorithms/fullsearch.jl
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
Loading

0 comments on commit 90dcc78

Please sign in to comment.