|
| 1 | +using EasyHybrid: HybridParams, ParameterContainer, SingleNNHybridModel, MultiNNHybridModel |
| 2 | +using EasyHybrid: _print_field, _print_header, IndentedIO |
| 3 | + |
| 4 | +@testset "show_generic.jl" begin |
| 5 | + |
| 6 | + @testset "_print_field and _print_header" begin |
| 7 | + result = sprint( |
| 8 | + io -> begin |
| 9 | + _print_header(io, "Test Header", color = :blue) |
| 10 | + _print_field(io, "scalar", 42) |
| 11 | + _print_field(io, "bool_true", true) |
| 12 | + _print_field(io, "bool_false", false) |
| 13 | + _print_field(io, "namedtuple", (a = 1, b = 2.0)) |
| 14 | + _print_field(io, "function", sum) |
| 15 | + end, context = :color => false |
| 16 | + ) |
| 17 | + |
| 18 | + @test occursin("Test Header", result) |
| 19 | + @test occursin("scalar", result) && occursin("42", result) |
| 20 | + @test occursin("bool_true", result) && occursin("bool_false", result) |
| 21 | + @test occursin("namedtuple", result) && occursin("a =", result) && occursin("b =", result) |
| 22 | + @test occursin("function", result) && occursin("sum", result) |
| 23 | + end |
| 24 | + |
| 25 | + @testset "HybridParams show" begin |
| 26 | + params = (a = (1.0, 0.0, 2.0), b = (2.0, 1.0, 3.0)) |
| 27 | + pc = ParameterContainer(params) |
| 28 | + hp = HybridParams{typeof(sum)}(pc) |
| 29 | + |
| 30 | + # Compact show |
| 31 | + result_compact = sprint(show, hp, context = :color => false) |
| 32 | + @test occursin("HybridParams", result_compact) && occursin("ParameterContainer", result_compact) |
| 33 | + |
| 34 | + # Text/plain show |
| 35 | + result_full = sprint(show, MIME"text/plain"(), hp, context = :color => false) |
| 36 | + @test occursin("Hybrid Parameters", result_full) # only check for the header |
| 37 | + end |
| 38 | + |
| 39 | + @testset "ParameterContainer compact show" begin |
| 40 | + params = (a = (1.0, 0.0, 2.0), b = (2.0, 1.0, 3.0)) |
| 41 | + pc = ParameterContainer(params) |
| 42 | + |
| 43 | + result = sprint(show, pc, context = :color => false) |
| 44 | + @test occursin("ParameterContainer(a, b)", result) |
| 45 | + end |
| 46 | + |
| 47 | + @testset "SingleNNHybridModel show" begin |
| 48 | + function test_model(; x1, a, b) |
| 49 | + return (; y_pred = a .* x1 .+ b) |
| 50 | + end |
| 51 | + |
| 52 | + model = constructHybridModel( |
| 53 | + [:x1, :x2], [:x3], [:y], test_model, |
| 54 | + (a = (1.0, 0.0, 2.0), b = (2.0, 1.0, 3.0)), |
| 55 | + [:a], [:b]; |
| 56 | + hidden_layers = [4, 4], activation = tanh |
| 57 | + ) |
| 58 | + |
| 59 | + result = sprint(show, MIME"text/plain"(), model, context = :color => false) |
| 60 | + |
| 61 | + @test occursin("Hybrid Model (Single NN)", result) |
| 62 | + @test occursin("Neural Network:", result) && occursin("Configuration:", result) |
| 63 | + @test all( |
| 64 | + occursin.( |
| 65 | + [ |
| 66 | + "predictors", "forcing", "targets", "mechanistic_model", |
| 67 | + "neural_param_names", "global_param_names", "scale_nn_outputs", |
| 68 | + "start_from_default", "config", "Parameters:", |
| 69 | + ], Ref(result) |
| 70 | + ) |
| 71 | + ) |
| 72 | + end |
| 73 | + |
| 74 | + @testset "MultiNNHybridModel show" begin |
| 75 | + function test_model(; x1, x2, x3, a, b, c, d) |
| 76 | + return (; obs = a .* x2 .+ d .* x1 .+ b) |
| 77 | + end |
| 78 | + |
| 79 | + model = constructHybridModel( |
| 80 | + (a = [:x2, :x3], d = [:x1]), [:x1], [:obs], test_model, |
| 81 | + (a = (1.0, 0.0, 5.0), b = (2.0, 0.0, 10.0), c = (0.5, 0.0, 2.0), d = (0.5, 0.0, 2.0)), |
| 82 | + [:b]; # Only global_param_names, neural_param_names derived from predictors keys |
| 83 | + hidden_layers = [4, 4], activation = tanh |
| 84 | + ) |
| 85 | + |
| 86 | + result = sprint(show, MIME"text/plain"(), model, context = :color => false) |
| 87 | + |
| 88 | + @test occursin("Hybrid Model (Multi NN)", result) |
| 89 | + @test occursin("Neural Networks:", result) && occursin("Configuration:", result) |
| 90 | + @test all(occursin.(["predictors", "a", "d", "forcing", "targets", "config", "Parameters:"], Ref(result))) |
| 91 | + end |
| 92 | + |
| 93 | + @testset "IndentedIO" begin |
| 94 | + result = sprint( |
| 95 | + io -> begin |
| 96 | + ido = IndentedIO(io; indent = " ") |
| 97 | + println(ido, "line1") |
| 98 | + println(ido, "line2") |
| 99 | + end, context = :color => false |
| 100 | + ) |
| 101 | + |
| 102 | + @test occursin(" line1", result) && occursin(" line2", result) |
| 103 | + |
| 104 | + # Test flush, isopen, close, readavailable methods |
| 105 | + io1 = IOBuffer() |
| 106 | + ido1 = IndentedIO(io1; indent = " ") |
| 107 | + @test isopen(ido1) == isopen(io1) |
| 108 | + print(ido1, "test") |
| 109 | + flush(ido1) |
| 110 | + @test String(take!(io1)) == " test" |
| 111 | + |
| 112 | + # Test readavailable (for IOBuffer) |
| 113 | + io2 = IOBuffer("data") |
| 114 | + ido2 = IndentedIO(io2) |
| 115 | + @test readavailable(ido2) == UInt8[0x64, 0x61, 0x74, 0x61] |
| 116 | + |
| 117 | + # Test close |
| 118 | + io3 = IOBuffer() |
| 119 | + ido3 = IndentedIO(io3) |
| 120 | + @test isopen(ido3) |
| 121 | + close(ido3) |
| 122 | + @test !isopen(ido3) |
| 123 | + end |
| 124 | + |
| 125 | +end |
0 commit comments