Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update EmpiricalFunction show #29

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/empirical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
51 changes: 0 additions & 51 deletions src/empirical/variogram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 24 additions & 1 deletion test/empirical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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,))
Expand Down