Skip to content

Commit 4999f95

Browse files
authored
schema interface (#39)
* `schema` interface * throw any input with LightInterface anyway * add tests
1 parent a5ca7c0 commit 4999f95

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/data_utils.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ classes(x) = classes(get_interface_mode(), x)
123123

124124
classes(::LightInterface, x) = errlight("classes")
125125

126+
# ------------------------------------------------------------------------
127+
# schema
128+
129+
"""
130+
schema(X)
131+
132+
If `X` is tabular, inspect the column types and scitypes, otherwise return
133+
`nothing`.
134+
"""
135+
schema(X; kw...) = schema(get_interface_mode(), vtrait(X), X; kw...)
136+
137+
schema(::Mode, ::Val{:other}, X; kw...) = nothing
138+
139+
schema(::LightInterface, ::Val{:other}, X; kw...) = errlight("schema")
140+
141+
schema(::LightInterface, ::Val{:table}, X; kw...) = errlight("schema")
142+
126143
# ------------------------------------------------------------------------
127144
# decoder
128145

test/data_utils.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ end
5858
@test classes(x[1]) == ['a', 'b']
5959
end
6060
# ------------------------------------------------------------------------
61+
@testset "schema-light" begin
62+
# throw error for any input anyway
63+
setlight()
64+
ary = rand(10, 3)
65+
@test_throws M.InterfaceError M.schema(ary)
66+
df = DataFrame(rand(10, 3))
67+
@test_throws M.InterfaceError M.schema(df)
68+
end
69+
@testset "schema-full" begin
70+
setfull()
71+
ary = rand(10, 3)
72+
@test M.schema(ary) === nothing
73+
M.schema(::FI, ::Val{:table}, X; kw...) = schema(X; kw...) # this would be defined in MLJBase.jl
74+
df = DataFrame(A = rand(10), B = categorical(rand('a':'c', 10)))
75+
sch = M.schema(df)
76+
@test sch.names == (:A, :B)
77+
@test sch.types[1] <: Float64
78+
@test sch.types[2] <: CategoricalValue
79+
@test sch.scitypes[1] <: Continuous
80+
@test sch.scitypes[2] <: Multiclass
81+
end
82+
# ------------------------------------------------------------------------
6183
@testset "decoder-light" begin
6284
setlight()
6385
x = 5

0 commit comments

Comments
 (0)