From ec7318416a8bc13a6179f7e0a596f3f456293128 Mon Sep 17 00:00:00 2001 From: Daniel Thom Date: Thu, 14 Sep 2023 15:45:04 -0600 Subject: [PATCH] Fix handling of results with unknown timestamps --- src/core/results_by_time.jl | 15 +++++++++++---- .../decision_model_simulation_results.jl | 14 ++++++++++++++ test/Project.toml | 1 + 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/core/results_by_time.jl b/src/core/results_by_time.jl index 4dc2405410..827b32d4c4 100644 --- a/src/core/results_by_time.jl +++ b/src/core/results_by_time.jl @@ -1,4 +1,4 @@ -struct ResultsByTime{T} +mutable struct ResultsByTime{T} key::OptimizationContainerKey data::SortedDict{Dates.DateTime, T} resolution::Dates.Period @@ -51,9 +51,16 @@ function _add_timestamps!( timestamp::Dates.DateTime, data, ) - time_col = - range(timestamp; length = get_num_rows(results, data), step = results.resolution) - DataFrames.insertcols!(df, 1, :DateTime => time_col) + if results.resolution != Dates.Period(Dates.Millisecond(0)) + time_col = + range( + timestamp; + length = get_num_rows(results, data), + step = results.resolution, + ) + DataFrames.insertcols!(df, 1, :DateTime => time_col) + end + return end function make_dataframe( diff --git a/src/simulation/decision_model_simulation_results.jl b/src/simulation/decision_model_simulation_results.jl index f8bc532276..38e6bb958c 100644 --- a/src/simulation/decision_model_simulation_results.jl +++ b/src/simulation/decision_model_simulation_results.jl @@ -138,6 +138,7 @@ function _get_store_value( Dict{OptimizationContainerKey, ResultsByTime{DenseAxisArray{Float64, 2}}}() model_name = Symbol(get_model_name(sim_results)) resolution = get_resolution(sim_results) + horizon = get_forecast_horizon(sim_results) for key in container_keys results_by_time = ResultsByTime( @@ -146,11 +147,24 @@ function _get_store_value( resolution, get_column_names(store, DecisionModelIndexType, model_name, key), ) + array_size::Union{Nothing, Tuple{Int, Int}} = nothing for ts in timestamps array = read_result(DenseAxisArray, store, model_name, key, ts) + if isnothing(array_size) + array_size = size(array) + elseif size(array) != array_size + error( + "Arrays for $(encode_key_as_string(key)) at different timestamps have different sizes", + ) + end if convert_result_to_natural_units(key) array.data .*= base_power end + if array_size[2] != horizon + @warn "$(encode_key_as_string(key)) has a different horizon than the " * + "problem specification. Can't assign timestamps to the resulting DataFrame." + results_by_time.resolution = Dates.Period(Dates.Millisecond(0)) + end results_by_time[ts] = array end results_by_key[key] = results_by_time diff --git a/test/Project.toml b/test/Project.toml index 5e338be182..1b71bf8a36 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -21,6 +21,7 @@ PowerSimulations = "e690365d-45e2-57bb-ac84-44ba829e73c4" PowerSystemCaseBuilder = "f00506e0-b84f-492a-93c2-c0a9afc4364e" PowerSystems = "bcd98974-b02a-5e2f-9ee0-a103f5c450dd" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" SCS = "c946c3f1-0d1f-5ce8-9dea-7daa1f7e2d13" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"