From 986846b89563d70bd840b9de9b8e9feef6794a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Hoffimann?= Date: Tue, 29 Oct 2024 16:27:32 -0300 Subject: [PATCH] Refactor EmpiricalVarioplane --- ext/varioplot.jl | 30 ++++++++---------------------- src/empirical/varioplane.jl | 27 ++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/ext/varioplot.jl b/ext/varioplot.jl index 84fca9b..7118d2f 100644 --- a/ext/varioplot.jl +++ b/ext/varioplot.jl @@ -66,38 +66,24 @@ function Makie.plot!(plot::VarioPlot{<:Tuple{EmpiricalVarioplane}}) # retrieve varioplane object v = plot[:γ] - # underyling variograms - γs = Makie.@lift $v.γs - # polar angle θs = Makie.@lift $v.θs # polar radius - rs = Makie.@lift ustrip.($γs[1].abscissas) - - # variogram ordinates for all variograms - H = Makie.@lift let - hs = map($γs) do γ - # retrieve ordinates without units - ys = ustrip.(γ.ordinates) - - # handle NaN ordinates (i.e. empty bins) - isnan(ys[1]) && (ys[1] = 0) - for i in 2:length(ys) - isnan(ys[i]) && (ys[i] = ys[i - 1]) - end - - ys - end - reduce(hcat, hs) - end + rs = Makie.@lift $v.rs + + # varioplane values + hs = Makie.@lift $v.hs + + # values in matrix form + H = Makie.@lift reduce(hcat, $hs) # exploit symmetry θs = Makie.@lift range(0, 2π, length=2 * length($θs)) H = Makie.@lift [$H $H] # hide hole at center - rs = Makie.@lift [0; $rs] + rs = Makie.@lift [zero(eltype($rs)); $rs] H = Makie.@lift [$H[1:1, :]; $H] # transpose for plotting diff --git a/src/empirical/varioplane.jl b/src/empirical/varioplane.jl index 575fd25..3839fda 100644 --- a/src/empirical/varioplane.jl +++ b/src/empirical/varioplane.jl @@ -16,9 +16,10 @@ the tolerance `dtol` in length units for the direction partition, the number of angles `nangs` in the plane, and forward the `parameters` to the underlying [`EmpiricalVariogram`](@ref). """ -struct EmpiricalVarioplane{T,V} +struct EmpiricalVarioplane{T,R,V} θs::Vector{T} - γs::Vector{V} + rs::Vector{R} + hs::Vector{V} end function EmpiricalVarioplane( @@ -48,18 +49,34 @@ function EmpiricalVarioplane( planes = collect(subset) u, v = householderbasis(normal) else - @error "varioplane only supported in 2D or 3D" + throw(ArgumentError("varioplane only supported in 2D or 3D")) end - # loop over half of the plane + # polar angles for half plane (variogram is symmetric) θs = collect(range(0, stop=π, length=nangs)) + + # estimate directional variograms across planes γs = map(θs) do θ dir = DirectionPartition(cos(θ) * u + sin(θ) * v, tol=dtol) γ(plane) = EmpiricalVariogram(partition(rng, plane, dir), var₁, var₂; kwargs...) tmapreduce(γ, merge, planes) end - EmpiricalVarioplane(θs, γs) + # polar radii + rs = first(γs).abscissas + + # varioplane values + hs = map(γs) do γ + h = γ.ordinates + # handle NaN ordinates (i.e. empty bins) + isnan(h[1]) && (h[1] = zero(eltype(h))) + for i in 2:length(h) + isnan(h[i]) && (h[i] = h[i - 1]) + end + h + end + + EmpiricalVarioplane(θs, rs, hs) end # -----------