Skip to content

UnboundLocalError when calling baseline() function #22

@Matheus-gif

Description

@Matheus-gif

Hey guys, thank you all very much for the package. I'm trying to replicate the baselines_examples on the notebook. I'm able to use all the function methods except for "gcvspline". I've create a virtual environment and ran the following

pkg] add PyCall
julia> using PyCall
julia> @pyimport pip
julia> pip.main(["install","rampy"])
julia> pip.main(["install", "rampy"])
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Requirement already satisfied: rampy in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (0.5.3)
Requirement already satisfied: numpy>=1.12 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from rampy) (2.2.4)
Requirement already satisfied: scipy in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from rampy) (1.15.2)
Requirement already satisfied: scikit-learn in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from rampy) (1.6.1)
Requirement already satisfied: pandas in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from rampy) (2.2.3)
Requirement already satisfied: xlrd in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from rampy) (2.0.1)
Requirement already satisfied: matplotlib in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from rampy) (3.10.1)
Requirement already satisfied: contourpy>=1.0.1 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from matplotlib->rampy) (1.3.1)
Requirement already satisfied: cycler>=0.10 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from matplotlib->rampy) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from matplotlib->rampy) (4.56.0)
Requirement already satisfied: kiwisolver>=1.3.1 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from matplotlib->rampy) (1.4.8)
Requirement already satisfied: packaging>=20.0 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from matplotlib->rampy) (24.2)
Requirement already satisfied: pillow>=8 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from matplotlib->rampy) (11.1.0)
Requirement already satisfied: pyparsing>=2.3.1 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from matplotlib->rampy) (3.2.1)
Requirement already satisfied: python-dateutil>=2.7 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from matplotlib->rampy) (2.9.0.post0)        
Requirement already satisfied: pytz>=2020.1 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from pandas->rampy) (2025.1)
Requirement already satisfied: tzdata>=2022.7 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from pandas->rampy) (2025.1)
Requirement already satisfied: joblib>=1.2.0 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from scikit-learn->rampy) (1.4.2)
Requirement already satisfied: threadpoolctl>=3.1.0 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from scikit-learn->rampy) (3.6.0)
Requirement already satisfied: six>=1.5 in c:\users\user\.julia\conda\3\x86_64\lib\site-packages (from python-dateutil>=2.7->matplotlib->rampy) (1.17.0)
0

But I'm still unable to run the notebook examples regarding baseline() on VSCode. All methods work except for "gcvspline".

# We define the portions of the spectra where we want to fit the signal background.
# roi should be an n x 2 array, see documentation at 
roi = [0 100.;200 220;280 290;420 430; 480 500]

# caling the baseline function with a natural spline that will fit the signal in ROIs
y_gcvspl, bas_gcvspl = baseline(x,y,roi,"gcvspline",s=1.)

# using the ALS algorithm (Baek et al. 2015), 10^2-10^5 lambda and 0.001-0.1 p values are recommended
y_als, bas_als = baseline(x,y,roi,"als",p=0.01,lam=10^6,niter=10)

# using the arPLS algorithm (Baek et al. 2015)
y_arpls, bas_arpls = baseline(x,y,roi,"arPLS",p=0.1,lam=10.0^6)

figure()

# plotting the initial signal and the roi
plot(x,y,"black",label="signal")

# plotting the baselines
plot(x,bas_gcvspl,"cyan",label="gcvspline")
plot(x,bas_als,"purple",linestyle="--",label="ALS")
plot(x,bas_arpls,"orange",linestyle="-.",label="arPLS")
legend()

xlabel("X",fontname="Arial",fontsize=18)
ylabel("Y",fontname="Arial",fontsize=18)

display(gcf())
PyError ($(Expr(:escape, :(ccall(#= C:\Users\User\.julia\packages\PyCall\1gn3u\src\pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'UnboundLocalError'>
UnboundLocalError("cannot access local variable 'gcvspline' where it is not associated with a value")
  File "C:\Users\User\.julia\conda\3\x86_64\Lib\site-packages\rampy\baseline.py", line 149, in baseline
    c, wk, ier = gcvspline(yafit[:,0],yafit[:,1],np.sqrt(np.abs(yafit[:,1])),splinesmooth,splmode = 1) # gcvspl with mode 1 and smooth factor
                 ^^^^^^^^^


Stacktrace:
  [1] pyerr_check
    @ C:\Users\User\.julia\packages\PyCall\1gn3u\src\exception.jl:75 [inlined]
  [2] pyerr_check
    @ C:\Users\User\.julia\packages\PyCall\1gn3u\src\exception.jl:79 [inlined]
  [3] _handle_error(msg::String)
    @ PyCall C:\Users\User\.julia\packages\PyCall\1gn3u\src\exception.jl:96
  [4] macro expansion
    @ C:\Users\User\.julia\packages\PyCall\1gn3u\src\exception.jl:110 [inlined]
  [5] #107
    @ C:\Users\User\.julia\packages\PyCall\1gn3u\src\pyfncall.jl:43 [inlined]
  [6] disable_sigint
    @ .\c.jl:167 [inlined]
  [7] __pycall!
    @ C:\Users\User\.julia\packages\PyCall\1gn3u\src\pyfncall.jl:42 [inlined]
  [8] _pycall!(ret::PyObject, o::PyObject, args::Tuple{Vector{Float64}, Vector{Float64}, Matrix{Float64}, String}, nargs::Int64, kw::PyObject)
    @ PyCall C:\Users\User\.julia\packages\PyCall\1gn3u\src\pyfncall.jl:29
  [9] _pycall!(ret::PyObject, o::PyObject, args::Tuple{Vector{Float64}, Vector{Float64}, Matrix{Float64}, String}, kwargs::@Kwargs{polynomial_order::Int64, s::Float64, lam::Int64, niter::Int64, p0_exp::Vector{Float64}, p0_log::Vector{Float64}})
    @ PyCall C:\Users\User\.julia\packages\PyCall\1gn3u\src\pyfncall.jl:11
 [10] (::PyObject)(::Vector{Float64}, ::Vararg{Any}; kwargs::@Kwargs{polynomial_order::Int64, s::Float64, lam::Int64, niter::Int64, p0_exp::Vector{Float64}, p0_log::Vector{Float64}})
    @ PyCall C:\Users\User\.julia\packages\PyCall\1gn3u\src\pyfncall.jl:86
 [11] baseline(x::Vector{Float64}, y::Vector{Float64}, roi::Matrix{Float64}, basetype::String; polynomial_order::Int64, s::Float64, lam::Int64, p::Float64, ratio::Float64, niter::Int64, p0_exp::Vector{Float64}, p0_log::Vector{Float64})
    @ Spectra C:\Users\User\.julia\packages\Spectra\gifVq\src\baseline.jl:86
 [12] top-level scope
    @ c:\Users\User\Desktop\tratar_dados\codigos_julia\doutorado\dems\jl_notebook_cell_df34fa98e69747e1a8f8a730347b8e2f_X15sZmlsZQ==.jl:6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions