MethodError: objects of type NamedTuple not callable

Hello Pumas team; I guess I have a problem with the simulation. Most of the code is taken from the tutorial part, some additional parameters added.

I have the issue as: MethodError: objects of type NamedTuple{(:Ka, :K2, :CL, :Vd),NTuple{4,Float64}} are not callable

Any suggestions?

using Pumas, Plots, DifferentialEquations
using StochasticDiffEq

p = ParamSet((θ = VectorDomain(4, lower = zeros(4)), 
              Ω = PSDDomain(2)))

function randomfx(p) 
        ParamSet((η=MvNormal(p.Ω),))
end

function pre_f(params, randoms, subj)
        θ = params.θ
        η = randoms.η
        (
        Ka = θ[1],
        K2 = θ[2], 
        CL = θ[3]*exp(η[1]), 
        Vd = θ[4]*exp(η[2])
        )
end

# define the stochastic equations here 


function f(du, u, p, t)
        depot1, depot2, central = u
        du[1] = p.Ka * depot1
        du[2] = p.K2 *  depot2 
        du[3] = (p.Ka*depot1 + p.K2*depot2) - (p.CL/p.Vd)*central
end 

function g(du, u, p, t)
        du[1] = 0.4*u[1]
        du[2] = 0.0
        du[3] = 0.5*u[3]
end

prob = SDEProblem(f, g, nothing, nothing)  

init_f = (t, c) -> [0.0, 0.0]

function derived_f(c, sol, obstimes, subj)
        central = sol(obstimes; idxs=5)
        conc = @. central/c.V
        (conc=conc, )
end

stoch_model = Pumas.PumasModel(p, randomfx, pre_f, init_f, prob, derived_f)

pop = Population([Subject(evs= DosageRegimen(10, time = 0)) for i in 1:5]) 
param = (θ = [
              0.3, 
              0.2, 
              1.3, 
              64.7
              ],
         Ω = [1.0 0.0; 0.0 1.0])
### 
obs = simobs(stoch_model, pop, param, alg=SOSRI())

Hi @vijay , any ideas?

@mjaber we had a change in the API for the pre block. @ChrisRackauckas or @pkofod will get back to you with a detailed response.

1 Like

Hey,
Sorry, we do need to update that tutorial. The main thing is that pre is a function of time now. I also corrected a few things, so working code is this:

using Pumas, Plots, StochasticDiffEq

p = ParamSet((θ = VectorDomain(4, lower = zeros(4)),
              Ω = PSDDomain(2)))

function randomfx(p)
        ParamSet((η=MvNormal(p.Ω),))
end

function pre_f(params, randoms, subj)
        function (t)
                θ = params.θ
                η = randoms.η
                (
                Ka = θ[1],
                K2 = θ[2],
                CL = θ[3]*exp(η[1]),
                Vd = θ[4]*exp(η[2])
                )
        end
end

# define the stochastic equations here


function f(du, u, p, t)
        depot1, depot2, central = u
        du[1] = p.Ka * depot1
        du[2] = p.K2 *  depot2
        du[3] = (p.Ka*depot1 + p.K2*depot2) - (p.CL/p.Vd)*central
end

function g(du, u, p, t)
        du[1] = 0.4*u[1]
        du[2] = 0.0
        du[3] = 0.5*u[3]
end

prob = SDEProblem(f, g, nothing, nothing)

init_f = (t, c) -> [0.0, 0.0, 0.0]

function derived_f(c, sol, obstimes, subj,param, randeffs)
        Vs = getfield.(c.(obstimes),:Vd)
        central = sol(obstimes; idxs=3)
        conc = @. central/Vs
        (conc=conc, )
end

stoch_model = Pumas.PumasModel(p, randomfx, pre_f, init_f, prob, derived_f)

pop = Population([Subject(evs= DosageRegimen(10, time = 0)) for i in 1:5])
param = (θ = [
              0.3,
              0.2,
              1.3,
              64.7
              ],
         Ω = [1.0 0.0; 0.0 1.0])
###
obs = simobs(stoch_model, pop, param, alg=SOSRI())

For a quick explanation, notice that

function pre_f(params, randoms, subj)
        function (t)

pre is a function that returns time-varying functions (and thus you can see how to represent a time-varying covariate in this sense). Another issue I saw in your code is that you increased the number of dynamical variables to 3, but forgot to do:

