-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstructures_utils.jl
More file actions
336 lines (283 loc) · 10.3 KB
/
structures_utils.jl
File metadata and controls
336 lines (283 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
"""
function installed()
Get a list of installed packages (from the depricated Pkg.installed()).
"""
function installed()
deps = Pkg.dependencies()
installs = Dict{String,VersionNumber}()
for (uuid, dep) ∈ deps
dep.is_direct_dep || continue
dep.version === nothing && continue
installs[dep.name] = dep.version::VersionNumber
end
return installs
end
"""
function loaded()
Get a list of loaded packages.
"""
loaded() = [String(n) for n ∈ names(Main, imported = true) if getfield(Main, n) isa Module]
"""
place_nodes_in_circle(total_nodes::Int64, current_node::Int64, r::Float32, xₒ::Float32, yₒ::Float32)
Return coordinate for point number `i` of a total of `n` points evenly distributed around
a circle of radius `r` centered at (xₒ, yₒ) from -π/4 to 5π/4.
"""
function place_nodes_in_circle(n::Int64, i::Int64, r::Float32, xₒ::Float32, yₒ::Float32)
θ::Float32 = n == 1 ? π : -π / 4 + 3π / 2 * (1 - (i - 1) / (n - 1))
x::Float32 = xₒ + r * cos(θ)
y::Float32 = yₒ + r * sin(θ)
return x, y
end
"""
set_colors(products::Vector{<:Resource}, id_to_color_map::Dict)
Returns a dictionary that completes the dictionary `id_to_color_map` with default color values
for standard names (like Power, NG, Coal, CO2) collected from `src/colors.yml`.
Color can be represented as a hex (*i.e.*, #a4220b2) or a symbol (*i.e.*, :green), but also a
string of the identifier for default colors in the `src/colors.yml` file.
"""
function set_colors(products::Vector{<:Resource}, id_to_color_map::Dict)
complete_id_to_color_map::Dict = Dict()
# Get the default colors
default_colors::Dict = get_default_colors()
# Add default colors to the complete_id_to_color_map
for product ∈ products
if haskey(default_colors, product.id)
complete_id_to_color_map[product.id] = default_colors[product.id]
end
end
# Add the colors from the id_to_color_map (also overwrites default colors)
for (key, val) ∈ id_to_color_map
complete_id_to_color_map[string(key)] = val
end
# Add missing colors and make these most distinguishable to the existing ones
# Find the resources that are missing colors
missing_product_colors::Vector{Resource} = filter(
product -> !haskey(complete_id_to_color_map, product.id), products,
)
# Create a seed based on the existing colors
seed::Vector{RGB} = [
parse(Colorant, hex_color) for hex_color ∈ values(complete_id_to_color_map)
]
# Add non-desired colors to the seed
foul_colors = [
"#FFFF00", # Yellow
"#FF00FF", # Magenta
"#00FFFF", # Cyan
"#00FF00", # Green
"#000000", # Black
"#FFFFFF", # White
]
for color ∈ values(foul_colors)
push!(seed, parse(Colorant, color))
end
# Create new colors for the missing resources
products_colors::Vector{RGB} = distinguishable_colors(
length(missing_product_colors), seed; dropseed = true,
)
# Set the new colors for the missing resources
for (product, color) ∈ zip(missing_product_colors, products_colors)
complete_id_to_color_map[product.id] = color
end
return complete_id_to_color_map
end
"""
get_default_colors()
Get the default colors in the EnergyModelsGUI repository at `src/colors.yml`.
"""
function get_default_colors()
return YAML.load_file(joinpath(@__DIR__, "..", "colors.yml"))
end
"""
set_icons(id_to_icon_map::Dict)
Return a dictionary `id_to_icon_map` with id from nodes and icon paths based on provided
paths (or name of .png icon file which will be found in the icons folder of any of the
EMX packages).
The icon images are assumed to be in .png format, and the strings should not contain this
file ending.
"""
function set_icons(id_to_icon_map::Dict)
if isempty(id_to_icon_map)
return id_to_icon_map
end
for (key, val) ∈ id_to_icon_map
id_to_icon_map[key] = find_icon_path(val)
end
return id_to_icon_map
end
"""
function find_icon_path(icon::String)
Search for path to icon based on icon name `icon`.
"""
function find_icon_path(icon::String)
icon_path = "" # in case not found
if isfile(icon)
icon_path = icon * ".png"
elseif isfile(joinpath(@__DIR__, "..", "..", "icons", icon * ".png"))
icon_path = joinpath(@__DIR__, "..", "..", "icons", icon * ".png")
else
# Get a dictionary of installed packages
installed_packages = installed()
# Filter packages with names matching the pattern "EnergyModels*"
emx_packages = filter(
pkg -> occursin(r"EnergyModels", pkg), keys(installed_packages),
)
# Search through EMX packages if icons are available there
for package ∈ emx_packages
package_path::Union{String,Nothing} = Base.find_package(package)
if !isnothing(package_path)
icons_file::String =
joinpath(package_path, "ext", "EMGUIExt", "icons", icon) * ".png"
if isfile(icons_file)
icon_path = icons_file
break
end
end
end
end
return icon_path
end
"""
design_file(system::AbstractSystem, path::String)
Construct the path for the .yml file for `system` in the folder `path`.
"""
function design_file(system::AbstractSystem, path::String)
if isempty(path)
return ""
end
return joinpath(path, "$(get_parent(system)).yml")
end
"""
find_icon(system::AbstractSystem, id_to_icon_map::Dict)
Find the icon associated with a given `system`'s node id utilizing the mapping provided
through `id_to_icon_map`.
"""
function find_icon(system::AbstractSystem, id_to_icon_map::Dict)
icon::String = ""
if !isempty(id_to_icon_map)
supertype::DataType = find_type_field(id_to_icon_map, get_parent(system))
if haskey(id_to_icon_map, get_parent(system).id)
icon = id_to_icon_map[get_parent(system).id]
elseif supertype != Nothing
icon = id_to_icon_map[supertype]
else
@warn("Could not find $(get_parent(system).id) in id_to_icon_map \
nor the type $(typeof(get_parent(system))). Using default setup instead")
end
end
return icon
end
"""
save_design(design::EnergySystemDesign)
Save the x,y-coordinates of EnergySystemDesign `design` to a .yml file specifield in the
field `file` of `design`.
"""
function save_design(design::EnergySystemDesign)
if isempty(design.file)
@error "Path not specified for saving; use GUI(case; design_path)"
return nothing
end
design_dict::Dict = Dict()
for component ∈ get_components(design)
# Extract x,y-coordinates
x, y = component.xy[]
design_dict[string(get_parent(get_system(component)))] = Dict(
:x => round(x; digits = 5), :y => round(y; digits = 5),
)
# Also save the coordinates from sub designs
if !isempty(get_components(component))
save_design(component)
end
end
@info "Saving design coordinates to file $(design.file)"
return save_design(design_dict, design.file)
end
"""
save_design(design::EnergySystemDesign, file::String)
Save the x,y-coordinates of `design_dict` to a .yml file at location and filename given by
`file`.
"""
function save_design(design_dict::Dict, file::String)
design_dir = dirname(file)
if !isdir(design_dir)
mkpath(design_dir)
end
return YAML.write_file(file, design_dict)
end
"""
get_linked_nodes!(
node::EMB.Node,
links::Vector{Link},
area_links::Vector{Link},
area_nodes::Vector{EMB.Node},
indices::Vector{Int},
)
Recursively find all nodes connected (directly or indirectly) to `node` in a system of `links`
and store the found links in `area_links` and nodes in `area_nodes`.
Here, `indices` contains the indices where the next link and node is to be stored,
respectively.
"""
function get_linked_nodes!(
node::EMB.Node,
links::Vector{Link},
area_links::Vector{Link},
area_nodes::Vector{EMB.Node},
indices::Vector{Int},
)
for link ∈ links
if node ∈ [link.from, link.to] &&
(indices[1] == 1 || !(link ∈ area_links[1:(indices[1]-1)]))
area_links[indices[1]] = link
indices[1] += 1
new_node_added::Bool = false
if node == link.from && !(link.to ∈ area_nodes[1:(indices[2]-1)])
area_nodes[indices[2]] = link.to
new_node_added = true
elseif node == link.to && !(link.from ∈ area_nodes[1:(indices[2]-1)])
area_nodes[indices[2]] = link.from
new_node_added = true
end
# Recursively add other nodes
if new_node_added
indices[2] += 1
get_linked_nodes!(
area_nodes[indices[2]-1],
links,
area_links,
area_nodes,
indices,
)
end
end
end
end
"""
get_resource_colors(resources::Vector{Resource}, id_to_color_map::Dict{Any,Any})
Get the colors linked the the resources in `resources` based on the mapping `id_to_color_map`.
"""
function get_resource_colors(resources::Vector{<:Resource}, id_to_color_map::Dict{Any,Any})
hexColors::Vector{Any} = [id_to_color_map[resource.id] for resource ∈ resources]
return [parse(Colorant, hex_color) for hex_color ∈ hexColors]
end
"""
get_resource_colors(l::Vector{Link}, id_to_color_map::Dict{Any,Any})
Get the colors linked to the resources in the link `l` based on the mapping `id_to_color_map`.
"""
function get_resource_colors(l::Link, id_to_color_map::Dict{Any,Any})
resources::Vector{Resource} = EMB.link_res(l)
return get_resource_colors(resources, id_to_color_map)
end
"""
get_resource_colors(::Vector{Any}, ::Dict{Any,Any})
Return empty RGB vector for empty input.
"""
function get_resource_colors(::Vector{Any}, ::Dict{Any,Any})
return Vector{RGB}(undef, 0)
end
"""
getfirst(f::Function, a::Vector)
Return the first element of Vector `a` satisfying the requirement of Function `f`.
"""
function getfirst(f::Function, a::Vector)
index = findfirst(f, a)
return isnothing(index) ? nothing : a[index]
end