Skip to content

[feature] Optimize does not work on Colorant{T,1} #12

Open
@kunzaatko

Description

@kunzaatko

When trying to fit an image of a bead, it is most commonly represented as an array of colorants. The most likely for this use in particular is for it to be some subtype of AbstractGray. But when trying to fit some kind of Gray array, there it throws:

ERROR: MethodError: no method matching DiffResults.DiffResult(::Gray{Float64}, ::Vector{Float64})
Closest candidates are:
DiffResults.DiffResult(::Union{Number, AbstractArray}, ::Union{Number, AbstractArray}...) at ~/.julia/packages/DiffResults/wASAy/src/DiffResults.jl:52
Stacktrace:
[1] (::NLSolversBase.var"#14#18"{Gray{Float64}, PSFModels.var"#_loss#42"{NamedTuple{(), Tuple{}}, typeof(abs2), Float64, typeof(gaussian), Matrix{Gray{Float64}}, Tuple{Int64, Int64}, Tuple{Int64, Int64}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, NTuple{4, Symbol}}, ForwardDiff.GradientConfig{ForwardDiff.Tag{PSFModels.var"#_loss#42"{NamedTuple{(), Tuple{}}, typeof(abs2), Float64, typeof(gaussian), Matrix{Gray{Float64}}, Tuple{Int64, Int64}, Tuple{Int64, Int64}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, NTuple{4, Symbol}}, Gray{Float64}}, Gray{Float64}, 5, Vector{ForwardDiff.Dual{ForwardDiff.Tag{PSFModels.var"#_loss#42"{NamedTuple{(), Tuple{}}, typeof(abs2), Float64, typeof(gaussian), Matrix{Gray{Float64}}, Tuple{Int64, Int64}, Tuple{Int64, Int64}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, NTuple{4, Symbol}}, Gray{Float64}}, Gray{Float64}, 5}}}})(out::Vector{Float64}, x::Vector{Gray{Float64}})
@ NLSolversBase ~/.julia/packages/NLSolversBase/cfJrN/src/objective_types/oncedifferentiable.jl:69
[2] value_gradient!!(obj::NLSolversBase.OnceDifferentiable{Float64, Vector{Float64}, Vector{Gray{Float64}}}, x::Vector{Gray{Float64}})
@ NLSolversBase ~/.julia/packages/NLSolversBase/cfJrN/src/interface.jl:82
[3] initial_state(method::Optim.LBFGS{Nothing, LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Optim.var"#19#21"}, options::Optim.Options{Float64, Nothing}, d::NLSolversBase.OnceDifferentiable{Float64, Vector{Float64}, Vector{Gray{Float64}}}, initial_x::Vector{Gray{Float64}})
@ Optim ~/.julia/packages/Optim/wFOeG/src/multivariate/solvers/first_order/l_bfgs.jl:164
[4] optimize(d::NLSolversBase.OnceDifferentiable{Float64, Vector{Float64}, Vector{Gray{Float64}}}, initial_x::Vector{Gray{Float64}}, method::Optim.LBFGS{Nothing, LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Optim.var"#19#21"}, options::Optim.Options{Float64, Nothing})
@ Optim ~/.julia/packages/Optim/wFOeG/src/multivariate/optimize/optimize.jl:36
[5] optimize(f::Function, initial_x::Vector{Gray{Float64}}, method::Optim.LBFGS{Nothing, LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Optim.var"#19#21"}, options::Optim.Options{Float64, Nothing}; inplace::Bool, autodiff::Symbol)
@ Optim ~/.julia/packages/Optim/wFOeG/src/multivariate/optimize/interface.jl:142
[6] fit(model::typeof(gaussian), params::NamedTuple{(:x, :y, :fwhm, :amp), Tuple{Float64, Float64, Tuple{Int64, Int64}, Int64}}, image::Matrix{Gray{Float64}}, inds::Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}; func_kwargs::NamedTuple{(), Tuple{}}, loss::typeof(abs2), alg::Optim.LBFGS{Nothing, LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Optim.var"#19#21"}, maxfwhm::Float64, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ PSFModels ~/.julia/packages/PSFModels/G3WuK/src/fitting.jl:99
[7] fit (repeats 2 times)
@ ~/.julia/packages/PSFModels/G3WuK/src/fitting.jl:76 [inlined]
[8] top-level scope
@ REPL[167]:1

Could there be definition such as:

function fit(..., image::AbstractArray{T}, ...) where {T<:AbstractGray}
    fit(..., Real.(image), ...)
end

Do you think it is a good idea?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions