Skip to content

Commit

Permalink
Fix handling of results with unknown timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-thom committed Sep 14, 2023
1 parent e69e29d commit 19834ff
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/core/results_by_time.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct ResultsByTime{T}
mutable struct ResultsByTime{T}
key::OptimizationContainerKey
data::SortedDict{Dates.DateTime, T}
resolution::Dates.Period
Expand Down Expand Up @@ -51,9 +51,12 @@ 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(
Expand Down
12 changes: 12 additions & 0 deletions src/simulation/decision_model_simulation_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -146,11 +147,22 @@ 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")

Check warning on line 156 in src/simulation/decision_model_simulation_results.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/decision_model_simulation_results.jl#L156

Added line #L156 was not covered by tests
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 " *

Check warning on line 162 in src/simulation/decision_model_simulation_results.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/decision_model_simulation_results.jl#L162

Added line #L162 was not covered by tests
"problem specification. Can't assign timestamps to the resulting DataFrame."
results_by_time.resolution = Dates.Period(Dates.Millisecond(0))

Check warning on line 164 in src/simulation/decision_model_simulation_results.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/decision_model_simulation_results.jl#L164

Added line #L164 was not covered by tests
end
results_by_time[ts] = array
end
results_by_key[key] = results_by_time
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 19834ff

Please sign in to comment.