Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial tests with a Makie extension #47

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"

[weakdeps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[extensions]
TimeStructDataFramesExt = "DataFrames"
TimeStructMakieExt = "Makie"
TimeStructUnitfulExt = "Unitful"

[compat]
DataFrames = "1"
Dates = "1"
Makie = "0.21"
TimeZones = "1"
Unitful = "1"
julia = "^1.9"
39 changes: 39 additions & 0 deletions ext/TimeStructMakieExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module TimeStructMakieExt
using Makie
using TimeStruct
import TimeStruct: profilechart, profilechart!

function Makie.convert_arguments(P::PointBased, periods, profile::TimeProfile)
pts = [Point2(start_oper_time(t, periods), profile[t]) for t in periods]
l = last(periods)
push!(pts, Point2(end_oper_time(l, periods), profile[l]))
return (pts,)
end

@recipe(ProfileChart) do scene
return Attributes(type = :stairs)
end

function Makie.plot!(sc::ProfileChart{<:Tuple{<:TimeStructure,<:TimeProfile}})
periods = sc[1]
profile = sc[2]

for opscen in opscenarios(periods[])
stairs!(sc, opscen, profile[]; step = :post)
end

return sc
end

function Makie.plot(periods::TwoLevel, profile::TimeProfile)
fig = Figure()
for (i, sp) in enumerate(strat_periods(periods))
fig[1, i] = Axis(fig, title = "sp = $sp")
for opscen in opscenarios(sp)
stairs!(fig[1, i], opscen, profile; step = :post)
end
end
return fig
end

end
4 changes: 3 additions & 1 deletion src/TimeStruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ include("op_scenarios/rep_periods.jl")
include("op_scenarios/strat_periods.jl")
include("op_scenarios/tree_periods.jl")

include("utils.jl")
include("discount.jl")
include("profiles.jl")
include("utils.jl")

export TimeStructure
export SimpleTimes
Expand Down Expand Up @@ -58,4 +58,6 @@ export Discounter
export discount
export objective_weight

export profilechart, profilechart!

end # module
90 changes: 90 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ function end_oper_time(t::TimePeriod, ts::OperationalScenarios)
return end_oper_time(t, ts.scenarios[_opscen(t)])
end

function end_oper_time(t::TimePeriod, opscen::AbstractOperationalScenario)
return end_oper_time(t, opscen.operational)
end

function end_oper_time(t::TimePeriod, ts::RepresentativePeriods)
return end_oper_time(t, ts.rep_periods[_rper(t)])
end

function end_oper_time(t::TimePeriod, ts::TwoLevel)
return end_oper_time(t, ts.operational[_strat_per(t)])
end
Expand Down Expand Up @@ -155,3 +163,85 @@ function Base.iterate(ts::TreeStructure, state = nothing)

return TreePeriod(ts, next[1]), next[2]
end

function profilechart end
function profilechart! end

_scen(sc::AbstractOperationalScenario) = "sc-$(TimeStruct._opscen(sc))"
_repr(rp::AbstractRepresentativePeriod) = "rp-$(TimeStruct._rper(rp))"
_strat(sp::AbstractStrategicPeriod) = "sp-$(TimeStruct._strat_per(sp))"

function rowtable(profile::TimeProfile, periods::SimpleTimes; include_end = true)
rowtable = []
for t in periods
push!(rowtable, (t = start_oper_time(t, periods), value = profile[t]))
end
if include_end
last_period = last(periods)
push!(
rowtable,
(t = end_oper_time(last_period, periods), value = profile[last_period]),
)
end
return rowtable
end

function rowtable(profile::TimeProfile, periods::OperationalScenarios; include_end = true)
rowtable = []
for sc in opscenarios(periods)
for t in sc
push!(
rowtable,
(opscen = _scen(sc), t = start_oper_time(t, periods), value = profile[t]),
)
end
if include_end
last_period = last(sc)
push!(
rowtable,
(
opscen = _scen(sc),
t = end_oper_time(last_period, periods),
value = profile[last_period],
),
)
end
end
return rowtable
end

function rowtable(profile::TimeProfile, periods::TwoLevel; include_end = true)
rowtable = []
for sp in strat_periods(periods)
for rp in repr_periods(sp)
for sc in opscenarios(rp)
for t in sc
push!(
rowtable,
(
strat = _strat(sp),
repr = _repr(rp),
opscen = _scen(sc),
t = start_oper_time(t, periods),
value = profile[t],
),
)
end
if include_end
last_period = last(sc)
push!(
rowtable,
(
strat = _strat(sp),
repr = _repr(rp),
opscen = _scen(sc),
t = end_oper_time(last_period, periods),
value = profile[last_period],
),
)
end
end
end
end
return rowtable
end
68 changes: 68 additions & 0 deletions test/test_makie.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using CairoMakie
using TimeStruct
using AlgebraOfGraphics

periods = SimpleTimes([1, 2, 4, 2, 3])
profile = OperationalProfile([2.0, 3.4, 3.5, 1.2, 0.6])

stairs(periods, profile; step = :post)
scatter!(periods, profile)
current_figure()

lines(periods, profile)

scens = OperationalScenarios(3, periods)
scen_prof = ScenarioProfile([profile, 0.8 * profile, 1.2 * profile])
profilechart(scens, scen_prof)

repr = RepresentativePeriods(2, [0.7, 0.3], [scens, scens])

two_level = TwoLevel(3, repr)
plot(two_level, scen_prof)

# Testing with AlgebraOfGraphics and rowtables
set_aog_theme!()
axis = (width = 225, height = 225)

tab = TimeStruct.rowtable(profile, periods)

plt = data(tab) * mapping(:t, :value) * visual(Stairs, step = :post)
draw(plt; axis = axis)

sc_tab = TimeStruct.rowtable(scen_prof, scens)
plt = data(sc_tab) * mapping(:t, :value, color = :opscen) * visual(Stairs, step = :post)
draw(plt; axis = axis)

plt = data(sc_tab) * mapping(:t, :value, col = :opscen) * visual(Stairs, step = :post)
draw(plt; axis = axis)

two_tab = TimeStruct.rowtable(scen_prof, two_level)
plt =
data(two_tab) *
mapping(:t, :value, row = :repr, col = :strat, color = :opscen) *
visual(Stairs, step = :post)
draw(plt; axis = axis)

plt = data(two_tab) * mapping(:t, :value, layout = :strat) * visual(Stairs, step = :post)
draw(plt; axis = axis)

using JuMP, HiGHS

m = Model()
@variable(m, x[periods] >= 0)
@constraint(m, [t in periods], x[t] == profile[t])
@objective(m, Min, 0)

set_optimizer(m, HiGHS.Optimizer)
optimize!(m)

tab = Containers.rowtable(value, x)

set_aog_theme!()
axis = (width = 225, height = 225)

plt = data(tab) * mapping(:x1 => (t -> start_oper_time(t, periods)), :y)
draw(plt; axis = axis)

plt = data(tab) * mapping(:x1, :y)
draw(plt; axis = axis)
Loading