init_f = (t, c) -> [0.0, 0.0, 0.0]

i.e. give a third initial value. Additionally, the derived needs to then calculate the pre at each t to grab the V (Vs = getfield.(c.(obstimes),:Vd)), and utilize the DifferentialEquations.jl solution object to snag the values of the third ODE at the obstimes (central = sol(obstimes; idxs=3)).

We’ll get our example updated, but hopefully that extra information will be helpful as well. One last tip is that turning off parallelism, i.e.

obs = simobs(stoch_model, pop, param, alg=SOSRI(),ensemblealg=EnsembleSerial())

can be quite helpful when debugging. Let me know if you need anything else.

3 Likes

That make much more sense. Thank you! For now everything seems working fine since I’m working with SDEs, DiffEqTutorial has a good structural examples there.

Hi @ChrisRackauckas,
If I want to add a delay function (DDE) using SDDEProblem. Can it work on same algorithm just adding the h value?

I try to do it, and got this error

TaskFailedException:
  MethodError: no method matching make_function(::SDDEProblem{Array{Float64,1},Tuple{Float64,Float64},Tuple{},Tuple{},true,var"#11#12"{NamedTuple{(:θ, :Ω),Tuple{Array{Float64,1},Array{Float64,2}}},NamedTuple{(:η,),Tuple{Array{Float64,1}}}},Nothing,SDDEFunction{true,typeof(f),typeof(g),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},typeof(g),typeof(h),Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}},Nothing}, ::Pumas.DiffEqWrapper{typeof(f),Array{Float64,1}})
  Closest candidates are:
    make_function(!Matched::ODEProblem, ::Any) at /Users/mjaber/.juliapro/JuliaPro_v1.4.2-1/packages/Pumas/Kxgzs/src/simulate_methods/diffeqs.jl:58
    make_function(!Matched::DDEProblem, ::Any) at /Users/mjaber/.juliapro/JuliaPro_v1.4.2-1/packages/Pumas/Kxgzs/src/simulate_methods/diffeqs.jl:59
    make_function(!Matched::DiscreteProblem, ::Any) at /Users/mjaber/.juliapro/JuliaPro_v1.4.2-1/packages/Pumas/Kxgzs/src/simulate_methods/diffeqs.jl:60
    ...
  Stacktrace:
   [1] _build_diffeq_problem(::PumasModel{ParamSet{NamedTuple{(:θ, :Ω),Tuple{VectorDomain{Array{Float64,1},Array{TransformVariables.Infinity{true},1},Array{Float64,1}},PSDDomain{Array{Float64,2}}}}},typeof(randomfx),typeof(pre_f),var"#13#14",SDDEProblem{Array{Float64,1},Tuple{Float64,Float64},Tuple{},Tuple{},true,var"#11#12"{NamedTuple{(:θ, :Ω),Tuple{Array{Float64,1},Array{Float64,2}}},NamedTuple{(:η,),Tuple{Array{Float64,1}}}},Nothing,SDDEFunction{true,typeof(f),typeof(g),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},typeof(g),typeof(h),Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}},Nothing},typeof(derived_f),Pumas.var"#114#115"}, ::Subject{Nothing,Nothing,Array{Pumas.Event,1},Nothing,Pumas.var"#10#11",StaticArrays.SArray{Tuple{1},Float64,1,1}}; saveat::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, save_discont::Bool, continuity::Symbol, callback::Nothing, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}}) at /Users/mjaber/.juliapro/JuliaPro_v1.4.2-1/packages/Pumas/Kxgzs/src/simulate_methods/diffeqs.jl:32
   [2] _problem(::PumasModel{ParamSet{NamedTuple{(:θ, :Ω),Tuple{VectorDomain{Array{Float64,1},Array{TransformVariables.Infinity{true},1},Array{Float64,1}},PSDDomain{Array{Float64,2}}}}},typeof(randomfx),typeof(pre_f),var"#13#14",SDDEProblem{Nothing,Tuple{Nothing,Nothing},Tuple{},Tuple{},true,DiffEqBase.NullParameters,Nothing,SDDEFunction{true,typeof(f),typeof(g),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},typeof(g),typeof(h),Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}},Nothing},typeof(derived_f),Pumas.var"#114#115"}, ::Subject{Nothing,Nothing,Array{Pumas.Event,1},Nothing,Pumas.var"#10#11",StaticArrays.SArray{Tuple{1},Float64,1,1}}, ::var"#11#12"{NamedTuple{(:θ, :Ω),Tuple{Array{Float64,1},Array{Float64,2}}},NamedTuple{(:η,),Tuple{Array{Float64,1}}}}; tspan::Nothing, saveat::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, kwargs::Base.Iterators.Pairs{Symbol,Nothing,Tuple{Symbol},NamedTuple{(:callback,),Tuple{Nothing}}}) at /Users/mjaber/.juliapro/JuliaPro_v1.4.2-1/packages/Pumas/Kxgzs/src/models/model_api.jl:146
   [3] (::Pumas.var"#simobs_prob_func#131"{Nothing,Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}},PumasModel{ParamSet{NamedTuple{(:θ, :Ω),Tuple{VectorDomain{Array{Float64,1},Array{TransformVariables.Infinity{true},1},Array{Float64,1}},PSDDomain{Array{Float64,2}}}}},typeof(randomfx),typeof(pre_f),var"#13#14",SDDEProblem{Nothing,Tuple{Nothing,Nothing},Tuple{},Tuple{},true,DiffEqBase.NullParameters,Nothing,SDDEFunction{true,typeof(f),typeof(g),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},typeof(g),typeof(h),Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}},Nothing},typeof(derived_f),Pumas.var"#114#115"},Array{Subject{Nothing,Nothing,Array{Pumas.Event,1},Nothing,Pumas.var"#10#11",StaticArrays.SArray{Tuple{1},Float64,1,1}},1},NamedTuple{(:θ, :Ω),Tuple{Array{Float64,1},Array{Float64,2}}},Nothing,Tuple{}})(::SDDEProblem{Nothing,Tuple{Nothing,Nothing},Tuple{},Tuple{},true,DiffEqBase.NullParameters,Nothing,SDDEFunction{true,typeof(f),typeof(g),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},typeof(g),typeof(h),Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}},Nothing}, ::Int64, ::Int64) at /Users/mjaber/.juliapro/JuliaPro_v1.4.2-1/packages/Pumas/Kxgzs/src/models/model_api.jl:261
   [4] macro expansion at /Users/mjaber/.juliapro/JuliaPro_v1.4.2-1/packages/DiffEqBase/XoVg5/src/ensemble/basic_ensemble_solve.jl:162 [inlined]
   [5]..

