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

CanonicalIndexError with TrendFilter #83

Open
ptaitAtMcMaster opened this issue Nov 26, 2024 · 0 comments
Open

CanonicalIndexError with TrendFilter #83

ptaitAtMcMaster opened this issue Nov 26, 2024 · 0 comments

Comments

@ptaitAtMcMaster
Copy link

Hi,
I wanted to raise awareness of an error I have been getting with trend filtering. It looks like it could be an edge case of some kind. Here is a reproducible example:

`using Lasso, LinearAlgebra, Random
rng = Xoshiro(45);
n = 100;
y = rand(rng, n) .- 0.5;

f_fl = fit(FusedLasso, y, 0.5);
f_tf = fit(TrendFilter, y, 2, 0.5);
`

The trend filter fit results in the following error:

TrendFilter{Float64, Lasso.FusedLassoMod.NormalCoefs{Float64}, SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}}([ERROR: CanonicalIndexError: getindex not defined for Lasso.TrendFiltering.DifferenceMatrix{Float64}
Stacktrace:
[1] error_if_canonical_getindex(::IndexCartesian, ::Lasso.TrendFiltering.DifferenceMatrix{Float64}, ::Int64, ::Int64)
@ Base ./abstractarray.jl:1327
[2] getindex
@ ./abstractarray.jl:1311 [inlined]
[3] isassigned(::Lasso.TrendFiltering.DifferenceMatrix{Float64}, ::Int64, ::Int64)
@ Base ./multidimensional.jl:1612
[4] _show_nonempty(io::IOContext{Base.TTY}, X::AbstractMatrix, prefix::String, drop_brackets::Bool, axs::Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}})
@ Base ./arrayshow.jl:441
[5] _show_nonempty(io::IOContext{Base.TTY}, X::Lasso.TrendFiltering.DifferenceMatrix{Float64}, prefix::String)
@ Base ./arrayshow.jl:413
[6] show(io::IOContext{Base.TTY}, X::Lasso.TrendFiltering.DifferenceMatrix{Float64})
@ Base ./arrayshow.jl:491
[7] _show_default(io::IOContext{Base.TTY}, x::Any)
@ Base ./show.jl:496
[8] show_default
@ ./show.jl:479 [inlined]
[9] show
@ ./show.jl:474 [inlined]
[10] show(io::IOContext{Base.TTY}, ::MIME{Symbol("text/plain")}, x::TrendFilter{Float64, Lasso.FusedLassoMod.NormalCoefs{Float64}, SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}})
@ Base.Multimedia ./multimedia.jl:47
[11] (::REPL.var"#68#69"{REPL.REPLDisplay{REPL.LineEditREPL}, MIME{Symbol("text/plain")}, Base.RefValue{Any}})(io::Any)
@ REPL /snap/julia/113/share/julia/stdlib/v1.11/REPL/src/REPL.jl:367
[12] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
@ REPL /snap/julia/113/share/julia/stdlib/v1.11/REPL/src/REPL.jl:661
[13] display(d::REPL.REPLDisplay, mime::MIME{Symbol("text/plain")}, x::Any)
@ REPL /snap/julia/113/share/julia/stdlib/v1.11/REPL/src/REPL.jl:353
[14] display(d::REPL.REPLDisplay, x::Any)
@ REPL /snap/julia/113/share/julia/stdlib/v1.11/REPL/src/REPL.jl:372
[15] display(x::Any)
@ Base.Multimedia ./multimedia.jl:340
[16] #invokelatest#2
@ ./essentials.jl:1055 [inlined]
[17] invokelatest
@ ./essentials.jl:1052 [inlined]

The error is generated regardless of the value of the order parameter. I have attached a plot of the data and the fused lasso coefficients.

fused_lasso

In the next example, trend filtering dose work:

`fs = 250.0 # Sampling frequency in Hz
t = 0:1/fs:10 # 10 seconds of data
signal = sin.(2π1.0.t) + 0.5sin.(2π50.0.*t)

lambda = norm(signal, Inf)/2
fit_fl = fit(FusedLasso, signal, lambda);
fit_tf = fit(TrendFilter, signal, 2, lambda);
`
A plot of the results is attached.
tf

I have been using trend filtering often and run into this indexing error ~1/4 of the time.
Cheers
Peter

(@v1.11) pkg> st Lasso
Status ~/.julia/environments/v1.11/Project.toml
[b4fcebef] Lasso v0.7.2

julia> versioninfo()
Julia Version 1.11.1
Build Info:
Official https://julialang.org/ release
Platform Info:
OS: Linux (x86_64-linux-gnu)
LLVM: libLLVM-16.0.6 (ORCJIT, goldmont)

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

1 participant