From b37a78d10be0fc35bee0168c9bd5b5866d008f85 Mon Sep 17 00:00:00 2001 From: GabrielKS <23368820+GabrielKS@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:08:24 -0600 Subject: [PATCH] Accommodate `ComponentSelector` API changes --- src/builtin_component_selectors.jl | 6 +++--- src/metrics.jl | 14 +++----------- test/test_builtin_metrics.jl | 8 ++++++-- test/test_metrics.jl | 21 +++++++++++++-------- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/builtin_component_selectors.jl b/src/builtin_component_selectors.jl index 7d46048..f176fa5 100644 --- a/src/builtin_component_selectors.jl +++ b/src/builtin_component_selectors.jl @@ -38,7 +38,7 @@ function make_fuel_component_selector(category_spec::Dict) selector_name = join(ifelse.(isnothing.(parse_results), "", string.(parse_results)), COMPONENT_NAME_DELIMITER) - return make_selector(gen_type, filter_closure; name = selector_name) + return make_selector(filter_closure, gen_type; name = selector_name) end # Based on old PowerAnalytics' get_generator_mapping @@ -69,10 +69,10 @@ export generators_by_category "A ComponentSelector representing all the electric load in a System" -all_loads::ComponentSelector = make_selector(PSY.ElectricLoad) +all_loads::ComponentSelector = make_selector(PSY.ElectricLoad; groupby = :all) "A ComponentSelector representing all the storage in a System" -all_storage::ComponentSelector = make_selector(PSY.Storage) +all_storage::ComponentSelector = make_selector(PSY.Storage; groupby = :all) """ A dictionary of `ComponentSelector`s, each of which corresponds to one of the generator diff --git a/src/metrics.jl b/src/metrics.jl index 19e0b16..f6c2f62 100644 --- a/src/metrics.jl +++ b/src/metrics.jl @@ -261,11 +261,7 @@ function _compute_one(metric::ComponentTimedMetric, results::IS.Results, # TODO incorporate allow_missing agg_fn = get_component_agg_fn(metric) meta_agg_fn = get_component_meta_agg_fn(metric) - components = get_components( - selector, - PowerSimulations.get_system(results); - scope_limiter = is_available_for_analytics, - ) + components = get_components(selector, results) vals = [ compute(metric, results, com; kwargs...) for com in components @@ -312,18 +308,14 @@ group. Exclude components marked as not available. """ function compute(metric::ComponentTimedMetric, results::IS.Results, selector::ComponentSelector; kwargs...) - subents = PSY.get_groups( - selector, - PowerSimulations.get_system(results); - scope_limiter = is_available_for_analytics, - ) + subents = get_groups(selector, results) subcomputations = [_compute_one(metric, results, sub; kwargs...) for sub in subents] return hcat_timed(subcomputations...) end # COMPUTE_ALL() _is_single_group(selector::ComponentSelector, results::IS.Results) = - length(get_groups(selector, get_system(results))) == 1 + length(get_groups(selector, results)) == 1 _is_single_group(selector, results::IS.Results) = true # The core of compute_all, shared between the timed and timeless versions diff --git a/test/test_builtin_metrics.jl b/test/test_builtin_metrics.jl index ae482fb..60913d7 100644 --- a/test/test_builtin_metrics.jl +++ b/test/test_builtin_metrics.jl @@ -34,7 +34,9 @@ function test_metric(::Val{:calc_capacity_factor}) end function test_metric(::Val{:calc_curtailment}) - @test calc_curtailment(make_selector(RenewableDispatch), results_uc) isa ResultType + # TODO broken for groupby = :each? + @test calc_curtailment(make_selector(RenewableDispatch; groupby = :all), results_uc) isa + ResultType end function test_metric(::Val{:calc_curtailment_frac}) @@ -47,7 +49,9 @@ function test_metric(::Val{:calc_discharge_cycles}) end function test_metric(::Val{:calc_integration}) - @test calc_integration(make_selector(RenewableDispatch), results_uc) isa ResultType + # TODO broken for groupby = :each? + @test calc_integration(make_selector(RenewableDispatch; groupby = :all), results_uc) isa + ResultType end function test_metric(::Val{:calc_is_slack_up}) diff --git a/test/test_metrics.jl b/test/test_metrics.jl index 5b4c6b8..b82da15 100644 --- a/test/test_metrics.jl +++ b/test/test_metrics.jl @@ -344,7 +344,7 @@ end test_selector_sets = [ make_selector(make_selector(wind_sel, solar_sel)), make_selector(make_selector(test_selectors...)), - make_selector(ThermalStandard), + make_selector(ThermalStandard; groupby = :all), ] for (label, res) in pairs(resultses) @@ -431,8 +431,10 @@ end test_calc_production_cost, test_calc_production_cost] my_component = first(get_components(RenewableDispatch, get_system(results_uc))) my_selectors = - [make_selector(ThermalStandard), make_selector(RenewableDispatch), - make_selector(ThermalStandard), my_component] + [make_selector(ThermalStandard; groupby = :all), + make_selector(RenewableDispatch; groupby = :all), + make_selector(ThermalStandard; groupby = :all), + my_component] all_result = compute_all(results_uc, my_metrics, my_selectors) for (metric, selector) in zip(my_metrics, my_selectors) @@ -486,11 +488,12 @@ end broadcasted_compute_all = compute_all( results_uc, [test_calc_active_power, test_calc_active_power], - make_selector(ThermalStandard), + make_selector(ThermalStandard; groupby = :all), ["discard", "ThermalStandard"], ) @test broadcasted_compute_all[!, [DATETIME_COL, "ThermalStandard"]] == - compute(test_calc_active_power, results_uc, make_selector(ThermalStandard)) + compute(test_calc_active_power, results_uc, + make_selector(ThermalStandard; groupby = :all)) @test compute_all(results_uc, my_metrics, my_selectors, my_names) == compute_all(results_uc, collect(zip(my_metrics, my_selectors, my_names))...) @@ -513,7 +516,8 @@ end end @testset "Test compose_metrics" begin - mysel = make_selector(ThermalStandard) + # TODO broken for groupby = :each? + mysel = make_selector(ThermalStandard; groupby = :all) "Computes ActivePower*3" mymet1 = compose_metrics( "ThriceActivePower", @@ -595,7 +599,8 @@ end end @testset "Test component_agg_fn and corresponding `compute` aggregation behavior" begin - my_selector = make_selector(ThermalStandard) + # TODO broken for groupby = :each? + my_selector = make_selector(ThermalStandard; groupby = :all) my_results = results_uc sum_metric = test_calc_active_power @test get_component_agg_fn(sum_metric) == sum # Should be the default @@ -633,7 +638,7 @@ end end @testset "Test time_agg_fn and corresponding `aggregate_time` aggregation behavior" begin - my_selector = make_selector(ThermalStandard) + my_selector = make_selector(ThermalStandard; groupby = :all) my_results = results_uc sum_metric = test_calc_active_power @test get_time_agg_fn(sum_metric) == sum # Should be the default