Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flat_params hotfix respect to empty models #179

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/parameter_inspection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function params(m, ::Val{true})
return NamedTuple{fields}(Tuple([params(getfield(m, field)) for field in fields]))
end

isamodel(::Any) = false
isamodel(::Model) = true
isnotaleaf(::Any) = false
isnotaleaf(m::Model) = length(propertynames(m)) > 0

"""
flat_params(m::Model)
Expand All @@ -53,7 +53,7 @@ not a hard requirement.
parallel = true,)

"""
flat_params(m; prefix="") = flat_params(m, Val(isamodel(m)); prefix=prefix)
flat_params(m; prefix="") = flat_params(m, Val(isnotaleaf(m)); prefix=prefix)
flat_params(m, ::Val{false}; prefix="") = NamedTuple{(Symbol(prefix),), Tuple{Any}}((m,))
function flat_params(m, ::Val{true}; prefix="")
fields = propertynames(m)
Expand Down
16 changes: 9 additions & 7 deletions test/parameter_inspection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ end
end

struct ChildModel <: Model
x::Int
y::String
r::Int
s
end

struct ParentModel <: Model
Expand All @@ -46,18 +46,20 @@ struct ParentModel <: Model
second_child::ChildModel
end

struct Missy <: Model end

@testset "flat_params method" begin

m = ParentModel(1, "parent", ChildModel(2, "child1"),
ChildModel(3, "child2"))
ChildModel(3, Missy()))

@test MLJModelInterface.flat_params(m) == (
x = 1,
y = "parent",
first_child__x = 2,
first_child__y = "child1",
second_child__x = 3,
second_child__y = "child2"
first_child__r = 2,
first_child__s = "child1",
second_child__r = 3,
second_child__s = Missy()
)
end
true
Loading