Skip to content

Commit

Permalink
Select plot components (#107)
Browse files Browse the repository at this point in the history
* Add ability to filter plot components

* add documentation for filtered comp plot
  • Loading branch information
noahrhodes authored Aug 24, 2022
1 parent a76371b commit 7af6401
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 11 deletions.
12 changes: 12 additions & 0 deletions docs/src/examples/basic examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ powerplot(data;
)
```

# Specify components for plotting
Supported components for plotting are specified as:
```@example power_data
supported_component_types
```

By default, all of these components found in the data dictionary will be plotted. However, it is possible to plot a subset of the components using the `components` keyword.

```@example power_data
powerplot(data; components=["bus","branch"], width=300, height=300)
```

# Power Flow
If the variables `pf` (power from) and `pt` (power to) exist in the data, power flow directions can be visualized using the `show_flow` boolean toggle (true by default).

Expand Down
1 change: 1 addition & 0 deletions docs/src/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ These paramters modify the entire plot.
| `layout_algorithim` | algorithm for generating network layout (see [Layouts](@ref)) | `kamada_kawai` |
| `fixed` | use fixed coordinates from network model | `false` |
| `parallel_edge_offset` | offset distance between parallel edges | `0.05` |
| `components` | string array of components to plot | `supported_component_types`|



Expand Down
5 changes: 4 additions & 1 deletion src/core/types.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

const supported_component_types = ["bus","gen","branch","dcline","load", "connector", "nw"]
const supported_component_types = ["bus","gen","branch","dcline","load", "connector"]
const supported_node_types = ["bus","gen","load"]
const supported_edge_types = ["branch","dcline","connector"]


"""
PowerModelsGraph
Expand Down
1 change: 0 additions & 1 deletion src/experimental/experimental.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ module Experimental
end

function _set_geo_coords!(plot_layer::AbstractDict{<:Any,<:Any})
println("running_branch")
# create lat/lon channels from x/y channels
for i in keys(plot_layer["layer"])
if haskey(plot_layer["layer"][i],"layer")
Expand Down
4 changes: 2 additions & 2 deletions src/layouts/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ components is returned.
function layout_network(case::Dict{String,<:Any};
fixed = false,
layout_algorithm = kamada_kawai,
node_types::Array{String,1}=["bus","gen","storage","load"],
edge_types::Array{String,1}=["switch","branch","dcline","transformer"],
node_types::Array{String,1}=supported_node_types,
edge_types::Array{String,1}=supported_edge_types,
connector_weight=0.5,
edge_weight=1.0,
kwargs...
Expand Down
26 changes: 19 additions & 7 deletions src/plots/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Create a plower plot. Check github repo for documentation on kwarg options.
function powerplot(
case::Dict{String,<:Any};
layout_algorithm=kamada_kawai,
components=supported_component_types,
fixed=false,
invalid_keys = Dict("branch" => ["mu_angmin", "mu_angmax", "mu_sf", "shift", "rate_b", "rate_c", "g_to", "g_fr", "mu_st", "source_id", "f_bus", "t_bus", "qf", "angmin", "angmax", "qt", "tap"],#["b_fr","b_to", "xcoord_1", "xcoord_2", "ycoord_1", "ycoord_2", "pf", "src","dst","rate_a","br_r","br_x","index","br_status"],
"bus" => ["mu_vmax", "lam_q", "mu_vmin", "source_id","lam_p"],#["xcoord_1", "ycoord_1", "bus_type", "name", "vmax", "vmin", "index", "va", "vm", "base_kv"],
Expand All @@ -24,15 +25,26 @@ function powerplot(
return _powerplot_mn(case; layout_algorithm=layout_algorithm, fixed=fixed, invalid_keys=invalid_keys, kwargs...)
end

# copy data for modification by plots
data = deepcopy(case)

# remove components that are not to be plotted
for component_type in supported_component_types
if !(component_type in components) && haskey(data, component_type)
delete!(data, component_type)
end
end


# modify case dictionary for distribution grid data
if haskey(case, "is_kron_reduced")
case = distr_data(case)
if haskey(data, "is_kron_reduced")
data = distr_data(data)
end

@prepare_plot_attributes(kwargs) # creates the plot_attributes dictionary
_validate_plot_attributes!(plot_attributes) # check the attributes for valid input types

data = layout_network(case; layout_algorithm=layout_algorithm, fixed=fixed, kwargs...)
data = layout_network(data; layout_algorithm=layout_algorithm, fixed=fixed, kwargs...)

# fix parallel branch coordinates
offset_parallel_edges!(data,plot_attributes[:parallel_edge_offset])
Expand All @@ -42,7 +54,7 @@ function powerplot(

# validate data-related attributes
_validate_data_type(plot_attributes, :gen_data_type)
_validate_data(PMD.gen, plot_attributes[:gen_data], "generator")
_validate_data(PMD.gen, plot_attributes[:gen_data], "gen")
_validate_data_type(plot_attributes, :bus_data_type)
_validate_data(PMD.bus, plot_attributes[:bus_data], "bus")
_validate_data_type(plot_attributes, :branch_data_type)
Expand Down Expand Up @@ -116,7 +128,7 @@ function powerplot!(plt_layer::VegaLite.VLSpec, case::Dict{String,<:Any};

# validate data-related attributes
_validate_data_type(plot_attributes, :gen_data_type)
_validate_data(PMD.gen, plot_attributes[:gen_data], "generator")
_validate_data(PMD.gen, plot_attributes[:gen_data], "gen")
_validate_data_type(plot_attributes, :bus_data_type)
_validate_data(PMD.bus, plot_attributes[:bus_data], "bus")
_validate_data_type(plot_attributes, :branch_data_type)
Expand Down Expand Up @@ -184,7 +196,7 @@ function _powerplot_mn(case::Dict{String,<:Any};

# validate data-related attributes
PowerPlots._validate_data_type(plot_attributes, :gen_data_type)
PowerPlots._validate_data(PMD.gen, plot_attributes[:gen_data], "generator")
PowerPlots._validate_data(PMD.gen, plot_attributes[:gen_data], "gen")
PowerPlots._validate_data_type(plot_attributes, :bus_data_type)
PowerPlots._validate_data(PMD.bus, plot_attributes[:bus_data], "bus")
PowerPlots._validate_data_type(plot_attributes, :branch_data_type)
Expand Down Expand Up @@ -251,7 +263,7 @@ function _powerplot_mn!(plt_layer::VegaLite.VLSpec, case::Dict{String,<:Any};

# validate data-related attributes
PowerPlots._validate_data_type(plot_attributes, :gen_data_type)
PowerPlots._validate_data(PMD.gen, plot_attributes[:gen_data], "generator")
PowerPlots._validate_data(PMD.gen, plot_attributes[:gen_data], "gen")
PowerPlots._validate_data_type(plot_attributes, :bus_data_type)
PowerPlots._validate_data(PMD.bus, plot_attributes[:bus_data], "bus")
PowerPlots._validate_data_type(plot_attributes, :branch_data_type)
Expand Down
28 changes: 28 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ data = PowerModels.parse_file("$(joinpath(dirname(pathof(PowerModels)), ".."))/t
end
end

@testset "filter plot components" begin
case = PowerModels.parse_file("$(joinpath(dirname(pathof(PowerModels)), ".."))/test/data/matpower/case5.m")
p=powerplot(case, components=["bus","branch"])
@test length(keys(p.layer))==2

end

@testset "Experimental" begin
using PowerPlots.Experimental
Expand Down Expand Up @@ -204,6 +210,28 @@ data = PowerModels.parse_file("$(joinpath(dirname(pathof(PowerModels)), ".."))/t
end


@testset "Verify new components are fully supported" begin

# set of nodes and edges is equivalent to all supported components
@test Set(union(supported_node_types,supported_edge_types))==Set(supported_component_types)

for comp_type in supported_component_types
Memento.info(PowerPlots._LOGGER, "checking support for: $comp_type")

# check that all components have a plot function
@test isdefined(PowerPlots, Symbol("plot_$comp_type"))

# check that all components have kwargs
@test haskey(default_plot_attributes, Symbol("$(comp_type)_size"))
@test haskey(default_plot_attributes, Symbol("$(comp_type)_color"))
# skip connector
if comp_type != "connector"
@test haskey(default_plot_attributes, Symbol("$(comp_type)_data"))
@test haskey(default_plot_attributes, Symbol("$(comp_type)_data_type"))
end
end
end

end

PowerModels.logger_config!(prev_level); # reset PowerModels logger to previous level
Expand Down

0 comments on commit 7af6401

Please sign in to comment.