Skip to content

Commit

Permalink
Add levels to transiogram and plot
Browse files Browse the repository at this point in the history
  • Loading branch information
juliohm committed Oct 7, 2024
1 parent e24a9bc commit 3cfbbb1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
18 changes: 10 additions & 8 deletions ext/transioplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,32 @@ function transioplot(t::Transiogram;
size=1.5,
maxlag=nothing
)
# effective ranges and variable levels
r = GeoStatsFunctions.ranges(t)
l = GeoStatsFunctions.levels(t)

# number of levels
L = length(l)

# retrieve maximum lag
H = if isnothing(maxlag)
_maxlag(t)
else
_addunit(maxlag, u"m")
end

# effective ranges
r = GeoStatsFunctions.ranges(t)

# transiogram up to maximum lag
x = range(zero(H), stop=H, length=100)
y = t.(x)

# number of levels
L = Base.size(first(y), 1)

fig = Makie.Figure()
for i in 1:L, j in 1:L
lᵢ, lⱼ = l[i], l[j]
ax = Makie.Axis(fig[i, j])
ys = getindex.(y, i, j)
Makie.lines!(ax, x, ys, color=color, linewidth=size, label = "$i$j")
Makie.lines!(ax, x, ys, color=color, linewidth=size, label = "$lᵢ$lⱼ")
if i == j
# effective range
# show effective range
Makie.lines!(ax, [zero(H), r[i]], [1.0, 0.0], color=color, linewidth=size, linestyle=:dash)
end
Makie.axislegend(position = i == j ? :rt : :rb)
Expand Down
1 change: 1 addition & 0 deletions src/GeoStatsFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ using Printf

import Base: merge, +, *
import Meshes: isisotropic
import CategoricalArrays: levels

# temporary fix for ⋅ with missing values
# https://github.com/JuliaLang/julia/issues/40743
Expand Down
20 changes: 13 additions & 7 deletions src/theoretical/transiogram/exponential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@ Optionally, specify a metric `ball` to model anisotropy.
* Carle, S.F. & Fogg, G.E. 1996. [Transition probability-based
indicator geostatistics](https://link.springer.com/article/10.1007/BF02083656)
"""
struct ExponentialTransiogram{R<:StaticMatrix,B<:MetricBall} <: Transiogram
struct ExponentialTransiogram{R<:StaticMatrix,L<:AbstractVector,B<:MetricBall} <: Transiogram
rate::R
levs::L
ball::B

function ExponentialTransiogram{R,B}(rate, ball) where {R<:StaticMatrix,B<:MetricBall}
function ExponentialTransiogram{R,L,B}(rate, levs, ball) where {R<:StaticMatrix,L<:AbstractVector,B<:MetricBall}
if !allequal(size(rate))
throw(ArgumentError("transition rate matrix must be square"))
end
new(rate, ball)
if length(levs) != size(rate, 1)
throw(ArgumentError("levels do not match size of transition rate matrix"))
end
new(rate, levs, ball)
end
end

function ExponentialTransiogram(rate::AbstractMatrix, ball::MetricBall)
function ExponentialTransiogram(ball::MetricBall, rate::AbstractMatrix; levels=1:size(rate, 1))
srate = SMatrix{size(rate)...}(rate)
ExponentialTransiogram{typeof(srate),typeof(ball)}(srate, ball)
ExponentialTransiogram{typeof(srate),typeof(levels),typeof(ball)}(srate, levels, ball)
end

function ExponentialTransiogram(rate::AbstractMatrix)
function ExponentialTransiogram(rate::AbstractMatrix; levels=1:size(rate, 1))
ball = MetricBall(1 / unit(eltype(rate)))
ExponentialTransiogram(rate, ball)
ExponentialTransiogram(ball, rate; levels)
end

"""
Expand All @@ -42,4 +46,6 @@ Return the transition rate matrix of the transiogram `t`.
"""
ratematrix(t::ExponentialTransiogram) = t.rate

levels(t::ExponentialTransiogram) = t.levs

(t::ExponentialTransiogram)(h) = exp(h * t.rate)

0 comments on commit 3cfbbb1

Please sign in to comment.