From b7874f2318c5a8b803774dad9c1e61630845409d Mon Sep 17 00:00:00 2001 From: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:56:12 -0300 Subject: [PATCH] Refactor the Processes show (#31) --- src/ioutils.jl | 23 +++++++++++++++++++++- src/processes.jl | 19 ++++++++++++++++++ test/field.jl | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ test/point.jl | 34 +++++++++++++++++++++++++++++++- 4 files changed, 125 insertions(+), 2 deletions(-) diff --git a/src/ioutils.jl b/src/ioutils.jl index 8555f81..baed39e 100644 --- a/src/ioutils.jl +++ b/src/ioutils.jl @@ -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 diff --git a/src/processes.jl b/src/processes.jl index 8e78db6..4a57c87 100644 --- a/src/processes.jl +++ b/src/processes.jl @@ -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 #----------------- diff --git a/test/field.jl b/test/field.jl index 96a33f1..33d75ce 100644 --- a/test/field.jl +++ b/test/field.jl @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/point.jl b/test/point.jl index 06d1401..ae784c5 100644 --- a/test/point.jl +++ b/test/point.jl @@ -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 @@ -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) @@ -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 @@ -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