Skip to content

Commit

Permalink
Add tests for built-in metrics; fix bugs thereby uncovered
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielKS committed Oct 1, 2024
1 parent c43c5e4 commit d0c7e1f
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/builtin_metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ import
..weighted_mean,
..unweighted_sum,
..mean,
..read_component_result
..read_component_result,
...Selectors.all_loads, # number of dots obtained by trial and error
...Selectors.all_storage
export calc_active_power,
calc_production_cost,
calc_active_power_in,
Expand Down Expand Up @@ -160,7 +162,7 @@ calc_system_load_from_storage = let
eval_fn = (
res::IS.Results; kwargs...
) ->
compute(calc_load_from_storage, res, storage_component_selector; kwargs...),
compute(calc_load_from_storage, res, all_storage; kwargs...),
)
end

Expand Down
2 changes: 1 addition & 1 deletion src/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ get_time_agg_fn(m::TimedMetric) = m.time_agg_fn
get_component_agg_fn(m::ComponentTimedMetric) = m.component_agg_fn
get_time_meta_agg_fn(m::TimedMetric) = m.time_meta_agg_fn
get_component_meta_agg_fn(m::ComponentTimedMetric) = m.component_meta_agg_fn
get_eval_zero(m::ComponentTimedMetric) = m.component_meta_agg_fn
get_eval_zero(m::ComponentTimedMetric) = m.eval_zero

"""
Returns a `Metric` identical to the input `metric` except with the changes to its
Expand Down
2 changes: 2 additions & 0 deletions test/setuptests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import InfrastructureSystems: Deterministic, Probabilistic, Scenarios, Forecast
using PowerSystems
using PowerAnalytics
using PowerAnalytics.Selectors
using PowerAnalytics.Metrics
using PowerSimulations
using GLPK
using TimeSeries
Expand All @@ -35,6 +36,7 @@ const PSB = PowerSystemCaseBuilder

const DISABLED_TEST_FILES = [ # Can generate with ls -1 test | grep "test_.*.jl"
# "test_builtin_component_selectors.jl",
# "test_builtin_metrics.jl",
# "test_input.jl",
# "test_metrics.jl",
# "test_result_sorting.jl",
Expand Down
121 changes: 121 additions & 0 deletions test/test_builtin_metrics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# For now we are mostly just testing that all the metrics can be called without error,
# though I've built out the structure to do much more than that. TODO it would be great if
# we did.

(results_uc, results_ed) = run_test_sim(TEST_RESULT_DIR, TEST_SIM_NAME)
const ResultType = AbstractDataFrame

# Future implementers of built-in metrics must add tests as shown below or this will trigger
function test_metric(::Val{metric_name}) where {metric_name}
throw("Could not find test for $metric_name")
end

function test_metric(::Val{:calc_active_power})
@test calc_active_power(make_selector(ThermalStandard), results_uc) isa ResultType
end

function test_metric(::Val{:calc_active_power_forecast})
@test calc_active_power_forecast(make_selector(RenewableDispatch), results_uc) isa
ResultType
end

function test_metric(::Val{:calc_active_power_in})
@test calc_active_power_in(make_selector(EnergyReservoirStorage), results_uc) isa
ResultType
end

function test_metric(::Val{:calc_active_power_out})
@test calc_active_power_out(make_selector(EnergyReservoirStorage), results_uc) isa
ResultType
end

function test_metric(::Val{:calc_capacity_factor})
@test calc_capacity_factor(make_selector(RenewableDispatch), results_uc) isa ResultType
end

function test_metric(::Val{:calc_curtailment})
@test calc_curtailment(make_selector(RenewableDispatch), results_uc) isa ResultType
end

function test_metric(::Val{:calc_curtailment_frac})
@test calc_curtailment_frac(make_selector(RenewableDispatch), results_uc) isa ResultType
end

function test_metric(::Val{:calc_discharge_cycles})
@test calc_discharge_cycles(make_selector(EnergyReservoirStorage), results_uc) isa
ResultType
end

function test_metric(::Val{:calc_integration})
@test calc_integration(make_selector(RenewableDispatch), results_uc) isa ResultType
end

function test_metric(::Val{:calc_is_slack_up})
@test calc_is_slack_up(results_ed) isa ResultType
end

function test_metric(::Val{:calc_load_forecast})
@test calc_load_forecast(make_selector(ElectricLoad), results_uc) isa ResultType
end

function test_metric(::Val{:calc_load_from_storage})
@test calc_load_from_storage(make_selector(EnergyReservoirStorage), results_uc) isa
ResultType
end

function test_metric(::Val{:calc_net_load_forecast})
@test calc_load_from_storage(make_selector(EnergyReservoirStorage), results_uc) isa
ResultType
end

function test_metric(::Val{:calc_production_cost})
@test calc_production_cost(make_selector(ThermalStandard), results_uc) isa ResultType
end

function test_metric(::Val{:calc_shutdown_cost})
@test calc_shutdown_cost(make_selector(ThermalStandard), results_uc) isa ResultType
end

function test_metric(::Val{:calc_startup_cost})
@test calc_startup_cost(make_selector(ThermalStandard), results_uc) isa ResultType
end

function test_metric(::Val{:calc_stored_energy})
@test calc_stored_energy(make_selector(EnergyReservoirStorage), results_uc) isa
ResultType
end

function test_metric(::Val{:calc_sum_bytes_alloc})
@test calc_sum_bytes_alloc(results_uc) isa ResultType
end

function test_metric(::Val{:calc_sum_objective_value})
@test calc_sum_objective_value(results_uc) isa ResultType
end

function test_metric(::Val{:calc_sum_solve_time})
@test calc_sum_solve_time(results_uc) isa ResultType
end

function test_metric(::Val{:calc_system_load_forecast})
@test calc_system_load_forecast(results_uc) isa ResultType
end

function test_metric(::Val{:calc_system_load_from_storage})
@test calc_system_load_from_storage(results_uc) isa ResultType
end

function test_metric(::Val{:calc_system_slack_up})
@test calc_system_slack_up(results_ed) isa ResultType
end

function test_metric(::Val{:calc_total_cost})
@test calc_total_cost(make_selector(ThermalStandard), results_uc) isa ResultType
end

all_metric_names = filter(x -> getproperty(PowerAnalytics.Metrics, x) isa Metric,
names(PowerAnalytics.Metrics))

@testset for metric_name in all_metric_names
test_metric(Val(metric_name))
end

0 comments on commit d0c7e1f

Please sign in to comment.