From 8ed2e302c7212388ba3c4ec67c4737eaa44aeed5 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 30 Apr 2024 21:41:03 +1200 Subject: [PATCH] Add a test for non-finite data in publication_plot (#738) --- .github/workflows/ci.yml | 18 +++++------------ src/visualization/publication_plot.jl | 10 ++++++++++ test/visualization/visualization.jl | 28 +++++++++++++++++---------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4fad91696..0765492e1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,24 +24,16 @@ jobs: os: ubuntu-latest arch: x64 steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@v1 + - uses: actions/checkout@v4 + - uses: julia-actions/setup-julia@v2 with: version: ${{ matrix.version }} arch: ${{ matrix.arch }} - - uses: actions/cache@v1 - env: - cache-name: cache-artifacts - with: - path: ~/.julia/artifacts - key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }} - restore-keys: | - ${{ runner.os }}-test-${{ env.cache-name }}- - ${{ runner.os }}-test- - ${{ runner.os }}- + - uses: julia-actions/cache@v1 - uses: julia-actions/julia-buildpkg@v1 - uses: julia-actions/julia-runtest@v1 - uses: julia-actions/julia-processcoverage@v1 - - uses: codecov/codecov-action@v1 + - uses: codecov/codecov-action@v4 with: file: lcov.info + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/src/visualization/publication_plot.jl b/src/visualization/publication_plot.jl index 021d3fb3a..20a86157b 100644 --- a/src/visualization/publication_plot.jl +++ b/src/visualization/publication_plot.jl @@ -14,6 +14,16 @@ function publication_data( output_array = fill(NaN, length(quantiles), max_stages) for stage in 1:max_stages stage_data = stage_function.([data[stage] for data in dataset]) + for (i, s) in enumerate(stage_data) + if !isfinite(s) + error( + "Unable to plot `publication_plot` because stage $stage " * + "of replication $i contains data that is not finite. " * + "The data function must return a finite real-valued " * + "scalar. Got: $s", + ) + end + end output_array[:, stage] .= Statistics.quantile(stage_data, quantiles) end return output_array diff --git a/test/visualization/visualization.jl b/test/visualization/visualization.jl index 7927b872a..56d97a2eb 100644 --- a/test/visualization/visualization.jl +++ b/test/visualization/visualization.jl @@ -57,16 +57,24 @@ function test_SpaghettiPlot() end function test_PublicationPlot() - data = SDDP.publication_data( - [ - [Dict{Symbol,Any}(:x => 1), Dict{Symbol,Any}(:x => 5)], - [Dict{Symbol,Any}(:x => 2), Dict{Symbol,Any}(:x => 6)], - [Dict{Symbol,Any}(:x => 3), Dict{Symbol,Any}(:x => 4)], - ], - [0.0, 1.0], - (d) -> d[:x], - ) - @test data == [1 4; 3 6] + simulations = [ + [Dict{Symbol,Any}(:x => 1), Dict{Symbol,Any}(:x => 5)], + [Dict{Symbol,Any}(:x => 2), Dict{Symbol,Any}(:x => 6)], + [Dict{Symbol,Any}(:x => 3), Dict{Symbol,Any}(:x => 4)], + ] + data = SDDP.publication_data(simulations, [0.0, 0.25, 0.5, 1.0], d -> d[:x]) + @test data == [1 4; 1.5 4.5; 2 5; 3 6] + for val in (-Inf, Inf, NaN) + simulations[2][2] = Dict{Symbol,Any}(:x => val) + @test_throws( + ErrorException( + "Unable to plot `publication_plot` because stage 2 of " * + "replication 2 contains data that is not finite. The data " * + "function must return a finite real-valued scalar. Got: $val", + ), + SDDP.publication_data(simulations, [0.5], d -> d[:x]), + ) + end return end