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

DimensionMismatch error with an animation for the size changing array with Observable #2441

Closed
michikazjp opened this issue Nov 24, 2022 · 3 comments

Comments

@michikazjp
Copy link

michikazjp commented Nov 24, 2022

I am trying to make an animation with size changing array with Observable like

using GLMakie
function plot_graph_realtime(x)
    fig=Figure()
    ax=Axis(fig[1,1])
    it=Observable(1)
    @lift(lines!(ax,x[1,1:$it],x[2,1:$it]))
    xlims!(ax,-1,3)
    ylims!(ax,-1,3)
    display(fig)
    for i=1:size(x,2)
        sleep(1)
        it[]=i
    end
end
plot_graph_realtime([0 1 2 3;0 1 2 3])

The above code does work. But the similar code like

using GLMakie
function plot_graph_realtime(x)
    fig=Figure()
    ax=Axis(fig[1,1])
    it=Observable(1)
    lines!(ax,@lift(x[1,1:$it]),@lift(x[2,1:$it]))
    xlims!(ax,-1,3)
    ylims!(ax,-1,3)
    display(fig)
    for i=1:size(x,2)
        sleep(1)
        it[]=i
    end
end
plot_graph_realtime([0 1 2 3;0 1 2 3])

does not work with the following error

DimensionMismatch("arrays could not be broadcast to a common size; got a dimension with lengths 3 and 2")

Stacktrace:
  [1] _bcs1
    @ ./broadcast.jl:516 [inlined]
  [2] _bcs
    @ ./broadcast.jl:510 [inlined]
  [3] broadcast_shape
    @ ./broadcast.jl:504 [inlined]
  [4] combine_axes
    @ ./broadcast.jl:499 [inlined]
  [5] instantiate
    @ ./broadcast.jl:281 [inlined]
  [6] materialize
    @ ./broadcast.jl:860 [inlined]
  [7] convert_arguments(P::PointBased, x::Vector{Int64}, y::Vector{Int64})
    @ Makie ~/.julia/packages/Makie/Ppzqh/src/conversions.jl:136
  [8] convert_arguments(::Type{Lines{Tuple{Vector{Int64}, Vector{Int64}}}}, ::Vector{Int64}, ::Vararg{Vector{Int64}}; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Makie ~/.julia/packages/Makie/Ppzqh/src/conversions.jl:10
  [9] convert_arguments(::Type{Lines{Tuple{Vector{Int64}, Vector{Int64}}}}, ::Vector{Int64}, ::Vector{Int64})
    @ Makie ~/.julia/packages/Makie/Ppzqh/src/conversions.jl:8
 [10] (::Makie.var"#142#144"{Attributes, Observable{Tuple{Vector{Point{2, Float32}}}}, DataType})(kwargs::Tuple{}, args::Tuple{Vector{Int64}, Vector{Int64}})
    @ Makie ~/.julia/packages/Makie/Ppzqh/src/interfaces.jl:326
 [11] (::Observables.var"#callback#13"{Makie.var"#142#144"{Attributes, Observable{Tuple{Vector{Point{2, Float32}}}}, DataType}, Tuple{Observable{Tuple{}}, Observable{Tuple{Vector{Int64}, Vector{Int64}}}}})(x::Any)
    @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:339
 [12] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
 [13] invokelatest
    @ ./essentials.jl:714 [inlined]
 [14] notify
    @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:143 [inlined]
 [15] setindex!(observable::Observable, val::Any)
    @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:86
 [16] #15
    @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:393 [inlined]
 [17] (::Observables.var"#callback#13"{Observables.var"#15#16"{typeof(tuple), Observable{Tuple{Vector{Int64}, Vector{Int64}}}}, Tuple{Observable{Vector{Int64}}, Observable{Vector{Int64}}}})(x::Any)
    @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:339
 [18] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
 [19] invokelatest
    @ ./essentials.jl:714 [inlined]
 [20] notify
    @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:143 [inlined]
 [21] setindex!(observable::Observable, val::Any)
    @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:86
 [22] #15
    @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:393 [inlined]
 [23] (::Observables.var"#callback#13"{Observables.var"#15#16"{var"#499#501"{Matrix{Int64}}, Observable{Vector{Int64}}}, Tuple{Observable{Int64}}})(x::Any)
    @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:339
 [24] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
 [25] invokelatest
    @ ./essentials.jl:714 [inlined]
 [26] notify
    @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:143 [inlined]
 [27] setindex!
    @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:86 [inlined]
 [28] plot_graph_realtime(x::Matrix{Int64})
    @ Main ./In[135]:12
 [29] top-level scope
    @ In[135]:15
 [30] eval
    @ ./boot.jl:373 [inlined]
 [31] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1196

Do you have any idea for the reason with the error?

@fatteneder
Copy link
Contributor

fatteneder commented Nov 25, 2022

In your first example, you create one Observable which wraps a call to lines! with two arrays that area always of equal length.
In your second example, you create two Observables and pass them to a lines! call.
When you then trigger an update with it[]=i the two Observables are updated separately one after the other. This causes lines! to fail, because it sees two arrays with different lengths for a short time.

If you want to use two Observables then you should update one 'silently' (e.g. without sending notifications to its listeners) and trigger a replot with the second, e.g.

using GLMakie
function plot_graph_realtime(x)
    fig=Figure()
    ax=Axis(fig[1,1])
    it=Observable(1)
    obs_x1 = Observable(x[1,1:it[]])
    obs_x2 = Observable(x[2,1:it[]])
    on(it) do _it
       # using .val to change the value will not propagate an update
       obs_x1.val = x[1,1:_it]
       # using [] will trigger an update
       obs_x2[] = x[2,1:_it]
    end
    lines!(ax,obs_x1, obs_x2)
    xlims!(ax,-1,3)
    ylims!(ax,-1,3)
    display(fig)
    for i=1:size(x,2)
        sleep(1)
        it[]=i
    end
end
plot_graph_realtime([0 1 2 3;0 1 2 3])

@michikazjp
Copy link
Author

Thanks a lot!
Probably,
obs_x2[] = x[2:1:_it]
in line 12 should be corrected to be
obs_x2[] = x[2,1:_it]
Anyway, your suggestion worked in better than my first code.

@likev
Copy link
Contributor

likev commented May 28, 2024

see also #3658

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants