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.