Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
VegaLite = "112f6efa-9a02-5b7d-90c0-432ed331239a"

[compat]
PowerModels = "^0.19.2"
PowerModelsDistribution = "^0.14.5, ^0.15"
PowerModels = "^0.19.2, ^0.20, ^0.21"
PowerModelsDistribution = "^0.14.5, ^0.15, ^0.16"
8 changes: 6 additions & 2 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Documenter, PowerPlots, VegaLite, UUIDs, PowerModels, PowerModelsDistribution

function Base.show(io::IO, m::MIME"text/html", v::VegaLite.VLSpec)
divid = string("vl", replace(string(uuid4()), "-"=>""))
divid = string("vl", replace(string(uuid4()), "-" => ""))
print(io, "<div id='$divid' style=\"width:100%;height:100%;\"></div>")
print(io, "<script type='text/javascript'>requirejs.config({paths:{'vg-embed': 'https://cdn.jsdelivr.net/npm/vega-embed@6?noext','vega-lib': 'https://cdn.jsdelivr.net/npm/vega-lib?noext','vega-lite': 'https://cdn.jsdelivr.net/npm/vega-lite@4?noext','vega': 'https://cdn.jsdelivr.net/npm/vega@5?noext'}}); require(['vg-embed'],function(vegaEmbed){vegaEmbed('#$divid',")
VegaLite.our_json_print(io, v)
Expand All @@ -11,7 +11,11 @@ end
makedocs(
warnonly = true,
modules = [PowerPlots],
format = Documenter.HTML(mathengine = Documenter.MathJax()),
format = Documenter.HTML(mathengine = Documenter.MathJax(),
size_threshold_warn = 2^20,
size_threshold = nothing,
example_size_threshold = nothing,
),
sitename = "PowerPlots",
authors = "Noah Rhodes",
pages = [
Expand Down
108 changes: 59 additions & 49 deletions src/core/attribute_validation.jl
Original file line number Diff line number Diff line change
@@ -1,67 +1,77 @@

"Validates the given plot_attributes according to their type"
function _validate_plot_attributes!(plot_attributes::Dict{Symbol, Any})
for attr in keys(plot_attributes)
if !haskey(default_plot_attributes, attr)
Memento.warn(_LOGGER, "Ignoring unexpected attribute $(repr(attr))")
end
end
for (k,v) in plot_attributes
_validate_plot_attributes!(plot_attributes, k,v)
end
end

# validate color attributes
for attr in _color_attributes
color = plot_attributes[attr]
if !(typeof(color) <: Union{String, Symbol, AbstractVector})
Memento.warn(_LOGGER, "Color value for $(repr(attr)) should be given as symbol or string")
else
try
if typeof(color) <: AbstractVector
parse.(Colors.Colorant, color) # parses all colors as CSS color
else
parse(Colors.Colorant, color) # try to parse the color as a CSS color
plot_attributes[attr] = [color] # package color into an array
end
catch e
Memento.warn(_LOGGER, "Invalid color $(repr(color)) given for $(repr(attr))")
end
end
end
"Validates the given plot_attributes according to their type"
function _validate_plot_attributes!(plot_attributes::Dict{Symbol,Any}, k::Symbol, v::Dict{Symbol,Any})
for (k1,v1) in v
_validate_plot_attributes!(v, k1,v1)
end
end

# validate numeric attributes
for attr in _numeric_attributes
value = plot_attributes[attr]
if !(typeof(value) <: Union{Number, String})
Memento.warn(_LOGGER, "Value for $(repr(attr)) should be given as a number or numeric String")
elseif typeof(value) <: String
try
parse(Float64, value)
catch e
Memento.warn(_LOGGER, "Invalid number $(repr(value)) given for $(repr(attr))")
end
end
end
"Validates the given plot_attributes according to their type"
function _validate_plot_attributes!(plot_attributes::Dict{Symbol,Any}, attr::Symbol, v::Any)
if attr in _color_attributes
_validate_color_attribute!(plot_attributes, attr, v)
elseif attr in _numeric_attributes
_validate_numeric_attribute!(plot_attributes, attr, v)
elseif attr in _label_attributes
_validate_label_attribute!(plot_attributes, attr, v)
elseif attr in _boolean_attributes
_validate_boolean_attribute!(plot_attributes, attr, v)
end
end

# validate data label attributes
for attr in _label_attributes
value = plot_attributes[attr]
if !(typeof(value) <: Union{String, Symbol})
Memento.warn(_LOGGER, "Value for $(repr(attr)) should be given as a String or Symbol")
function _validate_color_attribute!(plot_attributes::Dict{Symbol,Any}, attr::Symbol, v::Any)
if !(typeof(v) <: Union{String, Symbol, AbstractVector})
Memento.warn(_LOGGER, "Color value for $(repr(attr)) should be given as symbol or string")
else
try
if typeof(v) <: AbstractVector
parse.(Colors.Colorant, v) # parses all colors as CSS color
else
parse(Colors.Colorant, v) # try to parse the color as a CSS color
plot_attributes[attr] = [v] # package color into an array
end
catch e
Memento.warn(_LOGGER, "Invalid color $(repr(v)) given for $(repr(attr))")
end
end
end

# validate boolean attributes
for attr in _boolean_attributes
value = plot_attributes[attr]
if !(typeof(value) <: Bool)
Memento.warn(_LOGGER, "Value for $(repr(attr)) should be given as a Bool")
end
function _validate_numeric_attribute!(plot_attributes::Dict{Symbol,Any}, attr::Symbol, v::Any)
if !(typeof(v) <: Union{Number, String})
Memento.warn(_LOGGER, "Value for $(repr(attr)) should be given as a number or numeric String")
elseif typeof(v) <: String
try
parse(Float64, v)
catch e
Memento.warn(_LOGGER, "Invalid number $(repr(v)) given for $(repr(attr))")
end
end
end

function _validate_label_attribute!(plot_attributes::Dict{Symbol,Any}, attr::Symbol, v::Any)
if !(typeof(v) <: Union{String, Symbol})
Memento.warn(_LOGGER, "Value for $(repr(attr)) should be given as a String or Symbol")
end
end

function _validate_boolean_attribute!(plot_attributes::Dict{Symbol,Any}, attr::Symbol, v::Any)
if !(typeof(v) <: Bool)
Memento.warn(_LOGGER, "Value for $(repr(attr)) should be given as a Bool")
end
end

"Checks that the given column plot_attributes[data_attr] exists in the data"
function _validate_data(data::DataFrames.DataFrame, data_column::Any, data_name::String)
function _validate_data(data::DataFrames.DataFrame, data_column::Any, data_name::Symbol)
if !(typeof(data_column) <: Union{String, Symbol})
return
Memento.warn(_LOGGER, "Value for $(repr(attr)) should be given as a String or Symbol")
return
end
if !(data_column in names(data) || data_column in propertynames(data))
Memento.warn(_LOGGER, "Data column $(repr(data_column)) does not exist for $(data_name)")
Expand Down
11 changes: 6 additions & 5 deletions src/core/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function get_parallel_edges(data, edge_types=supported_edge_types)
edge_pairs = Dict()
for edge_type in edge_types # supported edge_types
for (id,edge) in get(data, edge_type, Dict())
for (id,edge) in get(data, string(edge_type), Dict())
bus_pair = (min(edge["f_bus"],edge["t_bus"]), max(edge["f_bus"],edge["t_bus"])) # get unique direction
if !haskey(edge_pairs, bus_pair)
edge_pairs[bus_pair] = []
Expand All @@ -22,6 +22,7 @@ end

"Add x/y coords for all any parallel branches, and offset the endpoints so each branch is visible"
function offset_parallel_edges!(data,offset; edge_types=supported_edge_types)
get_parallel_edges(data, edge_types)
for (bus_pair, edges) in get_parallel_edges(data, edge_types)
n_edges = length(edges)
xcoord_1 = data["bus"]["$(bus_pair[1])"]["xcoord_1"]
Expand All @@ -37,10 +38,10 @@ function offset_parallel_edges!(data,offset; edge_types=supported_edge_types)

for i in eachindex(edges)
(edge_type, edge_id) = edges[i]
data[edge_type][edge_id]["ycoord_1"] = ycoord_1 + offset_range[i]*normal_direction[2]
data[edge_type][edge_id]["ycoord_2"] = ycoord_2 + offset_range[i]*normal_direction[2]
data[edge_type][edge_id]["xcoord_1"] = xcoord_1 + offset_range[i]*normal_direction[1]
data[edge_type][edge_id]["xcoord_2"] = xcoord_2 + offset_range[i]*normal_direction[1]
data[string(edge_type)][edge_id]["ycoord_1"] = ycoord_1 + offset_range[i]*normal_direction[2]
data[string(edge_type)][edge_id]["ycoord_2"] = ycoord_2 + offset_range[i]*normal_direction[2]
data[string(edge_type)][edge_id]["xcoord_1"] = xcoord_1 + offset_range[i]*normal_direction[1]
data[string(edge_type)][edge_id]["xcoord_2"] = xcoord_2 + offset_range[i]*normal_direction[1]
end
end
return data
Expand Down
136 changes: 29 additions & 107 deletions src/core/options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,132 +10,54 @@ const color_schemes=Dict{Symbol,Any}(
:yellows => colorscheme2array(ColorSchemes.ColorScheme(range(Colors.colorant"#8C6D31", Colors.colorant"#E7CB94", length=5))),
)


# Default plot attributes
default_plot_attributes = Dict{Symbol, Any}(
:gen_color => color_schemes[:oranges],
:bus_color => color_schemes[:greens],
:load_color => color_schemes[:reds],
:branch_color => color_schemes[:blues],
:switch_color => color_schemes[:grays],
:transformer_color => color_schemes[:yellows],
:connector_color => [:gray],
:dcline_color => color_schemes[:purples],
:storage_color => color_schemes[:oranges],
:flow_color => :black,
:gen_size => 2e2,
:bus_size => 5e2,
:load_size => 2e2,
:branch_size => 5,
:switch_size => 5,
:transformer_size => 5,
:connector_size => 3,
:dcline_size => 5,
:storage_size => 2e2,
:width => 500,
:height => 500,
:gen_data => "ComponentType",
:bus_data => "ComponentType",
:load_data => "ComponentType",
:branch_data => "ComponentType",
:switch_data => "ComponentType",
:transformer_data => "ComponentType",
:connector_data => "ComponentType",
:dcline_data => "ComponentType",
:storage_data => "ComponentType",
:gen_data_type => "nominal",
:bus_data_type => "nominal",
:load_data_type => "nominal",
:branch_data_type => "nominal",
:switch_data_type => "nominal",
:transformer_data_type => "nominal",
:dcline_data_type => "nominal",
:storage_data_type => "nominal",
:parallel_edge_offset=>0.05,
)

default_edge_attributes = Dict{Symbol, Any}(
:color => color_schemes[:blues],
:size => 5,
:data => "ComponentType",
:data_type => "nominal",
:flow_color => :black,
:flow_arrow_size_range=>[500,3000],
:show_flow => false,
:show_flow_legend => false,
:flow_arrow_size_range=>[500,3000],
:parallel_edge_offset => 0.05,
:connector_dash=>[4,4],
);
)

# Returns a deepcopy of the default_plot_attributes, used to initialize plot_attributes in utils.jl
function copy_default_attributes()
return deepcopy(default_plot_attributes);
end
default_connector_attributes = Dict{Symbol, Any}(
:color => [:gray],
:size => 3,
:dash => [4, 4],
:data => "ComponentType",
:data_type => "nominal",
)

# Dictionary of aliases of properties
# Aliases are replaced by their key in plot_attributes
# for example, nodecolor/markercolor/marker_color, edgecolor/edge_color/ec can be used to
# set nodecolor and edgecolor respectively
const attribute_aliases = Dict(
:gen_color => [:gencolor, :generatorcolor, :generator_color, :node_color],
:bus_color => [:buscolor, :substationcolor, :substation_color, :node_color],
:load_color => [:loadcolor, :demandcolor, :demand_color, :node_color],
:branch_color => [:branchcolor, :transmissionlinecolor, :transmissionline_color,:transmission_line_color, :edge_color],
:switch_color => [:switchcolor, :edge_color],
:transformer_color => [:transformercolor, :edge_color],
:connector_color => [:connectorcolor, :edge_color],
:storage_color => [:storagecolor, :batterycolor, :battery_color, :node_color],
:dcline_color => [:dclinecolor, :dc_line_color, :edge_color],
:bus_size => [:bussize, :node_size],
:gen_size => [:gensize, :node_size],
:load_size => [:loadsize, :node_size],
:storage_size => [:storagesize, :node_size],
:branch_size => [:branchsize, :line_size, :edge_size],
:switch_size => [:switchsize, :edge_size],
:transformer_size => [:transformersize, :edge_size],
:dcline_size => [:dclinesize, :line_size, :edge_size],
:connector_size => [:connectorsize, :edge_size],
:flow_arrow_size_range =>[:flow_size, :flowsize, :arrow_size, :arrowsize],
:flow_color => [:flowcolor, :arrow_color, :arrowcolor],
:show_flow => [:flow, :showflow, :arrows, :show_arrows, :showarrows, :flows, :show_flows, :showflows],
:show_flow_legend => [:flowlegend, :flow_legend, :arrowlegend, :arrow_legend, :show_arrow_legend],
default_node_attributes = Dict{Symbol, Any}(
:color => color_schemes[:greens],
:size => 5e2,
:data => "ComponentType",
:data_type => "nominal",
)


const _color_attributes = [ # color (String or Symbol) type parameters
:gen_color,
:bus_color,
:load_color,
:branch_color,
:connector_color,
:switch_color,
:transformer_color,
:storage_color,
:dcline_color,
:flow_color
:color,
]
const _numeric_attributes = [ # numeric parameters
:gen_size,
:bus_size,
:load_size,
:branch_size,
:connector_size,
:switch_size,
:transformer_size,
:dcline_size,
:storage_size,
:size,
:width,
:height,
:parallel_edge_offset,
]
const _label_attributes = [ # label (String or Symbol) type parameters
:gen_data,
:bus_data,
:load_data,
:branch_data,
:dcline_data,
:switch_data,
:transformer_data,
:storage_data,
:gen_data_type,
:bus_data_type,
:branch_data_type,
:switch_data_type,
:transformer_data_type,
:dcline_data_type,
:storage_data_type
:data,
:data_type
]
const _boolean_attributes = [ # boolean type parameters
:show_flow,
:show_flow_legend,
]

Loading
Loading