11"""
2- StatsStep{Alias,F<:Function}
2+ StatsStep{Alias, F<:Function}
33
44Specify the function for moving a step in an [`AbstractStatsProcedure`](@ref).
55An instance of `StatsStep` is callable.
@@ -12,7 +12,7 @@ An instance of `StatsStep` is callable.
1212 (step::StatsStep{A,F})(ntargs::NamedTuple; verbose::Bool=false)
1313
1414Call an instance of function of type `F` with arguments
15- formed by updating `NamedArgs ` with `ntargs`.
15+ formed by updating `NamedTuple` returned by `[`namedargs(step)`](@ref) ` with `ntargs`.
1616
1717A message with the name of the `StatsStep` is printed to `stdout`
1818if a keyword `verbose` takes the value `true`
@@ -21,20 +21,30 @@ The value from `ntargs` supersedes the keyword argument
2121in case both are specified.
2222
2323## Returns
24- - `NamedTuple`: named intermidiate results.
24+ - `NamedTuple`: named intermediate results.
2525"""
26- struct StatsStep{Alias,F<: Function } end
26+ struct StatsStep{Alias, F<: Function } end
2727
2828_f (:: StatsStep{A,F} ) where {A,F} = F. instance
2929
30+ """
31+ namedargs(s::StatsStep)
32+
33+ Return a `NamedTuple` with keys showing the names of arguments
34+ accepted by `s` and values representing the defaults.
35+ """
36+ namedargs (s:: StatsStep ) = error (" method for $(typeof (s)) is not defined" )
37+
3038_getargs (ntargs:: NamedTuple , s:: StatsStep ) = _update (ntargs, namedargs (s))
3139_update (a:: NamedTuple{N1} , b:: NamedTuple{N2} ) where {N1,N2} =
3240 NamedTuple {N2} (map (n-> getfield (sym_in (n, N1) ? a : b, n), N2))
3341
42+ _combinedargs (:: StatsStep , :: Any ) = ()
43+
3444function (step:: StatsStep{A,F} )(ntargs:: NamedTuple ; verbose:: Bool = false ) where {A,F}
3545 haskey (ntargs, :verbose ) && (verbose = ntargs. verbose)
3646 verbose && printstyled (" Running " , step, " \n " , color= :green )
37- ret = F. instance (_getargs (ntargs, step)... )
47+ ret, share = F. instance (_getargs (ntargs, step) ... , _combinedargs (step, (ntargs,) )... )
3848 if ret isa NamedTuple
3949 return merge (ntargs, ret)
4050 elseif ret === nothing
@@ -56,15 +66,7 @@ function show(io::IO, ::MIME"text/plain", s::StatsStep{A,F}) where {A,F}
5666end
5767
5868"""
59- namedargs(s::StatsStep)
60-
61- Return a `NamedTuple` with keys showing the names of arguments
62- accepted by `s` and values representing the defaults.
63- """
64- namedargs (s:: StatsStep ) = error (" method for $(typeof (s)) is not defined" )
65-
66- """
67- AbstractStatsProcedure{Alias,T<:NTuple{N,StatsStep} where N}
69+ AbstractStatsProcedure{Alias, T<:NTuple{N,StatsStep} where N}
6870
6971Supertype for all types specifying the procedure for statistical estimation or inference.
7072
@@ -75,7 +77,7 @@ all subtypes of `AbstractStatsProcedure`.
7577- `Alias::Symbol`: alias of the type for pretty-printing.
7678- `T<:NTuple{N,StatsStep}`: steps involved in the procedure.
7779"""
78- abstract type AbstractStatsProcedure{A, T<: NTuple{N,StatsStep} where N} end
80+ abstract type AbstractStatsProcedure{Alias, T<: NTuple{N,StatsStep} where N} end
7981
8082length (:: AbstractStatsProcedure{A,T} ) where {A,T} = length (T. parameters)
8183eltype (:: Type{<:AbstractStatsProcedure} ) = StatsStep
@@ -109,7 +111,7 @@ function show(io::IO, ::MIME"text/plain", p::AbstractStatsProcedure{A,T}) where
109111end
110112
111113"""
112- SharedStatsStep{T<:StatsStep,I}
114+ SharedStatsStep{T<:StatsStep, I}
113115
114116A [`StatsStep`](@ref) that is possibly shared by
115117multiple instances of procedures that are subtypes of [`AbstractStatsProcedure`](@ref).
@@ -119,7 +121,7 @@ See also [`PooledStatsProcedure`](@ref).
119121- `T<:StatsStep`: type of the only field `step`.
120122- `I`: indices of the procedures that share this step.
121123"""
122- struct SharedStatsStep{T<: StatsStep ,I}
124+ struct SharedStatsStep{T<: StatsStep , I}
123125 step:: T
124126 function SharedStatsStep (s:: StatsStep , pid)
125127 pid = (unique! (sort! ([pid... ]))... ,)
130132_sharedby (:: SharedStatsStep{T,I} ) where {T,I} = I
131133_f (s:: SharedStatsStep ) = _f (s. step)
132134_getargs (ntargs:: NamedTuple , s:: SharedStatsStep ) = _getargs (ntargs, s. step)
135+ _combinedargs (s:: SharedStatsStep , v:: AbstractArray ) = _combinedargs (s. step, v)
133136
134137show (io:: IO , s:: SharedStatsStep ) = print (io, s. step)
135138
@@ -143,7 +146,7 @@ const SharedStatsSteps = NTuple{N, SharedStatsStep} where N
143146const StatsProcedures = NTuple{N, AbstractStatsProcedure} where N
144147
145148"""
146- PooledStatsProcedure{P<:StatsProcedures,S<:SharedStatsSteps}
149+ PooledStatsProcedure{P<:StatsProcedures, S<:SharedStatsSteps}
147150
148151A collection of procedures and shared steps.
149152
@@ -155,7 +158,7 @@ See also [`pool`](@ref).
155158- `procs::P`: a tuple of instances of subtypes of [`AbstractStatsProcedure`](@ref).
156159- `steps::S`: a tuple of [`SharedStatsStep`](@ref) for the procedures in `procs`.
157160"""
158- struct PooledStatsProcedure{P<: StatsProcedures ,S<: SharedStatsSteps }
161+ struct PooledStatsProcedure{P<: StatsProcedures , S<: SharedStatsSteps }
159162 procs:: P
160163 steps:: S
161164end
@@ -274,7 +277,7 @@ function show(io::IO, ::MIME"text/plain", ps::PooledStatsProcedure{P,S}) where {
274277end
275278
276279"""
277- StatsSpec{Alias,T<:AbstractStatsProcedure}
280+ StatsSpec{Alias, T<:AbstractStatsProcedure}
278281
279282Record the specification for a statistical procedure of type `T`.
280283
@@ -297,7 +300,7 @@ or the last value returned by the last [`StatsStep`](@ref) is returned.
297300- `keep=nothing`: names (of type `Symbol`) of additional objects to be returned.
298301- `keepall::Bool=false`: return all objects returned by each step.
299302"""
300- struct StatsSpec{Alias,T<: AbstractStatsProcedure }
303+ struct StatsSpec{Alias, T<: AbstractStatsProcedure }
301304 args:: NamedTuple
302305 StatsSpec (name:: Union{Symbol,String} ,
303306 T:: Type{<:AbstractStatsProcedure} , args:: NamedTuple ) =
@@ -391,7 +394,7 @@ For end users, `Macro`s that generate `Expr`s for these function calls should be
391394
392395Optional default arguments are merged
393396with the arguments provided for each individual specification
394- and replace the default values specified for each procedure.
397+ and supersede the default values specified for each procedure through [`namedargs`](@ref) .
395398These default arguments should be specified in the same pattern as
396399how arguments are specified for each specification inside the code block,
397400as `@specset` processes these arguments by calling
@@ -470,12 +473,18 @@ function proceed(sps::AbstractVector{<:StatsSpec};
470473 taskids = vcat ((gids[steps. procs[i]] for i in _sharedby (step)). .. )
471474 tasks = groupview (r-> _getargs (r, step), view (traces, taskids))
472475 for (ins, subtb) in pairs (tasks)
473- ret = _f (step)(ins... )
476+ ret, share = _f (step)(ins... , _combinedargs (step, subtb) ... )
474477 ntask += 1
475478 ntask_total += 1
476479 if ret != = nothing
477- for i in eachindex (subtb)
478- subtb[i] = merge (subtb[i], deepcopy (ret))
480+ if share
481+ for i in eachindex (subtb)
482+ subtb[i] = merge (subtb[i], ret)
483+ end
484+ else
485+ for i in eachindex (subtb)
486+ subtb[i] = merge (subtb[i], deepcopy (ret))
487+ end
479488 end
480489 end
481490 end
0 commit comments