My model is

using Pumas, DifferentialEquations

p = ParamSet((θ = VectorDomain(5, lower = zeros(4)),
              Ω = PSDDomain(3)))

function randomfx(p)
        ParamSet((η=MvNormal(p.Ω),))
end

function pre_f(params, randoms, subj)
        function (t)
                θ = params.θ
                η = randoms.η
                (
                Ka = θ[1],
                K2 = θ[2],
                CL = θ[3]*exp(η[1]),
                Vd = θ[4]*exp(η[2]),
                τ  = θ[5]*exp(η[3])
                )
        end
end

# define the stochastic equations here


function f(du, u, p,h,  t)
        depot1, depot2, central = u
        hist = h(u,  t-p.τ)[1]
        du[1] = p.Ka * hist
        du[2] = p.K2 *  depot2
        du[3] = (p.Ka*depot1 + p.K2*depot2) - (p.CL/p.Vd)*central
end

function g(du, u, p, t)
        du[1] = 0.4*u[1]
        du[2] = 0.0
        du[3] = 0.5*u[3]
end

h(p, t) = [1.0, 0.0, 0.0]

prob = SDDEProblem(f, g, nothing, h, nothing)

init_f = (t, c) -> [0.0, 0.0, 0.0]

function derived_f(c, sol, obstimes, subj,param, randeffs)
        Vs = getfield.(c.(obstimes),:Vd)
        central = sol(obstimes; idxs=3)
        conc = @. central/Vs
        (conc=conc, )
end

stoch_model = Pumas.PumasModel(p, randomfx, pre_f, init_f, prob, derived_f)

pop = Population([Subject(evs= DosageRegimen(10, time = 0)) for i in 1:5])
param = (θ = [
              0.3,
              0.2,
              1.3,
              64.7,
              0.9,
              ],
         Ω = [1.0 0.0 0.0 ; 0.0 1.0 0.0; 0.0 0.0 1.0])
###
obs = simobs(stoch_model, pop, param, alg=SOSRI())

The SDDE tools are not ready yet. I wouldn’t use them until they’ve had their formal release.

1 Like