Skip to content

Commit

Permalink
add visualizations code
Browse files Browse the repository at this point in the history
since I anyways use it to test stuff
  • Loading branch information
Datseris committed Dec 17, 2023
1 parent 4bb2edf commit 39239a3
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
TimeseriesSurrogates = "c804724b-8c18-5caa-8579-6025a0767c70"

[weakdeps]
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"

[extensions]
TITVisualizations = "Makie"

[compat]
DelimitedFiles = "1"
Downloads = "1"
FFTW = "^1.6"
HypothesisTests = "0.11"
InteractiveUtils = "1"
LinearAlgebra = "1"
Makie = "≥ 0.19"
Random = "1"
Reexport = "1.2"
Statistics = "1"
Expand Down
69 changes: 69 additions & 0 deletions ext/TITVisualizations.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module TITVisualizations

using TransitionsInTimeseries, Makie

function TransitionsInTimeseries.plot_indicator_changes(res::SlidingWindowResults,
colors = ["#7143E0", "#0A9A84", "#191E44", "#AF9327", "#701B80", "#2E6137",],
skip = Int[],
)
config = res.config
fig = Figure()
axts = Axis(fig[1,1]; ylabel = "input")
if isnothing(config.indicators)
axcha = Axis(fig[2,1]; ylabel = "changes")
linkxaxes!(axts, axcha)
else
axind = Axis(fig[2,1]; ylabel = "indicators")
axcha = Axis(fig[3,1]; ylabel = "changes", xlabel = "time")
linkxaxes!(axts, axind, axcha)
hidexdecorations!(axind; grid = false)
end
hidexdecorations!(axts; grid = false)

lines!(axts, res.t, res.x; color = "black", linewidth = 3)
for (i, cha) in enumerate(config.change_metrics)
i skip && continue
if !isnothing(config.indicators)
lines!(axind, res.t_indicator, res.x_indicator[:, i];
color = colors[i], linewidth = 3, label = string(nameof(config.indicators[i]))
)
end
lines!(axcha, res.t_change, res.x_change[:, i];
color = colors[i], linewidth = 3, label = string(nameof(cha))
)
end

!isnothing(config.indicators) && axislegend(axind)
axislegend(axcha)
return fig
end

function TransitionsInTimeseries.plot_significance!(fig::Figure, res::SlidingWindowResults, signif::SurrogatesSignificance;
colors = ["#7143E0", "#0A9A84", "#191E44", "#AF9327", "#701B80", "#2E6137",],
skip = Int[],
nsurro = 20,
)
config = res.config
for (i, cha) in enumerate(config.change_metrics)
i skip && continue

for _ in 1:nsurro
s = TimeseriesSurrogates.surrogate(res.x, signif.surrogate)
if isnothing(config.indicators)
p = s
chaj = 2
else
p = windowmap(config.indicators[i], s; width = config.width_ind, stride = config.stride_ind)
lines!(fig[2, 1], res.t_indicator, q; color = (colors[i], 2/nsurro), linewidth = 1)
chaj = 3
end
q = windowmap(cha, p; width = config.width_cha, stride = config.stride_cha)
lines!(fig[chaj, 1], res.t_change, q; color = (colors[i], 2/nsurro), linewidth = 1)
end
end

return fig
end


end
4 changes: 4 additions & 0 deletions src/TransitionsInTimeseries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ include("indicators/statistics.jl")
include("change_metrics/slope.jl")
include("change_metrics/valuediff.jl")

include("visualizations.jl")

# windowing.jl
export WindowViewer, windowmap, windowmap!, midpoint, midvalue
Expand Down Expand Up @@ -66,4 +67,7 @@ export isequispaced, equispaced_step
# load_data.jl
export load_linear_vs_doublewell

# visualizations
export plot_indicator_changes, plot_significance!

end # module TransitionsInTimeseries
9 changes: 9 additions & 0 deletions src/visualizations.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function plot_changes_significance(res::IndicatorsChangesConfig, signif::TransitionsSignificance)
fig = plot_indicator_changes()
plot_significance!()
return fig
end

function plot_indicator_changes end

function plot_significance! end
7 changes: 6 additions & 1 deletion test/full_analysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ end
@test count(>(1), c) == 1

# surrogates
signif = SurrogatesSignificance(surromethod = RandomShuffle(), n = 1000, tail = :both, p = 0.05)
signif = SurrogatesSignificance(surromethod = RandomShuffle(), n = 1000, tail = :left, p = 0.05)
flags = significant_transitions(res, signif)

@test count(flags) == 1

# Test the surrogate results
# using CairoMakie
# fig = plot_indicator_changes(res)
# plot_significance!(fig, res, signif)

end

0 comments on commit 39239a3

Please sign in to comment.