Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ablaom committed Oct 14, 2020
1 parent 4ab2762 commit e8653a4
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/model_def.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
clean!(::Model) = ""

"""
_process_model_def(ex)
_process_model_def(modl, ex)
Take an expression defining a model (`mutable struct Model ...`) and unpack key
elements for further processing:
Expand All @@ -17,12 +17,17 @@ elements for further processing:
- Names of parameters (`params`)
- Default values (`defaults`)
- Constraints (`constraints`)
"""
function _process_model_def(ex)
defaults = Dict{Symbol,Any}()
When no default field value is given a heuristic is to guess an
appropriate default (eg, zero for a `Float64` parameter). To this end,
the specified type expression is evaluated in the module `modl`.
"""
function _process_model_def(modl, ex)
defaults = Dict{Symbol,Any}()
constraints = Dict{Symbol,Any}()
modelname = ex.args[2] isa Symbol ? ex.args[2] : ex.args[2].args[1]
params = Symbol[]
modelname = ex.args[2] isa Symbol ? ex.args[2] : ex.args[2].args[1]
params = Symbol[]

# inspect all lines which may define parameters, retrieve their names,
# default values and constraints on values that can be given to them
Expand Down Expand Up @@ -67,7 +72,7 @@ function _process_model_def(ex)
# these are simple heuristics when no default value is given for the
# field but an "obvious" one can be provided implicitly (ideally this should
# not be used as it's not very clear that the intention matches the usage)
eff_type = eval(type)
eff_type = modl.eval(type)
if eff_type <: Number
defaults[param] = zero(eff_type)
elseif eff_type <: AbstractString
Expand Down Expand Up @@ -168,7 +173,7 @@ end
Macro to help define MLJ models with constraints on the default parameters.
"""
macro mlj_model(ex)
ex, modelname, params, defaults, constraints = _process_model_def(ex)
ex, modelname, params, defaults, constraints = _process_model_def(__module__, ex)
# keyword constructor
const_ex = _model_constructor(modelname, params, defaults)
# associate the constructor with the definition of the struct
Expand Down

0 comments on commit e8653a4

Please sign in to comment.