Skip to content

Commit

Permalink
Refactor the Processes show (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Jun 11, 2024
1 parent 68c0d24 commit b7874f2
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/ioutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,26 @@ prettyname(obj) = prettyname(typeof(obj))
function prettyname(T::Type)
name = string(T)
name = replace(name, r"{.*" => "")
replace(name, r".+\." => "")
replace(name, r".*\." => "")
end

printfields(io, obj; kwargs...) = printfields(io, obj, fieldnames(typeof(obj)); kwargs...)

function printfields(io, obj, fnames; singleline=false)
if singleline
vals = map(enumerate(fnames)) do (i, field)
val = getfield(obj, i)
str = repr(val, context=io)
"$field: $str"
end
join(io, vals, ", ")
else
len = length(fnames)
for (i, field) in enumerate(fnames)
div = i == len ? "\n└─ " : "\n├─ "
val = getfield(obj, i)
str = repr(val, context=io)
print(io, "$div$field: $str")
end
end
end
19 changes: 19 additions & 0 deletions src/processes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,25 @@ _extract(::Type{Pair{Symbol,DataType}}, pairs) = nothing, first.(pairs), last.(p
_extract(::Type{Pair{T,DataType}}, pairs) where {T<:AbstractString} = nothing, Symbol.(first.(pairs)), last.(pairs)
_extract(::Type, pairs) = throw(ArgumentError("the data argument must be a geotable, a pair, or an iterable of pairs"))

# -----------
# IO METHODS
# -----------

Base.summary(io::IO, process::GeoStatsProcess) = print(io, prettyname(process))

function Base.show(io::IO, process::GeoStatsProcess)
name = prettyname(process)
ioctx = IOContext(io, :compact => true)
print(io, "$name(")
printfields(ioctx, process, singleline=true)
print(io, ")")
end

function Base.show(io::IO, ::MIME"text/plain", process::GeoStatsProcess)
summary(io, process)
printfields(io, process)
end

#-----------------
# IMPLEMENTATIONS
#-----------------
Expand Down
51 changes: 51 additions & 0 deletions test/field.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@
# the number of parameters must be equal to the number of variables
@test_throws AssertionError rand(process, 𝒟, [:a => Float64, :b => Float64], method)
end

@testset "show" begin
process = GaussianProcess()
@test sprint(show, process) == "GaussianProcess(variogram: GaussianVariogram(sill: 1.0, nugget: 0.0, range: 1.0 m, distance: Euclidean), mean: 0.0)"
@test sprint(show, MIME("text/plain"), process) == """
GaussianProcess
├─ variogram: GaussianVariogram(sill: 1.0, nugget: 0.0, range: 1.0 m, distance: Euclidean)
└─ mean: 0.0"""
end
end

@testset "LindgrenProcess" begin
Expand Down Expand Up @@ -218,6 +227,14 @@
for i in 1:3
@test isapprox(sum(r[i].z) / length(r[i].z), 0.0, atol=1e-3)
end

process = LindgrenProcess()
@test sprint(show, process) == "LindgrenProcess(range: 1.0 m, sill: 1.0, init: NearestInit())"
@test sprint(show, MIME("text/plain"), process) == """
LindgrenProcess
├─ range: 1.0 m
├─ sill: 1.0
└─ init: GeoStatsBase.NearestInit()"""
end

@testset "QuiltingProcess" begin
Expand All @@ -235,6 +252,19 @@
@test eltype(sims[1].facies) <: Union{Float64,Missing}
@test any(ismissing, sims[1].facies)
@test all(!isnan, skipmissing(sims[1].facies))

process = QuiltingProcess(trainimg, (30, 30))
@test sprint(show, process) == "QuiltingProcess(trainimg: 62500×2 GeoTable over 250×250 CartesianGrid, tilesize: (30, 30), overlap: nothing, path: :raster, inactive: nothing, soft: nothing, tol: 0.1, init: NearestInit())"
@test sprint(show, MIME("text/plain"), process) == """
QuiltingProcess
├─ trainimg: 62500×2 GeoTable over 250×250 CartesianGrid
├─ tilesize: (30, 30)
├─ overlap: nothing
├─ path: :raster
├─ inactive: nothing
├─ soft: nothing
├─ tol: 0.1
└─ init: GeoStatsBase.NearestInit()"""
end

@testset "TuringProcess" begin
Expand All @@ -244,6 +274,15 @@
@test length(sims) == 3
@test size(domain(sims[1])) == (200, 200)
@test eltype(sims[1].z) <: Float64

process = TuringProcess()
@test sprint(show, process) == "TuringProcess(params: nothing, blur: nothing, edge: nothing, iter: 100)"
@test sprint(show, MIME("text/plain"), process) == """
TuringProcess
├─ params: nothing
├─ blur: nothing
├─ edge: nothing
└─ iter: 100"""
end

@testset "StrataProcess" begin
Expand All @@ -257,5 +296,17 @@
@test eltype(sims[1].z) <: Union{Float64,Missing}
@test any(ismissing, sims[1].z)
@test all(!isnan, skipmissing(sims[1].z))

rng = MersenneTwister(2019)
proc = SmoothingProcess()
env = Environment(rng, [proc, proc], [0.5 0.5; 0.5 0.5], ExponentialDuration(rng, 1.0))
process = StrataProcess(env)
@test sprint(show, process) == "StrataProcess(environment: Environment{MersenneTwister}(MersenneTwister(2019), SmoothingProcess[SmoothingProcess(3.0), SmoothingProcess(3.0)], [0.5 0.5; 0.5 0.5], ExponentialDuration{MersenneTwister}(MersenneTwister(2019), 1.0)), state: nothing, stack: :erosional, nepochs: 10)"
@test sprint(show, MIME("text/plain"), process) == """
StrataProcess
├─ environment: Environment{MersenneTwister}(MersenneTwister(2019), SmoothingProcess[SmoothingProcess(3.0), SmoothingProcess(3.0)], [0.5 0.5; 0.5 0.5], ExponentialDuration{MersenneTwister}(MersenneTwister(2019), 1.0))
├─ state: nothing
├─ stack: :erosional
└─ nepochs: 10"""
end
end
34 changes: 33 additions & 1 deletion test/point.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
pp = rand(p, g)
@test nelements(pp) == 10
end

p = BinomialProcess(10)
@test sprint(show, p) == "BinomialProcess(n: 10)"
@test sprint(show, MIME("text/plain"), p) == """
BinomialProcess
└─ n: 10"""
end

@testset "Poisson" begin
Expand All @@ -58,9 +64,21 @@

pp = PointSet(rand(Point{2}, 10))
@test isnothing(rand(PoissonProcess(100.0), pp))

p = PoissonProcess(100.0)
@test sprint(show, p) == "PoissonProcess(λ: 100.0)"
@test sprint(show, MIME("text/plain"), p) == """
PoissonProcess
└─ λ: 100.0"""
end

@testset "Inhibition" begin end
@testset "Inhibition" begin
p = InhibitionProcess(0.1)
@test sprint(show, p) == "InhibitionProcess(δ: 0.1)"
@test sprint(show, MIME("text/plain"), p) == """
InhibitionProcess
└─ δ: 0.1"""
end

@testset "Cluster" begin
binom = BinomialProcess(100)
Expand Down Expand Up @@ -94,6 +112,13 @@
@test all((g), pp)
end
end

p = ClusterProcess(binom, identity)
@test sprint(show, p) == "ClusterProcess(proc: BinomialProcess(n: 100), ofun: identity)"
@test sprint(show, MIME("text/plain"), p) == """
ClusterProcess
├─ proc: BinomialProcess(n: 100)
└─ ofun: identity"""
end

@testset "Union" begin
Expand All @@ -107,6 +132,13 @@
@test s[1] isa PointSet
@test s[2] isa PointSet
@test nelements.(s) == [100, 100]

p = BinomialProcess(50) BinomialProcess(100)
@test sprint(show, p) == "UnionProcess(p₁: BinomialProcess(n: 50), p₂: BinomialProcess(n: 100))"
@test sprint(show, MIME("text/plain"), p) == """
UnionProcess
├─ p₁: BinomialProcess(n: 50)
└─ p₂: BinomialProcess(n: 100)"""
end

@testset "Thinning" begin
Expand Down

0 comments on commit b7874f2

Please sign in to comment.