Slicing a SparseAxisArray Variable in JuMP

Hi everyone,

I wish to slice a SparseAxisArray Container in JuMP. The documentation says this should work:

julia> x = Containers.@container([i = 1:3, j = [:A, :B]; i > 1], (i, j))
JuMP.Containers.SparseAxisArray{Tuple{Int64, Symbol}, 2, Tuple{Int64, Symbol}} with 4 entries:
  [2, A]  =  (2, :A)
  [2, B]  =  (2, :B)
  [3, A]  =  (3, :A)
  [3, B]  =  (3, :B)
julia> y = x[:, :B]
JuMP.Containers.SparseAxisArray{Tuple{Int64, Symbol}, 1, Tuple{Int64}} with 2 entries:
  [2]  =  (2, :B)
  [3]  =  (3, :B)

If I try the same code in my command line I get this Error:

julia> x = Containers.@container([i = 1:3, j = [:A, :B]; i > 1], (i, j))
JuMP.Containers.SparseAxisArray{Tuple{Int64, Symbol}, 2, Tuple{Int64, Symbol}} with 4 entries:
  [2, A]  =  (2, :A)
  [2, B]  =  (2, :B)
  [3, A]  =  (3, :A)
  [3, B]  =  (3, :B)
julia> x[:,:B]
ERROR: ArgumentError: Indexing with `:` is not supported by Containers.SparseAxisArray
Stacktrace:
 [1] _colon_error(#unused#::Colon, args::Symbol)
   @ JuMP.Containers C:\Users\jaelsner\.julia\packages\JuMP\Y4piv\src\Containers\SparseAxisArray.jl:94
 [2] getindex(::JuMP.Containers.SparseAxisArray{Tuple{Int64, Symbol}, 2, Tuple{Int64, Symbol}}, ::Function, ::Symbol)
   @ JuMP.Containers C:\Users\jaelsner\.julia\packages\JuMP\Y4piv\src\Containers\SparseAxisArray.jl:129
 [3] top-level scope
   @ REPL[28]:1

Does anyone have an idea what’s the problem here? Thanks!

Welcome! I cannot reproduce this with JuMP v1.3.1 and Julia 1.6.7 on a Linux machine. For me it works as stated in the documentation. Which versions are you using?

2 Likes

Hi Mike, that was the trick, thanks! I was running JuMP v1.1.1 and there slicing was not supported for SparseAxisArray.

2 Likes

Hi everyone,

I have exactly the same problem when trying to reproduce the above example taken from the documentation. I am running Julia 1.8.2 and JuMP 1.3.1 on a Windows machine.

Any idea why could this be happening? Thanks!

Any idea why could this be happening? Thanks!

You aren’t actually using JuMP v1.3.1. What is import Pkg; Pkg.status()? If you recently upgraded your packages, remember to restart Julia for the changes to take effect.

It shows β€œJuMP v1.3.1”.

Now I realized that I can reproduce the example in the Julia shell, but not in a notebook (IJulia)… does that make any sense?

Now I realized that I can reproduce the example in the Julia shell, but not in a notebook (IJulia)… does that make any sense?

No. It means you are running a different version of JuMP in the notebook. When did you update your packages? Have you restarted the notebook since?

Can you show the full output of Pkg.status() from a cell in the notebook?

I updated the packages yesterday. And by now I have restarted the notebook, Julia, and my laptop.

When I run (from a cell in the notebook):

import Pkg; Pkg.status()

I get:

Status C:\Users\xxx\.julia\environments\v1.8\Project.toml
[336ed68f] CSV v0.10.4
βŒ… [324d7699] CategoricalArrays v0.10.6
βŒ… [a93c6f00] DataFrames v1.3.4
[1313f7d8] DataFramesMeta v0.12.0
[7806a523] DecisionTree v0.11.2
[b4f34e82] Distances v0.10.7
βŒ… [31c24e10] Distributions v0.25.68
[38e38edf] GLM v1.8.0
[2e9cd046] Gurobi v0.11.3
[7073ff75] IJulia v1.23.3
[4076af6c] JuMP v1.3.1
[f0e99cf1] MLBase v0.9.0
[cc2ba9b6] MLDataUtils v0.5.4
βŒ… [b8f27783] MathOptInterface v1.7.0
βŒ… [b8a86587] NearestNeighbors v0.4.11
[42b8e9d4] ParallelKMeans v1.0.1
[a03496cd] PlotlyBase v0.8.19
βŒƒ [91a5bcdd] Plots v1.35.3
[3646fa90] ScikitLearn v0.6.4
[f3b207a7] StatsPlots v0.15.4
βŒ… [bd369af6] Tables v1.7.0
[9a3f8284] Random
[10745b16] Statistics
Info Packages marked with βŒƒ and βŒ… have new versions available, but those with βŒ… cannot be upgraded. To see why use status --outdated

And what is the exact error if you run:

using JuMP
x = Containers.@container([i = 1:3, j = [:A, :B]; i > 1], (i, j))
x[:, :A]

This is the error it shows:

ArgumentError: Indexing with : is not supported by Containers.SparseAxisArray

Stacktrace:
[1] _colon_error(#unused#::Colon, args::Symbol)
@ JuMP.Containers C:\Users\iai\builds\InterpretableAI\SystemImage\SysImgBuilder.julia\packages\JuMP\60Bnj\src\Containers\SparseAxisArray.jl:94
[2] getindex(::JuMP.Containers.SparseAxisArray{Tuple{Int64, Symbol}, 2, Tuple{Int64, Symbol}}, ::Function, ::Symbol)
@ JuMP.Containers C:\Users\iai\builds\InterpretableAI\SystemImage\SysImgBuilder.julia\packages\JuMP\60Bnj\src\Containers\SparseAxisArray.jl:129
[3] top-level scope
@ In[1]:3
[4] eval
@ .\boot.jl:368 [inlined]
[5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1428

This suggests you built a system image with an older version of JuMP? If you update your packages, you need to rebuild the system image.

That fixes the problem. Thank you!

1 Like