Skip to content

Commit

Permalink
Make modelcols(::Term, d) an error (#137)
Browse files Browse the repository at this point in the history
* reproducer for #136

* throw argument error for modelcols on Term

* (broken) test for lack of fallback

* fallback guard in modelcols(::Any, ::Any)

* bump version (patch)

* add comment in source about fallback error
  • Loading branch information
kleinschmidt committed Aug 2, 2019
1 parent 0ba1278 commit 91efc7a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
@@ -1,6 +1,6 @@
name = "StatsModels"
uuid = "3eaba693-59b7-5ba5-a881-562e759f1c8d"
version = "0.6.2"
version = "0.6.3"

[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Expand Down
11 changes: 11 additions & 0 deletions src/terms.jl
Expand Up @@ -419,6 +419,13 @@ converted to a `NamedTuple` of `Vectors` (e.g., a `Tables.ColumnTable`).
"""
function modelcols(t, d::D) where D
Tables.istable(d) || throw(ArgumentError("Data of type $D is not a table!"))
## throw an error for t which don't have a more specific modelcols method defined
## TODO: this seems like it ought to be handled by dispatching on something
## like modelcols(::Any, ::NamedTuple) or modelcols(::AbstractTerm, ::NamedTuple)
## but that causes ambiguity errors or under-constrained modelcols methods for
## custom term types...
d isa NamedTuple && throw(ArgumentError("don't know how to generate modelcols for " *
"term $t. Did you forget to call apply_schema?"))
modelcols(t, columntable(d))
end

Expand Down Expand Up @@ -468,6 +475,10 @@ julia> modelcols(MatrixTerm(ts), d)
"""
modelcols(ts::TupleTerm, d::NamedTuple) = modelcols.(ts, Ref(d))

modelcols(t::Term, d::NamedTuple) =
throw(ArgumentError("can't generate modelcols for un-typed term $t. " *
"Use apply_schema to create concrete terms first"))

# TODO: @generated to unroll the getfield stuff
modelcols(ft::FunctionTerm{Fo,Fa,Names}, d::NamedTuple) where {Fo,Fa,Names} =
ft.fanon.(getfield.(Ref(d), Names)...)
Expand Down
7 changes: 6 additions & 1 deletion test/extension.jl
Expand Up @@ -24,6 +24,9 @@ StatsModels.apply_schema(t::NonMatrixTerm, sch, Mod::Type) =
NonMatrixTerm(apply_schema(t.term, sch, Mod))
StatsModels.modelcols(t::NonMatrixTerm, d) = modelcols(t.term, d)

struct DummyTerm <: AbstractTerm
end

@testset "Extended formula/models" begin
d = (z = rand(10), y = rand(10), x = collect(1:10))
sch = schema(d)
Expand Down Expand Up @@ -87,5 +90,7 @@ StatsModels.modelcols(t::NonMatrixTerm, d) = modelcols(t.term, d)

end

@testset "Fallback" begin
@test_throws ArgumentError modelcols(DummyTerm(), (a=[1], ))
end
end

6 changes: 6 additions & 0 deletions test/modelmatrix.jl
Expand Up @@ -374,4 +374,10 @@
end
end

@testset "#136" begin
t = (x = rand(100), y = randn(100));
f = @formula(y ~ x)
@test_throws ArgumentError modelcols(f, t)
end

end

2 comments on commit 91efc7a

@kleinschmidt
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/2472

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.3 -m "<description of version>" 91efc7a002a1d758bef6c7e1adc522a5124007b2
git push origin v0.6.3

Please sign in to comment.