diff --git a/src/empirical.jl b/src/empirical.jl index 335ab3c..35a8cd2 100644 --- a/src/empirical.jl +++ b/src/empirical.jl @@ -22,7 +22,85 @@ An empirical function estimated from data. """ abstract type EmpiricalFunction end +# ----------- +# IO METHODS +# ----------- + +Base.summary(io::IO, γ::EmpiricalFunction) = print(io, nameof(typeof(γ))) + +function Base.show(io::IO, γ::EmpiricalFunction) + ioctx = IOContext(io, :compact => true) + summary(ioctx, γ) + print(ioctx, "(") + print(ioctx, "distance: ", γ.distance) + print(ioctx, ", estimator: ", γ.estimator) + print(ioctx, ", npairs: ", sum(γ.counts)) + print(ioctx, ")") +end + +function Base.show(io::IO, ::MIME"text/plain", γ::EmpiricalFunction) + ioctx = IOContext(io, :compact => true, :limit => true) + summary(ioctx, γ) + println(ioctx) + print(ioctx, "├─ abscissas: ") + _printlnvec(ioctx, γ.abscissas, 3) + print(ioctx, "├─ ordinates: ") + _printlnvec(ioctx, γ.ordinates, 3) + println(ioctx, "├─ distance: ", γ.distance) + println(ioctx, "├─ estimator: ", γ.estimator) + print(ioctx, "└─ npairs: ", sum(γ.counts)) +end + +# ---------------- +# IMPLEMENTATIONS +# ---------------- + include("empirical/variogram.jl") include("empirical/transiogram.jl") include("empirical/varioplane.jl") + +# ----------------- +# HELPER FUNCTIONS +# ----------------- + +function _printlnvec(io, vec, n) + _printvec(io, vec, n) + println(io) +end + +function _printvec(io, vec::AbstractArray, n) + print(io, "[") + if length(vec) > 2n + k = n - 1 + join(io, vec[begin:(begin + k)], ", ") + print(io, ", ..., ") + join(io, vec[(end - k):end], ", ") + else + join(io, vec, ", ") + end + print(io, "]") +end + +function _printvec(io, vec::AbstractArray{<:AbstractArray}, n) + len = length(vec) + println(io) + if len > 2n + for i in 1:n + print(io, "│ ├─ ") + _printlnvec(io, vec[i], n) + end + println(io, "│ ⋮") + for i in (len - n + 1):(len - 1) + print(io, "│ ├─ ") + _printlnvec(io, vec[i], n) + end + else + for i in 1:(len - 1) + print(io, "│ ├─ ") + _printlnvec(io, vec[i], n) + end + end + print(io, "│ └─ ") + _printvec(io, vec[len], n) +end diff --git a/src/empirical/variogram.jl b/src/empirical/variogram.jl index a60c728..f1bdf92 100644 --- a/src/empirical/variogram.jl +++ b/src/empirical/variogram.jl @@ -163,54 +163,3 @@ function merge(γα::EmpiricalVariogram{ℒ,V,D,E}, γβ::EmpiricalVariogram{ℒ EmpiricalVariogram(n, x, y, d, e) end - -# ----------- -# IO METHODS -# ----------- - -function Base.show(io::IO, γ::EmpiricalVariogram) - ioctx = IOContext(io, :compact => true) - print(ioctx, "EmpiricalVariogram(") - print(ioctx, "abscissas: ") - _printvec(ioctx, γ.abscissas, 1) - print(ioctx, ", ordinates: ") - _printvec(ioctx, γ.ordinates, 1) - print(ioctx, ", distance: ", γ.distance) - print(ioctx, ", estimator: ", γ.estimator) - print(ioctx, ", npairs: ", sum(γ.counts)) - print(ioctx, ")") -end - -function Base.show(io::IO, ::MIME"text/plain", γ::EmpiricalVariogram) - ioctx = IOContext(io, :compact => true, :limit => true) - println(ioctx, "EmpiricalVariogram") - print(ioctx, "├─ abscissas: ") - _printlnvec(ioctx, γ.abscissas, 3) - print(ioctx, "├─ ordinates: ") - _printlnvec(ioctx, γ.ordinates, 3) - println(ioctx, "├─ distance: ", γ.distance) - println(ioctx, "├─ estimator: ", γ.estimator) - print(ioctx, "└─ npairs: ", sum(γ.counts)) -end - -# ----------------- -# HELPER FUNCTIONS -# ----------------- - -function _printlnvec(io, vec, n) - _printvec(io, vec, n) - println(io) -end - -function _printvec(io, vec, n) - print(io, "[") - if length(vec) > 2n - k = n - 1 - join(io, vec[begin:(begin + k)], ", ") - print(io, ", ..., ") - join(io, vec[(end - k):end], ", ") - else - join(io, vec, ", ") - end - print(io, "]") -end diff --git a/test/empirical.jl b/test/empirical.jl index c639540..a1c08fb 100644 --- a/test/empirical.jl +++ b/test/empirical.jl @@ -47,7 +47,7 @@ d = georef((z=rand(rng, 100, 100),)) g = EmpiricalVariogram(d, :z) @test sprint(show, g) == - "EmpiricalVariogram(abscissas: [0.25 m, ..., 9.93304 m], ordinates: [0.0, ..., 0.0841979], distance: Euclidean(0.0), estimator: MatheronEstimator(), npairs: 1447200)" + "EmpiricalVariogram(distance: Euclidean(0.0), estimator: MatheronEstimator(), npairs: 1447200)" @test sprint(show, MIME"text/plain"(), g) == """ EmpiricalVariogram ├─ abscissas: [0.25 m, 1.0 m, 1.41421 m, ..., 8.7407 m, 9.28182 m, 9.93304 m] @@ -129,6 +129,29 @@ end @test range(γₕ) / range(γᵥ) ≈ 3.0 atol = 0.1 end +@testset "EmpiricalTransiogram" begin + # print methods + rng = StableRNG(123) + d = georef((; z=rand(rng, 1:10, 1000)), rand(rng, Point, 1000)) + t = EmpiricalTransiogram(d, :z) + @test sprint(show, t) == + "EmpiricalTransiogram(distance: Euclidean(0.0), estimator: CarleEstimator(), npairs: 176100)" + @test sprint(show, MIME"text/plain"(), t) == """ + EmpiricalTransiogram + ├─ abscissas: [0.00249657 m, 0.00828093 m, 0.0128198 m, ..., 0.0875733 m, 0.0925317 m, 0.0974132 m] + ├─ ordinates: + │ ├─ [0.0, 0.0, 0.0, ..., 0.0, 0.0, 0.107143] + │ ├─ [0.0, 0.0, 0.0, ..., 0.0, 0.0869565, 0.103448] + │ ├─ [0.0, 0.0, 0.0, ..., 0.130435, 0.0909091, 0.142857] + │ ⋮ + │ ├─ [0.0, 0.0, 0.0, ..., 0.0555556, 0.166667, 0.0833333] + │ ├─ [0.0, 0.0, 0.0, ..., 0.125, 0.208333, 0.1] + │ └─ [0.0, 0.0, 0.0, ..., 0.130435, 0.0833333, 0.0909091] + ├─ distance: Euclidean(0.0) + ├─ estimator: CarleEstimator() + └─ npairs: 176100""" +end + @testset "EmpiricalVarioplane" begin img = readdlm(joinpath(datadir, "anisotropic.tsv")) data = georef((z=img,))